mirror of
https://github.com/All-Of-Us-Mods/MiraAPI.git
synced 2026-11-19 13:17:10 +00:00
Changed IModdedOptionList structure to actually allow typed iteration
This commit is contained in:
1 parent
2a7570f022
commit
0e02a02156
11 files changed
+58
-42
No files matched your search
@@ -104,3 +104,9 @@ dotnet_diagnostic.S1244.severity = none
|
||||
|
||||
# S2325: Methods and properties that don't access instance data should be static
|
||||
dotnet_diagnostic.S2325.severity = none
|
||||
|
||||
# CA1710: Identifiers should have correct suffix
|
||||
dotnet_diagnostic.CA1710.severity = silent
|
||||
|
||||
# CA1010: Generic interface should also be implemented
|
||||
dotnet_diagnostic.CA1010.severity = silent
|
||||
@@ -15,7 +15,7 @@ namespace MiraAPI.GameOptions.Attributes;
|
||||
public class ModdedEnumOptionListAttribute(string title, Type enumType, string[]? values = null)
|
||||
: ModdedOptionListAttribute(title)
|
||||
{
|
||||
internal override IModdedOptionList CreateOptionList(IList value, PropertyInfo property)
|
||||
internal override IBaseModdedOptionList CreateOptionList(IList value, PropertyInfo property)
|
||||
{
|
||||
var optList = new ModdedOptionList<ModdedEnumOption>(
|
||||
value.Count, idx => new(GetFormattedTitle(idx), (int)(value[idx] ?? 0), enumType, values));
|
||||
@@ -25,14 +25,14 @@ public class ModdedEnumOptionListAttribute(string title, Type enumType, string[]
|
||||
/// <inheritdoc />
|
||||
public override void SetValue(int idx, object value)
|
||||
{
|
||||
var opt = HolderOptionList?[idx] as ModdedEnumOption;
|
||||
var opt = HolderOptionList?.ElementAt(idx) as ModdedEnumOption;
|
||||
opt?.SetValue((int)value);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override object GetValue(int idx)
|
||||
{
|
||||
return HolderOptionList?[idx] is ModdedEnumOption opt
|
||||
return HolderOptionList?.ElementAt(idx) is ModdedEnumOption opt
|
||||
? Enum.ToObject(enumType, opt.Value)
|
||||
: throw new InvalidOperationException($"HolderOption for option \"{GetFormattedTitle(idx)}\" with EnumType {enumType.FullName} is not a ModdedEnumOption");
|
||||
}
|
||||
@@ -49,7 +49,7 @@ public class ModdedEnumOptionListAttribute<T>(string title, string[]? values = n
|
||||
: ModdedOptionListAttribute(title)
|
||||
where T : struct, Enum
|
||||
{
|
||||
internal override IModdedOptionList CreateOptionList(IList value, PropertyInfo property)
|
||||
internal override IBaseModdedOptionList CreateOptionList(IList value, PropertyInfo property)
|
||||
{
|
||||
var optList = new ModdedOptionList<ModdedEnumOption<T>>(
|
||||
value.Count, idx => new(GetFormattedTitle(idx), (T)(value[idx] ?? 0), values));
|
||||
@@ -59,14 +59,14 @@ public class ModdedEnumOptionListAttribute<T>(string title, string[]? values = n
|
||||
/// <inheritdoc />
|
||||
public override void SetValue(int idx, object value)
|
||||
{
|
||||
var opt = HolderOptionList?[idx] as ModdedEnumOption<T>;
|
||||
var opt = HolderOptionList?.ElementAt(idx) as ModdedEnumOption<T>;
|
||||
opt?.SetValue((T)value);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override object GetValue(int idx)
|
||||
{
|
||||
return HolderOptionList?[idx] is ModdedEnumOption<T> opt
|
||||
return HolderOptionList?.ElementAt(idx) is ModdedEnumOption<T> opt
|
||||
? opt.Value
|
||||
: throw new InvalidOperationException($"HolderOption for option \"{GetFormattedTitle(idx)}\" with EnumType {typeof(T).FullName} is not a ModdedEnumOption<{typeof(T).FullName}>");
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ public class ModdedNumberOptionListAttribute(
|
||||
{
|
||||
}
|
||||
|
||||
internal override IModdedOptionList CreateOptionList(IList value, PropertyInfo property)
|
||||
internal override IBaseModdedOptionList CreateOptionList(IList value, PropertyInfo property)
|
||||
{
|
||||
return new ModdedOptionList<ModdedNumberOption>(value.Count, idx =>
|
||||
{
|
||||
@@ -81,7 +81,7 @@ public class ModdedNumberOptionListAttribute(
|
||||
/// <inheritdoc />
|
||||
public override void SetValue(int idx, object value)
|
||||
{
|
||||
if (HolderOptionList?[idx] is not ModdedNumberOption opt)
|
||||
if (HolderOptionList?.ElementAt(idx) is not ModdedNumberOption opt)
|
||||
return;
|
||||
|
||||
var final = value switch
|
||||
@@ -95,7 +95,7 @@ public class ModdedNumberOptionListAttribute(
|
||||
/// <inheritdoc />
|
||||
public override object GetValue(int idx)
|
||||
{
|
||||
if (HolderOptionList?[idx] is not ModdedNumberOption opt)
|
||||
if (HolderOptionList?.ElementAt(idx) is not ModdedNumberOption opt)
|
||||
throw new InvalidOperationException($"HolderOption for option \"{GetFormattedTitle(idx)}\" is not a ModdedNumberOption");
|
||||
|
||||
var value = opt.Value;
|
||||
|
||||
@@ -14,7 +14,7 @@ namespace MiraAPI.GameOptions.Attributes;
|
||||
[AttributeUsage(AttributeTargets.Property)]
|
||||
public abstract class ModdedOptionListAttribute(string title) : PropertyOptionAttribute
|
||||
{
|
||||
internal IModdedOptionList? HolderOptionList { get; set; }
|
||||
internal IBaseModdedOptionList? HolderOptionList { get; set; }
|
||||
internal IList? Value { get; set; }
|
||||
internal Type ValueType { get; set; }
|
||||
|
||||
@@ -68,7 +68,7 @@ public abstract class ModdedOptionListAttribute(string title) : PropertyOptionAt
|
||||
/// <returns>The value of the option as an object.</returns>
|
||||
public abstract object GetValue(int idx);
|
||||
|
||||
internal abstract IModdedOptionList? CreateOptionList(IList value, PropertyInfo property);
|
||||
internal abstract IBaseModdedOptionList? CreateOptionList(IList value, PropertyInfo property);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="Title"/> formatted with the option's index.
|
||||
|
||||
@@ -16,7 +16,7 @@ public class ModdedPlayerOptionListAttribute(
|
||||
bool allowNone = true)
|
||||
: ModdedOptionListAttribute(title)
|
||||
{
|
||||
internal override IModdedOptionList CreateOptionList(IList value, PropertyInfo property)
|
||||
internal override IBaseModdedOptionList CreateOptionList(IList value, PropertyInfo property)
|
||||
{
|
||||
return new ModdedOptionList<ModdedPlayerOption>(value.Count, idx =>
|
||||
{
|
||||
@@ -27,7 +27,7 @@ public class ModdedPlayerOptionListAttribute(
|
||||
/// <inheritdoc />
|
||||
public override void SetValue(int idx, object value)
|
||||
{
|
||||
if (HolderOptionList?[idx] is not ModdedPlayerOption opt)
|
||||
if (HolderOptionList?.ElementAt(idx) is not ModdedPlayerOption opt)
|
||||
return;
|
||||
|
||||
var final = value switch
|
||||
@@ -41,7 +41,7 @@ public class ModdedPlayerOptionListAttribute(
|
||||
/// <inheritdoc />
|
||||
public override object GetValue(int idx)
|
||||
{
|
||||
if (HolderOptionList?[idx] is not ModdedPlayerOption opt)
|
||||
if (HolderOptionList?.ElementAt(idx) is not ModdedPlayerOption opt)
|
||||
throw new InvalidOperationException($"HolderOption for option \"{GetFormattedTitle(idx)}\" is not a ModdedPlayerOption");
|
||||
|
||||
if (ValueType == typeof(int))
|
||||
|
||||
@@ -17,7 +17,7 @@ public class ModdedStringOptionListAttribute(
|
||||
string[] values)
|
||||
: ModdedOptionListAttribute(title)
|
||||
{
|
||||
internal override IModdedOptionList CreateOptionList(IList value, PropertyInfo property)
|
||||
internal override IBaseModdedOptionList CreateOptionList(IList value, PropertyInfo property)
|
||||
{
|
||||
return new ModdedOptionList<ModdedStringOption>(value.Count, idx =>
|
||||
{
|
||||
@@ -28,14 +28,14 @@ public class ModdedStringOptionListAttribute(
|
||||
/// <inheritdoc />
|
||||
public override void SetValue(int idx, object value)
|
||||
{
|
||||
var opt = HolderOptionList?[idx] as ModdedStringOption;
|
||||
var opt = HolderOptionList?.ElementAt(idx) as ModdedStringOption;
|
||||
opt?.SetValue((string)value);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override object GetValue(int idx)
|
||||
{
|
||||
return HolderOptionList?[idx] is ModdedStringOption opt
|
||||
return HolderOptionList?.ElementAt(idx) is ModdedStringOption opt
|
||||
? opt.Value
|
||||
: throw new InvalidOperationException($"HolderOption for option \"{GetFormattedTitle(idx)}\" is not a ModdedStringOption");
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ public class ModdedToggleOptionListAttribute(
|
||||
{
|
||||
}
|
||||
|
||||
internal override IModdedOptionList CreateOptionList(IList value, PropertyInfo property)
|
||||
internal override IBaseModdedOptionList CreateOptionList(IList value, PropertyInfo property)
|
||||
{
|
||||
var optList = new ModdedOptionList<ModdedToggleOption>(
|
||||
value.Count, idx => new(GetFormattedTitle(idx), (bool)(value[idx] ?? false), on, off));
|
||||
@@ -37,14 +37,14 @@ public class ModdedToggleOptionListAttribute(
|
||||
/// <inheritdoc />
|
||||
public override void SetValue(int idx, object value)
|
||||
{
|
||||
var opt = HolderOptionList?[idx] as ModdedToggleOption;
|
||||
var opt = HolderOptionList?.ElementAt(idx) as ModdedToggleOption;
|
||||
opt?.SetValue((bool)value);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override object GetValue(int idx)
|
||||
{
|
||||
return HolderOptionList?[idx] is ModdedToggleOption opt
|
||||
return HolderOptionList?.ElementAt(idx) is ModdedToggleOption opt
|
||||
? opt.Value
|
||||
: throw new InvalidOperationException($"Holder option for {GetFormattedTitle(idx)} is not a ModdedToggleOption.");
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using BepInEx.Configuration;
|
||||
using MiraAPI.Networking;
|
||||
@@ -143,9 +144,11 @@ public interface IModdedOption
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Interface for list of modded options.
|
||||
/// Base interface for list of modded options.
|
||||
/// <para/>
|
||||
/// Do not implement directly, implement <see cref="IModdedOptionList"/> or <see cref="IModdedOptionList{T}"/> instead.
|
||||
/// </summary>
|
||||
public interface IModdedOptionList : IReadOnlyList<IModdedOption>
|
||||
public interface IBaseModdedOptionList : IEnumerable
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the function to name the keys of the individual options in the configuration file.
|
||||
@@ -153,18 +156,34 @@ public interface IModdedOptionList : IReadOnlyList<IModdedOption>
|
||||
/// Return <see langword="null"/> to use the default of the property's name with its index appended.
|
||||
/// </summary>
|
||||
Func<int, string>? ConfigName { get; init; }
|
||||
|
||||
/// <inheritdoc cref="IReadOnlyCollection{T}.Count"/>
|
||||
int Count { get; }
|
||||
|
||||
/// <inheritdoc cref="System.Linq.Enumerable.ElementAt{TSource}(IEnumerable{TSource}, int)"/>
|
||||
IModdedOption ElementAt(int index);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Interface for list of modded options.
|
||||
/// </summary>
|
||||
public interface IModdedOptionList : IBaseModdedOptionList, IReadOnlyList<IModdedOption>
|
||||
{
|
||||
IModdedOption IBaseModdedOptionList.ElementAt(int index)
|
||||
{
|
||||
return this[index];
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Interface for list of <typeparamref name="T"/> options.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of <see cref="IModdedOption"/>.</typeparam>
|
||||
public interface IModdedOptionList<out T> : IModdedOptionList
|
||||
public interface IModdedOptionList<out T> : IBaseModdedOptionList, IReadOnlyList<T>
|
||||
where T : IModdedOption
|
||||
{
|
||||
/// <inheritdoc cref="IReadOnlyList{T}.this[int]"/>
|
||||
new T this[int index] { get; }
|
||||
|
||||
/// <inheritdoc cref="IEnumerable{T}.GetEnumerator"/>
|
||||
new IEnumerator<T> GetEnumerator();
|
||||
IModdedOption IBaseModdedOptionList.ElementAt(int index)
|
||||
{
|
||||
return this[index];
|
||||
}
|
||||
}
|
||||
@@ -217,7 +217,7 @@ public static class ModdedOptionsManager
|
||||
return;
|
||||
}
|
||||
|
||||
if (property.GetValue(group) is not IModdedOptionList optionList)
|
||||
if (property.GetValue(group) is not IBaseModdedOptionList optionList)
|
||||
{
|
||||
Error($"Failed to get option list for {property.Name}");
|
||||
return;
|
||||
@@ -227,7 +227,7 @@ public static class ModdedOptionsManager
|
||||
|
||||
for (var i = 0; i < optionList.Count; i++)
|
||||
{
|
||||
RegisterOption(optionList[i], group, configName(i), pluginInfo);
|
||||
RegisterOption(optionList.ElementAt(i), group, configName(i), pluginInfo);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -324,7 +324,7 @@ public static class ModdedOptionsManager
|
||||
|
||||
for (var i = 0; i < optionList.Count; i++)
|
||||
{
|
||||
var option = optionList[i];
|
||||
var option = optionList.ElementAt(i);
|
||||
|
||||
if (visibilityFactory != null)
|
||||
option.Visible = (Func<bool>)(visibilityFactory(i) ?? DelegateUtilities.AlwaysTrue);
|
||||
|
||||
@@ -62,13 +62,4 @@ public class ModdedOptionList<T> : IModdedOptionList<T>
|
||||
{
|
||||
return Options.GetEnumerator();
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IEnumerable{T}.GetEnumerator"/>
|
||||
IEnumerator<IModdedOption> IEnumerable<IModdedOption>.GetEnumerator()
|
||||
{
|
||||
return ((IEnumerable<IModdedOption>)Options).GetEnumerator();
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IReadOnlyList{T}.this"/>
|
||||
IModdedOption IReadOnlyList<IModdedOption>.this[int index] => this[index];
|
||||
}
|
||||
@@ -328,7 +328,7 @@ public sealed class MiraPluginManager
|
||||
continue;
|
||||
}
|
||||
|
||||
if (property.PropertyType.IsAssignableTo(typeof(IModdedOptionList)))
|
||||
if (property.PropertyType.IsAssignableTo(typeof(IBaseModdedOptionList)))
|
||||
{
|
||||
ModdedOptionsManager.RegisterPropertyOptionList(type, property, pluginInfo);
|
||||
continue;
|
||||
@@ -349,7 +349,7 @@ public sealed class MiraPluginManager
|
||||
}
|
||||
|
||||
foreach (var field in type.GetFields()
|
||||
.Where(f => f.FieldType.IsAssignableTo(typeof(IModdedOption)) || f.FieldType.IsAssignableTo(typeof(IModdedOptionList))))
|
||||
.Where(f => f.FieldType.IsAssignableTo(typeof(IModdedOption)) || f.FieldType.IsAssignableTo(typeof(IBaseModdedOptionList))))
|
||||
{
|
||||
Error($"{field.Name} is a field, not a property. Use properties for options.");
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user