Extract fileList to a separate class

This commit is contained in:
VaDiM
2026-03-02 17:21:34 +03:00
parent 1856378377
commit 0a381aff55
6 changed files with 41 additions and 35 deletions
+2 -2
View File
@@ -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);
+2 -6
View File
@@ -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<StreamFile> 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<StreamFile>(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
+9
View File
@@ -0,0 +1,9 @@
using System.Collections.Generic;
namespace AssetStudio
{
public abstract class FileArchive
{
public readonly List<StreamFile> FileList = new List<StreamFile>();
}
}
+2 -5
View File
@@ -4,10 +4,8 @@ using System.Text;
namespace AssetStudio
{
public class WebFile
public class WebFile : FileArchive
{
public List<StreamFile> 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<StreamFile>(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);
}
}
}
+13 -11
View File
@@ -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<StreamFile> 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;
}
+13 -11
View File
@@ -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<StreamFile> 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;
}