Skip to content

Commit 575ca98

Browse files
committed
improve destructor logging
1 parent 4b24c3c commit 575ca98

File tree

10 files changed

+129
-116
lines changed

10 files changed

+129
-116
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using KSPBuildTools;
3+
4+
namespace Shabby.DynamicProperties;
5+
6+
public abstract class Disposable : IDisposable
7+
{
8+
protected virtual bool IsUnused() => false;
9+
10+
protected abstract void OnDispose();
11+
12+
private bool _disposed = false;
13+
14+
private void HandleDispose(bool disposing)
15+
{
16+
if (_disposed) return;
17+
18+
if (disposing) {
19+
Log.Debug($"disposing {GetType().Name} instance {GetHashCode()}");
20+
OnDispose();
21+
} else if (!IsUnused()) {
22+
Log.Warning(
23+
$"active {GetType().Name} instance {GetHashCode()} was not disposed");
24+
}
25+
26+
_disposed = true;
27+
}
28+
29+
public void Dispose()
30+
{
31+
HandleDispose(true);
32+
GC.SuppressFinalize(this);
33+
}
34+
35+
~Disposable()
36+
{
37+
HandleDispose(false);
38+
}
39+
}

Source/DynamicProperties/MaterialPropertyManager.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ private void OnDestroy()
4343
MpbCompilerCache.CheckCleared();
4444

4545
// Poor man's GC :'(
46-
MaterialColorUpdaterPatch.temperatureColorProps.Clear();
47-
ModuleColorChangerPatch.mccProps.Clear();
46+
PartPatch.ClearOnSceneSwitch();
47+
MaterialColorUpdaterPatch.ClearOnSceneSwitch();
48+
ModuleColorChangerPatch.ClearOnSceneSwitch();
4849

4950
this.LogMessage("destroyed");
5051
}

Source/DynamicProperties/MpbCompiler.cs

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
#nullable enable
22

3-
using System;
43
using System.Collections.Generic;
54
using System.Runtime.CompilerServices;
65
using KSPBuildTools;
76
using UnityEngine;
87

98
namespace Shabby.DynamicProperties;
109

11-
internal class MpbCompiler : IDisposable
10+
internal class MpbCompiler : Disposable
1211
{
1312
#region Fields
1413

@@ -126,43 +125,22 @@ private void ApplyAll()
126125
}
127126

128127
foreach (var dead in _deadRenderers) {
129-
MaterialPropertyManager.Instance.LogDebug($"dead renderer {dead.GetHashCode()}");
130-
MaterialPropertyManager.Instance.Remove(dead);
128+
MaterialPropertyManager.Instance?.LogDebug($"dead renderer {dead.GetHashCode()}");
129+
MaterialPropertyManager.Instance?.Remove(dead);
131130
}
132131

133132
_deadRenderers.Clear();
134133
}
135134

136135
#endregion
137136

138-
#region dtor
137+
protected override bool IsUnused() => linkedRenderers.Count == 0;
139138

140-
private bool _disposed = false;
141-
142-
private void HandleDispose()
139+
protected override void OnDispose()
143140
{
144-
if (_disposed) return;
145-
146-
Log.Debug($"disposing MPB compiler instance {RuntimeHelpers.GetHashCode(this)}");
147-
148141
foreach (var props in Cascade) {
149142
props.OnEntriesChanged -= OnPropsEntriesChanged;
150143
props.OnValueChanged -= OnPropsValueChanged;
151144
}
152-
153-
_disposed = true;
154-
}
155-
156-
public void Dispose()
157-
{
158-
HandleDispose();
159-
GC.SuppressFinalize(this);
160-
}
161-
162-
~MpbCompiler()
163-
{
164-
HandleDispose();
165145
}
166-
167-
#endregion
168146
}

Source/DynamicProperties/MpbCompilerCache.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Runtime.CompilerServices;
34
using KSPBuildTools;

Source/DynamicProperties/Patches/MaterialColorUpdaterPatch.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,22 @@
66
namespace Shabby.DynamicProperties;
77

