forked from potatosalad775/MWC_Localization_Core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnifiedTextMeshMonitor.cs
More file actions
461 lines (391 loc) · 18 KB
/
UnifiedTextMeshMonitor.cs
File metadata and controls
461 lines (391 loc) · 18 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
using MSCLoader;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace MWC_Localization_Core
{
/// <summary>
/// Represents a TextMesh component being monitored for translation
/// </summary>
public class TextMeshEntry
{
public TextMesh TextMesh;
public GameObject GameObject;
public string Path;
public MonitoringStrategy Strategy;
public string LastText;
public bool WasTranslated;
public bool IsVisible;
public TextMeshEntry(TextMesh textMesh, string path, MonitoringStrategy strategy)
{
TextMesh = textMesh;
GameObject = textMesh != null ? textMesh.gameObject : null;
Path = path;
Strategy = strategy;
LastText = textMesh != null ? textMesh.text : "";
WasTranslated = false;
IsVisible = GameObject != null && GameObject.activeInHierarchy;
}
public bool IsValid()
{
return TextMesh != null && GameObject != null;
}
public bool HasTextChanged()
{
if (TextMesh == null || string.IsNullOrEmpty(TextMesh.text))
return false;
return LastText != TextMesh.text;
}
public void UpdateLastText()
{
if (TextMesh != null)
{
LastText = TextMesh.text;
}
}
public void UpdateVisibility()
{
IsVisible = GameObject != null && GameObject.activeInHierarchy;
}
}
/// <summary>
/// Unified monitoring system for all TextMesh translation
/// Replaces UpdatePriorityTextMeshes, UpdateDynamicTextMeshes, and FSM monitoring
/// </summary>
public class UnifiedTextMeshMonitor
{
private TextMeshTranslator translator;
// Throttling timers
private float fastPollingTimer;
private float slowPollingTimer;
private float visibilityPollingTimer;
// Path-based monitoring rules (pattern -> strategy mapping)
private Dictionary<string, MonitoringStrategy> pathRules;
// Instance-based storage (supports multiple TextMeshes per path)
private Dictionary<int, TextMeshEntry> instanceEntries; // instanceID -> entry
private Dictionary<string, HashSet<int>> pathToInstances; // path -> instanceIDs
private Dictionary<MonitoringStrategy, HashSet<int>> strategyGroups; // strategy -> instanceIDs
private HashSet<string> monitoredPaths = new HashSet<string>();
private List<int> removalBuffer = new List<int>(64);
public UnifiedTextMeshMonitor(TextMeshTranslator translator)
{
this.translator = translator;
pathRules = new Dictionary<string, MonitoringStrategy>();
instanceEntries = new Dictionary<int, TextMeshEntry>();
pathToInstances = new Dictionary<string, HashSet<int>>();
strategyGroups = new Dictionary<MonitoringStrategy, HashSet<int>>();
// Initialize strategy groups
foreach (MonitoringStrategy strategy in System.Enum.GetValues(typeof(MonitoringStrategy)))
{
strategyGroups[strategy] = new HashSet<int>();
}
InitializeDefaultPathRules();
}
private void InitializeDefaultPathRules()
{
// Critical UI - every frame
AddPathRule("GUI/Indicators/Interaction", MonitoringStrategy.EveryFrame);
AddPathRule("GUI/Indicators/Interaction/Shadow", MonitoringStrategy.EveryFrame);
AddPathRule("GUI/Indicators/Partname", MonitoringStrategy.EveryFrame);
AddPathRule("GUI/Indicators/Partname/Shadow", MonitoringStrategy.EveryFrame);
AddPathRule("GUI/Indicators/Subtitles", MonitoringStrategy.EveryFrame);
AddPathRule("GUI/Indicators/Subtitles/Shadow", MonitoringStrategy.EveryFrame);
AddPathRule("GUI/Indicators/TaxiGUI", MonitoringStrategy.EveryFrame);
AddPathRule("GUI/Indicators/TaxiGUI/Shadow", MonitoringStrategy.EveryFrame);
AddPathRule("GUI/Indicators/Gear", MonitoringStrategy.EveryFrame);
AddPathRule("GUI/Indicators/Gear/Shadow", MonitoringStrategy.EveryFrame);
AddPathRule("GUI/HUD/Thrist/HUDLabel", MonitoringStrategy.EveryFrame);
AddPathRule("GUI/HUD/Thrist/HUDLabel/Shadow", MonitoringStrategy.EveryFrame);
// Active HUD - fast polling (10 FPS)
AddPathRule("GUI/HUD/Mortal/HUDValue", MonitoringStrategy.FastPolling);
AddPathRule("GUI/HUD/Day/HUDValue", MonitoringStrategy.FastPolling);
AddPathRule("GUI/HUD/Thirst/HUDValue", MonitoringStrategy.FastPolling);
AddPathRule("GUI/HUD/Hunger/HUDValue", MonitoringStrategy.FastPolling);
AddPathRule("GUI/HUD/Stress/HUDValue", MonitoringStrategy.FastPolling);
AddPathRule("GUI/HUD/Urine/HUDValue", MonitoringStrategy.FastPolling);
AddPathRule("GUI/HUD/Fatigue/HUDValue", MonitoringStrategy.FastPolling);
AddPathRule("GUI/HUD/Money/HUDValue", MonitoringStrategy.FastPolling);
AddPathRule("GUI/HUD/Bodytemp/HUDValue", MonitoringStrategy.FastPolling);
AddPathRule("GUI/HUD/Sweat/HUDValue", MonitoringStrategy.FastPolling);
AddPathRule("GUI/HUD/Jailtime/HUDValue", MonitoringStrategy.FastPolling);
AddPathRule("Systems/TV/TVGraphics/CHAT/Day", MonitoringStrategy.FastPolling);
AddPathRule("Systems/TV/TVGraphics/CHAT/Moderator", MonitoringStrategy.FastPolling);
AddPathRule("Systems/TV/Teletext/VKTekstiTV/HEADER/Texts/Status", MonitoringStrategy.FastPolling);
// Teletext/FSM displays are primarily translated at array/FSM source level.
// Use one-shot late registration to avoid scanning large TV trees every second.
AddPathRule("Systems/TV/Teletext/VKTekstiTV/PAGES", MonitoringStrategy.LateTranslateOnce);
AddPathRule("Systems/TV/TVGraphics/CHAT/Generated", MonitoringStrategy.LateTranslateOnce);
// Magazine / Sheets - on visibility change
AddPathRule("Sheets/UnemployPaper", MonitoringStrategy.OnVisibilityChange);
AddPathRule("Sheets/ServiceBrochure", MonitoringStrategy.OnVisibilityChange);
AddPathRule("Sheets/ServicePayment", MonitoringStrategy.OnVisibilityChange);
AddPathRule("Sheets/TrafficTicket", MonitoringStrategy.OnVisibilityChange);
AddPathRule("COMPUTER/SYSTEM/TELEBBS/CONLINE/CommandLine", MonitoringStrategy.OnVisibilityChange);
// Magazine / Sheets - persistent monitoring due to dynamic content changes and rebuilds
AddPathRule("Sheets/YellowPagesMagazine/Page1", MonitoringStrategy.Persistent);
AddPathRule("Sheets/YellowPagesMagazine/Page2", MonitoringStrategy.Persistent);
AddPathRule("PERAPORTTI/ActiveFunctions/ATMs/MoneyATM/Screen/Tapahtumat", MonitoringStrategy.Persistent);
AddPathRule("COMPUTER/SYSTEM/POS/NoOS", MonitoringStrategy.Persistent);
}
public void AddPathRule(string pathPattern, MonitoringStrategy strategy)
{
pathRules[pathPattern] = strategy;
}
/// <summary>
/// Determine monitoring strategy for a given path
/// Matches most specific (longest) rule first to avoid ambiguity
/// </summary>
public MonitoringStrategy DetermineStrategy(string path)
{
string longestMatch = null;
MonitoringStrategy matchedStrategy = MonitoringStrategy.TranslateOnce;
// Find the longest (most specific) matching rule
foreach (var rule in pathRules)
{
if (path.Contains(rule.Key))
{
// Use longest match (most specific)
if (longestMatch == null || rule.Key.Length > longestMatch.Length)
{
longestMatch = rule.Key;
matchedStrategy = rule.Value;
}
}
}
return matchedStrategy;
}
/// <summary>
/// Register all TextMeshes under defined path rules
/// </summary>
public void RegisterAllPathRuleElements()
{
foreach (string parentPath in pathRules.Keys)
{
MonitoringStrategy strategy = pathRules[parentPath];
if (strategy == MonitoringStrategy.LateTranslateOnce ||
strategy == MonitoringStrategy.OnVisibilityChange ||
strategy == MonitoringStrategy.Persistent)
{
monitoredPaths.Add(parentPath);
}
Register(parentPath, strategy);
}
}
/// <summary>
/// Periodically monitor TextMeshes to be available for monitoring
/// </summary>
public void MonitorLateRegister()
{
// Use ToList() to avoid modifying collection during iteration
foreach (string parentPath in monitoredPaths.ToList())
{
MonitoringStrategy strategy = pathRules.ContainsKey(parentPath) ? pathRules[parentPath] : MonitoringStrategy.LateTranslateOnce;
int registered = Register(parentPath, strategy);
// Remove from monitoring list if successfully registered.
// Persistent paths stay in the scan because their child TextMeshes can be rebuilt.
if (registered > 0 && strategy != MonitoringStrategy.Persistent)
{
monitoredPaths.Remove(parentPath);
}
}
}
/// <summary>
/// Register a TextMesh for monitoring
/// Supports multiple TextMeshes at the same path
/// </summary>
public int Register(string parentPath, MonitoringStrategy? strategy = null)
{
int registeredCount = 0;
// Determine strategy if not provided
MonitoringStrategy finalStrategy = strategy ?? pathRules[parentPath];
GameObject parent = MLCUtils.FindGameObjectCached(parentPath);
if (parent == null)
return registeredCount;
// Get all TextMesh components under this parent
TextMesh[] textMeshes = parent.GetComponentsInChildren<TextMesh>(true);
foreach (var textMesh in textMeshes)
{
if (textMesh == null)
continue;
int instanceID = textMesh.GetInstanceID();
// Skip if this specific instance is already registered
if (instanceEntries.ContainsKey(instanceID))
continue;
string textMeshPath = MLCUtils.GetGameObjectPath(textMesh.gameObject);
// Translate first to reduce visible unlocalized text
bool translated = translator.TranslateAndApplyFont(textMesh, textMeshPath, null);
TextMeshEntry entry = new TextMeshEntry(textMesh, textMeshPath, finalStrategy);
if (translated)
{
entry.WasTranslated = true;
entry.UpdateLastText();
}
// Register instance
instanceEntries[instanceID] = entry;
// Index by path
if (!pathToInstances.ContainsKey(textMeshPath))
pathToInstances[textMeshPath] = new HashSet<int>();
pathToInstances[textMeshPath].Add(instanceID);
// Group by strategy
strategyGroups[finalStrategy].Add(instanceID);
registeredCount++;
}
return registeredCount;
}
/// <summary>
/// Unregister a TextMesh from monitoring by path
/// Removes all TextMesh instances at the given path
/// </summary>
public void Unregister(string path)
{
if (path == null || !pathToInstances.ContainsKey(path))
return;
var instanceIDs = pathToInstances[path];
foreach (int instanceID in instanceIDs)
{
if (!instanceEntries.ContainsKey(instanceID))
continue;
var entry = instanceEntries[instanceID];
strategyGroups[entry.Strategy].Remove(instanceID);
instanceEntries.Remove(instanceID);
}
pathToInstances.Remove(path);
}
/// <summary>
/// Unregister a specific TextMesh instance
/// </summary>
public void UnregisterInstance(int instanceID)
{
if (!instanceEntries.ContainsKey(instanceID))
return;
var entry = instanceEntries[instanceID];
// Remove from strategy group
strategyGroups[entry.Strategy].Remove(instanceID);
// Remove from path index
if (pathToInstances.ContainsKey(entry.Path))
{
pathToInstances[entry.Path].Remove(instanceID);
if (pathToInstances[entry.Path].Count == 0)
pathToInstances.Remove(entry.Path);
}
// Remove instance
instanceEntries.Remove(instanceID);
}
/// <summary>
/// Main update loop - called every frame
/// </summary>
public void Update(float deltaTime)
{
// Always update EveryFrame
UpdateGroup(MonitoringStrategy.EveryFrame);
// Throttled fast polling (0.1s)
fastPollingTimer += deltaTime;
if (fastPollingTimer >= LocalizationConstants.FAST_POLLING_INTERVAL)
{
UpdateGroup(MonitoringStrategy.FastPolling);
UpdateGroup(MonitoringStrategy.Persistent);
fastPollingTimer = 0f;
}
// Throttled slow polling (1.0s)
slowPollingTimer += deltaTime;
if (slowPollingTimer >= LocalizationConstants.SLOW_POLLING_INTERVAL)
{
UpdateGroup(MonitoringStrategy.SlowPolling);
UpdateGroup(MonitoringStrategy.LateTranslateOnce);
MonitorLateRegister(); // Also check for late registrations
slowPollingTimer = 0f;
}
// OnVisibilityChange - check at a throttled interval
visibilityPollingTimer += deltaTime;
if (visibilityPollingTimer >= LocalizationConstants.VISIBILITY_POLLING_INTERVAL)
{
UpdateVisibilityChangeGroup();
visibilityPollingTimer = 0f;
}
}
private void UpdateGroup(MonitoringStrategy strategy)
{
var instanceIDs = strategyGroups[strategy];
removalBuffer.Clear();
foreach (int instanceID in instanceIDs)
{
if (!instanceEntries.ContainsKey(instanceID))
continue;
var entry = instanceEntries[instanceID];
// Skip inactive entries for polling strategies.
// OnVisibilityChange has its own dedicated pass.
if (!entry.GameObject.activeInHierarchy)
continue;
// Persistent strategy: Keep checking even if entry is not valid
// Other strategies: Remove if entry becomes invalid (e.g. destroyed)
if (!entry.IsValid() && strategy != MonitoringStrategy.Persistent)
{
removalBuffer.Add(instanceID);
continue;
}
bool textChanged = entry.HasTextChanged();
if (textChanged || !entry.WasTranslated)
{
bool translated = translator.TranslateAndApplyFont(entry.TextMesh, entry.Path, null);
if (translated)
{
entry.WasTranslated = true;
entry.UpdateLastText();
// Mark for removal if TranslateOnce
if (strategy == MonitoringStrategy.TranslateOnce || strategy == MonitoringStrategy.LateTranslateOnce)
{
removalBuffer.Add(instanceID);
}
}
}
}
// Clean up removed instances
foreach (int instanceID in removalBuffer)
{
UnregisterInstance(instanceID);
}
}
private void UpdateVisibilityChangeGroup()
{
var instanceIDs = strategyGroups[MonitoringStrategy.OnVisibilityChange];
foreach (int instanceID in instanceIDs)
{
if (!instanceEntries.ContainsKey(instanceID))
continue;
var entry = instanceEntries[instanceID];
if (!entry.IsValid())
continue;
bool wasVisible = entry.IsVisible;
entry.UpdateVisibility();
// Only translate when visibility changes from hidden to visible
if (!wasVisible && entry.IsVisible)
{
bool translated = translator.TranslateAndApplyFont(entry.TextMesh, entry.Path, null);
if (translated)
{
entry.WasTranslated = true;
entry.UpdateLastText();
}
}
}
}
/// <summary>
/// Clear all monitored entries
/// </summary>
public void Clear()
{
foreach (var group in strategyGroups.Values)
{
group.Clear();
}
instanceEntries.Clear();
pathToInstances.Clear();
pathRules.Clear();
monitoredPaths.Clear();
fastPollingTimer = 0f;
slowPollingTimer = 0f;
visibilityPollingTimer = 0f;
InitializeDefaultPathRules();
}
}
}