support UnityWebData

This commit is contained in:
Perfare 2018-03-05 02:35:53 +08:00
parent c8393e165f
commit a9f1bd320e
9 changed files with 186 additions and 53 deletions

View File

@ -31,9 +31,20 @@ namespace Lz4
inBufEnd = DecBufLen;
}
public override void Close()
protected override void Dispose(bool disposing)
{
input = null;
try
{
if (disposing && input != null)
{
input.Close();
}
input = null;
}
finally
{
base.Dispose(disposing);
}
}
private long inputLength;
@ -500,15 +511,12 @@ namespace Lz4
{
}
public override long Length
{
get { throw new NotSupportedException(); }
}
public override long Length => throw new NotSupportedException();
public override long Position
{
get { throw new NotSupportedException(); }
set { throw new NotSupportedException(); }
get => throw new NotSupportedException();
set => throw new NotSupportedException();
}
public override long Seek(long offset, SeekOrigin origin)

View File

@ -60,11 +60,9 @@ namespace Unity_Studio
var decompressedSize = reader.ReadUInt32();
var m_SubProgramBlob = reader.ReadBytes(reader.ReadInt32());
var decompressedBytes = new byte[decompressedSize];
using (var mstream = new MemoryStream(m_SubProgramBlob))
using (var decoder = new Lz4DecoderStream(new MemoryStream(m_SubProgramBlob)))
{
var decoder = new Lz4DecoderStream(mstream);
decoder.Read(decompressedBytes, 0, (int)decompressedSize);
decoder.Dispose();
}
m_Script = m_Script.Concat(decompressedBytes.ToArray()).ToArray();
}

View File

