Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 19 additions & 30 deletions Abstracts/CustomAncientModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace BaseLib.Abstracts;

public abstract class CustomAncientModel : AncientEventModel, ICustomModel
public abstract class CustomAncientModel : AncientEventModel, ICustomModel, ILocalizationProvider
{
//Suggested overrides: ButtonColor, DialogueColor
private readonly bool _logDialogueLoad;
Expand All @@ -20,6 +20,8 @@ public CustomAncientModel(bool autoAdd = true, bool logDialogueLoad = false)
if (autoAdd) CustomContentDictionary.AddAncient(this);
_logDialogueLoad = logDialogueLoad;
}

public virtual List<(string, string)>? Localization => null;

/// <summary>
/// Suggested to check act.ActNumber == 2 or 3.
Expand Down Expand Up @@ -82,7 +84,7 @@ public static AncientOption AncientOption<T>(int weight = 1, Func<T, RelicModel>
/****************** Assets ******************/

/// <summary>
/// Override to load custom event scene.
/// Overridden to load custom event scene.
/// </summary>
/// <param name="runState"></param>
/// <returns></returns>
Expand All @@ -100,8 +102,8 @@ public override IEnumerable<string> GetAssetPaths(IRunState runState)
public virtual string? CustomMapIconPath => null;
public virtual string? CustomMapIconOutlinePath => null;

public virtual Texture2D? CustomRunHistoryIcon => null;
public virtual Texture2D? CustomRunHistoryIconOutline => null;
public virtual string? CustomRunHistoryIconPath => null;
public virtual string? CustomRunHistoryIconOutlinePath => null;


/****************** Localization ******************/
Expand All @@ -126,63 +128,50 @@ protected override AncientDialogueSet DefineDialogues()
{
FirstVisitEverDialogue = firstVisit,
CharacterDialogues = characterDialogues,
AgnosticDialogues = AncientDialogueUtil.GetDialoguesForKey("ancients", "ANY", log)
AgnosticDialogues = AncientDialogueUtil.GetDialoguesForKey("ancients", AncientDialogueUtil.BaseLocKey(Id.Entry, "ANY"), log)
};
if (log != null) MainFile.Logger.Info(log.ToString());
if (log != null) BaseLibMain.Logger.Info(log.ToString());
return dialogueSet;
}
}

[HarmonyPatch(typeof(AncientEventModel), "MapIconPath", MethodType.Getter)]
class MapIconPath
[HarmonyPatch(typeof(EventModel), "BackgroundScenePath", MethodType.Getter)]
class BackgroundScenePath
{
[HarmonyPrefix]
static bool Custom(AncientEventModel __instance, ref string? __result)
{
if (__instance is not CustomAncientModel custom)
return true;

__result = custom.CustomMapIconPath;
__result = custom.CustomScenePath;
return __result == null;
}
}
[HarmonyPatch(typeof(AncientEventModel), "MapIconOutlinePath", MethodType.Getter)]
class MapIconOutlinePath
{
[HarmonyPrefix]
static bool Custom(AncientEventModel __instance, ref string? __result)
{
if (__instance is not CustomAncientModel custom)
return true;

__result = custom.CustomMapIconOutlinePath;
return __result == null;
}
}

[HarmonyPatch(typeof(AncientEventModel), "RunHistoryIcon", MethodType.Getter)]
class RunHistoryIcon
[HarmonyPatch(typeof(AncientEventModel), "MapIconPath", MethodType.Getter)]
class MapIconPath
{
[HarmonyPrefix]
static bool Custom(AncientEventModel __instance, ref Texture2D? __result)
static bool Custom(AncientEventModel __instance, ref string? __result)
{
if (__instance is not CustomAncientModel custom)
return true;

__result = custom.CustomRunHistoryIcon;
__result = custom.CustomMapIconPath;
return __result == null;
}
}
[HarmonyPatch(typeof(AncientEventModel), "RunHistoryIconOutline", MethodType.Getter)]
class RunHistoryIconOutline
[HarmonyPatch(typeof(AncientEventModel), "MapIconOutlinePath", MethodType.Getter)]
class MapIconOutlinePath
{
[HarmonyPrefix]
static bool Custom(AncientEventModel __instance, ref Texture2D? __result)
static bool Custom(AncientEventModel __instance, ref string? __result)
{
if (__instance is not CustomAncientModel custom)
return true;

__result = custom.CustomRunHistoryIconOutline;
__result = custom.CustomMapIconOutlinePath;
return __result == null;
}
}
3 changes: 2 additions & 1 deletion Abstracts/CustomCardModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace BaseLib.Abstracts;

