Skip to content

Commit f78d5fe

Browse files
committed
just use dynamic dispatch
1 parent 014c6a7 commit f78d5fe

File tree

1 file changed

+48
-28
lines changed

1 file changed

+48
-28
lines changed

Source/DynamicProperties/Props.cs

Lines changed: 48 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Linq;
34
using System.Runtime.CompilerServices;
@@ -48,15 +49,42 @@ internal static string Get(int id) =>
4849
IdToName.TryGetValue(id, out var name) ? name : $"<{id}>";
4950
}
5051

51-
internal abstract class Prop;
52+
internal abstract class Prop
53+
{
54+
internal abstract void Write(int id, MaterialPropertyBlock mpb);
55+
}
5256

53-
internal class Prop<T>(T value) : Prop
57+
internal abstract class Prop<T>(T value) : Prop
5458
{
5559
internal T Value = value;
56-
5760
public override string ToString() => Value.ToString();
5861
}
5962

63+
internal class PropColor(Color value) : Prop<Color>(value)
64+
{
65+
internal override void Write(int id, MaterialPropertyBlock mpb) => mpb.SetColor(id, Value);
66+
}
67+
68+
internal class PropFloat(float value) : Prop<float>(value)
69+
{
70+
internal override void Write(int id, MaterialPropertyBlock mpb) => mpb.SetFloat(id, Value);
71+
}
72+
73+
internal class PropInt(int value) : Prop<int>(value)
74+
{
75+
internal override void Write(int id, MaterialPropertyBlock mpb) => mpb.SetInt(id, Value);
76+
}
77+
78+
internal class PropTexture(Texture value) : Prop<Texture>(value)
79+
{
80+
internal override void Write(int id, MaterialPropertyBlock mpb) => mpb.SetTexture(id, Value);
81+
}
82+
83+
internal class PropVector(Vector4 value) : Prop<Vector4>(value)
84+
{
85+
internal override void Write(int id, MaterialPropertyBlock mpb) => mpb.SetVector(id, Value);
86+
}
87+
6088
public sealed class Props(int priority)
6189
{
6290
public readonly int Priority = priority;
@@ -79,39 +107,36 @@ public sealed class Props(int priority)
79107
internal IEnumerable<int> ManagedIds => _props.Keys;
80108

81109
[MethodImpl(MethodImplOptions.AggressiveInlining)]
82-
private void _internalSet<T>(int id, T value)
110+
private void _internalSet<T, TProp>(int id, T value) where TProp : Prop<T>
83111
{
84-
if (!_props.TryGetValue(id, out var prop)) {
85-
_props[id] = new Prop<T>(value);
86-
Changed = true;
87-
return;
88-
}
112+
if (_props.TryGetValue(id, out var prop)) {
113+
if (prop is TProp typedProp) {
114+
if (EqualityComparer<T>.Default.Equals(value, typedProp.Value)) return;
115+
116+
typedProp.Value = value;
117+
Changed = true;
118+
return;
119+
}
89120

90-
if (prop is not Prop<T> propT) {
91121
MaterialPropertyManager.Instance.LogWarning(
92122
$"property {PropIdToName.Get(id)} has mismatched type; overwriting with {typeof(T).Name}!");
93-
_props[id] = new Prop<T>(value);
94-
Changed = true;
95-
return;
96123
}
97124

98-
if (EqualityComparer<T>.Default.Equals(value, propT.Value)) return;
99-
100-
propT.Value = value;
125+
_props[id] = (TProp)Activator.CreateInstance(typeof(TProp), value);
101126
Changed = true;
102127
}
103128

104129
[MethodImpl(MethodImplOptions.AggressiveInlining)]
105-
public void SetColor(int id, Color value) => _internalSet<Color>(id, value);
130+
public void SetColor(int id, Color value) => _internalSet<Color, PropColor>(id, value);
106131

107132
[MethodImpl(MethodImplOptions.AggressiveInlining)]
108-
public void SetFloat(int id, float value) => _internalSet<float>(id, value);
133+
public void SetFloat(int id, float value) => _internalSet<float, PropFloat>(id, value);
109134

110135
[MethodImpl(MethodImplOptions.AggressiveInlining)]
111-
public void SetInt(int id, int value) => _internalSet<int>(id, value);
136+
public void SetInt(int id, int value) => _internalSet<int, PropInt>(id, value);
112137

113-
public void SetTexture(int id, Texture value) => _internalSet<Texture>(id, value);
114-
public void SetVector(int id, Vector4 value) => _internalSet<Vector4>(id, value);
138+
public void SetTexture(int id, Texture value) => _internalSet<Texture, PropTexture>(id, value);
139+
public void SetVector(int id, Vector4 value) => _internalSet<Vector4, PropVector>(id, value);
115140

116141
private bool _internalHas<T>(int id) => _props.TryGetValue(id, out var prop) && prop is Prop<T>;
117142

@@ -121,6 +146,7 @@ private void _internalSet<T>(int id, T value)
121146
public bool HasTexture(int id) => _internalHas<Texture>(id);
122147
public bool HasVector(int id) => _internalHas<Vector4>(id);
123148

149+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
124150
internal void Write(int id, MaterialPropertyBlock mpb)
125151
{
126152
if (!_props.TryGetValue(id, out var prop)) {
@@ -130,13 +156,7 @@ internal void Write(int id, MaterialPropertyBlock mpb)
130156
MaterialPropertyManager.Instance.LogDebug(
131157
$"writing property {PropIdToName.Get(id)} = {prop}");
132158

133-
switch (prop) {
134-
case Prop<Color> c: mpb.SetColor(id, c.Value); break;
135-
case Prop<float> f: mpb.SetFloat(id, f.Value); break;
136-
case Prop<int> i: mpb.SetInt(id, i.Value); break;
137-
case Prop<Texture> t: mpb.SetTexture(id, t.Value); break;
138-
case Prop<Vector4> v: mpb.SetVector(id, v.Value); break;
139-
}
159+
prop.Write(id, mpb);
140160
}
141161

142162
public override string ToString()

0 commit comments

Comments
 (0)