Skip to content

Commit

Permalink
Add Reset method to config/section/keys
Browse files Browse the repository at this point in the history
  • Loading branch information
Banane9 committed Aug 24, 2024
1 parent edaf7f6 commit 7e86f8c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
11 changes: 11 additions & 0 deletions MonkeyLoader/Configuration/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,17 @@ public TSection LoadSection<TSection>(TSection section) where TSection : ConfigS
return section;
}

/// <summary>
/// Removes the value of all of this configuration's sections' keys and resets them to their default.
/// </summary>
public void Reset()
{
Logger.Warn(() => $"Resetting config: {FullId}");

foreach (var section in Sections)
section.Reset();
}

/// <summary>
/// Persists this configuration to disk.
/// </summary>
Expand Down
11 changes: 11 additions & 0 deletions MonkeyLoader/Configuration/ConfigSection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,17 @@ public void OnItemChanged(IConfigKeyChangedEventArgs configKeyChangedEventArgs)
Config.OnItemChanged(configKeyChangedEventArgs);
}

/// <summary>
/// Removes the value of all keys of this config section and resets them to their default.
/// </summary>
public void Reset()
{
Logger.Warn(() => $"Resetting config section: {FullId}");

foreach (var configKey in keys)
configKey.Reset();
}

/// <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
18 changes: 18 additions & 0 deletions MonkeyLoader/Configuration/DefiningConfigKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,19 @@ IEnumerator<IComponent<IDefiningConfigKey<T>>> IEnumerable<IComponent<IDefiningC

object? IDefiningConfigKey.GetValue() => GetValue();

/// <inheritdoc/>
public void Reset()
{
Logger.Info(() => $"Resetting config key: {FullId}");

var hadValue = HasValue;
var oldValue = _value;

HasValue = TryComputeDefault(out _value);

OnChanged(hadValue, oldValue, nameof(Reset));
}

/// <inheritdoc/>
public void SetValue(T value, string? eventLabel = null)
{
Expand Down Expand Up @@ -473,6 +486,11 @@ public interface IDefiningConfigKey : ITypedConfigKey, IEntity<IDefiningConfigKe
/// <returns>The item's internal value or its <see cref="ITypedConfigKey.ValueType">type's</see> <c>default</c>.</returns>
public object? GetValue();

/// <summary>
/// Removes this config item's value and attempts to reset it back to its default.
/// </summary>
public void Reset();

/// <summary>
/// Set the config item's internal value to the given one.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion MonkeyLoader/MonkeyLoader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Title>MonkeyLoader</Title>
<Authors>Banane9</Authors>
<Version>0.22.6-beta</Version>
<Version>0.22.7-beta</Version>
<Description>A convenience and extendability focused mod loader using NuGet packages.</Description>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageLicenseExpression>LGPL-3.0-or-later</PackageLicenseExpression>
Expand Down

0 comments on commit 7e86f8c

Please sign in to comment.