|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Security.Cryptography; |
| 4 | +using System.Text; |
| 5 | +using TACTSharp; |
| 6 | + |
| 7 | +namespace TACTTool.RunModes |
| 8 | +{ |
| 9 | + public static class Verify |
| 10 | + { |
| 11 | + public static void Run(BuildInstance buildInstance) |
| 12 | + { |
| 13 | + Console.WriteLine("Starting TACT CDN directory verification..."); |
| 14 | + Console.WriteLine("!!!"); |
| 15 | + Console.WriteLine("NOTE: This is currently a very basic implementation that only checks archive/loose file existence & sizes."); |
| 16 | + Console.WriteLine("!!!"); |
| 17 | + |
| 18 | + if (buildInstance.Settings.CDNConfig is null) |
| 19 | + { |
| 20 | + Console.WriteLine("!!! Warning, no CDN config given so will check entire directory. This may take a long time and may fail on SMB mounted shares."); |
| 21 | + Console.WriteLine("Also, this is not yet implemented."); |
| 22 | + throw new NotImplementedException(); |
| 23 | + } |
| 24 | + else |
| 25 | + { |
| 26 | + var config = new TACTSharp.Config(buildInstance.cdn, buildInstance.Settings.CDNConfig, false); |
| 27 | + |
| 28 | + var archiveCount = config.Values["archives"].Length; |
| 29 | + for (var i = 0; i < archiveCount; i++) |
| 30 | + { |
| 31 | + var archiveIndex = config.Values["archives"][i]; |
| 32 | + |
| 33 | + var indexPath = Path.Combine(buildInstance.Settings.CDNDir, "tpr", "wow", "data", archiveIndex[0..2], archiveIndex[2..4], archiveIndex + ".index"); |
| 34 | + if (!File.Exists(indexPath)) |
| 35 | + throw new FileNotFoundException(indexPath); |
| 36 | + |
| 37 | + var index = new IndexInstance(indexPath); |
| 38 | + |
| 39 | + var allFiles = index.GetAllEntries(); |
| 40 | + var highestOffset = allFiles.Select(x => x.offset + x.size).Max(); |
| 41 | + |
| 42 | + var archiveFileInfo = new FileInfo(indexPath.Replace(".index", "")); |
| 43 | + if (!archiveFileInfo.Exists) |
| 44 | + throw new FileNotFoundException(archiveFileInfo.FullName); |
| 45 | + var archiveLength = archiveFileInfo.Length; |
| 46 | + |
| 47 | + if (highestOffset != archiveLength) |
| 48 | + { |
| 49 | + Console.WriteLine($"!!! Archive {archiveIndex} has wrong size! Expected {highestOffset} bytes but only found {archiveLength} bytes."); |
| 50 | + } |
| 51 | + |
| 52 | + Console.Write("Checking archives.. " + (i + 1) + "/" + archiveCount + "\r"); |
| 53 | + } |
| 54 | + |
| 55 | + Console.WriteLine(); |
| 56 | + |
| 57 | + if (config.Values.TryGetValue("file-index", out string[]? fileIndexName)) |
| 58 | + { |
| 59 | + var fileIndexPath = Path.Combine(buildInstance.Settings.CDNDir, "tpr", "wow", "data", fileIndexName[0][0..2], fileIndexName[0][2..4], fileIndexName[0] + ".index"); |
| 60 | + var fileIndex = new IndexInstance(fileIndexPath); |
| 61 | + var allFiles = fileIndex.GetAllEntries(); |
| 62 | + var looseFileCount = allFiles.Count; |
| 63 | + for(var i = 0; i < looseFileCount; i++) |
| 64 | + { |
| 65 | + Console.Write("Checking loose files.. " + (i + 1) + "/" + looseFileCount + "\r"); |
| 66 | + var looseFile = allFiles[i]; |
| 67 | + var looseFileName = Convert.ToHexStringLower(looseFile.eKey); |
| 68 | + var looseFilePath = Path.Combine(buildInstance.Settings.CDNDir, "tpr", "wow", "data", looseFileName[0..2], looseFileName[2..4], looseFileName); |
| 69 | + var looseFileInfo = new FileInfo(looseFilePath); |
| 70 | + if (!looseFileInfo.Exists) |
| 71 | + throw new FileNotFoundException(looseFilePath); |
| 72 | + |
| 73 | + var looseFileSize = looseFileInfo.Length; |
| 74 | + var looseFileSupposedSize = looseFile.size; |
| 75 | + |
| 76 | + //var looseFileMD5 = Convert.ToHexStringLower(MD5.HashData(File.ReadAllBytes(looseFilePath))); |
| 77 | + //if(looseFileMD5 != looseFileName) |
| 78 | + //{ |
| 79 | + // Console.WriteLine($"!!! MD5 for file {looseFileName} is incorrect ({looseFileName} != {looseFileMD5}!"); |
| 80 | + //} |
| 81 | + |
| 82 | + if (looseFileSize != looseFileSupposedSize) |
| 83 | + { |
| 84 | + Console.WriteLine($"!!! Loose file {looseFileName} has wrong size! Expected {looseFileSupposedSize} bytes but only found {looseFileSize} bytes."); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + Console.WriteLine(); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments