mirror of
https://github.com/soarqin/DSP_Mods.git
synced 2025-12-09 03:33:29 +08:00
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using HarmonyLib;
|
||||
|
||||
namespace UXAssist.Common;
|
||||
@@ -8,6 +7,7 @@ namespace UXAssist.Common;
|
||||
public static class I18N
|
||||
{
|
||||
private static bool _initialized;
|
||||
private static bool _dirty;
|
||||
|
||||
public static Action OnInitialized;
|
||||
|
||||
@@ -17,88 +17,123 @@ public static class I18N
|
||||
}
|
||||
|
||||
public static bool Initialized() => _initialized;
|
||||
private struct Translation
|
||||
{
|
||||
public string Key;
|
||||
public string English;
|
||||
public string Chinese;
|
||||
}
|
||||
private static readonly List<Translation> StringsToAdd = [];
|
||||
private static readonly List<Tuple<string, string, int>> Keys = [];
|
||||
private static readonly Dictionary<int, List<string>> Strings = [];
|
||||
|
||||
public static void Add(string key, string enus, string zhcn = null)
|
||||
{
|
||||
if (zhcn == null && key == enus) return;
|
||||
var strProto = new Translation
|
||||
Keys.Add(Tuple.Create(key, enus, -1));
|
||||
if (Strings.TryGetValue(2052, out var zhcnList))
|
||||
{
|
||||
Key = key,
|
||||
English = enus,
|
||||
Chinese = string.IsNullOrEmpty(zhcn) ? enus : zhcn
|
||||
};
|
||||
StringsToAdd.Add(strProto);
|
||||
zhcnList.Add(string.IsNullOrEmpty(zhcn) ? enus : zhcn);
|
||||
}
|
||||
else
|
||||
{
|
||||
Strings.Add(2052, [string.IsNullOrEmpty(zhcn) ? enus : zhcn]);
|
||||
}
|
||||
_dirty = true;
|
||||
}
|
||||
|
||||
private static void ApplyIndexers()
|
||||
{
|
||||
var indexer = Localization.namesIndexer;
|
||||
var index = indexer.Count;
|
||||
var len = Keys.Count;
|
||||
for (var i = 0; i < len; i++)
|
||||
{
|
||||
var (key, def, value) = Keys[i];
|
||||
if (value >= 0) continue;
|
||||
if (indexer.TryGetValue(key, out var idx))
|
||||
{
|
||||
Keys[i] = Tuple.Create(key, def, idx);
|
||||
continue;
|
||||
}
|
||||
indexer[key] = index;
|
||||
Keys[i] = Tuple.Create(key, def, index);
|
||||
index++;
|
||||
}
|
||||
_dirty = false;
|
||||
var strings = Localization.strings;
|
||||
if (strings == null) return;
|
||||
var len2 = strings.Length;
|
||||
for (var i = 0; i < len2; i++)
|
||||
{
|
||||
ApplyLanguage(i);
|
||||
if (i != Localization.currentLanguageIndex) continue;
|
||||
Localization.currentStrings = Localization.strings[i];
|
||||
Localization.currentFloats = Localization.floats[i];
|
||||
}
|
||||
}
|
||||
|
||||
private static void ApplyLanguage(int index)
|
||||
{
|
||||
var indexerLength = Localization.namesIndexer.Count;
|
||||
var strs = Localization.strings[index];
|
||||
if (strs == null) return;
|
||||
if (strs.Length < indexerLength)
|
||||
{
|
||||
var newStrs = new string[indexerLength];
|
||||
Array.Copy(strs, newStrs, strs.Length);
|
||||
strs = newStrs;
|
||||
Localization.strings[index] = strs;
|
||||
}
|
||||
var floats = Localization.floats[index];
|
||||
if (floats != null)
|
||||
{
|
||||
if (floats.Length < indexerLength)
|
||||
{
|
||||
var newFloats = new float[indexerLength];
|
||||
Array.Copy(floats, newFloats, floats.Length);
|
||||
floats = newFloats;
|
||||
Localization.floats[index] = floats;
|
||||
}
|
||||
}
|
||||
|
||||
var keyLength = Keys.Count;
|
||||
if (Strings.TryGetValue(Localization.Languages[index].lcId, out var list))
|
||||
{
|
||||
for (var j = 0; j < keyLength; j++)
|
||||
{
|
||||
strs[Keys[j].Item3] = list[j];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (var j = 0; j < keyLength; j++)
|
||||
{
|
||||
strs[Keys[j].Item3] = Keys[j].Item2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void Apply()
|
||||
{
|
||||
if (!_initialized) return;
|
||||
var indexer = Localization.namesIndexer;
|
||||
var enIdx = -1;
|
||||
var zhIdx = -1;
|
||||
var llen = 0;
|
||||
for (var i = 0; i < Localization.strings.Length; i++)
|
||||
{
|
||||
switch (Localization.Languages[i].lcId)
|
||||
{
|
||||
case Localization.LCID_ENUS:
|
||||
if (!Localization.LanguageLoaded(i) && Localization.Loaded)
|
||||
{
|
||||
Localization.LoadLanguage(i);
|
||||
}
|
||||
enIdx = i;
|
||||
break;
|
||||
case Localization.LCID_ZHCN:
|
||||
if (!Localization.LanguageLoaded(i) && Localization.Loaded)
|
||||
{
|
||||
Localization.LoadLanguage(i);
|
||||
}
|
||||
zhIdx = i;
|
||||
llen = Localization.strings[i].Length;
|
||||
break;
|
||||
}
|
||||
}
|
||||
var enus = new string[StringsToAdd.Count];
|
||||
var zhcn = new string[StringsToAdd.Count];
|
||||
for (var i = 0; i < StringsToAdd.Count; i++)
|
||||
{
|
||||
var str = StringsToAdd[i];
|
||||
enus[i] = str.English;
|
||||
zhcn[i] = str.Chinese;
|
||||
indexer[str.Key] = llen + i;
|
||||
}
|
||||
|
||||
Localization.strings[enIdx] = Localization.strings[enIdx].Concat(enus).ToArray();
|
||||
if (enIdx == Localization.currentLanguageIndex)
|
||||
{
|
||||
Localization.currentStrings = Localization.strings[enIdx];
|
||||
}
|
||||
Localization.strings[zhIdx] = Localization.strings[zhIdx].Concat(zhcn).ToArray();
|
||||
if (zhIdx == Localization.currentLanguageIndex)
|
||||
{
|
||||
Localization.currentStrings = Localization.strings[zhIdx];
|
||||
}
|
||||
StringsToAdd.Clear();
|
||||
if (Keys.Count == 0) return;
|
||||
if (!_dirty) return;
|
||||
ApplyIndexers();
|
||||
}
|
||||
|
||||
[HarmonyPostfix, HarmonyPriority(Priority.Last), HarmonyPatch(typeof(VFPreload), "InvokeOnLoadWorkEnded")]
|
||||
private static void VFPreload_InvokeOnLoadWorkEnded_Postfix()
|
||||
[HarmonyPostfix, HarmonyPriority(Priority.Last), HarmonyPatch(typeof(Localization), nameof(Localization.LoadSettings))]
|
||||
private static void Localization_LoadSettings_Postfix()
|
||||
{
|
||||
if (_initialized) return;
|
||||
_initialized = true;
|
||||
if (StringsToAdd.Count == 0)
|
||||
{
|
||||
OnInitialized?.Invoke();
|
||||
return;
|
||||
}
|
||||
|
||||
Apply();
|
||||
}
|
||||
|
||||
[HarmonyPostfix, HarmonyPriority(Priority.Last), HarmonyPatch(typeof(Localization), nameof(Localization.LoadLanguage))]
|
||||
private static void Localization_LoadLanguage_Postfix(int index)
|
||||
{
|
||||
if (!_initialized) return;
|
||||
ApplyLanguage(index);
|
||||
}
|
||||
|
||||
[HarmonyPostfix, HarmonyPriority(Priority.Last), HarmonyPatch(typeof(Localization), nameof(Localization.NotifyLanguageChange))]
|
||||
private static void Localization_NotifyLanguageChange_Postfix()
|
||||
{
|
||||
if (!_initialized) return;
|
||||
OnInitialized?.Invoke();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user