using System.Collections.Generic; using System.Reflection; using System.Reflection.Emit; using HarmonyLib; namespace PriorityUX { [HarmonyPatch(typeof(PriorityScreen))] static class PriorityScreenPatches { static readonly MethodInfo SetScreenPriorityMethodName = typeof(PriorityScreen).GetMethod(nameof(PriorityScreen.SetScreenPriority)); [HarmonyPatch(nameof(PriorityScreen.ResetPriority))] [HarmonyPrefix] static bool ResetPriority_Prefix(PriorityScreen __instance) { var options = Options.Instance; Debug.Log("[PriorityUX] >> ResetPriority patch options.resetPriority:" + options.resetPriority); if (options.resetPriority) { __instance.SetScreenPriority(new PrioritySetting(PriorityScreen.PriorityClass.basic, options.resetPriorityLevel)); } return false; // Ignore base implementation } [HarmonyPatch(nameof(PriorityScreen.SetScreenPriority))] [HarmonyPostfix] static void SetScreenPriorityPostfix(PrioritySetting priority, PriorityScreen __instance) { if (__instance == ToolMenu.Instance.PriorityScreen || __instance == Utils.getPlanPriorityScreen()) { // Debug.Log("[PriorityUX] >> SetScreenPriorityPostfix: " + priority.priority_class.ToString() + "-" + priority.priority_value); Utils.setLastPriority(priority); } // If this was set from some other instance than the tool menu, propagate the setting here // var toolPriorityScreen = ToolMenu.Instance.PriorityScreen; // if (toolPriorityScreen != null && toolPriorityScreen != __instance) { // Debug.Log("[PriorityUX] >> Syncing priority to tool priority"); // if (toolPriorityScreen.gameObject.activeSelf) { // Debug.Log("[PriorityUX] >> & active"); // toolPriorityScreen.SetScreenPriority(priority); // } // } } static void SetInitialPriority(PriorityScreen priorityScreen) { Debug.Log("[PriorityUX] >> SetInitialPriority", priorityScreen); priorityScreen.SetScreenPriority(Utils.getLastPriority()); } [HarmonyPatch(nameof(PriorityScreen.InstantiateButtons))] [HarmonyTranspiler] static IEnumerable InstantiateButtonsTranspiler(IEnumerable instructions) { foreach (var instruction in instructions) { // SetScreenPriority(new PrioritySetting(PriorityClass.basic, 5)); // IL_0166: ldarg.0 // IL_0167: ldc.i4.0 // IL_0168: ldc.i4.5 // IL_0169: newobj instance void PrioritySetting::.ctor(valuetype PriorityScreen/PriorityClass, int32) // IL_016e: ldc.i4.0 // IL_016f: call instance void PriorityScreen::SetScreenPriority(valuetype PrioritySetting, bool) if (instruction.Calls(SetScreenPriorityMethodName)) { Debug.Log("[PriorityUX] >> Transpile default priority in InstantiateButtons"); yield return new CodeInstruction(OpCodes.Pop); // Priority yield return new CodeInstruction(OpCodes.Pop); // Bool yield return CodeInstruction.Call(typeof(PriorityScreenPatches), nameof(PriorityScreenPatches.SetInitialPriority)); continue; } yield return instruction; } } } }