Skip to content

Commit

Permalink
Track when tags and modules are loaded, add automatic loading for new…
Browse files Browse the repository at this point in the history
… tags, prevent duplicate tag/module loading.
  • Loading branch information
homothetyhk committed Aug 26, 2022
1 parent 79e6698 commit f65b583
Show file tree
Hide file tree
Showing 20 changed files with 178 additions and 30 deletions.
30 changes: 10 additions & 20 deletions ItemChanger/Internal/ModuleCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,25 @@ public class ModuleCollection

public void Initialize()
{
foreach (Module m in Modules)
for (int i = 0; i < Modules.Count; i++)
{
try
{
m.Initialize();
}
catch (Exception e)
{
LogError($"Error initializing module {m.Name}:\n{e}");
}
Modules[i].LoadOnce();
}
}

public void Unload()
{
foreach (Module m in Modules)
for (int i = 0; i < Modules.Count; i++)
{
try
{
m.Unload();
}
catch (Exception e)
{
LogError($"Error unloading module {m.Name}:\n{e}");
}
Modules[i].UnloadOnce();
}
}

public Module Add(Module m)
{
if (m == null) throw new ArgumentNullException(nameof(m));
Modules.Add(m);
if (Settings.loaded) m.Initialize();
if (Settings.loaded) m.LoadOnce();
return m;
}

Expand Down Expand Up @@ -90,7 +76,7 @@ public Module GetOrAdd(Type T)

public void Remove(Module m)
{
if (Modules.Remove(m) && Settings.loaded) m.Unload();
if (Modules.Remove(m) && Settings.loaded) m.UnloadOnce();
}

public void Remove<T>()
Expand All @@ -100,6 +86,10 @@ public void Remove<T>()

public void Remove(Type T)
{
if (Settings.loaded)
{
foreach (Module m in Modules.Where(m => m.GetType() == T)) m.UnloadOnce();
}
Modules.RemoveAll(m => m.GetType() == T);
}

Expand Down
42 changes: 42 additions & 0 deletions ItemChanger/Modules/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,48 @@ public abstract class Module
public string Name => GetType().Name;
public abstract void Initialize();
public abstract void Unload();

/// <summary>
/// Initializes the module and sets the Loaded property. If the module is already loaded, does nothing.
/// <br/>Preferred to "Initialize", which does not set the Loaded property.
/// </summary>
public void LoadOnce()
{
if (!Loaded)
{
try
{
Initialize();
}
catch (Exception e)
{
LogError($"Error initializing module {Name}:\n{e}");
}
Loaded = true;
}
}

/// <summary>
/// Unloads the module and sets the Loaded property. If the module is not loaded, does nothing.
/// <br/>Preferred to "Unload", which does not set the Loaded property.
/// </summary>
public void UnloadOnce()
{
if (Loaded)
{
try
{
Unload();
}
catch (Exception e)
{
LogError($"Error unloading module {Name}:\n{e}");
}
Loaded = false;
}
}

public bool Loaded { get; private set; }
}