public abstract class CustomCardModel : CardModel, ICustomModel
public abstract class CustomCardModel : CardModel, ICustomModel, ILocalizationProvider
{
/// <summary>
/// For convenience; can be manually overridden if necessary.
Expand All @@ -23,6 +23,7 @@ public CustomCardModel(int baseCost, CardType type, CardRarity rarity, TargetTyp

public virtual Texture2D? CustomFrame => null;
public virtual string? CustomPortraitPath => null;
public virtual List<(string, string)>? Localization => null;
}

[HarmonyPatch(typeof(CardModel), nameof(CardModel.Frame), MethodType.Getter)]
Expand Down
12 changes: 9 additions & 3 deletions Abstracts/CustomCharacterModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,18 @@

namespace BaseLib.Abstracts;

public abstract class CustomCharacterModel : CharacterModel, ICustomModel
public abstract class CustomCharacterModel : CharacterModel, ICustomModel, ILocalizationProvider
{
public CustomCharacterModel()
{
ModelDbCustomCharacters.Register(this);
}

/// <summary>
/// Override this to define localization directly in your class.
/// You are recommended to return a CharacterLoc<seealso cref="CharacterLoc"/>.
/// </summary>
public virtual List<(string, string)>? Localization => null;

/// <summary>
/// Override this or place your scene at res://scenes/creature_visuals/class_name.tscn
Expand Down Expand Up @@ -213,10 +219,10 @@ static bool Prefix(Player player, ref NEnergyCounter? __result) {
}
catch (Exception e)
{
MainFile.Logger.Error($"Failed to create custom energy counter for {player.Character.Id}: {e}");
BaseLibMain.Logger.Error($"Failed to create custom energy counter for {player.Character.Id}: {e}");
}

MainFile.Logger.Info($"Player {model.GetType().Name} does not have a custom NEnergyCounter.");
BaseLibMain.Logger.Info($"Player {model.GetType().Name} does not have a custom NEnergyCounter.");

return true;
}
Expand Down
106 changes: 106 additions & 0 deletions Abstracts/CustomEncounterModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
using System.Collections.Generic;
using Godot;
using HarmonyLib;
using MegaCrit.Sts2.Core.Assets;
using MegaCrit.Sts2.Core.Helpers;
using MegaCrit.Sts2.Core.Models;
using MegaCrit.Sts2.Core.Nodes.Rooms;
using MegaCrit.Sts2.Core.Random;
using MegaCrit.Sts2.Core.Rooms;
using MegaCrit.Sts2.Core.Runs;

namespace BaseLib.Abstracts;

public abstract class CustomEncounterModel : EncounterModel, ICustomModel
{
protected override bool HasCustomBackground => false;
public virtual string? CustomBackgroundScenePath => null;
public virtual string AssetsName => Id.Entry.ToLowerInvariant();
public virtual bool UseVanillaBackground => false;
private string CustomBackgroundScenePathFallback => SceneHelper.GetScenePath($"backgrounds/{AssetsName}/{AssetsName}_background");
public virtual NCombatBackground CreateCustomBackground(ActModel parentAct, Rng rng) {
if(UseVanillaBackground)
return NCombatBackground.Create(CreateBackgroundAssetsForCustom(rng));
return CreateNCombatBackground(CustomBackgroundScenePath);
}
public NCombatBackground CreateNCombatBackground(string? path)
{
Control control = PreloadManager.Cache.GetScene(path ?? CustomBackgroundScenePathFallback).Instantiate<Control>();
NCombatBackground? ncombatBackground = control as NCombatBackground;
if(ncombatBackground != null)
return ncombatBackground;
ncombatBackground = new NCombatBackground();
AddCustomLayer(ncombatBackground,control);
return ncombatBackground;
}

private static void AddCustomLayer(NCombatBackground bg, Control layer) {
layer.Visible = true;
bg.AddChildSafely(layer);
}

public IEnumerable<string> GetAssetPaths(IRunState runState)
{
HashSet<string> assetPaths = new HashSet<string>();
if (this.HasScene) {
string ScenePath = Traverse.Create(this).Property<string>("ScenePath").Value;
assetPaths.Add(CustomScenePath ?? ScenePath);
}
if (this.ExtraAssetPaths != null)
assetPaths.UnionWith(this.ExtraAssetPaths);
foreach ((MonsterModel monsterModel, string _) in (IEnumerable<(MonsterModel, string)>) this.MonstersWithSlots)
assetPaths.UnionWith(monsterModel.AssetPaths);
return (IEnumerable<string>) assetPaths;
}
private BackgroundAssets CreateBackgroundAssetsForCustom(Rng rng)//CreateBackgroundAssetsForCustom
{
return new BackgroundAssets(AssetsName, rng);//TODO create from custom path
}

public override bool HasScene => false;
public virtual string? CustomScenePath => null;
public virtual Control CreateCustomScene()
{
string ScenePath = Traverse.Create(this).Property<string>("ScenePath").Value;
return PreloadManager.Cache.GetScene(CustomScenePath ?? ScenePath).Instantiate<Control>();
}

[HarmonyPatch(typeof(EncounterModel), nameof(EncounterModel.CreateScene))]
private static class ScenePatch {
static bool Prefix(EncounterModel __instance, ref Control __result) {
CustomEncounterModel? model = __instance as CustomEncounterModel;
if (__instance is not CustomEncounterModel)
return true;
__result = model!.CreateCustomScene();
return false;
}
}

[HarmonyPatch(typeof(EncounterModel), nameof(EncounterModel.GetAssetPaths))]
private static class AssetPathsPatch {
[HarmonyPrefix]
static bool Prefix(EncounterModel __instance, ref IEnumerable<string> __result, IRunState runState) {
CustomEncounterModel? model = __instance as CustomEncounterModel;
if (__instance is not CustomEncounterModel)
return true;
if (model!.UseVanillaBackground)
return true;
__result = model.GetAssetPaths(runState);
return false;
}
}
[HarmonyPatch(typeof(EncounterModel), nameof(EncounterModel.CreateBackground))]
private static class BackgroundPatch {
[HarmonyPrefix]
static bool Prefix(EncounterModel __instance, ref NCombatBackground __result, ActModel parentAct, Rng rng) {
CustomEncounterModel? model = __instance as CustomEncounterModel;
if (__instance is not CustomEncounterModel)
return true;
bool HasCustomBackground = Traverse.Create(model).Property<bool>("HasCustomBackground").Value;
if(!HasCustomBackground)
return true;
__result = model!.CreateCustomBackground(parentAct, rng);
return false;
}
}
}
Loading