Skip to content

Commit

Permalink
Change ConfigKey Name to Id, Add FullId property
Browse files Browse the repository at this point in the history
  • Loading branch information
Banane9 committed Apr 1, 2024
1 parent a365208 commit 16d6656
Show file tree
Hide file tree
Showing 11 changed files with 152 additions and 82 deletions.
12 changes: 6 additions & 6 deletions MonkeyLoader/Configuration/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,16 @@ public T GetValue<T>(ITypedConfigKey<T> key)
public TSection LoadSection<TSection>(TSection section) where TSection : ConfigSection
{
if (_sections.Contains(section))
throw new ConfigLoadException($"Attempted to load section [{section.Name}] twice!");
throw new ConfigLoadException($"Attempted to load section [{section.Id}] twice!");

section.Config = this;
_sections.Add(section);

foreach (var key in section.Keys)
_configurationItemDefinitionsSelfMap.Add(key, key);

if (_loadedConfig[SectionsKey]![section.Name] is not JObject sectionObject)
Logger.Warn(() => $"Section [{section.Name}] didn't appear in the loaded config - using defaults!");
if (_loadedConfig[SectionsKey]![section.Id] is not JObject sectionObject)
Logger.Warn(() => $"Section [{section.Id}] didn't appear in the loaded config - using defaults!");
else
section.Load(sectionObject, Owner.Loader.JsonSerializer);

Expand Down Expand Up @@ -182,11 +182,11 @@ public void Save()
continue;

successfulSections.Add(section);
sectionsJson[section.Name] = sectionJson;
sectionsJson[section.Id] = sectionJson;
}
catch (Exception ex)
{
Logger.Error(() => ex.Format($"Exception while serializing section [{section.Name}] - skipping it!"));
Logger.Error(() => ex.Format($"Exception while serializing section [{section.Id}] - skipping it!"));
}
}

Expand Down Expand Up @@ -378,7 +378,7 @@ private JObject LoadConfig()

[DoesNotReturn]
private void ThrowKeyNotFound(IConfigKey key)
=> throw new KeyNotFoundException($"Key [{key.Name}] not found in this config!");
=> throw new KeyNotFoundException($"Key [{key.Id}] not found in this config!");

/// <summary>
/// Called when the value of one of this config's items gets changed.
Expand Down
46 changes: 23 additions & 23 deletions MonkeyLoader/Configuration/ConfigKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
namespace MonkeyLoader.Configuration
{
/// <summary>
/// Represents a name-only config item, which can be used to
/// get or set the values of defining keys with the same <see cref="Name">name</see>.
/// Represents a identifier-only config item, which can be used to
/// get or set the values of defining keys with the same <see cref="Id">Id</see>.
/// </summary>
public class ConfigKey : IConfigKey
{
Expand All @@ -22,29 +22,29 @@ public class ConfigKey : IConfigKey
IConfigKey IConfigKey.AsUntyped => this;

/// <inheritdoc/>
public bool IsDefiningKey => false;
public string Id { get; }

/// <inheritdoc/>
public string Name { get; }
public bool IsDefiningKey => false;

/// <summary>
/// Creates a new name-only config item with the given name.
/// Creates a new identifier-only config item with the given id.
/// </summary>
/// <param name="name">The mod-unique name of the config item. Must not be null or whitespace.</param>
/// <exception cref="ArgumentNullException">If the <paramref name="name"/> is null or whitespace.</exception>
public ConfigKey(string name)
/// <param name="id">The mod-unique identifier of the config item. Must not be null or whitespace.</param>
/// <exception cref="ArgumentNullException">If the <paramref name="id"/> is null or whitespace.</exception>
public ConfigKey(string id)
{
if (string.IsNullOrWhiteSpace(name))
throw new ArgumentNullException("Config key name must not be null or whitespace!");
if (string.IsNullOrWhiteSpace(id))
throw new ArgumentNullException(nameof(id), "Config key identifier must not be null or whitespace!");

Name = name;
Id = id;
}

/// <summary>
/// Creates a new <see cref="ConfigKey"/> instance from the given name.
/// </summary>
/// <param name="name">The mod-unique name of the config item.</param>
public static implicit operator ConfigKey(string name) => new(name);
/// <param name="id">The mod-unique identifier of the config item.</param>
public static implicit operator ConfigKey(string id) => new(id);

/// <summary>
/// Checks if two <see cref="ConfigKey"/>s are unequal.
Expand Down Expand Up @@ -89,18 +89,18 @@ public bool Equals(IConfigKey? x, IConfigKey? y)
return true;

if (x is ITypedConfigKey typedX && y is ITypedConfigKey typedY)
return typedX.ValueType == typedY.ValueType && typedX.Name == typedY.Name;
return typedX.ValueType == typedY.ValueType && typedX.Id == typedY.Id;

return x is not null && y is not null && x.Name == y.Name;
return x is not null && y is not null && x.Id == y.Id;
}

/// <inheritdoc/>
public int GetHashCode(IConfigKey? obj)
{
if (obj is ITypedConfigKey typedKey)
return unchecked((31 * typedKey.ValueType.GetHashCode()) + obj.Name.GetHashCode());
return unchecked((31 * typedKey.ValueType.GetHashCode()) + obj.Id.GetHashCode());

return obj?.Name.GetHashCode() ?? 0;
return obj?.Id.GetHashCode() ?? 0;
}
}
}
Expand All @@ -116,9 +116,9 @@ public class ConfigKey<T> : ConfigKey, ITypedConfigKey<T>
public Type ValueType { get; } = typeof(T);

