mirror of
https://github.com/All-Of-Us-Mods/MiraAPI.git
synced 2026-10-19 15:51:48 +00:00
wip translation id builder
This commit is contained in:
6 files changed
+163
-67
No files matched your search
@@ -4,6 +4,7 @@ using System.Linq;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using Il2CppInterop.Runtime.InteropTypes.Arrays;
|
using Il2CppInterop.Runtime.InteropTypes.Arrays;
|
||||||
using MiraAPI.Networking;
|
using MiraAPI.Networking;
|
||||||
|
using MiraAPI.PluginLoading;
|
||||||
using MiraAPI.Translation;
|
using MiraAPI.Translation;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using Object = UnityEngine.Object;
|
using Object = UnityEngine.Object;
|
||||||
@@ -36,7 +37,7 @@ public class ModdedEnumOption : ModdedOption<int>
|
|||||||
var data = ScriptableObject.CreateInstance<StringGameSetting>();
|
var data = ScriptableObject.CreateInstance<StringGameSetting>();
|
||||||
data.Title = StringName;
|
data.Title = StringName;
|
||||||
data.Type = global::OptionTypes.String;
|
data.Type = global::OptionTypes.String;
|
||||||
data.Values = Values.Select(MiraLocaleManager.GetOrCreateLocaleString).ToArray();
|
data.Values = Values.Select(x => MiraLocaleManager.GetOrCreateLocaleString(ParentMod!.IdBuilder.CreateEnumOptionId(x, enumType))).ToArray();
|
||||||
data.Index = Value;
|
data.Index = Value;
|
||||||
|
|
||||||
Data = data;
|
Data = data;
|
||||||
@@ -135,12 +136,18 @@ public class ModdedEnumOption<T> : ModdedOption<T>
|
|||||||
var data = ScriptableObject.CreateInstance<StringGameSetting>();
|
var data = ScriptableObject.CreateInstance<StringGameSetting>();
|
||||||
data.Title = StringName;
|
data.Title = StringName;
|
||||||
data.Type = global::OptionTypes.String;
|
data.Type = global::OptionTypes.String;
|
||||||
data.Values = Values.Select(MiraLocaleManager.GetOrCreateLocaleString).ToArray();
|
|
||||||
data.Index = Convert.ToInt32(Value, NumberFormatInfo.InvariantInfo);
|
data.Index = Convert.ToInt32(Value, NumberFormatInfo.InvariantInfo);
|
||||||
|
|
||||||
Data = data;
|
Data = data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void OnParentModSet(IMiraPlugin plugin)
|
||||||
|
{
|
||||||
|
base.OnParentModSet(plugin);
|
||||||
|
((StringGameSetting)Data).Values = Values.Select(x => MiraLocaleManager.GetOrCreateLocaleString(ParentMod!.IdBuilder.CreateEnumOptionId<T>(x))).ToArray();
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override OptionBehaviour CreateOption(ToggleOption toggleOpt, NumberOption numberOpt, StringOption stringOpt, PlayerOption playerOpt, Transform container)
|
public override OptionBehaviour CreateOption(ToggleOption toggleOpt, NumberOption numberOpt, StringOption stringOpt, PlayerOption playerOpt, Transform container)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -12,16 +12,19 @@ namespace MiraAPI.GameOptions.OptionTypes;
|
|||||||
/// Represents a modded option.
|
/// Represents a modded option.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T">The value type.</typeparam>
|
/// <typeparam name="T">The value type.</typeparam>
|
||||||
public abstract class ModdedOption<T> : IModdedOption
|
/// <param name="title">The option title.</param>
|
||||||
|
/// <param name="defaultValue">The default value.</param>
|
||||||
|
/// <param name="includeInPreset">Whether to include the option in the preset.</param>
|
||||||
|
public abstract class ModdedOption<T>(string title, T defaultValue, bool includeInPreset = true) : IModdedOption
|
||||||
{
|
{
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public uint Id { get; }
|
public uint Id { get; } = ModdedOptionsManager.NextId;
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public string Title { get; set; }
|
public string Title { get; set; } = title;
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public StringNames StringName { get; }
|
public StringNames StringName { get; private set; }
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public BaseGameSetting Data { get; protected set; } = null!;
|
public BaseGameSetting Data { get; protected set; } = null!;
|
||||||
@@ -34,6 +37,7 @@ public abstract class ModdedOption<T> : IModdedOption
|
|||||||
{
|
{
|
||||||
if (field != null || value == null) return;
|
if (field != null || value == null) return;
|
||||||
field = value;
|
field = value;
|
||||||
|
OnParentModSet(value);
|
||||||
|
|
||||||
var entry = field.GetConfigFile().Bind(ConfigDefinition, DefaultValue);
|
var entry = field.GetConfigFile().Bind(ConfigDefinition, DefaultValue);
|
||||||
Value = entry.Value;
|
Value = entry.Value;
|
||||||
@@ -43,12 +47,12 @@ public abstract class ModdedOption<T> : IModdedOption
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the value of the option.
|
/// Gets or sets the value of the option.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public T Value { get; protected set; }
|
public T Value { get; protected set; } = defaultValue;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the default value of the option.
|
/// Gets the default value of the option.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public T DefaultValue { get; }
|
public T DefaultValue { get; } = defaultValue;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the event that is invoked when the value of the option changes.
|
/// Gets or sets the event that is invoked when the value of the option changes.
|
||||||
@@ -56,10 +60,10 @@ public abstract class ModdedOption<T> : IModdedOption
|
|||||||
public Action<T>? ChangedEvent { get; set; }
|
public Action<T>? ChangedEvent { get; set; }
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public Func<bool> Visible { get; set; }
|
public Func<bool> Visible { get; set; } = () => true;
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public bool IncludeInPreset { get; set; }
|
public bool IncludeInPreset { get; set; } = includeInPreset;
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public OptionBehaviour? OptionBehaviour { get; protected set; }
|
public OptionBehaviour? OptionBehaviour { get; protected set; }
|
||||||
@@ -67,22 +71,11 @@ public abstract class ModdedOption<T> : IModdedOption
|
|||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public ConfigDefinition? ConfigDefinition { get; set; }
|
public ConfigDefinition? ConfigDefinition { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <inheritdoc />
|
||||||
/// Initializes a new instance of the <see cref="ModdedOption{T}"/> class.
|
public AbstractOptionGroup ParentGroup { get; set; }
|
||||||
/// </summary>
|
|
||||||
/// <param name="title">The option title.</param>
|
/// <inheritdoc />
|
||||||
/// <param name="defaultValue">The default value.</param>
|
public virtual OptionNotifConfiguration Configuration => new(ParentGroup);
|
||||||
/// <param name="includeInPreset">Whether to include the option in the preset.</param>
|
|
||||||
protected ModdedOption(string title, T defaultValue, bool includeInPreset = true)
|
|
||||||
{
|
|
||||||
Id = ModdedOptionsManager.NextId;
|
|
||||||
Title = title.Translate();
|
|
||||||
DefaultValue = defaultValue;
|
|
||||||
Value = defaultValue;
|
|
||||||
StringName = MiraLocaleManager.GetOrCreateLocaleString(Title);
|
|
||||||
Visible = () => true;
|
|
||||||
IncludeInPreset = includeInPreset;
|
|
||||||
}
|
|
||||||
|
|
||||||
internal void ValueChanged(OptionBehaviour optionBehaviour)
|
internal void ValueChanged(OptionBehaviour optionBehaviour)
|
||||||
{
|
{
|
||||||
@@ -149,6 +142,15 @@ public abstract class ModdedOption<T> : IModdedOption
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// An event that is invoked when the option's parent mod as been assigned to the option.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="plugin">The plugin that was assigned.</param>
|
||||||
|
protected virtual void OnParentModSet(IMiraPlugin plugin)
|
||||||
|
{
|
||||||
|
StringName = MiraLocaleManager.GetOrCreateLocaleString(plugin.IdBuilder.CreateOptionTitleId(Title));
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public abstract float GetFloatData();
|
public abstract float GetFloatData();
|
||||||
|
|
||||||
@@ -188,10 +190,4 @@ public abstract class ModdedOption<T> : IModdedOption
|
|||||||
{
|
{
|
||||||
return option.Value;
|
return option.Value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public AbstractOptionGroup ParentGroup { get; set; }
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public virtual OptionNotifConfiguration Configuration => new(ParentGroup);
|
|
||||||
}
|
}
|
||||||
@@ -4,6 +4,7 @@ using System.Linq;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using Il2CppInterop.Runtime.InteropTypes.Arrays;
|
using Il2CppInterop.Runtime.InteropTypes.Arrays;
|
||||||
using MiraAPI.Networking;
|
using MiraAPI.Networking;
|
||||||
|
using MiraAPI.PluginLoading;
|
||||||
using MiraAPI.Translation;
|
using MiraAPI.Translation;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using Object = UnityEngine.Object;
|
using Object = UnityEngine.Object;
|
||||||
@@ -57,43 +58,41 @@ public class ModdedStringOption : ModdedOption<string>
|
|||||||
newVals.Insert(index, value);
|
newVals.Insert(index, value);
|
||||||
Values = [.. newVals];
|
Values = [.. newVals];
|
||||||
var newStrVals = StringNameValues.ToList();
|
var newStrVals = StringNameValues.ToList();
|
||||||
newStrVals.Insert(index, MiraLocaleManager.GetOrCreateLocaleString(value));
|
newStrVals.Insert(index, MiraLocaleManager.GetOrCreateLocaleString(ParentMod!.IdBuilder.CreateStringOptionId(value)));
|
||||||
StringNameValues = [.. newStrVals];
|
StringNameValues = [.. newStrVals];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Data != null)
|
if (Data == null)
|
||||||
{
|
|
||||||
var data = (StringGameSetting)Data;
|
|
||||||
data.Values = StringNameValues;
|
|
||||||
|
|
||||||
data.Index = GetIndex(Value);
|
|
||||||
if (OptionBehaviour != null)
|
|
||||||
{
|
|
||||||
var strOpt = OptionBehaviour.TryCast<StringOption>();
|
|
||||||
if (strOpt != null)
|
|
||||||
{
|
|
||||||
strOpt.Values = StringNameValues;
|
|
||||||
strOpt.Value = GetIndex(Value);
|
|
||||||
Warning("StringOption is valid");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Error("StringOption is null.");
|
|
||||||
}
|
|
||||||
|
|
||||||
Warning("OptionBehaviour is valid");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Error("OptionBehaviour is null.");
|
|
||||||
}
|
|
||||||
|
|
||||||
Warning("Data is valid");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
Error("Data is null.");
|
Error("Data is null.");
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var data = (StringGameSetting)Data;
|
||||||
|
data.Values = StringNameValues;
|
||||||
|
data.Index = GetIndex(Value);
|
||||||
|
|
||||||
|
Warning("Data is valid");
|
||||||
|
|
||||||
|
if (OptionBehaviour == null)
|
||||||
|
{
|
||||||
|
Error("OptionBehaviour is null.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Warning("OptionBehaviour is valid");
|
||||||
|
var strOpt = OptionBehaviour.TryCast<StringOption>();
|
||||||
|
|
||||||
|
if (strOpt == null)
|
||||||
|
{
|
||||||
|
Error("StringOption is null.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
strOpt.Values = StringNameValues;
|
||||||
|
strOpt.Value = GetIndex(Value);
|
||||||
|
Warning("StringOption is valid");
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -112,12 +111,18 @@ public class ModdedStringOption : ModdedOption<string>
|
|||||||
|
|
||||||
data.Title = StringName;
|
data.Title = StringName;
|
||||||
data.Type = global::OptionTypes.String;
|
data.Type = global::OptionTypes.String;
|
||||||
StringNameValues = [.. values.Select(MiraLocaleManager.GetOrCreateLocaleString)];
|
|
||||||
data.Values = StringNameValues;
|
|
||||||
|
|
||||||
data.Index = GetIndex(Value);
|
data.Index = GetIndex(Value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void OnParentModSet(IMiraPlugin plugin)
|
||||||
|
{
|
||||||
|
base.OnParentModSet(plugin);
|
||||||
|
|
||||||
|
StringNameValues = [.. Values.Select(x => MiraLocaleManager.GetOrCreateLocaleString(plugin.IdBuilder.CreateStringOptionId(x)))];
|
||||||
|
((StringGameSetting)Data).Values = StringNameValues;
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override OptionBehaviour CreateOption(ToggleOption toggleOpt, NumberOption numberOpt, StringOption stringOpt, PlayerOption playerOpt, Transform container)
|
public override OptionBehaviour CreateOption(ToggleOption toggleOpt, NumberOption numberOpt, StringOption stringOpt, PlayerOption playerOpt, Transform container)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ public partial class MiraApiPlugin : BasePlugin, IMiraPlugin
|
|||||||
public static CultureInfo Culture { get; internal set; } = new("en-US");
|
public static CultureInfo Culture { get; internal set; } = new("en-US");
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public string OptionsTitleText => "MiraAPI";
|
public string OptionsTitleText { get; } = "MiraAPI";
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public bool DisplayOnOptionsMenu => false;
|
public bool DisplayOnOptionsMenu => false;
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using BepInEx.Configuration;
|
using BepInEx.Configuration;
|
||||||
|
using MiraAPI.Translation;
|
||||||
|
|
||||||
namespace MiraAPI.PluginLoading;
|
namespace MiraAPI.PluginLoading;
|
||||||
|
|
||||||
@@ -58,6 +59,11 @@ public interface IMiraPlugin
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
string CustomOptionMenuTwoDescription => "Apply game settings for this mod!";
|
string CustomOptionMenuTwoDescription => "Apply game settings for this mod!";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the translation id builder for this mod.
|
||||||
|
/// </summary>
|
||||||
|
TranslationIdBuilder IdBuilder => TranslationIdBuilder.Default;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the <see cref="ConfigFile"/> for the plugin.
|
/// Gets the <see cref="ConfigFile"/> for the plugin.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -0,0 +1,82 @@
|
|||||||
|
using System;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
|
||||||
|
namespace MiraAPI.Translation;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A utility class that can build ids based on the provided id part.
|
||||||
|
/// </summary>
|
||||||
|
public class TranslationIdBuilder
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets an instance of <see cref="TranslationIdBuilder"/> that has default behaviour.
|
||||||
|
/// </summary>
|
||||||
|
public static TranslationIdBuilder Default { get; } = new();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Constructs a translation id for a provided enum option value.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The type of the enum.</typeparam>
|
||||||
|
/// <param name="value">The enum option value.</param>
|
||||||
|
/// <returns>The constructed translation id.</returns>
|
||||||
|
public virtual string CreateEnumOptionId<T>(T value)
|
||||||
|
where T : Enum
|
||||||
|
{
|
||||||
|
return CreateEnumOptionId<T>(value.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Constructs a translation id for a provided enum option value.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The type of the enum.</typeparam>
|
||||||
|
/// <param name="value">The enum option value.</param>
|
||||||
|
/// <returns>The constructed translation id.</returns>
|
||||||
|
public virtual string CreateEnumOptionId<T>(string value)
|
||||||
|
where T : Enum
|
||||||
|
{
|
||||||
|
return CreateEnumOptionId(value, typeof(T));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Constructs a translation id for a provided enum option value.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="value">The enum option value.</param>
|
||||||
|
/// <param name="enumType">The type of the enum.</param>
|
||||||
|
/// <returns>The constructed translation id.</returns>
|
||||||
|
public virtual string CreateEnumOptionId(object value, Type enumType)
|
||||||
|
{
|
||||||
|
return CreateEnumOptionId(value.ToString()!, enumType);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Constructs a translation id for a provided enum option value.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="value">The enum option value.</param>
|
||||||
|
/// <param name="enumType">The type of the enum.</param>
|
||||||
|
/// <returns>The constructed translation id.</returns>
|
||||||
|
public virtual string CreateEnumOptionId(string value, Type enumType)
|
||||||
|
{
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Constructs a translation id for a string option.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="option">The id part.</param>
|
||||||
|
/// <returns>The constructed translation id.</returns>
|
||||||
|
[SuppressMessage("Naming", "CA1716:Identifiers should not match keywords", Justification = "Just confusing.")]
|
||||||
|
public virtual string CreateStringOptionId(string option)
|
||||||
|
{
|
||||||
|
return option;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Constructs a translation id for an option's title.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="title">The id part.</param>
|
||||||
|
/// <returns>The constructed translation id.</returns>
|
||||||
|
public virtual string CreateOptionTitleId(string title)
|
||||||
|
{
|
||||||
|
return title;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user