Initial commit

This commit is contained in:
2022-12-30 00:58:33 +01:00
commit 3f1075e67f
176 changed files with 25715 additions and 0 deletions

19
mod/Patches/BuildMenu.cs Normal file
View File

@@ -0,0 +1,19 @@
using HarmonyLib;
namespace PriorityUX {
[HarmonyPatch(typeof(BuildMenu))]
public class BuildMenuPatches {
[HarmonyPatch(nameof(BuildMenu.OnKeyDown))]
[HarmonyPrefix]
static void BuildMenu_OnKeyDown_Prefix(KButtonEvent e) {
Debug.Log("BuildMenu_OnKeyDown_Prefix" + e);
}
[HarmonyPatch(nameof(BuildMenu.OnKeyUp))]
[HarmonyPrefix]
static void BuildMenu_OnKeyUp_Prefix(KButtonEvent e) {
Debug.Log("BuildMenu_OnKeyUp_Prefix" + e);
}
}
}

View File

@@ -0,0 +1,45 @@
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
using HarmonyLib;
using KMod;
namespace PriorityUX {
[HarmonyPatch(typeof(BuildMenuCategoriesScreen))]
public class BuildMenuCategoriesScreenPatches {
[HarmonyPatch(nameof(BuildMenuCategoriesScreen.OnKeyDown))]
[HarmonyPrefix]
static bool BuildMenu_OnKeyDown_Prefix(KButtonEvent e) {
Debug.Log("BuildMenu_OnKeyDown_Prefix" + e);
FieldInfo productInfoScreenField = typeof(BuildMenu).GetField("productInfoScreen", BindingFlags.NonPublic | BindingFlags.Instance);
Debug.Log(productInfoScreenField);
ProductInfoScreen ___productInfoScreen = (ProductInfoScreen)productInfoScreenField.GetValue(BuildMenu.Instance);
MaterialSelectionPanel msp = ___productInfoScreen.materialSelectionPanel;
if (___productInfoScreen.gameObject.activeSelf && msp.gameObject.activeSelf) {
MaterialSelectionPanelPatches.OnKeyDown(e, msp);
}
return true;
}
[HarmonyPatch(nameof(BuildMenuCategoriesScreen.OnKeyUp))]
[HarmonyPrefix]
static bool BuildMenu_OnKeyUp_Prefix(KButtonEvent e) {
Debug.Log("BuildMenu_OnKeyUp_Prefix" + e);
FieldInfo productInfoScreenField = typeof(BuildMenu).GetField("productInfoScreen", BindingFlags.NonPublic | BindingFlags.Instance);
Debug.Log(productInfoScreenField);
ProductInfoScreen ___productInfoScreen = (ProductInfoScreen)productInfoScreenField.GetValue(BuildMenu.Instance);
MaterialSelectionPanel msp = ___productInfoScreen.materialSelectionPanel;
if (___productInfoScreen.gameObject.activeSelf && msp.gameObject.activeSelf) {
MaterialSelectionPanelPatches.OnKeyUp(e, msp);
}
return true;
}
}
}

View File

