Skip to content

Commit f1295be

Browse files
committed
switch to KSPAddon
1 parent e8aa297 commit f1295be

File tree

3 files changed

+45
-27
lines changed

3 files changed

+45
-27
lines changed

Source/DynamicProperties/CompiledProps.cs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using KSPBuildTools;
23
using UnityEngine;
34

45
namespace Shabby.DynamicProperties;
@@ -21,9 +22,10 @@ internal class CompiledProps
2122
private static readonly Dictionary<SortedSet<Props>, MpbCacheEntry> MpbCache =
2223
new(CascadeKeyComparer);
2324

24-
internal static void Clear() => MpbCache.Clear();
25+
internal static void ClearCache() => MpbCache.Clear();
2526

2627
private readonly SortedSet<Props> cascade = new(Props.PriorityComparer);
28+
private MpbCacheEntry? cacheEntry = null;
2729

2830
internal bool Add(Props props)
2931
{
@@ -39,21 +41,19 @@ internal bool Remove(Props props)
3941
return removed;
4042
}
4143

42-
private MpbCacheEntry? cacheEntry = null;
43-
4444
// Should this be a hashset?
4545
private static readonly List<Props> _changedProps = [];
4646

4747
internal static void RefreshChangedProps()
4848
{
49-
foreach (var (cascade, cache) in MpbCache) {
50-
cache.Changed = false;
49+
foreach (var (cascade, cacheEntry) in MpbCache) {
50+
cacheEntry.Changed = false;
5151
foreach (var props in cascade) {
5252
if (!props.Changed) continue;
53-
cache.Changed = true;
53+
cacheEntry.Changed = true;
5454
_changedProps.Add(props);
55-
foreach (var managedId in cache.ManagedIds[props]) {
56-
props.Write(managedId, cache.Mpb);
55+
foreach (var managedId in cacheEntry.ManagedIds[props]) {
56+
props.Write(managedId, cacheEntry.Mpb);
5757
}
5858
}
5959
}
@@ -90,16 +90,24 @@ internal bool GetIfChanged(out MaterialPropertyBlock? mpb)
9090
{
9191
if (cacheEntry != null) {
9292
mpb = cacheEntry.Changed ? cacheEntry.Mpb : null;
93+
// if (cacheEntry.Changed && HighLogic.LoadedSceneIsEditor) {
94+
// Debug.Log(cascade.Aggregate("props:\n", (current, props) => current + props));
95+
// }
96+
9397
return cacheEntry.Changed;
9498
}
9599

96100
if (!MpbCache.TryGetValue(cascade, out cacheEntry)) {
97-
Debug.Log("cache not hit");
101+
MaterialPropertyManager.Instance.LogDebug("building new MPB");
98102
cacheEntry = BuildCacheEntry(cascade);
99103
} else {
100-
Debug.Log("cache hit!");
104+
MaterialPropertyManager.Instance.LogDebug("MPB cache hit");
101105
}
102106

107+
// if (HighLogic.LoadedSceneIsEditor) {
108+
// Debug.Log(cascade.Aggregate("props:\n", (current, props) => current + props));
109+
// }
110+
103111
mpb = cacheEntry.Mpb;
104112
return true;
105113
}

Source/DynamicProperties/MaterialPropertyManager.cs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
using System.Collections.Generic;
2+
using KSPBuildTools;
23
using UnityEngine;
34

45
namespace Shabby.DynamicProperties;
56

6-
[KSPScenario(
7-
createOptions: ScenarioCreationOptions.AddToAllGames,
8-
tgtScenes: [GameScenes.LOADING, GameScenes.EDITOR, GameScenes.FLIGHT])]
9-
public sealed class MaterialPropertyManager : ScenarioModule
7+
[KSPAddon(KSPAddon.Startup.EveryScene, false)]
8+
public sealed class MaterialPropertyManager : MonoBehaviour
109
{
1110
#region Fields
1211

@@ -22,18 +21,26 @@ private MaterialPropertyManager()
2221
{
2322
}
2423

25-
public override void OnAwake()
24+
private void Awake()
2625
{
26+
if (Instance != null) {
27+
DestroyImmediate(this);
28+
return;
29+
}
30+
2731
name = nameof(MaterialPropertyManager);
2832
Instance = this;
2933
}
3034

3135
private void LateUpdate() => Refresh();
3236

33-
public void OnDestroy()
37+
private void OnDestroy()
3438
{
39+
if (Instance != this) return;
40+
3541
Instance = null;
36-
CompiledProps.Clear();
42+
CompiledProps.ClearCache();
43+
this.LogDebug("destroyed");
3744
}
3845

3946
#endregion
@@ -61,17 +68,20 @@ private void Refresh()
6168

6269
foreach (var (renderer, compiledProps) in compiledProperties) {
6370
if (renderer == null) {
71+
this.LogDebug($"dead renderer {renderer.GetHashCode()}");
6472
_deadRenderers.Add(renderer);
6573
continue;
6674
}
6775

76+
if (!renderer.gameObject.activeInHierarchy) continue;
77+
6878
if (compiledProps.GetIfChanged(out var mpb)) {
79+
this.LogDebug($"set mpb on renderer {renderer.name} {renderer.GetHashCode()}\n");
6980
renderer.SetPropertyBlock(mpb);
7081
}
7182
}
7283

73-
foreach (var dead in _deadRenderers) {
74-
compiledProperties.Remove(dead);
75-
}
84+
foreach (var dead in _deadRenderers) compiledProperties.Remove(dead);
85+
_deadRenderers.Clear();
7686
}
7787
}

Source/DynamicProperties/Patches/PartPatch.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@ namespace Shabby.DynamicProperties;
88
[HarmonyPatch(typeof(Part))]
99
internal static class PartPatch
1010
{
11-
private static readonly Dictionary<Part, Props> highlightProperties = [];
11+
private static readonly Dictionary<Part, Props> rimHighlightProps = [];
1212

1313
[HarmonyPostfix]
1414
[HarmonyPatch("Awake")]
1515
private static void Awake_Postfix(Part __instance)
1616
{
17-
highlightProperties[__instance] = new Props(int.MinValue + 1);
17+
rimHighlightProps[__instance] = new Props(int.MinValue + 1);
1818
}
1919

2020
[HarmonyPostfix]
2121
[HarmonyPatch("CreateRendererLists")]
2222
private static void CreateRendererLists_Postfix(Part __instance)
2323
{
24-
var props = highlightProperties[__instance];
24+
var props = rimHighlightProps[__instance];
2525
props.SetFloat(PropertyIDs._RimFalloff, 2f);
2626
props.SetColor(PropertyIDs._RimColor, Part.defaultHighlightNone);
2727
foreach (var renderer in __instance.HighlightRenderer) {
@@ -35,13 +35,13 @@ private static bool SetOpacity_Prefix(Part __instance, float opacity)
3535
{
3636
__instance.CreateRendererLists();
3737
__instance.mpb.SetFloat(PropertyIDs._Opacity, opacity);
38-
highlightProperties[__instance].SetFloat(PropertyIDs._Opacity, opacity);
38+
rimHighlightProps[__instance].SetFloat(PropertyIDs._Opacity, opacity);
3939
return false;
4040
}
4141

4242
private static void Highlight_SetRimColor(Part part, Color color)
4343
{
44-
highlightProperties[part].SetColor(PropertyIDs._RimColor, color);
44+
rimHighlightProps[part].SetColor(PropertyIDs._RimColor, color);
4545
}
4646

4747
[HarmonyTranspiler]
@@ -52,7 +52,7 @@ private static IEnumerable<CodeInstruction> Highlight_Transpiler(
5252
var MPB_SetColor = AccessTools.Method(
5353
typeof(MaterialPropertyBlock),
5454
nameof(MaterialPropertyBlock.SetColor),
55-
[typeof(int)]);
55+
[typeof(int), typeof(Color)]);
5656
var Part_get_mpb = AccessTools.PropertyGetter(typeof(Part), nameof(Part.mpb));
5757
var Part_highlightRenderer =
5858
AccessTools.Field(typeof(Part), nameof(Part.highlightRenderer));
@@ -122,6 +122,6 @@ private static IEnumerable<CodeInstruction> Highlight_Transpiler(
122122
[HarmonyPatch("OnDestroy")]
123123
private static void OnDestroy_Postfix(Part __instance)
124124
{
125-
highlightProperties.Remove(__instance);
125+
rimHighlightProps.Remove(__instance);
126126
}
127127
}

0 commit comments

Comments
 (0)