Skip to content

Commit

Permalink
add ModdedEnumOption<T>
Browse files Browse the repository at this point in the history
  • Loading branch information
XtraCube committed Jan 30, 2025
1 parent d5ac231 commit 51fcc2c
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 2 deletions.
4 changes: 2 additions & 2 deletions MiraAPI.Example/Options/ExampleOptions2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ public class ExampleOptions2 : AbstractOptionGroup
Visible = () => OptionGroupSingleton<ExampleOptions2>.Instance.ToggleOpt1, // implicit cast from ModdedToggleOption to bool
};

public ModdedEnumOption EnumOpt { get; } = new("Enum Opt", 0, typeof(TestingData))
public ModdedEnumOption<TestingData> EnumOpt { get; } = new("Enum Opt", 0)
{
ChangedEvent = x => Logger<ExamplePlugin>.Info($"changed Enum Opt to {x}"),
};
}

public enum TestingData
public enum TestingData : ulong
{
Happy,
Sad,
Expand Down
95 changes: 95 additions & 0 deletions MiraAPI/GameOptions/OptionTypes/ModdedEnumOption.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Globalization;
using System.Linq;
using System.Text;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using MiraAPI.Networking;
using Reactor.Localization.Utilities;
Expand Down Expand Up @@ -98,3 +100,96 @@ protected override void OnValueChanged(int newValue)
}
}
}


/// <summary>
/// An option for selecting an enum value.
/// </summary>
/// <typeparam name="T">The enum type.</typeparam>
public class ModdedEnumOption<T> : ModdedOption<T> where T : Enum
{
/// <summary>
/// Gets the string values of the enum.
/// </summary>
public string[]? Values { get; }

/// <summary>
/// Initializes a new instance of the <see cref="ModdedEnumOption{T}"/> class.
/// </summary>
/// <param name="title">The title of the option.</param>
/// <param name="defaultValue">The default value as an int.</param>
/// <param name="enumType">The Enum type.</param>
/// <param name="values">An option list of string values to use in place of the enum name.</param>
/// <param name="roleType">An optional role type to specify for a specific role.</param>
public ModdedEnumOption(string title, T defaultValue, string[]? values = null, Type? roleType = null) : base(title, defaultValue, roleType)
{
Values = values ?? Enum.GetNames(typeof(T));
Data = ScriptableObject.CreateInstance<StringGameSetting>();
var data = (StringGameSetting)Data;

data.Title = StringName;
data.Type = global::OptionTypes.String;
data.Values = values is null ?
Enum.GetNames(typeof(T)).Select(CustomStringName.CreateAndRegister).ToArray()
: values.Select(CustomStringName.CreateAndRegister).ToArray();

data.Index = Convert.ToInt32(Value);
}

/// <inheritdoc />
public override OptionBehaviour CreateOption(ToggleOption toggleOpt, NumberOption numberOpt, StringOption stringOpt, Transform container)
{
var stringOption = Object.Instantiate(stringOpt, container);

stringOption.SetUpFromData(Data, 20);
stringOption.OnValueChanged = (Il2CppSystem.Action<OptionBehaviour>)ValueChanged;

// SetUpFromData method doesnt work correctly so we must set the values manually
stringOption.Title = StringName;
stringOption.Values = (Data as StringGameSetting)?.Values ?? new Il2CppStructArray<StringNames>(0);
stringOption.Value = Convert.ToInt32(Value);

OptionBehaviour = stringOption;

return stringOption;
}

/// <inheritdoc />
public override float GetFloatData()
{
return Convert.ToSingle(Value);
}

/// <inheritdoc />
public override NetData GetNetData()
{
return new NetData(Id, Encoding.Unicode.GetBytes(Convert.ToString(Value)));

Check warning on line 166 in MiraAPI/GameOptions/OptionTypes/ModdedEnumOption.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 's' in 'byte[] Encoding.GetBytes(string s)'.
}

/// <inheritdoc />
public override void HandleNetData(byte[] data)
{
SetValue((T)Enum.Parse(typeof(T), Encoding.Unicode.GetString(data)));
}

/// <inheritdoc />
public override T GetValueFromOptionBehaviour(OptionBehaviour optionBehaviour)
{
return (T)Enum.Parse(typeof(T), optionBehaviour.GetInt().ToString());
}

/// <inheritdoc />
protected override void OnValueChanged(T newValue)
{
HudManager.Instance.Notifier.AddSettingsChangeMessage(StringName, Data?.GetValueString(Convert.ToInt32(newValue)), false);
if (!OptionBehaviour)
{
return;
}

if (OptionBehaviour is StringOption opt)
{
opt.Value = Convert.ToInt32(newValue);
}
}
}

0 comments on commit 51fcc2c

Please sign in to comment.