@@ -0,0 +1,74 @@
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
using HarmonyLib;
using KMod;
namespace PriorityUX {
[HarmonyDebug]
[HarmonyPatch(typeof(Deconstructable))]
public class DeconstructablePatches {
public static PrioritySetting getChorePriority() {
Debug.Log("Getting deconstruct chore priority");
if (Options.Instance.deconstructInheritPriority)
return Utils.getLastPriority();
else
return Utils.getDefaultPriority();
}
[HarmonyPatch("QueueDeconstruction")]
[HarmonyPostfix]
static void QueueDeconstruction_Postfix(Deconstructable __instance) {
Prioritizable prioritizable = __instance.GetComponent<Prioritizable>();
if (prioritizable != null) {
Debug.Log("Updating deconstruct priority");
prioritizable.SetMasterPriority(getChorePriority());
}
}
/*
[HarmonyPatch("QueueDeconstruction")]
[HarmonyTranspiler]
static IEnumerable<CodeInstruction> QueueDeconstruction_Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator ilGenerator) {
Harmony.DEBUG = true;
var priorityLocal = ilGenerator.DeclareLocal(typeof(int));
var priorityClassLocal = ilGenerator.DeclareLocal(typeof(PriorityScreen.PriorityClass));
var tmpLocal0 = ilGenerator.DeclareLocal(typeof(int));
var tmpLocal1 = ilGenerator.DeclareLocal(typeof(int));
yield return CodeInstruction.Call(typeof(DeconstructablePatches), nameof(getChorePriority));
yield return new CodeInstruction(OpCodes.Dup);
// yield return new CodeInstruction(OpCodes.Pop);
// yield return new CodeInstruction(OpCodes.Pop);
yield return CodeInstruction.LoadField(typeof(PrioritySetting), nameof(PrioritySetting.priority_value));
yield return new CodeInstruction(OpCodes.Stloc, priorityLocal.LocalIndex);
yield return CodeInstruction.LoadField(typeof(PrioritySetting), nameof(PrioritySetting.priority_class));
yield return new CodeInstruction(OpCodes.Stloc, priorityClassLocal.LocalIndex);
ConstructorInfo choreConstructor = typeof(WorkChore<Deconstructable>).GetConstructors()[0];
// FieldInfo field = typeof(Deconstructable).GetField(nameof(Deconstructable.chore));
foreach (var instruction in instructions) {
// IL_0087: ldc.i4.1
// IL_0088: ldc.i4.0 // priority_class
// IL_0089: ldc.i4.5 // priority_value
// IL_008a: ldc.i4.1
// IL_008b: ldc.i4.1
// IL_008c: newobj instance void class WorkChore`1<class Deconstructable>::.ctor(class ChoreType, class ['Assembly-CSharp-firstpass']IStateMachineTarget, class ChoreProvider, bool, class [mscorlib]System.Action`1<class Chore>, class [mscorlib]System.Action`1<class Chore>, class [mscorlib]System.Action`1<class Chore>, bool, class ScheduleBlockType, bool, bool, class ['Assembly-CSharp-firstpass']KAnimFile, bool, bool, bool, valuetype PriorityScreen/PriorityClass, int32, bool, bool)
// IL_0091: stfld class Chore Deconstructable::chore
if (instruction.OperandIs(choreConstructor)) {
Debug.Log("PATCHING CHORE CONSTRUCTOR\n" + System.Environment.StackTrace);
yield return new CodeInstruction(OpCodes.Stloc, tmpLocal1.LocalIndex);
yield return new CodeInstruction(OpCodes.Stloc, tmpLocal0.LocalIndex);
yield return new CodeInstruction(OpCodes.Pop);
yield return new CodeInstruction(OpCodes.Pop);
yield return new CodeInstruction(OpCodes.Ldloc, priorityClassLocal.LocalIndex);
yield return new CodeInstruction(OpCodes.Ldloc, priorityLocal.LocalIndex);
yield return new CodeInstruction(OpCodes.Ldloc, tmpLocal0.LocalIndex);
yield return new CodeInstruction(OpCodes.Ldloc, tmpLocal1.LocalIndex);
yield return instruction;
continue;
}
yield return instruction;
}
} */
}
}

26
mod/Patches/DragTool.cs Normal file
View File

@@ -0,0 +1,26 @@
using System.Reflection;
using HarmonyLib;
namespace PriorityUX {
[HarmonyPatch(typeof(DragTool))]
static class DragToolPatches {
static readonly FieldInfo interceptNumberKeysForPriority = typeof(DragTool).GetField("interceptNumberKeysForPriority", BindingFlags.NonPublic | BindingFlags.Instance);
[HarmonyPatch("OnActivateTool")]
[HarmonyPostfix]
static void OnActivateTool_Postfix(DragTool __instance) {
Debug.Log("[PriorityUX] >> OnActivateTool_Postfix " + __instance.GetType().Name + " -> refreshToolMenuPriority");
Utils.refreshToolMenuPriority();
}
[HarmonyPatch("OnPrefabInit")]
[HarmonyPostfix]
static void OnPrefabInit_Postfix(object __instance) {
if (Options.Instance.enableKeysForAllTools) {
interceptNumberKeysForPriority.SetValue(__instance, true);
}
}
}
}

View File

@@ -0,0 +1,45 @@
using HarmonyLib;
namespace PriorityUX {
[HarmonyPatch(typeof(MaterialSelectionPanel))]
public class MaterialSelectionPanelPatches {
[HarmonyPatch(nameof(MaterialSelectionPanel.RefreshSelectors))]
[HarmonyPostfix]
static void RefreshSelectorsPostfix(PriorityScreen ___priorityScreen) {
Debug.Log("RefreshSelectors");
Debug.Log("___priorityScreen.gameObject.activeSelf: " + ___priorityScreen.gameObject.activeSelf);
// if (___priorityScreen.gameObject.activeSelf)
Utils.refreshPriorityScreen(___priorityScreen);
}
public static void OnKeyDown(KButtonEvent e, MaterialSelectionPanel panel) {
Debug.Log("MaterialSelectionPanelPatches OnKeyDown " + e.GetAction());
if (!Options.Instance.enableKeysForBuilding)
return;
Action action = e.GetAction();
// Repurpose GotoUserNav1 keys (default: Shift+1/2/3/etc.)
// if (Action.GotoUserNav1 <= action && action <= Action.GotoUserNav10 && e.TryConsume(action)) {
// int num = (int)(action - 25 + 1);
if (Action.Plan1 <= action && action <= Action.Plan10 && e.TryConsume(action)) {
int num = (int)(action - 36 + 1);
if (num <= 9) {
panel.PriorityScreen.SetScreenPriority(new PrioritySetting(PriorityScreen.PriorityClass.basic, num), play_sound: true);
} else {
panel.PriorityScreen.SetScreenPriority(new PrioritySetting(PriorityScreen.PriorityClass.topPriority, 1), play_sound: true);
}
}
}
public static void OnKeyUp(KButtonEvent e, MaterialSelectionPanel panel) {
if (!Options.Instance.enableKeysForBuilding)
return;
Action action = e.GetAction();
if (Action.GotoUserNav1 <= action && action <= Action.GotoUserNav10) {
e.TryConsume(action);
}
}
}
}

