-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGameObjectInspector.cs
More file actions
1573 lines (1376 loc) · 65.1 KB
/
GameObjectInspector.cs
File metadata and controls
1573 lines (1376 loc) · 65.1 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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ==============================
// GameObjectInspector - Displays selected GameObject properties
// ==============================
using System;
using System.Collections.Generic;
using System.Numerics;
using GameSDK;
using GameSDK.ModHost;
namespace MDB.Explorer.ImGui
{
/// <summary>
/// Displays inspector panel for a selected GameObject.
/// </summary>
public class GameObjectInspector
{
private const string LOG_TAG = "GameObjectInspector";
// IL2CPP class resolution
private bool _classesResolved;
// Current target
private HierarchyNode _target;
private List<ComponentInfo> _components = new List<ComponentInfo>();
// Transform values
private Vector3 _position;
private Vector3 _rotation;
private Vector3 _scale;
private IntPtr _transformPtr;
// Drill-down inspection stack
private Stack<InspectionContext> _inspectionStack = new Stack<InspectionContext>();
// Track expanded arrays (key = "compIndex_fieldIndex")
private HashSet<string> _expandedArrays = new HashSet<string>();
// Cache for string field editing (key = field pointer, value = current edit string)
private Dictionary<IntPtr, string> _stringEditCache = new Dictionary<IntPtr, string>();
// Auto-size: set when target changes so the parent can resize the window
private bool _needsAutoSize;
private float _desiredWidth;
public HierarchyNode Target => _target;
/// <summary>
/// If true, the inspector wants the window to auto-resize. Consume with ConsumeAutoSizeRequest.
/// </summary>
public bool NeedsAutoSize => _needsAutoSize;
/// <summary>
/// The computed ideal width. Valid when NeedsAutoSize is true.
/// </summary>
public float DesiredWidth => _desiredWidth;
/// <summary>
/// Consume the auto-size request (resets the flag). Returns the desired width.
/// </summary>
public float ConsumeAutoSizeRequest()
{
_needsAutoSize = false;
return _desiredWidth;
}
/// <summary>
/// Initialize IL2CPP class pointers.
/// </summary>
public bool Initialize()
{
if (_classesResolved) return true;
try
{
_classesResolved = Il2CppHelpers.ResolveClasses();
return _classesResolved;
}
catch (Exception ex)
{
ModLogger.LogInternal(LOG_TAG, $"[ERROR] Initialize failed: {ex.Message}");
return false;
}
}
/// <summary>
/// Set the target GameObject to inspect.
/// </summary>
public void SetTarget(HierarchyNode node)
{
if (_target?.Pointer == node?.Pointer) return;
_target = node;
_components.Clear();
_inspectionStack.Clear(); // Clear drill-down when changing target
_stringEditCache.Clear(); // Clear string edit cache when changing target
if (node == null || !node.IsValid) return;
RefreshComponents();
RefreshTransform();
ComputeDesiredWidth();
}
/// <summary>
/// Compute the ideal window width based on the widest content in the current components.
/// </summary>
private void ComputeDesiredWidth()
{
// Base: padding + indent for tree nodes
float maxContentWidth = 200f; // minimum baseline
const float charW = 7.5f;
const float padding = 80f; // tree indent + margins + scrollbar
const float spacing = 30f; // gap between name and value columns
foreach (var comp in _components)
{
if (comp.ReflectionData == null) continue;
var data = comp.ReflectionData;
foreach (var field in data.Fields)
{
string typeName = GetSimpleTypeName(field.DisplayTypeName);
string fieldName = field.DisplayName ?? "(unnamed)";
// Estimate value width from the field's type name (shown as value for object/class types)
string valueTypeName = GetSimpleTypeName(field.DisplayTypeName);
float valueWidth = Math.Max(80f, valueTypeName.Length * charW + 40f);
float rowWidth = padding + (typeName.Length + fieldName.Length + 2) * charW + spacing + valueWidth;
if (rowWidth > maxContentWidth) maxContentWidth = rowWidth;
}
foreach (var prop in data.Properties)
{
string typeName = GetSimpleTypeName(prop.DisplayTypeName);
string propName = prop.DisplayName ?? "(unnamed)";
string valueTypeName = GetSimpleTypeName(prop.DisplayTypeName);
float valueWidth = Math.Max(80f, valueTypeName.Length * charW + 40f);
float rowWidth = padding + (typeName.Length + propName.Length + 2) * charW + spacing + valueWidth;
if (rowWidth > maxContentWidth) maxContentWidth = rowWidth;
}
}
// Clamp to reasonable bounds
_desiredWidth = Math.Max(350f, Math.Min(maxContentWidth, 1200f));
_needsAutoSize = true;
}
/// <summary>
/// Refresh the component list.
/// </summary>
public void RefreshComponents()
{
_components.Clear();
if (_target == null || !_target.IsValid) return;
if (!_classesResolved && !Initialize()) return;
try
{
// Use the native helper to get components
IntPtr result = Il2CppBridge.mdb_gameobject_get_components(_target.Pointer);
if (result == IntPtr.Zero)
{
ModLogger.LogInternal(LOG_TAG, "[WARN] mdb_gameobject_get_components returned null");
return;
}
int length = Il2CppBridge.mdb_array_length(result);
ModLogger.LogInternal(LOG_TAG, $"[INFO] Found {length} components");
for (int i = 0; i < length; i++)
{
IntPtr compPtr = Il2CppBridge.mdb_array_get_element(result, i);
if (compPtr == IntPtr.Zero) continue;
string typeName = Il2CppHelpers.GetComponentTypeName(compPtr);
// Get reflection data for this component
ComponentReflectionData reflectionData = ComponentReflector.GetReflectionData(compPtr);
_components.Add(new ComponentInfo
{
Pointer = compPtr,
TypeName = typeName ?? "<unknown>",
ReflectionData = reflectionData
});
}
ModLogger.LogInternal(LOG_TAG, $"[INFO] Loaded {_components.Count} components");
}
catch (Exception ex)
{
ModLogger.LogInternal(LOG_TAG, $"[ERROR] RefreshComponents failed: {ex.Message}");
}
}
/// <summary>
/// Refresh transform values.
/// </summary>
public void RefreshTransform()
{
if (_target == null || !_target.IsValid) return;
if (!_classesResolved && !Initialize()) return;
try
{
_transformPtr = Il2CppHelpers.GetTransform(_target.Pointer);
if (_transformPtr == IntPtr.Zero) return;
// Use native helpers that properly handle IL2CPP value type unboxing
float x, y, z;
if (Il2CppBridge.mdb_transform_get_local_position(_transformPtr, out x, out y, out z))
_position = new Vector3(x, y, z);
else
_position = Vector3.Zero;
if (Il2CppBridge.mdb_transform_get_local_euler_angles(_transformPtr, out x, out y, out z))
_rotation = new Vector3(x, y, z);
else
_rotation = Vector3.Zero;
if (Il2CppBridge.mdb_transform_get_local_scale(_transformPtr, out x, out y, out z))
_scale = new Vector3(x, y, z);
else
_scale = Vector3.One;
}
catch (Exception ex)
{
ModLogger.LogInternal(LOG_TAG, $"[ERROR] RefreshTransform failed: {ex.Message}");
}
}
/// <summary>
/// Draw the inspector panel using ImGui.
/// </summary>
public void Draw()
{
if (_target == null || !_target.IsValid)
{
ImGui.TextDisabled("No GameObject selected");
return;
}
// Header with name and active toggle
bool active = _target.IsActive;
if (ImGui.Checkbox("##active", ref active))
{
Il2CppHelpers.SetGameObjectActive(_target.Pointer, active);
_target.IsActive = active;
}
ImGui.SameLine();
ImGui.Text(_target.Name);
ImGui.Separator();
// Transform section - use unique ID to avoid conflicts with "Transform" component
if (ImGui.CollapsingHeader("Transform##inspector_transform", ImGuiTreeNodeFlags.DefaultOpen))
{
ImGui.Indent();
// Position
ImGui.Text("Position:");
ImGui.SameLine(LayoutConstants.TransformLabelIndent);
ImGui.SetNextItemWidth(-RIGHT_PADDING);
if (ImGui.DragFloat3("##pos", ref _position, 0.1f))
{
if (_transformPtr != IntPtr.Zero)
Il2CppBridge.mdb_transform_set_local_position(_transformPtr, _position.X, _position.Y, _position.Z);
}
// Rotation
ImGui.Text("Rotation:");
ImGui.SameLine(LayoutConstants.TransformLabelIndent);
ImGui.SetNextItemWidth(-RIGHT_PADDING);
if (ImGui.DragFloat3("##rot", ref _rotation, 1.0f))
{
if (_transformPtr != IntPtr.Zero)
Il2CppBridge.mdb_transform_set_local_euler_angles(_transformPtr, _rotation.X, _rotation.Y, _rotation.Z);
}
// Scale
ImGui.Text("Scale:");
ImGui.SameLine(LayoutConstants.TransformLabelIndent);
ImGui.SetNextItemWidth(-RIGHT_PADDING);
if (ImGui.DragFloat3("##scale", ref _scale, 0.01f))
{
if (_transformPtr != IntPtr.Zero)
Il2CppBridge.mdb_transform_set_local_scale(_transformPtr, _scale.X, _scale.Y, _scale.Z);
}
ImGui.Unindent();
}
// Components section
if (ImGui.CollapsingHeader($"Components ({_components.Count})##inspector_components", ImGuiTreeNodeFlags.DefaultOpen))
{
ImGui.Indent();
// If we have a drill-down stack, show that instead
if (_inspectionStack.Count > 0)
{
DrawInspectionStack();
}
else
{
// Normal component display
for (int i = 0; i < _components.Count; i++)
{
var comp = _components[i];
// Skip Transform as we already show it above
if (comp.TypeName == "Transform") continue;
// Use display name (deobfuscated if available) for header
string displayName = comp.DisplayTypeName;
// Show original obfuscated name in parentheses if different
if (displayName != comp.TypeName && DeobfuscationHelper.IsObfuscatedName(comp.TypeName))
{
displayName = $"{displayName} [{comp.TypeName}]";
}
// Use index suffix to ensure unique IDs for each component
ImGui.PushID(i);
if (ImGui.CollapsingHeader($"{displayName}##comp_{i}"))
{
ImGui.Indent();
DrawComponentMembers(comp, i);
ImGui.Unindent();
}
// Right-click context menu for component
if (ImGui.BeginPopupContextItem("comp_ctx"))
{
if (ImGui.MenuItem("Copy Type Name"))
ImGui.SetClipboardText(comp.TypeName);
if (comp.DisplayTypeName != comp.TypeName && ImGui.MenuItem("Copy Display Name"))
ImGui.SetClipboardText(comp.DisplayTypeName);
if (ImGui.MenuItem($"Copy Pointer: 0x{comp.Pointer.ToInt64():X}"))
ImGui.SetClipboardText($"0x{comp.Pointer.ToInt64():X}");
ImGui.EndPopup();
}
ImGui.PopID();
}
}
ImGui.Unindent();
}
}
/// <summary>
/// Draw the inspection stack for drill-down navigation.
/// Groups members by their declaring class in the inheritance hierarchy.
/// </summary>
private void DrawInspectionStack()
{
var current = _inspectionStack.Peek();
// Back button and breadcrumb - use display name
if (ImGui.Button("<< Back"))
{
_inspectionStack.Pop();
}
ImGui.SameLine();
ImGui.TextColored(Theme.Highlight, current.DisplayTypeName ?? "Object");
ImGui.Separator();
if (current.ReflectionData == null)
{
ImGui.TextDisabled("No reflection data");
return;
}
var data = current.ReflectionData;
const int drilldownCompIndex = 999;
// Fields - grouped by declaring class
if (data.Fields.Count > 0 && ImGui.TreeNode($"Fields ({data.Fields.Count})##drilldown_fields"))
{
int fieldIndex = 0;
foreach (var (className, displayClassName, fields) in data.GetFieldsByClass())
{
bool isInherited = className != data.ClassName;
if (isInherited)
{
ImGui.PushStyleColor(ImGuiCol.Text, Theme.InheritedClass);
bool classExpanded = ImGui.TreeNode($"[{displayClassName}]##drilldown_inherited_fields_{className}");
ImGui.PopStyleColor();
// Right-click context menu for inherited class
if (ImGui.BeginPopupContextItem($"inherited_class_ctx_fields_{className}"))
{
if (ImGui.MenuItem("Copy Class Name (Obfuscated)"))
ImGui.SetClipboardText(className);
if (displayClassName != className && ImGui.MenuItem("Copy Class Name (Display)"))
ImGui.SetClipboardText(displayClassName);
ImGui.EndPopup();
}
if (classExpanded)
{
foreach (var field in fields)
{
DrawField(current.Instance, field, drilldownCompIndex, fieldIndex++);
}
ImGui.TreePop();
}
else
{
fieldIndex += fields.Count;
}
}
else
{
foreach (var field in fields)
{
DrawField(current.Instance, field, drilldownCompIndex, fieldIndex++);
}
}
}
ImGui.TreePop();
}
// Properties - grouped by declaring class
if (data.Properties.Count > 0 && ImGui.TreeNode($"Properties ({data.Properties.Count})##drilldown_props"))
{
int propIndex = 0;
foreach (var (className, displayClassName, props) in data.GetPropertiesByClass())
{
bool isInherited = className != data.ClassName;
if (isInherited)
{
ImGui.PushStyleColor(ImGuiCol.Text, Theme.InheritedClass);
bool classExpanded = ImGui.TreeNode($"[{displayClassName}]##drilldown_inherited_props_{className}");
ImGui.PopStyleColor();
// Right-click context menu for inherited class
if (ImGui.BeginPopupContextItem($"inherited_class_ctx_props_{className}"))
{
if (ImGui.MenuItem("Copy Class Name (Obfuscated)"))
ImGui.SetClipboardText(className);
if (displayClassName != className && ImGui.MenuItem("Copy Class Name (Display)"))
ImGui.SetClipboardText(displayClassName);
ImGui.EndPopup();
}
if (classExpanded)
{
foreach (var prop in props)
{
DrawProperty(current.Instance, prop, drilldownCompIndex, propIndex++);
}
ImGui.TreePop();
}
else
{
propIndex += props.Count;
}
}
else
{
foreach (var prop in props)
{
DrawProperty(current.Instance, prop, drilldownCompIndex, propIndex++);
}
}
}
ImGui.TreePop();
}
// Methods - grouped by declaring class
if (data.Methods.Count > 0 && ImGui.TreeNode($"Methods ({data.Methods.Count})##drilldown_methods"))
{
int methodIndex = 0;
foreach (var (className, displayClassName, methods) in data.GetMethodsByClass())
{
bool isInherited = className != data.ClassName;
if (isInherited)
{
ImGui.PushStyleColor(ImGuiCol.Text, Theme.InheritedClass);
bool classExpanded = ImGui.TreeNode($"[{displayClassName}]##drilldown_inherited_methods_{className}");
ImGui.PopStyleColor();
// Right-click context menu for inherited class
if (ImGui.BeginPopupContextItem($"inherited_class_ctx_methods_{className}"))
{
if (ImGui.MenuItem("Copy Class Name (Obfuscated)"))
ImGui.SetClipboardText(className);
if (displayClassName != className && ImGui.MenuItem("Copy Class Name (Display)"))
ImGui.SetClipboardText(displayClassName);
ImGui.EndPopup();
}
if (classExpanded)
{
foreach (var method in methods)
{
DrawMethod(method, drilldownCompIndex, methodIndex++);
}
ImGui.TreePop();
}
else
{
methodIndex += methods.Count;
}
}
else
{
foreach (var method in methods)
{
DrawMethod(method, drilldownCompIndex, methodIndex++);
}
}
}
ImGui.TreePop();
}
}
/// <summary>
/// Draw component fields, properties, and methods.
/// Groups members by their declaring class in the inheritance hierarchy.
/// </summary>
private void DrawComponentMembers(ComponentInfo comp, int compIndex)
{
if (comp.ReflectionData == null)
{
ImGui.TextDisabled("No reflection data available");
return;
}
var data = comp.ReflectionData;
// Fields section - grouped by declaring class
if (data.Fields.Count > 0 && ImGui.TreeNode($"Fields ({data.Fields.Count})##fields_{compIndex}"))
{
int fieldIndex = 0;
foreach (var (className, displayClassName, fields) in data.GetFieldsByClass())
{
// Show class header for inherited classes
bool isInherited = className != data.ClassName;
if (isInherited)
{
// Inherited class header with distinct styling
ImGui.PushStyleColor(ImGuiCol.Text, Theme.InheritedClass);
bool classExpanded = ImGui.TreeNode($"[{displayClassName}]##inherited_fields_{className}_{compIndex}");
ImGui.PopStyleColor();
// Right-click context menu for inherited class
if (ImGui.BeginPopupContextItem($"inherited_class_ctx_fields_{className}_{compIndex}"))
{
if (ImGui.MenuItem("Copy Class Name (Obfuscated)"))
ImGui.SetClipboardText(className);
if (displayClassName != className && ImGui.MenuItem("Copy Class Name (Display)"))
ImGui.SetClipboardText(displayClassName);
ImGui.EndPopup();
}
if (classExpanded)
{
foreach (var field in fields)
{
DrawField(comp.Pointer, field, compIndex, fieldIndex++);
}
ImGui.TreePop();
}
else
{
fieldIndex += fields.Count; // Skip indices for collapsed fields
}
}
else
{
// Direct class members (no extra header)
foreach (var field in fields)
{
DrawField(comp.Pointer, field, compIndex, fieldIndex++);
}
}
}
ImGui.TreePop();
}
// Properties section - grouped by declaring class
if (data.Properties.Count > 0 && ImGui.TreeNode($"Properties ({data.Properties.Count})##props_{compIndex}"))
{
int propIndex = 0;
foreach (var (className, displayClassName, props) in data.GetPropertiesByClass())
{
bool isInherited = className != data.ClassName;
if (isInherited)
{
ImGui.PushStyleColor(ImGuiCol.Text, Theme.InheritedClass);
bool classExpanded = ImGui.TreeNode($"[{displayClassName}]##inherited_props_{className}_{compIndex}");
ImGui.PopStyleColor();
// Right-click context menu for inherited class
if (ImGui.BeginPopupContextItem($"inherited_class_ctx_props_{className}_{compIndex}"))
{
if (ImGui.MenuItem("Copy Class Name (Obfuscated)"))
ImGui.SetClipboardText(className);
if (displayClassName != className && ImGui.MenuItem("Copy Class Name (Display)"))
ImGui.SetClipboardText(displayClassName);
ImGui.EndPopup();
}
if (classExpanded)
{
foreach (var prop in props)
{
DrawProperty(comp.Pointer, prop, compIndex, propIndex++);
}
ImGui.TreePop();
}
else
{
propIndex += props.Count;
}
}
else
{
foreach (var prop in props)
{
DrawProperty(comp.Pointer, prop, compIndex, propIndex++);
}
}
}
ImGui.TreePop();
}
// Methods section - grouped by declaring class
if (data.Methods.Count > 0 && ImGui.TreeNode($"Methods ({data.Methods.Count})##methods_{compIndex}"))
{
int methodIndex = 0;
foreach (var (className, displayClassName, methods) in data.GetMethodsByClass())
{
bool isInherited = className != data.ClassName;
if (isInherited)
{
ImGui.PushStyleColor(ImGuiCol.Text, Theme.InheritedClass);
bool classExpanded = ImGui.TreeNode($"[{displayClassName}]##inherited_methods_{className}_{compIndex}");
ImGui.PopStyleColor();
// Right-click context menu for inherited class
if (ImGui.BeginPopupContextItem($"inherited_class_ctx_methods_{className}_{compIndex}"))
{
if (ImGui.MenuItem("Copy Class Name (Obfuscated)"))
ImGui.SetClipboardText(className);
if (displayClassName != className && ImGui.MenuItem("Copy Class Name (Display)"))
ImGui.SetClipboardText(displayClassName);
ImGui.EndPopup();
}
if (classExpanded)
{
foreach (var method in methods)
{
DrawMethod(method, compIndex, methodIndex++);
}
ImGui.TreePop();
}
else
{
methodIndex += methods.Count;
}
}
else
{
foreach (var method in methods)
{
DrawMethod(method, compIndex, methodIndex++);
}
}
}
ImGui.TreePop();
}
}
// Layout constants for inspector field rendering
private const float APPROX_CHAR_WIDTH = 7f;
private const float RIGHT_PADDING = 16f;
private const float CONTENT_MARGIN = 4f;
private const float EDIT_WIDGET_MAX = 140f;
private const float STRING_WIDGET_MAX = 160f;
/// <summary>
/// Compute dynamic character limits for type and name columns based on available width.
/// Allocates roughly 30% to type and 45% to name, leaving the rest for the value.
/// </summary>
private void ComputeColumnLimits(out int typeMaxChars, out int nameMaxChars)
{
float avail = ImGui.GetContentRegionAvailX();
// Type gets ~30%, name gets ~45%, value gets the remaining ~25%
float typeWidth = avail * 0.28f;
float nameWidth = avail * 0.40f;
typeMaxChars = Math.Max(6, (int)(typeWidth / APPROX_CHAR_WIDTH));
nameMaxChars = Math.Max(8, (int)(nameWidth / APPROX_CHAR_WIDTH));
}
/// <summary>
/// Position the cursor for right-aligned text. Never moves cursor backward (prevents overlap).
/// Call this after SameLine(), before drawing the text.
/// </summary>
private void RightAlignValue(string text)
{
float textWidth = ImGui.CalcTextSize(text).X;
// Use content region to respect indent/scrollbar, not raw window width
float rightEdge = ImGui.GetCursorPosX() + ImGui.GetContentRegionAvailX();
float cursorX = ImGui.GetCursorPosX();
float rightAligned = rightEdge - textWidth - RIGHT_PADDING;
// Only right-align if there's room; otherwise just flow naturally
if (rightAligned > cursorX)
ImGui.SetCursorPosX(rightAligned);
}
/// <summary>
/// Right-align a small widget (checkbox, small button) by reserving a fixed width from the right edge.
/// </summary>
private void RightAlignWidget(float widgetWidth)
{
float rightEdge = ImGui.GetCursorPosX() + ImGui.GetContentRegionAvailX();
float cursorX = ImGui.GetCursorPosX();
float rightAligned = rightEdge - widgetWidth - RIGHT_PADDING;
if (rightAligned > cursorX)
ImGui.SetCursorPosX(rightAligned);
}
/// <summary>
/// Draw a field with type display, value, and drill-down support.
/// Supports editing for simple types (int, float, bool, string).
/// Supports expanding arrays/lists.
/// </summary>
private void DrawField(IntPtr instance, FieldInfo field, int compIndex, int fieldIndex)
{
ImGui.PushID(fieldIndex);
// Use deobfuscated type name if available
string typeName = GetSimpleTypeName(field.DisplayTypeName);
bool isObjectType = field.TypeEnum == Il2CppBridge.Il2CppTypeEnum.IL2CPP_TYPE_CLASS ||
field.TypeEnum == Il2CppBridge.Il2CppTypeEnum.IL2CPP_TYPE_OBJECT;
bool isArrayType = field.TypeEnum == Il2CppBridge.Il2CppTypeEnum.IL2CPP_TYPE_SZARRAY ||
field.TypeEnum == Il2CppBridge.Il2CppTypeEnum.IL2CPP_TYPE_ARRAY;
// Dynamic column sizing based on available width
ComputeColumnLimits(out int typeMaxChars, out int nameMaxChars);
// Type name in color
string displayType = TruncateText(typeName, typeMaxChars);
ImGui.TextColored(Theme.TypeName, displayType);
if (displayType != typeName && ImGui.IsItemHovered())
ImGui.SetTooltip(typeName);
ImGui.SameLine();
// Field name - use deobfuscated name
string fieldName = field.DisplayName ?? "(unnamed)";
string displayName = TruncateText(fieldName, nameMaxChars);
// For arrays, use a TreeNode so it can be expanded
if (isArrayType && instance != IntPtr.Zero)
{
// Get array info — works for both static and instance
IntPtr arrPtr = field.IsStatic
? ReadStaticObjectPtr(field.Pointer)
: GetArrayPointer(instance, field);
int arrLength = arrPtr != IntPtr.Zero ? Il2CppBridge.mdb_array_length(arrPtr) : 0;
bool isExpanded = ImGui.TreeNode($"{displayName} [{arrLength}]##arr_{fieldIndex}");
if (displayName != fieldName && ImGui.IsItemHovered())
ImGui.SetTooltip(fieldName);
if (isExpanded)
{
DrawArrayElements(arrPtr, arrLength, field);
ImGui.TreePop();
}
}
else
{
ImGui.Text(displayName);
if (displayName != fieldName && ImGui.IsItemHovered())
ImGui.SetTooltip(fieldName);
// For object types, add drill-down button
if (isObjectType)
{
ImGui.SameLine();
if (ImGui.SmallButton($">##drill_{fieldIndex}"))
{
DrillIntoField(instance, field, field.IsStatic);
}
}
// Value column - auto-flow after name
ImGui.SameLine();
// Pre-read value for read-only displays
string preReadValue = null;
bool isEditable = !field.IsStatic && instance != IntPtr.Zero && IsEditableType(field.TypeEnum);
if (!isEditable)
{
try
{
object val = field.IsStatic
? ComponentReflector.ReadStaticFieldValue(field)
: (instance != IntPtr.Zero ? ComponentReflector.ReadFieldValue(instance, field) : null);
preReadValue = FormatValue(val);
}
catch { preReadValue = "(error)"; }
}
// Read and display/edit the field value (static or instance)
if (field.IsStatic)
{
DrawReadOnlyValue(preReadValue);
}
else if (instance != IntPtr.Zero)
{
if (isEditable)
{
DrawFieldValue(instance, field, fieldIndex);
}
else
{
DrawReadOnlyValue(preReadValue);
}
}
else
{
ImGui.TextDisabled("(N/A)");
}
// Right-click context menu for field
if (ImGui.BeginPopupContextItem("field_ctx"))
{
if (ImGui.MenuItem("Copy Field Name"))
ImGui.SetClipboardText(field.Name);
if (field.DisplayName != field.Name && ImGui.MenuItem("Copy Display Name"))
ImGui.SetClipboardText(field.DisplayName);
if (ImGui.MenuItem("Copy Type"))
ImGui.SetClipboardText(field.TypeName);
if (field.DisplayTypeName != field.TypeName && ImGui.MenuItem("Copy Display Type"))
ImGui.SetClipboardText(field.DisplayTypeName);
// Try to get and copy value
try
{
object val = field.IsStatic
? ComponentReflector.ReadStaticFieldValue(field)
: (instance != IntPtr.Zero ? ComponentReflector.ReadFieldValue(instance, field) : null);
if (val != null)
{
string valStr = FormatValue(val);
if (ImGui.MenuItem($"Copy Value: {TruncateText(valStr, 30)}"))
ImGui.SetClipboardText(valStr);
}
}
catch { }
ImGui.EndPopup();
}
}
ImGui.PopID();
}
/// <summary>
/// Draw field value with editing support for simple types.
/// </summary>
private void DrawFieldValue(IntPtr instance, FieldInfo field, int fieldIndex)
{
try
{
switch (field.TypeEnum)
{
case Il2CppBridge.Il2CppTypeEnum.IL2CPP_TYPE_BOOLEAN:
{
object val = ComponentReflector.ReadFieldValue(instance, field);
bool boolVal = val is bool b ? b : false;
// Right-align the checkbox (approx 20px wide)
RightAlignWidget(20f);
if (ImGui.Checkbox($"##val_{fieldIndex}", ref boolVal))
{
ComponentReflector.WriteFieldValue(instance, field, boolVal);
}
}
break;
case Il2CppBridge.Il2CppTypeEnum.IL2CPP_TYPE_I4:
{
object val = ComponentReflector.ReadFieldValue(instance, field);
int intVal = val is int i ? i : 0;
// Cap widget width, right-aligned
float avail = ImGui.GetContentRegionAvailX();
float w = Math.Min(EDIT_WIDGET_MAX, avail - RIGHT_PADDING);
RightAlignWidget(w);
ImGui.SetNextItemWidth(w);
if (ImGui.DragInt($"##val_{fieldIndex}", ref intVal, 1.0f))
{
ComponentReflector.WriteFieldValue(instance, field, intVal);
}
}
break;
case Il2CppBridge.Il2CppTypeEnum.IL2CPP_TYPE_R4:
{
object val = ComponentReflector.ReadFieldValue(instance, field);
float floatVal = val is float f ? f : 0f;
float avail2 = ImGui.GetContentRegionAvailX();
float w2 = Math.Min(EDIT_WIDGET_MAX, avail2 - RIGHT_PADDING);
RightAlignWidget(w2);
ImGui.SetNextItemWidth(w2);
if (ImGui.DragFloat($"##val_{fieldIndex}", ref floatVal, 0.1f))
{
ComponentReflector.WriteFieldValue(instance, field, floatVal);
}
}
break;
case Il2CppBridge.Il2CppTypeEnum.IL2CPP_TYPE_STRING:
{
// Always read current value from field
object currentVal = ComponentReflector.ReadFieldValue(instance, field);
string currentStr = currentVal as string ?? "";
// Use cache for editing, initialize from current value if not in cache
if (!_stringEditCache.TryGetValue(field.Pointer, out string editStr))
{
editStr = currentStr;
_stringEditCache[field.Pointer] = editStr;
}
// Cap string input width, leave room for Set button
float availStr = ImGui.GetContentRegionAvailX();
float strW = Math.Min(STRING_WIDGET_MAX, availStr - 50f);
RightAlignWidget(strW + 40f); // account for Set button
ImGui.SetNextItemWidth(strW);
if (ImGui.InputText($"##val_{fieldIndex}", ref editStr, 256))
{
_stringEditCache[field.Pointer] = editStr;
}
// Apply button to commit the string change
ImGui.SameLine();
if (ImGui.SmallButton("Set"))
{
// Get the current cached value to write
string valueToWrite = _stringEditCache.TryGetValue(field.Pointer, out string cached) ? cached : editStr;
// Create new IL2CPP string and set the field
IntPtr newStr = Il2CppBridge.mdb_string_new(valueToWrite);
if (newStr != IntPtr.Zero)
{
bool result = WriteStringField(instance, field.Pointer, newStr);
// Clear cache so next frame reads the new value
_stringEditCache.Remove(field.Pointer);
}
}
// Show if value differs from field value
if (_stringEditCache.TryGetValue(field.Pointer, out string cachedVal) && cachedVal != currentStr)
{
ImGui.SameLine();
ImGui.TextColored(Theme.Highlight, "*");
}
}
break;
case Il2CppBridge.Il2CppTypeEnum.IL2CPP_TYPE_ENUM:
{
object val = ComponentReflector.ReadFieldValue(instance, field);
int enumVal = val is int i ? i : 0;
float avail3 = ImGui.GetContentRegionAvailX();
float w3 = Math.Min(EDIT_WIDGET_MAX, avail3 - RIGHT_PADDING);
RightAlignWidget(w3);
ImGui.SetNextItemWidth(w3);
if (ImGui.DragInt($"##val_{fieldIndex}", ref enumVal, 1.0f))
{
ComponentReflector.WriteFieldValue(instance, field, enumVal);
}
}
break;
default:
{
object value = ComponentReflector.ReadFieldValue(instance, field);
string valueStr = FormatValue(value);