-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoveControllerBridge.cs
More file actions
359 lines (321 loc) · 13.6 KB
/
Copy pathMoveControllerBridge.cs
File metadata and controls
359 lines (321 loc) · 13.6 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
using System;
using System.Reflection;
using BepInEx.Logging;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace MoveControllerAxisSplit
{
internal sealed class ButtonTarget
{
internal Button MoveXZButton;
}
internal sealed class MoveControllerBridge
{
private const BindingFlags AllStatic = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static;
private const BindingFlags AllInstance = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
private readonly ManualLogSource _logger;
private readonly Func<bool> _invertZ;
private Assembly _assembly;
private Type _pluginType;
private Type _windowType;
private Type _dragActionType;
private Type _buttonManagerType;
private Type _moveServiceType;
private Type _undoServiceType;
private Type _accessoryServiceType;
private FieldInfo _windowField;
private FieldInfo _guiField;
private FieldInfo _allSelectedField;
private PropertyInfo _ikSelectedProperty;
private MethodInfo _dragButtonMethod;
private MethodInfo _moveXZFactoryMethod;
private MethodInfo _isAccessoryControlMethod;
private MethodInfo _initUndoAccessoryMoveMethod;
private MethodInfo _moveAccessoryMethod;
private MethodInfo _createUndoAccessoryMoveMethod;
private MethodInfo _checkIfIkSelectedMethod;
private MethodInfo _moveIkMethod;
private MethodInfo _moveObjMethod;
private MethodInfo _resizeObjMethod;
private MethodInfo _storeOldSizesMethod;
private MethodInfo _storeOldIkPositionMethod;
private MethodInfo _storeOldPositionsMethod;
private MethodInfo _createUndoForResizeMethod;
private MethodInfo _createUndoForIkMoveMethod;
private MethodInfo _createUndoForMoveMethod;
private bool _loggedConnection;
internal MoveControllerBridge(ManualLogSource logger, Func<bool> invertZ)
{
_logger = logger;
_invertZ = invertZ;
}
internal bool TryResolveTarget(out ButtonTarget target)
{
target = null;
if (!TryInitialize())
return false;
object window;
Canvas canvas;
try
{
window = _windowField.GetValue(null);
if (window == null)
return false;
canvas = _guiField.GetValue(window) as Canvas;
}
catch (Exception ex)
{
LogError("Could not read the Move Controller window", ex);
return false;
}
if (canvas == null)
return false;
Transform transform = canvas.transform.Find("MovePanel/MoveXZ");
if (transform == null)
return false;
Button button = transform.GetComponent<Button>();
if (button == null)
return false;
target = new ButtonTarget();
target.MoveXZButton = button;
return true;
}
internal bool BindAxisButton(Button button, Axis axis)
{
try
{
object action = Activator.CreateInstance(_dragActionType);
SetAction(action, "StartDrag", delegate(BaseEventData data) { SafeEvent("start drag", delegate { StartDrag(data); }); });
SetAction(action, "Drag", delegate(BaseEventData data) { SafeEvent("drag", delegate { Drag(axis, data); }); });
SetAction(action, "EndDrag", delegate(BaseEventData data) { SafeEvent("end drag", delegate { EndDrag(data); }); });
_dragButtonMethod.Invoke(null, new object[] { button, action });
return true;
}
catch (Exception ex)
{
LogError("Could not bind the " + axis + " control", ex);
return false;
}
}
internal bool BindOriginalXZ(Button button)
{
try
{
object action = _moveXZFactoryMethod.Invoke(null,
new object[] { new Vector3(1f, 0f, 1f) });
_dragButtonMethod.Invoke(null, new object[] { button, action });
return true;
}
catch (Exception ex)
{
LogError("Could not restore the original XZ control", ex);
return false;
}
}
private bool TryInitialize()
{
if (_assembly != null)
return true;
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
for (int i = 0; i < assemblies.Length; i++)
{
if (string.Equals(assemblies[i].GetName().Name, "MoveControllerKOI", StringComparison.OrdinalIgnoreCase))
{
_assembly = assemblies[i];
break;
}
}
if (_assembly == null)
return false;
try
{
_pluginType = RequireType("MoveController.MoveCtrlPlugin");
_windowType = RequireType("MoveController.MoveCtrlWindow");
_dragActionType = RequireType("MoveController.DragButtonAction");
_buttonManagerType = RequireType("MoveController.ButtonManager");
_moveServiceType = RequireType("MoveController.MoveObjectService");
_undoServiceType = RequireType("MoveController.UndoRedoService");
_accessoryServiceType = RequireType("MoveController.AccessoryCtrlService");
Type buttonActionManagerType = RequireType("MoveController.ButtonActionManager");
_windowField = RequireField(_pluginType, "window", AllStatic);
_guiField = RequireField(_windowType, "GUI", AllInstance);
_allSelectedField = RequireField(_windowType, "AllSelected", AllInstance);
_ikSelectedProperty = RequireProperty(_moveServiceType, "IkSelected", AllStatic);
_dragButtonMethod = RequireMethod(_buttonManagerType, "DragButton", 2);
_moveXZFactoryMethod = RequireMethod(buttonActionManagerType, "MoveXZ", 1);
_isAccessoryControlMethod = RequireMethod(_accessoryServiceType, "IsAccessoryControl", 0);
_initUndoAccessoryMoveMethod = RequireMethod(_accessoryServiceType, "InitUndoMove", 0);
_moveAccessoryMethod = RequireMethod(_accessoryServiceType, "MoveAccessory", 1);
_createUndoAccessoryMoveMethod = RequireMethod(_accessoryServiceType, "CreateUndoMove", 0);
_checkIfIkSelectedMethod = RequireMethod(_moveServiceType, "CheckIfIkSelected", 0);
_moveIkMethod = RequireMethod(_moveServiceType, "MoveIk", 1);
_moveObjMethod = RequireMethod(_moveServiceType, "MoveObj", 2);
_resizeObjMethod = RequireMethod(_moveServiceType, "resizeObj", 2);
_storeOldSizesMethod = RequireMethod(_undoServiceType, "StoreOldSizes", 1);
_storeOldIkPositionMethod = RequireMethod(_undoServiceType, "StoreOldIkPosition", 0);
_storeOldPositionsMethod = RequireMethod(_undoServiceType, "StoreOldPositions", 1);
_createUndoForResizeMethod = RequireMethod(_undoServiceType, "CreateUndoForResize", 1);
_createUndoForIkMoveMethod = RequireMethod(_undoServiceType, "CreateUndoForIkMove", 0);
_createUndoForMoveMethod = RequireMethod(_undoServiceType, "CreateUndoForMove", 1);
if (!_loggedConnection)
{
Version version = _assembly.GetName().Version;
_logger.LogInfo("Connected to MoveControllerKOI " + version + ".");
if (version == null || version.Major != 1 || version.Minor != 7 || version.Build != 1 ||
(version.Revision != 1 && version.Revision != 2))
{
_logger.LogWarning("This MoveControllerKOI version was not explicitly verified; attempting compatible attachment.");
}
_loggedConnection = true;
}
return true;
}
catch (Exception ex)
{
LogError("MoveControllerKOI has an incompatible structure", ex);
_assembly = null;
return false;
}
}
private void StartDrag(BaseEventData data)
{
if (IsAccessoryControl())
{
Invoke(_initUndoAccessoryMoveMethod);
}
else if (IsRightButton(data))
{
Invoke(_storeOldSizesMethod, GetAllSelected());
}
else if ((bool)Invoke(_checkIfIkSelectedMethod))
{
Invoke(_storeOldIkPositionMethod);
}
else
{
Invoke(_storeOldPositionsMethod, GetAllSelected());
}
}
private void Drag(Axis axis, BaseEventData data)
{
float input = Input.GetAxis("Mouse X");
if (IsRightButton(data))
{
Invoke(_resizeObjMethod, GetAllSelected(), new Vector3(input, input, input));
return;
}
if (axis == Axis.Z && _invertZ())
input = -input;
Vector3 worldInput = axis == Axis.X
? new Vector3(input, 0f, 0f)
: new Vector3(0f, 0f, input);
if (IsAccessoryControl())
{
Invoke(_moveAccessoryMethod, worldInput);
}
else if ((bool)_ikSelectedProperty.GetValue(null, null))
{
Invoke(_moveIkMethod, worldInput);
}
else
{
Invoke(_moveObjMethod, GetAllSelected(), worldInput);
}
}
private void EndDrag(BaseEventData data)
{
if (IsAccessoryControl())
{
Invoke(_createUndoAccessoryMoveMethod);
}
else if (IsRightButton(data))
{
Invoke(_createUndoForResizeMethod, GetAllSelected());
}
else if ((bool)_ikSelectedProperty.GetValue(null, null))
{
Invoke(_createUndoForIkMoveMethod);
_ikSelectedProperty.SetValue(null, false, null);
}
else
{
Invoke(_createUndoForMoveMethod, GetAllSelected());
}
}
private object GetAllSelected()
{
object window = _windowField.GetValue(null);
if (window == null)
throw new InvalidOperationException("Move Controller window is not available.");
return _allSelectedField.GetValue(window);
}
private bool IsAccessoryControl()
{
return (bool)Invoke(_isAccessoryControlMethod);
}
private static bool IsRightButton(BaseEventData data)
{
PointerEventData pointer = data as PointerEventData;
return pointer != null && pointer.button == PointerEventData.InputButton.Right;
}
private void SetAction(object action, string propertyName, Action<BaseEventData> callback)
{
PropertyInfo property = RequireProperty(_dragActionType, propertyName, AllInstance);
property.SetValue(action, callback, null);
}
private void SafeEvent(string operation, Action callback)
{
try
{
callback();
}
catch (Exception ex)
{
LogError("Move Controller axis " + operation + " failed", ex);
}
}
private static object Invoke(MethodInfo method, params object[] arguments)
{
return method.Invoke(null, arguments);
}
private Type RequireType(string fullName)
{
Type type = _assembly.GetType(fullName, false);
if (type == null)
throw new MissingMemberException(fullName);
return type;
}
private static FieldInfo RequireField(Type type, string name, BindingFlags flags)
{
FieldInfo field = type.GetField(name, flags);
if (field == null)
throw new MissingFieldException(type.FullName, name);
return field;
}
private static PropertyInfo RequireProperty(Type type, string name, BindingFlags flags)
{
PropertyInfo property = type.GetProperty(name, flags);
if (property == null)
throw new MissingMemberException(type.FullName, name);
return property;
}
private static MethodInfo RequireMethod(Type type, string name, int parameterCount)
{
MethodInfo[] methods = type.GetMethods(AllStatic);
for (int i = 0; i < methods.Length; i++)
{
if (methods[i].Name == name && methods[i].GetParameters().Length == parameterCount)
return methods[i];
}
throw new MissingMethodException(type.FullName, name);
}
private void LogError(string message, Exception exception)
{
Exception actual = exception;
while (actual is TargetInvocationException && actual.InnerException != null)
actual = actual.InnerException;
_logger.LogError(message + ": " + actual.Message);
}
}
}