35
mod/Patches/PlanScreen.cs Normal file
View File

@@ -0,0 +1,35 @@
using System.Reflection;
using HarmonyLib;
namespace PriorityUX {
[HarmonyPatch(typeof(PlanScreen))]
static class PlanScreenPatches {
[HarmonyPatch("OnKeyDown")]
[HarmonyPrefix]
public static bool OnKeyDown_Prefix(KButtonEvent e, PlanScreen __instance) {
// Debug.Log("PlanScreen OnKeyDown " + e.GetAction());
// Debug.Log("__instance.ProductInfoScreen: " + __instance.ProductInfoScreen);
// Debug.Log("ProductInfoScreen Active: " + __instance.ProductInfoScreen.gameObject.activeSelf);
if (__instance.ProductInfoScreen.gameObject.activeSelf) {
MaterialSelectionPanelPatches.OnKeyDown(e, __instance.ProductInfoScreen.materialSelectionPanel);
return false;
}
return true;
}
[HarmonyPatch("OnKeyUp")]
[HarmonyPrefix]
public static bool OnKeyUp_Prefix(KButtonEvent e, PlanScreen __instance) {
// Debug.Log("PlanScreen OnKeyUp " + e.GetAction());
// Debug.Log("__instance.ProductInfoScreen: " + __instance.ProductInfoScreen);
// Debug.Log("ProductInfoScreen Active: " + __instance.ProductInfoScreen.gameObject.activeSelf);
if (__instance.ProductInfoScreen.gameObject.activeSelf) {
MaterialSelectionPanelPatches.OnKeyUp(e, __instance.ProductInfoScreen.materialSelectionPanel);
return false;
}
return true;
}
}
}

View File

@@ -0,0 +1,69 @@
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<CodeInstruction> InstantiateButtonsTranspiler(IEnumerable<CodeInstruction> 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;
}
}
}
}

48
mod/Patches/Screen.cs Normal file
View File

@@ -0,0 +1,48 @@
// Generic place to intercept screen functions that are not overriden
using System;
using HarmonyLib;
using UnityEngine;
namespace PriorityUX {
// [HarmonyPatch(typeof(KScreen))]
// static class ScreenKeyPatches {
// [HarmonyPatch("OnKeyDown")]
// [HarmonyPostfix]
// static void OnKeyDown_Postfix(KButtonEvent e, object __instance) {
// // Debug.Log("KeyDown " + e + " " + __instance);
// // Debug.Log(Environment.StackTrace);
// // if (__instance is MaterialSelectionPanel msp) {
// // Debug.Log("KD> Material Selection");
// // }
// // if (__instance is MaterialSelectionPanel msp) {
// // Debug.Log("[PriorityUX] >> Redirect MaterialSelectionPanel KeyDown");
// // Debug.Log(msp);
// // MaterialSelectionPanelPatches.OnKeyDown(e, msp);
// // }
// }
// [HarmonyPatch("OnKeyUp")]
// [HarmonyPostfix]
// static void OnKeyUp_Postfix(KButtonEvent e, object __instance) {
// // if (__instance is MaterialSelectionPanel msp) {
// // Debug.Log("[PriorityUX] >> Redirect MaterialSelectionPanel KeyUp");
// // Debug.Log(msp);
// // MaterialSelectionPanelPatches.OnKeyUp(e, msp);
// // }
// }
// }
// [HarmonyPatch(typeof(KIconToggleMenu))]
// static class Test {
// [HarmonyPatch("OnKeyDown")]
// [HarmonyPostfix]
// static void OnKeyDown_Postfix(KButtonEvent e, object __instance) {
// // Debug.Log("KIconToggleMenu KeyDown " + e + " " + __instance);
// // Debug.Log(Environment.StackTrace);
// }
// }
}