Skip to content

Commit bfac6a2

Browse files
committed
fix stock patches
1 parent ed0268b commit bfac6a2

File tree

3 files changed

+46
-19
lines changed

3 files changed

+46
-19
lines changed

Source/DynamicProperties/MpbCompilerCache.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
using System;
21
using System.Collections.Generic;
32
using System.Runtime.CompilerServices;
43
using KSPBuildTools;
5-
using UnityEngine;
64

75
namespace Shabby.DynamicProperties;
86

@@ -45,7 +43,7 @@ internal static void CheckCleared()
4543
{
4644
if (Cache.Count == 0) return;
4745

48-
Debug.LogError($"{Cache.Count} MpbCompilers were not disposed; forcing removal");
46+
Log.Error($"{Cache.Count} MpbCompilers were not disposed; forcing removal");
4947
foreach (var compiler in Cache.Values) compiler.Dispose();
5048
Cache.Clear();
5149
}

Source/DynamicProperties/Patches/MaterialColorUpdaterPatch.cs

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,12 @@ public class MaterialColorUpdaterPatch
1010
{
1111
internal static readonly Dictionary<MaterialColorUpdater, Props> temperatureColorProps = [];
1212

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-
2013
[HarmonyPostfix]
2114
[HarmonyPatch("CreateRendererList")]
2215
private static void MaterialColorUpdater_CreateRendererList_Postfix(
2316
MaterialColorUpdater __instance)
2417
{
25-
var props = temperatureColorProps[__instance];
18+
var props = temperatureColorProps[__instance] = new Props(int.MinValue + 1);
2619
foreach (var renderer in __instance.renderers) {
2720
MaterialPropertyManager.Instance?.Set(renderer, props);
2821
}
@@ -46,6 +39,7 @@ private static IEnumerable<CodeInstruction> Update_Transpiler(
4639
foreach (var insn in insns) {
4740
yield return insn;
4841

42+
// this.mpb.SetColor(this.propertyID, this.setColor);
4943
// IL_0022: ldarg.0 // this
5044
// IL_0023: ldfld class UnityEngine.MaterialPropertyBlock MaterialColorUpdater::mpb
5145
// IL_0028: ldarg.0 // this
@@ -54,14 +48,17 @@ private static IEnumerable<CodeInstruction> Update_Transpiler(
5448
// IL_002f: ldfld valuetype UnityEngine.Color MaterialColorUpdater::setColor
5549
// IL_0034: callvirt instance void UnityEngine.MaterialPropertyBlock::SetColor(int32, valuetype UnityEngine.Color)
5650
if (insn.Calls(MPB_SetColor)) break;
51+
// Remaining code applies MPB to renderers.
5752
}
5853

59-
CodeInstruction[] replace = [
54+
// MaterialColorUpdaterPatch.Update_SetProperty(this);
55+
// return;
56+
CodeInstruction[] updateProp = [
6057
new(OpCodes.Ldarg_0), // this
6158
CodeInstruction.Call(() => Update_SetProperty(default)),
6259
new(OpCodes.Ret)
6360
];
64-
foreach (var insn in replace) yield return insn;
61+
foreach (var insn in updateProp) yield return insn;
6562
}
6663

6764
private static void DisposeIfExists(MaterialColorUpdater mcu)
@@ -84,6 +81,39 @@ private static void Part_OnDestroy_Postfix(Part __instance)
8481
DisposeIfExists(__instance.temperatureRenderer);
8582
}
8683

84+
[HarmonyTranspiler]
85+
[HarmonyPatch(typeof(ModuleJettison), nameof(ModuleJettison.Jettison))]
86+
private static IEnumerable<CodeInstruction> ModuleJettison_Jettison_Transpiler(
87+
IEnumerable<CodeInstruction> insns)
88+
{
89+
var ModuleJettison_jettisonTemperatureRenderer = AccessTools.Field(
90+
typeof(ModuleJettison), nameof(ModuleJettison.jettisonTemperatureRenderer));
91+
92+
// this.jettisonTemperatureRenderer = null;
93+
// IL_0327: ldarg.0 // this
94+
// IL_0328: ldnull
95+
// IL_0329: stfld class MaterialColorUpdater ModuleJettison::jettisonTemperatureRenderer
96+
CodeMatch[] matchSetTempRendererNull = [
97+
new(OpCodes.Ldarg_0),
98+
new(OpCodes.Ldnull),
99+
new(OpCodes.Stfld, ModuleJettison_jettisonTemperatureRenderer)
100+
];
101+
102+
var matcher = new CodeMatcher(insns);
103+
104+
matcher
105+
.MatchStartForward(matchSetTempRendererNull)
106+
.ThrowIfNotMatch("failed to find set temp renderer null")
107+
.Insert(
108+
// MaterialColorUpdaterPatch.DisposeIfExists(this.jettisonTemperatureRenderer);
109+
new CodeInstruction(OpCodes.Ldarg_0), // this
110+
new CodeInstruction(OpCodes.Ldfld, ModuleJettison_jettisonTemperatureRenderer),
111+
CodeInstruction.Call(() => DisposeIfExists(default))
112+
);
113+
114+
return matcher.InstructionEnumeration();
115+
}
116+
87117
// FIXME: write a transpiler for ModuleJettison.Jettison.
88118

89119
[HarmonyPostfix]

Source/DynamicProperties/Patches/NoDuplicateMaterials.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Collections.Generic;
2-
using System.Diagnostics;
32
using System.Reflection;
43
using HarmonyLib;
54
using Highlighting;
@@ -48,11 +47,11 @@ internal static class NoDuplicateMaterials
4847

4948
private static IEnumerable<MethodBase> TargetMethods() => [
5049
AccessTools.Method(typeof(Highlighter), "GrabRenderers"),
51-
AccessTools.Method(typeof(MaterialColorUpdater), "CreateRendererList"),
52-
AccessTools.Method(typeof(ModuleColorChanger), "ProcessMaterialsList"),
53-
AccessTools.Method(
54-
typeof(GameObjectExtension), nameof(GameObjectExtension.SetLayerRecursive),
55-
[typeof(GameObject), typeof(int), typeof(bool), typeof(int)])
50+
// AccessTools.Method(typeof(MaterialColorUpdater), "CreateRendererList"),
51+
AccessTools.Method(typeof(ModuleColorChanger), "ProcessMaterialsList")
52+
// AccessTools.Method(
53+
// typeof(GameObjectExtension), nameof(GameObjectExtension.SetLayerRecursive),
54+
// [typeof(GameObject), typeof(int), typeof(bool), typeof(int)])
5655
];
5756

5857
[HarmonyTranspiler]

0 commit comments

Comments
 (0)