Repository navigation
Expand file tree
/
Copy pathTextUtilities.cs
More file actions
313 lines (286 loc) · 13.1 KB
/
Copy pathTextUtilities.cs
File metadata and controls
313 lines (286 loc) · 13.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
using System.Text.RegularExpressions;
using Il2Cpp;
using UnityEngine;
namespace DigimonNOAccess
{
/// <summary>
/// Centralized text utility methods shared across all handlers.
/// Provides rich text stripping, text cleaning, placeholder detection,
/// localization readiness checks, and game loading state checks.
/// </summary>
public static class TextUtilities
{
// Control characters the game uses as button icon placeholders in rendered text.
// Language.ReplaceButtonIcon converts <icon> tags into these characters,
// which map to custom font glyphs that display as button images.
// We replace them with readable button names via ButtonIconResolver.
private static readonly (string character, string iconKey)[] _buttonPlaceholders = new[]
{
("\u0010", "L"), // DLE → L shoulder
("\u0011", "R"), // DC1 → R shoulder
("\u0012", "LS"), // DC2 → Left Stick
("\u0013", "RS"), // DC3 → Right Stick (probable)
("\u0018", "\u25CB"), // CAN → ○ Confirm
("\u0019", "\u00D7"), // EM → × Cancel (probable)
("\u001A", "\u25B3"), // SUB → △ Triangle (probable)
("\u001B", "\u25A1"), // ESC → □ Square (probable)
};
// Control characters the game uses as stat icon placeholders in messages.
// NGUI BMSymbol sequences mapped to sprite glyphs for stat/attribute icons.
// Appear in food eating results, education messages, and partner status text.
// Character code = ParamKind enum value (zero-indexed):
// Satiety=0, MaxHp=1, Defense=2, Mood=3, Weight=4, MaxMp=5,
// Wisdom=6, Education=7, Attack=8, Speed=9, Fatigue=10
private static readonly (string character, string name)[] _statIconPlaceholders = new[]
{
("\u0001", "Max HP"),
("\u0002", "Defense"),
("\u0003", "Mood"),
("\u0004", "Weight"),
("\u0005", "Max MP"),
("\u0006", "Wisdom"),
("\u0007", "Discipline"),
("\u0008", "Mood"),
("\u000B", "Speed"), // skip 0x09 Tab, 0x0A LF
("\u000C", "Fatigue"),
("\u000E", "Satiety"), // skip 0x0D CR; 0x00 is null so Satiety may use 0x0E
};
// Bare Unicode button symbols (fallback for any text that uses raw PS symbols)
private static readonly (string symbol, string key)[] _bareButtonSymbols = new[]
{
("\u25CB", "\u25CB"), // ○ Circle
("\u00D7", "\u00D7"), // × Multiplication sign
("\u25B3", "\u25B3"), // △ Triangle
("\u25A1", "\u25A1"), // □ Square
};
/// <summary>
/// Strips Unity rich text tags like <color>, <b>, etc. for clean screen reader output.
/// </summary>
/// <summary>
/// Why a piece of rendered text was not usable as a name, for logging.
///
/// The readers reject text for four different reasons and used to report all
/// of them as "was empty", which is a log line asserting a cause it has not
/// established. That actively misleads: a Digimon you have not met yet renders
/// as "???", which is punctuation-only and therefore a placeholder - the mod
/// saying "Unknown" there is correct behaviour, not a failure, and the log
/// should not imply the game handed us nothing.
/// </summary>
public static string DescribeUnusable(string text)
{
if (text == null) return "null";
if (text.Length == 0) return "empty";
if (string.IsNullOrWhiteSpace(text)) return "whitespace only";
if (IsButtonHintText(text)) return "button hint bar";
if (text.Trim() == "???") return "not yet discovered (???)";
if (IsPlaceholderText(text)) return $"placeholder text (\"{text.Trim()}\")";
return $"unexpected (\"{text.Trim()}\")";
}
/// <summary>
/// True when this text is the game's button-hint bar rather than a title.
///
/// The caption strip at the bottom of most screens is set through
/// uCaptionBase.SetCaptionNoWithButtonIcon, so its text always carries the
/// control characters that render as button glyphs. Titles never do. That
/// makes the two reliably distinguishable without hardcoding which screen is
/// which.
///
/// Pass the RAW text, before StripRichTextTags or any button-name
/// substitution, or the markers will already be gone.
/// </summary>
public static bool IsButtonHintText(string rawText)
{
if (string.IsNullOrEmpty(rawText))
return false;
foreach (var (character, _) in _buttonPlaceholders)
{
if (rawText.Contains(character))
return true;
}
return false;
}
/// <summary>
/// Returns the button-hint bar as speakable text, or null when this text is
/// not a hint bar. The button glyph characters are converted to readable
/// names, so "○ OK × Back" comes out as words rather than symbols.
///
/// Pass the RAW text, before StripRichTextTags.
/// </summary>
public static string ExtractButtonHints(string rawText)
{
if (!IsButtonHintText(rawText))
return null;
string cleaned = CleanText(rawText)?.Trim();
return string.IsNullOrWhiteSpace(cleaned) ? null : cleaned;
}
public static string StripRichTextTags(string text)
{
if (string.IsNullOrEmpty(text))
return text;
// Replace <icon>...</icon> with readable button names based on active input device
text = Regex.Replace(text, @"<icon>([^<]*)</icon>", m => ButtonIconResolver.ResolveIconTag(m.Groups[1].Value));
// Strip remaining rich text tags
text = Regex.Replace(text, @"<[^>]+>", "");
// Replace control character button placeholders (from Language.ReplaceButtonIcon)
foreach (var (character, iconKey) in _buttonPlaceholders)
{
if (text.Contains(character))
text = text.Replace(character, ButtonIconResolver.ResolveIconTag(iconKey));
}
// Replace bare Unicode button symbols (fallback)
foreach (var (symbol, key) in _bareButtonSymbols)
{
if (text.Contains(symbol))
text = text.Replace(symbol, ButtonIconResolver.ResolveIconTag(key));
}
// Replace stat icon placeholders (NGUI BMSymbol sequences for stat attributes)
foreach (var (character, name) in _statIconPlaceholders)
{
if (text.Contains(character))
text = text.Replace(character, name);
}
// Strip ・ bullet characters (screen reader reads them incorrectly)
text = text.Replace("\u30FB", "");
return text;
}
/// <summary>
/// Strip rich text tags, normalize whitespace, and trim.
/// Use this for any text that will be announced via screen reader.
/// </summary>
public static string CleanText(string text)
{
if (string.IsNullOrEmpty(text))
return "";
// Replace <icon>...</icon> with readable button names before stripping other tags
string cleaned = Regex.Replace(text, @"<icon>([^<]*)</icon>", m => ButtonIconResolver.ResolveIconTag(m.Groups[1].Value));
// Remove remaining Unity rich text tags
cleaned = Regex.Replace(cleaned, @"<[^>]+>", "");
// Replace control character button placeholders (from Language.ReplaceButtonIcon)
foreach (var (character, iconKey) in _buttonPlaceholders)
{
if (cleaned.Contains(character))
cleaned = cleaned.Replace(character, ButtonIconResolver.ResolveIconTag(iconKey));
}
// Replace bare Unicode button symbols (fallback)
foreach (var (symbol, key) in _bareButtonSymbols)
{
if (cleaned.Contains(symbol))
cleaned = cleaned.Replace(symbol, ButtonIconResolver.ResolveIconTag(key));
}
// Replace stat icon placeholders (NGUI BMSymbol sequences for stat attributes)
foreach (var (character, name) in _statIconPlaceholders)
{
if (cleaned.Contains(character))
cleaned = cleaned.Replace(character, name);
}
// Strip ・ bullet characters (screen reader reads them incorrectly)
cleaned = cleaned.Replace("\u30FB", "");
// Normalize whitespace
cleaned = Regex.Replace(cleaned, @"\s+", " ");
return cleaned.Trim();
}
/// <summary>
/// Reformats game item messages for more natural screen reader output.
/// Handles multiple game formats:
/// - "Duty Fruit x 1 received." → "Received 1 Duty Fruit."
/// - "You got Medicine x 2!" → "You got 2 Medicine!"
/// </summary>
public static string FormatItemMessage(string text)
{
if (string.IsNullOrEmpty(text))
return text;
// Match "ItemName x N received." pattern (field pickups)
var match = Regex.Match(text, @"^(.+?)\s+x\s+(\d+)\s+received\.\s*$");
if (match.Success)
{
string itemName = match.Groups[1].Value.Trim();
string count = match.Groups[2].Value;
return $"Received {count} {itemName}.";
}
// Match "You got ItemName x N!" pattern (NPC gifts)
match = Regex.Match(text, @"^(You got)\s+(.+?)\s+x\s+(\d+)!\s*$");
if (match.Success)
{
string prefix = match.Groups[1].Value;
string itemName = match.Groups[2].Value.Trim();
string count = match.Groups[3].Value;
return $"{prefix} {count} {itemName}!";
}
// General: swap any remaining "ItemName x N" to "N ItemName"
// Handles patterns like "Recovery Disk x 7 will all be discarded."
if (text.Contains(" x "))
return Regex.Replace(text, @"([\w'-]+(?:\s+[\w'-]+)*?)\s+x\s+(\d+)", "$2 $1");
return text;
}
/// <summary>
/// Check if text is a placeholder (localization keys, placeholder chars, system messages).
/// </summary>
public static bool IsPlaceholderText(string text)
{
if (string.IsNullOrEmpty(text))
return true;
// Skip Japanese placeholder characters (black/white squares)
if (text.Contains("■") || text.Contains("□"))
return true;
// Skip unresolved localization keys
if (text.StartsWith("EV_") || text.StartsWith("SYS_") || text.StartsWith("MSG_"))
return true;
// Skip "Language not found" error messages (Japanese)
if (text.Contains("ランゲージ"))
return true;
// Skip Japanese placeholder text "メッセージ入力欄" (Message input field)
if (text.Contains("メッセージ入力欄"))
return true;
// Skip punctuation-only text
string trimmed = text.Trim();
bool onlyPunctuation = true;
foreach (char c in trimmed)
{
if (!char.IsPunctuation(c) && !char.IsWhiteSpace(c))
{
onlyPunctuation = false;
break;
}
}
if (onlyPunctuation && trimmed.Length > 0)
return true;
return false;
}
/// <summary>
/// Check if game localization system is ready.
/// Returns false if Localization is not yet active or on any exception.
/// </summary>
public static bool IsLocalizationReady()
{
try
{
return Localization.isActive;
}
catch
{
return false;
}
}
/// <summary>
/// Check if game is in a loading state.
/// Returns true only when MainGameManager exists and _IsLoad() is true.
/// Returns false when MainGameManager is null (title/copyright screen)
/// so that pre-title text can be announced. Other filters (IsPlaceholderText,
/// IsLocalizationReady) handle garbage text at that stage.
/// </summary>
public static bool IsGameLoading()
{
try
{
var mgr = MainGameManager.m_instance;
if (mgr == null)
return false;
return mgr._IsLoad();
}
catch
{
return false;
}
}
}
}