88
[HarmonyPatch(typeof(MaterialColorUpdater))]
9-
public class MaterialColorUpdaterPatch
9+
internal class MaterialColorUpdaterPatch : StockPatchBase<MaterialColorUpdater>
1010
{
11-
internal static readonly Dictionary<MaterialColorUpdater, Props> temperatureColorProps = [];
12-
1311
[HarmonyPostfix]
1412
[HarmonyPatch("CreateRendererList")]
1513
private static void MaterialColorUpdater_CreateRendererList_Postfix(
1614
MaterialColorUpdater __instance)
1715
{
18-
var props = temperatureColorProps[__instance] = new Props(int.MinValue + 1);
16+
var props = Props[__instance] = new Props(int.MinValue + 1);
1917
foreach (var renderer in __instance.renderers) {
2018
MaterialPropertyManager.Instance?.Set(renderer, props);
2119
}
2220
}
2321

2422
private static void Update_SetProperty(MaterialColorUpdater mcu)
2523
{
26-
temperatureColorProps[mcu].SetColor(mcu.propertyID, mcu.setColor);
24+
Props[mcu].SetColor(mcu.propertyID, mcu.setColor);
2725
}
2826

2927
[HarmonyTranspiler]
@@ -64,7 +62,7 @@ private static IEnumerable<CodeInstruction> Update_Transpiler(
6462
private static void DisposeIfExists(MaterialColorUpdater mcu)
6563
{
6664
if (mcu == null) return;
67-
if (temperatureColorProps.TryGetValue(mcu, out var props)) props.Dispose();
65+
if (Props.TryGetValue(mcu, out var props)) props.Dispose();
6866
}
6967

7068
[HarmonyPrefix]

Source/DynamicProperties/Patches/ModuleColorChangerPatch.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
1-
using System.Collections.Generic;
21
using HarmonyLib;
32

43
namespace Shabby.DynamicProperties;
54

65
[HarmonyPatch(typeof(ModuleColorChanger))]
7-
internal class ModuleColorChangerPatch
6+
internal class ModuleColorChangerPatch : StockPatchBase<ModuleColorChanger>
87
{
9-
internal static readonly Dictionary<ModuleColorChanger, Props> mccProps = [];
10-
118
[HarmonyPostfix]
129
[HarmonyPatch(nameof(ModuleColorChanger.OnStart))]
1310
private static void OnStart_Postfix(ModuleColorChanger __instance)
1411
{
15-
mccProps[__instance] = new Props(0);
12+
Props[__instance] = new Props(0);
1613
}
1714

1815
[HarmonyPostfix]
1916
[HarmonyPatch("EditRenderers")]
2017
private static void EditRenderers_Postfix(ModuleColorChanger __instance)
2118
{
22-
var props = mccProps[__instance];
19+
var props = Props[__instance];
2320
foreach (var renderer in __instance.renderers) {
2421
MaterialPropertyManager.Instance?.Set(renderer, props);
2522
}
@@ -29,7 +26,7 @@ private static void EditRenderers_Postfix(ModuleColorChanger __instance)
2926
[HarmonyPatch("UpdateColor")]
3027
public static bool UpdateColor_Prefix(ModuleColorChanger __instance)
3128
{
32-
mccProps[__instance].SetColor(__instance.shaderPropertyInt, __instance.color);
29+
Props[__instance].SetColor(__instance.shaderPropertyInt, __instance.color);
3330
return false;
3431
}
3532

@@ -38,7 +35,7 @@ public static bool UpdateColor_Prefix(ModuleColorChanger __instance)
3835
private static void Part_OnDestroy_Postfix(Part __instance)
3936
{
4037
foreach (var mcc in __instance.FindModulesImplementing<ModuleColorChanger>()) {
41-
if (mccProps.Remove(mcc, out var props)) props.Dispose();
38+
if (Props.Remove(mcc, out var props)) props.Dispose();
4239
}
4340
}
4441

Source/DynamicProperties/Patches/PartPatch.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,20 @@
66
namespace Shabby.DynamicProperties;
77

88
[HarmonyPatch(typeof(Part))]
9-
internal static class PartPatch
9+
internal class PartPatch : StockPatchBase<Part>
1010
{
11-
private static readonly Dictionary<Part, Props> rimHighlightProps = [];
12-
1311
[HarmonyPostfix]
1412
[HarmonyPatch("Awake")]
1513
private static void Awake_Postfix(Part __instance)
1614
{
17-
rimHighlightProps[__instance] = new Props(int.MinValue + 1);
15+
Props[__instance] = new Props(int.MinValue + 1);
1816
}
1917

2018
[HarmonyPostfix]
2119
[HarmonyPatch("CreateRendererLists")]
2220
private static void CreateRendererLists_Postfix(Part __instance)
2321
{
24-
var props = rimHighlightProps[__instance];
22+
var props = Props[__instance];
2523
props.SetFloat(PropertyIDs._RimFalloff, 2f);
2624
props.SetColor(PropertyIDs._RimColor, Part.defaultHighlightNone);
2725
foreach (var renderer in __instance.HighlightRenderer) {
@@ -35,13 +33,13 @@ private static bool SetOpacity_Prefix(Part __instance, float opacity)
3533
{
3634
__instance.CreateRendererLists();
3735
__instance.mpb.SetFloat(PropertyIDs._Opacity, opacity);
38-
rimHighlightProps[__instance].SetFloat(PropertyIDs._Opacity, opacity);
36+
Props[__instance].SetFloat(PropertyIDs._Opacity, opacity);
3937
return false;
4038
}
4139

4240
private static void Highlight_SetRimColor(Part part, Color color)
4341
{
44-
rimHighlightProps[part].SetColor(PropertyIDs._RimColor, color);
42+
Props[part].SetColor(PropertyIDs._RimColor, color);
4543
}
4644

4745
[HarmonyTranspiler]
@@ -122,7 +120,7 @@ private static IEnumerable<CodeInstruction> Highlight_Transpiler(
122120
[HarmonyPatch("OnDestroy")]
123121
private static void OnDestroy_Postfix(Part __instance)
124122
{
125-
if (rimHighlightProps.Remove(__instance, out var props)) {
123+
if (Props.Remove(__instance, out var props)) {
126124
props.Dispose();
127125
}
128126
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System.Collections.Generic;
2+
using KSPBuildTools;
3+
4+
namespace Shabby.DynamicProperties;
5+
6+
internal abstract class StockPatchBase<T>
7+
{
8+
internal static readonly Dictionary<T, Props> Props = [];
9+
10+
internal static void ClearOnSceneSwitch()
11+
{
12+
if (Props.Count == 0) return;
13+
14+
Log.Message($"cleared {Props.Count} Props instances", $"[{typeof(T).Name} MPM Patch]");
15+
Props.Clear();
16+
}
17+
}

0 commit comments

Comments
 (0)