Repository navigation
Expand file tree
/
Copy pathOptionPanelPatch.cs
More file actions
400 lines (348 loc) · 16.3 KB
/
Copy pathOptionPanelPatch.cs
File metadata and controls
400 lines (348 loc) · 16.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
using HarmonyLib;
using Il2Cpp;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using System;
using UnityEngine;
using UnityEngine.UI;
namespace DigimonNOAccess
{
/// <summary>
/// Harmony patches to inject an "Accessibility" item into the game's options TOP panel.
/// When selected, activates AccessibilityMenuHandler instead of a game subpanel.
///
/// Strategy: extend m_CommandInfoArray and m_DataMax for cursor range, AND add a
/// dummy uOptionPanelItemVoid to m_items so the game's native confirm path works.
/// Use PREFIX/POSTFIX sandwich on ItemSetUp: PREFIX removes our m_items entry before
/// the game rebuilds, POSTFIX adds it back. This prevents our entry from being
/// treated as a visual item (which would displace Agreement/Quit).
/// SetMainSettingState_Prefix intercepts the confirm and activates our menu.
/// </summary>
public static class OptionPanelPatch
{
public static int AccessibilityItemIndex { get; private set; } = -1;
private static uOptionPanel _optionPanel;
private static uOptionTopPanelCommand _topPanel;
private static GameObject _accessibilityGo;
private static uOptionPanelItemVoid _dummyItem;
/// <summary>
/// Time-based input suppression after returning from accessibility menu.
/// Blocks CheckInputKey until the timer expires, ensuring the cancel press
/// doesn't propagate regardless of script execution order.
/// </summary>
private static float _suppressUntilTime;
public static void Apply(HarmonyLib.Harmony harmony)
{
try
{
var itemSetUpMethod = AccessTools.Method(typeof(uOptionTopPanelCommand), "ItemSetUp");
if (itemSetUpMethod != null)
{
harmony.Patch(itemSetUpMethod,
prefix: new HarmonyMethod(AccessTools.Method(typeof(OptionPanelPatch), nameof(ItemSetUp_Prefix))),
postfix: new HarmonyMethod(AccessTools.Method(typeof(OptionPanelPatch), nameof(ItemSetUp_Postfix))));
DebugLogger.Log("[OptionPanelPatch] Patched uOptionTopPanelCommand.ItemSetUp (prefix+postfix)");
}
else
{
DebugLogger.Warning("[OptionPanelPatch] Could not find uOptionTopPanelCommand.ItemSetUp");
}
var setStateMethod = AccessTools.Method(typeof(uOptionPanel), "SetMainSettingState",
new Type[] { typeof(uOptionPanel.MainSettingState) });
if (setStateMethod != null)
{
harmony.Patch(setStateMethod,
prefix: new HarmonyMethod(AccessTools.Method(typeof(OptionPanelPatch), nameof(SetMainSettingState_Prefix))));
DebugLogger.Log("[OptionPanelPatch] Patched uOptionPanel.SetMainSettingState");
}
else
{
DebugLogger.Warning("[OptionPanelPatch] Could not find uOptionPanel.SetMainSettingState");
}
var checkInputMethod = AccessTools.Method(typeof(uOptionPanel), "CheckInputKey");
if (checkInputMethod != null)
{
harmony.Patch(checkInputMethod,
prefix: new HarmonyMethod(AccessTools.Method(typeof(OptionPanelPatch), nameof(CheckInputKey_Prefix))));
DebugLogger.Log("[OptionPanelPatch] Patched uOptionPanel.CheckInputKey");
}
else
{
DebugLogger.Warning("[OptionPanelPatch] Could not find uOptionPanel.CheckInputKey");
}
}
catch (Exception ex)
{
DebugLogger.Error($"[OptionPanelPatch] Apply error: {ex.Message}");
}
}
/// <summary>
/// Before the game rebuilds its item list, remove our dummy entry from m_items.
/// This prevents the game from treating our entry as a visual item.
/// </summary>
private static void ItemSetUp_Prefix(uOptionTopPanelCommand __instance)
{
try
{
if (_dummyItem == null)
return;
var items = __instance.m_items;
if (items == null)
return;
// Remove our dummy item if present
for (int i = items.Count - 1; i >= 0; i--)
{
var item = items[i];
if (item != null && item.Pointer == _dummyItem.Pointer)
{
items.RemoveAt(i);
break;
}
}
// Shrink CommandInfoArray back to game-only size
// (our postfix extended it last time; the game reuses it otherwise)
int gameItemCount = items.Count;
var array = __instance.m_CommandInfoArray;
if (array != null && array.Length > gameItemCount)
{
var trimmed = new Il2CppReferenceArray<uOptionTopPanelCommand.CommandInfo>(gameItemCount);
for (int i = 0; i < gameItemCount; i++)
trimmed[i] = array[i];
__instance.m_CommandInfoArray = trimmed;
}
// Reset m_DataMax to match
var cursorCtrl = __instance.m_KeyCursorController;
if (cursorCtrl != null)
cursorCtrl.m_DataMax = gameItemCount;
}
catch (Exception ex)
{
DebugLogger.Error($"[OptionPanelPatch] ItemSetUp_Prefix error: {ex.Message}");
}
}
/// <summary>
/// After the game builds its items, extend the array with our entry
/// and add a dummy uOptionPanelItemVoid to m_items for the confirm path.
/// </summary>
private static void ItemSetUp_Postfix(uOptionTopPanelCommand __instance)
{
try
{
_topPanel = __instance;
var cursorCtrl = __instance.m_KeyCursorController;
if (cursorCtrl == null)
{
DebugLogger.Warning("[OptionPanelPatch] No KeyCursorController");
return;
}
var array = __instance.m_CommandInfoArray;
if (array == null || array.Length == 0)
{
DebugLogger.Warning("[OptionPanelPatch] m_CommandInfoArray is null/empty");
return;
}
// Create standalone GO and dummy item once
if (_accessibilityGo == null)
{
_accessibilityGo = new GameObject("AccessibilityOptionStandalone");
UnityEngine.Object.DontDestroyOnLoad(_accessibilityGo);
_accessibilityGo.SetActive(false);
DebugLogger.Log("[OptionPanelPatch] Created standalone Accessibility GO");
}
if (_dummyItem == null)
{
_dummyItem = _accessibilityGo.AddComponent<uOptionPanelItemVoid>();
// State doesn't matter — SetMainSettingState_Prefix intercepts
// based on cursor position, not the state value
_dummyItem.m_settingState = uOptionPanel.MainSettingState.TOP;
DebugLogger.Log("[OptionPanelPatch] Created dummy uOptionPanelItemVoid");
}
int gameArrayLen = array.Length;
// Calculate cursor position from last game item
int lastIndex = gameArrayLen - 1;
var lastInfo = array[lastIndex];
var cursorPos = lastInfo.m_CommandCursorPos;
if (gameArrayLen >= 2)
{
var prevCursorPos = array[lastIndex - 1].m_CommandCursorPos;
float spacing = lastInfo.m_CommandCursorPos.y - prevCursorPos.y;
cursorPos.y += spacing;
}
var newInfo = new uOptionTopPanelCommand.CommandInfo();
newInfo.m_CommandObject = _accessibilityGo;
newInfo.m_CommandCursorPos = cursorPos;
// Extend CommandInfoArray with our item at the end
var newArray = new Il2CppReferenceArray<uOptionTopPanelCommand.CommandInfo>(gameArrayLen + 1);
for (int i = 0; i < gameArrayLen; i++)
newArray[i] = array[i];
newArray[gameArrayLen] = newInfo;
__instance.m_CommandInfoArray = newArray;
AccessibilityItemIndex = gameArrayLen;
cursorCtrl.m_DataMax = gameArrayLen + 1;
// Add dummy item to m_items so native CheckInputKey can read it on confirm
var items = __instance.m_items;
if (items != null)
{
items.Add(_dummyItem.Cast<uOptionPanelItemBase>());
DebugLogger.Log($"[OptionPanelPatch] POSTFIX: Added dummy to m_items (count: {items.Count}), array index {AccessibilityItemIndex}, m_DataMax: {gameArrayLen + 1}");
}
DebugLogger.Log($"[OptionPanelPatch] Appended Accessibility at index {AccessibilityItemIndex} (game array: {gameArrayLen}, m_DataMax: {gameArrayLen + 1})");
}
catch (Exception ex)
{
DebugLogger.Error($"[OptionPanelPatch] ItemSetUp_Postfix error: {ex.Message}");
}
}
/// <summary>
/// Intercept SetMainSettingState when cursor is on our index.
/// The game's native CheckInputKey reads m_items[ourIndex].m_settingState → calls this.
/// We suppress the state change and activate the accessibility menu instead.
/// </summary>
private static bool SetMainSettingState_Prefix(uOptionPanel __instance, uOptionPanel.MainSettingState state)
{
try
{
_optionPanel = __instance;
if (__instance.m_MainSettingState != uOptionPanel.MainSettingState.TOP)
return true;
if (AccessibilityItemIndex < 0)
return true;
var topPanel = GetTopPanel(__instance);
if (topPanel == null)
return true;
var cursorCtrl = topPanel.m_KeyCursorController;
if (cursorCtrl == null)
return true;
if (cursorCtrl.m_DataIndex != AccessibilityItemIndex)
return true;
// Cursor is on our item — suppress the game's state change and open our menu
DebugLogger.Log("[OptionPanelPatch] SetMainSettingState intercepted for Accessibility index");
topPanel.disablePanel();
var handler = AccessibilityMenuHandler.Instance;
if (handler != null)
handler.Activate();
else
DebugLogger.Error("[OptionPanelPatch] AccessibilityMenuHandler.Instance is null!");
return false;
}
catch (Exception ex)
{
DebugLogger.Error($"[OptionPanelPatch] SetMainSettingState_Prefix error: {ex.Message}");
return true;
}
}
/// <summary>
/// Blocks the game's CheckInputKey when:
/// 1. Our accessibility menu is open
/// 2. One-frame suppress after returning from our menu
/// </summary>
private static bool CheckInputKey_Prefix(uOptionPanel __instance)
{
try
{
_optionPanel = __instance;
// Block all game input while our menu is open
if (AccessibilityMenuHandler.Instance != null && AccessibilityMenuHandler.Instance.IsOpen())
return false;
// Suppress input briefly after returning from our menu
// (prevents the cancel press from closing the whole options panel)
if (Time.unscaledTime < _suppressUntilTime)
return false;
}
catch (Exception ex)
{
DebugLogger.Error($"[OptionPanelPatch] CheckInputKey_Prefix error: {ex.Message}");
}
return true;
}
/// <summary>
/// Called by AccessibilityMenuHandler when the user presses Cancel at the category level.
/// Returns to the game's TOP menu and suppresses one frame of input.
/// </summary>
public static void ReturnToTopMenu()
{
try
{
if (_optionPanel == null)
_optionPanel = UnityEngine.Object.FindObjectOfType<uOptionPanel>();
if (_optionPanel == null)
{
DebugLogger.Warning("[OptionPanelPatch] Cannot return to TOP - no uOptionPanel found");
return;
}
var topPanel = GetTopPanel(_optionPanel);
if (topPanel != null)
{
topPanel.enablePanel(true, false);
if (AccessibilityItemIndex >= 0)
topPanel.SetCursor(AccessibilityItemIndex);
}
// Suppress CheckInputKey briefly as extra safety
_suppressUntilTime = Time.unscaledTime + 0.2f;
// Announce return to TOP menu (matches game's announcement when
// backing out of Key Config/Graphics/etc.)
int total = topPanel?.m_KeyCursorController != null ? topPanel.m_KeyCursorController.m_DataMax : 0;
int index = AccessibilityItemIndex + 1; // 1-based for speech
string menuName = GetTopPanelCaption(topPanel);
ScreenReader.Say($"{menuName}. Accessibility, {index} of {total}");
DebugLogger.Log("[OptionPanelPatch] Returned to TOP menu");
}
catch (Exception ex)
{
DebugLogger.Error($"[OptionPanelPatch] ReturnToTopMenu error: {ex.Message}");
}
}
private static string GetTopPanelCaption(uOptionTopPanelCommand topPanel)
{
const string fallback = "System Menu";
try
{
if (topPanel == null)
{
DebugLogger.Log("[OptionPanelPatch] Top caption: top panel was null");
return fallback;
}
var captionPanel = topPanel.m_Caption;
if (captionPanel == null)
{
DebugLogger.Log("[OptionPanelPatch] Top caption: m_Caption was null");
return fallback;
}
var captionText = captionPanel.m_Caption;
if (captionText == null)
{
DebugLogger.Log("[OptionPanelPatch] Top caption: m_Caption.m_Caption was null");
return fallback;
}
string caption = TextUtilities.StripRichTextTags(ButtonHintCache.Filter(captionText.text))?.Trim();
if (string.IsNullOrWhiteSpace(caption) || TextUtilities.IsPlaceholderText(caption))
{
DebugLogger.Log($"[OptionPanelPatch] Top caption: m_Caption.m_Caption.text unusable: {TextUtilities.DescribeUnusable(caption)}");
return fallback;
}
return caption;
}
catch (Exception ex)
{
DebugLogger.Log($"[OptionPanelPatch] Top caption read failed: {ex.Message}");
return fallback;
}
}
private static uOptionTopPanelCommand GetTopPanel(uOptionPanel optionPanel)
{
try
{
if (_topPanel != null)
return _topPanel;
var panels = optionPanel.m_uOptionPanelCommand;
if (panels == null || panels.Length == 0)
return null;
var topBase = panels[(int)uOptionPanel.MainSettingState.TOP];
return topBase?.TryCast<uOptionTopPanelCommand>();
}
catch
{
return null;
}
}
}
}