/// <summary>
Expand Down
62 changes: 60 additions & 2 deletions ItemChanger/Tag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,66 @@
{
public abstract class Tag
{
public virtual void Load(object parent) { }
public virtual void Unload(object parent) { }
/// <summary>
/// Virtual method called on tags when their parent loads. The base method checks and throws an exception if the tag is already loaded.
/// <br/>This should not be called directly. Instead, use "LoadOnce" to load and set the Loaded property.
/// </summary>
/// <exception cref="InvalidOperationException">The tag is already loaded.</exception>
public virtual void Load(object parent)
{
if (Loaded) throw new InvalidOperationException($"Tag {GetType().Name} is already loaded.");
}
/// <summary>
/// Virtual method called on tags when their parent unloads. The base method checks and throws an exception if the tag is not loaded.
/// <br/>This should not be called directly. Instead, use "UnloadOnce" to unload and set the Loaded property.
/// </summary>
/// <exception cref="InvalidOperationException">The tag is not loaded.</exception>
public virtual void Unload(object parent)
{
if (!Loaded) throw new InvalidOperationException($"Tag {GetType().Name} was not loaded.");
}
public virtual Tag Clone() => (Tag)MemberwiseClone();

/// <summary>
/// Loads the tag and sets the Loaded property. If the tag is already loaded, does nothing.
/// <br/>Preferred to "Load", which does not set the Loaded property.
/// </summary>
public void LoadOnce(TaggableObject parent)
{
if (!Loaded)
{
try
{
Load(parent);
}
catch (Exception e)
{
LogError($"Error loading {GetType().Name}:\n{e}");
}
Loaded = true;
}
}

/// <summary>
/// Unloads the tag and sets the Loaded property. If the tag is not loaded, does nothing.
/// <br/>Preferred to "Unload", which does not set the Loaded property.
/// </summary>
public void UnloadOnce(TaggableObject parent)
{
if (Loaded)
{
try
{
Unload(parent);
}
catch (Exception e)
{
LogError($"Error unloading {GetType().Name}:\n{e}");
}
Loaded = false;
}
}

public bool Loaded { get; private set; }
}
}
37 changes: 30 additions & 7 deletions ItemChanger/TaggableObject.cs
Original file line number Diff line number Diff line change
@@ -1,33 +1,52 @@
namespace ItemChanger
using Newtonsoft.Json;

namespace ItemChanger
{
public class TaggableObject
{
[Newtonsoft.Json.JsonProperty]
public List<Tag> tags;
[JsonProperty] public List<Tag> tags;
private bool _tagsLoaded;

protected void LoadTags()
{
_tagsLoaded = true;
if (tags == null) return;
foreach (Tag tag in tags) tag.Load(this);
for (int i = 0; i < tags.Count; i++)
{
tags[i].LoadOnce(this);
}
}

protected void UnloadTags()
{
_tagsLoaded = false;
if (tags == null) return;
foreach (Tag tag in tags) tag.Unload(this);
for (int i = 0; i < tags.Count; i++)
{
tags[i].UnloadOnce(this);
}
}

public T AddTag<T>() where T : Tag, new()
{
if (tags == null) tags = new List<Tag>();
T t = new T();
T t = new();
if (_tagsLoaded) t.LoadOnce(this);
tags.Add(t);
return t;
}

public void AddTag(Tag t)
{
if (tags == null) tags = new();
if (_tagsLoaded) t.LoadOnce(this);
tags.Add(t);
}

public void AddTags(IEnumerable<Tag> ts)
{
if (tags == null) tags = new List<Tag>();
if (_tagsLoaded) foreach (Tag t in ts) t.LoadOnce(this);
tags.AddRange(ts);
}

Expand Down Expand Up @@ -61,7 +80,11 @@ public bool HasTag<T>() where T : Tag

public void RemoveTags<T>()
{
tags = tags?.Where(t => !(t is T))?.ToList();
if (_tagsLoaded && tags != null)
{
foreach (Tag t in tags.Where(t => t is T)) t.UnloadOnce(this);
}
tags = tags?.Where(t => t is not T)?.ToList();
}
}
}
2 changes: 2 additions & 0 deletions ItemChanger/Tags/CostTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ public class CostTag : Tag
public Cost Cost { get; set; }
public override void Load(object parent)
{
base.Load(parent);
Cost?.Load();
}
public override void Unload(object parent)
{
base.Unload(parent);
Cost?.Unload();
}
}
Expand Down
2 changes: 2 additions & 0 deletions ItemChanger/Tags/DestroyFsmTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ public class DestroyFsmTag : Tag

public override void Load(object parent)
{
base.Load(parent);
if (sceneName == null) Events.AddFsmEdit(id, Destroy);
else Events.AddFsmEdit(sceneName, id, Destroy);
}

public override void Unload(object parent)
{
base.Unload(parent);
if (sceneName == null) Events.RemoveFsmEdit(id, Destroy);
else Events.RemoveFsmEdit(sceneName, id, Destroy);
}
Expand Down
2 changes: 2 additions & 0 deletions ItemChanger/Tags/DestroyGrubRewardTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ public class DestroyGrubRewardTag : Tag

