oni-priority-ux/mod/Patches/Deconstructable.cs

83 lines
4.0 KiB
C#

using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
using System;
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(new Type[] { typeof(bool) })]
[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;
}
} */
}
}