mirror of
https://github.com/All-Of-Us-Mods/MiraAPI.git
synced 2026-10-19 15:51:48 +00:00
Created attribute versions for option list
This commit is contained in:
1 parent
c3384ef850
commit
25794b149b
3 files changed
+160
No files matched your search
@@ -0,0 +1,67 @@
|
||||
using MiraAPI.GameOptions.OptionTypes;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Reflection;
|
||||
|
||||
namespace MiraAPI.GameOptions.Attributes;
|
||||
|
||||
/// <summary>
|
||||
/// Attribute for creating a list of enum options.
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Property)]
|
||||
public class ModdedEnumOptionListAttribute(Func<int, string> titler, Type enumType, string[]? values = null)
|
||||
: ModdedOptionListAttribute(titler)
|
||||
{
|
||||
internal override IModdedOptionList CreateOptionList(IList value, PropertyInfo property)
|
||||
{
|
||||
var optList = new ModdedOptionList<ModdedEnumOption>(
|
||||
value.Count, idx => new(Titler(idx), (int)(value[idx] ?? 0), enumType, values));
|
||||
return optList;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void SetValue(int idx, object value)
|
||||
{
|
||||
var opt = HolderOptionList?[idx] as ModdedEnumOption;
|
||||
opt?.SetValue((int)value);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override object GetValue(int idx)
|
||||
{
|
||||
return HolderOptionList?[idx] is ModdedEnumOption opt
|
||||
? Enum.ToObject(enumType, opt.Value)
|
||||
: throw new InvalidOperationException($"HolderOption for option \"{Titler(idx)}\" with EnumType ${enumType.FullName} is not a ModdedEnumOption");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attribute for creating a list of enum options.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The enum type.</typeparam>
|
||||
[AttributeUsage(AttributeTargets.Property)]
|
||||
public class ModdedEnumOptionListAttribute<T>(Func<int, string> titler, string[]? values = null)
|
||||
: ModdedOptionListAttribute(titler) where T : Enum
|
||||
{
|
||||
internal override IModdedOptionList CreateOptionList(IList value, PropertyInfo property)
|
||||
{
|
||||
var optList = new ModdedOptionList<ModdedEnumOption<T>>(
|
||||
value.Count, idx => new(Titler(idx), (T)(value[idx] ?? 0), values));
|
||||
return optList;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void SetValue(int idx, object value)
|
||||
{
|
||||
var opt = HolderOptionList?[idx] as ModdedEnumOption<T>;
|
||||
opt?.SetValue((T)value);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override object GetValue(int idx)
|
||||
{
|
||||
return HolderOptionList?[idx] is ModdedEnumOption<T> opt
|
||||
? opt.Value
|
||||
: throw new InvalidOperationException($"HolderOption for option \"{Titler(idx)}\" with EnumType ${typeof(T).FullName} is not a ModdedEnumOption");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
using MiraAPI.GameOptions.OptionTypes;
|
||||
using MiraAPI.Utilities;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Reflection;
|
||||
|
||||
namespace MiraAPI.GameOptions.Attributes;
|
||||
|
||||
/// <summary>
|
||||
/// A number option attribute for the modded options system.
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Property)]
|
||||
public class ModdedNumberOptionListAttribute(
|
||||
Func<int, string> titler,
|
||||
float min,
|
||||
float max,
|
||||
float increment = 1,
|
||||
MiraNumberSuffixes suffixType = MiraNumberSuffixes.None,
|
||||
string? formatString = null,
|
||||
bool zeroInfinity = false)
|
||||
: ModdedOptionListAttribute(titler)
|
||||
{
|
||||
internal override IModdedOptionList CreateOptionList(IList value, PropertyInfo property)
|
||||
{
|
||||
return new ModdedOptionList<ModdedNumberOption>(value.Count, idx =>
|
||||
{
|
||||
return new(
|
||||
Titler(idx),
|
||||
(float)(value[idx] ?? min + increment),
|
||||
min,
|
||||
max,
|
||||
increment,
|
||||
suffixType,
|
||||
formatString,
|
||||
zeroInfinity);
|
||||
});
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void SetValue(int idx, object value)
|
||||
{
|
||||
var opt = HolderOptionList?[idx] as ModdedNumberOption;
|
||||
opt?.SetValue((float)value);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override object GetValue(int idx)
|
||||
{
|
||||
if (HolderOptionList?[idx] is ModdedNumberOption opt)
|
||||
{
|
||||
return opt.Value;
|
||||
}
|
||||
throw new InvalidOperationException($"HolderOption for option \"{Titler(idx)}\" is not a ModdedNumberOption");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
using MiraAPI.GameOptions.OptionTypes;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Reflection;
|
||||
|
||||
namespace MiraAPI.GameOptions.Attributes;
|
||||
|
||||
/// <summary>
|
||||
/// Attribute for a list of toggle options.
|
||||
/// </summary>
|
||||
/// <param name="titler">The function to get each option's title.</param>
|
||||
[AttributeUsage(AttributeTargets.Property)]
|
||||
public class ModdedToggleOptionListAttribute(Func<int, string> titler) : ModdedOptionListAttribute(titler)
|
||||
{
|
||||
internal override IModdedOptionList CreateOptionList(IList value, PropertyInfo property)
|
||||
{
|
||||
var optList = new ModdedOptionList<ModdedToggleOption>(
|
||||
value.Count, idx => new(Titler(idx), (bool)(value[idx] ?? false)));
|
||||
return optList;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void SetValue(int idx, object value)
|
||||
{
|
||||
var opt = HolderOptionList?[idx] as ModdedToggleOption;
|
||||
opt?.SetValue((bool)value);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override object GetValue(int idx)
|
||||
{
|
||||
if (HolderOptionList?[idx] is ModdedToggleOption opt)
|
||||
{
|
||||
return opt.Value;
|
||||
}
|
||||
throw new InvalidOperationException($"Holder option for {Titler(idx)} is not a ModdedToggleOption.");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user