AssetsTools.NET icon indicating copy to clipboard operation
AssetsTools.NET copied to clipboard

Can't open written file

Open bluewave41 opened this issue 3 years ago • 0 comments

Opening my bundle with AssetViewer shows 3 files

Icon - Sprite SpriteIndex - AssetBundle Icon - Texture2D

It's unity version is 2019.4.23f1

I've copy pasted the code for Writing an Asset Bundle as so

            var am = new AssetsManager();

            var bunInst = am.LoadBundleFile("art.bundle");
            //read the boring file from the bundle
            var inst = am.LoadAssetsFileFromBundle(bunInst, 0, false);

            var inf = inst.table.GetAssetInfo("Icon");
            var baseField = am.GetTypeInstance(inst, inf).GetBaseField();
            var thing = baseField.Get("m_Name").GetValue().AsString();
            Console.WriteLine(thing);
            baseField.Get("m_Name").GetValue().Set("MyCoolAsset");

            var newGoBytes = baseField.WriteToByteArray();
            var repl = new AssetsReplacerFromMemory(0, inf.index, (int)inf.curFileType, 0xffff, newGoBytes);

            //write changes to memory
            byte[] newAssetData;
            using (var stream = new MemoryStream())
            using (var writer = new AssetsFileWriter(stream))
            {
                inst.file.Write(writer, 0, new List<AssetsReplacer>() { repl }, 0);
                newAssetData = stream.ToArray();
            }

            //rename this asset name from boring to cool when saving
            var bunRepl = new BundleReplacerFromMemory("Icon", "Renamed", true, newAssetData, -1);

            var bunWriter = new AssetsFileWriter(File.OpenWrite("coolbundle.bundle"));
            bunInst.file.Write(bunWriter, new List<BundleReplacer>() { bunRepl });

Opening the created bundle gives the following error

System.ArgumentOutOfRangeException: Stream length must be non-negative and less than 2^31 - 1 - origin. (Parameter 'value')
   at System.IO.MemoryStream.set_Position(Int64 value)
   at AssetsTools.NET.AssetsFile..ctor(AssetsFileReader reader) in C:\Users\nesquack\Documents\GitReposLocal\AssetsTools.NET\AssetTools.NET\Standard\AssetsFileFormat\AssetsFile.cs:line 49
   at AssetsTools.NET.Extra.AssetsFileInstance..ctor(Stream stream, String filePath, String root) in C:\Users\nesquack\Documents\GitReposLocal\AssetsTools.NET\AssetTools.NET\Extra\AssetsManager\AssetsFileInstance.cs:line 26
   at AssetsTools.NET.Extra.AssetsManager.LoadAssetsFile(Stream stream, String path, Boolean loadDeps, String root, BundleFileInstance bunInst) in C:\Users\nesquack\Documents\GitReposLocal\AssetsTools.NET\AssetTools.NET\Extra\AssetsManager\AssetsManager.cs:line 29
   at AssetsView.Winforms.StartScreen.LoadBundleFile(String path) in C:\Users\nesquack\Documents\GitReposLocal\AssetsTools.NET\AssetsView\Winforms\StartScreen.cs:line 128
   at AssetsView.Winforms.StartScreen.addFileToolStripMenuItem_Click(Object sender, EventArgs e) in C:\Users\nesquack\Documents\GitReposLocal\AssetsTools.NET\AssetsView\Winforms\StartScreen.cs:line 103
   at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
   at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)
   at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
   at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
   at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
   at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
   at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
   at System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.ToolStrip.WndProc(Message& m)
   at System.Windows.Forms.ToolStripDropDown.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

If I DON'T rename the file then it saves and opens properly. That's exactly why I need this though... to rename a file in a bundle and I can't find a different tool that can do this programatically.

The headers are here where the first is changed and the second unchanged:

UnityFS    5.x.x 2019.4.23f1       1»   [   [   @                                   bH  bH @                 1    CAB-0207725071a623d62e45b4a33e9c7b85       1       1(   509   	é  1           2019.4.23f1 
UnityFS    5.x.x 2019.4.23f1       1»   [   [   @                                   1   1  @                 1    CAB-0207725071a623d62e45b4a33e9c7b85   				é  1           2019.4.23f1 

So how do I rename this file then?

bluewave41 avatar Mar 09 '22 03:03 bluewave41