|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Reflection.Emit; |
| 3 | +using HarmonyLib; |
| 4 | +using UnityEngine; |
| 5 | + |
| 6 | +namespace Shabby.DynamicProperties; |
| 7 | + |
| 8 | +[HarmonyPatch(typeof(MaterialColorUpdater))] |
| 9 | +public class MaterialColorUpdaterPatch |
| 10 | +{ |
| 11 | + internal static readonly Dictionary<MaterialColorUpdater, Props> temperatureColorProps = []; |
| 12 | + |
| 13 | + [HarmonyPostfix] |
| 14 | + [HarmonyPatch(MethodType.Constructor, typeof(Transform), typeof(int), typeof(Part))] |
| 15 | + private static void MaterialColorUpdater_Ctor_Postfix(MaterialColorUpdater __instance) |
| 16 | + { |
| 17 | + temperatureColorProps[__instance] = new Props(int.MinValue + 1); |
| 18 | + } |
| 19 | + |
| 20 | + [HarmonyPostfix] |
| 21 | + [HarmonyPatch("CreateRendererList")] |
| 22 | + private static void MaterialColorUpdater_CreateRendererList_Postfix( |
| 23 | + MaterialColorUpdater __instance) |
| 24 | + { |
| 25 | + var props = temperatureColorProps[__instance]; |
| 26 | + foreach (var renderer in __instance.renderers) { |
| 27 | + MaterialPropertyManager.Instance?.Set(renderer, props); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + private static void Update_SetProperty(MaterialColorUpdater mcu) |
| 32 | + { |
| 33 | + temperatureColorProps[mcu].SetColor(mcu.propertyID, mcu.setColor); |
| 34 | + } |
| 35 | + |
| 36 | + [HarmonyTranspiler] |
| 37 | + [HarmonyPatch(nameof(MaterialColorUpdater.Update))] |
| 38 | + private static IEnumerable<CodeInstruction> Update_Transpiler( |
| 39 | + IEnumerable<CodeInstruction> insns) |
| 40 | + { |
| 41 | + var MPB_SetColor = AccessTools.Method( |
| 42 | + typeof(MaterialPropertyBlock), |
| 43 | + nameof(MaterialPropertyBlock.SetColor), |
| 44 | + [typeof(int), typeof(Color)]); |
| 45 | + |
| 46 | + foreach (var insn in insns) { |
| 47 | + yield return insn; |
| 48 | + |
| 49 | + // IL_0022: ldarg.0 // this |
| 50 | + // IL_0023: ldfld class UnityEngine.MaterialPropertyBlock MaterialColorUpdater::mpb |
| 51 | + // IL_0028: ldarg.0 // this |
| 52 | + // IL_0029: ldfld int32 MaterialColorUpdater::propertyID |
| 53 | + // IL_002e: ldarg.0 // this |
| 54 | + // IL_002f: ldfld valuetype UnityEngine.Color MaterialColorUpdater::setColor |
| 55 | + // IL_0034: callvirt instance void UnityEngine.MaterialPropertyBlock::SetColor(int32, valuetype UnityEngine.Color) |
| 56 | + if (insn.Calls(MPB_SetColor)) break; |
| 57 | + } |
| 58 | + |
| 59 | + CodeInstruction[] replace = [ |
| 60 | + new(OpCodes.Ldarg_0), // this |
| 61 | + CodeInstruction.Call(() => Update_SetProperty(default)), |
| 62 | + new(OpCodes.Ret) |
| 63 | + ]; |
| 64 | + foreach (var insn in replace) yield return insn; |
| 65 | + } |
| 66 | + |
| 67 | + private static void DisposeIfExists(MaterialColorUpdater mcu) |
| 68 | + { |
| 69 | + if (mcu == null) return; |
| 70 | + if (temperatureColorProps.TryGetValue(mcu, out var props)) props.Dispose(); |
| 71 | + } |
| 72 | + |
| 73 | + [HarmonyPrefix] |
| 74 | + [HarmonyPatch(typeof(Part), nameof(Part.ResetMPB))] |
| 75 | + private static void Part_ResetMPB_Prefix(Part __instance) |
| 76 | + { |
| 77 | + DisposeIfExists(__instance.temperatureRenderer); |
| 78 | + } |
| 79 | + |
| 80 | + [HarmonyPostfix] |
| 81 | + [HarmonyPatch(typeof(Part), "OnDestroy")] |
| 82 | + private static void Part_OnDestroy_Postfix(Part __instance) |
| 83 | + { |
| 84 | + DisposeIfExists(__instance.temperatureRenderer); |
| 85 | + } |
| 86 | + |
| 87 | + // FIXME: write a transpiler for ModuleJettison.Jettison. |
| 88 | + |
| 89 | + [HarmonyPostfix] |
| 90 | + [HarmonyPatch(typeof(ModuleJettison), "OnDestroy")] |
| 91 | + private static void ModuleJettison_OnDestroy_Postfix(ModuleJettison __instance) |
| 92 | + { |
| 93 | + DisposeIfExists(__instance.jettisonTemperatureRenderer); |
| 94 | + } |
| 95 | +} |
0 commit comments