mirror of
https://github.com/aelurum/AssetStudio.git
synced 2025-07-15 11:04:16 -04:00
clean up code
This commit is contained in:
@ -9,7 +9,7 @@ namespace Unity_Studio
|
||||
{
|
||||
public class AssetsFile
|
||||
{
|
||||
public EndianStream a_Stream;
|
||||
public EndianBinaryReader a_Stream;
|
||||
public string filePath;
|
||||
public string bundlePath;
|
||||
public string fileName;
|
||||
@ -150,7 +150,7 @@ namespace Unity_Studio
|
||||
public string fileName = "";
|
||||
}
|
||||
|
||||
public AssetsFile(string fullName, EndianStream fileStream)
|
||||
public AssetsFile(string fullName, EndianBinaryReader fileStream)
|
||||
{
|
||||
a_Stream = fileStream;
|
||||
filePath = fullName;
|
||||
|
@ -42,87 +42,91 @@ namespace Unity_Studio
|
||||
decoder.Dispose();
|
||||
}
|
||||
}
|
||||
using (var b_Stream = new EndianStream(new MemoryStream(filebuffer), EndianType.BigEndian))
|
||||
using (var b_Stream = new EndianBinaryReader(new MemoryStream(filebuffer)))
|
||||
{
|
||||
readBundle(b_Stream);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
using (var b_Stream = new EndianStream(File.OpenRead(fileName), EndianType.BigEndian))
|
||||
using (var b_Stream = new EndianBinaryReader(File.OpenRead(fileName)))
|
||||
{
|
||||
readBundle(b_Stream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void readBundle(EndianStream b_Stream)
|
||||
private void readBundle(EndianBinaryReader b_Stream)
|
||||
{
|
||||
var signature = b_Stream.ReadStringToNull();
|
||||
|
||||
if (signature == "UnityWeb" || signature == "UnityRaw" || signature == "\xFA\xFA\xFA\xFA\xFA\xFA\xFA\xFA")
|
||||
switch (signature)
|
||||
{
|
||||
format = b_Stream.ReadInt32();
|
||||
versionPlayer = b_Stream.ReadStringToNull();
|
||||
versionEngine = b_Stream.ReadStringToNull();
|
||||
if (format < 6)
|
||||
{
|
||||
int bundleSize = b_Stream.ReadInt32();
|
||||
}
|
||||
else if (format == 6)
|
||||
{
|
||||
ReadFormat6(b_Stream, true);
|
||||
return;
|
||||
}
|
||||
short dummy2 = b_Stream.ReadInt16();
|
||||
int offset = b_Stream.ReadInt16();
|
||||
int dummy3 = b_Stream.ReadInt32();
|
||||
int lzmaChunks = b_Stream.ReadInt32();
|
||||
|
||||
int lzmaSize = 0;
|
||||
long streamSize = 0;
|
||||
|
||||
for (int i = 0; i < lzmaChunks; i++)
|
||||
{
|
||||
lzmaSize = b_Stream.ReadInt32();
|
||||
streamSize = b_Stream.ReadInt32();
|
||||
}
|
||||
|
||||
b_Stream.Position = offset;
|
||||
switch (signature)
|
||||
{
|
||||
case "\xFA\xFA\xFA\xFA\xFA\xFA\xFA\xFA": //.bytes
|
||||
case "UnityWeb":
|
||||
case "UnityWeb":
|
||||
case "UnityRaw":
|
||||
case "\xFA\xFA\xFA\xFA\xFA\xFA\xFA\xFA":
|
||||
{
|
||||
format = b_Stream.ReadInt32();
|
||||
versionPlayer = b_Stream.ReadStringToNull();
|
||||
versionEngine = b_Stream.ReadStringToNull();
|
||||
if (format < 6)
|
||||
{
|
||||
byte[] lzmaBuffer = new byte[lzmaSize];
|
||||
b_Stream.Read(lzmaBuffer, 0, lzmaSize);
|
||||
|
||||
using (var lzmaStream = new EndianStream(SevenZipHelper.StreamDecompress(new MemoryStream(lzmaBuffer)), EndianType.BigEndian))
|
||||
{
|
||||
getFiles(lzmaStream, 0);
|
||||
}
|
||||
break;
|
||||
int bundleSize = b_Stream.ReadInt32();
|
||||
}
|
||||
case "UnityRaw":
|
||||
else if (format == 6)
|
||||
{
|
||||
getFiles(b_Stream, offset);
|
||||
break;
|
||||
ReadFormat6(b_Stream, true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (signature == "UnityFS")
|
||||
{
|
||||
format = b_Stream.ReadInt32();
|
||||
versionPlayer = b_Stream.ReadStringToNull();
|
||||
versionEngine = b_Stream.ReadStringToNull();
|
||||
if (format == 6)
|
||||
{
|
||||
ReadFormat6(b_Stream);
|
||||
}
|
||||
short dummy2 = b_Stream.ReadInt16();
|
||||
int offset = b_Stream.ReadInt16();
|
||||
int dummy3 = b_Stream.ReadInt32();
|
||||
int lzmaChunks = b_Stream.ReadInt32();
|
||||
|
||||
int lzmaSize = 0;
|
||||
long streamSize = 0;
|
||||
|
||||
for (int i = 0; i < lzmaChunks; i++)
|
||||
{
|
||||
lzmaSize = b_Stream.ReadInt32();
|
||||
streamSize = b_Stream.ReadInt32();
|
||||
}
|
||||
|
||||
b_Stream.Position = offset;
|
||||
switch (signature)
|
||||
{
|
||||
case "\xFA\xFA\xFA\xFA\xFA\xFA\xFA\xFA": //.bytes
|
||||
case "UnityWeb":
|
||||
{
|
||||
byte[] lzmaBuffer = new byte[lzmaSize];
|
||||
b_Stream.Read(lzmaBuffer, 0, lzmaSize);
|
||||
|
||||
using (var lzmaStream = new EndianBinaryReader(SevenZipHelper.StreamDecompress(new MemoryStream(lzmaBuffer))))
|
||||
{
|
||||
getFiles(lzmaStream, 0);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "UnityRaw":
|
||||
{
|
||||
getFiles(b_Stream, offset);
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "UnityFS":
|
||||
format = b_Stream.ReadInt32();
|
||||
versionPlayer = b_Stream.ReadStringToNull();
|
||||
versionEngine = b_Stream.ReadStringToNull();
|
||||
if (format == 6)
|
||||
{
|
||||
ReadFormat6(b_Stream);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void getFiles(EndianStream f_Stream, int offset)
|
||||
private void getFiles(EndianBinaryReader f_Stream, int offset)
|
||||
{
|
||||
int fileCount = f_Stream.ReadInt32();
|
||||
for (int i = 0; i < fileCount; i++)
|
||||
@ -143,7 +147,7 @@ namespace Unity_Studio
|
||||
}
|
||||
}
|
||||
|
||||
private void ReadFormat6(EndianStream b_Stream, bool padding = false)
|
||||
private void ReadFormat6(EndianBinaryReader b_Stream, bool padding = false)
|
||||
{
|
||||
var bundleSize = b_Stream.ReadInt64();
|
||||
int compressedSize = b_Stream.ReadInt32();
|
||||
@ -163,17 +167,17 @@ namespace Unity_Studio
|
||||
{
|
||||
blocksInfoBytes = b_Stream.ReadBytes(compressedSize);
|
||||
}
|
||||
EndianStream blocksInfo;
|
||||
MemoryStream blocksInfoStream;
|
||||
switch (flag & 0x3F)
|
||||
{
|
||||
default://None
|
||||
{
|
||||
blocksInfo = new EndianStream(new MemoryStream(blocksInfoBytes), EndianType.BigEndian);
|
||||
blocksInfoStream = new MemoryStream(blocksInfoBytes);
|
||||
break;
|
||||
}
|
||||
case 1://LZMA
|
||||
{
|
||||
blocksInfo = new EndianStream(SevenZipHelper.StreamDecompress(new MemoryStream(blocksInfoBytes)), EndianType.BigEndian);
|
||||
blocksInfoStream = SevenZipHelper.StreamDecompress(new MemoryStream(blocksInfoBytes));
|
||||
break;
|
||||
}
|
||||
case 2://LZ4
|
||||
@ -186,16 +190,15 @@ namespace Unity_Studio
|
||||
decoder.Read(uncompressedBytes, 0, uncompressedSize);
|
||||
decoder.Dispose();
|
||||
}
|
||||
blocksInfo = new EndianStream(new MemoryStream(uncompressedBytes), EndianType.BigEndian);
|
||||
blocksInfoStream = new MemoryStream(uncompressedBytes);
|
||||
break;
|
||||
}
|
||||
//case 4:LZHAM?
|
||||
}
|
||||
using (blocksInfo)
|
||||
using (var blocksInfo = new EndianBinaryReader(blocksInfoStream))
|
||||
{
|
||||
blocksInfo.Position = 0x10;
|
||||
int blockcount = blocksInfo.ReadInt32();
|
||||
EndianStream assetsData;
|
||||
var assetsDataStream = new MemoryStream();
|
||||
for (int i = 0; i < blockcount; i++)
|
||||
{
|
||||
@ -238,8 +241,7 @@ namespace Unity_Studio
|
||||
//case 4:LZHAM?
|
||||
}
|
||||
}
|
||||
assetsData = new EndianStream(assetsDataStream, EndianType.BigEndian);
|
||||
using (assetsData)
|
||||
using (var assetsData = new EndianBinaryReader(assetsDataStream))
|
||||
{
|
||||
var entryinfo_count = blocksInfo.ReadInt32();
|
||||
for (int i = 0; i < entryinfo_count; i++)
|
||||
|
@ -53,7 +53,7 @@ namespace Unity_Studio
|
||||
return null;
|
||||
}
|
||||
|
||||
private static void ReadClassStruct(StringBuilder sb, List<ClassMember> members, EndianStream a_Stream)
|
||||
private static void ReadClassStruct(StringBuilder sb, List<ClassMember> members, EndianBinaryReader a_Stream)
|
||||
{
|
||||
for (int i = 0; i < members.Count; i++)
|
||||
{
|
||||
|
@ -11,16 +11,20 @@ namespace Unity_Studio
|
||||
LittleEndian
|
||||
}
|
||||
|
||||
public class EndianStream : BinaryReader
|
||||
public class EndianBinaryReader : BinaryReader
|
||||
{
|
||||
public EndianType endian;
|
||||
private byte[] a16 = new byte[2];
|
||||
private byte[] a32 = new byte[4];
|
||||
private byte[] a64 = new byte[8];
|
||||
|
||||
public EndianStream(Stream stream, EndianType endian) : base(stream) { }
|
||||
public EndianBinaryReader(Stream stream, EndianType endian = EndianType.BigEndian) : base(stream) { this.endian = endian; }
|
||||
|
||||
public long Position { get { return BaseStream.Position; } set { BaseStream.Position = value; } }
|
||||
public long Position
|
||||
{
|
||||
get => BaseStream.Position;
|
||||
set => BaseStream.Position = value;
|
||||
}
|
||||
|
||||
public override short ReadInt16()
|
||||
{
|
@ -17,7 +17,7 @@ namespace Unity_Studio
|
||||
public static HashSet<string> unityFilesHash = new HashSet<string>(); //improve performance
|
||||
public static List<AssetsFile> assetsfileList = new List<AssetsFile>(); //loaded files
|
||||
public static HashSet<string> assetsfileListHash = new HashSet<string>(); //improve performance
|
||||
public static Dictionary<string, EndianStream> assetsfileandstream = new Dictionary<string, EndianStream>(); //use for read res files
|
||||
public static Dictionary<string, EndianBinaryReader> assetsfileandstream = new Dictionary<string, EndianBinaryReader>(); //use for read res files
|
||||
public static List<AssetPreloadData> exportableAssets = new List<AssetPreloadData>(); //used to hold all assets while the ListView is filtered
|
||||
private static HashSet<string> exportableAssetsHash = new HashSet<string>(); //improve performance
|
||||
public static List<AssetPreloadData> visibleAssets = new List<AssetPreloadData>(); //used to build the ListView from all or filtered assets
|
||||
@ -44,7 +44,7 @@ namespace Unity_Studio
|
||||
{
|
||||
//open file here and pass the stream to facilitate loading memory files
|
||||
//also by keeping the stream as a property of AssetsFile, it can be used later on to read assets
|
||||
AssetsFile assetsFile = new AssetsFile(fileName, new EndianStream(File.OpenRead(fileName), EndianType.BigEndian));
|
||||
AssetsFile assetsFile = new AssetsFile(fileName, new EndianBinaryReader(File.OpenRead(fileName)));
|
||||
//if (Path.GetFileName(fileName) == "mainData") { mainDataFile = assetsFile; }
|
||||
|
||||
//totalAssetCount += assetsFile.preloadTable.Count;
|
||||
@ -64,7 +64,7 @@ namespace Unity_Studio
|
||||
}
|
||||
else if (File.Exists(Path.GetDirectoryName(fileName) + "\\mainData"))
|
||||
{
|
||||
mainDataFile = new AssetsFile(Path.GetDirectoryName(fileName) + "\\mainData", new EndianStream(File.OpenRead(Path.GetDirectoryName(fileName) + "\\mainData"), EndianType.BigEndian));
|
||||
mainDataFile = new AssetsFile(Path.GetDirectoryName(fileName) + "\\mainData", new EndianBinaryReader(File.OpenRead(Path.GetDirectoryName(fileName) + "\\mainData")));
|
||||
|
||||
assetsFile.m_Version = mainDataFile.m_Version;
|
||||
assetsFile.version = mainDataFile.version;
|
||||
@ -131,7 +131,7 @@ namespace Unity_Studio
|
||||
StatusStripUpdate("Loading " + memFile.fileName);
|
||||
//create dummy path to be used for asset extraction
|
||||
memFile.fileName = Path.GetDirectoryName(bundleFileName) + "\\" + memFile.fileName;
|
||||
AssetsFile assetsFile = new AssetsFile(memFile.fileName, new EndianStream(memFile.memStream, EndianType.BigEndian));
|
||||
AssetsFile assetsFile = new AssetsFile(memFile.fileName, new EndianBinaryReader(memFile.memStream));
|
||||
if (assetsFile.valid)
|
||||
{
|
||||
assetsFile.bundlePath = bundleFileName;
|
||||
@ -186,7 +186,7 @@ namespace Unity_Studio
|
||||
{
|
||||
int extractedCount = 0;
|
||||
|
||||
StatusStripUpdate("Decompressing " + Path.GetFileName(bundleFileName) + " ,,,");
|
||||
StatusStripUpdate("Decompressing " + Path.GetFileName(bundleFileName) + " ...");
|
||||
|
||||
string extractPath = bundleFileName + "_unpacked\\";
|
||||
Directory.CreateDirectory(extractPath);
|
||||
@ -1822,5 +1822,23 @@ namespace Unity_Studio
|
||||
if (str.Length >= 260) return Path.GetRandomFileName();
|
||||
return Path.GetInvalidFileNameChars().Aggregate(str, (current, c) => current.Replace(c, '_'));
|
||||
}
|
||||
|
||||
public static bool CheckBundleFile(string fileName)
|
||||
{
|
||||
using (var stream = new EndianBinaryReader(File.OpenRead(fileName)))
|
||||
{
|
||||
var signature = stream.ReadStringToNull();
|
||||
switch (signature)
|
||||
{
|
||||
case "UnityWeb":
|
||||
case "UnityRaw":
|
||||
case "\xFA\xFA\xFA\xFA\xFA\xFA\xFA\xFA":
|
||||
case "UnityFS":
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user