-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProgram.cs
More file actions
280 lines (260 loc) · 9.92 KB
/
Program.cs
File metadata and controls
280 lines (260 loc) · 9.92 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
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO.Compression;
#if DEBUG
using System.Runtime.ExceptionServices;
#endif
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Nodes;
using AssetsTools.NET;
using AssetsTools.NET.Extra;
namespace LELocalePatch {
public static class Program {
/// <summary>
/// Entry point of the program.
/// </summary>
/// <remarks>
/// Parses the command line <paramref name="args"/> and calls <see cref="Run"/>.
/// Then outputs the success message or exception to the <see cref="Console"/>.
/// </remarks>
public static void Main(string[] args) {
static void PrintUsage() {
Console.WriteLine("Usage: LELocalePatch <bundlePath> {export|import} <folderPath|zipPath>");
Console.WriteLine(" or: LELocalePatch <bundlePath> translate <dictionaryFilePath>");
}
if (args.Length != 3) {
PrintUsage();
if (args.Length == 0)
goto pause;
return;
}
try {
Mode? mode = args[1].ToLowerInvariant() switch {
"export" => Mode.Export,
"import" => Mode.Import,
"translate" => Mode.Translate,
_ => null
};
if (mode.HasValue)
Run(args[0], args[2], mode.Value);
else
PrintUsage();
return; // Do not pause if success
} catch (Exception ex) {
var tmp = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Error");
Console.Error.WriteLine(ex);
Console.ForegroundColor = tmp;
#if DEBUG
ExceptionDispatchInfo.Capture(ex).Throw(); // Throw to the debugger
#endif
}
pause:
Console.WriteLine();
Console.Write("Enter to exit . . .");
Console.ReadLine();
}
/// <param name="bundlePath">
/// The path of the bundle file.
/// (e.g. @"Last Epoch_Data\StreamingAssets\aa\StandaloneWindows64\localization-string-tables-chinese(simplified)(zh)_assets_all.bundle")
/// </param>
/// <param name="targetPath">
/// Path of the folder/zip-file to ex(im)port the json files (in UTF-8), or the dictionary json file for translation.
/// </param>
/// <exception cref="FileNotFoundException"/>
/// <exception cref="DirectoryNotFoundException"/>
/// <exception cref="DummyFieldAccessException"/>
/// <exception cref="JsonException"/>
/// <exception cref="KeyNotFoundException"/>
public static void Run(string bundlePath, string targetPath, Mode mode) {
ArgumentException.ThrowIfNullOrWhiteSpace(bundlePath);
ArgumentException.ThrowIfNullOrWhiteSpace(targetPath);
ArgumentOutOfRangeException.ThrowIfGreaterThan((uint)mode, (uint)Mode.Translate);
var manager = new AssetsManager();
if (mode != Mode.Export) {
var basePath = Path.GetFullPath(bundlePath + "/../../catalog");
var catPath = basePath + ".bin";
if (!File.Exists(catPath))
catPath = basePath + ".json";
if (!File.Exists(catPath))
catPath = basePath + ".bundle";
if (!File.Exists(catPath))
ThrowFileNotFound(basePath + ".bin");
Catalog.RemoveCRC(catPath, manager);
}
targetPath = Path.GetFullPath(targetPath);
ZipArchive? zip = null;
IDictionary<string, JsonNode?>? contents = null;
try {
switch (mode) {
case Mode.Export:
if (targetPath.EndsWith(".zip", StringComparison.OrdinalIgnoreCase))
zip = ZipFile.Open(targetPath, ZipArchiveMode.Create);
else
Directory.CreateDirectory(targetPath);
break;
case Mode.Import:
if (!Directory.Exists(targetPath)) {
if (File.Exists(targetPath))
zip = ZipFile.OpenRead(targetPath);
else
ThrowDirectoryNotFound(targetPath);
}
break;
case Mode.Translate:
if (!File.Exists(targetPath))
ThrowFileNotFound(targetPath);
Stream stream;
if (targetPath.EndsWith(".zip", StringComparison.OrdinalIgnoreCase)) {
zip = ZipFile.Open(targetPath, ZipArchiveMode.Read);
stream = zip.Entries.First(e => e.FullName.Equals("dictionary.json", StringComparison.OrdinalIgnoreCase)).Open();
} else {
stream = File.OpenRead(targetPath);
}
using (stream) {
contents = JsonNode.Parse(stream, null, new() { AllowTrailingCommas = true,
CommentHandling = JsonCommentHandling.Skip })!.AsObject();
if (zip is not null) {
zip.Dispose();
zip = null;
}
}
break;
}
bundlePath = Path.GetFullPath(bundlePath);
var bundle = manager.LoadBundleFile(new MemoryStream(File.ReadAllBytes(bundlePath)), bundlePath);
const int ASSETS_INDEX_IN_BUNDLE = 0;
var assets = manager.LoadAssetsFileFromBundle(bundle, ASSETS_INDEX_IN_BUNDLE);
var modified = false;
foreach (var info in assets.file.AssetInfos) {
if (info.TypeId != (int)AssetClassID.MonoBehaviour)
continue; // MonoScript or AssetBundle
var stringTable = manager.GetBaseField(assets, info);
var tableEnties = stringTable["m_TableData"]["Array"];
var filename = stringTable["m_Name"].AsString + ".json";
Console.Write(filename + " ... ");
switch (mode) {
case Mode.Export:
using(Stream stream = zip is null
? new FileStream($"{targetPath}/{filename}", FileMode.Create, FileAccess.Write, FileShare.Read)
: zip.CreateEntry(filename, CompressionLevel.SmallestSize).Open())
Export(tableEnties.Children, stream);
break;
case Mode.Import:
using (Stream? stream = zip is null
? File.Exists(filename = Path.GetFullPath($"{targetPath}/{filename}")) ? File.OpenRead(filename) : null
: zip.Entries.FirstOrDefault(e => e.FullName == filename) is ZipArchiveEntry e ? e.Open() : null) {
if (stream is null) {
Console.WriteLine("Not found");
continue;
}
contents = JsonNode.Parse(stream, null, new() { AllowTrailingCommas = true,
CommentHandling = JsonCommentHandling.Skip })!.AsObject();
}
goto case Mode.Translate;
case Mode.Translate:
if (Import(tableEnties.Children, contents!, mode is Mode.Translate)) {
//tableEnties.AsArray = new(tableEnties.Children.Count); // Uncomment this if someday the Import method will add/remove entries
info.SetNewData(stringTable);
modified = true;
}
break;
default:
Console.Error.WriteLine("Unknown mode: " + mode);
return;
}
Console.WriteLine("Done");
}
if (modified) {
bundle.file.BlockAndDirInfo.DirectoryInfos[ASSETS_INDEX_IN_BUNDLE].SetNewData(assets.file);
// The `Pack` method doesn't consider the replacer (The modified data), so write and read again here.
var uncompressed = new MemoryStream();
bundle.file.Write(new(uncompressed));
bundle.file.Close();
bundle.file.Read(new(uncompressed));
using var writer = new AssetsFileWriter(bundlePath);
bundle.file.Pack(writer, AssetBundleCompressionType.LZMA);
}
Console.WriteLine("Done!");
} finally {
zip?.Dispose();
manager.UnloadAll();
}
[DoesNotReturn, DebuggerNonUserCode]
static void ThrowDirectoryNotFound(string folderPath)
=> throw new DirectoryNotFoundException("The input folder does not exist: " + Path.GetFullPath(folderPath));
[DoesNotReturn, DebuggerNonUserCode]
static void ThrowFileNotFound(string path)
=> throw new FileNotFoundException(null, path);
}
/// <summary>
/// Internal implementation of <see cref="Run"/>.
/// </summary>
/// <param name="tableEnties">
/// <code>UnityEngine.Localization.Tables.StringTable.m_TableData</code>
/// Get by <c>BaseField["m_TableData"]["Array"].Children</c> of an asset in bundle
/// </param>
/// <param name="utf8Json">Json file stream to write</param>
/// <exception cref="DummyFieldAccessException"/>
public static void Export(IReadOnlyList<AssetTypeValueField> tableEnties, Stream utf8Json) {
var dic = new SortedList<long, string>(tableEnties.Count);
foreach (var tableEntryData in tableEnties)
dic.Add(tableEntryData["m_Id"].AsLong, tableEntryData["m_Localized"].AsString);
using var json = new Utf8JsonWriter(utf8Json, new() {
Indented = true,
#if NET9_0_OR_GREATER
IndentCharacter = '\t',
#endif
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
});
json.WriteStartObject();
foreach (var (id, str) in dic)
json.WriteString(id.ToString(), str);
json.WriteEndObject();
}
/// <param name="tableEntries">
/// <code>UnityEngine.Localization.Tables.StringTable.m_TableData</code>
/// Get by <c>BaseField["m_TableData"]["Array"].Children</c> of an asset in bundle
/// </param>
/// <param name="utf8JsonFile">Json file to read the content to patch</param>
/// <param name="throwNotMatch">
/// <see langword="true"/> to throw an exception when any entry in bundle is not found in the json file.<br />
/// Ignored when <paramref name="dump"/> is <see langword="true"/>.
/// </param>
/// <returns>Whether any entry is modified</returns>
/// <exception cref="JsonException"/>
/// <exception cref="DummyFieldAccessException"/>
/// <exception cref="KeyNotFoundException"/>
public static bool Import(IReadOnlyList<AssetTypeValueField> tableEntries,
IDictionary<string, JsonNode?> contents, bool isTranslation, bool throwNotMatch = false) {
if (tableEntries.Count == 0)
return false;
if (contents.Count == 0)
return false;
var modified = false;
for (var i = 0; i < tableEntries.Count; ++i) {
var localized = tableEntries[i]["m_Localized"].Value;
var key = isTranslation ? localized : tableEntries[i]["m_Id"].Value;
if (contents.TryGetValue(key.AsString, out var n)) {
var str = (string?)n;
if (!modified && localized.AsString == str)
continue; // No modification
localized.AsString = str;
modified = true;
} else if (throwNotMatch)
ThrowKeyNotFound(key.AsString);
}
return modified;
[DoesNotReturn, DebuggerNonUserCode]
static void ThrowKeyNotFound(string key)
=> throw new KeyNotFoundException($"The key {key} is not found in the json file");
}
public enum Mode {
Export,
Import,
Translate
}
}
}