mirror of
https://github.com/All-Of-Us-Mods/MiraAPI.git
synced 2026-10-20 01:46:15 +00:00
Some more modded enum option changes
This commit is contained in:
1 parent
7cbf9ffe1b
commit
5eba1c9d7f
2 files changed
+38
-6
No files matched your search
@@ -32,3 +32,33 @@ public class ModdedEnumOptionAttribute(string title, Type enumType, string[]? va
|
||||
: throw new InvalidOperationException($"HolderOption for option \"{Title}\" with EnumType ${enumType.FullName} is not a ModdedEnumOption");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generic attribute for creating an enum option.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">An enum type.</typeparam>
|
||||
[AttributeUsage(AttributeTargets.Property)]
|
||||
public class ModdedEnumOptionAttribute<T>(string title, string[]? values = null)
|
||||
: ModdedOptionAttribute(title) where T : struct, Enum
|
||||
{
|
||||
internal override IModdedOption CreateOption(object? value, PropertyInfo property)
|
||||
{
|
||||
var opt = new ModdedEnumOption<T>(Title, (T)(value ?? 0), values);
|
||||
return opt;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void SetValue(object value)
|
||||
{
|
||||
var opt = HolderOption as ModdedEnumOption;
|
||||
opt?.SetValue((int)value);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override object GetValue()
|
||||
{
|
||||
return HolderOption is ModdedEnumOption<T> opt
|
||||
? Enum.ToObject(typeof(T), opt.Value)
|
||||
: throw new InvalidOperationException($"HolderOption for option \"{Title}\" is not a ModdedEnumOption<{typeof(T).Name}>");
|
||||
}
|
||||
}
|
||||
@@ -35,9 +35,10 @@ public class ModdedEnumOption : ModdedOption<int>
|
||||
|
||||
data.Title = StringName;
|
||||
data.Type = global::OptionTypes.String;
|
||||
data.Values = values is null ?
|
||||
Enum.GetNames(enumType).Select(CustomStringName.CreateAndRegister).ToArray()
|
||||
: values.Select(CustomStringName.CreateAndRegister).ToArray();
|
||||
data.Values = (values is null
|
||||
? Enum.GetNames(enumType)
|
||||
: values)
|
||||
.Select(CustomStringName.CreateAndRegister).ToArray();
|
||||
|
||||
data.Index = Value;
|
||||
}
|
||||
@@ -50,7 +51,7 @@ public class ModdedEnumOption : ModdedOption<int>
|
||||
stringOption.SetUpFromData(Data, 20);
|
||||
stringOption.OnValueChanged = (Il2CppSystem.Action<OptionBehaviour>)ValueChanged;
|
||||
|
||||
// SetUpFromData method doesnt work correctly so we must set the values manually
|
||||
// SetUpFromData method doesn't work correctly so we must set the values manually
|
||||
stringOption.Title = StringName;
|
||||
stringOption.Values = (Data as StringGameSetting)?.Values ?? new Il2CppStructArray<StringNames>(0);
|
||||
stringOption.Value = Value;
|
||||
@@ -126,8 +127,9 @@ public class ModdedEnumOption<T> : ModdedOption<T> where T : struct, Enum
|
||||
data.Title = StringName;
|
||||
data.Type = global::OptionTypes.String;
|
||||
data.Values = (values is null
|
||||
? Enum.GetNames<T>().Select(CustomStringName.CreateAndRegister)
|
||||
: values.Select(CustomStringName.CreateAndRegister)).ToArray();
|
||||
? Enum.GetNames<T>()
|
||||
: values)
|
||||
.Select(CustomStringName.CreateAndRegister).ToArray();
|
||||
|
||||
data.Index = Convert.ToInt32(Value, NumberFormatInfo.InvariantInfo);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user