mirror of
https://github.com/All-Of-Us-Mods/MiraAPI.git
synced 2026-10-19 15:51:48 +00:00
Added patches to the list's getter and setter
This commit is contained in:
1 parent
80b8af85cb
commit
68fb1d2b58
2 files changed
+46
-9
No files matched your search
@@ -29,8 +29,7 @@ public abstract class ModdedOptionListAttribute(Func<int, string> 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<int, string> titler) : Prop
|
||||
/// <returns>The value of the options as an object.</returns>
|
||||
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)!;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -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<MiraApiPlugin>.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<MiraApiPlugin>.Instance.Harmony.Patch(listSetterOriginal, postfix: new HarmonyMethod(listSetterPatch));
|
||||
|
||||
var listGetterOriginal = listIndex.GetSetMethod();
|
||||
var listGetterPatch = typeof(ModdedOptionsManager).GetMethod(nameof(PropertyListGetterPatch));
|
||||
PluginSingleton<MiraApiPlugin>.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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Patches the setter of a list property to update the value of the option.
|
||||
/// </summary>
|
||||
/// <param name="__originalMethod">The original setter method.</param>
|
||||
/// <param name="index">The index to find in the list.</param>
|
||||
/// <param name="value">The new object value.</param>
|
||||
#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
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Patches the getter of a list property to return the value of the option.
|
||||
/// </summary>
|
||||
/// <param name="__originalMethod">The original getter method.</param>
|
||||
/// <param name="index">The index to find in the list.</param>
|
||||
/// <param name="__result">The result of the property getter.</param>
|
||||
/// <returns>False so the original getter gets skipped.</returns>
|
||||
#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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user