Skip to content

Commit

Permalink
Adds an ItemChanged event to ConfigSections and fixes its FullId
Browse files Browse the repository at this point in the history
  • Loading branch information
Banane9 committed Jul 31, 2024
1 parent 7826471 commit 8149586
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 11 deletions.
6 changes: 4 additions & 2 deletions MonkeyLoader/Configuration/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public sealed class Config : INestedIdentifiable<IConfigOwner>,
/// </summary>
public IEnumerable<IDefiningConfigKey> ConfigurationItemDefinitions => _configurationItemDefinitionsSelfMap.Values.AsSafeEnumerable();

string IIdentifiable.FullId => $"{Owner.FullId}.Config";
/// <inheritdoc/>
public string FullId { get; }

string IIdentifiable.Id => "Config";

Expand Down Expand Up @@ -82,6 +83,7 @@ public object? this[IConfigKey key]
internal Config(IConfigOwner owner)
{
Owner = owner;
FullId = $"{Owner.FullId}.Config";
Logger = new Logger(owner.Logger, "Config");

_loadedConfig = LoadConfig();
Expand Down Expand Up @@ -356,7 +358,7 @@ internal void OnItemChanged(IConfigKeyChangedEventArgs configKeyChangedEventArgs
}
catch (AggregateException ex)
{
Logger.Error(() => ex.Format($"Some {nameof(ItemChanged)} event subscriber(s) threw an exception:"));
Logger.Error(() => ex.Format($"Some Config.{nameof(ItemChanged)} event subscriber(s) threw an exception:"));
}

Owner.Loader.OnAnyConfigChanged(configKeyChangedEventArgs);
Expand Down
42 changes: 36 additions & 6 deletions MonkeyLoader/Configuration/ConfigSection.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using HarmonyLib;
using MonkeyLoader.Logging;
using MonkeyLoader.Meta;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
Expand Down Expand Up @@ -78,7 +79,7 @@ public abstract class ConfigSection : INestedIdentifiable<Config>, IIdentifiable
/// </summary>
public virtual string Name => Id;

IIdentifiable INestedIdentifiable.Parent => Config.Owner;
IIdentifiable INestedIdentifiable.Parent => Config;

Config INestedIdentifiable<Config>.Parent => Config;

Expand All @@ -100,12 +101,17 @@ public abstract class ConfigSection : INestedIdentifiable<Config>, IIdentifiable
/// </summary>
protected virtual IncompatibleConfigHandling IncompatibilityHandling => IncompatibleConfigHandling.Error;

/// <summary>
/// Gets the logger of the config this section belongs to.
/// </summary>
private Logger Logger => Config.Logger;

/// <summary>
/// Creates a new config section instance.
/// </summary>
protected ConfigSection()
{
_fullId = new(() => $"{Config.Owner.Id}.{Id}");
_fullId = new(() => $"{Config.FullId}.{Id}");
}

/// <summary>
Expand All @@ -124,15 +130,15 @@ protected ConfigSection()
/// <param name="right">The second section.</param>
/// <returns><c>true</c> if they're considered equal.</returns>
public static bool operator ==(ConfigSection? left, ConfigSection? right)
=> ReferenceEquals(left, right)
|| (left is not null && right is not null && left.Id == right.Id);
=> left?.FullId == right?.FullId;

/// <summary>
/// Checks if the given object can be considered equal to this one.
/// </summary>
/// <param name="obj">The other object.</param>
/// <returns><c>true</c> if the other object is considered equal.</returns>
public override bool Equals(object obj) => obj is ConfigSection section && section == this;
public override bool Equals(object obj)
=> obj is ConfigSection section && section == this;

/// <summary>
/// Gets the <see cref="IDefiningConfigKey"/> defined in this config section,
Expand Down Expand Up @@ -167,6 +173,25 @@ public IDefiningConfigKey<T> GetDefinedKey<T>(IDefiningConfigKey<T> templateKey)
/// <inheritdoc/>
public override int GetHashCode() => Id.GetHashCode();

/// <summary>
/// Invokes this config section's <see cref="ItemChanged">ItemChanged</see> event,
/// and passes the invocation on to the <see cref="Config"/> and <see cref="MonkeyLoader"/> it belongs to.
/// </summary>
/// <param name="configKeyChangedEventArgs">The data for the event.</param>
public void OnItemChanged(IConfigKeyChangedEventArgs configKeyChangedEventArgs)
{
try
{
ItemChanged?.TryInvokeAll(this, configKeyChangedEventArgs);
}
catch (AggregateException ex)
{
Logger.Error(() => ex.Format($"Some ConfigSection.{nameof(ItemChanged)} event subscriber(s) threw an exception:"));
}

Config.OnItemChanged(configKeyChangedEventArgs);
}

/// <summary>
/// Determines if this config section contains an item matching the <paramref name="typedTemplateKey"/>
/// and returns the optional match as <paramref name="definingKey"/>.
Expand Down Expand Up @@ -378,7 +403,7 @@ private void ValidateCompatibility(Version serializedVersion)
switch (IncompatibilityHandling)
{
case IncompatibleConfigHandling.Clobber:
Config.Logger.Warn(() => $"Saved section [{Id}] version [{serializedVersion}] is incompatible with mod's version [{Version}]. Clobbering old config and starting fresh.");
Logger.Warn(() => $"Saved section [{Id}] version [{serializedVersion}] is incompatible with mod's version [{Version}]. Clobbering old config and starting fresh.");
return;

case IncompatibleConfigHandling.ForceLoad:
Expand All @@ -392,5 +417,10 @@ private void ValidateCompatibility(Version serializedVersion)
}
}
}

/// <summary>
/// Called when the value of one of this config's items gets changed.
/// </summary>
public event ConfigKeyChangedEventHandler? ItemChanged;
}
}
14 changes: 11 additions & 3 deletions MonkeyLoader/Configuration/DefiningConfigKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public ConfigSection Section
public Type ValueType { get; } = typeof(T);

/// <summary>
/// Gets the logger of the config this item belongs to if it's a <see cref="IsDefiningKey">defining key</see>.
/// Gets the logger of the config this item belongs to.
/// </summary>
private Logger Logger => Section.Config.Logger;

Expand Down Expand Up @@ -322,7 +322,15 @@ public bool Unset()
/// <inheritdoc/>
public bool Validate(T value)
=> Components.GetAll<IConfigKeyValidator<T>>()
.All(validator => validator.IsValid(value));
.All(validator =>
{
var valid = validator.IsValid(value);

if (!valid)
Logger.Debug(() => $"Validator [{validator}] of key [{Id}] didn't validate value: {value}");

return valid;
});

/// <inheritdoc/>
bool IDefiningConfigKey.Validate(object? value) => Validate(value);
Expand Down Expand Up @@ -385,7 +393,7 @@ private void OnChanged(bool hadValue, T? oldValue, string? eventLabel, string? c
Logger.Error(() => ex.Format($"Some untyped {nameof(Changed)} event subscriber(s) of key [{Id}] threw an exception:"));
}

Config.OnItemChanged(eventArgs);
Section.OnItemChanged(eventArgs);
}

private bool Validate(object? value)
Expand Down

0 comments on commit 8149586

Please sign in to comment.