Skip to content

Commit 0b53f7f

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

File tree

2 files changed

+70
-7
lines changed

2 files changed

+70
-7
lines changed

Source/DynamicProperties/Patches/PartPatch.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections.Generic;
22
using HarmonyLib;
3+
using UnityEngine;
34

45
namespace Shabby;
56

@@ -22,6 +23,7 @@ private static void CreateRendererLists_Postfix(Part __instance)
2223
var props = highlightProperties[__instance];
2324
props.SetFloat(PropertyIDs._RimFalloff, 2f);
2425
props.SetColor(PropertyIDs._RimColor, Part.defaultHighlightNone);
26+
props.SetColor(PropertyIDs._Color, Color.red);
2527
foreach (var renderer in __instance.HighlightRenderer) {
2628
MaterialPropertyManager.Instance.Set(renderer, props);
2729
}
@@ -37,6 +39,25 @@ private static bool SetOpacity_Prefix(Part __instance, float opacity)
3739
return false;
3840
}
3941

42+
// [HarmonyTranspiler]
43+
// [HarmonyPatch(nameof(Part.Highlight))]
44+
// private static IEnumerable<CodeInstruction> Highlight_Transpiler(
45+
// IEnumerable<CodeInstruction> insns)
46+
// {
47+
// var Renderer_SetPropertyBlock =
48+
// AccessTools.Method(typeof(Renderer), nameof(Renderer.SetPropertyBlock));
49+
//
50+
// var matcher = new CodeMatcher(insns);
51+
// matcher.MatchStartForward(
52+
// Code.Ldarg_0,
53+
// new CodeMatch(CodeInstruction.LoadField(typeof(Part), nameof(Part.highlightRenderer))),
54+
// Code.Ldloc,
55+
// Code.Call,
56+
//
57+
// );
58+
// return matcher.Instructions();
59+
// }
60+
4061
[HarmonyPostfix]
4162
[HarmonyPatch("OnDestroy")]
4263
private static void OnDestroy_Postfix(Part __instance)

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)