Skip to content

Commit 014c6a7

Browse files
committed
a best-effort attempt at recovering property names for diagnostics
1 parent a3f8e13 commit 014c6a7

File tree

1 file changed

+49
-7
lines changed

1 file changed

+49
-7
lines changed

Source/DynamicProperties/Props.cs

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,53 @@
11
using System.Collections.Generic;
2+
using System.Linq;
23
using System.Runtime.CompilerServices;
34
using KSPBuildTools;
45
using UnityEngine;
56

67
namespace Shabby;
78

9+
internal static class PropIdToName
10+
{
11+
private static readonly string[] CommonProperties = [
12+
"TransparentFX",
13+
"_BumpMap",
14+
"_Color",
15+
"_EmissiveColor",
16+
"_MainTex",
17+
"_MaxX",
18+
"_MaxY",
19+
"_MinX",
20+
"_MinY",
21+
"_Multiplier",
22+
"_Opacity",
23+
"_RimColor",
24+
"_RimFalloff",
25+
"_TC1Color",
26+
"_TC1MetalBlend",
27+
"_TC1Metalness",
28+
"_TC1SmoothBlend",
29+
"_TC1Smoothness",
30+
"_TC2Color",
31+
"_TC2MetalBlend",
32+
"_TC2Metalness",
33+
"_TC2SmoothBlend",
34+
"_TC2Smoothness",
35+
"_TemperatureColor",
36+
"_Tex",
37+
"_Tint",
38+
"_TintColor",
39+
"_subdiv",
40+
"localMatrix",
41+
"upMatrix"
42+
];
43+
44+
private static readonly Dictionary<int, string> IdToName =
45+
CommonProperties.ToDictionary(Shader.PropertyToID, name => name);
46+
47+
internal static string Get(int id) =>
48+
IdToName.TryGetValue(id, out var name) ? name : $"<{id}>";
49+
}
50+
851
internal abstract class Prop;
952

1053
internal class Prop<T>(T value) : Prop
@@ -38,10 +81,6 @@ public sealed class Props(int priority)
3881
[MethodImpl(MethodImplOptions.AggressiveInlining)]
3982
private void _internalSet<T>(int id, T value)
4083
{
41-
Dirty = true;
42-
43-
MaterialPropertyManager.Instance.LogDebug($"setting {id} to {value}");
44-
4584
if (!_props.TryGetValue(id, out var prop)) {
4685
_props[id] = new Prop<T>(value);
4786
Changed = true;
@@ -50,7 +89,7 @@ private void _internalSet<T>(int id, T value)
5089

5190
if (prop is not Prop<T> propT) {
5291
MaterialPropertyManager.Instance.LogWarning(
53-
$"property {id} has mismatched type; overwriting with {typeof(T).Name}!");
92+
$"property {PropIdToName.Get(id)} has mismatched type; overwriting with {typeof(T).Name}!");
5493
_props[id] = new Prop<T>(value);
5594
Changed = true;
5695
return;
@@ -85,9 +124,12 @@ private void _internalSet<T>(int id, T value)
85124
internal void Write(int id, MaterialPropertyBlock mpb)
86125
{
87126
if (!_props.TryGetValue(id, out var prop)) {
88-
throw new KeyNotFoundException($"property {id} not found");
127+
throw new KeyNotFoundException($"property {PropIdToName.Get(id)} not found");
89128
}
90129

130+
MaterialPropertyManager.Instance.LogDebug(
131+
$"writing property {PropIdToName.Get(id)} = {prop}");
132+
91133
switch (prop) {
92134
case Prop<Color> c: mpb.SetColor(id, c.Value); break;
93135
case Prop<float> f: mpb.SetFloat(id, f.Value); break;
@@ -102,7 +144,7 @@ public override string ToString()
102144
var sb = StringBuilderCache.Acquire();
103145
sb.AppendFormat("(Priority {0}) {{\n", Priority);
104146
foreach (var (id, prop) in _props) {
105-
sb.AppendFormat("{0} = {1}\n", id, prop);
147+
sb.AppendFormat("{0} = {1}\n", PropIdToName.Get(id), prop);
106148
}
107149

108150
sb.AppendLine("}");

0 commit comments

Comments
 (0)