mirror of
https://github.com/All-Of-Us-Mods/MiraAPI.git
synced 2026-11-19 13:17:10 +00:00
optimise modded option arrays
This commit is contained in:
2 files changed
+76
-27
No files matched your search
@@ -11,16 +11,22 @@ namespace MiraAPI.GameOptions;
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the values that the array holds.</typeparam>
|
||||
[CollectionBuilder(typeof(ModdedArrayBuilder), nameof(ModdedArrayBuilder.Create))]
|
||||
public sealed class ModdedArray<T> : IList<T>, IReadOnlyList<T>, IList
|
||||
public sealed class ModdedArray<T> : IList<T>, IReadOnlyList<T>, IList, IModdedArrayBinding
|
||||
{
|
||||
private readonly T[] items;
|
||||
private int version;
|
||||
|
||||
private ModdedOptionListAttribute? listAttribute;
|
||||
private ITypedOptionList<T>? typedAttribute;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public int Count => items.Length;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public bool IsReadOnly => false;
|
||||
bool ICollection<T>.IsReadOnly => true;
|
||||
|
||||
/// <inheritdoc/>
|
||||
bool IList.IsReadOnly => false;
|
||||
|
||||
/// <inheritdoc/>
|
||||
bool IList.IsFixedSize => true;
|
||||
@@ -75,34 +81,45 @@ public sealed class ModdedArray<T> : IList<T>, IReadOnlyList<T>, IList
|
||||
{
|
||||
get
|
||||
{
|
||||
if (ModdedOptionsManager.ListInstanceCache.TryGetValue(this, out var attribute))
|
||||
{
|
||||
items[index] = attribute is ITypedOptionList<T> typedAttr
|
||||
? typedAttr.GetValueUnboxed(index)
|
||||
: (T)attribute.GetValue(index);
|
||||
}
|
||||
if (listAttribute == null)
|
||||
return items[index];
|
||||
|
||||
return items[index];
|
||||
return items[index] = typedAttribute != null
|
||||
? typedAttribute.GetValueUnboxed(index)
|
||||
: (T)listAttribute.GetValue(index);
|
||||
}
|
||||
set
|
||||
{
|
||||
if (ModdedOptionsManager.ListInstanceCache.TryGetValue(this, out var attribute))
|
||||
{
|
||||
if (attribute is ITypedOptionList<T> typedAttr)
|
||||
typedAttr.SetValueUnboxed(index, value);
|
||||
else
|
||||
attribute.SetValue(index, value!);
|
||||
var old = items[index];
|
||||
|
||||
items[index] = attribute is ITypedOptionList<T> typedAttr2
|
||||
? typedAttr2.GetValueUnboxed(index)
|
||||
: (T)attribute.GetValue(index);
|
||||
if (listAttribute == null)
|
||||
{
|
||||
if (!EqualityComparer<T>.Default.Equals(old, value))
|
||||
{
|
||||
items[index] = value;
|
||||
version++;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
T current;
|
||||
|
||||
if (typedAttribute != null)
|
||||
{
|
||||
typedAttribute.SetValueUnboxed(index, value);
|
||||
current = typedAttribute.GetValueUnboxed(index);
|
||||
}
|
||||
else
|
||||
{
|
||||
items[index] = value;
|
||||
listAttribute.SetValue(index, value!);
|
||||
current = (T)listAttribute.GetValue(index);
|
||||
}
|
||||
|
||||
version++;
|
||||
items[index] = current;
|
||||
|
||||
if (!EqualityComparer<T>.Default.Equals(old, current))
|
||||
version++;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -116,18 +133,21 @@ public sealed class ModdedArray<T> : IList<T>, IReadOnlyList<T>, IList
|
||||
/// <inheritdoc/>
|
||||
public bool Contains(T item)
|
||||
{
|
||||
Refresh();
|
||||
return Array.IndexOf(items, item) >= 0;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void CopyTo(T[] array, int arrayIndex)
|
||||
{
|
||||
Refresh();
|
||||
items.CopyTo(array, arrayIndex);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public int IndexOf(T item)
|
||||
{
|
||||
Refresh();
|
||||
return Array.IndexOf(items, item);
|
||||
}
|
||||
|
||||
@@ -137,21 +157,47 @@ public sealed class ModdedArray<T> : IList<T>, IReadOnlyList<T>, IList
|
||||
return new(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Re-reads every element from its backing option, updating the cached values. Does nothing if the array isn't bound to an option list.
|
||||
/// </summary>
|
||||
public void Refresh()
|
||||
{
|
||||
if (typedAttribute != null)
|
||||
{
|
||||
for (var i = 0; i < items.Length; i++)
|
||||
items[i] = typedAttribute.GetValueUnboxed(i);
|
||||
}
|
||||
else if (listAttribute != null)
|
||||
{
|
||||
for (var i = 0; i < items.Length; i++)
|
||||
items[i] = (T)listAttribute.GetValue(i);
|
||||
}
|
||||
}
|
||||
|
||||
void IModdedArrayBinding.Bind(ModdedOptionListAttribute attribute)
|
||||
{
|
||||
listAttribute = attribute;
|
||||
typedAttribute = attribute as ITypedOptionList<T>;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
bool IList.Contains(object? value)
|
||||
{
|
||||
Refresh();
|
||||
return ((IList)items).Contains(value);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
int IList.IndexOf(object? value)
|
||||
{
|
||||
Refresh();
|
||||
return ((IList)items).IndexOf(value);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void ICollection.CopyTo(Array array, int index)
|
||||
{
|
||||
Refresh();
|
||||
items.CopyTo(array, index);
|
||||
}
|
||||
|
||||
@@ -312,3 +358,8 @@ public static class ModdedArrayBuilder
|
||||
return new(values);
|
||||
}
|
||||
}
|
||||
|
||||
internal interface IModdedArrayBinding
|
||||
{
|
||||
void Bind(ModdedOptionListAttribute attribute);
|
||||
}
|
||||
@@ -4,7 +4,6 @@ using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using BepInEx.Configuration;
|
||||
using HarmonyLib;
|
||||
using MiraAPI.GameModes;
|
||||
@@ -39,7 +38,6 @@ public static class ModdedOptionsManager
|
||||
private static readonly Dictionary<Type, HarmonyMethod> ConcretisedGetters = [];
|
||||
private static readonly Dictionary<Type, HarmonyMethod> ConcretisedSetters = [];
|
||||
|
||||
internal static readonly ConditionalWeakTable<object, ModdedOptionListAttribute> ListInstanceCache = [];
|
||||
internal static readonly Dictionary<OptionBehaviour, ModdedPlayerOption> CreatedPlayerOptions = [];
|
||||
internal static readonly Dictionary<OptionBehaviour, ModdedStringOption> CreatedStringOptions = [];
|
||||
internal static readonly Dictionary<Type, List<AbstractOptionGroup>> GameModeOptionGroups = [];
|
||||
@@ -332,7 +330,7 @@ public static class ModdedOptionsManager
|
||||
attribute.Value = list;
|
||||
attribute.ValueType = valueType;
|
||||
|
||||
ListInstanceCache.Add(propertyVal, attribute);
|
||||
((IModdedArrayBinding)propertyVal).Bind(attribute);
|
||||
|
||||
Func<int, Delegate?>? visibilityFactory = null, getterFactory = null, setterFactory = null, changedFactory = null;
|
||||
|
||||
@@ -452,8 +450,8 @@ public static class ModdedOptionsManager
|
||||
|
||||
attribute.SetValue(value);
|
||||
|
||||
if (attribute is ModdedOptionListAttribute listAttribute && value != null)
|
||||
ListInstanceCache.AddOrUpdate(value, listAttribute);
|
||||
if (attribute is ModdedOptionListAttribute listAttribute && value is IModdedArrayBinding binding)
|
||||
binding.Bind(listAttribute);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -474,8 +472,8 @@ public static class ModdedOptionsManager
|
||||
else
|
||||
attribute.SetValue(value!);
|
||||
|
||||
if (attribute is ModdedOptionListAttribute listAttribute && value is not null)
|
||||
ListInstanceCache.AddOrUpdate(value, listAttribute);
|
||||
if (attribute is ModdedOptionListAttribute listAttribute && value is IModdedArrayBinding binding)
|
||||
binding.Bind(listAttribute);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in new issue
Block a user