-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
532 lines (488 loc) · 27.2 KB
/
Program.cs
File metadata and controls
532 lines (488 loc) · 27.2 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
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Reflection;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
namespace GFTextMerge
{
using AssetBundles;
using JsonObject;
class Program
{
private static Settings Settings;
private static DirectoryInfo TSourceDir, TDestDir, TResultDir;
static void Main(string[] args)
{
string executedFileName = Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location);
string executedFileNameSpace = string.Empty.PadLeft(executedFileName.Length);
// 매개변수 부족할 경우
if (args.Length < 1) goto ParamException;
switch (args[0].ToLower())
{
default: goto ParamException;
case "merge":
{
if (args.Length < 2)
Console.WriteLine($"Usage: {executedFileName} merge <Source> <Destination>");
else
{
Console.WriteLine("Initializing...");
// 기본값 설정 생성
if (!File.Exists("Settings.json")) File.WriteAllBytes("Settings.json", Properties.Resources.Settings);
if (Directory.Exists("mismatch")) Directory.Delete("mismatch", true); Thread.Sleep(200); Directory.CreateDirectory("mismatch");
if (Directory.Exists("result")) Directory.Delete("result", true); Thread.Sleep(200); TResultDir = Directory.CreateDirectory("result");
if (!Directory.Exists("overrides")) Directory.CreateDirectory("overrides");
Thread.Sleep(200);
Settings = JsonConvert.DeserializeObject<Settings>(File.ReadAllText("Settings.json"));
// 매개변수 받은 항목 초기화
TSourceDir = new DirectoryInfo(args[1]);
TDestDir = new DirectoryInfo(args[2]);
List<FileInfo> destFiles = new List<FileInfo>(TDestDir.GetFiles());
for (int i = destFiles.Count - 1; i >= 0; i--)
{
foreach (var content in Settings.Contents)
foreach (var file in content.Files)
if (file == RemovePathID(destFiles[i].Name))
{
destFiles.RemoveAt(i);
goto Continue;
}
foreach (var locale in Settings.Locales)
foreach (var file in locale.Files)
if (file == RemovePathID(destFiles[i].Name))
{
destFiles.RemoveAt(i);
goto Continue;
}
Continue:;
}
if (destFiles.Count > 0)
{
StringBuilder builder = new StringBuilder();
foreach (var item in destFiles)
builder.AppendLine(RemovePathID(item.Name));
if (Settings.MismatchLog)
File.AppendAllText($@".\mismatch\Unexcepted Files.txt", $"{builder.ToString()}\r\n");
}
// 통합 항목 병합
foreach (var content in Settings.Contents)
{
if (!content.Usage) continue;
if (content.Delete)
{
if (TDestDir.FullName != TResultDir.FullName) continue;
foreach (var assetName in content.Files)
{
foreach (var asset in TSourceDir.GetFiles($"{assetName}-*", SearchOption.TopDirectoryOnly))
asset.Delete();
foreach (var asset in TDestDir.GetFiles($"{assetName}-*", SearchOption.TopDirectoryOnly))
asset.Delete();
}
}
else if (content.Name == "RawCopy") CloneContents(content);
else if (content.Regex != null) ReplaceContents(content);
}
// 언어별 항목 병합
foreach (var locale in Settings.Locales)
{
if (!locale.Usage) continue;
switch (locale.Name)
{
case "AVG":
{
DirectoryInfo overrideDir = new DirectoryInfo("overrides");
FileInfo[] sources = TSourceDir.GetFiles($"{locale.BaseFileName}_{Settings.Source}-*", SearchOption.TopDirectoryOnly),
dests = TDestDir.GetFiles($"{locale.BaseFileName}_{Settings.Destination}-*", SearchOption.TopDirectoryOnly),
overrides = null;
List<string> overridePaths = null;
if (Settings.UseOverride && overrideDir.Exists)
{
overrides = overrideDir.GetFiles($"{locale.BaseFileName}*", SearchOption.AllDirectories);
if (overrides.Length > 0)
{
overridePaths = new List<string>();
foreach (var item in overrides)
overridePaths.Add(item.FullName);
}
}
if (sources.Length > 0)
foreach (var dest in dests)
ReplaceSingleContent(locale.Regex, sources[0].FullName, dest.FullName, Path.Combine(TResultDir.FullName, dest.Name), overridePaths?.Count > 0 ? overridePaths.ToArray() : null);
if (Settings.RemoveDummy)
{
FileInfo[] datas = TDestDir.GetFiles($"{locale.BaseFileName}_*", SearchOption.TopDirectoryOnly);
foreach (var data in datas)
{
if (data.Name.Contains(Settings.Destination)) continue;
ClearSingleContent(locale.Regex, data.FullName, Path.Combine(TResultDir.FullName, data.Name));
}
}
}
break;
case "CFG":
{
DirectoryInfo overrideDir = new DirectoryInfo("overrides");
FileInfo[] sources = TSourceDir.GetFiles($"{locale.BaseFileName}_{Settings.Source}-*", SearchOption.TopDirectoryOnly);
FileInfo[] dests = TDestDir.GetFiles($"{locale.BaseFileName}_{Settings.Destination}-*", SearchOption.TopDirectoryOnly);
if (Settings.UseOverride && overrideDir.Exists)
{
FileInfo[] overrides = overrideDir.GetFiles($"{locale.BaseFileName}*", SearchOption.AllDirectories);
if (overrides.Length > 0)
{
foreach (var item in dests)
{
Console.WriteLine($"Copying \"{Path.GetFileName(item.Name)}\"...");
overrides[0].CopyTo(Path.Combine(TResultDir.FullName, item.Name), true);
}
continue;
}
}
if (sources.Length > 0)
foreach (var item in dests)
{
Console.WriteLine($"Copying \"{Path.GetFileName(item.Name)}\"...");
sources[0].CopyTo(Path.Combine(TResultDir.FullName, item.Name), true);
}
if (Settings.RemoveDummy)
{
FileInfo[] CFGs = TDestDir.GetFiles($"{locale.BaseFileName}_*", SearchOption.TopDirectoryOnly);
if (CFGs.Length > 0)
{
FileInfo SmallCFG = CFGs[0];
for (int i = 0; i < CFGs.Length; i++)
{
if (SmallCFG.Length > CFGs[i].Length)
SmallCFG = CFGs[i];
if (CFGs[i].Name.Contains(Settings.Source)
|| CFGs[i].Name.Contains(Settings.Destination))
CFGs[i] = null;
}
for (int i = 0; i < CFGs.Length; i++)
if (CFGs[i] != null && CFGs[i] != SmallCFG)
{
Console.WriteLine($"Clearing \"{Path.GetFileName(CFGs[i].Name)}\"...");
SmallCFG.CopyTo(Path.Combine(TResultDir.FullName, CFGs[i].Name), true);
}
}
}
}
break;
case "Data":
{
DirectoryInfo overrideDir = new DirectoryInfo("overrides");
FileInfo[] sources = TSourceDir.GetFiles(Settings.Source != "CN" ? $"{locale.BaseFileName}_{Settings.Source}-*"
: $"{locale.BaseFileName}-*", SearchOption.TopDirectoryOnly),
dests = TDestDir.GetFiles(Settings.Destination != "CN" ? $"{locale.BaseFileName}_{Settings.Destination}-*"
: $"{locale.BaseFileName}-*", SearchOption.TopDirectoryOnly),
overrides = null;
if (Settings.UseOverride && overrideDir.Exists)
overrides = overrideDir.GetFiles(Settings.Source != "CN" ? $"{locale.BaseFileName}_{Settings.Destination}*"
: $"{locale.BaseFileName}*", SearchOption.AllDirectories);
if (sources.Length > 0)
foreach (var dest in dests)
ReplaceSingleContent(locale.Regex
, sources[0].FullName
, dest.FullName
, Path.Combine(TResultDir.FullName, dest.Name)
, overrides?.Length > 0 ? new string[] { overrides[0].FullName } : null);
if (Settings.RemoveDummy)
{
FileInfo[] datas = TDestDir.GetFiles($"{locale.BaseFileName}*", SearchOption.TopDirectoryOnly);
foreach (var data in datas)
{
if (data.Name.Contains(Settings.Destination != "CN" ? $"{locale.BaseFileName}_{Settings.Destination}-"
: $"{locale.BaseFileName}-")) continue;
ClearSingleContent(locale.Regex, data.FullName, Path.Combine(TResultDir.FullName, data.Name));
}
}
}
break;
}
}
Console.WriteLine("Operation Complete");
}
break;
}
case "pack":
{
if (args.Length < 2)
{
Console.WriteLine($"Usage: {executedFileName} pack <Source>");
Console.WriteLine($" {executedFileNameSpace} pack <Source>.xml <CompressionType>");
Console.WriteLine($" {executedFileNameSpace} LZMA");
Console.WriteLine($" {executedFileNameSpace} LZ4");
Console.WriteLine($" {executedFileNameSpace} LZ4HC");
Console.WriteLine($" {executedFileNameSpace} NONE");
}
else
{
string str = args[1];
AssetBundle assetBundle = new AssetBundle();
if (File.Exists(str))
if (Path.GetExtension(str).ToLower() == ".xml")
{
assetBundle.LoadFromXml(str);
if (args.Length > 1)
switch (args[2].ToLower())
{
case "lz4":
assetBundle.compression = CompressionType.LZ4;
break;
case "lz4hc":
assetBundle.compression = CompressionType.LZ4HC;
break;
case "lzma":
assetBundle.compression = CompressionType.LZMA;
break;
case "none":
assetBundle.compression = CompressionType.NONE;
break;
}
assetBundle.Create();
}
else
try
{
using (FileStream fileStream = File.Open(str, FileMode.Open))
using (BinaryReader file = new BinaryReader(fileStream))
{
assetBundle.Load(file, str);
assetBundle.dump();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
break;
}
}
return;
ParamException:
Console.WriteLine($"Usage: {executedFileName} merge <Source> <Destination>");
Console.WriteLine($" {executedFileNameSpace} pack <Source>");
Console.WriteLine($" {executedFileNameSpace} pack <Source>.xml <CompressionType>");
Console.WriteLine($" {executedFileNameSpace} LZMA");
Console.WriteLine($" {executedFileNameSpace} LZ4");
Console.WriteLine($" {executedFileNameSpace} LZ4HC");
Console.WriteLine($" {executedFileNameSpace} NONE");
}
private static string RemovePathID(string filename)
=> Path.GetFileNameWithoutExtension(filename).Split('-')[0];
private static bool CompareStreams(Stream a, Stream b)
{
if (a == null && b == null) return true;
if (a == null || b == null) return false;
if (a.Length != b.Length ) return false;
for (int i = 0; i < a.Length; i++)
if (a.ReadByte().CompareTo(b.ReadByte()) != 0)
return false;
return true;
}
public static bool CompareFile(string pSource, string pDest)
=> CompareFile(new FileInfo(pSource), new FileInfo(pDest));
public static bool CompareFile(FileInfo pSource, FileInfo pDest)
{
if (pSource == null && pDest == null) return true;
if (pSource == null || pDest == null) return false;
if (pSource.Exists && pDest.Exists
&& pSource.Length == pDest.Length)
using (FileStream sourceFile = pSource.OpenRead())
using (FileStream destFile = pDest.OpenRead())
if (CompareStreams(sourceFile, destFile))
{
sourceFile.Close();
destFile.Close();
return true;
}
return false;
}
public static void ClearContents(Content pContent)
{
foreach (var item in pContent.Files)
foreach (var dest in TDestDir.GetFiles($"{item}-*", SearchOption.TopDirectoryOnly))
{
if (dest == null) continue;
ClearSingleContent(pContent.Regex, dest.FullName, Path.Combine(TResultDir.FullName, dest.Name));
}
}
public static void ClearSingleContent(RegexPreset pRegex, string pDest, string pResult)
{
if (pRegex == null) throw new ArgumentNullException(nameof(pRegex));
Console.WriteLine($"Clearing \"{Path.GetFileName(pResult)}\"...");
List<string[]> dList = new List<string[]>();
foreach (string item in File.ReadAllLines(pDest)) dList.Add(Regex.Split(item, pRegex.Search));
StringBuilder builder = new StringBuilder();
foreach (string[] dItem in dList)
{
builder.AppendFormat(pRegex.Empty, dItem);
builder.AppendLine();
}
if (File.Exists(pResult)) File.Delete(pResult);
File.WriteAllText(pResult, builder.ToString());
}
public static void CloneContents(Content pContent)
{
if (pContent == null) throw new ArgumentNullException(nameof(pContent));
DirectoryInfo overrideDir = new DirectoryInfo("overrides");
foreach (var item in pContent.Files)
{
FileInfo[] sources = TSourceDir.GetFiles($"{item}-*", SearchOption.TopDirectoryOnly),
dests = TDestDir.GetFiles($"{item}-*", SearchOption.TopDirectoryOnly),
overrides = null;
if (Settings.UseOverride && overrideDir.Exists)
{
overrides = overrideDir.GetFiles($"{item}*", SearchOption.AllDirectories);
if (overrides.Length > 0)
{
foreach (var dest in dests)
{
if (dest == null) continue;
Console.WriteLine($"Copying \"{dest.Name}\"...");
overrides[0].CopyTo(Path.Combine(TResultDir.FullName, dest.Name), true);
}
continue;
}
}
if (sources.Length > 1 || dests.Length > 1)
{
for (int s = 0; s < sources.Length; s++)
for (int d = 0; d < dests.Length; d++)
if (CompareFile(sources[s], dests[d]))
sources[s] = dests[d] = null;
}
foreach (var source in sources)
{
if (source == null) continue;
foreach (var dest in dests)
{
if (dest == null) continue;
Console.WriteLine($"Copying \"{dest.Name}\"...");
source.CopyTo(Path.Combine(TResultDir.FullName, dest.Name), true);
}
break;
}
}
}
public static void ReplaceContents(Content pContent)
{
if (pContent == null) throw new ArgumentNullException(nameof(pContent));
DirectoryInfo overrideDir = new DirectoryInfo("overrides");
foreach (var item in pContent.Files)
{
FileInfo[] sources = TSourceDir.GetFiles($"{item}-*", SearchOption.TopDirectoryOnly),
dests = TDestDir.GetFiles($"{item}-*", SearchOption.TopDirectoryOnly),
overrides = null;
List<string> overridePaths = null;
if (Settings.UseOverride && overrideDir.Exists)
{
overrides = overrideDir.GetFiles($"{item}*", SearchOption.AllDirectories);
if(overrides.Length > 0)
{
overridePaths = new List<string>();
foreach (var file in overrides) overridePaths.Add(file.FullName);
}
}
if (sources.Length > 1 || dests.Length > 1)
{
for (int s = 0; s < sources.Length; s++)
for (int d = 0; d < dests.Length; d++)
if (CompareFile(sources[s], dests[d]))
sources[s] = dests[d] = null;
}
foreach (var source in sources)
{
if (source == null) continue;
foreach (var dest in dests)
{
if (dest == null) continue;
ReplaceSingleContent(pContent.Regex
, source.FullName
, dest.FullName
, Path.Combine(TResultDir.FullName, dest.Name)
, overridePaths?.Count > 0 ? overridePaths.ToArray() : null);
}
break;
}
}
}
public static void ReplaceSingleContent(RegexPreset pRegex, string pSource, string pDest, string pResult, string[] pOverride = null)
{
if (pRegex == null) throw new ArgumentNullException(nameof(pRegex));
Console.WriteLine($"Writing \"{Path.GetFileName(pResult)}\"...");
List<string[]> sList = new List<string[]>(),
dList = new List<string[]>(),
oList = null;
foreach (string item in File.ReadAllLines(pSource)) sList.Add(Regex.Split(item, pRegex.Search));
foreach (string item in File.ReadAllLines(pDest)) dList.Add(Regex.Split(item, pRegex.Search));
if (Settings.UseOverride && pOverride != null)
{
oList = new List<string[]>();
foreach (string path in pOverride)
foreach (string item in File.ReadAllLines(path))
oList.Add(Regex.Split(item, pRegex.Search));
}
int lines = 1;
StringBuilder builder = new StringBuilder();
foreach (string[] dItem in dList)
{
if (dItem == null) continue;
for (int i = 1; i < dItem.Length - 1; i++)
if (pRegex.PrimaryKey.IndexOf(i) == -1
&& (string.IsNullOrWhiteSpace(dItem[i])
|| dItem[i].Contains("(todo)")))
{
builder.AppendFormat(pRegex.Empty, dItem);
goto Continue;
}
string[] sItem = sList.Find(temp =>
{
if (temp.Length != dItem.Length) return false;
for (int i = 1; i < temp.Length; i++)
{
if (pRegex.PrimaryKey.IndexOf(i) != -1
&& temp[i] != dItem[i]) return false;
}
return true;
});
if (oList != null)
{
string[] oItem = oList.Find(temp =>
{
if (temp.Length != dItem.Length) return false;
for (int i = 1; i < temp.Length; i++)
{
if (pRegex.PrimaryKey.IndexOf(i) != -1
&& temp[i] != dItem[i]) return false;
}
return true;
});
if (oItem != null) sItem = oItem;
}
if (sItem != null)
{
for (int i = 1; i < sItem.Length - 1; i++)
if (pRegex.PrimaryKey.IndexOf(i) == -1
&& (string.IsNullOrWhiteSpace(sItem[i])
|| sItem[i].Contains("(todo)")))
goto SourceNotExist;
builder.AppendFormat(pRegex.Replace, sItem);
goto Continue;
}
SourceNotExist:
string result = string.Format(pRegex.Replace, dItem);
builder.Append(result);
if (Settings.MismatchLog)
File.AppendAllText($@".\mismatch\{RemovePathID(pDest)}.txt", $"{result}\r\n");
Continue: builder.AppendLine();
lines++;
}
if (File.Exists(pResult)) File.Delete(pResult);
File.WriteAllText(pResult, builder.ToString());
}
}
}