diff --git a/MiraAPI/GameOptions/Attributes/ModdedOptionListAttribute.cs b/MiraAPI/GameOptions/Attributes/ModdedOptionListAttribute.cs index bce0190..3e26432 100644 --- a/MiraAPI/GameOptions/Attributes/ModdedOptionListAttribute.cs +++ b/MiraAPI/GameOptions/Attributes/ModdedOptionListAttribute.cs @@ -29,8 +29,7 @@ public abstract class ModdedOptionListAttribute(Func titler) : Prop public override void SetValue(object value) { var list = (IList)value; - if (list.Count != ((IList)BaseProperty!.GetValue(Group)!).Count || - list.Count != HolderOptionList!.Count) + if (list.Count != HolderOptionList!.Count) { throw new InvalidOperationException($"Value set to {BaseProperty!.Name} cannot change the list's length."); } @@ -54,12 +53,7 @@ public abstract class ModdedOptionListAttribute(Func titler) : Prop /// The value of the options as an object. public override object GetValue() { - var list = (IList)BaseProperty!.GetValue(Group)!; - for (int i = 0; i < list!.Count; i++) - { - list[i] = GetValue(HolderOptionList![i]); - } - return list; + return BaseProperty!.GetValue(Group)!; } /// diff --git a/MiraAPI/GameOptions/ModdedOptionsManager.cs b/MiraAPI/GameOptions/ModdedOptionsManager.cs index 1706719..3dbab02 100644 --- a/MiraAPI/GameOptions/ModdedOptionsManager.cs +++ b/MiraAPI/GameOptions/ModdedOptionsManager.cs @@ -168,6 +168,11 @@ public static class ModdedOptionsManager Error($"Failed to get option for {property.Name}"); return; } + if (propertyList.Count != optionList.Count) + { + Error("Mismatch in count between values and created options."); + return; + } var setterOriginal = property.GetSetMethod(); var setterPatch = typeof(ModdedOptionsManager).GetMethod(nameof(PropertySetterPatch)); @@ -177,7 +182,15 @@ public static class ModdedOptionsManager var getterPatch = typeof(ModdedOptionsManager).GetMethod(nameof(PropertyGetterPatch)); PluginSingleton.Instance.Harmony.Patch(getterOriginal, prefix: new HarmonyMethod(getterPatch)); - // Add indexer patches + var listIndex = propertyList.GetType().GetProperty("Item")!; + + var listSetterOriginal = listIndex.GetSetMethod(); + var listSetterPatch = typeof(ModdedOptionsManager).GetMethod(nameof(PropertyListSetterPatch)); + PluginSingleton.Instance.Harmony.Patch(listSetterOriginal, postfix: new HarmonyMethod(listSetterPatch)); + + var listGetterOriginal = listIndex.GetSetMethod(); + var listGetterPatch = typeof(ModdedOptionsManager).GetMethod(nameof(PropertyListGetterPatch)); + PluginSingleton.Instance.Harmony.Patch(listGetterOriginal, prefix: new HarmonyMethod(listGetterPatch)); OptionAttributes.Add(property, attribute); attribute.HolderOptionList = optionList; @@ -277,4 +290,34 @@ public static class ModdedOptionsManager __result = attribute.GetValue(); return false; } + + /// + /// Patches the setter of a list property to update the value of the option. + /// + /// The original setter method. + /// The index to find in the list. + /// The new object value. +#pragma warning disable CA1707 + [SuppressMessage("ReSharper", "InconsistentNaming", Justification = "Harmony naming convention")] + public static void PropertyListSetterPatch(MethodBase __originalMethod, int index, object value) +#pragma warning restore CA1707 + { + // Actually implement + } + + /// + /// Patches the getter of a list property to return the value of the option. + /// + /// The original getter method. + /// The index to find in the list. + /// The result of the property getter. + /// False so the original getter gets skipped. +#pragma warning disable CA1707 + [SuppressMessage("ReSharper", "InconsistentNaming", Justification = "Harmony naming convention")] + public static bool PropertyListGetterPatch(MethodBase __originalMethod, int index, ref object __result) +#pragma warning restore CA1707 + { + // Actually implement + return true; + } }