-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cs
More file actions
299 lines (275 loc) · 12.7 KB
/
test.cs
File metadata and controls
299 lines (275 loc) · 12.7 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
using Harmony;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
using System.Xml;
namespace LangImporter
{
public class Harmony_Patch
{
//for debugging
public static void GenerateError(string message)
{
if(path != null) File.AppendAllText(path + "/Error.txt", message + "\n");
}
public static Dictionary<string, string> supportedLanguages = new Dictionary<string, string>()
{
{"en","English"},
{"kr","한국어"},
{"cn","中文(简体)"},
{"cn_tr","中文(繁體)"},
{"jp","日本語"},
{"ru","русский"},
{"vn","Tiếng Việt"},
{"bg","български"},
{"es","Español Latinoamérica"},
{"fr","français"},
{"pt_br","Português do Brasil"},
{"pt_pt","Português"}
};
public static Dictionary<string, string[]> associated_paths = new Dictionary<string, string[]>()
{
// {uniqkey}, {path, targetkey}
};
public static string[] moddedLangs = null;
public static string path = null;
public Harmony_Patch()
{
path = Path.GetDirectoryName(Uri.UnescapeDataString(new UriBuilder(Assembly.GetExecutingAssembly().CodeBase).Path));
if (Directory.Exists(path + "/Languages"))
{
#region loading
Dictionary<string, string> moddedLangs = new Dictionary<string, string>();
int counter = 1;
foreach (string dir in Directory.GetDirectories(path + "/Languages"))
{
if (File.Exists(dir+"/LangInfo.xml"))
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(File.ReadAllText(dir + "/LangInfo.xml"));
try
{
string name = doc.SelectSingleNode("langInfo/name").InnerText,
key = doc.SelectSingleNode("langInfo/key").InnerText;
string[] pair = {dir, key};
if (name == "Example(cant be used as real name)") continue;
if (supportedLanguages.ContainsKey(key)) key = key + $"modded{counter++}";
moddedLangs.Add(key, name);
associated_paths.Add(key, pair);
}
catch { GenerateError($"LangInfo file in {'"'}{dir.Split('\\').Last()}{'"'} folder is broken or doesnt contains necessary information"); }
}
else
{
GenerateError($"lack of LangInfo file in {'"'}{dir.Split('\\').Last()}{'"'} folder");
}
}
if (moddedLangs.Count == 0) { supportedLanguages = null; return; }
#endregion
#region marging
Harmony_Patch.moddedLangs = moddedLangs.Keys.ToArray();
foreach (KeyValuePair<string, string> item in supportedLanguages)
{
try
{
moddedLangs.Add(item.Key, item.Value);
}
catch(Exception)
{
//probably will not get throwed ever
GenerateError($"key {'"'}{item.Key}{'"'} is already exist, try to use different one");
}
}
supportedLanguages = moddedLangs;
#endregion
#region patching
HarmonyInstance harmonyInstance = HarmonyInstance.Create("Lobotomy.justnikocat.LangImporter");
harmonyInstance.Patch(
typeof(SupportedLanguage).GetMethod(nameof(SupportedLanguage.GetSupprotedList), AccessTools.all),
new HarmonyMethod(typeof(Harmony_Patch).GetMethod("SupportedLanguage_GetSupprotedList")),
null,
null);
harmonyInstance.Patch(
typeof(SupportedLanguage).GetMethod(nameof(SupportedLanguage.GetCurrentLanguageName), AccessTools.all, null, new Type[] { typeof(string) }, null),
new HarmonyMethod(typeof(Harmony_Patch).GetMethod("SupportedLanguage_GetCurrentLanguageName")),
null,
null);
harmonyInstance.Patch(
typeof(AssetLoader).GetMethod(nameof(AssetLoader.LoadExternalXML), AccessTools.all),
new HarmonyMethod(typeof(Harmony_Patch).GetMethod("AssetLoader_LoadExternalXML")),
null,
null);
#endregion
}
else
{
supportedLanguages = null;
Directory.CreateDirectory(path + "/Language");
}
}
#region patches
//SupportedLanguage
public static bool SupportedLanguage_GetSupprotedList(ref List<string> __result)
{
__result = supportedLanguages.Keys.ToList<string>();
return false;
}
public static bool SupportedLanguage_GetCurrentLanguageName(ref string __result, string language)
{
supportedLanguages.TryGetValue(language, out __result);
return false;
}
//AssetLoader
public static bool AssetLoader_LoadExternalXML_Prefix(string src, ref XmlDocument __result, out bool __state)
{
__state = false;
if (src == "Language/AgentName")
{
__state = true;
return true;
}
if (src == "Language/ResearchDesc")
{
}
string key = src.Split('_').Last();
if (!moddedLangs.Contains(key)) return true;
string root;
root = associated_paths[key] + "/Text/" + src + ".xml";
try
{
XmlDocument xml = new XmlDocument();
xml.LoadXml(File.ReadAllText(root));
__result = xml;
}
catch(Exception e)
{
GenerateError("cant load custom lang file: " +e.Message);
return true;
}
return false;
}
public void AssetLoader_LoadExternalXML_Postfix(string src, ref XmlDocument __result, bool __state)
{
if (__state)
{
Dictionary<string, string> reversedSup = supportedLanguages.Keys.ToDictionary((string key) => supportedLanguages[key]);
if (src == "Language/AgentName")
{
IEnumerator orignames = __result.SelectNodes("root/data").GetEnumerator();
foreach (string dir in associated_paths.Values)
{
try
{
XmlDocument doc = new XmlDocument();
doc.Load(dir + "/LangInfo.xml");
string uniqueKey = reversedSup[doc.SelectSingleNode("langInfo/name").InnerText],
key = doc.SelectSingleNode("langInfo/key").InnerText;
doc.Load(dir + "/Text/AgentName.xml");
foreach (XmlNode curr in doc.SelectNodes("root/data"))
{
orignames.MoveNext();
string localizedName = null;
try
{
localizedName = curr.Attributes.GetNamedItem(key).InnerText;
}
catch (Exception) { GenerateError("cant get attr in " + key); }
if (localizedName != null)
{
if (curr.Attributes.GetNamedItem("id").InnerText != ((XmlNode)orignames.Current).Attributes.GetNamedItem("id").InnerText)
throw new Exception($"localized names have a wrong order or have a lack of all orig names in {uniqueKey} localization");
XmlAttribute att = __result.CreateAttribute(uniqueKey);
att.InnerText = localizedName;
((XmlNode)orignames.Current).Attributes.Append(att);
}
}
}
catch (Exception e)
{
GenerateError(dir + " recieve a error during loading agents: " + e.Message);
}
orignames.Reset();
}
IDisposable disposable;
if ((disposable = (orignames as IDisposable)) != null)
{
disposable.Dispose();
}
}
else if (src == "Language/ResearchDesc")
{
XmlNode origFile = __result.SelectSingleNode("root/supportLanguage");
foreach (string mdLn in moddedLangs)
{
XmlElement ln = __result.CreateElement("ln");
ln.InnerText = mdLn;
origFile.AppendChild(ln);
}
IEnumerator orignames = __result.SelectNodes("root/node").GetEnumerator();
foreach (string dir in associated_paths.Values)
{
try
{
XmlDocument doc = new XmlDocument();
doc.Load(dir + "/LangInfo.xml");
string uniqueKey = reversedSup[doc.SelectSingleNode("langInfo/name").InnerText],
key = doc.SelectSingleNode("langInfo/key").InnerText;
doc.Load(dir + "/Text/ResearchDesc.xml");
foreach (XmlNode curr in doc.SelectNodes("root/node"))
{
if (curr.Attributes.GetNamedItem("id").InnerText != ((XmlNode)orignames.Current).Attributes.GetNamedItem("id").InnerText)
throw new Exception($"localized researches have a wrong order or have a lack of all orig names in {uniqueKey} localization");
}
}
catch (Exception e)
{
GenerateError(dir + " recieve a error during loading researches: " + e.Message);
}
orignames.Reset();
}
}
}
}
//GameStaticDataLoader
public static void GameStaticDataLoader_LoadResearchDescData(List<ResearchItemTypeInfo> research)
{
Dictionary<int, int> ids = new Dictionary<int, int>();
for (int i = 0; i < research.Count; i++)
{
ids.Add(research[i].id, i);
}
foreach (string lang in moddedLangs)
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(associated_paths[lang][0] + "/Text/ResearchDesc.xml");
foreach(XmlNode currNode in xmlDocument.SelectNodes("root/node"))
{
try
{
int resID = int.Parse(currNode.Attributes.GetNamedItem("id").InnerText);
Dictionary<string, ResearchItemDesc> dictionary2 = new Dictionary<string, ResearchItemDesc>();
XmlNode langNode = currNode.SelectSingleNode(lang);
dictionary2.Add(lang, new ResearchItemDesc
{
name = langNode.SelectSingleNode("name").InnerText,
desc = langNode.SelectSingleNode("current").InnerText,
shortDesc = langNode.SelectSingleNode("short").InnerText
});
dictionary[resID].Add(dictionary2);
}
catch (Exception e)
{
GenerateError("error during loading descriptions: " + e.Message);
continue;
}
}
}
}
#endregion
//GameStaticDataLoader подгружает инфу об агентах
}
}