mirror of
https://github.com/All-Of-Us-Mods/MiraAPI.git
synced 2026-10-19 15:51:48 +00:00
Merge pull request #106 from TheDavSmasher/generic-option-list
Adds a generic IModdedOptionList definition for easier typed iteration
This commit is contained in:
3 files changed
+36
-6
No files matched your search
@@ -121,4 +121,24 @@ public interface IModdedOption
|
||||
/// </summary>
|
||||
public interface IModdedOptionList : IReadOnlyList<IModdedOption>
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the function to name the keys of the individual options in the configuration file.
|
||||
/// <para/>
|
||||
/// Return <see langword="null"/> to use the default of the property's name with its index appended.
|
||||
/// </summary>
|
||||
Func<int, string>? ConfigName { get; init; }
|
||||
}
|
||||
|
||||
/// <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
|
||||
where T : IModdedOption
|
||||
{
|
||||
/// <inheritdoc cref="IReadOnlyList{T}.this[int]"/>
|
||||
new T this[int index] { get; }
|
||||
|
||||
/// <inheritdoc cref="IEnumerable{T}.GetEnumerator"/>
|
||||
new IEnumerator<T> GetEnumerator();
|
||||
}
|
||||
@@ -198,9 +198,11 @@ public static class ModdedOptionsManager
|
||||
return;
|
||||
}
|
||||
|
||||
Func<int, string> configName = optionList.ConfigName ?? (i => property.Name + (i + 1));
|
||||
|
||||
for (int i = 0; i < optionList.Count; i++)
|
||||
{
|
||||
RegisterOption(optionList[i], group, property.Name + i, pluginInfo);
|
||||
RegisterOption(optionList[i], group, configName(i), pluginInfo);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -277,7 +279,7 @@ public static class ModdedOptionsManager
|
||||
option.Visible = () => visibilityFunc(i);
|
||||
}
|
||||
|
||||
RegisterOption(option, group, property.Name + i, pluginInfo);
|
||||
RegisterOption(option, group, property.Name + (i + 1), pluginInfo);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ namespace MiraAPI.GameOptions.OptionTypes;
|
||||
/// Represents a modded option list.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of options.</typeparam>
|
||||
public class ModdedOptionList<T> : IModdedOptionList
|
||||
public class ModdedOptionList<T> : IModdedOptionList<T>
|
||||
where T : IModdedOption
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
@@ -20,6 +20,9 @@ public class ModdedOptionList<T> : IModdedOptionList
|
||||
/// </summary>
|
||||
public IReadOnlyList<T> Options { get; }
|
||||
|
||||
/// <inheritdoc/>
|
||||
public Func<int, string>? ConfigName { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ModdedOptionList{T}"/> class.
|
||||
/// </summary>
|
||||
@@ -41,10 +44,10 @@ public class ModdedOptionList<T> : IModdedOptionList
|
||||
Options = [.. options];
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IEnumerator<IModdedOption> GetEnumerator()
|
||||
/// <inheritdoc cref="IEnumerable{T}.GetEnumerator"/>
|
||||
public IEnumerator<T> GetEnumerator()
|
||||
{
|
||||
return ((IEnumerable<IModdedOption>)Options).GetEnumerator();
|
||||
return Options.GetEnumerator();
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
@@ -52,6 +55,11 @@ public class ModdedOptionList<T> : IModdedOptionList
|
||||
return Options.GetEnumerator();
|
||||
}
|
||||
|
||||
IEnumerator<IModdedOption> IEnumerable<IModdedOption>.GetEnumerator()
|
||||
{
|
||||
return ((IEnumerable<IModdedOption>)Options).GetEnumerator();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Indexes the option of type <typeparamref name="T"/>.
|
||||
/// </summary>
|
||||
|
||||
Reference in new issue
Block a user