public override void Load(object parent)
{
base.Load(parent);
Events.AddSceneChangeEdit(SceneNames.Crossroads_38, DestroyGrubRewards);
}

public override void Unload(object parent)
{
base.Unload(parent);
Events.RemoveSceneChangeEdit(SceneNames.Crossroads_38, DestroyGrubRewards);
}

Expand Down
2 changes: 2 additions & 0 deletions ItemChanger/Tags/DestroyObjectTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ public class DestroyObjectTag : Tag

public override void Load(object parent)
{
base.Load(parent);
Events.AddSceneChangeEdit(sceneName, DestroyObject);
}

public override void Unload(object parent)
{
base.Unload(parent);
Events.RemoveSceneChangeEdit(sceneName, DestroyObject);
}

Expand Down
2 changes: 2 additions & 0 deletions ItemChanger/Tags/DestroyOnECLReplaceTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ public class DestroyOnECLReplaceTag : Tag

public override void Load(object parent)
{
base.Load(parent);
location = (ExistingContainerLocation)parent;
Events.AddSceneChangeEdit(sceneName, OnSceneChange);
}

public override void Unload(object parent)
{
base.Unload(parent);
Events.RemoveSceneChangeEdit(sceneName, OnSceneChange);
}

Expand Down
3 changes: 2 additions & 1 deletion ItemChanger/Tags/DestroySeerRewardTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class DestroySeerRewardTag : Tag

public override void Load(object parent)
{
base.Load(parent);
for (int i = 0; i < 5; i++)
{
if ((destroyRewards & (SeerRewards)(1 << i)) != 0) PlayerData.instance.SetBool($"dreamReward{i + 1}", true);
Expand All @@ -22,7 +23,7 @@ public override void Load(object parent)

public override void Unload(object parent)
{

base.Unload(parent);
}

}
Expand Down
2 changes: 2 additions & 0 deletions ItemChanger/Tags/DisableFsmTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ public class DisableFsmTag : Tag

public override void Load(object parent)
{
base.Load(parent);
if (sceneName == null) Events.AddFsmEdit(id, Disable);
else Events.AddFsmEdit(sceneName, id, Disable);
}

public override void Unload(object parent)
{
base.Unload(parent);
if (sceneName == null) Events.RemoveFsmEdit(id, Disable);
else Events.RemoveFsmEdit(sceneName, id, Disable);
}
Expand Down
8 changes: 8 additions & 0 deletions ItemChanger/Tags/EquipCharmOnGiveTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,19 @@ public class EquipCharmOnGiveTag : Tag

public override void Load(object parent)
{
base.Load(parent);
Items.CharmItem charm = (Items.CharmItem)parent;
charmNum = charm.charmNum;
charm.AfterGive += AfterGiveItem;
}

public override void Unload(object parent)
{
base.Unload(parent);
Items.CharmItem charm = (Items.CharmItem)parent;
charm.AfterGive -= AfterGiveItem;
}

public void AfterGiveItem(ReadOnlyGiveEventArgs args)
{
if (PlayerData.instance.GetBool(equipBool)) return;
Expand Down
2 changes: 2 additions & 0 deletions ItemChanger/Tags/ImplicitCostTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ public class ImplicitCostTag : Tag

public override void Load(object parent)
{
base.Load(parent);
Cost?.Load();
}

public override void Unload(object parent)
{
base.Unload(parent);
Cost?.Unload();
}
}
Expand Down
1 change: 1 addition & 0 deletions ItemChanger/Tags/IncompatibilityWarningTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class IncompatibilityWarningTag : Tag
public string IncompatiblePlacementName;
public override void Load(object parent)
{
base.Load(parent);
string parentPlacementName = parent switch
{
AbstractPlacement parentPlacement => parentPlacement.Name,
Expand Down
Loading

0 comments on commit f65b583

Please sign in to comment.