From 18505caa3b8e64ee2eada6052b97b5a9affdd92b Mon Sep 17 00:00:00 2001 From: Soar Qin Date: Wed, 6 Sep 2023 03:22:54 +0800 Subject: [PATCH] WIP --- CheatEnabler/CheatEnabler.cs | 6 ++ CheatEnabler/I18N.cs | 61 +++++++++++++ CheatEnabler/UI/MyCheckbox.cs | 88 +++++++++++++++++++ CheatEnabler/UI/MyWindow.cs | 159 ++++++++++++++++++++++++++++++++++ 4 files changed, 314 insertions(+) create mode 100644 CheatEnabler/I18N.cs create mode 100644 CheatEnabler/UI/MyCheckbox.cs create mode 100644 CheatEnabler/UI/MyWindow.cs diff --git a/CheatEnabler/CheatEnabler.cs b/CheatEnabler/CheatEnabler.cs index 863a482..6eeca7c 100644 --- a/CheatEnabler/CheatEnabler.cs +++ b/CheatEnabler/CheatEnabler.cs @@ -64,6 +64,12 @@ public class CheatEnabler : BaseUnityPlugin "Birth star has high luminosity").Value; _terraformAnyway = Config.Bind("General", "TerraformAnyway", _terraformAnyway, "Can do terraform without enough sands").Value; + + I18N.Init(); + + // UI Patch + Harmony.CreateAndPatchAll(typeof(UI.MyWindowManager.Patch)); + if (_devShortcuts) { Harmony.CreateAndPatchAll(typeof(DevShortcuts)); diff --git a/CheatEnabler/I18N.cs b/CheatEnabler/I18N.cs new file mode 100644 index 0000000..1b7cd03 --- /dev/null +++ b/CheatEnabler/I18N.cs @@ -0,0 +1,61 @@ +using System.Collections.Generic; +using System.Linq; +using HarmonyLib; + +namespace CheatEnabler; + +public class I18N +{ + public static void Init() + { + Harmony.CreateAndPatchAll(typeof(I18N)); + } + private static int _nextID = 0; + private static readonly List StringsToAdd = new(); + public static void Add(string key, string enus, string zhcn = null, string frfr = null) + { + var strings = LDB._strings; + while (_nextID <= 12000) + { + if (!strings.dataIndices.ContainsKey(_nextID)) + { + break; + } + + _nextID++; + } + var strProto = new StringProto + { + Name = key, + ID = _nextID, + SID = "", + ENUS = enus, + ZHCN = string.IsNullOrEmpty(zhcn) ? enus : zhcn, + FRFR = string.IsNullOrEmpty(frfr) ? enus : frfr + }; + StringsToAdd.Add(strProto); + _nextID++; + } + + private static bool _initialized = false; + [HarmonyPostfix, HarmonyPriority(Priority.Last), HarmonyPatch(typeof(VFPreload), "InvokeOnLoadWorkEnded")] + private static void VFPreload_InvokeOnLoadWorkEnded_Postfix() + { + if (_initialized) return; + _initialized = true; + if (StringsToAdd.Count == 0) + { + return; + } + var strings = LDB._strings; + var index = strings.dataArray.Length; + strings.dataArray = strings.dataArray.Concat(StringsToAdd).ToArray(); + StringsToAdd.Clear(); + var newIndex = strings.dataArray.Length; + for (; index < newIndex; index++) + { + strings.dataIndices[strings.dataArray[index].ID] = index; + strings.nameIndices[strings.dataArray[index].Name] = index; + } + } +} \ No newline at end of file diff --git a/CheatEnabler/UI/MyCheckbox.cs b/CheatEnabler/UI/MyCheckbox.cs new file mode 100644 index 0000000..2c7f55b --- /dev/null +++ b/CheatEnabler/UI/MyCheckbox.cs @@ -0,0 +1,88 @@ +using System; +using UnityEngine; +using UnityEngine.UI; + +namespace CheatEnabler.UI; + +// MyCheckBox modified from LSTM: https://github.com/hetima/DSP_LSTM/blob/main/LSTM/MyCheckBox.cs +public class MyCheckBox : MonoBehaviour +{ + public UIButton uiButton; + public Image checkImage; + public RectTransform rectTrans; + public Text labelText; + + public event Action OnChecked; + public bool Checked + { + get => _checked; + set + { + _checked = value; + checkImage.enabled = value; + } + } + + private bool _checked; + + public static MyCheckBox CreateCheckBox(bool check, Transform parent = null, float x = 0f, float y = 0f, string label = "", int fontSize = 15) + { + var buildMenu = UIRoot.instance.uiGame.buildMenu; + var src = buildMenu.uxFacilityCheck; + + var go = Instantiate(src.gameObject); + var rect = go.transform as RectTransform; + if (rect == null) + { + return null; + } + go.name = "my-checkbox"; + var cb = go.AddComponent(); + cb._checked = check; + if (parent != null) + { + rect.SetParent(parent); + } + rect.anchorMax = new Vector2(0f, 1f); + rect.anchorMin = new Vector2(0f, 1f); + rect.pivot = new Vector2(0f, 1f); + rect.anchoredPosition3D = new Vector3(x, -y, 0f); + + cb.rectTrans = rect; + cb.uiButton = go.GetComponent(); + cb.checkImage = go.transform.Find("checked")?.GetComponent(); + + var child = go.transform.Find("text"); + if (child != null) + { + DestroyImmediate(child.GetComponent()); + cb.labelText = child.GetComponent(); + cb.labelText.fontSize = fontSize; + cb.SetLabelText(label); + } + + //value + cb.uiButton.onClick += cb.OnClick; + if (cb.checkImage != null) + { + cb.checkImage.enabled = check; + } + + return cb; + } + + public void SetLabelText(string val) + { + if (labelText != null) + { + labelText.text = val; + } + } + + public void OnClick(int obj) + { + _checked = !_checked; + checkImage.enabled = _checked; + OnChecked?.Invoke(); + } +} diff --git a/CheatEnabler/UI/MyWindow.cs b/CheatEnabler/UI/MyWindow.cs new file mode 100644 index 0000000..998f0ea --- /dev/null +++ b/CheatEnabler/UI/MyWindow.cs @@ -0,0 +1,159 @@ +using HarmonyLib; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.UI; + +namespace CheatEnabler.UI; + +// MyWindowManager modified from LSTM: https://github.com/hetima/DSP_LSTM/blob/main/LSTM/MyWindowCtl.cs +public static class MyWindowManager +{ + private static readonly List Windows = new(4); + private static bool _inited = false; + + public static T CreateWindow(string name, string title = "") where T : Component + { + var srcWin = UIRoot.instance.uiGame.tankWindow; + var src = srcWin.gameObject; + var go = Object.Instantiate(src, srcWin.transform.parent); + go.name = name; + go.SetActive(false); + Object.Destroy(go.GetComponent()); + var win = go.AddComponent() as ManualBehaviour; + if (win == null) + return null; + //shadow + for (var i = 0; i < go.transform.childCount; i++) + { + var child = go.transform.GetChild(i).gameObject; + if (child.name == "panel-bg") + { + var btn = child.GetComponentInChildren