From 6361ab21c6c2d3b0b04c228384933f78fac1ef1b Mon Sep 17 00:00:00 2001 From: Soar Qin Date: Mon, 11 Sep 2023 20:55:49 +0800 Subject: [PATCH] WIP --- CheatEnabler/DysonSpherePatch.cs | 34 +++++++- CheatEnabler/UI/MyCheckbox.cs | 2 +- CheatEnabler/UI/MyKeyBinder.cs | 14 +--- CheatEnabler/UI/MyWindow.cs | 45 ++++++++--- CheatEnabler/UIConfigWindow.cs | 135 +++++++++++++++++++++++-------- 5 files changed, 170 insertions(+), 60 deletions(-) diff --git a/CheatEnabler/DysonSpherePatch.cs b/CheatEnabler/DysonSpherePatch.cs index 2319411..e96fd46 100644 --- a/CheatEnabler/DysonSpherePatch.cs +++ b/CheatEnabler/DysonSpherePatch.cs @@ -128,6 +128,34 @@ public static class DysonSpherePatch } } + public static void InitCurrentDysonSphere(int index) + { + var star = GameMain.localStar; + if (star == null) return; + var dysonSpheres = GameMain.data?.dysonSpheres; + if (dysonSpheres == null) return; + if (index < 0) + { + if (dysonSpheres[star.index] == null) return; + var dysonSphere = new DysonSphere(); + dysonSpheres[star.index] = dysonSphere; + dysonSphere.Init(GameMain.data, star); + dysonSphere.ResetNew(); + return; + } + + var ds = dysonSpheres[star.index]; + if (ds?.layersIdBased[index] == null) return; + var pool = ds.rocketPool; + for (var id = ds.rocketCursor - 1; id > 0; id--) + { + if (pool[id].id != id) continue; + if (pool[id].nodeLayerId != index) continue; + ds.RemoveDysonRocket(id); + } + ds.RemoveLayer(index); + } + private static class SkipBulletPatch { private static long _sailLifeTime; @@ -345,6 +373,7 @@ public static class DysonSpherePatch private static IEnumerable DysonSphereLayer_GameTick_Patch(IEnumerable instructions, ILGenerator generator) { var matcher = new CodeMatcher(instructions, generator); + /* Insert absorption functions on beginning */ matcher.Start().InsertAndAdvance( new CodeInstruction(OpCodes.Ldarg_0), new CodeInstruction(OpCodes.Ldarg_1), @@ -354,6 +383,7 @@ public static class DysonSpherePatch new CodeMatch(OpCodes.Ldfld, AccessTools.Field(typeof(DysonSphereLayer), nameof(DysonSphereLayer.dysonSphere))), new CodeMatch(OpCodes.Ldfld, AccessTools.Field(typeof(DysonSphere), nameof(DysonSphere.swarm))) ).Insert(new CodeInstruction(OpCodes.Ret)); + /* Insert a RETURN before old absorption functions */ return matcher.InstructionEnumeration(); } @@ -379,9 +409,9 @@ public static class DysonSpherePatch { var matcher = new CodeMatcher(instructions, generator); matcher.MatchForward(false, - new CodeMatch(OpCodes.Ldfld, AccessTools.Field(typeof(EjectorComponent), nameof(EjectorComponent.localRot))) + new CodeMatch(instr => instr.opcode == OpCodes.Ldc_R8 && Math.Abs((double)instr.operand - 0.08715574) < 0.00000001) ); - var start = matcher.Pos - 6; + var start = matcher.Pos - 3; matcher.MatchForward(false, new CodeMatch(OpCodes.And) ).Advance(1).MatchForward(false, diff --git a/CheatEnabler/UI/MyCheckbox.cs b/CheatEnabler/UI/MyCheckbox.cs index bfdb0d9..708aba1 100644 --- a/CheatEnabler/UI/MyCheckbox.cs +++ b/CheatEnabler/UI/MyCheckbox.cs @@ -73,7 +73,7 @@ public class MyCheckBox : MonoBehaviour { if (labelText != null) { - labelText.text = val; + labelText.text = val.Translate(); } } diff --git a/CheatEnabler/UI/MyKeyBinder.cs b/CheatEnabler/UI/MyKeyBinder.cs index f46e29c..3333580 100644 --- a/CheatEnabler/UI/MyKeyBinder.cs +++ b/CheatEnabler/UI/MyKeyBinder.cs @@ -46,7 +46,7 @@ public class MyKeyBinder : MonoBehaviour public static RectTransform CreateKeyBinder(float x, float y, RectTransform parent, ConfigEntry config, string label = "", int fontSize = 17) { var optionWindow = UIRoot.instance.optionWindow; - var uikeyEntry = GameObject.Instantiate(optionWindow.entryPrefab); + var uikeyEntry = Instantiate(optionWindow.entryPrefab); GameObject go; (go = uikeyEntry.gameObject).SetActive(true); go.name = "my-keybinder"; @@ -65,7 +65,7 @@ public class MyKeyBinder : MonoBehaviour kb.setNoneKeyUIButton = uikeyEntry.setNoneKeyUIButton; - kb.functionText.text = label; + kb.functionText.text = label.Translate(); kb.functionText.fontSize = 17; ((RectTransform)kb.keyText.transform).anchoredPosition = new Vector2(20f, -27f); @@ -79,23 +79,17 @@ public class MyKeyBinder : MonoBehaviour kb.rectTrans = rect; //rect.sizeDelta = new Vector2(240f, 64f); - GameObject.Destroy(uikeyEntry); + Destroy(uikeyEntry); kb.setNoneKeyUIButton.gameObject.SetActive(false); kb.SettingChanged(); - config.SettingChanged += (sender, args) => { + config.SettingChanged += (_, _) => { kb.SettingChanged(); }; kb.inputUIButton.onClick += kb.OnInputUIButtonClick; kb.setDefaultUIButton.onClick += kb.OnSetDefaultKeyClick; //kb.setNoneKeyUIButton.onClick += kb.OnSetNoneKeyClick; return go.transform as RectTransform; - - void ResetAnchor(RectTransform theRect) - { - theRect.anchorMax = Vector2.zero; - theRect.anchorMin = Vector2.zero; - } } private void Update() diff --git a/CheatEnabler/UI/MyWindow.cs b/CheatEnabler/UI/MyWindow.cs index 54e5f44..caeb8e9 100644 --- a/CheatEnabler/UI/MyWindow.cs +++ b/CheatEnabler/UI/MyWindow.cs @@ -39,7 +39,7 @@ public class MyWindow: ManualBehaviour var txt = gameObject.transform.Find("panel-bg/title-text")?.gameObject.GetComponent(); if (txt) { - txt.text = title; + txt.text = title.Translate(); } } @@ -56,7 +56,7 @@ public class MyWindow: ManualBehaviour var src = UIRoot.instance.uiGame.assemblerWindow.stateText; var txt = Instantiate(src); txt.gameObject.name = objName; - txt.text = label; + txt.text = label.Translate(); txt.color = new Color(1f, 1f, 1f, 0.4f); txt.alignment = TextAnchor.MiddleLeft; txt.fontSize = fontSize; @@ -74,7 +74,7 @@ public class MyWindow: ManualBehaviour var btn = Instantiate(panel.cpuActiveButton); btn.gameObject.name = objName; var rect = Util.NormalizeRectWithTopLeft(btn, x, y, parent); - rect.sizeDelta = new Vector2(120, rect.sizeDelta.y); + rect.sizeDelta = new Vector2(150, rect.sizeDelta.y); var l = btn.gameObject.transform.Find("button-text").GetComponent(); var t = btn.gameObject.transform.Find("button-text").GetComponent(); if (l != null) @@ -99,6 +99,28 @@ public class MyWindow: ManualBehaviour return btn; } + protected UIButton AddFlatButton(float x, float y, RectTransform parent, string text = "", int fontSize = 12, string objName = "button", UnityAction onClick = null) + { + var panel = UIRoot.instance.uiGame.dysonEditor.controlPanel.hierarchy.layerPanel; + var btn = Instantiate(panel.layerButtons[0]); + btn.gameObject.name = objName; + Util.NormalizeRectWithTopLeft(btn, x, y, parent); + var t = btn.gameObject.transform.Find("Text").GetComponent(); + if (t != null) + { + t.text = text.Translate(); + } + t.fontSize = fontSize; + btn.button.onClick.RemoveAllListeners(); + _buttons[btn] = onClick; + if (EventRegistered) + { + if (onClick != null) + btn.button.onClick.AddListener(onClick); + } + return btn; + } + protected InputField AddInputField(float x, float y, RectTransform parent, string text = "", int fontSize = 16, string objName = "input", UnityAction onChanged = null, UnityAction onEditEnd = null) { var stationWindow = UIRoot.instance.uiGame.stationWindow; @@ -169,7 +191,7 @@ public class MyWindow: ManualBehaviour public class MyWindowWithTabs : MyWindow { - private readonly List> _tabs = new(); + protected readonly List> Tabs = new(); public override void TryClose() { _Close(); @@ -202,11 +224,11 @@ public class MyWindowWithTabs : MyWindow } var btnText = btn.transform.Find("Text").GetComponent(); - btnText.text = label; + btnText.text = label.Translate(); btnText.fontSize = 16; btn.data = index; - _tabs.Add(Tuple.Create(tabRect, btn)); + Tabs.Add(Tuple.Create(tabRect, btn)); if (EventRegistered) { btn.onClick += OnTabButtonClick; @@ -218,7 +240,7 @@ public class MyWindowWithTabs : MyWindow { if (!EventRegistered) { - foreach (var t in _tabs) + foreach (var t in Tabs) { t.Item2.onClick += OnTabButtonClick; } @@ -230,7 +252,7 @@ public class MyWindowWithTabs : MyWindow { if (EventRegistered) { - foreach (var t in _tabs) + foreach (var t in Tabs) { t.Item2.onClick -= OnTabButtonClick; } @@ -242,17 +264,16 @@ public class MyWindowWithTabs : MyWindow private void OnTabButtonClick(int index) { - foreach (var t in _tabs) + foreach (var (rectTransform, btn) in Tabs) { - var btn = t.Item2; if (btn.data != index) { btn.highlighted = false; - t.Item1.gameObject.SetActive(false); + rectTransform.gameObject.SetActive(false); continue; } btn.highlighted = true; - t.Item1.gameObject.SetActive(true); + rectTransform.gameObject.SetActive(true); } } } diff --git a/CheatEnabler/UIConfigWindow.cs b/CheatEnabler/UIConfigWindow.cs index 73d8c05..f7f58f5 100644 --- a/CheatEnabler/UIConfigWindow.cs +++ b/CheatEnabler/UIConfigWindow.cs @@ -1,4 +1,5 @@ -using UnityEngine; +using Steamworks; +using UnityEngine; namespace CheatEnabler; @@ -6,6 +7,8 @@ public class UIConfigWindow : UI.MyWindowWithTabs { private RectTransform _windowTrans; + private readonly UIButton[] _dysonLayerBtn = new UIButton[10]; + static UIConfigWindow() { I18N.Add("General", "General", "常规"); @@ -26,6 +29,8 @@ public class UIConfigWindow : UI.MyWindowWithTabs I18N.Add("Skip absorption period", "Skip absorption period", "跳过吸收阶段"); I18N.Add("Quick absorb", "Quick absorb", "快速吸收"); I18N.Add("Eject anyway", "Eject anyway", "全球弹射"); + I18N.Add("Initialize Dyson Sphere", "Initialize Dyson Sphere", "初始化戴森球"); + I18N.Add("Click to dismantle selected layer", "Click to dismantle selected layer", "点击拆除对应的戴森壳"); I18N.Add("Birth", "Birth Sys", "母星系"); I18N.Add("Terraform without enought sands", "Terraform without enough sands", "沙土不够时依然可以整改地形"); I18N.Add("Silicon/Titanium on birth planet", "Silicon/Titanium on birth planet", "母星有硅和钛"); @@ -45,7 +50,7 @@ public class UIConfigWindow : UI.MyWindowWithTabs public static UIConfigWindow CreateInstance() { - return UI.MyWindowManager.CreateWindow("CEConfigWindow", "CheatEnabler Config".Translate()); + return UI.MyWindowManager.CreateWindow("CEConfigWindow", "CheatEnabler Config"); } public override void _OnCreate() @@ -61,35 +66,35 @@ public class UIConfigWindow : UI.MyWindowWithTabs // General tab var x = 0f; var y = 10f; - var tab1 = AddTab(36f, 0, _windowTrans, "General".Translate()); - UI.MyCheckBox.CreateCheckBox(x, y, tab1, DevShortcuts.Enabled, "Enable Dev Shortcuts".Translate()); + var tab1 = AddTab(36f, 0, _windowTrans, "General"); + UI.MyCheckBox.CreateCheckBox(x, y, tab1, DevShortcuts.Enabled, "Enable Dev Shortcuts"); y += 36f; - UI.MyCheckBox.CreateCheckBox(x, y, tab1, AbnormalDisabler.Enabled, "Disable Abnormal Checks".Translate()); + UI.MyCheckBox.CreateCheckBox(x, y, tab1, AbnormalDisabler.Enabled, "Disable Abnormal Checks"); y += 86f; - UI.MyKeyBinder.CreateKeyBinder(x, y, tab1, CheatEnabler.Hotkey, "Hotkey".Translate()); + UI.MyKeyBinder.CreateKeyBinder(x, y, tab1, CheatEnabler.Hotkey, "Hotkey"); - var tab2 = AddTab(136f, 1, _windowTrans, "Build".Translate()); + var tab2 = AddTab(136f, 1, _windowTrans, "Build"); x = 0f; y = 10f; - UI.MyCheckBox.CreateCheckBox(x, y, tab2, BuildPatch.ImmediateEnabled, "Finish build immediately".Translate()); + UI.MyCheckBox.CreateCheckBox(x, y, tab2, BuildPatch.ImmediateEnabled, "Finish build immediately"); y += 36f; - UI.MyCheckBox.CreateCheckBox(x, y, tab2, BuildPatch.NoCostEnabled, "Infinite buildings".Translate()); + UI.MyCheckBox.CreateCheckBox(x, y, tab2, BuildPatch.NoCostEnabled, "Infinite buildings"); y += 36f; - UI.MyCheckBox.CreateCheckBox(x, y, tab2, BuildPatch.NoConditionEnabled, "Build without condition".Translate()); + UI.MyCheckBox.CreateCheckBox(x, y, tab2, BuildPatch.NoConditionEnabled, "Build without condition"); y += 36f; - UI.MyCheckBox.CreateCheckBox(x, y, tab2, BuildPatch.NoCollisionEnabled, "No collision".Translate()); + UI.MyCheckBox.CreateCheckBox(x, y, tab2, BuildPatch.NoCollisionEnabled, "No collision"); // Planet Tab - var tab3 = AddTab(236f, 2, _windowTrans, "Planet".Translate()); + var tab3 = AddTab(236f, 2, _windowTrans, "Planet"); x = 0f; y = 10f; - UI.MyCheckBox.CreateCheckBox(x, y, tab3, ResourcePatch.InfiniteEnabled, "Infinite Natural Resources".Translate()); + UI.MyCheckBox.CreateCheckBox(x, y, tab3, ResourcePatch.InfiniteEnabled, "Infinite Natural Resources"); y += 36f; - UI.MyCheckBox.CreateCheckBox(x, y, tab3, ResourcePatch.FastEnabled, "Fast Mining".Translate()); + UI.MyCheckBox.CreateCheckBox(x, y, tab3, ResourcePatch.FastEnabled, "Fast Mining"); y += 36f; - UI.MyCheckBox.CreateCheckBox(x, y, tab3, WaterPumperPatch.Enabled, "Pump Anywhere".Translate()); + UI.MyCheckBox.CreateCheckBox(x, y, tab3, WaterPumperPatch.Enabled, "Pump Anywhere"); y += 36f; - UI.MyCheckBox.CreateCheckBox(x, y, tab3, TerraformPatch.Enabled, "Terraform without enought sands".Translate()); + UI.MyCheckBox.CreateCheckBox(x, y, tab3, TerraformPatch.Enabled, "Terraform without enought sands"); x = 300f; y = 10f; AddButton(x, y, tab3, "矿物掩埋标题", 16, "button-bury-all", () => @@ -129,41 +134,96 @@ public class UIConfigWindow : UI.MyWindowWithTabs PlanetFunctions.DismantleAll(false); }); - var tab4 = AddTab(336f, 3, _windowTrans, "Dyson Sphere".Translate()); + var tab4 = AddTab(336f, 3, _windowTrans, "Dyson Sphere"); x = 0f; y = 10f; - UI.MyCheckBox.CreateCheckBox(x, y, tab4, DysonSpherePatch.SkipBulletEnabled, "Skip bullet period".Translate()); + UI.MyCheckBox.CreateCheckBox(x, y, tab4, DysonSpherePatch.SkipBulletEnabled, "Skip bullet period"); y += 36f; - UI.MyCheckBox.CreateCheckBox(x, y, tab4, DysonSpherePatch.SkipAbsorbEnabled, "Skip absorption period".Translate()); + UI.MyCheckBox.CreateCheckBox(x, y, tab4, DysonSpherePatch.SkipAbsorbEnabled, "Skip absorption period"); y += 36f; - UI.MyCheckBox.CreateCheckBox(x, y, tab4, DysonSpherePatch.QuickAbsortEnabled, "Quick absorb".Translate()); + UI.MyCheckBox.CreateCheckBox(x, y, tab4, DysonSpherePatch.QuickAbsortEnabled, "Quick absorb"); y += 36f; - UI.MyCheckBox.CreateCheckBox(x, y, tab4, DysonSpherePatch.EjectAnywayEnabled, "Eject anyway".Translate()); + UI.MyCheckBox.CreateCheckBox(x, y, tab4, DysonSpherePatch.EjectAnywayEnabled, "Eject anyway"); + x = 300f; + y = 10f; + AddButton(x, y, tab4, "Initialize Dyson Sphere", 16, "init-dyson-sphere", () => + { + DysonSpherePatch.InitCurrentDysonSphere(-1); + }); + y += 36f; + AddText(x, y, tab4, "Click to dismantle selected layer", 16, "text-dismantle-layer"); + y += 26f; + for (var i = 0; i < 10; i++) + { + var id = i + 1; + var btn = AddFlatButton(x, y, tab4, id.ToString(), 12, "dismantle-layer-" + id, () => { DysonSpherePatch.InitCurrentDysonSphere(id); }); + ((RectTransform)btn.transform).sizeDelta = new Vector2(40f, 20f); + _dysonLayerBtn[i] = btn; + if (i == 4) + { + x -= 160f; + y += 20f; + } + else + { + x += 40f; + } + } - var tab5 = AddTab(436f, 4, _windowTrans, "Birth".Translate()); + var tab5 = AddTab(436f, 4, _windowTrans, "Birth"); x = 0f; y = 10f; - UI.MyCheckBox.CreateCheckBox(x, y, tab5, BirthPlanetPatch.SitiVeinsOnBirthPlanet, "Silicon/Titanium on birth planet".Translate()); + UI.MyCheckBox.CreateCheckBox(x, y, tab5, BirthPlanetPatch.SitiVeinsOnBirthPlanet, "Silicon/Titanium on birth planet"); y += 36f; - UI.MyCheckBox.CreateCheckBox(x, y, tab5, BirthPlanetPatch.FireIceOnBirthPlanet, "Fire ice on birth planet".Translate()); + UI.MyCheckBox.CreateCheckBox(x, y, tab5, BirthPlanetPatch.FireIceOnBirthPlanet, "Fire ice on birth planet"); y += 36f; - UI.MyCheckBox.CreateCheckBox(x, y, tab5, BirthPlanetPatch.KimberliteOnBirthPlanet, "Kimberlite on birth planet".Translate()); + UI.MyCheckBox.CreateCheckBox(x, y, tab5, BirthPlanetPatch.KimberliteOnBirthPlanet, "Kimberlite on birth planet"); y += 36f; - UI.MyCheckBox.CreateCheckBox(x, y, tab5, BirthPlanetPatch.FractalOnBirthPlanet, "Fractal silicon on birth planet".Translate()); + UI.MyCheckBox.CreateCheckBox(x, y, tab5, BirthPlanetPatch.FractalOnBirthPlanet, "Fractal silicon on birth planet"); y += 36f; - UI.MyCheckBox.CreateCheckBox(x, y, tab5, BirthPlanetPatch.OrganicOnBirthPlanet, "Organic crystal on birth planet".Translate()); + UI.MyCheckBox.CreateCheckBox(x, y, tab5, BirthPlanetPatch.OrganicOnBirthPlanet, "Organic crystal on birth planet"); x = 200f; y = 10f; - UI.MyCheckBox.CreateCheckBox(x, y, tab5, BirthPlanetPatch.OpticalOnBirthPlanet, "Optical grating crystal on birth planet".Translate()); + UI.MyCheckBox.CreateCheckBox(x, y, tab5, BirthPlanetPatch.OpticalOnBirthPlanet, "Optical grating crystal on birth planet"); y += 36f; - UI.MyCheckBox.CreateCheckBox(x, y, tab5, BirthPlanetPatch.SpiniformOnBirthPlanet, "Spiniform stalagmite crystal on birth planet".Translate()); + UI.MyCheckBox.CreateCheckBox(x, y, tab5, BirthPlanetPatch.SpiniformOnBirthPlanet, "Spiniform stalagmite crystal on birth planet"); y += 36f; - UI.MyCheckBox.CreateCheckBox(x, y, tab5, BirthPlanetPatch.UnipolarOnBirthPlanet, "Unipolar magnet on birth planet".Translate()); + UI.MyCheckBox.CreateCheckBox(x, y, tab5, BirthPlanetPatch.UnipolarOnBirthPlanet, "Unipolar magnet on birth planet"); y += 36f; - UI.MyCheckBox.CreateCheckBox(x, y, tab5, BirthPlanetPatch.FlatBirthPlanet, "Birth planet is solid flat (no water at all)".Translate()); + UI.MyCheckBox.CreateCheckBox(x, y, tab5, BirthPlanetPatch.FlatBirthPlanet, "Birth planet is solid flat (no water at all)"); y += 36f; - UI.MyCheckBox.CreateCheckBox(x, y, tab5, BirthPlanetPatch.HighLuminosityBirthStar, "Birth star has high luminosity".Translate()); + UI.MyCheckBox.CreateCheckBox(x, y, tab5, BirthPlanetPatch.HighLuminosityBirthStar, "Birth star has high luminosity"); SetCurrentTab(0); + + UpdateUI(); + } + + public void UpdateUI() + { + if (Tabs[3].Item1.gameObject.activeSelf) + { + var star = GameMain.localStar; + if (star != null) + { + var dysonSpheres = GameMain.data?.dysonSpheres; + if (dysonSpheres?[star.index] != null) + { + var ds = dysonSpheres[star.index]; + for (var i = 1; i <= 10; i++) + { + var layer = ds.layersIdBased[i]; + _dysonLayerBtn[i - 1].button.interactable = layer != null && layer.id == i; + } + + return; + } + } + + for (var i = 0; i < 10; i++) + { + _dysonLayerBtn[i].button.interactable = false; + } + } } public override void _OnDestroy() @@ -190,8 +250,13 @@ public class UIConfigWindow : UI.MyWindowWithTabs public override void _OnUpdate() { - if (!VFInput.escape || VFInput.inputing) return; - VFInput.UseEscape(); - _Close(); + if (VFInput.escape && !VFInput.inputing) + { + VFInput.UseEscape(); + _Close(); + return; + } + + UpdateUI(); } } \ No newline at end of file