-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
297 lines (277 loc) · 11.6 KB
/
Program.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
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO.Compression;
using System.Runtime.CompilerServices;
#if DEBUG
using System.Runtime.ExceptionServices;
#endif
using System.Runtime.InteropServices;
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) {
if (args.Length != 3) {
Console.WriteLine("Usage: LELocalePatch <bundlePath> {dump|patch|patchFull} <folderPath|zipPath>");
if (args.Length == 0)
goto pause;
return;
}
bool dump, @throw = false;
switch (args[1].ToLowerInvariant()) {
case "dump":
dump = true;
break;
case "patchfull":
@throw = true; goto case "patch";
case "patch":
dump = false;
break;
default:
Console.WriteLine("Invalid action: " + args[1]);
return;
}
try {
Run(args[0], args[2], dump, @throw);
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="folderOrZipPath">
/// Path of the folder/zip-file to dump or apply the json files (in UTF-8).
/// </param>
/// <param name="dump">
/// <see langword="true"/> to dump the localization to json files; <see langword="false"/> to apply them back.
/// </param>
/// <param name="throwNotMatch">
/// <see langword="true"/> to throw an exception when any entry in bundle is not found in the json file,
/// or the json file doesn't exist.<br />
/// Ignored when <paramref name="dump"/> is <see langword="true"/>.
/// </param>
/// <exception cref="FileNotFoundException"/>
/// <exception cref="DirectoryNotFoundException"/>
/// <exception cref="DummyFieldAccessException"/>
/// <exception cref="JsonException"/>
/// <exception cref="KeyNotFoundException"/>
public static void Run(string bundlePath, string folderOrZipPath, bool dump, bool throwNotMatch = false) {
var manager = new AssetsManager();
ZipArchive? zip = null;
try {
if (dump) {
if (folderOrZipPath.EndsWith(".zip", StringComparison.OrdinalIgnoreCase))
zip = ZipFile.Open(folderOrZipPath, ZipArchiveMode.Create);
else
folderOrZipPath = Directory.CreateDirectory(folderOrZipPath).FullName;
} else { // patch
if (!Directory.Exists(folderOrZipPath)) {
if (File.Exists(folderOrZipPath))
zip = ZipFile.OpenRead(folderOrZipPath);
else
ThrowDirectoryNotFound(folderOrZipPath);
}
var catPath = Path.GetDirectoryName(Path.GetDirectoryName(bundlePath)) + "/catalog.json";
if (!File.Exists(catPath))
ThrowCatalogNotFound(catPath);
RemoveCRC(catPath);
}
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.WriteLine(filename);
if (dump) {
using Stream stream = zip is null
? new FileStream($"{folderOrZipPath}/{filename}", FileMode.Create, FileAccess.Write, FileShare.Read)
: zip.CreateEntry(filename, CompressionLevel.Optimal).Open();
Dump(tableEnties.Children, stream);
} else { // patch
using Stream? stream = zip is null
? File.Exists(filename = Path.GetFullPath($"{folderOrZipPath}/{filename}")) ? File.OpenRead(filename) : null
: zip.Entries.FirstOrDefault(e => e.FullName == filename) is ZipArchiveEntry e ? e.Open() : null;
if (stream is null) {
if (throwNotMatch)
ThrowJsonFileNotFound(filename);
continue;
}
if (Patch(tableEnties.Children, stream, throwNotMatch)) {
//tableEnties.AsArray = new(tableEnties.Children.Count); // Uncomment this if someday the Patch method will add/remove entries
info.SetNewData(stringTable);
modified = true;
}
}
}
if (!dump && modified) {
bundle.file.BlockAndDirInfo.DirectoryInfos[ASSETS_INDEX_IN_BUNDLE].SetNewData(assets.file);
var uncompressed = new MemoryStream();
bundle.file.Write(new(uncompressed)); // The `Pack` method doesn't consider the replacer (The modified data), so write and read again here.
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(true);
}
}
/// <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 Dump(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="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="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 Patch(IReadOnlyList<AssetTypeValueField> tableEnties, Stream utf8JsonFile, bool throwNotMatch = false) {
if (tableEnties.Count == 0)
return false;
var node = JsonNode.Parse(utf8JsonFile, null, new() { AllowTrailingCommas = true, CommentHandling = JsonCommentHandling.Skip })!.AsObject();
if (node.Count == 0)
return false;
var modified = false;
for (var i = 0; i < tableEnties.Count; ++i) {
if (node.TryGetPropertyValue(tableEnties[i]["m_Id"].Value.ToString(), out var n)) {
tableEnties[i]["m_Localized"].AsString = (string?)n;
modified = true;
} else if (throwNotMatch)
ThrowKeyNotFound(tableEnties[i]["m_Id"].Value.ToString());
}
return modified;
}
public static void RemoveCRC(string catalogJsonPath) {
var utf8 = new Utf8JsonReader(File.ReadAllBytes(catalogJsonPath));
var json = JsonNode.Parse(ref utf8);
var providerIndex = 0;
foreach (var v in json!["m_ProviderIds"]!.AsArray()) {
if ((string?)v == "UnityEngine.ResourceManagement.ResourceProviders.AssetBundleProvider")
break;
++providerIndex;
}
var entryData = Convert.FromBase64String((string)json["m_EntryDataString"]!);
var extraData = Convert.FromBase64String((string)json["m_ExtraDataString"]!);
var entryCount = MemoryMarshal.Read<int>(entryData);
var entryDatas = MemoryMarshal.CreateReadOnlySpan(ref Unsafe.As<byte, EntryData>(ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(entryData), sizeof(int))), entryCount);
var modified = false;
for (var i = 0; i < entryCount; ++i) {
if (entryDatas[i].ProviderIndex != providerIndex)
continue;
var offset = entryDatas[i].DataIndex;
if (extraData[offset] == 7) { // JsonObject
++offset;
offset += extraData[offset]; // ascii string length
++offset;
offset += extraData[offset]; // ascii string length
var len = MemoryMarshal.Read<int>(new(extraData, ++offset, sizeof(int)));
if (JsonNode.Parse(MemoryMarshal.Cast<byte, char>(new ReadOnlySpan<byte>(extraData, offset + sizeof(int), len)).ToString()) is JsonObject jsonObj) {
if (!jsonObj.ContainsKey("m_Crc"))
continue;
jsonObj["m_Crc"] = 0;
var result = jsonObj.ToJsonString();
Debug.Assert(result.Length * 2 <= len);
MemoryMarshal.Write(extraData.AsSpan(offset, sizeof(int)), result.Length * 2);
MemoryMarshal.AsBytes(result.AsSpan()).CopyTo(extraData.AsSpan(offset + sizeof(int)));
modified = true;
} else
Debug.Assert(false);
}
}
if (modified) {
json["m_ExtraDataString"] = Convert.ToBase64String(extraData);
using var fs = new FileStream(catalogJsonPath, FileMode.Create, FileAccess.Write, FileShare.None);
using var writer = new Utf8JsonWriter(fs, new() { Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping });
json.WriteTo(writer, new() { Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping });
}
}
private readonly struct EntryData {
#pragma warning disable CS0649
public readonly int InternalIdIndex;
public readonly int ProviderIndex;
public readonly int DependencyKey;
public readonly int DepHash;
public readonly int DataIndex;
public readonly int PrimaryKeyInde;
public readonly int ResourceTypeIndex;
#pragma warning restore CS0649
}
/// <exception cref="DirectoryNotFoundException"/>
[DoesNotReturn, DebuggerNonUserCode]
private static void ThrowDirectoryNotFound(string folderPath)
=> throw new DirectoryNotFoundException("The input folder does not exist: " + Path.GetFullPath(folderPath));
/// <exception cref="FileNotFoundException"/>
[DoesNotReturn, DebuggerNonUserCode]
private static void ThrowCatalogNotFound(string catalogJsonPath)
=> throw new FileNotFoundException("The catalog.json file does not exist: " + Path.GetFullPath(catalogJsonPath));
/// <exception cref="FileNotFoundException"/>
[DoesNotReturn, DebuggerNonUserCode]
private static void ThrowJsonFileNotFound(string jsonFileName)
=> throw new FileNotFoundException("The json file does not exist: " + jsonFileName);
/// <exception cref="DirectoryNotFoundException"/>
[DoesNotReturn, DebuggerNonUserCode]
private static void ThrowKeyNotFound(string key)
=> throw new KeyNotFoundException($"The key {key} is not found in the json file");
}
}