|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Linq; |
| 4 | + |
| 5 | +class Program |
| 6 | +{ |
| 7 | + static void Main(string[] args) |
| 8 | + { |
| 9 | + Console.Write("Enter the directory of files: "); |
| 10 | + string directory = Console.ReadLine(); |
| 11 | + |
| 12 | + if (!Directory.Exists(directory)) |
| 13 | + { |
| 14 | + Console.WriteLine("Directory not found."); |
| 15 | + return; |
| 16 | + } |
| 17 | + |
| 18 | + string[] files = Directory.GetFiles(directory); |
| 19 | + string outputDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Content"); |
| 20 | + Directory.CreateDirectory(outputDirectory); |
| 21 | + |
| 22 | + string convertAllType = null; |
| 23 | + string ignoreAllType = null; |
| 24 | + |
| 25 | + foreach (var file in files) |
| 26 | + { |
| 27 | + try |
| 28 | + { |
| 29 | + byte[] fileBytes = File.ReadAllBytes(file); |
| 30 | + |
| 31 | + string fileContent = System.Text.Encoding.ASCII.GetString(fileBytes); |
| 32 | + int blfIndex = fileContent.IndexOf("_blf"); |
| 33 | + |
| 34 | + if (blfIndex == -1) |
| 35 | + continue; |
| 36 | + |
| 37 | + string content = System.Text.Encoding.ASCII.GetString(fileBytes.Skip(0xD0C0).Take(0x7F).ToArray()).Trim('\0'); |
| 38 | + string sanitizedContent = string.Concat(content.Split(Path.GetInvalidFileNameChars())); |
| 39 | + string creatorName = System.Text.Encoding.ASCII.GetString(fileBytes.Skip(0xD088).Take(0x0F).ToArray()).Trim('\0'); |
| 40 | + string creatorXUID = BitConverter.ToString(fileBytes.Skip(0xD080).Take(0x08).ToArray()).Replace("-", ""); |
| 41 | + string modifierName = System.Text.Encoding.ASCII.GetString(fileBytes.Skip(0xD0AC).Take(0x0F).ToArray()).Trim('\0'); |
| 42 | + string modifierXUID = BitConverter.ToString(fileBytes.Skip(0xD0A4).Take(0x08).ToArray()).Replace("-", ""); |
| 43 | + string description = System.Text.Encoding.ASCII.GetString(fileBytes.Skip(0xD1C0).Take(0xFF).ToArray()).Trim('\0'); |
| 44 | + string headerType = System.Text.Encoding.ASCII.GetString(fileBytes.Skip(0xD2F0).Take(0x04).ToArray()).Trim('\0'); |
| 45 | + string contentType = null; |
| 46 | + |
| 47 | + switch (headerType) |
| 48 | + { |
| 49 | + case "mpvr": |
| 50 | + contentType = "Gametypes"; |
| 51 | + break; |
| 52 | + case "mvar": |
| 53 | + contentType = "Map variants"; |
| 54 | + break; |
| 55 | + case "athr": |
| 56 | + contentType = "Theater films"; |
| 57 | + break; |
| 58 | + case "scnc": |
| 59 | + contentType = "Screenshots"; |
| 60 | + break; |
| 61 | + } |
| 62 | + |
| 63 | + Console.WriteLine(); |
| 64 | + Console.WriteLine($"File: {Path.GetFileName(file)}"); |
| 65 | + Console.WriteLine($"Creator: {creatorName} (XUID: {creatorXUID})"); |
| 66 | + Console.WriteLine($"Modifier: {modifierName} (XUID: {modifierXUID})"); |
| 67 | + Console.WriteLine($"Content: {content}"); |
| 68 | + Console.WriteLine($"Description: {description}"); |
| 69 | + Console.WriteLine($"Header Type: {headerType}"); |
| 70 | + |
| 71 | + if (headerType == ignoreAllType) |
| 72 | + { |
| 73 | + Console.WriteLine($"Skipping all files of type: {headerType}"); |
| 74 | + continue; |
| 75 | + } |
| 76 | + |
| 77 | + if (headerType == convertAllType) |
| 78 | + { |
| 79 | + Console.WriteLine($"Automatically converting all files of type: {headerType}"); |
| 80 | + } |
| 81 | + else |
| 82 | + { |
| 83 | + Console.Write("Would you like to convert this file? (y)es / (n)o / (a)ll of type / (i)gnore all of type: "); |
| 84 | + string response = Console.ReadLine()?.ToLower(); |
| 85 | + |
| 86 | + if (response == "a") |
| 87 | + { |
| 88 | + convertAllType = headerType; |
| 89 | + } |
| 90 | + else if (response == "i") |
| 91 | + { |
| 92 | + ignoreAllType = headerType; |
| 93 | + Console.WriteLine($"Skipping all files of type: {headerType}"); |
| 94 | + continue; |
| 95 | + } |
| 96 | + else if (response != "y") |
| 97 | + { |
| 98 | + continue; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + if (headerType == "scnc") |
| 103 | + { |
| 104 | + int jpgStartIndex = FindJpgStart(fileBytes, blfIndex); |
| 105 | + int jpgEndIndex = FindJpgEnd(fileBytes, jpgStartIndex); |
| 106 | + |
| 107 | + if (jpgStartIndex == -1 || jpgEndIndex == -1) |
| 108 | + { |
| 109 | + Console.WriteLine("Valid JPEG range not found. Skipping file."); |
| 110 | + continue; |
| 111 | + } |
| 112 | + |
| 113 | + byte[] jpgBytes = fileBytes.Skip(jpgStartIndex).Take(jpgEndIndex - jpgStartIndex + 1).ToArray(); |
| 114 | + |
| 115 | + string headerDirectory = Path.Combine(outputDirectory, contentType); |
| 116 | + Directory.CreateDirectory(headerDirectory); |
| 117 | + |
| 118 | + string outputFile = Path.Combine(headerDirectory, sanitizedContent + ".jpg"); |
| 119 | + File.WriteAllBytes(outputFile, jpgBytes); |
| 120 | + Console.WriteLine($"JPEG file saved: {outputFile}"); |
| 121 | + continue; |
| 122 | + } |
| 123 | + |
| 124 | + int stopSequenceIndex = FindStopSequence(fileBytes, blfIndex); |
| 125 | + |
| 126 | + if (stopSequenceIndex == -1) |
| 127 | + { |
| 128 | + Console.WriteLine("Stop sequence not found. Skipping file."); |
| 129 | + continue; |
| 130 | + } |
| 131 | + |
| 132 | + byte[] extractedBytes = fileBytes.Skip(blfIndex).Take(stopSequenceIndex - blfIndex).ToArray(); |
| 133 | + |
| 134 | + string extension = null; |
| 135 | + if (headerType == "mpvr") |
| 136 | + { |
| 137 | + extension = ".bin"; |
| 138 | + } |
| 139 | + else if (headerType == "mvar") |
| 140 | + { |
| 141 | + extension = ".mvar"; |
| 142 | + } |
| 143 | + else if (headerType == "athr") |
| 144 | + { |
| 145 | + extension = ".film"; |
| 146 | + } |
| 147 | + |
| 148 | + string headerDirectoryForOthers = Path.Combine(outputDirectory, contentType); |
| 149 | + Directory.CreateDirectory(headerDirectoryForOthers); |
| 150 | + |
| 151 | + string outputFileForOthers = Path.Combine(headerDirectoryForOthers, sanitizedContent + extension); |
| 152 | + File.WriteAllBytes(outputFileForOthers, extractedBytes); |
| 153 | + Console.WriteLine($"File saved: {outputFileForOthers}"); |
| 154 | + } |
| 155 | + catch (Exception ex) |
| 156 | + { |
| 157 | + Console.WriteLine($"Error processing file {Path.GetFileName(file)}: {ex.Message}"); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + Console.WriteLine("Processing complete."); |
| 162 | + } |
| 163 | + |
| 164 | + static int FindStopSequence(byte[] fileBytes, int startIndex) |
| 165 | + { |
| 166 | + string stopSequence = "_eof"; |
| 167 | + int stopIndex = System.Text.Encoding.ASCII.GetString(fileBytes).IndexOf(stopSequence, startIndex); |
| 168 | + |
| 169 | + if (stopIndex != -1) |
| 170 | + { |
| 171 | + return stopIndex + stopSequence.Length + 0x0D; |
| 172 | + } |
| 173 | + |
| 174 | + return -1; |
| 175 | + } |
| 176 | + |
| 177 | + static int FindJpgStart(byte[] fileBytes, int startIndex) |
| 178 | + { |
| 179 | + byte[] jpgStartPattern = { 0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x10, 0x4A, 0x46, 0x49, 0x46 }; |
| 180 | + for (int i = startIndex; i <= fileBytes.Length - jpgStartPattern.Length; i++) |
| 181 | + { |
| 182 | + if (fileBytes.Skip(i).Take(jpgStartPattern.Length).SequenceEqual(jpgStartPattern)) |
| 183 | + { |
| 184 | + return i; |
| 185 | + } |
| 186 | + } |
| 187 | + return -1; |
| 188 | + } |
| 189 | + |
| 190 | + static int FindJpgEnd(byte[] fileBytes, int startIndex) |
| 191 | + { |
| 192 | + byte[] jpgEndPattern = { 0xFF, 0xD9 }; |
| 193 | + for (int i = startIndex; i <= fileBytes.Length - jpgEndPattern.Length; i++) |
| 194 | + { |
| 195 | + if (fileBytes.Skip(i).Take(jpgEndPattern.Length).SequenceEqual(jpgEndPattern)) |
| 196 | + { |
| 197 | + return i + jpgEndPattern.Length - 1; |
| 198 | + } |
| 199 | + } |
| 200 | + return -1; |
| 201 | + } |
| 202 | +} |
0 commit comments