|
| 1 | +using BepInEx; |
| 2 | +using BepInEx.Logging; |
| 3 | +using HarmonyLib; |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Linq; |
| 7 | +using System.Reflection; |
| 8 | +using System.Reflection.Emit; |
| 9 | +using System.Security; |
| 10 | +using System.Security.Permissions; |
| 11 | +using System.Text; |
| 12 | +using UnityEngine; |
| 13 | +using UnityEngine.UI; |
| 14 | + |
| 15 | +[module: UnverifiableCode] |
| 16 | +[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)] |
| 17 | +namespace DSP_AssemblerUI.AssemblerSpeedUI |
| 18 | +{ |
| 19 | + [BepInPlugin("dsp.assemblerUI.speedMod", "Assembler UI Speed Info Mod", "1.0.0.0")] |
| 20 | + public class AssemblerSpeedUIMod : BaseUnityPlugin |
| 21 | + { |
| 22 | + public new static ManualLogSource Logger; |
| 23 | + |
| 24 | + internal void Awake() |
| 25 | + { |
| 26 | + |
| 27 | + //Adding the Logger |
| 28 | + Logger = new ManualLogSource("AssemblerSpeedUIMod"); |
| 29 | + BepInEx.Logging.Logger.Sources.Add(Logger); |
| 30 | + |
| 31 | + Harmony.CreateAndPatchAll(typeof(AssemblerSpeedUIMod)); |
| 32 | + } |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | + internal class ItemSpeedInfoLabel |
| 37 | + { |
| 38 | + public GameObject gameObject; |
| 39 | + public Text value; |
| 40 | + } |
| 41 | + |
| 42 | + internal static Dictionary<string, ItemSpeedInfoLabel> speedInfos = new Dictionary<string, ItemSpeedInfoLabel>(); |
| 43 | + |
| 44 | + static readonly string TEXT_PATH = "UI Root/Overlay Canvas/In Game/Windows/Assembler Window/produce/speed/speed-text"; |
| 45 | + |
| 46 | + public static readonly string[] itemKeys = { "assembler-speed-item0", "assembler-speed-item1", "assembler-speed-item2" }; |
| 47 | + |
| 48 | + public static float[][] xLookup = new float[][]{ new float[]{ -60f }, new float[]{ -125f, -60f }, new float[] { -190f, -125f, -60f } }; |
| 49 | + public static Vector3? ogPos = null; |
| 50 | + |
| 51 | + [HarmonyPostfix, HarmonyPatch(typeof(UIAssemblerWindow), "OnAssemblerIdChange")] |
| 52 | + public static void OnAssemblerIdChangePostfix(UIAssemblerWindow __instance) |
| 53 | + { |
| 54 | + SetupLabels(__instance); |
| 55 | + } |
| 56 | + |
| 57 | + [HarmonyPostfix, HarmonyPatch(typeof(UIAssemblerWindow), "OnRecipePickerReturn")] |
| 58 | + public static void OnRecipePickerReturnPostfix(UIAssemblerWindow __instance) |
| 59 | + { |
| 60 | + SetupLabels(__instance); |
| 61 | + } |
| 62 | + |
| 63 | + public static void SetupLabels(UIAssemblerWindow window) |
| 64 | + { |
| 65 | + int? productCount = window.factorySystem?.assemblerPool[window.assemblerId].products?.Length; |
| 66 | + if (!productCount.HasValue) |
| 67 | + { |
| 68 | + return; |
| 69 | + } |
| 70 | + int loopCap = Math.Min(productCount.Value, itemKeys.Length); |
| 71 | + |
| 72 | + for (int cnt = 0; cnt < loopCap; cnt++) |
| 73 | + { |
| 74 | + if (!speedInfos.ContainsKey(itemKeys[cnt])) |
| 75 | + { |
| 76 | + AddSpeedLabel(itemKeys[cnt], cnt, loopCap); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + string perMinuteString = "每分钟".Translate(); |
| 81 | + |
| 82 | + for (int cnt2 = 0; cnt2 < speedInfos.Count; cnt2++) |
| 83 | + { |
| 84 | + if (cnt2 < productCount) |
| 85 | + { |
| 86 | + speedInfos[itemKeys[cnt2]].gameObject.SetActive(true); |
| 87 | + speedInfos[itemKeys[cnt2]].value.text = "0.0" + perMinuteString; |
| 88 | + PositionSpeedLabel(speedInfos[itemKeys[cnt2]].gameObject, cnt2, loopCap); |
| 89 | + } |
| 90 | + else |
| 91 | + { |
| 92 | + speedInfos[itemKeys[cnt2]].gameObject.SetActive(false); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + public static void AddSpeedLabel(string id, int num, int ofNum) |
| 98 | + { |
| 99 | + var originalDetailLabel = GameObject.Find(TEXT_PATH); |
| 100 | + if (originalDetailLabel == null) |
| 101 | + { |
| 102 | + throw new InvalidOperationException("Assembler speed base entry is not present"); |
| 103 | + } |
| 104 | + |
| 105 | + GameObject gameObject = null; |
| 106 | + var originalDetailLabelText = originalDetailLabel.GetComponent<Text>(); |
| 107 | + |
| 108 | + gameObject = Instantiate(originalDetailLabel, originalDetailLabel.transform.position, Quaternion.identity); |
| 109 | + Destroy(gameObject.GetComponentInChildren<Localizer>()); |
| 110 | + |
| 111 | + gameObject.name = id; |
| 112 | + gameObject.transform.SetParent(originalDetailLabel.transform.parent); |
| 113 | + |
| 114 | + var textComponents = gameObject.GetComponentsInChildren<Text>(); |
| 115 | + var value = textComponents[0]; |
| 116 | + |
| 117 | + if (!ogPos.HasValue) |
| 118 | + { |
| 119 | + ogPos = originalDetailLabel.transform.localPosition; |
| 120 | + } |
| 121 | + |
| 122 | + gameObject.transform.localScale = new Vector3(1f, 1f, 1f); |
| 123 | + PositionSpeedLabel(gameObject, num, ofNum); |
| 124 | + gameObject.transform.right = originalDetailLabel.transform.right; |
| 125 | + |
| 126 | + ItemSpeedInfoLabel newItemSpeedInfo = new ItemSpeedInfoLabel() |
| 127 | + { |
| 128 | + gameObject = gameObject, |
| 129 | + value = value |
| 130 | + }; |
| 131 | + speedInfos.Add(id, newItemSpeedInfo); |
| 132 | + } |
| 133 | + |
| 134 | + public static void PositionSpeedLabel(GameObject gameObject, int num, int ofNum) |
| 135 | + { |
| 136 | + |
| 137 | + Logger.LogDebug($"OgPosition:{gameObject.transform.localPosition}"); |
| 138 | + Vector3 shiftVector = getPosShift(num, ofNum); |
| 139 | + Logger.LogDebug($"ShiftedBy:{shiftVector}"); |
| 140 | + |
| 141 | + gameObject.transform.localPosition = ogPos.Value + shiftVector; |
| 142 | + } |
| 143 | + |
| 144 | + public static Vector3 getPosShift(int num, int ofNum) |
| 145 | + { |
| 146 | + return new Vector3(xLookup[ofNum - 1][num], -50f, 0f); |
| 147 | + } |
| 148 | + |
| 149 | + public static void UpdateSpeedLabel(string id, float value) |
| 150 | + { |
| 151 | + string perMinuteString = "每分钟".Translate(); |
| 152 | + speedInfos[id].value.text = value.ToString("0.0") + perMinuteString; |
| 153 | + } |
| 154 | + |
| 155 | + public static void UpdateSpeedLabels(float baseSpeed, int[] productCounts) |
| 156 | + { |
| 157 | + for(int cnt = 0; cnt < Math.Min(productCounts.Length, itemKeys.Length); cnt++) |
| 158 | + { |
| 159 | + UpdateSpeedLabel(itemKeys[cnt], productCounts[cnt]*baseSpeed); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + |
| 164 | + [HarmonyTranspiler, HarmonyPatch(typeof(UIAssemblerWindow), "_OnUpdate")] |
| 165 | + public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions) |
| 166 | + { |
| 167 | + Logger.LogDebug("UiTextTranspiler started!"); |
| 168 | + CodeMatcher matcher = new CodeMatcher(instructions); |
| 169 | + //find --> |
| 170 | + //ldloc.s 18 |
| 171 | + //ldloc.s 19 |
| 172 | + //mul |
| 173 | + //stloc.s 18 |
| 174 | + //<-- endFind |
| 175 | + Logger.LogDebug($"UiTextTranspiler Matcher Codes Count: {matcher.Instructions().Count}, Matcher Pos: {matcher.Pos}!"); |
| 176 | + matcher.MatchForward( |
| 177 | + true, |
| 178 | + new CodeMatch(i => i.opcode == OpCodes.Ldloc_S && i.operand is LocalBuilder lb && lb.LocalIndex == 18), |
| 179 | + new CodeMatch(i => i.opcode == OpCodes.Ldloc_S && i.operand is LocalBuilder lb && lb.LocalIndex == 19), |
| 180 | + new CodeMatch(OpCodes.Mul), |
| 181 | + new CodeMatch(i => i.opcode == OpCodes.Stloc_S && i.operand is LocalBuilder lb && lb.LocalIndex == 18) |
| 182 | + ); |
| 183 | + |
| 184 | + Logger.LogDebug($"UiTextTranspiler Matcher Codes Count: {matcher.Instructions().Count}, Matcher Pos: {matcher.Pos}!"); |
| 185 | + matcher.Advance(1); //move from last match to next element |
| 186 | + //insert--> |
| 187 | + //ldloc.s 18 |
| 188 | + //ldloca.s 0 //AssemblerComponent local var |
| 189 | + //ldfld int32[] AssemblerComponent::productCounts |
| 190 | + //call update |
| 191 | + //<-- endInsert |
| 192 | + Logger.LogDebug($"UiTextTranspiler Matcher Codes Count: {matcher.Instructions().Count}, Matcher Pos: {matcher.Pos}!"); |
| 193 | + matcher.InsertAndAdvance( |
| 194 | + new CodeInstruction(OpCodes.Ldloc_S, (byte)18), |
| 195 | + new CodeInstruction(OpCodes.Ldloca_S, (byte)0), |
| 196 | + new CodeInstruction(OpCodes.Ldfld, typeof(AssemblerComponent).GetField("productCounts")), |
| 197 | + new CodeInstruction(OpCodes.Call, typeof(AssemblerSpeedUIMod).GetMethod("UpdateSpeedLabels")) |
| 198 | + ); |
| 199 | + |
| 200 | + return matcher.InstructionEnumeration(); |
| 201 | + } |
| 202 | + } |
| 203 | +} |
0 commit comments