/// <inheritdoc/>
public ConfigKey(string name) : base(name)
public ConfigKey(string id) : base(id)
{
AsUntyped = new ConfigKey(name);
AsUntyped = new ConfigKey(id);
}

/// <summary>
Expand All @@ -139,14 +139,14 @@ public interface IConfigKey : IEquatable<IConfigKey>
public IConfigKey AsUntyped { get; }

/// <summary>
/// Gets whether this instance defines the config item with this <see cref="Name">Name</see>.
/// Gets the mod-unique identifier of this config item.
/// </summary>
public bool IsDefiningKey { get; }
public string Id { get; }

/// <summary>
/// Gets the mod-unique name of this config item.
/// Gets whether this instance defines the config item with this <see cref="Id">Name</see>.
/// </summary>
public string Name { get; }
public bool IsDefiningKey { get; }
}

/// <summary>
Expand Down
14 changes: 12 additions & 2 deletions MonkeyLoader/Configuration/ConfigKeyChangedEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public sealed class ConfigKeyChangedEventArgs<T> : IConfigKeyChangedEventArgs
/// <summary>
/// Gets the configuration item who's value changed.
/// </summary>
public DefiningConfigKey<T> Key { get; }
public IDefiningConfigKey<T> Key { get; }

IDefiningConfigKey IConfigKeyChangedEventArgs.Key => Key;

Expand All @@ -64,7 +64,17 @@ public sealed class ConfigKeyChangedEventArgs<T> : IConfigKeyChangedEventArgs

object? IConfigKeyChangedEventArgs.OldValue => OldValue;

internal ConfigKeyChangedEventArgs(Config config, DefiningConfigKey<T> key, bool hadValue, T? oldValue, bool hasValue, T? newValue, string? label)
/// <summary>
/// Creates a new event args instance for a changed config item.
/// </summary>
/// <param name="config">The config the item belongs to.</param>
/// <param name="key">The config item that changed.</param>
/// <param name="hadValue">Whether the config item had a value before the change.</param>
/// <param name="oldValue">The optional old value.</param>
/// <param name="hasValue">Whether the config item has a value now.</param>
/// <param name="newValue">The optional new value.</param>
/// <param name="label">A custom label assigned to the change.</param>
public ConfigKeyChangedEventArgs(Config config, IDefiningConfigKey<T> key, bool hadValue, T? oldValue, bool hasValue, T? newValue, string? label)
{
Config = config;
Key = key;
Expand Down
6 changes: 3 additions & 3 deletions MonkeyLoader/Configuration/ConfigKeyWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ public abstract class ConfigKeyWrapperBase<TKey> : IConfigKeyWrapper<TKey>
/// <inheritdoc/>
IConfigKey IConfigKey.AsUntyped => Key.AsUntyped;

/// <inheritdoc/>
public string Id => Key.Id;

/// <inheritdoc/>
public bool IsDefiningKey => Key.IsDefiningKey;

Expand All @@ -51,9 +54,6 @@ public abstract class ConfigKeyWrapperBase<TKey> : IConfigKeyWrapper<TKey>

IConfigKey IConfigKeyWrapper.Key => Key;

/// <inheritdoc/>
public string Name => Key.Name;

/// <summary>
/// Wraps the given config key.
/// </summary>
Expand Down
28 changes: 16 additions & 12 deletions MonkeyLoader/Configuration/ConfigSection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,20 @@ public abstract class ConfigSection
/// </summary>
public bool HasChanges => keys.Any(key => key.HasChanges);

/// <summary>
/// Gets the mod-unique identifier of this section.
/// </summary>
public abstract string Id { get; }

/// <summary>
/// Gets all the config keys of this section.
/// </summary>
public IEnumerable<IDefiningConfigKey> Keys => keys.AsSafeEnumerable();

/// <summary>
/// Gets the name of the section.<br/>
/// Must be unique for a given mod.
/// Gets the name for this section.
/// </summary>
public abstract string Name { get; }
public virtual string Name => Id;

/// <summary>
/// Gets whether this config section is allowed to be saved.<br/>
Expand Down Expand Up @@ -97,7 +101,7 @@ protected ConfigSection()
/// <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.Name == right.Name);
|| (left is not null && right is not null && left.Id == right.Id);

/// <summary>
/// Checks if the given object can be considered equal to this one.
Expand Down Expand Up @@ -137,7 +141,7 @@ public IDefiningConfigKey<T> GetDefinedKey<T>(IDefiningConfigKey<T> templateKey)
}

