-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeldCableKindResolver.cs
More file actions
308 lines (269 loc) · 9.3 KB
/
HeldCableKindResolver.cs
File metadata and controls
308 lines (269 loc) · 9.3 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
using System;
using System.Reflection;
using Il2Cpp;
using UnityEngine;
namespace FMF.HexLabelMod;
/// <summary>
/// Resolves RJ / SFP / QSFP for the cable item the player is holding, using
/// reflection on <see cref="PlayerClass"/> and related types (names vary by build).
/// </summary>
internal static class HeldCableKindResolver
{
public static string Resolve()
{
try
{
var pc = PlayerManager.instance?.playerClass;
if (pc == null)
return null;
var t = pc.GetType();
foreach (var member in t.GetMembers(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
{
object val = null;
try
{
if (member is FieldInfo fi)
val = fi.GetValue(pc);
else if (member is PropertyInfo pi && pi.GetIndexParameters().Length == 0)
val = pi.GetValue(pc);
else
continue;
}
catch
{
continue;
}
if (val == null)
continue;
var kind = ClassifyObject(val);
if (kind != null)
return kind;
}
}
catch
{
// ignored
}
return null;
}
/// <summary>
/// Rack, cable reel (spinner), or patch cable in hand — returns a display label and hex when possible.
/// Labels include port (RJ/SFP/QSFP) for reels and Normal/Colored for racks when detected.
/// </summary>
public static bool TryGetHeldItemHex(out string hex, out string kindLabel)
{
hex = null;
kindLabel = null;
try
{
var pc = PlayerManager.instance?.playerClass;
if (pc == null)
return false;
foreach (var m in pc.GetType().GetMembers(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
{
object val = null;
try
{
if (m is FieldInfo fi)
val = fi.GetValue(pc);
else if (m is PropertyInfo pi && pi.GetIndexParameters().Length == 0)
val = pi.GetValue(pc);
else
continue;
}
catch
{
continue;
}
if (val == null)
continue;
if (TryGetHexFromHeldObject(val, out hex, out kindLabel))
return true;
}
}
catch
{
// ignored
}
var kind = Resolve();
if (TryGetHeldCableHex(out hex))
{
kindLabel = string.IsNullOrEmpty(kind)
? "Kabel"
: $"Kabel · {CablePortKindUtil.ToShortPortLabel(kind)}";
return true;
}
return false;
}
private static bool TryGetHexFromHeldObject(object val, out string hex, out string kindLabel)
{
hex = null;
kindLabel = null;
if (val is Rack rack && GameObjectColorHex.TryGetRackHex(rack, out hex))
{
var v = GameObjectKindResolver.GetRackVariantLabel(rack);
kindLabel = v != null ? $"Rack · {v}" : "Rack";
return true;
}
if (val is CableSpinner sp && GameObjectColorHex.TryGetSpinnerHex(sp, out hex))
{
var p = GameObjectKindResolver.GetSpinnerPortKind(sp);
var shortPort = p != null ? CablePortKindUtil.ToShortPortLabel(p) : null;
kindLabel = shortPort != null ? $"Kabelrolle · {shortPort}" : "Kabelrolle";
return true;
}
if (val is GameObject go)
{
var r = go.GetComponentInParent<Rack>();
if (r != null && GameObjectColorHex.TryGetRackHex(r, out hex))
{
var v = GameObjectKindResolver.GetRackVariantLabel(r);
kindLabel = v != null ? $"Rack · {v}" : "Rack";
return true;
}
var s = go.GetComponentInParent<CableSpinner>();
if (s != null && GameObjectColorHex.TryGetSpinnerHex(s, out hex))
{
var p = GameObjectKindResolver.GetSpinnerPortKind(s);
var shortPort = p != null ? CablePortKindUtil.ToShortPortLabel(p) : null;
kindLabel = shortPort != null ? $"Kabelrolle · {shortPort}" : "Kabelrolle";
return true;
}
}
if (val is Component c)
{
var r = c.GetComponentInParent<Rack>();
if (r != null && GameObjectColorHex.TryGetRackHex(r, out hex))
{
var v = GameObjectKindResolver.GetRackVariantLabel(r);
kindLabel = v != null ? $"Rack · {v}" : "Rack";
return true;
}
var s = c.GetComponentInParent<CableSpinner>();
if (s != null && GameObjectColorHex.TryGetSpinnerHex(s, out hex))
{
var p = GameObjectKindResolver.GetSpinnerPortKind(s);
var shortPort = p != null ? CablePortKindUtil.ToShortPortLabel(p) : null;
kindLabel = shortPort != null ? $"Kabelrolle · {shortPort}" : "Kabelrolle";
return true;
}
}
return false;
}
public static bool TryGetHeldCableHex(out string hex)
{
hex = null;
try
{
var pc = PlayerManager.instance?.playerClass;
if (pc == null)
return false;
foreach (var m in pc.GetType().GetMembers(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
{
object val = null;
try
{
if (m is FieldInfo fi)
val = fi.GetValue(pc);
else if (m is PropertyInfo pi && pi.GetIndexParameters().Length == 0)
val = pi.GetValue(pc);
else
continue;
}
catch
{
continue;
}
if (val == null)
continue;
var name = m.Name;
if (name.IndexOf("cable", StringComparison.OrdinalIgnoreCase) < 0
&& name.IndexOf("held", StringComparison.OrdinalIgnoreCase) < 0
&& name.IndexOf("item", StringComparison.OrdinalIgnoreCase) < 0
&& name.IndexOf("inventory", StringComparison.OrdinalIgnoreCase) < 0)
continue;
if (TryHexFromObject(val, out hex))
return true;
}
return TryHexFromObject(pc, out hex);
}
catch
{
return false;
}
}
private static bool TryHexFromObject(object o, out string hex)
{
hex = null;
if (o == null)
return false;
if (o is string str && HexColorUtil.TryNormalizeHex(str, out hex))
return true;
if (o is UnityEngine.Color c)
{
hex = HexColorUtil.ToHex(c);
return true;
}
var t = o.GetType();
foreach (var m in t.GetMembers(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
{
if (m.Name.IndexOf("rgb", StringComparison.OrdinalIgnoreCase) < 0
&& m.Name.IndexOf("color", StringComparison.OrdinalIgnoreCase) < 0)
continue;
object val = null;
try
{
if (m is FieldInfo fi)
val = fi.GetValue(o);
else if (m is PropertyInfo pi && pi.GetIndexParameters().Length == 0)
val = pi.GetValue(o);
else
continue;
}
catch
{
continue;
}
if (val is string s2 && HexColorUtil.TryNormalizeHex(s2, out hex))
return true;
}
return false;
}
private static string ClassifyObject(object o)
{
if (o is string s)
return CablePortKindUtil.ClassifyPortString(s);
var s2 = o.ToString();
if (!string.IsNullOrEmpty(s2))
{
var k = CablePortKindUtil.ClassifyPortString(s2);
if (k != null)
return k;
}
var t = o.GetType();
foreach (var member in t.GetMembers(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
{
object val = null;
try
{
if (member is FieldInfo fi)
val = fi.GetValue(o);
else if (member is PropertyInfo pi && pi.GetIndexParameters().Length == 0)
val = pi.GetValue(o);
else
continue;
}
catch
{
continue;
}
if (val is string str)
{
var k = CablePortKindUtil.ClassifyPortString(str);
if (k != null)
return k;
}
}
return null;
}
}