-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathCustomInspectorInjector.cs
More file actions
223 lines (192 loc) · 9.66 KB
/
CustomInspectorInjector.cs
File metadata and controls
223 lines (192 loc) · 9.66 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
using Elements.Core;
using FrooxEngine;
using FrooxEngine.UIX;
using HarmonyLib;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
namespace MonkeyLoader.Resonite.UI.Inspectors
{
[HarmonyPatchCategory(nameof(CustomInspectorInjector))]
[HarmonyPatch(typeof(WorkerInspector), nameof(WorkerInspector.BuildUIForComponent))]
internal sealed class CustomInspectorInjector
: ResoniteEventSourceMonkey<CustomInspectorInjector,
BuildInspectorHeaderEvent, ResolveInspectorHeaderTextEvent, BuildInspectorBodyEvent>
{
public override bool CanBeDisabled => true;
private static MethodInfo _buildHeaderMethod = AccessTools.Method(typeof(CustomInspectorInjector), nameof(OnBuildInspectorHeader));
private static MethodInfo _buildHeaderTextMethod = AccessTools.Method(typeof(CustomInspectorInjector), nameof(OnBuildInspectorHeaderText));
private static MethodInfo _buildBodyMethod = AccessTools.Method(typeof(CustomInspectorInjector), nameof(OnBuildInspectorBody));
private static MethodInfo _storeVerticalLayoutMethod = AccessTools.Method(typeof(CustomInspectorInjector), nameof(StoreVerticalLayout));
private static VerticalLayout _verticalLayout;
private static void StoreVerticalLayout(VerticalLayout layout)
{
_verticalLayout = layout;
}
[HarmonyTranspiler]
static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
{
bool storedVerticalLayout = false;
Label? headerLabel = null;
Label? headerTextLabel = null;
bool headerDone = false;
bool headerTextDone = false;
bool bodyDone = false;
bool afterHeaderBranch = false;
bool afterHeaderTextBranch = false;
int numBranches = 0;
int branchDepth = 0;
List<Label?> labels = new();
foreach (var instruction in instructions)
{
bool didBranch = false;
if (instruction.Branches(out var label))
{
if (numBranches == 0)
headerLabel = label;
else if (numBranches == 6)
headerTextLabel = label;
numBranches++;
branchDepth++;
didBranch = true;
labels.Add(label);
}
if (!didBranch)
{
if (branchDepth > 0)
{
foreach (var storedLabel in labels.ToArray())
{
if (instruction.labels.Contains(storedLabel!.Value) && instruction.operand != (object)storedLabel!.Value)
{
if (storedLabel == headerLabel)
afterHeaderBranch = true;
else if (storedLabel == headerTextLabel)
afterHeaderTextBranch = true;
labels.Remove(storedLabel);
branchDepth--;
break;
}
}
}
if (numBranches == 1 && !headerDone)
{
// do header
yield return new CodeInstruction(OpCodes.Ldloc_0);
yield return new CodeInstruction(OpCodes.Ldarg, 0);
yield return new CodeInstruction(OpCodes.Ldarg, 1);
yield return new CodeInstruction(OpCodes.Ldarg, 2);
yield return new CodeInstruction(OpCodes.Ldarg, 3);
yield return new CodeInstruction(OpCodes.Ldarg, 4);
yield return new CodeInstruction(OpCodes.Ldarg, 5);
yield return new CodeInstruction(OpCodes.Call, _buildHeaderMethod);
headerDone = true;
}
if (numBranches == 7 && !headerTextDone)
{
// do header text
yield return new CodeInstruction(OpCodes.Ldloc_0);
yield return new CodeInstruction(OpCodes.Ldarg, 1);
yield return new CodeInstruction(OpCodes.Call, _buildHeaderTextMethod);
headerTextDone = true;
}
}
if (headerDone && !afterHeaderBranch) continue;
if (headerTextDone && !afterHeaderTextBranch) continue;
yield return instruction;
if (!storedVerticalLayout && instruction.Calls(AccessTools.Method(typeof(UIBuilder), nameof(UIBuilder.VerticalLayout), [typeof(float), typeof(float), typeof(Alignment?), typeof(bool?), typeof(bool?)])))
{
yield return new CodeInstruction(OpCodes.Dup);
yield return new CodeInstruction(OpCodes.Call, _storeVerticalLayoutMethod);
storedVerticalLayout = true;
}
if (!bodyDone && instruction.Calls(AccessTools.Method(typeof(WorkerInspector), nameof(WorkerInspector.BuildInspectorUI))))
{
// do body
yield return new CodeInstruction(OpCodes.Ldloc_0);
yield return new CodeInstruction(OpCodes.Ldarg, 0);
yield return new CodeInstruction(OpCodes.Ldarg, 1);
yield return new CodeInstruction(OpCodes.Ldarg, 2);
yield return new CodeInstruction(OpCodes.Ldarg, 3);
yield return new CodeInstruction(OpCodes.Ldarg, 4);
yield return new CodeInstruction(OpCodes.Ldarg, 5);
yield return new CodeInstruction(OpCodes.Call, _buildBodyMethod);
bodyDone = true;
}
}
}
private static void OnBuildInspectorBody(UIBuilder ui, WorkerInspector inspector, Worker worker,
bool allowDestroy, bool allowDuplicate, bool allowContainer, Predicate<ISyncMember> memberFilter)
{
var root = ui.Root;
var eventData = new BuildInspectorBodyEvent(ui, inspector, worker, allowContainer, allowDuplicate, allowDestroy, memberFilter);
Dispatch(eventData);
ui.NestInto(root);
}
private static void OnBuildInspectorHeader(UIBuilder ui, WorkerInspector inspector, Worker worker,
bool allowDestroy, bool allowDuplicate, bool allowContainer, Predicate<ISyncMember> memberFilter)
{
ui.Style.MinHeight = 32f;
ui.HorizontalLayout(4f);
ui.Style.MinHeight = 24f;
ui.Style.FlexibleWidth = 1000f;
var root = ui.Root;
var eventData = new BuildInspectorHeaderEvent(ui, inspector, worker, allowContainer, allowDuplicate, allowDestroy, memberFilter);
Dispatch(eventData);
ui.NestInto(root);
ui.NestInto(_verticalLayout.Slot);
}
private static void OnBuildInspectorHeaderText(UIBuilder ui, Worker worker)
{
var eventData = new ResolveInspectorHeaderTextEvent(worker);
Dispatch(eventData);
if (eventData.ItemCount is 0)
return;
// The expander code is based on SlotInspector.OnChanges
var root = ui.Root;
ui.PushStyle();
ui.Style.MinHeight = 32;
ui.Style.ForceExpandHeight = false;
ui.Style.ChildAlignment = Alignment.TopLeft;
var headerLayout = ui.HorizontalLayout(4);
var headerButton = headerLayout.Slot.AttachComponent<Button>();
var expander = headerLayout.Slot.AttachComponent<Expander>();
var expanderIndicator = headerLayout.Slot.AttachComponent<TextExpandIndicator>();
ui.Style.MinWidth = 32;
ui.Style.FlexibleWidth = -1;
var expanderButton = ui.Button();
expanderButton.ColorDrivers[0].MoveTo(headerButton);
expanderIndicator.Text.Target = expanderButton.Slot.GetComponentInChildren<Text>().Content;
expanderButton.Destroy();
ui.Style.FlexibleWidth = 1;
var label = ui.Text(Mod.GetLocaleString("Inspector.HeaderTextLabel"), alignment: Alignment.MiddleLeft);
var labelColorDriver = headerButton.ColorDrivers.Add();
labelColorDriver.ColorDrive.Target = label.Color;
RadiantUI_Constants.SetupLabelDriverColors(labelColorDriver);
ui.NestOut();
ui.Style.FlexibleWidth = -1;
ui.Style.MinHeight = -1;
var childrenLayout = ui.HorizontalLayout(4);
expander.SectionRoot.Target = childrenLayout.Slot;
expanderIndicator.SectionRoot.Target = childrenLayout.Slot;
DefaultInspectorHeaderConfig.Instance.StartHeaderTextExpanded.DriveWithLocalOverride(childrenLayout.Slot.ActiveSelf_Field);
ui.Style.MinWidth = 32;
ui.Empty("Spacer");
ui.Style.FlexibleWidth = 1;
var textLayout = ui.VerticalLayout(6);
expanderIndicator.ChildrenRoot.Target = textLayout.Slot;
ui.PopStyle();
ui.PushStyle();
foreach (var headerText in eventData.Items)
{
ui.Style.MinHeight = headerText.MinHeight;
ui.Text(headerText.Text, alignment: Alignment.TopLeft);
}
ui.PopStyle();
ui.NestInto(root);
}
}
}