/// <inheritdoc/>
public override int GetHashCode() => Name.GetHashCode();
public override int GetHashCode() => Id.GetHashCode();

/// <summary>
/// Determines if this config section contains an item matching the <paramref name="typedTemplateKey"/>
Expand Down Expand Up @@ -171,7 +175,7 @@ internal void Load(JObject source, JsonSerializer jsonSerializer)
{
// I know not what exceptions the JSON library will throw, but they must be contained
Saveable = false;
throw new ConfigLoadException($"Error loading version for section [{Name}]!", ex);
throw new ConfigLoadException($"Error loading version for section [{Id}]!", ex);
}

ValidateCompatibility(serializedVersion);
Expand Down Expand Up @@ -208,7 +212,7 @@ internal void ResetHasChanges()
/// <param name="jsonSerializer">The <see cref="JsonSerializer"/> to deserialize objects with.</param>
protected void DeserializeKey(IDefiningConfigKey key, JObject source, JsonSerializer jsonSerializer)
{
if (source[key.Name] is not JToken token)
if (source[key.Id] is not JToken token)
return;

var value = token.ToObject(key.ValueType, jsonSerializer);
Expand Down Expand Up @@ -263,7 +267,7 @@ protected virtual void OnLoad(JObject source, JsonSerializer jsonSerializer)
{
// I know not what exceptions the JSON library will throw, but they must be contained
Saveable = false;
throw new ConfigLoadException($"Error loading key [{key.Name}] of type [{key.ValueType}] in section [{Name}]!", ex);
throw new ConfigLoadException($"Error loading key [{key.Id}] of type [{key.ValueType}] in section [{Id}]!", ex);
}
}
}
Expand Down Expand Up @@ -296,7 +300,7 @@ protected void SerializeKey(IDefiningConfigKey key, JObject result, JsonSerializ
return;

// I don't need to typecheck this as there's no way to sneak a bad type past my Set() API
result[key.Name] = value == null ? null : JToken.FromObject(value, jsonSerializer);
result[key.Id] = value == null ? null : JToken.FromObject(value, jsonSerializer);
}

/// <summary>
Expand All @@ -306,7 +310,7 @@ protected void SerializeKey(IDefiningConfigKey key, JObject result, JsonSerializ
/// <exception cref="KeyNotFoundException">Always.</exception>
[DoesNotReturn]
protected void ThrowKeyNotFound(IConfigKey key)
=> throw new KeyNotFoundException($"Key [{key.Name}] not found in this config section!");
=> throw new KeyNotFoundException($"Key [{key.Id}] not found in this config section!");

private static bool AreVersionsCompatible(Version serializedVersion, Version currentVersion)
{
Expand Down Expand Up @@ -335,7 +339,7 @@ private void ValidateCompatibility(Version serializedVersion)
switch (IncompatibilityHandling)
{
case IncompatibleConfigHandling.Clobber:
Config.Logger.Warn(() => $"Saved section [{Name}] version [{serializedVersion}] is incompatible with mod's version [{Version}]. Clobbering old config and starting fresh.");
Config.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 @@ -345,7 +349,7 @@ private void ValidateCompatibility(Version serializedVersion)
case IncompatibleConfigHandling.Error: // fall through to default
default:
Saveable = false;
throw new ConfigLoadException($"Saved section [{Name}] version [{serializedVersion}] is incompatible with mod's version [{Version}]!");
throw new ConfigLoadException($"Saved section [{Id}] version [{serializedVersion}] is incompatible with mod's version [{Version}]!");
}
}
}
Expand Down
Loading

0 comments on commit 16d6656

Please sign in to comment.