@ -162,7 +162,7 @@ namespace Unity_Studio
int dataEnd = assetsFileReader.ReadInt32();
fileGen = assetsFileReader.ReadInt32();
uint dataOffset = assetsFileReader.ReadUInt32();
sharedAssetsList[0].fileName = Path.GetFileName(fullName); //reference itself because sharedFileIDs start from 1
sharedAssetsList[0].fileName = fileName; //reference itself because sharedFileIDs start from 1
switch (fileGen)
{

View File

@ -5,19 +5,18 @@ using SevenZip.Compression.LZMA;
namespace Unity_Studio
{
public class MemoryFile
{
public string fileName;
public MemoryStream stream;
}
public class BundleFile
{
public int format;
public string versionPlayer;
public string versionEngine;
public List<MemoryAssetsFile> MemoryAssetsFileList = new List<MemoryAssetsFile>();
public class MemoryAssetsFile
{
public string fileName;
public MemoryStream memStream;
}
public List<MemoryFile> fileList = new List<MemoryFile>();
public BundleFile(EndianBinaryReader bundleReader)
{
@ -92,16 +91,16 @@ namespace Unity_Studio
int fileCount = reader.ReadInt32();
for (int i = 0; i < fileCount; i++)
{
var memFile = new MemoryAssetsFile();
memFile.fileName = reader.ReadStringToNull();
var file = new MemoryFile();
file.fileName = reader.ReadStringToNull();
int fileOffset = reader.ReadInt32();
fileOffset += offset;
int fileSize = reader.ReadInt32();
long nextFile = reader.Position;
reader.Position = fileOffset;
var buffer = reader.ReadBytes(fileSize);
memFile.memStream = new MemoryStream(buffer);
MemoryAssetsFileList.Add(memFile);
file.stream = new MemoryStream(buffer);
fileList.Add(file);
reader.Position = nextFile;
}
}
@ -143,11 +142,9 @@ namespace Unity_Studio
case 3://LZ4HC
{
byte[] uncompressedBytes = new byte[uncompressedSize];
using (var mstream = new MemoryStream(blocksInfoBytes))
using (var decoder = new Lz4DecoderStream(new MemoryStream(blocksInfoBytes)))
{
var decoder = new Lz4DecoderStream(mstream);
decoder.Read(uncompressedBytes, 0, uncompressedSize);
decoder.Dispose();
}
blocksInfoStream = new MemoryStream(uncompressedBytes);
break;
@ -188,11 +185,9 @@ namespace Unity_Studio
case 3://LZ4HC
{
var uncompressedBytes = new byte[uncompressedSize];
using (var mstream = new MemoryStream(compressedBytes))
using (var decoder = new Lz4DecoderStream(new MemoryStream(compressedBytes)))
{
var decoder = new Lz4DecoderStream(mstream);
decoder.Read(uncompressedBytes, 0, uncompressedSize);
decoder.Dispose();
}
assetsDataStream.Write(uncompressedBytes, 0, uncompressedSize);
break;
@ -205,15 +200,15 @@ namespace Unity_Studio
var entryinfo_count = blocksInfo.ReadInt32();
for (int i = 0; i < entryinfo_count; i++)
{
var memFile = new MemoryAssetsFile();
var file = new MemoryFile();
var entryinfo_offset = blocksInfo.ReadInt64();
var entryinfo_size = blocksInfo.ReadInt64();
flag = blocksInfo.ReadInt32();
memFile.fileName = blocksInfo.ReadStringToNull();
file.fileName = Path.GetFileName(blocksInfo.ReadStringToNull());
assetsDataReader.Position = entryinfo_offset;
var buffer = assetsDataReader.ReadBytes((int)entryinfo_size);
memFile.memStream = new MemoryStream(buffer);
MemoryAssetsFileList.Add(memFile);
file.stream = new MemoryStream(buffer);
fileList.Add(file);
}
}
}

View File

@ -16,13 +16,17 @@ namespace Unity_Studio
public static void LoadFile(string fullName)
{
if (CheckBundleFile(fullName, out var reader))
switch (CheckFileType(fullName, out var reader))
{
LoadBundleFile(fullName, reader);
}
else
{
LoadAssetsFile(fullName, reader);
case FileType.AssetsFile:
LoadAssetsFile(fullName, reader);
break;
case FileType.BundleFile:
LoadBundleFile(fullName, reader);
break;
case FileType.WebFile:
LoadWebFile(fullName, reader);
break;
}
}
@ -94,12 +98,12 @@ namespace Unity_Studio
var fileName = Path.GetFileName(fullName);
StatusStripUpdate("Decompressing " + fileName);
var bundleFile = new BundleFile(reader);
foreach (var memFile in bundleFile.MemoryAssetsFileList)
foreach (var file in bundleFile.fileList)
{
if (!assetsfileListHash.Contains(memFile.fileName.ToUpper()))
if (!assetsfileListHash.Contains(file.fileName.ToUpper()))
{
StatusStripUpdate("Loading " + memFile.fileName);
var assetsFile = new AssetsFile(Path.GetDirectoryName(fullName) + "\\" + memFile.fileName, new EndianBinaryReader(memFile.memStream));
StatusStripUpdate("Loading " + file.fileName);
var assetsFile = new AssetsFile(Path.GetDirectoryName(fullName) + "\\" + file.fileName, new EndianBinaryReader(file.stream));
if (assetsFile.valid)
{
assetsFile.bundlePath = fullName;
@ -124,6 +128,30 @@ namespace Unity_Studio
reader.Dispose();
}
private static void LoadWebFile(string fullName, EndianBinaryReader reader)
{
var fileName = Path.GetFileName(fullName);
StatusStripUpdate("Loading " + fileName);
var bundleFile = new WebFile(reader);
reader.Dispose();
foreach (var file in bundleFile.fileList)
{
var dummyName = Path.GetDirectoryName(fullName) + "\\" + file.fileName;
switch (CheckFileType(file.stream, out reader))
{
case FileType.AssetsFile:
LoadAssetsFile(dummyName, reader);
break;
case FileType.BundleFile:
LoadBundleFile(dummyName, reader);
break;
case FileType.WebFile:
LoadWebFile(dummyName, reader);
break;
}
}
}
public static void MergeSplitAssets(string dirPath)
{
string[] splitFiles = Directory.GetFiles(dirPath, "*.split0");

View File

@ -29,17 +29,23 @@ namespace Unity_Studio
public static Action<string> StatusStripUpdate;
public static Action<int> ProgressBarMaximumAdd;
public enum FileType
{
AssetsFile,
BundleFile,
WebFile
}
public static int ExtractBundleFile(string bundleFileName)
{
int extractedCount = 0;
if (CheckBundleFile(bundleFileName, out var reader))
if (CheckFileType(bundleFileName, out var reader) == FileType.BundleFile)
{
StatusStripUpdate($"Decompressing {Path.GetFileName(bundleFileName)} ...");
var extractPath = bundleFileName + "_unpacked\\";
Directory.CreateDirectory(extractPath);
var bundleFile = new BundleFile(reader);
foreach (var memFile in bundleFile.MemoryAssetsFileList)
foreach (var memFile in bundleFile.fileList)
{
var filePath = extractPath + memFile.fileName.Replace('/', '\\');
if (!Directory.Exists(Path.GetDirectoryName(filePath)))
@ -52,8 +58,8 @@ namespace Unity_Studio
extractedCount += 1;
using (var file = File.Create(filePath))
{
memFile.memStream.WriteTo(file);
memFile.memStream.Close();
memFile.stream.WriteTo(file);
memFile.stream.Close();
}
}
}
@ -355,9 +361,20 @@ namespace Unity_Studio
return Path.GetInvalidFileNameChars().Aggregate(str, (current, c) => current.Replace(c, '_'));
}
public static bool CheckBundleFile(string fileName, out EndianBinaryReader reader)
public static FileType CheckFileType(MemoryStream stream, out EndianBinaryReader reader)
{
reader = new EndianBinaryReader(stream);
return CheckFileType(reader);
}
public static FileType CheckFileType(string fileName, out EndianBinaryReader reader)
{
reader = new EndianBinaryReader(File.OpenRead(fileName));
return CheckFileType(reader);
}
public static FileType CheckFileType(EndianBinaryReader reader)
{
var signature = reader.ReadStringToNull();
reader.Position = 0;
switch (signature)
@ -366,9 +383,20 @@ namespace Unity_Studio
case "UnityRaw":
case "\xFA\xFA\xFA\xFA\xFA\xFA\xFA\xFA":
case "UnityFS":
return true;
return FileType.BundleFile;
case "UnityWebData1.0":
return FileType.WebFile;
default:
return false;
{
var magic = reader.ReadBytes(2);
reader.Position = 0;
if (WebFile.gzipMagic.SequenceEqual(magic))
{
return FileType.WebFile;
}
return FileType.AssetsFile;
}
}
}

View File

@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
namespace Unity_Studio
{
public class WebFile
{
public static byte[] gzipMagic = { 0x1f, 0x8b };
public List<MemoryFile> fileList = new List<MemoryFile>();
public class WebData
{
public int dataOffset;
public int dataLength;
public string path;
}
public WebFile(EndianBinaryReader reader)
{
var magic = reader.ReadBytes(2);
reader.Position = 0;
if (gzipMagic.SequenceEqual(magic))
{
var stream = new MemoryStream();
using (var gstream = new GZipStream(reader.BaseStream, CompressionMode.Decompress))
{
gstream.CopyTo(stream);
}
stream.Position = 0;
using (reader = new EndianBinaryReader(stream, EndianType.LittleEndian))
{
ReadUnityWebData(reader);
}
}
else
{
ReadUnityWebData(reader);
}
}
private void ReadUnityWebData(EndianBinaryReader reader)
{
var signature = reader.ReadStringToNull();
if (signature != "UnityWebData1.0")
return;
var headLength = reader.ReadInt32();
var dataList = new List<WebData>();
while (reader.Position < headLength)
{
var data = new WebData();
data.dataOffset = reader.ReadInt32();
data.dataLength = reader.ReadInt32();
var pathLength = reader.ReadInt32();
data.path = Encoding.UTF8.GetString(reader.ReadBytes(pathLength));
dataList.Add(data);
}
foreach (var data in dataList)
{
var file = new MemoryFile();
file.fileName = Path.GetFileName(data.path);
reader.Position = data.dataOffset;
file.stream = new MemoryStream(reader.ReadBytes(data.dataLength));
fileList.Add(file);
}
}
}
}

View File

@ -169,7 +169,7 @@
<Compile Include="GOHierarchy.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Unity Studio Classes\Lz4DecoderStream.cs" />
<Compile Include="Lz4DecoderStream.cs" />
<Compile Include="Unity Classes\Material.cs" />
<Compile Include="Unity Classes\Mesh.cs" />
<Compile Include="Unity Classes\GameObject.cs" />
@ -187,6 +187,7 @@
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Unity Studio Classes\AssetsFile.cs" />
<Compile Include="Unity Studio Classes\WebFile.cs" />
<Compile Include="UnityStudioForm.cs">
<SubType>Form</SubType>
</Compile>

View File

@ -169,7 +169,7 @@
<Compile Include="GOHierarchy.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Unity Studio Classes\Lz4DecoderStream.cs" />
<Compile Include="Lz4DecoderStream.cs" />
<Compile Include="Unity Classes\Material.cs" />
<Compile Include="Unity Classes\Mesh.cs" />
<Compile Include="Unity Classes\GameObject.cs" />
@ -187,6 +187,7 @@
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Unity Studio Classes\AssetsFile.cs" />
<Compile Include="Unity Studio Classes\WebFile.cs" />
<Compile Include="UnityStudioForm.cs">
<SubType>Form</SubType>
</Compile>