Skip to content
This repository has been archived by the owner on Feb 28, 2024. It is now read-only.

Commit

Permalink
Handle ModConfiguration.Set() for null values
Browse files Browse the repository at this point in the history
Fixes #73
  • Loading branch information
zkxs committed Sep 1, 2022
1 parent 6dc2f32 commit 9906f8a
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions NeosModLoader/ModConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -354,19 +354,27 @@ public bool TryGetValue<T>(ModConfigurationKey<T> key, out T? value)
/// <param name="key">The key</param>
/// <param name="value">The new value</param>
/// <param name="eventLabel">A custom label you may assign to this event</param>
public void Set(ModConfigurationKey key, object value, string? eventLabel = null)
public void Set(ModConfigurationKey key, object? value, string? eventLabel = null)
{
if (!Definition.TryGetDefiningKey(key, out ModConfigurationKey? definingKey))
{
throw new KeyNotFoundException($"{key.Name} is not defined in the config definition for {LoadedNeosMod.NeosMod.Name}");
}

if (!definingKey!.ValueType().IsAssignableFrom(value.GetType()))
if (value == null)
{
bool cannotBeNull = definingKey!.ValueType().IsValueType && Nullable.GetUnderlyingType(definingKey!.ValueType()) == null;
if (cannotBeNull)
{
throw new ArgumentException($"null cannot be assigned to {definingKey.ValueType()}");
}
}
else if (!definingKey!.ValueType().IsAssignableFrom(value.GetType()))
{
throw new ArgumentException($"{value.GetType()} cannot be assigned to {definingKey.ValueType()}");
}

if (!definingKey.Validate(value))
if (!definingKey!.Validate(value))
{
throw new ArgumentException($"\"{value}\" is not a valid value for \"{Owner.Name}{definingKey.Name}\"");
}
Expand Down

0 comments on commit 9906f8a

Please sign in to comment.