-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMaterialPropertyManager.cs
More file actions
177 lines (136 loc) · 4.42 KB
/
MaterialPropertyManager.cs
File metadata and controls
177 lines (136 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#nullable enable
using System.Collections.Generic;
using KSPBuildTools;
using UnityEngine;
namespace Shabby.DynamicProperties;
[KSPAddon(KSPAddon.Startup.EveryScene, false)]
public sealed class MaterialPropertyManager : MonoBehaviour
{
#region Fields
public static MaterialPropertyManager? Instance { get; private set; }
private readonly Dictionary<Renderer, PropsCascade> rendererCascades = [];
private readonly List<Props> propsLateUpdateQueue = [];
#endregion
#region Lifecycle
private MaterialPropertyManager()
{
}
private void Awake()
{
if (Instance != null) {
DestroyImmediate(this);
return;
}
name = nameof(MaterialPropertyManager);
Instance = this;
}
private void OnDestroy()
{
if (Instance != this) return;
Instance = null;
foreach (var cascade in rendererCascades.Values) cascade.Dispose();
MpbCompilerCache.CheckCleared();
// Poor man's GC :'(
PartPatch.CheckCleared();
MaterialColorUpdaterPatch.CheckCleared();
ModuleColorChangerPatch.CheckCleared();
FairingPanelPatch.CheckCleared();
this.LogMessage("destroyed");
}
#endregion
#region Public API
public bool Set(Renderer renderer, Props props)
{
if (!CheckRendererAlive(renderer)) return false;
if (!rendererCascades.TryGetValue(renderer, out var cascade)) {
rendererCascades[renderer] = cascade = new PropsCascade(renderer);
}
return cascade.Add(props);
}
public bool Unset(Renderer renderer, Props props)
{
if (!CheckRendererAlive(renderer)) return false;
if (!rendererCascades.TryGetValue(renderer, out var cascade)) return false;
return cascade.Remove(props);
}
public bool Unregister(Renderer renderer)
{
if (renderer.IsNullref()) return false;
if (!rendererCascades.Remove(renderer, out var cascade)) return false;
if (renderer.IsDestroyed()) this.LogDebug($"destroyed renderer {renderer.GetHashCode()}");
cascade.Dispose();
return true;
}
/// Get a reference to the `Props` instance containing the stock properties of the given
/// `part` (namely, `_Opacity`, `_RimFalloff`, and `_RimColor`).
/// The returned instance must not be written to.
public Props? GetStockPropsForPart(Part part) => PartPatch.Props.GetValueOrDefault(part);
/// Get the part's current `_TemperatureColor` property, if it is set (only in flight).
public Color? GetStockTemperatureColorForPart(Part part)
{
if (!MaterialColorUpdaterPatch.Props.TryGetValue(part.temperatureRenderer, out var props)) {
return null;
}
if (!props.HasColor(PhysicsGlobals.temperaturePropertyID)) return null;
return props.GetColorOrDefault(PhysicsGlobals.temperaturePropertyID);
}
public static void RegisterPropertyNamesForDebugLogging(params string[] properties)
{
foreach (var property in properties) PropIdToName.Register(property);
}
#endregion
private bool CheckRendererAlive(Renderer renderer)
{
if (renderer.IsNullref()) {
Log.LogError(this, "renderer reference is null");
return false;
}
if (renderer.IsDestroyed()) {
this.LogWarning($"cannot modify destroyed renderer {renderer.GetHashCode()}");
Unregister(renderer);
return false;
}
return true;
}
private readonly List<Renderer> _destroyedRenderers = [];
internal void CheckRemoveDestroyedRenderers()
{
foreach (var renderer in rendererCascades.Keys) {
if (renderer.IsDestroyed()) _destroyedRenderers.Add(renderer);
}
foreach (var destroyed in _destroyedRenderers) Unregister(destroyed);
_destroyedRenderers.Clear();
}
/// Public API equivalent is calling `Props.Dispose`.
internal void Unregister(Props props)
{
foreach (var (renderer, cascade) in rendererCascades) {
if (!renderer.IsDestroyed()) cascade.Remove(props);
}
CheckRemoveDestroyedRenderers();
}
private bool _propsUpdateScheduled = false;
private static readonly WaitForEndOfFrame WfEoF = new();
private IEnumerator<YieldInstruction> Co_PropsLateUpdate()
{
yield return WfEoF;
foreach (var props in propsLateUpdateQueue) {
if (props.NeedsEntriesUpdate) {
props.OnEntriesChanged?.Invoke(props);
} else if (props.NeedsValueUpdate) {
props.OnValueChanged?.Invoke(props, null);
}
props.SuppressEagerUpdate =
props.NeedsEntriesUpdate = props.NeedsValueUpdate = false;
}
propsLateUpdateQueue.Clear();
_propsUpdateScheduled = false;
}
internal void ScheduleLateUpdate(Props props)
{
propsLateUpdateQueue.Add(props);
if (_propsUpdateScheduled) return;
StartCoroutine(Co_PropsLateUpdate());
_propsUpdateScheduled = true;
}
}