-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCore.cs
More file actions
221 lines (200 loc) · 9.36 KB
/
Core.cs
File metadata and controls
221 lines (200 loc) · 9.36 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
using CsvHelper.Configuration;
using HarmonyLib;
using Il2Cpp;
using Il2CppCom.LuisPedroFonseca.ProCamera2D;
using Il2CppDmm.Games.Store;
using Il2CppExcelDataReader.Log;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using Il2CppSystem.Resources;
using Il2CppTMPro;
using MelonLoader;
using MelonLoader.ICSharpCode.SharpZipLib.Core;
using MelonLoader.Utils;
using System.Formats.Asn1;
using System.Globalization;
using System.Runtime.CompilerServices;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.SocialPlatforms;
using static Il2CppSpine.Unity.Examples.SpineboyFootplanter;
[assembly: MelonInfo(typeof(LOLocalization.Core), "LOLocalization", "1.4.0", "kautism", "https://github.com/darkautism/LOLocalization")]
[assembly: MelonGame("PiG Corporation", "LastOrigin_R")]
namespace LOLocalization
{
public class Core : MelonMod
{
public static int langindex = 4;
public static string langtext = "tc";
public static System.Collections.Generic.Dictionary<string, System.Collections.Generic.Dictionary<string, string>> communityLocalizationPatch = null;
public static System.Collections.Generic.Dictionary<string, System.Collections.Generic.Dictionary<string, string>> communityTable_LocalizationPatch = null;
public static System.Collections.Generic.Dictionary<string, UnityEngine.Font> fonts = null;
private static MelonPreferences_Category mainCategory;
private static MelonPreferences_Entry<string> configLanguage;
private static MelonPreferences_Entry<string> configFont;
public override void OnInitializeMelon()
{
mainCategory = MelonPreferences.CreateCategory("LOLocalization");
configLanguage = mainCategory.CreateEntry<string>("Language", "tc");
configFont = mainCategory.CreateEntry<string>("Font", "sourcehansans");
communityLocalizationPatch = new System.Collections.Generic.Dictionary<string, System.Collections.Generic.Dictionary<string, string>>();
communityLocalizationPatch.Add("tc", LoadLocalization("localization_tc"));
communityLocalizationPatch.Add("en", LoadLocalization("localization_en"));
communityTable_LocalizationPatch = new System.Collections.Generic.Dictionary<string, System.Collections.Generic.Dictionary<string, string>>();
communityTable_LocalizationPatch.Add("en", LoadLocalization("table_localization_en"));
communityTable_LocalizationPatch.Add("tc", LoadLocalization("table_localization_tc"));
if (configLanguage.Value == "en")
{
langindex = 3;
langtext = "en";
}
else
{
langindex = 4;
langtext = "tc";
}
LoggerInstance.Msg("Initialized.");
MelonPreferences.Save();
}
[HarmonyPatch(typeof(Il2Cpp.ResourceManager), "LoadFont")]
public class LoadFont_Patch
{
private static AssetBundle fontBundle;
[HarmonyPostfix]
internal static void Postfix(ref Font __result, string fontName)
{
// 1. 初始化字體字典與載入邏輯
if (fonts == null)
{
fonts = new System.Collections.Generic.Dictionary<string, Font>();
string path = Path.Join(MelonEnvironment.GameRootDirectory, "Localization/fonts");
if (System.IO.File.Exists(path))
{
MelonLogger.Msg("開始載入自定義字體包...");
string url = "file:///" + path.Replace("\\", "/");
// 2. 建立請求
UnityWebRequest req = UnityWebRequestAssetBundle.GetAssetBundle(url);
try
{
var op = req.SendWebRequest();
while (!op.isDone) { /* 同步等待 */ }
if (req.result != UnityWebRequest.Result.Success)
{
MelonLogger.Error("載入字體包失敗: " + req.error);
}
else
{
// 3. 獲取內容
fontBundle = DownloadHandlerAssetBundle.GetContent(req);
if (fontBundle != null)
{
foreach (string name in fontBundle.GetAllAssetNames())
{
// 取得檔名做為 Key
string newname = Path.GetFileNameWithoutExtension(name);
Font loadedFont = fontBundle.LoadAsset<Font>(newname);
if (loadedFont != null)
{
fonts[newname] = loadedFont;
MelonLogger.Msg($"已載入字體資產: {newname}");
}
}
}
}
}
finally
{
// 4. 關鍵修正:釋放 WebRequest 佔用的 Native 記憶體
if (req != null)
{
req.Dispose();
}
}
}
}
// 5. 替換邏輯
if (fonts != null && fonts.TryGetValue(configFont.Value, out Font replaceFont))
{
__result = replaceFont;
}
}
}
[HarmonyPatch(typeof(Localization), nameof(Localization.AddCSV))]
public class Localization_AddCSV_Patch
{
[HarmonyPrefix]
internal static void Prefix(ref BetterList<string> newValues, Il2CppStringArray newLanguages, Dictionary<string, int> languageIndices)
{
if (newValues.size >= 6 && newValues[langindex] != null && newValues[langindex].Trim().Length != 0)
{
newValues[1] = newValues[langindex];
newValues[2] = newValues[langindex];
}
}
}
[HarmonyPatch(typeof(Localization), nameof(Localization.Get))]
public class Localization_Get_Patch
{
[HarmonyPrefix]
internal static bool Prefix(ref string __result, string key, bool warnIfMissing = true)
{
if (communityLocalizationPatch.ContainsKey(configLanguage.Value) &&
communityLocalizationPatch[configLanguage.Value].ContainsKey(key) &&
!String.IsNullOrEmpty(communityLocalizationPatch[configLanguage.Value][key]))
{
__result = communityLocalizationPatch[configLanguage.Value][key];
return false;
}
return true;
}
}
[HarmonyPatch(typeof(DataManager), nameof(DataManager.GetLocalizationTable))]
public class DataManager_GetLocalizationTable_Patch
{
[HarmonyPrefix]
internal static bool Prefix(ref string __result, string key)
{
if (communityTable_LocalizationPatch.ContainsKey(configLanguage.Value) &&
communityTable_LocalizationPatch[configLanguage.Value].ContainsKey(key) &&
!String.IsNullOrEmpty(communityTable_LocalizationPatch[configLanguage.Value][key]))
{
__result = communityTable_LocalizationPatch[configLanguage.Value][key].Replace("&n", "\n");
return false;
}
return true;
}
}
System.Collections.Generic.Dictionary<string, string> LoadLocalization(string lang)
{
System.Collections.Generic.Dictionary<string, string> ret = new System.Collections.Generic.Dictionary<string, string>();
string path = $"Localization/{lang}.csv";
if (File.Exists(path))
{
try
{
ret = new System.Collections.Generic.Dictionary<string, string>();
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
HasHeaderRecord = false,
};
using (var reader = new StreamReader(path))
using (var csv = new CsvHelper.CsvReader(reader, config))
{
while (csv.Read())
{
string key = csv.GetField(0);
string value = csv.GetField(1);
//Log.LogInfo($"'{key} {value}'");
ret[key] = value;
}
}
}
catch (Exception e)
{
LoggerInstance.BigError(e.Message);
LoggerInstance.BigError($"The {path} is wrong format.");
}
}
return ret;
}
}
}