forked from nesrak1/UABEA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommandLineHandler.cs
373 lines (308 loc) · 13.9 KB
/
CommandLineHandler.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
using AssetsTools.NET;
using AssetsTools.NET.Extra;
using System;
using System.Collections.Generic;
using System.IO;
namespace UABEAvalonia
{
public class CommandLineHandler
{
public static void PrintHelp()
{
Console.WriteLine("UABE AVALONIA");
Console.WriteLine("WARNING: Command line support VERY EARLY");
Console.WriteLine("There is a high chance of stuff breaking");
Console.WriteLine("Use at your own risk");
Console.WriteLine(" UABEAvalonia batchexportbundle <directory>");
Console.WriteLine(" UABEAvalonia batchimportbundle <directory>");
Console.WriteLine(" UABEAvalonia applyemip <emip file> <directory>");
Console.WriteLine("Bundle import/export arguments:");
Console.WriteLine(" -keepnames writes out to the exact file name in the bundle.");
Console.WriteLine(" Normally, file names are prepended with the bundle's name.");
Console.WriteLine(" Note: these names are not compatible with batchimport.");
Console.WriteLine(" -kd keep .decomp files. When UABEA opens compressed bundles,");
Console.WriteLine(" they are decompressed into .decomp files. If you want to");
Console.WriteLine(" decompress bundles, you can use this flag to keep them");
Console.WriteLine(" without deleting them.");
Console.WriteLine(" -fd overwrite old .decomp files.");
Console.WriteLine(" -md decompress into memory. Doesn't write .decomp files.");
Console.WriteLine(" -kd and -fd won't do anything with this flag set.");
}
private static string GetMainFileName(string[] args)
{
for (int i = 1; i < args.Length; i++)
{
if (!args[i].StartsWith("-"))
return args[i];
}
return string.Empty;
}
private static HashSet<string> GetFlags(string[] args)
{
HashSet<string> flags = new HashSet<string>();
for (int i = 1; i < args.Length; i++)
{
if (args[i].StartsWith("-"))
flags.Add(args[i]);
}
return flags;
}
private static AssetBundleFile DecompressBundle(string file, string? decompFile)
{
AssetBundleFile bun = new AssetBundleFile();
Stream fs = File.OpenRead(file);
AssetsFileReader r = new AssetsFileReader(fs);
bun.Read(r);
if (bun.Header.GetCompressionType() != 0)
{
Stream nfs;
if (decompFile == null)
nfs = new MemoryStream();
else
nfs = File.Open(decompFile, FileMode.Create, FileAccess.ReadWrite);
AssetsFileWriter w = new AssetsFileWriter(nfs);
bun.Unpack(w);
nfs.Position = 0;
fs.Close();
fs = nfs;
r = new AssetsFileReader(fs);
bun = new AssetBundleFile();
bun.Read(r);
}
return bun;
}
private static string GetNextBackup(string affectedFilePath)
{
for (int i = 0; i < 10000; i++)
{
string bakName = $"{affectedFilePath}.bak{i.ToString().PadLeft(4, '0')}";
if (!File.Exists(bakName))
{
return bakName;
}
}
Console.WriteLine("Too many backups, exiting for your safety.");
return null;
}
private static void BatchExportBundle(string[] args)
{
string exportDirectory = GetMainFileName(args);
if (!Directory.Exists(exportDirectory))
{
Console.WriteLine("Directory does not exist!");
return;
}
HashSet<string> flags = GetFlags(args);
foreach (string file in Directory.EnumerateFiles(exportDirectory))
{
string decompFile = $"{file}.decomp";
if (flags.Contains("-md"))
decompFile = null;
if (!File.Exists(file))
{
Console.WriteLine($"File {file} does not exist!");
return;
}
DetectedFileType fileType = AssetBundleDetector.DetectFileType(file);
if (fileType != DetectedFileType.BundleFile)
{
continue;
}
Console.WriteLine($"Decompressing {file}...");
AssetBundleFile bun = DecompressBundle(file, decompFile);
int entryCount = bun.BlockAndDirInfo.DirectoryInfos.Length;
for (int i = 0; i < entryCount; i++)
{
string name = bun.BlockAndDirInfo.DirectoryInfos[i].Name;
byte[] data = BundleHelper.LoadAssetDataFromBundle(bun, i);
string outName;
if (flags.Contains("-keepnames"))
outName = Path.Combine(exportDirectory, $"{name}.assets");
else
outName = Path.Combine(exportDirectory, $"{Path.GetFileName(file)}_{name}.assets");
Console.WriteLine($"Exporting {outName}...");
File.WriteAllBytes(outName, data);
}
bun.Close();
if (!flags.Contains("-kd") && !flags.Contains("-md") && File.Exists(decompFile))
File.Delete(decompFile);
Console.WriteLine("Done.");
}
}
private static void BatchImportBundle(string[] args)
{
string importDirectory = GetMainFileName(args);
if (!Directory.Exists(importDirectory))
{
Console.WriteLine("Directory does not exist!");
return;
}
HashSet<string> flags = GetFlags(args);
foreach (string file in Directory.EnumerateFiles(importDirectory))
{
string decompFile = $"{file}.decomp";
if (flags.Contains("-md"))
decompFile = null;
if (!File.Exists(file))
{
Console.WriteLine($"File {file} does not exist!");
return;
}
DetectedFileType fileType = AssetBundleDetector.DetectFileType(file);
if (fileType != DetectedFileType.BundleFile)
{
continue;
}
Console.WriteLine($"Decompressing {file} to {decompFile}...");
AssetBundleFile bun = DecompressBundle(file, decompFile);
List<BundleReplacer> reps = new List<BundleReplacer>();
List<Stream> streams = new List<Stream>();
int entryCount = bun.BlockAndDirInfo.DirectoryInfos.Length;
for (int i = 0; i < entryCount; i++)
{
string name = bun.BlockAndDirInfo.DirectoryInfos[i].Name;
string matchName = Path.Combine(importDirectory, $"{Path.GetFileName(file)}_{name}.assets");
if (File.Exists(matchName))
{
FileStream fs = File.OpenRead(matchName);
long length = fs.Length;
reps.Add(new BundleReplacerFromStream(name, name, true, fs, 0, length));
streams.Add(fs);
Console.WriteLine($"Importing {matchName}...");
}
}
//I guess uabe always writes to .decomp even if
//the bundle is already decompressed, that way
//here it can be used as a temporary file. for
//now I'll write to memory since having a .decomp
//file isn't guaranteed here
byte[] data;
using (MemoryStream ms = new MemoryStream())
using (AssetsFileWriter w = new AssetsFileWriter(ms))
{
bun.Write(w, reps);
data = ms.ToArray();
}
Console.WriteLine($"Writing changes to {file}...");
//uabe doesn't seem to compress here
foreach (Stream stream in streams)
stream.Close();
bun.Close();
File.WriteAllBytes(file, data);
if (!flags.Contains("-kd") && !flags.Contains("-md") && File.Exists(decompFile))
File.Delete(decompFile);
Console.WriteLine("Done.");
}
}
private static void ApplyEmip(string[] args)
{
HashSet<string> flags = GetFlags(args);
string emipFile = args[1];
string rootDir = args[2];
if (!File.Exists(emipFile))
{
Console.WriteLine($"File {emipFile} does not exist!");
return;
}
InstallerPackageFile instPkg = new InstallerPackageFile();
FileStream fs = File.OpenRead(emipFile);
AssetsFileReader r = new AssetsFileReader(fs);
instPkg.Read(r, true);
Console.WriteLine($"Installing emip...");
Console.WriteLine($"{instPkg.modName} by {instPkg.modCreators}");
Console.WriteLine(instPkg.modDescription);
foreach (var affectedFile in instPkg.affectedFiles)
{
string affectedFileName = Path.GetFileName(affectedFile.path);
string affectedFilePath = Path.Combine(rootDir, affectedFile.path);
if (affectedFile.isBundle)
{
string decompFile = $"{affectedFilePath}.decomp";
string modFile = $"{affectedFilePath}.mod";
string bakFile = GetNextBackup(affectedFilePath);
if (bakFile == null)
return;
if (flags.Contains("-md"))
decompFile = null;
Console.WriteLine($"Decompressing {affectedFileName} to {decompFile??"memory"}...");
AssetBundleFile bun = DecompressBundle(affectedFilePath, decompFile);
List<BundleReplacer> reps = new List<BundleReplacer>();
foreach (var rep in affectedFile.replacers)
{
var bunRep = (BundleReplacer)rep;
if (bunRep is BundleReplacerFromAssets)
{
//read in assets files from the bundle for replacers that need them
string assetName = bunRep.GetOriginalEntryName();
var bunRepInf = BundleHelper.GetDirInfo(bun, assetName);
long pos = bunRepInf.Offset;
bunRep.Init(bun.DataReader, pos, bunRepInf.DecompressedSize);
}
reps.Add(bunRep);
}
Console.WriteLine($"Writing {modFile}...");
FileStream mfs = File.Open(modFile, FileMode.Create);
AssetsFileWriter mw = new AssetsFileWriter(mfs);
bun.Write(mw, reps, instPkg.addedTypes); //addedTypes does nothing atm
mfs.Close();
bun.Close();
Console.WriteLine($"Swapping mod file...");
File.Move(affectedFilePath, bakFile);
File.Move(modFile, affectedFilePath);
if (!flags.Contains("-kd") && !flags.Contains("-md") && File.Exists(decompFile))
File.Delete(decompFile);
Console.WriteLine($"Done.");
}
else //isAssetsFile
{
string modFile = $"{affectedFilePath}.mod";
string bakFile = GetNextBackup(affectedFilePath);
if (bakFile == null)
return;
FileStream afs = File.OpenRead(affectedFilePath);
AssetsFileReader ar = new AssetsFileReader(afs);
AssetsFile assets = new AssetsFile();
assets.Read(ar);
List<AssetsReplacer> reps = new List<AssetsReplacer>();
foreach (var rep in affectedFile.replacers)
{
var assetsReplacer = (AssetsReplacer)rep;
reps.Add(assetsReplacer);
}
Console.WriteLine($"Writing {modFile}...");
FileStream mfs = File.Open(modFile, FileMode.Create);
AssetsFileWriter mw = new AssetsFileWriter(mfs);
assets.Write(mw, 0, reps, instPkg.addedTypes);
mfs.Close();
ar.Close();
Console.WriteLine($"Swapping mod file...");
File.Move(affectedFilePath, bakFile);
File.Move(modFile, affectedFilePath);
Console.WriteLine($"Done.");
}
}
return;
}
public static void CLHMain(string[] args)
{
if (args.Length < 2)
{
PrintHelp();
return;
}
string command = args[0];
if (command == "batchexportbundle")
{
BatchExportBundle(args);
}
else if (command == "batchimportbundle")
{
BatchImportBundle(args);
}
else if (command == "applyemip")
{
ApplyEmip(args);
}
}
}
}