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

Commit b855bdf

Browse files
authored
Handle ModConfiguration.Set() for null values (#74)
* Handle ModConfiguration.Set() for null values Fixes #73 * run `dotnet format` * Fix the build? why were we using pull_request_target?
1 parent 6dc2f32 commit b855bdf

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
on: [push, pull_request_target]
1+
on: [push, pull_request]
22

33
env:
44
NeosPath: "${{ github.workspace }}/Libs/current/"

NeosModLoader/AssemblyHider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ private static void FindTypePostfix(ref Type? __result)
6363
// since this is an edge case users may want to handle in different ways, the HideLateTypes nml config option allows them to choose.
6464
bool hideLate = ModLoaderConfiguration.Get().HideLateTypes;
6565
Logger.WarnInternal($"The \"{__result}\" type does not appear to part of Neos or a mod. It is unclear whether it should be hidden or not. due to the HideLateTypes config option being {hideLate} it will be {(hideLate ? "Hidden" : "Shown")}");
66-
if(hideLate) __result = null;
66+
if (hideLate) __result = null;
6767
}
6868
else
6969
{

NeosModLoader/ModConfiguration.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -354,19 +354,27 @@ public bool TryGetValue<T>(ModConfigurationKey<T> key, out T? value)
354354
/// <param name="key">The key</param>
355355
/// <param name="value">The new value</param>
356356
/// <param name="eventLabel">A custom label you may assign to this event</param>
357-
public void Set(ModConfigurationKey key, object value, string? eventLabel = null)
357+
public void Set(ModConfigurationKey key, object? value, string? eventLabel = null)
358358
{
359359
if (!Definition.TryGetDefiningKey(key, out ModConfigurationKey? definingKey))
360360
{
361361
throw new KeyNotFoundException($"{key.Name} is not defined in the config definition for {LoadedNeosMod.NeosMod.Name}");
362362
}
363363

364-
if (!definingKey!.ValueType().IsAssignableFrom(value.GetType()))
364+
if (value == null)
365+
{
366+
bool cannotBeNull = definingKey!.ValueType().IsValueType && Nullable.GetUnderlyingType(definingKey!.ValueType()) == null;
367+
if (cannotBeNull)
368+
{
369+
throw new ArgumentException($"null cannot be assigned to {definingKey.ValueType()}");
370+
}
371+
}
372+
else if (!definingKey!.ValueType().IsAssignableFrom(value.GetType()))
365373
{
366374
throw new ArgumentException($"{value.GetType()} cannot be assigned to {definingKey.ValueType()}");
367375
}
368376

369-
if (!definingKey.Validate(value))
377+
if (!definingKey!.Validate(value))
370378
{
371379
throw new ArgumentException($"\"{value}\" is not a valid value for \"{Owner.Name}{definingKey.Name}\"");
372380
}

0 commit comments

Comments
 (0)