forked from potatosalad775/MWC_Localization_Core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTableProxyHandler.cs
More file actions
202 lines (163 loc) · 6.31 KB
/
HashTableProxyHandler.cs
File metadata and controls
202 lines (163 loc) · 6.31 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
using MSCLoader;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
namespace MWC_Localization_Core
{
/// <summary>
/// Translates PlayMakerHashTableProxy data used by the magazine keyword system.
/// Updates the live hash table, snapshot, and prefill string list so runtime and reset paths stay consistent.
/// </summary>
public class HashTableProxyHandler
{
private readonly MagazineTextHandler magazineHandler;
private readonly HashSet<string> targetPaths = new HashSet<string>();
private readonly HashSet<string> translatedPaths = new HashSet<string>();
private readonly Dictionary<string, PlayMakerHashTableProxy[]> proxyCache = new Dictionary<string, PlayMakerHashTableProxy[]>();
private static readonly FieldInfo PreFillStringListField =
typeof(PlayMakerHashTableProxy).GetField("preFillStringList", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
public HashTableProxyHandler(MagazineTextHandler magazineHandler)
{
this.magazineHandler = magazineHandler;
}
public void InitializeTargetPaths()
{
targetPaths.Clear();
targetPaths.Add("CARPARTS/PARTSYSTEM/PostSystem/KeywordsFI");
targetPaths.Add("CARPARTS/PARTSYSTEM/PostSystem/KeywordsEN");
}
public void ClearTranslations()
{
translatedPaths.Clear();
proxyCache.Clear();
}
public void Reset()
{
translatedPaths.Clear();
proxyCache.Clear();
}
public int TranslateAllHashTables()
{
int totalTranslated = 0;
foreach (string path in targetPaths)
{
if (translatedPaths.Contains(path))
continue;
int translated = TranslateHashTablePath(path, out bool isReady);
if (translated > 0)
{
totalTranslated += translated;
}
if (isReady)
{
translatedPaths.Add(path);
}
}
return totalTranslated;
}
public int MonitorAndTranslateHashTables()
{
return TranslateAllHashTables();
}
private int TranslateHashTablePath(string objectPath, out bool isReady)
{
isReady = false;
PlayMakerHashTableProxy[] proxies = GetProxies(objectPath);
if (proxies == null || proxies.Length == 0)
return 0;
isReady = true;
int translatedCount = 0;
for (int i = 0; i < proxies.Length; i++)
{
PlayMakerHashTableProxy proxy = proxies[i];
if (proxy == null)
continue;
translatedCount += TranslateProxy(proxy, objectPath, i);
}
return translatedCount;
}
private PlayMakerHashTableProxy[] GetProxies(string objectPath)
{
if (proxyCache.TryGetValue(objectPath, out PlayMakerHashTableProxy[] cached))
{
bool valid = true;
for (int i = 0; i < cached.Length; i++)
{
if (cached[i] == null)
{
valid = false;
break;
}
}
if (valid)
return cached;
proxyCache.Remove(objectPath);
}
GameObject obj = MLCUtils.FindGameObjectCached(objectPath);
if (obj == null)
return null;
PlayMakerHashTableProxy[] proxies = obj.GetComponents<PlayMakerHashTableProxy>();
if (proxies == null || proxies.Length == 0)
{
CoreConsole.Warning($"PlayMakerHashTableProxy not found at {objectPath}");
return null;
}
proxyCache[objectPath] = proxies;
return proxies;
}
private int TranslateProxy(PlayMakerHashTableProxy proxy, string objectPath, int componentIndex)
{
int translatedCount = 0;
Hashtable hashTable = proxy.hashTable;
if (hashTable == null)
return 0;
List<object> keys = new List<object>();
foreach (object key in hashTable.Keys)
{
keys.Add(key);
}
for (int i = 0; i < keys.Count; i++)
{
object key = keys[i];
string original = hashTable[key] as string;
if (string.IsNullOrEmpty(original))
continue;
string translated = magazineHandler.GetTranslation(original);
if (string.IsNullOrEmpty(translated) || translated == original)
continue;
hashTable[key] = translated;
translatedCount++;
}
translatedCount += TranslatePreFillStringList(proxy);
if (translatedCount > 0)
{
proxy.TakeSnapShot();
CoreConsole.Print($"[HashTableProxyHandler] Translated {translatedCount} string values in {objectPath}[{componentIndex}]");
}
return translatedCount;
}
private int TranslatePreFillStringList(PlayMakerHashTableProxy proxy)
{
if (PreFillStringListField == null)
return 0;
List<string> preFillStringList = PreFillStringListField.GetValue(proxy) as List<string>;
if (preFillStringList == null || preFillStringList.Count == 0)
return 0;
int translatedCount = 0;
for (int i = 0; i < preFillStringList.Count; i++)
{
string original = preFillStringList[i];
if (string.IsNullOrEmpty(original))
continue;
string translated = magazineHandler.GetTranslation(original);
if (string.IsNullOrEmpty(translated) || translated == original)
continue;
preFillStringList[i] = translated;
translatedCount++;
}
return translatedCount;
}
}
}