-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMailPanelHandler.cs
More file actions
800 lines (702 loc) · 27.1 KB
/
Copy pathMailPanelHandler.cs
File metadata and controls
800 lines (702 loc) · 27.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
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
using Il2Cpp;
using UnityEngine;
namespace DigimonNOAccess
{
/// <summary>
/// Handles accessibility for the mail/digital messenger panel.
/// Tracks the uDigiviceDigimePanel state machine to handle folder selection,
/// mail list browsing (with title/sender/status), and body reading.
/// </summary>
public class MailPanelHandler : IAccessibilityHandler
{
private const string LogTag = "[MailPanel]";
public int Priority => 65;
private uDigiviceDigimePanel _digimePanel;
private uMailPanel _mailPanel;
private bool _wasActive;
private uDigiviceDigimePanel.State _lastState = uDigiviceDigimePanel.State.NONE;
private int _lastCursor = -1;
private int _lastTab = -1;
private bool _bodyAnnounced;
private bool _pendingFirstMail;
private UnityEngine.UI.Text _mailMenuNameText;
private string _mailMenuNameFailure;
private float _nextMailMenuNameLookupTime;
private const float MailMenuNameLookupInterval = 0.5f;
private DigitalMessengerManager.SortType _lastSortType = DigitalMessengerManager.SortType.NEW;
public bool IsOpen()
{
try
{
if (_digimePanel == null)
return false;
var state = _digimePanel.m_CurrentState;
return state != uDigiviceDigimePanel.State.NONE
&& state != uDigiviceDigimePanel.State.CLOSE
&& state != uDigiviceDigimePanel.State.MAX;
}
catch
{
_digimePanel = null;
return false;
}
}
public void Update()
{
TryCacheMailMenuName();
if (_digimePanel == null)
{
try
{
_digimePanel = Object.FindObjectOfType<uDigiviceDigimePanel>();
if (_digimePanel != null)
_mailPanel = _digimePanel.m_mailPanel;
}
catch { return; }
}
bool isActive = IsOpen();
if (isActive && !_wasActive)
OnOpen();
else if (!isActive && _wasActive)
OnClose();
else if (isActive)
OnUpdate();
_wasActive = isActive;
}
private void OnOpen()
{
_lastState = uDigiviceDigimePanel.State.NONE;
_lastCursor = -1;
_lastTab = -1;
_bodyAnnounced = false;
_pendingFirstMail = false;
_lastSortType = _mailPanel?.m_SortType ?? DigitalMessengerManager.SortType.NEW;
if (_digimePanel == null)
return;
_mailPanel = _digimePanel.m_mailPanel;
var state = _digimePanel.m_CurrentState;
_lastState = state;
string mailMenuName = GetMailMenuName();
string announcement = mailMenuName;
if (state == uDigiviceDigimePanel.State.SELECT_FOLDER && _mailPanel != null)
{
int tab = _mailPanel.m_SelectTab;
_lastTab = tab;
var (pos, total) = GetTabPosition(tab);
string tabName = GetTabName(tab);
announcement = $"{mailMenuName}, {tabName}, {pos} of {total}";
}
else if (state == uDigiviceDigimePanel.State.SELECT_MAIL && _mailPanel != null)
{
int tab = _mailPanel.m_SelectTab;
_lastTab = tab;
_pendingFirstMail = true;
}
ScreenReader.Say(announcement);
DebugLogger.Log($"{LogTag} Opened, state={state}");
}
private void OnClose()
{
_mailPanel = null;
_lastState = uDigiviceDigimePanel.State.NONE;
_lastCursor = -1;
_lastTab = -1;
_bodyAnnounced = false;
_pendingFirstMail = false;
_lastSortType = DigitalMessengerManager.SortType.NEW;
DebugLogger.Log($"{LogTag} Closed");
}
private void OnUpdate()
{
CheckStateChange();
var state = _digimePanel?.m_CurrentState ?? uDigiviceDigimePanel.State.NONE;
if (state == uDigiviceDigimePanel.State.SELECT_FOLDER)
{
CheckTabChange();
}
else if (state == uDigiviceDigimePanel.State.SELECT_MAIL)
{
CheckTabChange();
CheckSortChange();
if (_pendingFirstMail)
TryAnnounceFirstMail();
else
CheckCursorChange();
}
else if (state == uDigiviceDigimePanel.State.LOOK_BODY && !_bodyAnnounced)
{
TryAnnounceBody();
}
}
private void CheckStateChange()
{
if (_digimePanel == null)
return;
var currentState = _digimePanel.m_CurrentState;
if (currentState == _lastState)
return;
var prevState = _lastState;
_lastState = currentState;
switch (currentState)
{
case uDigiviceDigimePanel.State.SELECT_FOLDER:
_pendingFirstMail = false;
if (_mailPanel != null)
{
int tab = _mailPanel.m_SelectTab;
_lastTab = tab;
var (pos, total) = GetTabPosition(tab);
ScreenReader.Say($"{GetTabName(tab)}, {pos} of {total}");
}
break;
case uDigiviceDigimePanel.State.SELECT_MAIL:
_bodyAnnounced = false;
_pendingFirstMail = true;
_lastCursor = -1;
break;
case uDigiviceDigimePanel.State.LOOK_BODY:
_bodyAnnounced = false;
_pendingFirstMail = false;
TryAnnounceBody();
break;
case uDigiviceDigimePanel.State.GET_ITEM:
_pendingFirstMail = false;
ScreenReader.Say("Claiming attachment");
break;
}
DebugLogger.Log($"{LogTag} State: {prevState} -> {currentState}");
}
private void CheckSortChange()
{
if (_mailPanel == null)
return;
var currentSort = _mailPanel.m_SortType;
if (currentSort == _lastSortType)
return;
_lastSortType = currentSort;
string sortName = GetSortName();
int total = GetMailCount();
if (total > 0)
{
int cursor = _mailPanel.m_selectNo;
_lastCursor = cursor;
_pendingFirstMail = false;
string mailInfo = GetCurrentMailInfo();
if (!string.IsNullOrEmpty(mailInfo))
ScreenReader.Say($"Sorted by {sortName}. {mailInfo}, {cursor + 1} of {total}");
else
ScreenReader.Say($"Sorted by {sortName}. {cursor + 1} of {total}");
}
else
{
ScreenReader.Say($"Sorted by {sortName}");
}
DebugLogger.Log($"{LogTag} Sort changed to {sortName}");
}
private string GetSortName()
{
try
{
if (_mailPanel == null)
{
DebugLogger.Log($"{LogTag} Sort: mail panel is null");
}
else
{
var sortText = _mailPanel.m_SortText;
if (sortText == null)
{
DebugLogger.Log($"{LogTag} Sort: m_SortText is null");
}
else
{
string text = TextUtilities.StripRichTextTags(ButtonHintCache.Filter(sortText.text))?.Trim();
if (!string.IsNullOrWhiteSpace(text))
return text;
DebugLogger.Log($"{LogTag} Sort: m_SortText.text is empty");
}
}
}
catch (System.Exception ex)
{
DebugLogger.Log($"{LogTag} Sort text read failed: {ex.Message}");
}
return _mailPanel?.m_SortType switch
{
DigitalMessengerManager.SortType.NEW => "Newest",
DigitalMessengerManager.SortType.OLD => "Oldest",
DigitalMessengerManager.SortType.CHARACTER => "Sender",
_ => "Sort"
};
}
private void TryAnnounceFirstMail()
{
if (_mailPanel == null)
return;
int total = GetMailCount();
if (total == 0)
return; // List not populated yet, retry next frame
_pendingFirstMail = false;
int cursor = _mailPanel.m_selectNo;
_lastCursor = cursor;
string mailInfo = GetCurrentMailInfo();
if (!string.IsNullOrEmpty(mailInfo))
ScreenReader.Say($"{mailInfo}, {cursor + 1} of {total}");
else
ScreenReader.Say($"{cursor + 1} of {total}");
}
private void TryAnnounceBody()
{
try
{
var msgWindow = _digimePanel?.m_messageWindow;
if (msgWindow == null)
return;
var texts = msgWindow.m_text;
if (texts == null)
return;
string sender = GetTextAt(texts, (int)uDigiviceDigimePanelMessage_Window.TextIndex.SENDER_NAME);
string title = GetTextAt(texts, (int)uDigiviceDigimePanelMessage_Window.TextIndex.TITLE);
string body = GetTextAt(texts, (int)uDigiviceDigimePanelMessage_Window.TextIndex.BODY);
if (!string.IsNullOrEmpty(body))
{
var parts = new System.Collections.Generic.List<string>();
if (!string.IsNullOrEmpty(sender))
parts.Add($"From {sender}");
if (!string.IsNullOrEmpty(title))
parts.Add(title);
parts.Add(body);
ScreenReader.Say(string.Join(". ", parts));
_bodyAnnounced = true;
DebugLogger.Log($"{LogTag} Body announced: {title}");
}
}
catch (System.Exception ex)
{
DebugLogger.Log($"{LogTag} Error reading body: {ex.Message}");
}
}
private string GetTextAt(Il2CppInterop.Runtime.InteropTypes.Arrays.Il2CppReferenceArray<UnityEngine.UI.Text> texts, int index)
{
if (texts == null || index < 0 || index >= texts.Length)
return null;
var textComp = texts[index];
if (textComp == null)
return null;
string raw = textComp.text;
if (string.IsNullOrEmpty(raw))
return null;
return TextUtilities.StripRichTextTags(raw);
}
private void CheckTabChange()
{
if (_mailPanel == null)
return;
int currentTab = _mailPanel.m_SelectTab;
if (currentTab != _lastTab && _lastTab >= 0)
{
_lastCursor = -1;
_pendingFirstMail = false;
string tabName = GetTabName(currentTab);
var digimeState = _digimePanel?.m_CurrentState ?? uDigiviceDigimePanel.State.NONE;
if (digimeState == uDigiviceDigimePanel.State.SELECT_FOLDER)
{
var (pos, total) = GetTabPosition(currentTab);
ScreenReader.Say($"{tabName}, {pos} of {total}");
}
else
{
int total = GetMailCount();
if (total == 0)
{
ScreenReader.Say($"{tabName}, empty");
}
else
{
int cursor = _mailPanel.m_selectNo;
_lastCursor = cursor;
string mailInfo = GetCurrentMailInfo();
if (!string.IsNullOrEmpty(mailInfo))
ScreenReader.Say($"{tabName}. {mailInfo}, {cursor + 1} of {total}");
else
ScreenReader.Say($"{tabName}. {cursor + 1} of {total}");
}
}
DebugLogger.Log($"{LogTag} Tab changed to {tabName}");
_lastTab = currentTab;
}
else if (_lastTab < 0)
{
_lastTab = currentTab;
}
}
private void CheckCursorChange()
{
if (_mailPanel == null)
return;
int currentCursor = _mailPanel.m_selectNo;
if (currentCursor != _lastCursor)
{
if (_lastCursor >= 0)
{
int total = GetMailCount();
if (total == 0)
{
ScreenReader.Say("Empty");
}
else
{
string mailInfo = GetCurrentMailInfo();
if (!string.IsNullOrEmpty(mailInfo))
ScreenReader.Say($"{mailInfo}, {currentCursor + 1} of {total}");
else
ScreenReader.Say($"{currentCursor + 1} of {total}");
}
}
_lastCursor = currentCursor;
}
}
private string GetCurrentMailInfo()
{
try
{
if (_mailPanel == null)
return null;
var parts = new System.Collections.Generic.List<string>();
// Read title directly from UI text (most reliable - this is what the game renders)
try
{
var nameText = _mailPanel.m_panel3ItemNameText;
if (nameText != null)
{
string title = nameText.text;
if (!string.IsNullOrEmpty(title))
parts.Add(TextUtilities.StripRichTextTags(title));
}
}
catch (System.Exception ex)
{
DebugLogger.Log($"{LogTag} Title from UI error: {ex.Message}");
}
// Get mail ID from the selected item for sender/status lookups
int mailId = -1;
try
{
var selectItem = _mailPanel.m_selectItem;
if (selectItem != null)
mailId = selectItem.m_mailID;
}
catch { }
// Fallback: try GetSelectMail
if (mailId < 0)
{
try { mailId = _mailPanel.GetSelectMail(); }
catch { }
}
if (mailId >= 0)
{
var manager = DigitalMessengerManager.Ref;
if (manager != null)
{
// If we didn't get a title from UI, try deserializing
if (parts.Count == 0)
{
try
{
manager.DeserializeMail(mailId);
var deserializer = manager.m_MailTextDeserialize;
if (deserializer != null)
{
string title = deserializer.title;
if (!string.IsNullOrEmpty(title))
parts.Add(TextUtilities.StripRichTextTags(title));
}
}
catch { }
}
// Sender
try
{
string sender = manager.GetSenderName(mailId);
if (!string.IsNullOrEmpty(sender))
parts.Add($"from {TextUtilities.StripRichTextTags(sender)}");
}
catch { }
// Unread
try
{
if (!manager.IsReadMail(mailId))
parts.Add("new");
}
catch { }
// Attachment
try
{
if (manager.IsRemainingAttachedData(mailId))
parts.Add("has attachment");
}
catch { }
}
}
return parts.Count > 0 ? string.Join(", ", parts) : null;
}
catch (System.Exception ex)
{
DebugLogger.Log($"{LogTag} Error getting mail info: {ex.Message}");
return null;
}
}
private (int pos, int total) GetTabPosition(int tabIndex)
{
try
{
var tabInfoTbl = _mailPanel?.m_SelectTabInfoTbl;
if (tabInfoTbl == null)
return (tabIndex + 1, 4);
int total = 0;
int pos = 0;
for (int i = 0; i < tabInfoTbl.Length; i++)
{
var tabInfo = tabInfoTbl[i];
if (tabInfo != null && tabInfo.m_isEnable)
{
total++;
if (i == tabIndex)
pos = total;
}
}
return (pos > 0 ? pos : tabIndex + 1, total > 0 ? total : tabInfoTbl.Length);
}
catch
{
return (tabIndex + 1, 4);
}
}
private string GetTabName(int tabIndex)
{
try
{
if (_mailPanel == null)
{
DebugLogger.Log($"{LogTag} Folder {tabIndex}: mail panel is null");
}
else
{
var tabInfoTbl = _mailPanel.m_SelectTabInfoTbl;
if (tabInfoTbl == null)
{
DebugLogger.Log($"{LogTag} Folder {tabIndex}: m_SelectTabInfoTbl is null");
}
else if (tabIndex < 0 || tabIndex >= tabInfoTbl.Length)
{
DebugLogger.Log($"{LogTag} Folder {tabIndex}: index is outside m_SelectTabInfoTbl");
}
else
{
var tabInfo = tabInfoTbl[tabIndex];
if (tabInfo == null)
{
DebugLogger.Log($"{LogTag} Folder {tabIndex}: SelectTabInfo is null");
}
else
{
var tabText = tabInfo.m_TabText;
if (tabText == null)
{
DebugLogger.Log($"{LogTag} Folder {tabIndex}: m_TabText is null");
}
else
{
string text = TextUtilities.StripRichTextTags(ButtonHintCache.Filter(tabText.text))?.Trim();
if (!string.IsNullOrWhiteSpace(text))
return text;
DebugLogger.Log($"{LogTag} Folder {tabIndex}: m_TabText.text is empty");
}
}
}
}
}
catch (System.Exception ex)
{
DebugLogger.Log($"{LogTag} Error getting tab name: {ex.Message}");
}
return tabIndex switch
{
0 => "Main",
1 => "Sub",
2 => "DLC",
3 => "Daily Quest",
_ => AnnouncementBuilder.FallbackItem("Folder", tabIndex)
};
}
private void TryCacheMailMenuName()
{
if (_mailMenuNameText != null)
{
try
{
if (_mailMenuNameText.gameObject != null)
return;
LogMailMenuNameFailure("cached DigiMessenger m_headText.gameObject is null");
}
catch (System.Exception ex)
{
LogMailMenuNameFailure($"cached DigiMessenger m_headText validation failed: {ex.Message}");
}
_mailMenuNameText = null;
}
float now = Time.unscaledTime;
if (now < _nextMailMenuNameLookupTime)
return;
_nextMailMenuNameLookupTime = now + MailMenuNameLookupInterval;
try
{
var topPanel = Object.FindObjectOfType<uDigiviceTopPanel>();
if (topPanel == null)
{
LogMailMenuNameFailure("no uDigiviceTopPanel was found");
return;
}
var command = topPanel.m_Command;
if (command == null)
{
LogMailMenuNameFailure("top panel m_Command is null");
return;
}
var items = command.m_items;
if (items == null)
{
LogMailMenuNameFailure("m_Command.m_items is null");
return;
}
int index = (int)uDigiviceTopPanelCommand.Command.DigiMessenger;
if (index < 0 || index >= items.Length)
{
LogMailMenuNameFailure($"DigiMessenger index {index} is outside m_items");
return;
}
var item = items[index];
if (item == null)
{
LogMailMenuNameFailure("DigiMessenger item is null");
return;
}
var headText = item.m_headText;
if (headText == null)
{
LogMailMenuNameFailure("DigiMessenger m_headText is null");
return;
}
var headTextObject = headText.gameObject;
if (headTextObject == null)
{
LogMailMenuNameFailure("DigiMessenger m_headText.gameObject is null");
return;
}
_mailMenuNameText = headText;
_mailMenuNameFailure = null;
}
catch (System.Exception ex)
{
LogMailMenuNameFailure($"read failed: {ex.Message}");
}
}
private void LogMailMenuNameFailure(string reason)
{
if (_mailMenuNameFailure == reason)
return;
_mailMenuNameFailure = reason;
DebugLogger.Log($"{LogTag} Menu name: {reason}");
}
private string GetMailMenuName()
{
const string fallback = "Mail";
try
{
if (_mailMenuNameText == null)
{
LogMailMenuNameFailure("DigiMessenger m_headText reference is unavailable");
return fallback;
}
var headTextObject = _mailMenuNameText.gameObject;
if (headTextObject == null)
{
LogMailMenuNameFailure("cached DigiMessenger m_headText.gameObject is null");
_mailMenuNameText = null;
return fallback;
}
string text = TextUtilities.StripRichTextTags(ButtonHintCache.Filter(_mailMenuNameText.text))?.Trim();
if (string.IsNullOrWhiteSpace(text) || TextUtilities.IsPlaceholderText(text))
{
LogMailMenuNameFailure("DigiMessenger m_headText.text is empty");
return fallback;
}
_mailMenuNameFailure = null;
return text;
}
catch (System.Exception ex)
{
LogMailMenuNameFailure($"cached DigiMessenger m_headText read failed: {ex.Message}");
_mailMenuNameText = null;
return fallback;
}
}
private int GetMailCount()
{
try
{
// Use m_itemList from uItemBase - this is the actual display list
var itemList = _mailPanel?.m_itemList;
if (itemList != null && itemList.Count > 0)
return itemList.Count;
// Fallback to mail-specific list
var mailList = _mailPanel?.m_ListMailData;
if (mailList != null)
return mailList.Count;
}
catch (System.Exception ex)
{
DebugLogger.Log($"{LogTag} Error getting mail count: {ex.Message}");
}
return 0;
}
public void AnnounceStatus()
{
if (!IsOpen())
return;
var state = _digimePanel?.m_CurrentState ?? uDigiviceDigimePanel.State.NONE;
if (state == uDigiviceDigimePanel.State.LOOK_BODY)
{
ScreenReader.Say($"{GetMailMenuName()}, reading message");
return;
}
if (_mailPanel == null)
{
ScreenReader.Say(GetMailMenuName());
return;
}
int tab = _mailPanel.m_SelectTab;
string tabName = GetTabName(tab);
if (state == uDigiviceDigimePanel.State.SELECT_FOLDER)
{
var (pos, total) = GetTabPosition(tab);
ScreenReader.Say($"{GetMailMenuName()}, {tabName}, {pos} of {total}");
return;
}
int cursor = _mailPanel.m_selectNo;
int mailTotal = GetMailCount();
if (mailTotal == 0)
{
ScreenReader.Say($"{GetMailMenuName()}, {tabName}, empty");
}
else
{
string mailInfo = GetCurrentMailInfo();
if (!string.IsNullOrEmpty(mailInfo))
ScreenReader.Say($"{GetMailMenuName()}, {tabName}. {mailInfo}, {cursor + 1} of {mailTotal}");
else
ScreenReader.Say($"{GetMailMenuName()}, {tabName}. {cursor + 1} of {mailTotal}");
}
}
}
}