private static AssetBundleFile DecompressBundle(string file, string?decompFile) { AssetBundleFile bun = new AssetBundleFile(); Stream fs = File.OpenRead(file); AssetsFileReader r = new AssetsFileReader(fs); bun.Read(r, true); if (bun.bundleHeader6.GetCompressionType() != 0) { Stream nfs; if (decompFile == null) { nfs = new MemoryStream(); } else { nfs = File.Open(decompFile, FileMode.Create, FileAccess.ReadWrite); } AssetsFileWriter w = new AssetsFileWriter(nfs); bun.Unpack(r, w); nfs.Position = 0; fs.Close(); fs = nfs; r = new AssetsFileReader(fs); bun = new AssetBundleFile(); bun.Read(r, false); } return(bun); }
private void decompressButton_Click(object sender, System.EventArgs e) { SaveFileDialog sfd = new SaveFileDialog(); sfd.FileName = Path.GetFileName(bundleStream.Name) + ".unpacked"; sfd.Filter = "All types (*.*)|*.*"; if (sfd.ShowDialog() == DialogResult.OK) { note.Text = "Decompressing..."; decompressButton.Enabled = false; bundleFilename = sfd.FileName; FileStream fileStream = new FileStream(sfd.FileName, FileMode.Create); BackgroundWorker bw = new BackgroundWorker(); bw.DoWork += delegate { bundle.Unpack(reader, new AssetsFileWriter(fileStream)); fileStream.Position = 0; bundle = new AssetBundleFile(); bundle.Read(new AssetsFileReader(fileStream), false); fileStream.Close(); }; bw.RunWorkerCompleted += delegate { note.Text = "Done. Click Load to open the file."; decompressButton.Enabled = false; loadButton.Enabled = true; }; bw.RunWorkerAsync(); } }
public BundleFileInstance(Stream stream, string filePath, string root) { this.stream = stream; path = Path.GetFullPath(filePath); name = Path.Combine(root, Path.GetFileName(path)); file = new AssetBundleFile(); file.Read(new AssetsFileReader(stream), true); assetsFiles = new List <AssetsFileInstance>(); }
public IEnumerable <AssetFile> BuildAssetsFileInstance(Stream stream) { List <AssetFile> result = new List <AssetFile>(); classPackage = new ClassDatabasePackage(); Console.WriteLine("Reading class-data..."); using (var reader = new AssetsFileReader(new FileStream("classdata.tpk", FileMode.Open, FileAccess.Read, FileShare.Read))) { classPackage.Read(reader); } var file = new AssetBundleFile(); activeBundleFile = file; Console.WriteLine("Reading bundleFileStream..."); file.Read(new AssetsFileReader(stream), true); file.reader.Position = 0; Stream memoryStream = new MemoryStream(); Console.WriteLine("Unpacking bundleFile..."); file.Unpack(file.reader, new AssetsFileWriter(memoryStream)); memoryStream.Position = 0; file.Close(); file = new AssetBundleFile(); file.Read(new AssetsFileReader(memoryStream)); Console.WriteLine("file.bundleInf6.dirInf.Length: " + file.bundleInf6.dirInf.Length); for (int i = 0; i < file.bundleInf6.dirInf.Length; i++) { try { if (file.IsAssetsFile(file.reader, file.bundleInf6.dirInf[i])) { byte[] assetData = BundleHelper.LoadAssetDataFromBundle(file, i); var mainStream = new MemoryStream(assetData); activeStreams.Add(mainStream); string mainName = file.bundleInf6.dirInf[i].name; var fileInstance = new AssetsFileInstance(mainStream, mainName, ""); ClassDatabaseFile classDBFile = LoadClassDatabaseFromPackage(fileInstance.file.typeTree.unityVersion); if (classDBFile == null) { Console.WriteLine("classDatabaseFile was null? Okay, that's probably bad. Continuing anyway..."); } result.Add(new AssetFile(fileInstance, classDBFile)); } } catch (Exception e) { Console.WriteLine("caught exception while reading AssetsFile: " + e); //guess it's not an assetsFile then? } } Console.WriteLine("found " + result.Count + " AssetFiles"); return(result); }
public BundleFileInstance(Stream stream, string filePath, string root, bool unpackIfPacked) { path = Path.GetFullPath(filePath); name = Path.Combine(root, Path.GetFileName(path)); file = new AssetBundleFile(); file.Read(new AssetsFileReader(stream), true); if (file.bundleHeader6 != null && file.bundleHeader6.GetCompressionType() != 0 && unpackIfPacked) { file = BundleHelper.UnpackBundle(file); } loadedAssetsFiles = new List <AssetsFileInstance>(); }
public BundleFileInstance(FileStream stream, string root) { this.stream = stream; path = stream.Name; name = Path.Combine(root, Path.GetFileName(path)); file = new AssetBundleFile(); file.Read(new AssetsFileReader(stream), true); assetsFiles.AddRange( Enumerable.Range(0, (int)file.bundleInf6.blockCount) .Select(d => (AssetsFileInstance)null) ); }
public BundleFileInstance(FileStream stream, string root, bool unpackIfPacked) { this.stream = stream; path = stream.Name; name = Path.Combine(root, Path.GetFileName(path)); file = new AssetBundleFile(); file.Read(new AssetsFileReader(stream), true); if (file.bundleHeader6.GetCompressionType() != 0 && unpackIfPacked) { file = BundleHelper.UnpackBundle(file); } assetsFiles = new List <AssetsFileInstance>(); }
static void Main(string[] args) { var currentDir = new DirectoryInfo(@"."); var files = currentDir.GetFiles("*.unity3d"); foreach (var file in files) { AssetBundleFile bundle = new AssetBundleFile(); bundle.Read(new UnityBinaryReader(file.FullName)); Console.WriteLine("Loading " + file.Name); AssetsFile assets = new AssetsFile(); assets.Read(new UnityBinaryReader(bundle.Files[0].Data)); for (int typeid = 0; typeid < assets.Types.Length; typeid++) { if (assets.Types[typeid].ClassID == (int)ClassIDType.AssetBundle) { continue; } var des = DynamicAsset.GetDeserializer(assets.Types[typeid]); var ser = DynamicAsset.GetSerializer(assets.Types[typeid]); Console.WriteLine("Checking " + assets.Types[typeid].TypeTree.Nodes[0].Type); foreach (var obj in assets.Objects.Where(obj => obj.TypeID == typeid)) { byte[] org = obj.Data; var asset = des(new UnityBinaryReader(org)); UnityBinaryWriter w = new UnityBinaryWriter(); ser(w, asset); byte[] result = w.ToBytes(); for (int i = 0; i < result.Length; i++) { if (result[i] != org[i]) { throw new Exception(); } } string name = asset.HasMember("m_Name") ? asset.AsDynamic().m_Name : "(unnamed asset)"; Console.WriteLine(name + " Passed for check (" + result.Length + "bytes)"); } } } Console.WriteLine("Check has done."); System.Console.ReadLine(); }
private void DecompressToFile(string savePath) { var bundleStream = File.Open(savePath, FileMode.Create); BundleInst.file.Unpack(BundleInst.file.reader, new AssetsFileWriter(bundleStream)); bundleStream.Position = 0; var newBundle = new AssetBundleFile(); newBundle.Read(new AssetsFileReader(bundleStream)); BundleInst.file.reader.Close(); BundleInst.file = newBundle; }
public static AssetBundleFile UnpackBundleToStream(AssetBundleFile file, Stream stream, bool freeOriginalStream = true) { file.Unpack(file.reader, new AssetsFileWriter(stream)); stream.Position = 0; AssetBundleFile newFile = new AssetBundleFile(); newFile.Read(new AssetsFileReader(stream), false); if (freeOriginalStream) { file.reader.Close(); } return(newFile); }
private void DecompressToMemory() { var bundleStream = new MemoryStream(); BundleInst.file.Unpack(BundleInst.file.reader, new AssetsFileWriter(bundleStream)); bundleStream.Position = 0; var newBundle = new AssetBundleFile(); newBundle.Read(new AssetsFileReader(bundleStream)); BundleInst.file.reader.Close(); BundleInst.file = newBundle; }
public BundleFileInstance(FileStream stream, string root, bool unpackIfPacked) { this.stream = stream; path = stream.Name; name = Path.Combine(root, Path.GetFileName(path)); file = new AssetBundleFile(); file.Read(new AssetsFileReader(stream), true); if (file.bundleHeader6.GetCompressionType() != 0 && unpackIfPacked) { file = BundleHelper.UnpackBundle(file); } assetsFiles.AddRange( Enumerable.Range(0, file.bundleInf6.blockCount) .Select(d => (AssetsFileInstance)null) ); }
public static void UnpackInfoOnly(this AssetBundleFile bundle) { AssetsFileReader reader = bundle.reader; reader.Position = 0; if (bundle.Read(reader, true)) { reader.Position = bundle.bundleHeader6.GetBundleInfoOffset(); MemoryStream blocksInfoStream; AssetsFileReader memReader; int compressedSize = (int)bundle.bundleHeader6.compressedSize; switch (bundle.bundleHeader6.GetCompressionType()) { case 1: using (MemoryStream mstream = new MemoryStream(reader.ReadBytes(compressedSize))) { blocksInfoStream = SevenZipHelper.StreamDecompress(mstream); } break; case 2: case 3: byte[] uncompressedBytes = new byte[bundle.bundleHeader6.decompressedSize]; using (MemoryStream mstream = new MemoryStream(reader.ReadBytes(compressedSize))) { var decoder = new Lz4DecoderStream(mstream); decoder.Read(uncompressedBytes, 0, (int)bundle.bundleHeader6.decompressedSize); decoder.Dispose(); } blocksInfoStream = new MemoryStream(uncompressedBytes); break; default: blocksInfoStream = null; break; } if (bundle.bundleHeader6.GetCompressionType() != 0) { using (memReader = new AssetsFileReader(blocksInfoStream)) { memReader.Position = 0; bundle.bundleInf6.Read(0, memReader); } } } }
private void DecompressToMemory(BundleFileInstance bundleInst) { AssetBundleFile bundle = bundleInst.file; MemoryStream bundleStream = new MemoryStream(); bundle.Unpack(bundle.reader, new AssetsFileWriter(bundleStream)); bundleStream.Position = 0; AssetBundleFile newBundle = new AssetBundleFile(); newBundle.Read(new AssetsFileReader(bundleStream), false); bundle.reader.Close(); bundleInst.file = newBundle; }
private void DecompressToFile(BundleFileInstance bundleInst, string savePath) { AssetBundleFile bundle = bundleInst.file; FileStream bundleStream = File.Open(savePath, FileMode.Create); bundle.Unpack(bundle.reader, new AssetsFileWriter(bundleStream)); bundleStream.Position = 0; AssetBundleFile newBundle = new AssetBundleFile(); newBundle.Read(new AssetsFileReader(bundleStream), false); bundle.reader.Close(); bundleInst.file = newBundle; }
public static AssetBundleFile UnpackBundle(AssetBundleFile file, bool freeOriginalStream = true) { MemoryStream ms = new MemoryStream(); file.Unpack(file.reader, new AssetsFileWriter(ms)); ms.Position = 0; AssetBundleFile newFile = new AssetBundleFile(); newFile.Read(new AssetsFileReader(ms), false); if (freeOriginalStream) { file.reader.Close(); } return(newFile); }
private void DecompressBundle(Stream stream) { status.Text = "Decompressing..."; decompressBundle.Enabled = false; decompressBundleInMemory.Enabled = false; BackgroundWorker bw = new BackgroundWorker(); string error = string.Empty; bw.DoWork += delegate { try { file.reader.Position = 0; file.Unpack(file.reader, new AssetsFileWriter(stream)); stream.Position = 0; file = new AssetBundleFile(); file.Read(new AssetsFileReader(stream), false); inst.file = file; } catch (Exception ex) { error = ex.ToString(); } }; bw.RunWorkerCompleted += delegate { if (error != string.Empty) { MessageBox.Show("An error occurred:\n" + error, "Assets View", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { justThisFile.Enabled = true; fileAndDependencies.Enabled = true; compressBundle.Enabled = true; decompressBundle.Enabled = false; decompressBundleInMemory.Enabled = false; status.Text = $"Opening bundle file {fileName}..."; } }; bw.RunWorkerAsync(); }
private void ReadBundle(FileStream stream) { reader = new AssetsFileReader(stream); bundle = new AssetBundleFile(); bundleFilename = stream.Name; bundle.Read(reader, true); reader.Position = 0; switch (bundle.bundleHeader6.flags & 0x3F) { case 0: compressionMethod.Text = "Compression Method: None"; break; case 1: compressionMethod.Text = "Compression Method: LZMA"; break; case 2: case 3: compressionMethod.Text = "Compression Method: LZ4"; break; default: compressionMethod.Text = "Compression Method: Unknown"; break; } if ((bundle.bundleHeader6.flags & 0x3F) == 0) { note.Text = "Bundle is not compressed. You can load it or compress it."; compressButton.Enabled = true; loadButton.Enabled = true; } else { note.Text = "Bundle is compressed. You must decompress the bundle to load."; decompressButton.Enabled = true; } }
static string PostProcessBundle(BuiltAssetBundle bundle, Dictionary <string, string> assemblyReplacements) { Debug.Log($"Post Processing Bundle -> {bundle.File}"); var am = new AssetsManager(); am.LoadClassPackage("Assets\\WeaverCore\\Libraries\\classdata.tpk"); var bun = am.LoadBundleFile(bundle.File.FullName); //load the first entry in the bundle (hopefully the one we want) var assetsFileData = BundleHelper.LoadAssetDataFromBundle(bun.file, 0); var assetsFileName = bun.file.bundleInf6.dirInf[0].name; //name of the first entry in the bundle //I have a new update coming, but in the current release, assetsmanager //will only load from file, not from the AssetsFile class. so we just //put it into memory and load from that... var assetsFileInst = am.LoadAssetsFile(new MemoryStream(assetsFileData), "dummypath", false); var assetsFileTable = assetsFileInst.table; //load cldb from classdata.tpk am.LoadClassDatabaseFromPackage(assetsFileInst.file.typeTree.unityVersion); List <BundleReplacer> bundleReplacers = new List <BundleReplacer>(); List <AssetsReplacer> assetReplacers = new List <AssetsReplacer>(); foreach (var info in assetsFileTable.assetFileInfo) { //If object is a MonoScript, change the script's "m_AssemblyName" from "Assembly-CSharp" to name of mod assembly if (info.curFileType == 0x73) { //MonoDeserializer.GetMonoBaseField var monoScriptInst = am.GetTypeInstance(assetsFileInst.file, info).GetBaseField(); var m_AssemblyNameValue = monoScriptInst.Get("m_AssemblyName").GetValue(); foreach (var testAsm in assemblyReplacements) { var assemblyName = m_AssemblyNameValue.AsString(); if (assemblyName.Contains(testAsm.Key)) { var newAsmName = assemblyName.Replace(testAsm.Key, testAsm.Value); //change m_AssemblyName field m_AssemblyNameValue.Set(newAsmName); //rewrite the asset and add it to the pending list of changes //Debug.Log($"Replacing in Monoscript {assemblyName} -> {newAsmName}"); assetReplacers.Add(new AssetsReplacerFromMemory(0, info.index, (int)info.curFileType, 0xffff, monoScriptInst.WriteToByteArray())); break; } } } //If the object is a MonoBehaviour #if !REWRITE_REGISTRIES else if (info.curFileType == 0x72) { AssetTypeValueField monoBehaviourInst = null; try { monoBehaviourInst = am.GetTypeInstance(assetsFileInst, info).GetBaseField(); } catch (Exception e) { Debug.LogError("An exception occured when reading a MonoBehaviour, skipping"); foreach (var field in info.GetType().GetFields()) { Debug.LogError($"{field.Name} = {field.GetValue(info)}"); } Debug.LogException(e); continue; } // /*try * { * monoBehaviourInst = MonoDeserializer.GetMonoBaseField(am, assetsFileInst, info, new DirectoryInfo("Library\\ScriptAssemblies").FullName); * } * catch (Exception) * { * continue; * }*/ //If this MonoBehaviour has a field called "modAssemblyName", then it's a Registry object //If MonoBehaviour is a registry, replace the "modAssemblyName" variable from "Assembly-CSharp" if (!monoBehaviourInst.Get("modAssemblyName").IsDummy()) { var modAsmNameVal = monoBehaviourInst.Get("modAssemblyName").GetValue(); foreach (var replacement in assemblyReplacements) { if (modAsmNameVal.AsString() == replacement.Key) { modAsmNameVal.Set(replacement.Value); Debug.Log($"Replacing Registry Assembly From {replacement.Key} to {replacement.Value}"); assetReplacers.Add(new AssetsReplacerFromMemory(0, info.index, (int)info.curFileType, AssetHelper.GetScriptIndex(assetsFileInst.file, info), monoBehaviourInst.WriteToByteArray())); break; } } } } #endif } //rewrite the assets file back to memory byte[] modifiedAssetsFileBytes; using (MemoryStream ms = new MemoryStream()) using (AssetsFileWriter aw = new AssetsFileWriter(ms)) { aw.bigEndian = false; assetsFileInst.file.Write(aw, 0, assetReplacers, 0); modifiedAssetsFileBytes = ms.ToArray(); } //adding the assets file to the pending list of changes for the bundle bundleReplacers.Add(new BundleReplacerFromMemory(assetsFileName, assetsFileName, true, modifiedAssetsFileBytes, modifiedAssetsFileBytes.Length)); byte[] modifiedBundleBytes; using (MemoryStream ms = new MemoryStream()) using (AssetsFileWriter aw = new AssetsFileWriter(ms)) { bun.file.Write(aw, bundleReplacers); modifiedBundleBytes = ms.ToArray(); } using (MemoryStream mbms = new MemoryStream(modifiedBundleBytes)) using (AssetsFileReader ar = new AssetsFileReader(mbms)) { AssetBundleFile modifiedBundle = new AssetBundleFile(); modifiedBundle.Read(ar); //recompress the bundle and write it (this is optional of course) using (FileStream ms = File.OpenWrite(bundle.File.FullName + ".edit")) using (AssetsFileWriter aw = new AssetsFileWriter(ms)) { bun.file.Pack(modifiedBundle.reader, aw, AssetBundleCompressionType.LZ4); } } return(bundle.File.FullName + ".edit"); }
private void CompressBundle(Stream stream) { status.Text = "Compressing..."; justThisFile.Enabled = false; fileAndDependencies.Enabled = false; compressBundle.Enabled = false; DialogResult option = MessageBox.Show("Compress in LZMA?\nYes - LZMA\nNo - LZ4", "Compression options", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); AssetBundleCompressionType comp; if (option == DialogResult.Yes) { comp = AssetBundleCompressionType.LZMA; } else if (option == DialogResult.No) { comp = AssetBundleCompressionType.LZ4; } else { return; } BackgroundWorker bw = new BackgroundWorker(); string error = string.Empty; bw.DoWork += delegate { try { file.reader.Position = 0; file.Pack(file.reader, new AssetsFileWriter(stream), comp); stream.Position = 0; file = new AssetBundleFile(); file.Read(new AssetsFileReader(stream), false); inst.file = file; } catch (Exception ex) { error = ex.ToString(); } }; bw.RunWorkerCompleted += delegate { if (error != string.Empty) { MessageBox.Show("An error occurred:\n" + error, "Assets View", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { decompressBundle.Enabled = true; decompressBundleInMemory.Enabled = true; justThisFile.Enabled = false; fileAndDependencies.Enabled = false; compressBundle.Enabled = false; status.Text = $"Opening bundle file {fileName}..."; } }; bw.RunWorkerAsync(); }