-
Notifications
You must be signed in to change notification settings - Fork 4
/
LanguageSupport.cs
408 lines (365 loc) · 16.5 KB
/
LanguageSupport.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
using GlobalEnums;
using JetBrains.Annotations;
using Language;
using Modding;
using TMPro;
using UnityEngine;
using Object = UnityEngine.Object;
namespace LanguageSupport;
[UsedImplicitly]
internal class LanguageSupport : Mod
{
internal static LanguageSupport Instance;
private readonly string _dir;
private readonly string _folder = "LanguageSupport";
public LanguageSupport() : base("Language Support")
{
Log("!OnChangeFontByLanguageSetFont");
Instance = this;
_dir = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? throw new DirectoryNotFoundException("I have no idea how you did this, but good luck figuring it out."), _folder);
if (!Directory.Exists(_dir))
{
Directory.CreateDirectory(_dir);
}
// ToDo debug
InitCallbacks();
// ToDo debug
InitLanguage();
}
public override string GetVersion()
{
Log("!OnChangeFontByLanguageSetFont");
var asm = Assembly.GetExecutingAssembly();
var ver = asm.GetName().Version.ToString();
var sha1 = SHA1.Create();
var stream = File.OpenRead(asm.Location);
var hashBytes = sha1.ComputeHash(stream);
var hash = BitConverter.ToString(hashBytes).Replace("-", "").ToLowerInvariant();
stream.Close();
sha1.Clear();
return $"{ver}-{hash.Substring(0, 6)}";
}
public override void Initialize()
{
Log("!OnChangeFontByLanguageSetFont");
Instance = this;
foreach (var t in Object.FindObjectsOfType<UnityEngine.UI.MenuLanguageSetting>()) t.RefreshControls();
Log("Initialized");
}
private void InitCallbacks()
{
Log("!InitCallbacks");
// Hooks
On.Language.Language.HasLanguageFile += OnLanguageHasLanguageFile;
On.Language.Language.GetLanguageFileContents += OnLanguageGetLanguageFileContents;
On.UnityEngine.UI.MenuLanguageSetting.RefreshAvailableLanguages += OnMenuLanguageSettingRefreshAvailableLanguages;
On.UnityEngine.UI.MenuLanguageSetting.PushUpdateOptionList += OnMenuLanguageSettingPushUpdateOptionList;
// ToDo these break text display
On.ChangeFontByLanguage.SetFont += OnChangeFontByLanguageSetFont;
On.TMPro.TextMeshPro.LoadFontAsset += OnTextMeshProLoadFontAsset;
On.TMPro.TMP_FontAsset.ReadFontDefinition += OnTMP_FontAssetReadFontDefinition;
Log("~InitCallbacks");
}
private void OnTMP_FontAssetReadFontDefinition(On.TMPro.TMP_FontAsset.orig_ReadFontDefinition orig, TMP_FontAsset self)
{
Log("!OnTMP_FontAssetReadFontDefinition");
Log($"OnTMP_FontAssetReadFontDefinition - {ReflectionHelper.GetField<TMP_FontAsset, FaceInfo>(self, "m_fontInfo")}");
Log($"OnTMP_FontAssetReadFontDefinition - {ReflectionHelper.GetField<TMP_FontAsset, Texture2D>(self, "atlas")}");
Log($"OnTMP_FontAssetReadFontDefinition - {ReflectionHelper.GetField<TMP_FontAsset, List<TMP_Glyph>>(self, "m_glyphInfoList")}");
Log($"OnTMP_FontAssetReadFontDefinition - {ReflectionHelper.GetField<TMP_FontAsset, Dictionary<int, KerningPair>>(self, "m_characterDictionary")}");
Log($"OnTMP_FontAssetReadFontDefinition - {ReflectionHelper.GetField<TMP_FontAsset, KerningTable>(self, "m_kerningDictionary")}");
Log($"OnTMP_FontAssetReadFontDefinition - {ReflectionHelper.GetField<TMP_FontAsset, List<TMP_FontAsset>>(self, "m_kerningPair")}");
Log($"OnTMP_FontAssetReadFontDefinition - {ReflectionHelper.GetField<TMP_FontAsset, FontCreationSetting>(self, "fontCreationSettings")}");
orig(self);
Log("~OnTMP_FontAssetReadFontDefinition");
}
private void OnTextMeshProLoadFontAsset(On.TMPro.TextMeshPro.orig_LoadFontAsset orig, TextMeshPro self)
{
Log("!OnTextMeshProLoadFontAsset");
var mFontAsset = ReflectionHelper.GetField<TextMeshPro, TMP_FontAsset>(self,"m_fontAsset");
var mRenderer = ReflectionHelper.GetField<TextMeshPro, Renderer>(self,"m_renderer");
var mSharedMaterial = ReflectionHelper.GetField<TextMeshPro, Material>(self,"m_sharedMaterial");
if (mFontAsset.name == "noto_serif_thai_bold_tmpro")
{
Log($"OnTextMeshProLoadFontAsset - {mFontAsset.characterDictionary}"); // ""
if (mFontAsset.characterDictionary == null)
{
mFontAsset.ReadFontDefinition();
}
Log($"OnTextMeshProLoadFontAsset - {mFontAsset.characterDictionary}"); // ""
Log($"OnTextMeshProLoadFontAsset - {mRenderer.sharedMaterial}"); // "perpetua_tmpro Material (UnityEngine.Material)"
Log($"OnTextMeshProLoadFontAsset - {mRenderer.sharedMaterial.mainTexture}"); // "perpetua_tmpro Atlas (UnityEngine.Texture2D)"
Log($"OnTextMeshProLoadFontAsset - {mFontAsset.atlas}"); // ""
Log($"OnTextMeshProLoadFontAsset - {mFontAsset.material}");
Log($"OnTextMeshProLoadFontAsset - {mRenderer.receiveShadows}");
Log($"OnTextMeshProLoadFontAsset - {mRenderer.shadowCastingMode}");
}
orig(self);
Log("~OnTextMeshProLoadFontAsset");
}
private static AssetBundle _abFa = null;
private static TMP_FontAsset _fa = null;
private static Sprite _atlas = null;
private void OnChangeFontByLanguageSetFont(On.ChangeFontByLanguage.orig_SetFont orig, ChangeFontByLanguage self)
{
Log("!OnChangeFontByLanguageSetFont");
orig(self);
if (_fa == null) return;
var tmp = ReflectionHelper.GetField<ChangeFontByLanguage, TextMeshPro>(self, "tmpro");
var tmpMat = _fa.material;
tmpMat.shader = tmp.font.material.shader;
//tmpMat.SetTexture("_BumpMap", null);
//tmpMat.SetTexture("_Cube", null);
//tmpMat.SetTexture("_FaceTex", null);
//tmpMat.SetTexture("_MainTex", _fa.atlas);
//tmpMat.SetTexture("_OutlineTex", null);
//tmpMat.SetFloat("_Ambient", 0.5f);
//tmpMat.SetFloat("_Bevel", 0.5f);
//tmpMat.SetFloat("_BevelClamp", 0);
//tmpMat.SetFloat("_BevelOffset", 0);
//tmpMat.SetFloat("_BevelRoundness", 0);
//tmpMat.SetFloat("_BevelWidth", 0);
//tmpMat.SetFloat("_BumpFace", 0);
//tmpMat.SetFloat("_BumpOutline", 0);
//tmpMat.SetFloat("_ColorMask", 15);
//tmpMat.SetFloat("_Diffuse", 0.5f);
//tmpMat.SetFloat("_FaceDilate", 0);
//tmpMat.SetFloat("_FaceUVSpeedX", 0);
//tmpMat.SetFloat("_FaceUVSpeedY", 0);
//tmpMat.SetFloat("_GlowInner", 0.05f);
//tmpMat.SetFloat("_GlowOffset", 0);
//tmpMat.SetFloat("_GlowOuter", 0.05f);
//tmpMat.SetFloat("_GlowPower", 0.75f);
//tmpMat.SetFloat("_GradientScale", 5);
//tmpMat.SetFloat("_LightAngle", 3.1416f);
//tmpMat.SetFloat("_MaskID", 0);
//tmpMat.SetFloat("_MaskSoftnessX", 0);
//tmpMat.SetFloat("_MaskSoftnessY", 0);
//tmpMat.SetFloat("_OutlineSoftness", 0);
//tmpMat.SetFloat("_OutlineUVSpeedX", 0);
//tmpMat.SetFloat("_OutlineUVSpeedY", 0);
//tmpMat.SetFloat("_OutlineWidth", 0);
//tmpMat.SetFloat("_PerspectiveFilter", 0.875f);
//tmpMat.SetFloat("_Reflectivity", 10);
//tmpMat.SetFloat("_ScaleRatioA", 0.8f);
//tmpMat.SetFloat("_ScaleRatioB", 0.8f);
//tmpMat.SetFloat("_ScaleRatioC", 0.6504065f);
//tmpMat.SetFloat("_ScaleX", 1);
//tmpMat.SetFloat("_ScaleY", 1);
//tmpMat.SetFloat("_ShaderFlags", 0);
//tmpMat.SetFloat("_SpecularPower", 2);
//tmpMat.SetFloat("_Stencil", 0);
//tmpMat.SetFloat("_StencilComp", 8);
//tmpMat.SetFloat("_StencilOp", 0);
//tmpMat.SetFloat("_StencilReadMask", 255);
//tmpMat.SetFloat("_StencilWriteMask", 255);
//tmpMat.SetFloat("_TextureHeight", _fa.atlas.height);
//tmpMat.SetFloat("_TextureWidth", _fa.atlas.width);
//tmpMat.SetFloat("_UnderlayDilate", 0.23f);
//tmpMat.SetFloat("_UnderlayOffsetX", 0);
//tmpMat.SetFloat("_UnderlayOffsetY", 0);
//tmpMat.SetFloat("_UnderlaySoftness", 1);
//tmpMat.SetFloat("_VertexOffsetX", 0);
//tmpMat.SetFloat("_VertexOffsetY", 0);
//tmpMat.SetFloat("_WeightBold", 0.75f);
//tmpMat.SetFloat("_WeightNormal", 0);
//tmpMat.SetColor("_ClipRect", new Color(-10000, -10000, 10000, 10000));
//tmpMat.SetColor("_EnvMatrixRotation", new Color(0, 0, 0, 0));
//tmpMat.SetColor("_FaceColor", new Color(1, 1, 1, 1));
//tmpMat.SetColor("_GlowColor", new Color(0, 1, 0, 0.5f));
//tmpMat.SetColor("_MaskCoord", new Color(0, 0, 0, 0));
//tmpMat.SetColor("_OutlineCoord", new Color(0, 0, 0, 1));
//tmpMat.SetColor("_ReflectFaceColor", new Color(0, 0, 0, 1));
//tmpMat.SetColor("_ReflectOutlineColor", new Color(0, 0, 0, 1));
//tmpMat.SetColor("_SpecularColor", new Color(1, 1, 1, 1));
//tmpMat.SetColor("_UnderlayColor", new Color(0, 0, 0, 0.221f));
_fa.material = tmpMat;
tmp.font = _fa;
//tmp.font.atlas = _fa.atlas;
//Dictionary<int, TMP_Glyph> tmpGlyphs = _fa.characterDictionary;
//foreach (var pair in tmpGlyphs)
//{
// pair.Value.y = (_fa.atlas.height - (pair.Value.y + (pair.Value.height / 2))) - (pair.Value.height / 2);
//}
//tmp.font.SetAttr("m_characterDictionary", tmpGlyphs);
//tmp.font.SetAttr("m_fontInfo", _fa.fontInfo);
//tmp.font.SetAttr("m_kerningDictionary", _fa.kerningDictionary);
//tmp.font.SetAttr("m_kerningInfo", _fa.kerningInfo);
ReflectionHelper.SetField<ChangeFontByLanguage, TextMeshPro>(self, "tmpro", tmp);
Log("~OnChangeFontByLanguageSetFont");
}
private new void Log(string message)
{
Modding.Logger.Log(message);
Debug.Log($"[SFG Thing] - {message}");
}
private new void Log(object message)
{
Log($"{message}");
}
private void InitLanguage()
{
Log("!InitLanguage");
#region Dump All Files
var langs = Enum.GetNames(typeof(SupportedLanguages));
foreach (var lang in langs)
{
if (!Directory.Exists($"{_dir}/{lang}")) Directory.CreateDirectory($"{_dir}/{lang}");
string[] sheets =
{
"Achievements",
"Backer Messages",
"Banker",
"Charm Slug",
"Cornifer",
"CP2",
"CP3",
"Credits List",
"Dream Witch",
"Dreamers",
"Elderbug",
"Enemy Dreams",
"General",
"Ghosts",
"Hornet",
"Hunter",
"Iselda",
"Jiji",
"Journal",
"Lore Tablets",
"MainMenu",
"Map Zones",
"Minor NPC",
"Nailmasters",
"Nailsmith",
"Prices",
"Prompts",
"Quirrel",
"Relic Dealer",
"Shaman",
"Sly",
"Stag",
"StagMenu",
"Titles",
"UI",
"Zote"
};
foreach (var sheet in sheets)
if (!File.Exists($"{_dir}/{lang}/{sheet}.txt"))
{
var t = ((TextAsset)Resources.Load($"Languages/{lang}_{sheet}", typeof(TextAsset))).text;
using var outputFile = new StreamWriter($"{_dir}/{lang}/{sheet}.txt");
outputFile.Write(t);
}
}
#endregion
if (_abFa == null)
{
Log("!InitLanguage - _abFa == null - 0");
Assembly asm = Assembly.GetExecutingAssembly();
Log("!InitLanguage - _abFa == null - 1");
using Stream s = asm.GetManifestResourceStream("LanguageSupport.Resources.tmprofont-test.fixed");
Log("!InitLanguage - _abFa == null - 2");
if (s == null) return;
Log("!InitLanguage - _abFa == null - 3");
_abFa = AssetBundle.LoadFromStream(s);
Log("!InitLanguage - _abFa == null - 4");
Object.DontDestroyOnLoad(_abFa);
Log("!InitLanguage - _abFa == null - 5");
}
if (_fa == null && _abFa != null)
{
Log("!InitLanguage - _fa == null && _abFa != null - 0");
_fa = _abFa.LoadAsset<TMP_FontAsset>("NotoSerif-Regular-4096x2048-0000xD7FF-better");
Log("!InitLanguage - _fa == null && _abFa != null - 1");
_fa.name = "noto_serif_thai_bold_tmpro";
Log("!InitLanguage - _fa == null && _abFa != null - 2");
Object.DontDestroyOnLoad(_fa);
Log("!InitLanguage - _fa == null && _abFa != null - 3");
_fa.fallbackFontAssets = new List<TMP_FontAsset>();
Log("!InitLanguage - _fa == null && _abFa != null - 4");
//_fa.fallbackFontAssets.AddRange(Object.FindObjectsOfType<TMP_FontAsset>(true));
}
Language.Language.LoadAvailableLanguages();
Language.Language.LoadLanguage();
Log("~InitLanguage");
}
private void OnMenuLanguageSettingPushUpdateOptionList(On.UnityEngine.UI.MenuLanguageSetting.orig_PushUpdateOptionList orig, UnityEngine.UI.MenuLanguageSetting self)
{
Log("!OnMenuLanguageSettingPushUpdateOptionList");
orig(self);
SupportedLanguages[] langs = (SupportedLanguages[]) self.GetType().GetField("langs", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(self);
string[] array = new string[langs.Length];
for (int i = 0; i < langs.Length; i++)
{
array[i] = ((LanguageCode) langs[i]).ToString();
}
self.SetOptionList(array);
Log("~OnMenuLanguageSettingPushUpdateOptionList");
}
private bool OnLanguageHasLanguageFile(On.Language.Language.orig_HasLanguageFile orig, string lang, string sheetTitle)
{
Log("!OnLanguageHasLanguageFile");
var ret = orig(lang, sheetTitle);
if (!ret)
{
if (File.Exists($"{_dir}/{lang}/{sheetTitle}.txt"))
{
ret = true;
}
}
Log("~OnLanguageHasLanguageFile");
return ret;
}
private string OnLanguageGetLanguageFileContents(On.Language.Language.orig_GetLanguageFileContents orig, string sheetTitle)
{
Log("!OnLanguageGetLanguageFileContents");
string ret = orig(sheetTitle);
if (ret == string.Empty)
{
if (File.Exists($"{_dir}/{Language.Language.CurrentLanguage()}/{sheetTitle}.txt"))
{
Log("~OnLanguageGetLanguageFileContents");
return File.ReadAllText($"{_dir}/{Language.Language.CurrentLanguage()}/{sheetTitle}.txt");
}
}
Log("~OnLanguageGetLanguageFileContents");
return ret;
}
private void OnMenuLanguageSettingRefreshAvailableLanguages(On.UnityEngine.UI.MenuLanguageSetting.orig_RefreshAvailableLanguages orig, UnityEngine.UI.MenuLanguageSetting self)
{
Log("!OnMenuLanguageSettingRefreshAvailableLanguages");
orig(self);
LanguageCode[] customLangs;
if (GameManager.instance.gameConfig.hideLanguageOption)
{
customLangs = (Enum.GetValues(typeof(LanguageCode)) as LanguageCode[]);
}
else
{
customLangs = (Enum.GetValues(typeof(LanguageCode)) as LanguageCode[]);
}
SupportedLanguages[] langs = (SupportedLanguages[]) self.GetType().GetField("langs", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(self);
List<SupportedLanguages> finalLangs = new List<SupportedLanguages>();
foreach (var l in customLangs)
{
if (File.Exists($"{_dir}/{l}/General.txt") || (langs.Contains((SupportedLanguages) l)))
{
finalLangs.Add((SupportedLanguages) l);
}
}
self.GetType().GetField("langs", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(self, finalLangs.ToArray());
//self.optionList = new string[finalLangs.Count];
//for (int i = 0; i < finalLangs.Count; i++)
//{
// self.optionList[i] = finalLangs[i].ToString();
//}
Log("~OnMenuLanguageSettingRefreshAvailableLanguages");
}
}