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
+155
-59
No files matched your search
@@ -4,6 +4,7 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using Il2CppInterop.Runtime.InteropTypes.Arrays;
|
||||
using MiraAPI.Networking;
|
||||
using MiraAPI.PluginLoading;
|
||||
using MiraAPI.Translation;
|
||||
using UnityEngine;
|
||||
using Object = UnityEngine.Object;
|
||||
@@ -36,7 +37,7 @@ public class ModdedEnumOption : ModdedOption<int>
|
||||
var data = ScriptableObject.CreateInstance<StringGameSetting>();
|
||||
data.Title = StringName;
|
||||
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 = data;
|
||||
@@ -135,12 +136,18 @@ public class ModdedEnumOption<T> : ModdedOption<T>
|
||||
var data = ScriptableObject.CreateInstance<StringGameSetting>();
|
||||
data.Title = StringName;
|
||||
data.Type = global::OptionTypes.String;
|
||||
data.Values = Values.Select(MiraLocaleManager.GetOrCreateLocaleString).ToArray();
|
||||
data.Index = Convert.ToInt32(Value, NumberFormatInfo.InvariantInfo);
|
||||
|
||||
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 />
|
||||
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.
|
||||
/// </summary>
|
||||
/// <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 />
|
||||
public uint Id { get; }
|
||||
public uint Id { get; } = ModdedOptionsManager.NextId;
|
||||
|
||||
/// <inheritdoc />
|
||||
public string Title { get; set; }
|
||||
public string Title { get; set; } = title;
|
||||
|
||||
/// <inheritdoc />
|
||||
public StringNames StringName { get; }
|
||||
public StringNames StringName { get; private set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public BaseGameSetting Data { get; protected set; } = null!;
|
||||
@@ -34,6 +37,7 @@ public abstract class ModdedOption<T> : IModdedOption
|
||||
{
|
||||
if (field != null || value == null) return;
|
||||
field = value;
|
||||
OnParentModSet(value);
|
||||
|
||||
var entry = field.GetConfigFile().Bind(ConfigDefinition, DefaultValue);
|
||||
Value = entry.Value;
|
||||
@@ -43,12 +47,12 @@ public abstract class ModdedOption<T> : IModdedOption
|
||||
/// <summary>
|
||||
/// Gets or sets the value of the option.
|
||||
/// </summary>
|
||||
public T Value { get; protected set; }
|
||||
public T Value { get; protected set; } = defaultValue;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the default value of the option.
|
||||
/// </summary>
|
||||
public T DefaultValue { get; }
|
||||
public T DefaultValue { get; } = defaultValue;
|
||||
|
||||
/// <summary>
|
||||
/// 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; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public Func<bool> Visible { get; set; }
|
||||
public Func<bool> Visible { get; set; } = () => true;
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool IncludeInPreset { get; set; }
|
||||
public bool IncludeInPreset { get; set; } = includeInPreset;
|
||||
|
||||
/// <inheritdoc />
|
||||
public OptionBehaviour? OptionBehaviour { get; protected set; }
|
||||
@@ -67,22 +71,11 @@ public abstract class ModdedOption<T> : IModdedOption
|
||||
/// <inheritdoc />
|
||||
public ConfigDefinition? ConfigDefinition { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ModdedOption{T}"/> class.
|
||||
/// </summary>
|
||||
/// <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>
|
||||
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;
|
||||
}
|
||||
/// <inheritdoc />
|
||||
public AbstractOptionGroup ParentGroup { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public virtual OptionNotifConfiguration Configuration => new(ParentGroup);
|
||||
|
||||
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 />
|
||||
public abstract float GetFloatData();
|
||||
|
||||
@@ -188,10 +190,4 @@ public abstract class ModdedOption<T> : IModdedOption
|
||||
{
|
||||
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 Il2CppInterop.Runtime.InteropTypes.Arrays;
|
||||
using MiraAPI.Networking;
|
||||
using MiraAPI.PluginLoading;
|
||||
using MiraAPI.Translation;
|
||||
using UnityEngine;
|
||||
using Object = UnityEngine.Object;
|
||||
@@ -57,43 +58,41 @@ public class ModdedStringOption : ModdedOption<string>
|
||||
newVals.Insert(index, value);
|
||||
Values = [.. newVals];
|
||||
var newStrVals = StringNameValues.ToList();
|
||||
newStrVals.Insert(index, MiraLocaleManager.GetOrCreateLocaleString(value));
|
||||
newStrVals.Insert(index, MiraLocaleManager.GetOrCreateLocaleString(ParentMod!.IdBuilder.CreateStringOptionId(value)));
|
||||
StringNameValues = [.. newStrVals];
|
||||
}
|
||||
|
||||
if (Data != null)
|
||||
if (Data == null)
|
||||
{
|
||||
Error("Data is null.");
|
||||
return;
|
||||
}
|
||||
|
||||
var data = (StringGameSetting)Data;
|
||||
data.Values = StringNameValues;
|
||||
|
||||
data.Index = GetIndex(Value);
|
||||
if (OptionBehaviour != null)
|
||||
|
||||
Warning("Data is valid");
|
||||
|
||||
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.");
|
||||
Error("OptionBehaviour is null.");
|
||||
return;
|
||||
}
|
||||
|
||||
Warning("OptionBehaviour is valid");
|
||||
}
|
||||
else
|
||||
var strOpt = OptionBehaviour.TryCast<StringOption>();
|
||||
|
||||
if (strOpt == null)
|
||||
{
|
||||
Error("OptionBehaviour is null.");
|
||||
Error("StringOption is null.");
|
||||
return;
|
||||
}
|
||||
|
||||
Warning("Data is valid");
|
||||
}
|
||||
else
|
||||
{
|
||||
Error("Data is null.");
|
||||
}
|
||||
strOpt.Values = StringNameValues;
|
||||
strOpt.Value = GetIndex(Value);
|
||||
Warning("StringOption is valid");
|
||||
return;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -112,12 +111,18 @@ public class ModdedStringOption : ModdedOption<string>
|
||||
|
||||
data.Title = StringName;
|
||||
data.Type = global::OptionTypes.String;
|
||||
StringNameValues = [.. values.Select(MiraLocaleManager.GetOrCreateLocaleString)];
|
||||
data.Values = StringNameValues;
|
||||
|
||||
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 />
|
||||
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");
|
||||
|
||||
/// <inheritdoc />
|
||||
public string OptionsTitleText => "MiraAPI";
|
||||
public string OptionsTitleText { get; } = "MiraAPI";
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool DisplayOnOptionsMenu => false;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using BepInEx.Configuration;
|
||||
using MiraAPI.Translation;
|
||||
|
||||
namespace MiraAPI.PluginLoading;
|
||||
|
||||
@@ -58,6 +59,11 @@ public interface IMiraPlugin
|
||||
/// </summary>
|
||||
string CustomOptionMenuTwoDescription => "Apply game settings for this mod!";
|
||||
|
||||
/// <summary>
|
||||
/// Gets the translation id builder for this mod.
|
||||
/// </summary>
|
||||
TranslationIdBuilder IdBuilder => TranslationIdBuilder.Default;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="ConfigFile"/> for the plugin.
|
||||
/// </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