diff --git a/AssetStudio/AssetsManager.cs b/AssetStudio/AssetsManager.cs index 94c65fa..046d4aa 100644 --- a/AssetStudio/AssetsManager.cs +++ b/AssetStudio/AssetsManager.cs @@ -360,7 +360,7 @@ namespace AssetStudio private bool LoadBundleFiles(FileReader reader, BundleFile bundleFile, string originalPath = null) { - foreach (var file in bundleFile.fileList) + foreach (var file in bundleFile.FileList) { if (file.stream == null) continue; @@ -386,7 +386,7 @@ namespace AssetStudio try { var webFile = new WebFile(reader); - foreach (var file in webFile.fileList) + foreach (var file in webFile.FileList) { var dummyPath = Path.Combine(Path.GetDirectoryName(reader.FullPath), file.fileName); var subReader = new FileReader(dummyPath, file.stream); diff --git a/AssetStudio/BundleFile.cs b/AssetStudio/BundleFile.cs index 79d44b1..eb352eb 100644 --- a/AssetStudio/BundleFile.cs +++ b/AssetStudio/BundleFile.cs @@ -1,6 +1,5 @@ using AssetStudio.CustomOptions; using System; -using System.Collections.Generic; using System.IO; using System.Linq; @@ -42,7 +41,7 @@ namespace AssetStudio Oodle, //custom } - public class BundleFile + public class BundleFile : FileArchive { public readonly bool IsDataAfterBundle; private static TempFileStream _sharedTempFileStream; @@ -79,8 +78,6 @@ namespace AssetStudio private StorageBlock[] m_BlocksInfo; private Node[] m_DirectoryInfo; - public List fileList; - public BundleFile(FileReader reader, ImportOptions importOptions, bool isMultiBundle = false) { _bundleOptions = importOptions.BundleOptions; @@ -240,11 +237,10 @@ namespace AssetStudio private void ReadFiles(Stream inputStream, long blocksOffset = 0) { - fileList = new List(m_DirectoryInfo.Length); foreach (var node in m_DirectoryInfo) { var file = new StreamFile(); - fileList.Add(file); + this.FileList.Add(file); file.path = node.path; file.fileName = Path.GetFileName(node.path); try diff --git a/AssetStudio/FileArchive.cs b/AssetStudio/FileArchive.cs new file mode 100644 index 0000000..0f5ccfe --- /dev/null +++ b/AssetStudio/FileArchive.cs @@ -0,0 +1,9 @@ +using System.Collections.Generic; + +namespace AssetStudio +{ + public abstract class FileArchive + { + public readonly List FileList = new List(); + } +} diff --git a/AssetStudio/WebFile.cs b/AssetStudio/WebFile.cs index 2f901a5..4e905a7 100644 --- a/AssetStudio/WebFile.cs +++ b/AssetStudio/WebFile.cs @@ -4,10 +4,8 @@ using System.Text; namespace AssetStudio { - public class WebFile + public class WebFile : FileArchive { - public List fileList; - private class WebData { public int dataOffset; @@ -30,15 +28,14 @@ namespace AssetStudio data.path = Encoding.UTF8.GetString(reader.ReadBytes(pathLength)); dataList.Add(data); } - fileList = new List(dataList.Count); foreach (var data in dataList) { var file = new StreamFile(); + this.FileList.Add(file); file.path = data.path; file.fileName = Path.GetFileName(data.path); reader.BaseStream.Position = data.dataOffset; file.stream = new MemoryStream(reader.ReadBytes(data.dataLength)); - fileList.Add(file); } } } diff --git a/AssetStudioCLI/Studio.cs b/AssetStudioCLI/Studio.cs index 5849680..50d1a82 100644 --- a/AssetStudioCLI/Studio.cs +++ b/AssetStudioCLI/Studio.cs @@ -103,9 +103,9 @@ namespace AssetStudioCLI var bundleReader = new FileReader(reader.FullPath, bundleStream); var bundleFile = new BundleFile(bundleReader, assetsManager.Options); var extractPath = Path.Combine(savePath, reader.FileName + "_unpacked"); - if (bundleFile.fileList.Count > 0) + if (bundleFile.FileList.Count > 0) { - count += ExtractStreamFile(extractPath, bundleFile.fileList); + count += ExtractStreamFile(extractPath, bundleFile); } while (bundleFile.IsDataAfterBundle) { @@ -121,9 +121,9 @@ namespace AssetStudioCLI } Logger.Info($"[MultiBundle] Decompressing \"{reader.FileName}\" from offset: 0x{bundleStream.Offset:X}.."); bundleFile = new BundleFile(bundleReader, assetsManager.Options, isMultiBundle: true); - if (bundleFile.fileList.Count > 0) + if (bundleFile.FileList.Count > 0) { - count += ExtractStreamFile(extractPath, bundleFile.fileList); + count += ExtractStreamFile(extractPath, bundleFile); } } bundleStream.Dispose(); @@ -135,21 +135,23 @@ namespace AssetStudioCLI Logger.Info($"Decompressing {reader.FileName} ..."); var webFile = new WebFile(reader); reader.Dispose(); - if (webFile.fileList.Count > 0) + if (webFile.FileList.Count > 0) { var extractPath = Path.Combine(savePath, reader.FileName + "_unpacked"); - return ExtractStreamFile(extractPath, webFile.fileList, isOffsetStream: false); + return ExtractStreamFile(extractPath, webFile); } return 0; } - private static int ExtractStreamFile(string extractPath, List fileList, bool isOffsetStream = true) + private static int ExtractStreamFile(string extractPath, FileArchive fileArchive) { var extractedCount = 0; - foreach (var file in fileList) + var isWebFile = fileArchive is WebFile; + foreach (var file in fileArchive.FileList) { if (file.stream == null) continue; + var filePath = Path.Combine(extractPath, file.path); var fileDirectory = Path.GetDirectoryName(filePath); if (!Directory.Exists(fileDirectory)) @@ -165,11 +167,11 @@ namespace AssetStudioCLI } extractedCount++; } - if (!isOffsetStream) file.stream.Dispose(); + if (isWebFile) file.stream.Dispose(); } - if (isOffsetStream && fileList.Count > 0) + if (!isWebFile) { - fileList[0].stream?.Dispose(); + fileArchive.FileList.FirstOrDefault(x => x.stream != null)?.stream.Dispose(); } return extractedCount; } diff --git a/AssetStudioGUI/Studio.cs b/AssetStudioGUI/Studio.cs index 6fe50f4..b878f1c 100644 --- a/AssetStudioGUI/Studio.cs +++ b/AssetStudioGUI/Studio.cs @@ -142,9 +142,9 @@ namespace AssetStudioGUI var bundleReader = new FileReader(reader.FullPath, bundleStream); var bundleFile = new BundleFile(bundleReader, assetsManager.Options); var extractPath = Path.Combine(savePath, reader.FileName + "_unpacked"); - if (bundleFile.fileList.Count > 0) + if (bundleFile.FileList.Count > 0) { - count += ExtractStreamFile(extractPath, bundleFile.fileList); + count += ExtractStreamFile(extractPath, bundleFile); } while (bundleFile.IsDataAfterBundle) { @@ -160,9 +160,9 @@ namespace AssetStudioGUI } Logger.Info($"[MultiBundle] Decompressing \"{reader.FileName}\" from offset: 0x{bundleStream.Offset:X}.."); bundleFile = new BundleFile(bundleReader, assetsManager.Options, isMultiBundle: true); - if (bundleFile.fileList.Count > 0) + if (bundleFile.FileList.Count > 0) { - count += ExtractStreamFile(extractPath, bundleFile.fileList); + count += ExtractStreamFile(extractPath, bundleFile); } } bundleStream.Dispose(); @@ -174,21 +174,23 @@ namespace AssetStudioGUI Logger.Info($"Decompressing {reader.FileName} ..."); var webFile = new WebFile(reader); reader.Dispose(); - if (webFile.fileList.Count > 0) + if (webFile.FileList.Count > 0) { var extractPath = Path.Combine(savePath, reader.FileName + "_unpacked"); - return ExtractStreamFile(extractPath, webFile.fileList, isOffsetStream: false); + return ExtractStreamFile(extractPath, webFile); } return 0; } - private static int ExtractStreamFile(string extractPath, List fileList, bool isOffsetStream = true) + private static int ExtractStreamFile(string extractPath, FileArchive fileArchive) { var extractedCount = 0; - foreach (var file in fileList) + var isWebFile = fileArchive is WebFile; + foreach (var file in fileArchive.FileList) { if (file.stream == null) continue; + var filePath = Path.Combine(extractPath, file.path); var fileDirectory = Path.GetDirectoryName(filePath); if (!Directory.Exists(fileDirectory)) @@ -204,11 +206,11 @@ namespace AssetStudioGUI } extractedCount++; } - if (!isOffsetStream) file.stream.Dispose(); + if (isWebFile) file.stream.Dispose(); } - if (isOffsetStream && fileList.Count > 0) + if (!isWebFile) { - fileList[0].stream?.Dispose(); + fileArchive.FileList.FirstOrDefault(x => x.stream != null)?.stream.Dispose(); } return extractedCount; }