|
1 | 1 | using System.IO; |
2 | 2 | using BinaryObjectScanner.Interfaces; |
3 | | -using SabreTools.Compression.Deflate; |
4 | 3 |
|
5 | 4 | namespace BinaryObjectScanner.FileType |
6 | 5 | { |
@@ -29,96 +28,9 @@ public bool Extract(Stream? stream, string file, string outDir, bool includeDebu |
29 | 28 |
|
30 | 29 | // Extract all files |
31 | 30 | Directory.CreateDirectory(outDir); |
32 | | - ExtractAll(bfpk, outDir); |
| 31 | + bfpk.ExtractAll(outDir); |
33 | 32 |
|
34 | 33 | return true; |
35 | 34 | } |
36 | | - |
37 | | - /// <summary> |
38 | | - /// Extract all files from the BFPK to an output directory |
39 | | - /// </summary> |
40 | | - /// <param name="outputDirectory">Output directory to write to</param> |
41 | | - /// <returns>True if all files extracted, false otherwise</returns> |
42 | | - public static bool ExtractAll(SabreTools.Serialization.Wrappers.BFPK item, string outputDirectory) |
43 | | - { |
44 | | - // If we have no files |
45 | | - if (item.Model.Files == null || item.Model.Files.Length == 0) |
46 | | - return false; |
47 | | - |
48 | | - // Loop through and extract all files to the output |
49 | | - bool allExtracted = true; |
50 | | - for (int i = 0; i < item.Model.Files.Length; i++) |
51 | | - { |
52 | | - allExtracted &= ExtractFile(item, i, outputDirectory); |
53 | | - } |
54 | | - |
55 | | - return allExtracted; |
56 | | - } |
57 | | - |
58 | | - /// <summary> |
59 | | - /// Extract a file from the BFPK to an output directory by index |
60 | | - /// </summary> |
61 | | - /// <param name="index">File index to extract</param> |
62 | | - /// <param name="outputDirectory">Output directory to write to</param> |
63 | | - /// <returns>True if the file extracted, false otherwise</returns> |
64 | | - public static bool ExtractFile(SabreTools.Serialization.Wrappers.BFPK item, int index, string outputDirectory) |
65 | | - { |
66 | | - // If we have no files |
67 | | - if (item.Model.Files == null || item.Model.Files.Length == 0) |
68 | | - return false; |
69 | | - |
70 | | - // If we have an invalid index |
71 | | - if (index < 0 || index >= item.Model.Files.Length) |
72 | | - return false; |
73 | | - |
74 | | - // Get the file information |
75 | | - var file = item.Model.Files[index]; |
76 | | - if (file == null) |
77 | | - return false; |
78 | | - |
79 | | - // Get the read index and length |
80 | | - int offset = file.Offset + 4; |
81 | | - int compressedSize = file.CompressedSize; |
82 | | - |
83 | | - // Some files can lack the length prefix |
84 | | - if (compressedSize > item.GetEndOfFile()) |
85 | | - { |
86 | | - offset -= 4; |
87 | | - compressedSize = file.UncompressedSize; |
88 | | - } |
89 | | - |
90 | | - try |
91 | | - { |
92 | | - // Ensure the output directory exists |
93 | | - Directory.CreateDirectory(outputDirectory); |
94 | | - |
95 | | - // Create the output path |
96 | | - string filePath = Path.Combine(outputDirectory, file.Name ?? $"file{index}"); |
97 | | - using FileStream fs = File.OpenWrite(filePath); |
98 | | - |
99 | | - // Read the data block |
100 | | - var data = item.ReadFromDataSource(offset, compressedSize); |
101 | | - if (data == null) |
102 | | - return false; |
103 | | - |
104 | | - // If we have uncompressed data |
105 | | - if (compressedSize == file.UncompressedSize) |
106 | | - { |
107 | | - fs.Write(data, 0, compressedSize); |
108 | | - } |
109 | | - else |
110 | | - { |
111 | | - MemoryStream ms = new MemoryStream(data); |
112 | | - ZlibStream zs = new ZlibStream(ms, CompressionMode.Decompress); |
113 | | - zs.CopyTo(fs); |
114 | | - } |
115 | | - |
116 | | - return true; |
117 | | - } |
118 | | - catch |
119 | | - { |
120 | | - return false; |
121 | | - } |
122 | | - } |
123 | 35 | } |
124 | 36 | } |
0 commit comments