|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.IO.Compression; |
| 5 | +using System.Linq; |
| 6 | +using System.Text; |
| 7 | +using UnityDataTools.FileSystem; |
| 8 | + |
| 9 | +namespace UnityDataTools.UnityDataTool; |
| 10 | + |
| 11 | +public static class Archive |
| 12 | +{ |
| 13 | + private static readonly byte[] WebBundlePrefix = Encoding.UTF8.GetBytes("UnityWebData1.0\0"); |
| 14 | + |
| 15 | + public static int HandleExtract(FileInfo filename, DirectoryInfo outputFolder) |
| 16 | + { |
| 17 | + try |
| 18 | + { |
| 19 | + if (IsWebBundle(filename)) |
| 20 | + { |
| 21 | + ExtractWebBundle(filename, outputFolder); |
| 22 | + } |
| 23 | + else |
| 24 | + { |
| 25 | + ExtractAssetBundle(filename, outputFolder); |
| 26 | + } |
| 27 | + } |
| 28 | + catch (Exception err) when ( |
| 29 | + err is NotSupportedException |
| 30 | + || err is FileFormatException) |
| 31 | + { |
| 32 | + Console.Error.WriteLine("Error opening archive"); |
| 33 | + Console.Error.WriteLine(err.Message); |
| 34 | + return 1; |
| 35 | + } |
| 36 | + return 0; |
| 37 | + } |
| 38 | + |
| 39 | + public static int HandleList(FileInfo filename) |
| 40 | + { |
| 41 | + try |
| 42 | + { |
| 43 | + if (IsWebBundle(filename)) |
| 44 | + { |
| 45 | + ListWebBundle(filename); |
| 46 | + } |
| 47 | + else |
| 48 | + { |
| 49 | + ListAssetBundle(filename); |
| 50 | + } |
| 51 | + } |
| 52 | + catch (Exception err) when ( |
| 53 | + err is NotSupportedException |
| 54 | + || err is FileFormatException) |
| 55 | + { |
| 56 | + Console.Error.WriteLine("Error opening archive"); |
| 57 | + Console.Error.WriteLine(err.Message); |
| 58 | + return 1; |
| 59 | + } |
| 60 | + |
| 61 | + return 0; |
| 62 | + } |
| 63 | + |
| 64 | + |
| 65 | + public static bool IsWebBundle(FileInfo filename) |
| 66 | + { |
| 67 | + var path = filename.ToString(); |
| 68 | + return ( |
| 69 | + path.EndsWith(".data") |
| 70 | + || path.EndsWith(".data.gz") |
| 71 | + || path.EndsWith(".data.br") |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + struct WebBundleFileDescription |
| 76 | + { |
| 77 | + public uint ByteOffset; |
| 78 | + public uint Size; |
| 79 | + public string Path; |
| 80 | + } |
| 81 | + |
| 82 | + static void ExtractWebBundle(FileInfo filename, DirectoryInfo outputFolder) { |
| 83 | + Console.WriteLine($"Extracting web bundle: {filename}"); |
| 84 | + using var fileStream = File.Open(filename.ToString(), FileMode.Open); |
| 85 | + using var stream = GetStream(filename, fileStream); |
| 86 | + using var reader = new BinaryReader(stream, Encoding.UTF8); |
| 87 | + var fileDescriptions = ParseWebBundleHeader(reader); |
| 88 | + foreach (var description in fileDescriptions) |
| 89 | + { |
| 90 | + ExtractFileFromWebBundle(description, reader, outputFolder); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + static Stream GetStream(FileInfo filename, FileStream fileStream) { |
| 95 | + var fileExtension = Path.GetExtension(filename.ToString()); |
| 96 | + return fileExtension switch |
| 97 | + { |
| 98 | + ".data" => fileStream, |
| 99 | + ".gz" => new GZipStream(fileStream, CompressionMode.Decompress), |
| 100 | + ".br" => new BrotliStream(fileStream, CompressionMode.Decompress), |
| 101 | + _ => throw new FileFormatException("Incorrect file extension for web bundle"), |
| 102 | + }; |
| 103 | + } |
| 104 | + |
| 105 | + static List<WebBundleFileDescription> ParseWebBundleHeader(BinaryReader reader) |
| 106 | + { |
| 107 | + var result = new List<WebBundleFileDescription>(); |
| 108 | + var prefix = ReadBytes(reader, WebBundlePrefix.Length); |
| 109 | + if (!prefix.SequenceEqual(WebBundlePrefix)) { |
| 110 | + throw new FileFormatException("File is not a valid web bundle."); |
| 111 | + } |
| 112 | + uint headerSize = ReadUInt32(reader); |
| 113 | + // Advance offset past prefix string and header size uint. |
| 114 | + var currentByteOffset = WebBundlePrefix.Length + sizeof(uint); |
| 115 | + while (currentByteOffset < headerSize) |
| 116 | + { |
| 117 | + var fileByteOffset = ReadUInt32(reader); |
| 118 | + var fileSize = ReadUInt32(reader); |
| 119 | + var filePathLength = ReadUInt32(reader); |
| 120 | + var filePath = Encoding.UTF8.GetString(ReadBytes(reader, (int) filePathLength)); |
| 121 | + result.Add(new WebBundleFileDescription() { |
| 122 | + ByteOffset = fileByteOffset, |
| 123 | + Size = fileSize, |
| 124 | + Path = filePath, |
| 125 | + }); |
| 126 | + // Advance byte offset, so we keep track of the position (to know when we're done reading the header). |
| 127 | + currentByteOffset += 3 * sizeof(uint) + filePath.Length; |
| 128 | + } |
| 129 | + return result; |
| 130 | + } |
| 131 | + |
| 132 | + static void ExtractFileFromWebBundle(WebBundleFileDescription description, BinaryReader reader, DirectoryInfo outputFolder) |
| 133 | + { |
| 134 | + // This function assumes `reader` is at the start of the binary data representing the file contents. |
| 135 | + Console.WriteLine($"... Extracting {description.Path}"); |
| 136 | + var path = Path.Combine(outputFolder.ToString(), description.Path); |
| 137 | + Directory.CreateDirectory(Path.GetDirectoryName(path)); |
| 138 | + File.WriteAllBytes(path, ReadBytes(reader, (int) description.Size)); |
| 139 | + } |
| 140 | + |
| 141 | + static uint ReadUInt32(BinaryReader reader) |
| 142 | + { |
| 143 | + try { |
| 144 | + return reader.ReadUInt32(); |
| 145 | + } |
| 146 | + catch (EndOfStreamException) |
| 147 | + { |
| 148 | + throw new FileFormatException("File data is corrupt."); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + static byte[] ReadBytes(BinaryReader reader, int count) |
| 153 | + { |
| 154 | + var result = reader.ReadBytes(count); |
| 155 | + if (result.Length != count) |
| 156 | + { |
| 157 | + throw new FileFormatException("File data is corrupt."); |
| 158 | + } |
| 159 | + return result; |
| 160 | + } |
| 161 | + |
| 162 | + static void ExtractAssetBundle(FileInfo filename, DirectoryInfo outputFolder) |
| 163 | + { |
| 164 | + Console.WriteLine($"Extracting asset bundle: {filename}"); |
| 165 | + using var archive = UnityFileSystem.MountArchive(filename.FullName, "/"); |
| 166 | + foreach (var node in archive.Nodes) |
| 167 | + { |
| 168 | + Console.WriteLine($"... Extracting {node.Path}"); |
| 169 | + CopyFile("/" + node.Path, Path.Combine(outputFolder.FullName, node.Path)); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + static void ListAssetBundle(FileInfo filename) |
| 174 | + { |
| 175 | + using var archive = UnityFileSystem.MountArchive(filename.FullName, "/"); |
| 176 | + foreach (var node in archive.Nodes) |
| 177 | + { |
| 178 | + Console.WriteLine($"{node.Path}"); |
| 179 | + Console.WriteLine($" Size: {node.Size}"); |
| 180 | + Console.WriteLine($" Flags: {node.Flags}"); |
| 181 | + Console.WriteLine(); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + static void ListWebBundle(FileInfo filename) |
| 186 | + { |
| 187 | + using var fileStream = File.Open(filename.ToString(), FileMode.Open); |
| 188 | + using var stream = GetStream(filename, fileStream); |
| 189 | + using var reader = new BinaryReader(stream, Encoding.UTF8); |
| 190 | + var fileDescriptions = ParseWebBundleHeader(reader); |
| 191 | + foreach (var description in fileDescriptions) |
| 192 | + { |
| 193 | + Console.WriteLine($"{description.Path}"); |
| 194 | + Console.WriteLine($" Size: {description.Size}"); |
| 195 | + Console.WriteLine(); |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + static void CopyFile(string source, string dest) |
| 200 | + { |
| 201 | + using var sourceFile = UnityFileSystem.OpenFile(source); |
| 202 | + // Create the containing directory if it doesn't exist. |
| 203 | + Directory.CreateDirectory(Path.GetDirectoryName(dest)); |
| 204 | + using var destFile = new FileStream(dest, FileMode.Create); |
| 205 | + |
| 206 | + const int blockSize = 256 * 1024; |
| 207 | + var buffer = new byte[blockSize]; |
| 208 | + long actualSize; |
| 209 | + |
| 210 | + do |
| 211 | + { |
| 212 | + actualSize = sourceFile.Read(blockSize, buffer); |
| 213 | + destFile.Write(buffer, 0, (int)actualSize); |
| 214 | + } |
| 215 | + while (actualSize == blockSize); |
| 216 | + } |
| 217 | +} |
0 commit comments