This repository was archived by the owner on Apr 8, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPatches.cs
More file actions
558 lines (512 loc) · 22.3 KB
/
Patches.cs
File metadata and controls
558 lines (512 loc) · 22.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
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
using BaseMacro.Macro;
using BaseMacro.Platform;
using HarmonyLib;
using SkyHook;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using UnityEngine;
namespace BaseMacro
{
public class Patches
{
// 普通按键缓存状态
private static HashSet<KeyCode> _cachedFilteredKeys = null;
private static string _lastFilteredKeysString = "";
private static int _lastFilterMode = 0;
private static bool _lastEnableFilter = false;
// 异步按键缓存状态(独立,不共享)
private static HashSet<ushort> _cachedFilteredAsyncKeys = null;
private static string _lastFilteredAsyncKeysString = "";
private static int _lastAsyncFilterMode = 0; // ← 独立
private static bool _lastAsyncEnableFilter = false; // ← 独立
[HarmonyPatch(typeof(scrController), "PlayerControl_Update")]
public static class Patch_PlayerControl_Update
{
[HarmonyPrefix]
public static void Prefix(scrController __instance)
{
Macro.Macro.Update(__instance);
Macro.Macro.HandleInput();
}
}
[HarmonyPatch(typeof(scrController), nameof(scrController.Awake_Rewind))]
public static class Patch_Awake_Rewind
{
[HarmonyPostfix]
public static void Postfix(scrController __instance) => Macro.Macro.Reset(__instance);
}
[HarmonyPatch(typeof(scrController), nameof(scrController.Restart))]
public static class Patch_Restart
{
[HarmonyPrefix]
public static void Prefix(scrController __instance) => Macro.Macro.Reset(__instance);
}
[HarmonyPatch(typeof(scnEditor), "Start")]
public static class Patch_scnEdityor_Start
{
[HarmonyPostfix]
public static void Postfix(scnEditor __instance)
{
if (Main.Settings.LockLevelEditor)
__instance.LockPathEditing(true);
}
}
[HarmonyPatch(typeof(scrController), "Fail2Action")]
public static class Patch_FailAction
{
[HarmonyPostfix]
public static void Postfix()
{
if (Main.Settings.EnableDeathKey && Main.IsEnabled && Main.Settings.Macro)
{
ADOBase.controller?.StartCoroutine(DelayedSendDeathKey());
ADOBase.editor?.StartCoroutine(DelayedSendDeathKey());
ADOBase.customLevel?.StartCoroutine(DelayedSendDeathKey());
}
}
private static System.Collections.IEnumerator DelayedSendDeathKey()
{
yield return new WaitForSeconds(Main.Settings.DeathKeyDelay);
if (Main.Settings.SimulateKeyPress && Main.Settings.SkyHookMode && InputSystem.IsInitialized)
{
InputSystem.SendKeyDirect((byte)Main.Settings.DeathKeyCode, true);
}
}
}
[HarmonyPatch(typeof(scnEditor), nameof(scnEditor.Play))]
public static class Patch_scnEditor_Play
{
[HarmonyPostfix]
public static void Postfix(scnEditor __instance)
{
if (Main.Settings.Macro)
{
if (Main.Settings.ChangeJudementInPlay)
__instance.editorDifficultySelector.SetChangeable(true);
if (Main.Settings.ChangeNoFaillInPlay)
__instance.buttonNoFail.interactable = true;
}
}
}
[HarmonyPatch(typeof(scrConductor), "Update")]
public static class __scrConductor
{
public static unsafe long Update_1()
{
long l = BaseSelect.GetFileTime();
return DateTime.Now.Ticks - DateTime.UtcNow.Ticks + l;
}
public static double Update_2() => DSPTimeSimulater.GetDSPTime();
[HarmonyTranspiler]
public static IEnumerable<CodeInstruction> Transpiler_Update(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
{
if (!Main.Settings.HighPrecisionAsync)
{
return instructions;
}
bool patch = false;
int skip = 0;
List<CodeInstruction> result = [];
foreach (CodeInstruction ci in instructions)
{
if (patch)
{
patch = false;
result.Add(new CodeInstruction(OpCodes.Call, typeof(__scrConductor).GetMethod(nameof(Update_1), AccessTools.all)));
}
if (skip > 0)
{
skip--;
continue;
}
if (ci.opcode == OpCodes.Call && ((MethodInfo)ci.operand).Name == "get_Now")
{
skip = 3;
patch = true;
continue;
}
if (ci.opcode == OpCodes.Call && ((MethodInfo)ci.operand).Name == "get_dspTime")
{
ci.operand = typeof(__scrConductor).GetMethod(nameof(Update_2), AccessTools.all);
}
result.Add(ci);
}
return result.AsEnumerable();
}
}
// 解析按键字符串为KeyCode列表
private static readonly Dictionary<string, KeyCode> KeyCodeAliasMap = new(StringComparer.OrdinalIgnoreCase)
{
// 方向键
{"UP", KeyCode.UpArrow},
{"DOWN", KeyCode.DownArrow},
{"LEFT", KeyCode.LeftArrow},
{"RIGHT", KeyCode.RightArrow},
// 修饰键
{"CTRL", KeyCode.LeftControl},
{"LCTRL", KeyCode.LeftControl},
{"RCTRL", KeyCode.RightControl},
{"ALT", KeyCode.LeftAlt},
{"LALT", KeyCode.LeftAlt},
{"RALT", KeyCode.RightAlt},
{"SHIFT", KeyCode.LeftShift},
{"LSHIFT", KeyCode.LeftShift},
{"RSHIFT", KeyCode.RightShift},
{"WIN", KeyCode.LeftWindows},
{"LWIN", KeyCode.LeftWindows},
{"RWIN", KeyCode.RightWindows},
// 常用键
{"SPACE", KeyCode.Space},
{"ENTER", KeyCode.Return},
{"RETURN", KeyCode.Return},
{"ESC", KeyCode.Escape},
{"ESCAPE", KeyCode.Escape},
{"TAB", KeyCode.Tab},
{"BACKSPACE", KeyCode.Backspace},
{"DELETE", KeyCode.Delete},
{"DEL", KeyCode.Delete},
{"INSERT", KeyCode.Insert},
{"INS", KeyCode.Insert},
{"HOME", KeyCode.Home},
{"END", KeyCode.End},
{"PAGEUP", KeyCode.PageUp},
{"PGUP", KeyCode.PageUp},
{"PAGEDOWN", KeyCode.PageDown},
{"PGDN", KeyCode.PageDown},
{"CAPSLOCK", KeyCode.CapsLock},
{"CAPS", KeyCode.CapsLock},
{"NUMLOCK", KeyCode.Numlock},
{"SCROLLLOCK",KeyCode.ScrollLock},
{"PRINTSCREEN",KeyCode.Print},
{"PAUSE", KeyCode.Pause},
// 标点符号
{";", KeyCode.Semicolon},
{"SEMICOLON", KeyCode.Semicolon},
{"=", KeyCode.Equals},
{"EQUALS", KeyCode.Equals},
{",", KeyCode.Comma},
{"COMMA", KeyCode.Comma},
{"-", KeyCode.Minus},
{"MINUS", KeyCode.Minus},
{".", KeyCode.Period},
{"PERIOD", KeyCode.Period},
{"/", KeyCode.Slash},
{"SLASH", KeyCode.Slash},
{"`", KeyCode.BackQuote},
{"BACKQUOTE", KeyCode.BackQuote},
{"[", KeyCode.LeftBracket},
{"LBRACKET", KeyCode.LeftBracket},
{"\\", KeyCode.Backslash},
{"BACKSLASH", KeyCode.Backslash},
{"]", KeyCode.RightBracket},
{"RBRACKET", KeyCode.RightBracket},
{"'", KeyCode.Quote},
{"QUOTE", KeyCode.Quote},
// 小键盘
{"NUM0", KeyCode.Keypad0},
{"NUM1", KeyCode.Keypad1},
{"NUM2", KeyCode.Keypad2},
{"NUM3", KeyCode.Keypad3},
{"NUM4", KeyCode.Keypad4},
{"NUM5", KeyCode.Keypad5},
{"NUM6", KeyCode.Keypad6},
{"NUM7", KeyCode.Keypad7},
{"NUM8", KeyCode.Keypad8},
{"NUM9", KeyCode.Keypad9},
{"NUM.", KeyCode.KeypadPeriod},
{"NUM/", KeyCode.KeypadDivide},
{"NUM*", KeyCode.KeypadMultiply},
{"NUM-", KeyCode.KeypadMinus},
{"NUM+", KeyCode.KeypadPlus},
{"NUMENTER", KeyCode.KeypadEnter},
{"NUM=", KeyCode.KeypadEquals},
};
private static HashSet<KeyCode> ParseKeyCodes(string keyString)
{
var result = new HashSet<KeyCode>();
if (string.IsNullOrEmpty(keyString)) return result;
string[] keys = keyString.Split([','], StringSplitOptions.RemoveEmptyEntries);
foreach (string key in keys)
{
string trimmedKey = key.Trim().ToUpper();
// 优先查别名表(处理 UP/DOWN/LEFT/RIGHT 等)
if (KeyCodeAliasMap.TryGetValue(trimmedKey, out KeyCode aliasCode))
{
result.Add(aliasCode);
}
// 再尝试 Unity KeyCode 枚举名
else if (Enum.TryParse<KeyCode>(trimmedKey, true, out KeyCode keyCode))
{
result.Add(keyCode);
}
// 十六进制
else if (trimmedKey.StartsWith("0X") && int.TryParse(trimmedKey.Substring(2),
System.Globalization.NumberStyles.HexNumber, null, out int hexCode))
{
result.Add((KeyCode)hexCode);
}
}
return result;
}
// 检查普通按键是否允许通过
private static bool IsKeyAllowed(KeyCode keyCode)
{
if (!Main.IsEnabled || !Main.Settings.Macro)
return true; // 如果宏未启用,不过滤任何按键
if (!Main.Settings.EnableKeyFilter) return true;
// 缓存解析结果
if (_cachedFilteredKeys == null ||
_lastFilteredKeysString != Main.Settings.FilteredKeys ||
_lastEnableFilter != Main.Settings.EnableKeyFilter ||
_lastFilterMode != Main.Settings.FilterMode)
{
_cachedFilteredKeys = ParseKeyCodes(Main.Settings.FilteredKeys);
_lastFilteredKeysString = Main.Settings.FilteredKeys;
_lastEnableFilter = Main.Settings.EnableKeyFilter;
_lastFilterMode = Main.Settings.FilterMode;
}
bool inList = _cachedFilteredKeys.Contains(keyCode);
// 黑名单模式:在列表中则阻止
if (Main.Settings.FilterMode == 0)
{
return !inList;
}
// 白名单模式:在列表中才允许
else
{
return inList;
}
}
// 修改 CountValidKeysPressed 补丁添加黑白名单逻辑
[HarmonyPatch(typeof(scrController), "CountValidKeysPressed")]
public static class scrController_CountValidKeysPressed_Patch
{
private static int validKeys;
[HarmonyPrefix]
public static void Prefix()
{
validKeys = CountValidKeysPressed();
}
[HarmonyPostfix]
public static void Postfix(ref int __result)
{
__result = validKeys;
}
private static int CountValidKeysPressed()
{
int num = 0;
scrController instance = scrController.instance;
instance.keyLimiterOverCounter = 0;
foreach (AnyKeyCode anyKeyCode in RDInput.GetMainPressKeys())
{
object value = anyKeyCode.value;
if (value is KeyCode keyCode)
{
instance.keyFrequency[keyCode] = instance.keyFrequency.ContainsKey(keyCode) ? instance.keyFrequency[keyCode] + 1 : 0;
instance.keyTotal++;
// 黑白名单过滤
if (IsKeyAllowed(keyCode))
{
num++;
}
else
{
Macro.Macro.Log($"Filtered Key: {keyCode} ({(Main.Settings.FilterMode == 0 ? "Blacklist" : "Whitelist")})");
}
}
else if (value is AsyncKeyCode asyncKeyCode)
{
instance.keyFrequency[asyncKeyCode] = instance.keyFrequency.ContainsKey(asyncKeyCode) ? instance.keyFrequency[asyncKeyCode] + 1 : 0;
instance.keyTotal++;
if (IsAsyncKeyAllowed(asyncKeyCode.key)) // 小写 key
{
num++;
}
else
{
Macro.Macro.Log($"Filtered Async Key in CountValidKeysPressed: {asyncKeyCode.key} (0x{asyncKeyCode.key:X2})");
}
}
}
return num;
}
}
private static readonly Dictionary<string, ushort> AsyncKeyVKMap = new(StringComparer.OrdinalIgnoreCase)
{
// 字母键
{"A",0x41},{"B",0x42},{"C",0x43},{"D",0x44},{"E",0x45},{"F",0x46},
{"G",0x47},{"H",0x48},{"I",0x49},{"J",0x4A},{"K",0x4B},{"L",0x4C},
{"M",0x4D},{"N",0x4E},{"O",0x4F},{"P",0x50},{"Q",0x51},{"R",0x52},
{"S",0x53},{"T",0x54},{"U",0x55},{"V",0x56},{"W",0x57},{"X",0x58},
{"Y",0x59},{"Z",0x5A},
// 数字键
{"0",0x30},{"1",0x31},{"2",0x32},{"3",0x33},{"4",0x34},
{"5",0x35},{"6",0x36},{"7",0x37},{"8",0x38},{"9",0x39},
// 功能键
{"F1",0x70},{"F2",0x71},{"F3",0x72},{"F4",0x73},{"F5",0x74},
{"F6",0x75},{"F7",0x76},{"F8",0x77},{"F9",0x78},{"F10",0x79},
{"F11",0x7A},{"F12",0x7B},{"F13",0x7C},{"F14",0x7D},{"F15",0x7E},
{"F16",0x7F},{"F17",0x80},{"F18",0x81},{"F19",0x82},{"F20",0x83},
{"F21",0x84},{"F22",0x85},{"F23",0x86},{"F24",0x87},
// 方向键
{"UP",0x26},{"DOWN",0x28},{"LEFT",0x25},{"RIGHT",0x27},
// 常用键
{"SPACE", 0x20},
{"ENTER", 0x0D},{"RETURN", 0x0D},
{"ESC", 0x1B},{"ESCAPE", 0x1B},
{"TAB", 0x09},
{"BACKSPACE", 0x08},
{"DELETE", 0x2E},{"DEL", 0x2E},
{"INSERT", 0x2D},{"INS", 0x2D},
{"HOME", 0x24},
{"END", 0x23},
{"PAGEUP", 0x21},{"PGUP", 0x21},
{"PAGEDOWN", 0x22},{"PGDN", 0x22},
{"CAPSLOCK", 0x14},{"CAPS", 0x14},
{"NUMLOCK", 0x90},
{"SCROLLLOCK",0x91},
{"PRINTSCREEN",0x2C},
{"PAUSE", 0x13},
// 修饰键
{"SHIFT", 0x10},
{"LSHIFT", 0xA0},{"RSHIFT", 0xA1},
{"CTRL", 0x11},
{"LCTRL", 0xA2},{"RCTRL", 0xA3},
{"ALT", 0x12},
{"LALT", 0xA4},{"RALT", 0xA5},
{"WIN", 0x5B},{"LWIN", 0x5B},{"RWIN", 0x5C},
// 标点符号
{";", 0xBA},{"SEMICOLON", 0xBA},
{"=", 0xBB},{"EQUALS", 0xBB},
{",", 0xBC},{"COMMA", 0xBC},
{"-", 0xBD},{"MINUS", 0xBD},
{".", 0xBE},{"PERIOD", 0xBE},
{"/", 0xBF},{"SLASH", 0xBF},
{"`", 0xC0},{"BACKQUOTE", 0xC0},
{"[", 0xDB},{"LBRACKET", 0xDB},
{"\\", 0xDC},{"BACKSLASH", 0xDC},
{"]", 0xDD},{"RBRACKET", 0xDD},
{"'", 0xDE},{"QUOTE", 0xDE},
// 小键盘
{"NUM0", 0x60},{"NUM1", 0x61},{"NUM2", 0x62},
{"NUM3", 0x63},{"NUM4", 0x64},{"NUM5", 0x65},
{"NUM6", 0x66},{"NUM7", 0x67},{"NUM8", 0x68},
{"NUM9", 0x69},
{"NUM*", 0x6A},{"NUM+", 0x6B},{"NUM-", 0x6D},
{"NUM.", 0x6E},{"NUM/", 0x6F},{"NUMENTER", 0x0D},
// 媒体键
{"MUTE", 0xAD},
{"VOLUMEDOWN",0xAE},{"VOLDOWN", 0xAE},
{"VOLUMEUP", 0xAF},{"VOLUP", 0xAF},
{"MEDIANEXT", 0xB0},
{"MEDIAPREV", 0xB1},
{"MEDIASTOP", 0xB2},
{"MEDIAPLAY", 0xB3},
};
private static HashSet<ushort> ParseAsyncKeyCodes(string keyString)
{
var result = new HashSet<ushort>();
if (string.IsNullOrEmpty(keyString)) return result;
string[] keys = keyString.Split([','], StringSplitOptions.RemoveEmptyEntries);
foreach (string key in keys)
{
string trimmedKey = key.Trim().ToUpper();
// 优先查 VK 映射表
if (AsyncKeyVKMap.TryGetValue(trimmedKey, out ushort vkCode))
{
result.Add(vkCode);
Macro.Macro.Log($"Parsed async key: {trimmedKey} -> VK 0x{vkCode:X2}");
}
// 十六进制 (0x26 格式)
else if (trimmedKey.StartsWith("0X") && ushort.TryParse(trimmedKey.Substring(2),
System.Globalization.NumberStyles.HexNumber, null, out ushort hexCode))
{
result.Add(hexCode);
Macro.Macro.Log($"Parsed async hex: {trimmedKey} -> 0x{hexCode:X2}");
}
// 纯数字
else if (ushort.TryParse(trimmedKey, out ushort numCode))
{
result.Add(numCode);
Macro.Macro.Log($"Parsed async number: {trimmedKey} -> 0x{numCode:X2}");
}
else
{
Macro.Macro.Log($"Failed to parse async key: {trimmedKey}");
}
}
return result;
}
// 检查异步按键是否允许通过
private static bool IsAsyncKeyAllowed(ushort keyCode)
{
if (!Main.IsEnabled || !Main.Settings.Macro)
return true; // 如果宏未启用,不过滤任何按键
if (!Main.Settings.EnableKeyFilter)
{
// Main.Logger.Log($"Key filter disabled, allowing async key: {keyCode}");
return true;
}
// 缓存解析结果
if (_cachedFilteredAsyncKeys == null ||
_lastFilteredAsyncKeysString != Main.Settings.FilteredAsyncKeys ||
_lastAsyncEnableFilter != Main.Settings.EnableKeyFilter ||
_lastAsyncFilterMode != Main.Settings.FilterMode)
{
_cachedFilteredAsyncKeys = ParseAsyncKeyCodes(Main.Settings.FilteredAsyncKeys);
_lastFilteredAsyncKeysString = Main.Settings.FilteredAsyncKeys;
_lastAsyncEnableFilter = Main.Settings.EnableKeyFilter; // ← 改这里
_lastAsyncFilterMode = Main.Settings.FilterMode; // ← 改这里
}
bool inList = _cachedFilteredAsyncKeys.Contains(keyCode);
// 添加调试日志
Macro.Macro.Log($"Async key {keyCode} (0x{keyCode:X2}) in list: {inList}, FilterMode: {Main.Settings.FilterMode}");
// 黑名单模式:在列表中则阻止
if (Main.Settings.FilterMode == 0)
{
bool allowed = !inList;
if (!allowed)
{
Macro.Macro.Log($"Blacklist blocked async key: {keyCode} (0x{keyCode:X2})");
}
return allowed;
}
// 白名单模式:在列表中才允许
else
{
bool allowed = inList;
if (!allowed)
{
Macro.Macro.Log($"Whitelist blocked async key: {keyCode} (0x{keyCode:X2})");
}
return allowed;
}
}
// 修改 SkyHook 按键过滤补丁
[HarmonyPatch(typeof(SkyHookManager), "HookCallback")]
public static class SkyHookManager_HookCallback_Patch
{
[HarmonyPrefix]
public static bool Prefix(SkyHookEvent ev)
{
scrController instance = scrController.instance;
if (!instance) return true;
// KeyReleased(=1) 和 ESC 永远放行
if (ev.Type == SkyHook.EventType.KeyReleased || ev.Key == 27) return true;
// 未开启过滤 / 暂停 / 不在游戏世界 → 放行
if (!Main.Settings.EnableKeyFilter) return true;
if (instance.paused || !instance.gameworld) return true;
// 只在 PlayerControl 状态下过滤
Enum state = instance.stateMachine.GetState();
if (!(state is States s && s == States.PlayerControl)) return true;
bool allowed = IsAsyncKeyAllowed(ev.Key);
if (!allowed)
Macro.Macro.Log($"Filtered Async Key: {ev.Label} ({ev.Key}) - {(Main.Settings.FilterMode == 0 ? "Blacklist" : "Whitelist")}");
return allowed;
}
}
}
}