forked from modlist-org/KeyViewer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPI.cs
More file actions
400 lines (399 loc) · 14.1 KB
/
API.cs
File metadata and controls
400 lines (399 loc) · 14.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
using DG.Tweening;
using Jint;
using Jint.Native;
using Jint.Native.Function;
using Jint.Runtime.Interop;
using JSNet.API;
using KeyViewer.API;
using KeyViewer.Core.TextReplacing;
using KeyViewer.Migration.V3;
using KeyViewer.Models;
using KeyViewer.Scripting.Proxies;
using KeyViewer.Utils;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using TMPro;
using UnityEngine;
namespace KeyViewer.Scripting
{
public static class API
{
[Api("GetAllProfileNames",
Comment = new string[]
{
"Get All Profile Names"
})]
public static string[] GetAllProfileNames() => KeyViewer.Main.Managers.Keys.ToArray();
[Api("GetManager",
Comment = new string[]
{
"Get Key Manager Object"
},
ParamComment = new string[]
{
"Key Viewer Profile Name (File Name Without Extension)"
},
RequireTypes = new Type[]
{
typeof(KeyCode),
typeof(Ease),
typeof(RainImageDisplayMode),
typeof(Pivot),
typeof(Anchor),
typeof(Models.Direction),
typeof(Color),
typeof(VertexGradient),
typeof(GColor),
typeof(RainImage),
typeof(ActiveProfile),
typeof(Vector2),
typeof(Vector3),
typeof(PressRelease<string>),
typeof(PressRelease<float>),
typeof(PressRelease<int>),
typeof(PressRelease<Vector2>),
typeof(PressRelease<Vector3>),
typeof(PressRelease<GColor>),
typeof(Judge<GColor>),
typeof(GUIStatus),
typeof(EaseConfig),
typeof(VectorConfig),
typeof(ObjectConfig),
typeof(KeyConfig),
typeof(RainConfig),
typeof(Profile),
typeof(KeyManagerProxy),
typeof(KeyProxy),
typeof(ListProxy),
},
RequireTypesAliases = new string[]
{
null,
null,
null,
null,
null,
null,
"Color",
"Gradient",
"GradientColor",
null,
null,
null,
null,
"PressRelease_String",
"PressRelease_Float",
"PressRelease_Int",
"PressRelease_Vector2",
"PressRelease_Vector3",
"PressRelease_GradientColor",
"JudgeColor",
null,
null,
null,
null,
null,
null,
null,
"KeyManager",
"Key",
"List",
})]
public static KeyManagerProxy GetManager(string profileName) => KeyViewer.Main.Managers.TryGetValue(profileName, out var manager) ? new KeyManagerProxy(manager) : null;
[Api("GetKey",
Comment = new string[]
{
"Get Key From Key Manager Object"
},
ParamComment = new string[]
{
"Key Manager Object",
"Key Name (KeyCode Or Dummy Key Name) ex) KeyCode.Semicolon, KPS"
})]
public static KeyProxy GetKey(KeyManagerProxy manager, object keyName)
{
Unity.Key result = null;
if (keyName is double d)
result = manager.manager.keys.Find(k => k.Config.Code == (KeyCode)(int)d);
else if (keyName is string str)
result = manager.manager.keys.Find(k => KeyViewerUtils.KeyName(k.Config) == str);
if (result != null) return new KeyProxy(result);
return null;
}
[Api("Apply",
Comment = new string[]
{
"Apply Config Changes"
},
ParamComment = new string[]
{
"Key Manager Object"
})]
public static void UpdateLayout(KeyManagerProxy manager) => manager.manager.UpdateLayout();
[Api("RegisterTag",
Comment = new string[]
{
"Register Tag (Like Overlayer)"
},
ParamComment = new string[]
{
"To Register Key Manager",
"Tag Name",
"Tag Function"
})]
public static void RegisterTag(KeyManagerProxy manager, string name, JsValue func)
{
if (!(func is FunctionInstance fi)) return;
Tag tag = new Tag(name);
tag.SetGetter(GenerateTagWrapper(new FIWrapper(fi)));
manager.manager.AllTags.Add(tag);
}
[Api("OnKeyPressed",
Comment = new string[]
{
"On Key Pressed Event"
},
ParamComment = new string[]
{
"Pressed Key Code",
"Event Callback"
})]
public static void OnKeyPressed(KeyCode code, JsValue func)
{
if (!(func is FunctionInstance fi)) return;
InputAPI.EventActive = true;
FIWrapper wrapper = new FIWrapper(fi);
InputAPI.OnKeyPressed += c =>
{
if (code == c.Config.Code)
wrapper.CallRaw();
};
}
[Api("OnKeyReleased",
Comment = new string[]
{
"On Key Released Event"
},
ParamComment = new string[]
{
"Released Key Code",
"Event Callback"
})]
public static void OnKeyReleased(KeyCode code, JsValue func)
{
if (!(func is FunctionInstance fi)) return;
InputAPI.EventActive = true;
FIWrapper wrapper = new FIWrapper(fi);
InputAPI.OnKeyReleased += c =>
{
if (code == c.Config.Code)
wrapper.CallRaw();
};
}
[Api("EasedValue",
Comment = new string[]
{
"Eased Value"
},
ParamComment = new string[]
{
"Ease Type",
"Lifetime (0.0 ~ 1.0)"
},
ReturnComment = "Eased Value")]
public static float EasedValue(Ease ease, float lifetime)
{
return DOVirtual.EasedValue(0, 1, lifetime, ease);
}
[Api("DelayedCall",
Comment = new string[]
{
"Delayed Call"
},
ParamComment = new string[]
{
"Delay Seconds",
"Callback"
},
ReturnComment = "Delayed Caller Handle")]
public static object DelayedCall(float seconds, JsValue func)
{
if (!(func is FunctionInstance fi)) return null;
FIWrapper wrapper = new FIWrapper(fi);
return DOVirtual.DelayedCall(seconds, () => wrapper.CallRaw(), false);
}
[Api("CancelDelayedCall",
Comment = new string[]
{
"Cancel Delayed Call"
},
ParamComment = new string[]
{
"Delayed Caller Handle",
})]
public static void CancelDelayedCall(object handle)
{
if (!(handle is Sequence t)) return;
t.Kill(false);
}
public static object[] StrArrayToObjArray(string[] arr)
{
object[] newArr = new object[arr.Length];
for (int i = 0; i < arr.Length; i++)
newArr[i] = arr[i];
return newArr;
}
[Api("Resolve",
Comment = new string[]
{
"Resolve CLR Type"
},
ParamComment = new string[]
{
"CLR Type Full Name"
},
ReturnComment = "Resolved CLR Type")]
public static TypeReference Resolve(Engine engine, string clrType)
{
if (jsTypes.TryGetValue(engine, out var dict))
if (dict.TryGetValue(clrType, out var t))
return t;
else return dict[clrType] = TypeReference.CreateTypeReference(engine, MiscUtils.TypeByName(clrType));
dict = jsTypes[engine] = new Dictionary<string, TypeReference>();
return dict[clrType] = TypeReference.CreateTypeReference(engine, MiscUtils.TypeByName(clrType));
}
[Api("ReadV3Settings",
Comment = new string[]
{
"Read V3 Settings.xml"
},
ParamComment = new string[]
{
"Settings.xml Path"
},
ReturnComment = "V3 Settings Object",
RequireTypes = new Type[]
{
typeof(Migration.V3.Direction),
typeof(SpecialKeyType),
typeof(LanguageType),
typeof(KeyRain_Config),
typeof(Key_Config),
typeof(Group),
typeof(V3Profile),
typeof(V3Settings),
},
RequireTypesAliases = new string[]
{
"V3Direction",
"V3SpecialKeyType",
"V3LanguageType",
"V3RainConfig",
"V3KeyConfig",
"V3Group",
null,
null
})]
public static V3Settings ReadV3Settings(string xmlPath)
{
if (!File.Exists(xmlPath)) return null;
return KeyViewer.Main.ReadV3Settings(xmlPath);
}
[Api("ReadV3Profile",
Comment = new string[]
{
"Read V3 Profile Xml File"
},
ParamComment = new string[]
{
"Profile Xml File Path"
},
ReturnComment = "V3 Profile Object")]
public static V3Profile ReadV3Profile(string xmlPath)
{
if (!File.Exists(xmlPath)) return null;
return KeyViewer.Main.ReadV3Profile(xmlPath);
}
static MethodInfo GenerateTagWrapper(FIWrapper wrapper)
{
Type[] paramTypes = wrapper.args.Select(t => typeof(string)).ToArray();
var name = wrapper.fi.ToString().Replace("function ", string.Empty).Replace("() { [native code] }", string.Empty);
if (string.IsNullOrWhiteSpace(name)) name = "Anonymous";
TypeBuilder wrapperType = mod.DefineType($"{name}_WrapperType${uniqueId++}", TypeAttributes.Public);
MethodBuilder wrapperMethod = wrapperType.DefineMethod($"{name}_WrapperMethod", MethodAttributes.Public | MethodAttributes.Static, typeof(object), paramTypes);
FieldBuilder wrapperField = wrapperType.DefineField("wrapper", typeof(FIWrapper), FieldAttributes.Public | FieldAttributes.Static);
for (int i = 0; i < wrapper.args.Length; i++)
wrapperMethod.DefineParameter(i + 1, ParameterAttributes.None, wrapper.args[i]);
ILGenerator il = wrapperMethod.GetILGenerator();
if (paramTypes.Length > 0)
{
LocalBuilder strArray = il.MakeArray<string>(paramTypes.Length);
for (int i = 0; i < paramTypes.Length; i++)
{
il.Emit(OpCodes.Ldloc, strArray);
il.Emit(OpCodes.Ldc_I4, i);
il.Emit(OpCodes.Ldarg, i);
il.Emit(OpCodes.Stelem_Ref);
}
il.Emit(OpCodes.Ldsfld, wrapperField);
il.Emit(OpCodes.Ldloc, strArray);
il.Emit(OpCodes.Call, satoa);
}
else
{
il.Emit(OpCodes.Ldsfld, wrapperField);
il.Emit(OpCodes.Ldc_I4_0);
il.Emit(OpCodes.Newarr, typeof(object));
}
il.Emit(OpCodes.Call, call_fi);
il.Emit(OpCodes.Ret);
var resultT = wrapperType.CreateType();
resultT.GetField("wrapper").SetValue(null, wrapper);
return resultT.GetMethod($"{name}_WrapperMethod");
}
static LocalBuilder MakeArray<T>(this ILGenerator il, int length)
{
LocalBuilder array = il.DeclareLocal(typeof(T[]));
il.Emit(OpCodes.Ldc_I4, length);
il.Emit(OpCodes.Newarr, typeof(T));
il.Emit(OpCodes.Stloc, array);
return array;
}
public static Api api { get; private set; }
static int uniqueId;
static Dictionary<Engine, Dictionary<string, TypeReference>> jsTypes;
static MethodInfo satoa;
static MethodInfo call_fi;
static AssemblyBuilder ass;
static System.Reflection.Emit.ModuleBuilder mod;
public static void Initialize()
{
uniqueId = 0;
jsTypes = new Dictionary<Engine, Dictionary<string, TypeReference>>();
satoa = typeof(API).GetMethod(nameof(StrArrayToObjArray), (BindingFlags)15420);
call_fi = typeof(FIWrapper).GetMethod("Call");
ass = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName("KeyViewer.Scripting.TagWrapper"), AssemblyBuilderAccess.RunAndCollect);
mod = ass.DefineDynamicModule("KeyViewer.Scripting.TagWrapper");
api = new Api();
api.RegisterType(typeof(API));
api.RegisterType(typeof(ListProxy));
var scriptsFolder = Path.Combine(Main.Mod.Path, "Scripts");
Directory.CreateDirectory(scriptsFolder);
File.WriteAllText(Path.Combine(scriptsFolder, "Impl.js"), api.Generate());
}
public static void Release()
{
jsTypes = null;
satoa = null;
call_fi = null;
ass = null;
mod = null;
api = null;
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true);
}
}
}