-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
750 lines (644 loc) · 32.2 KB
/
Copy pathProgram.cs
File metadata and controls
750 lines (644 loc) · 32.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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using Microsoft.Win32;
using Mono.Cecil;
using Mono.Cecil.Cil;
namespace TerrariaSaveConverter
{
public enum Platform { Unknown, International, China }
public static class Logger
{
private static readonly string LogFile = "Converter_Debug.log";
private static readonly object _lock = new object();
public static bool EnableVerbose { get; set; } = false;
public static void Init()
{
if (!EnableVerbose) return;
try
{
File.WriteAllText(LogFile, $"--- Terraria Save Converter Log Started at {DateTime.Now:yyyy-MM-dd HH:mm:ss} ---\n", Encoding.UTF8);
}
catch { }
}
public static void Info(string msg, bool writeLine = true) => Log("INFO", msg, writeLine, ConsoleColor.White);
public static void Warn(string msg, bool writeLine = true) => Log("WARN", msg, writeLine, ConsoleColor.Yellow);
public static void Error(string msg, bool writeLine = true) => Log("ERROR", msg, writeLine, ConsoleColor.Red);
public static void Success(string msg, bool writeLine = true) => Log("SUCCESS", msg, writeLine, ConsoleColor.Green);
public static void Verbose(string msg, bool writeLine = true)
{
if (!EnableVerbose) return;
Log("DEBUG", msg, writeLine, ConsoleColor.DarkGray);
}
private static void Log(string level, string msg, bool writeLine, ConsoleColor color)
{
if (level == "DEBUG" && !EnableVerbose) return;
Console.ForegroundColor = color;
if (writeLine) Console.WriteLine(msg); else Console.Write(msg);
Console.ResetColor();
if (EnableVerbose)
{
try
{
lock (_lock)
{
string time = DateTime.Now.ToString("HH:mm:ss.fff");
string fileMsg = writeLine ? $"[{time}][{level}] {msg}\n" : msg;
File.AppendAllText(LogFile, fileMsg, Encoding.UTF8);
}
}
catch { }
}
}
}
class Program
{
private static readonly byte[] ENCRYPTION_KEY = Encoding.Unicode.GetBytes("h3y_gUyZ");
private const ulong INTL_MAGIC = 0x006369676F6C6572; // "cigoler"
private const ulong CN_MAGIC = 0x00676E6F646E6978; // "gnodnix"
private static int INTL_TILE_COUNT = 625;
private static int INTL_WALL_COUNT = 316;
static void Main(string[] args)
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
Console.OutputEncoding = Encoding.UTF8;
var rawArgs = args.ToList();
if (rawArgs.Contains("-v") || rawArgs.Contains("--verbose"))
{
Logger.EnableVerbose = true;
rawArgs.Remove("-v");
rawArgs.Remove("--verbose");
}
Logger.Init();
if (rawArgs.Count < 2)
{
Console.WriteLine("==================================================");
Console.WriteLine(" 泰拉瑞亚 存档全自动双向转换工具");
Console.WriteLine("==================================================");
Console.WriteLine("用法: TerrariaSaveConverter <输入路径或ZIP包> <目标平台(cn/intl)> [-v|--verbose]");
return;
}
Logger.Info("==================================================");
Logger.Info(" 泰拉瑞亚 存档全自动双向转换工具");
Logger.Info("==================================================");
TryLoadIntlConstants(out INTL_TILE_COUNT, out INTL_WALL_COUNT);
string inputPath = rawArgs[0];
string targetStr = rawArgs[1].ToLower();
Platform targetPlatform = targetStr == "cn" ? Platform.China : Platform.International;
if (targetPlatform == Platform.Unknown || !File.Exists(inputPath))
{
Logger.Error($"目标平台无效,或未找到指定输入路径: {inputPath}");
return;
}
string ext = Path.GetExtension(inputPath).ToLower();
string outputPath = inputPath.Replace(ext, $"_{targetStr}{ext}");
try
{
if (ext == ".zip")
ProcessZipArchive(inputPath, outputPath, targetPlatform);
else
ProcessSingleFile(inputPath, outputPath, targetPlatform);
Logger.Success("\n🎉 转换流程全部顺利结束!");
}
catch (Exception ex)
{
Logger.Error($"发生致命错误: {ex.Message}");
Logger.Verbose(ex.StackTrace ?? "");
}
}
static void ProcessZipArchive(string inputZipPath, string outputZipPath, Platform targetPlatform)
{
Logger.Info($"\n📦 正在处理 ZIP 归档包: {Path.GetFileName(inputZipPath)}");
if (File.Exists(outputZipPath)) File.Delete(outputZipPath);
Encoding safeEncoding = DetectZipEncoding(inputZipPath);
using ZipArchive sourceZip = ZipFile.Open(inputZipPath, ZipArchiveMode.Read, safeEncoding);
using ZipArchive targetZip = ZipFile.Open(outputZipPath, ZipArchiveMode.Create, Encoding.UTF8);
foreach (ZipArchiveEntry entry in sourceZip.Entries)
{
if (entry.FullName.EndsWith("/")) continue;
string entryExt = Path.GetExtension(entry.Name).ToLower();
bool isSaveFile = entryExt == ".plr" || entryExt == ".wld" || entryExt == ".map" || entry.Name.Contains(".bak");
if (!isSaveFile)
{
ZipArchiveEntry newEntry = targetZip.CreateEntry(entry.FullName);
using Stream sIn = entry.Open();
using Stream sOut = newEntry.Open();
sIn.CopyTo(sOut);
continue;
}
byte[] fileBytes;
using (Stream entryStream = entry.Open())
using (MemoryStream ms = new MemoryStream())
{
entryStream.CopyTo(ms);
fileBytes = ms.ToArray();
}
if (Logger.EnableVerbose) Logger.Info($"\n -> [解析文件] {entry.FullName}");
else Logger.Info($" -> [处理] {entry.FullName} ... ", false);
byte[] processedBytes = ConvertSavePayload(fileBytes, entry.Name, targetPlatform);
ZipArchiveEntry targetEntry = targetZip.CreateEntry(entry.FullName);
using Stream outStream = targetEntry.Open();
outStream.Write(processedBytes, 0, processedBytes.Length);
}
}
static void ProcessSingleFile(string inputPath, string outputPath, Platform targetPlatform)
{
byte[] rawBytes = File.ReadAllBytes(inputPath);
if (Logger.EnableVerbose) Logger.Info($"\n📄 正在解析单文件: {Path.GetFileName(inputPath)}");
else Logger.Info($"📄 正在处理 {Path.GetFileName(inputPath)} ... ", false);
byte[] processedBytes = ConvertSavePayload(rawBytes, Path.GetFileName(inputPath), targetPlatform);
File.WriteAllBytes(outputPath, processedBytes);
}
static byte[] ConvertSavePayload(byte[] fileBytes, string fileName, Platform targetPlatform)
{
string fileNameLower = fileName.ToLower();
bool isEncrypted = fileNameLower.EndsWith(".plr") || fileNameLower.EndsWith(".plr.bak");
if (isEncrypted) Logger.Verbose($" [AES 解密] 目标文件受加密保护,正在解密...");
byte[] processData = isEncrypted ? DecryptAES(fileBytes) : fileBytes;
if (processData.Length < 12)
{
if (Logger.EnableVerbose) Logger.Warn(" └─ [跳过] 文件过小或损坏");
else Logger.Warn("跳过 (文件损坏)");
return fileBytes;
}
using MemoryStream ms = new MemoryStream(processData);
using BinaryReader br = new BinaryReader(ms);
int versionRaw = br.ReadInt32();
bool isCompressed = (versionRaw & 0x8000) == 0x8000;
int trueVersion = versionRaw & ~0x8000;
bool hasMetadata = trueVersion >= 135;
Logger.Verbose($"\n [文件解析] Version: {trueVersion}, isCompressed: {isCompressed}, hasMetadata: {hasMetadata}");
if (fileNameLower.EndsWith(".map") || fileNameLower.EndsWith(".map.bak"))
{
Platform currentPlatform = Platform.Unknown;
byte fileType = 3;
if (hasMetadata)
{
ulong magic = br.ReadUInt64();
fileType = (byte)((magic >> 56) & 0xFF);
ulong baseMagic = magic & 0x00FFFFFFFFFFFFFF;
if (baseMagic == INTL_MAGIC) currentPlatform = Platform.International;
else if (baseMagic == CN_MAGIC) currentPlatform = Platform.China;
}
// 核心修复:即使不是分块压缩格式(如 PC 版原生 V2 地图),只要有 Metadata,也必须修改 Magic Number!
if (!isCompressed)
{
if (hasMetadata && currentPlatform != Platform.Unknown && currentPlatform != targetPlatform)
{
ulong newMagic = (targetPlatform == Platform.China ? CN_MAGIC : INTL_MAGIC) | ((ulong)fileType << 56);
Logger.Verbose($" [特征码] {(currentPlatform == Platform.China ? "CN" : "INTL")} -> {(targetPlatform == Platform.China ? "CN" : "INTL")} (非分块流地图)");
ms.Seek(4, SeekOrigin.Begin);
using (BinaryWriter bw = new BinaryWriter(ms, Encoding.UTF8, true))
{
bw.Write(newMagic);
}
if (Logger.EnableVerbose) Logger.Success(" └─ [完成] 无损转换 (PC原生连续流,仅切换特征码)");
else Logger.Success("成功 (无损切换特征码)");
return ms.ToArray();
}
Logger.Verbose(" [拦截] 地图非 Zlib 分块压缩格式,且平台匹配或未知,已安全放行。");
if (Logger.EnableVerbose) Logger.Success(" └─ [跳过] 格式不符或无需转换");
else Logger.Success("跳过 (无需转换)");
return processData;
}
ms.Seek(0, SeekOrigin.Begin);
processData = ProcessMapData(ms, currentPlatform, targetPlatform, hasMetadata);
}
else
{
if (!hasMetadata)
{
Logger.Verbose(" [拦截] 文件无元数据,无法进行解析。");
if (Logger.EnableVerbose) Logger.Success(" └─ [跳过] 无元数据");
else Logger.Success("跳过 (无元数据)");
return fileBytes;
}
ulong magic = br.ReadUInt64();
byte fileType = (byte)((magic >> 56) & 0xFF);
ulong baseMagic = magic & 0x00FFFFFFFFFFFFFF;
Platform currentPlatform = Platform.Unknown;
if (baseMagic == INTL_MAGIC) currentPlatform = Platform.International;
else if (baseMagic == CN_MAGIC) currentPlatform = Platform.China;
if (currentPlatform == targetPlatform || currentPlatform == Platform.Unknown)
{
Logger.Verbose(" [拦截] 平台已匹配或格式未知,已安全放行。");
if (Logger.EnableVerbose) Logger.Success(" └─ [跳过] 平台一致或未知");
else Logger.Success("跳过 (无需转换)");
return fileBytes;
}
ulong newMagic = (targetPlatform == Platform.China ? CN_MAGIC : INTL_MAGIC) | ((ulong)fileType << 56);
Logger.Verbose($" [特征码] {baseMagic:X16} -> {(newMagic & 0x00FFFFFFFFFFFFFF):X16}");
ms.Seek(4, SeekOrigin.Begin);
using (BinaryWriter bw = new BinaryWriter(ms, Encoding.UTF8, true))
{
bw.Write(newMagic);
}
processData = ms.ToArray();
if (Logger.EnableVerbose) Logger.Success(" └─ [完成] 成功切换文件特征码");
else Logger.Success("成功 (切换特征码)");
}
if (isEncrypted)
{
Logger.Verbose($" [AES 加密] 重新加密数据流...");
byte[] encryptedData = EncryptAES(processData);
Array.Resize(ref encryptedData, fileBytes.Length);
return encryptedData;
}
return processData;
}
static byte[] ProcessMapData(MemoryStream mapStream, Platform from, Platform to, bool hasMetadata)
{
using BinaryReader br = new BinaryReader(mapStream);
int versionNum = br.ReadInt32();
byte fileType = 3;
uint revision = 0;
ulong isFav = 0;
if (hasMetadata)
{
ulong magic = br.ReadUInt64();
fileType = (byte)((magic >> 56) & 0xFF);
revision = br.ReadUInt32();
isFav = br.ReadUInt64();
}
string mapName = br.ReadString();
int worldId = br.ReadInt32();
int maxY = br.ReadInt32();
int maxX = br.ReadInt32();
short num4 = br.ReadInt16();
short num5 = br.ReadInt16();
short num6 = br.ReadInt16();
short num7 = br.ReadInt16();
short num8 = br.ReadInt16();
short num9 = br.ReadInt16();
Logger.Verbose($" [Map Header] 读取到文件头 TileCount: {num4}, WallCount: {num5}");
Logger.Verbose($" [Engine Info] 当前 PC 引擎上限 TileCount: {INTL_TILE_COUNT}, WallCount: {INTL_WALL_COUNT}");
bool[] array3 = ReadBools(br, num4);
bool[] array4 = ReadBools(br, num5);
byte[] array5 = new byte[num4];
for (int i = 0; i < num4; i++) array5[i] = array3[i] ? br.ReadByte() : (byte)1;
byte[] array6 = new byte[num5];
for (int i = 0; i < num5; i++) array6[i] = array4[i] ? br.ReadByte() : (byte)1;
bool requiresChunkRewrite = (to == Platform.International) && (num4 > INTL_TILE_COUNT || num5 > INTL_WALL_COUNT);
if (!requiresChunkRewrite)
{
using MemoryStream quickMs = new MemoryStream();
quickMs.Write(mapStream.ToArray(), 0, (int)mapStream.Length);
if (hasMetadata)
{
quickMs.Seek(4, SeekOrigin.Begin);
using BinaryWriter quickBw = new BinaryWriter(quickMs, Encoding.UTF8, true);
ulong newMagic = (to == Platform.China ? CN_MAGIC : INTL_MAGIC) | ((ulong)fileType << 56);
quickBw.Write(newMagic);
quickBw.Flush();
}
if (Logger.EnableVerbose) Logger.Success(" └─ [完成] 无损转换 (字典已对齐,仅切换特征码)");
else Logger.Success("成功 (无损切换特征码)");
return quickMs.ToArray();
}
short newNum4 = (short)Math.Min(num4, INTL_TILE_COUNT);
short newNum5 = (short)Math.Min(num5, INTL_WALL_COUNT);
Logger.Verbose($" [Truncate] 执行字典截断 -> Tile: {newNum4}, Wall: {newNum5}");
Dictionary<ushort, ushort> dict = new Dictionary<ushort, ushort> { { 0, 0 } };
ushort srcId = 1, dstId = 1;
for (int i = 0; i < num4; i++)
{
bool isRetained = i < newNum4;
for (int j = 0; j < array5[i]; j++) dict.Add(srcId++, isRetained ? dstId++ : (ushort)0);
}
for (int i = 0; i < num5; i++)
{
bool isRetained = i < newNum5;
for (int j = 0; j < array6[i]; j++) dict.Add(srcId++, isRetained ? dstId++ : (ushort)0);
}
for (int i = 0; i < num6; i++) dict.Add(srcId++, dstId++);
for (int i = 0; i < num7; i++) dict.Add(srcId++, dstId++);
for (int i = 0; i < num8; i++) dict.Add(srcId++, dstId++);
for (int i = 0; i < num9; i++) dict.Add(srcId++, dstId++);
dict.Add(srcId++, dstId++); // 处理末尾默认背景层映射
Logger.Verbose($" [Dict Build] 成功重构字典, File Options: {srcId - 1}, Target Options: {dstId - 1}");
Dictionary<ushort, string>? auditMap = null;
if (Logger.EnableVerbose)
{
auditMap = new Dictionary<ushort, string>();
ushort aId = 1;
for (int i = 0; i < num4; i++)
for (int j = 0; j < array5[i]; j++) auditMap[aId++] = $"地砖(Tile) ID:{i}" + (array5[i] > 1 ? $" 变体:{j}" : "");
for (int i = 0; i < num5; i++)
for (int j = 0; j < array6[i]; j++) auditMap[aId++] = $"墙壁(Wall) ID:{i}" + (array6[i] > 1 ? $" 变体:{j}" : "");
for (int i = 0; i < num6; i++) auditMap[aId++] = $"天空层背景(Sky BG) 变体:{i}";
for (int i = 0; i < num7; i++) auditMap[aId++] = $"泥土层背景(Dirt BG) 变体:{i}";
for (int i = 0; i < num8; i++) auditMap[aId++] = $"岩石层背景(Rock BG) 变体:{i}";
for (int i = 0; i < num9; i++) auditMap[aId++] = $"地狱层背景(Hell BG) 变体:{i}";
auditMap[aId++] = "默认深渊背景";
}
using MemoryStream outMs = new MemoryStream();
using BinaryWriter bw = new BinaryWriter(outMs, Encoding.UTF8, true);
bw.Write(versionNum);
if (hasMetadata)
{
ulong newMagic = INTL_MAGIC | ((ulong)fileType << 56);
bw.Write(newMagic);
bw.Write(revision);
bw.Write(isFav);
}
bw.Write(mapName);
bw.Write(worldId);
bw.Write(maxY);
bw.Write(maxX);
bw.Write(newNum4);
bw.Write(newNum5);
bw.Write(num6);
bw.Write(num7);
bw.Write(num8);
bw.Write(num9);
bool[] newArray3 = new bool[newNum4];
Array.Copy(array3, newArray3, newNum4);
bool[] newArray4 = new bool[newNum5];
Array.Copy(array4, newArray4, newNum5);
byte[] newArray5 = new byte[newNum4];
Array.Copy(array5, newArray5, newNum4);
byte[] newArray6 = new byte[newNum5];
Array.Copy(array6, newArray6, newNum5);
WriteBools(bw, newNum4, newArray3);
WriteBools(bw, newNum5, newArray4);
for (int i = 0; i < newNum4; i++) if (newArray3[i]) bw.Write(newArray5[i]);
for (int i = 0; i < newNum5; i++) if (newArray4[i]) bw.Write(newArray6[i]);
int num18 = (maxX + 63) / 64;
int num19 = (maxY + 63) / 64;
int totalChunks = num18 * num19;
int erasedCount = 0;
Dictionary<ushort, int> erasedTypes = new Dictionary<ushort, int>();
for (int l = 0; l < totalChunks; l++)
{
int compSize = br.ReadInt32();
if (compSize > 0)
{
byte[] compData = br.ReadBytes(compSize);
byte[] chunkBuf = DecompressZlib(compData, 16384);
for (int i = 0; i < 4096; i++)
{
int typeOffset = i * 4;
ushort type = BitConverter.ToUInt16(chunkBuf, typeOffset);
if (type > 0)
{
if (!dict.ContainsKey(type) || dict[type] == 0)
{
if (!erasedTypes.ContainsKey(type)) erasedTypes[type] = 0;
erasedTypes[type]++;
erasedCount++;
chunkBuf[typeOffset] = 0;
chunkBuf[typeOffset + 1] = 0;
chunkBuf[typeOffset + 2] = 0;
chunkBuf[typeOffset + 3] = 0;
}
else
{
ushort newType = dict[type];
chunkBuf[typeOffset] = (byte)(newType & 0xFF);
chunkBuf[typeOffset + 1] = (byte)(newType >> 8);
}
}
}
byte[] newCompData = CompressZlib(chunkBuf);
bw.Write(newCompData.Length);
bw.Write(newCompData);
}
else
{
bw.Write(0);
}
}
if (Logger.EnableVerbose) Logger.Success($" └─ [完成] 有损转换 (字典对齐, 擦除越界像素: {erasedCount})");
else Logger.Success($"成功 (擦除像素: {erasedCount})");
if (erasedCount > 0 && Logger.EnableVerbose)
{
Logger.Verbose($" [擦除审计] 详细越界像素抹除清单 ({erasedCount} 个):");
foreach (var kvp in erasedTypes.OrderByDescending(k => k.Value))
{
string desc = auditMap != null && auditMap.ContainsKey(kvp.Key) ? auditMap[kvp.Key] : "未知数据结构";
Logger.Verbose($" - [Option {kvp.Key}] {desc} => 安全擦除 {kvp.Value} 个像素");
}
}
bw.Flush();
return outMs.ToArray();
}
static void TryLoadIntlConstants(out int intlTileCount, out int intlWallCount)
{
intlTileCount = 753;
intlWallCount = 367;
Logger.Verbose("\n🔍 正在通过 IL 静态解析引擎核心数据...");
string? exePath = null;
if (OperatingSystem.IsWindows())
{
try
{
using RegistryKey? key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\WOW6432Node\Valve\Steam");
string? steamPath = key?.GetValue("InstallPath")?.ToString();
if (!string.IsNullOrEmpty(steamPath))
{
string tPath = Path.Combine(steamPath, @"steamapps\common\Terraria\Terraria.exe");
if (File.Exists(tPath)) exePath = tPath;
}
}
catch { }
if (exePath == null)
{
foreach (DriveInfo drive in DriveInfo.GetDrives().Where(d => d.IsReady))
{
string[] winPaths = {
Path.Combine(drive.Name, @"SteamLibrary\steamapps\common\Terraria\Terraria.exe"),
Path.Combine(drive.Name, @"Steam\steamapps\common\Terraria\Terraria.exe"),
Path.Combine(drive.Name, @"Program Files (x86)\Steam\steamapps\common\Terraria\Terraria.exe")
};
exePath = winPaths.FirstOrDefault(File.Exists);
if (exePath != null) break;
}
}
}
if (string.IsNullOrEmpty(exePath))
{
Logger.Verbose(" [拦截] 磁盘中未找到 PC 版 Terraria.exe 引擎,回退至安全默认上限。");
return;
}
try
{
Logger.Verbose($" ✅ 成功定位引擎: {exePath}");
using ModuleDefinition module = ModuleDefinition.ReadModule(exePath);
var tileIdType = module.Types.FirstOrDefault(t => t.FullName == "Terraria.ID.TileID");
var wallIdType = module.Types.FirstOrDefault(t => t.FullName == "Terraria.ID.WallID");
if (tileIdType != null && wallIdType != null)
{
intlTileCount = GetFieldConstantFromIL(tileIdType, "Count");
intlWallCount = GetFieldConstantFromIL(wallIdType, "Count");
Logger.Verbose($" 🎯 IL 穿透提取成功! [TileCount: {intlTileCount} | WallCount: {intlWallCount}]");
}
else
{
Logger.Warn(" [警告] 装配件中未找到目标数据结构类。");
}
}
catch (Exception ex)
{
Logger.Error($" [异常] 静态解析发生异常: {ex.Message}");
}
}
static int GetFieldConstantFromIL(TypeDefinition typeDef, string fieldName)
{
var field = typeDef.Fields.FirstOrDefault(f => f.Name == fieldName);
if (field == null) throw new Exception($"无法找到对应字段 {fieldName}。");
if (field.HasConstant) return Convert.ToInt32(field.Constant);
var cctor = typeDef.Methods.FirstOrDefault(m => m.Name == ".cctor");
if (cctor != null && cctor.Body != null)
{
var instructions = cctor.Body.Instructions;
for (int i = instructions.Count - 1; i >= 0; i--)
{
var inst = instructions[i];
if (inst.OpCode == OpCodes.Stsfld && (inst.Operand as FieldReference)?.Name == fieldName)
{
int prevIndex = i - 1;
Instruction? prevInst = prevIndex >= 0 ? instructions[prevIndex] : null;
while (prevInst != null && (prevInst.OpCode == OpCodes.Nop || prevInst.OpCode == OpCodes.Conv_U2 || prevInst.OpCode == OpCodes.Conv_I4))
{
prevIndex--;
prevInst = prevIndex >= 0 ? instructions[prevIndex] : null;
}
if (prevInst == null) continue;
if (prevInst.OpCode == OpCodes.Ldc_I4 && prevInst.Operand != null) return (int)prevInst.Operand;
if (prevInst.OpCode == OpCodes.Ldc_I4_S && prevInst.Operand != null) return (sbyte)prevInst.Operand;
if (prevInst.OpCode.Name.StartsWith("ldc.i4."))
{
string numStr = prevInst.OpCode.Name.Substring(7);
if (numStr == "m1") return -1;
if (int.TryParse(numStr, out int val)) return val;
}
}
}
}
throw new Exception("底层指令匹配失败,无法从 IL 中安全提取常数。");
}
static bool[] ReadBools(BinaryReader br, int count)
{
bool[] arr = new bool[count];
byte b = 0, b2 = 128;
for (int i = 0; i < count; i++)
{
if (b2 == 128) { b = br.ReadByte(); b2 = 1; }
else { b2 <<= 1; }
arr[i] = (b & b2) == b2;
}
return arr;
}
static void WriteBools(BinaryWriter bw, int count, bool[] bools)
{
byte b = 0, b2 = 128;
for (int i = 0; i < count; i++)
{
if (b2 == 128) { b = 0; b2 = 1; }
else { b2 <<= 1; }
if (bools[i]) b |= b2;
if (b2 == 128) bw.Write(b);
}
if (b2 != 128) bw.Write(b);
}
static Encoding DetectZipEncoding(string zipPath)
{
Encoding fallbackEncoding = Encoding.GetEncoding("GBK");
try
{
Encoding rawEncoding = Encoding.GetEncoding("iso-8859-1");
using ZipArchive zip = ZipFile.Open(zipPath, ZipArchiveMode.Read, rawEncoding);
bool requiresUtf8 = false;
foreach (var entry in zip.Entries)
{
if (entry.FullName.Any(c => c > 255)) return Encoding.UTF8;
byte[] rawBytes = entry.FullName.Select(c => (byte)c).ToArray();
if (rawBytes.Any(b => b > 127))
{
if (IsValidUtf8(rawBytes)) requiresUtf8 = true;
else return fallbackEncoding;
}
}
if (requiresUtf8)
{
Logger.Verbose(" [编码识别] ZIP 内部路径编码推断为: Unicode (UTF-8)");
return Encoding.UTF8;
}
}
catch { }
Logger.Verbose(" [编码识别] ZIP 内部路径编码推断为: GBK (CP936)");
return fallbackEncoding;
}
static bool IsValidUtf8(byte[] bytes)
{
try { var utf8 = new UTF8Encoding(false, true); utf8.GetString(bytes); return true; }
catch { return false; }
}
static byte[] DecompressZlib(byte[] data, int expectedSize)
{
using MemoryStream msIn = new MemoryStream(data);
msIn.Seek(2, SeekOrigin.Begin);
using DeflateStream deflate = new DeflateStream(msIn, CompressionMode.Decompress);
byte[] outBuf = new byte[expectedSize];
int totalRead = 0;
while (totalRead < expectedSize)
{
int bytesRead = deflate.Read(outBuf, totalRead, expectedSize - totalRead);
if (bytesRead == 0) break;
totalRead += bytesRead;
}
return outBuf;
}
static byte[] CompressZlib(byte[] data)
{
using MemoryStream outMs = new MemoryStream();
outMs.WriteByte(0x78); outMs.WriteByte(0x9C);
using (DeflateStream deflate = new DeflateStream(outMs, CompressionLevel.Optimal, true))
{
deflate.Write(data, 0, data.Length);
}
uint adler = CalculateAdler32(data);
outMs.WriteByte((byte)(adler >> 24));
outMs.WriteByte((byte)(adler >> 16));
outMs.WriteByte((byte)(adler >> 8));
outMs.WriteByte((byte)adler);
return outMs.ToArray();
}
static uint CalculateAdler32(byte[] data)
{
uint a = 1, b = 0;
foreach (byte val in data)
{
a = (a + val) % 65521;
b = (b + a) % 65521;
}
return (b << 16) | a;
}
static byte[] DecryptAES(byte[] input)
{
int paddedLength = (int)Math.Ceiling(input.Length / 16.0) * 16;
byte[] padded = new byte[paddedLength];
Array.Copy(input, padded, input.Length);
using Aes aes = Aes.Create();
aes.Key = ENCRYPTION_KEY; aes.IV = ENCRYPTION_KEY;
aes.Mode = CipherMode.CBC; aes.Padding = PaddingMode.None;
using ICryptoTransform decryptor = aes.CreateDecryptor();
return decryptor.TransformFinalBlock(padded, 0, padded.Length);
}
static byte[] EncryptAES(byte[] input)
{
int paddedLength = (int)Math.Ceiling(input.Length / 16.0) * 16;
byte[] padded = new byte[paddedLength];
Array.Copy(input, padded, input.Length);
using Aes aes = Aes.Create();
aes.Key = ENCRYPTION_KEY; aes.IV = ENCRYPTION_KEY;
aes.Mode = CipherMode.CBC; aes.Padding = PaddingMode.None;
using ICryptoTransform encryptor = aes.CreateEncryptor();
return encryptor.TransformFinalBlock(padded, 0, padded.Length);
}
}
}