Example #1
0
        public BundleFile(string fileName)
        {
            if (Path.GetExtension(fileName) == ".lz4")
            {
                byte[] filebuffer;

                using (BinaryReader lz4Stream = new BinaryReader(File.OpenRead(fileName)))
                {
                    int version          = lz4Stream.ReadInt32();
                    int uncompressedSize = lz4Stream.ReadInt32();
                    int compressedSize   = lz4Stream.ReadInt32();
                    int something        = lz4Stream.ReadInt32(); //1

                    byte[] lz4buffer = new byte[compressedSize];
                    lz4Stream.Read(lz4buffer, 0, compressedSize);

                    using (var inputStream = new MemoryStream(lz4buffer))
                    {
                        var decoder = new Lz4DecoderStream(inputStream);

                        filebuffer = new byte[uncompressedSize]; //is this ok?
                        for (;;)
                        {
                            int nRead = decoder.Read(filebuffer, 0, uncompressedSize);
                            if (nRead == 0)
                            {
                                break;
                            }
                        }
                    }
                }

                using (var b_Stream = new EndianStream(new MemoryStream(filebuffer), EndianType.BigEndian))
                {
                    readBundle(b_Stream);
                }
            }
            else
            {
                using (var b_Stream = new EndianStream(File.OpenRead(fileName), EndianType.BigEndian))
                {
                    readBundle(b_Stream);
                }
            }
        }
Example #2
0
        private void getFiles(EndianStream f_Stream, int offset)
        {
            int fileCount = f_Stream.ReadInt32();

            for (int i = 0; i < fileCount; i++)
            {
                MemoryAssetsFile memFile = new MemoryAssetsFile();
                memFile.fileName = f_Stream.ReadStringToNull();
                int fileOffset = f_Stream.ReadInt32();
                fileOffset += offset;
                int  fileSize = f_Stream.ReadInt32();
                long nextFile = f_Stream.Position;
                f_Stream.Position = fileOffset;

                byte[] buffer = new byte[fileSize];
                f_Stream.Read(buffer, 0, fileSize);
                memFile.memStream = new MemoryStream(buffer);
                MemoryAssetsFileList.Add(memFile);
                f_Stream.Position = nextFile;
            }
        }
Example #3
0
        private void readBundle(EndianStream b_Stream)
        {
            var header = b_Stream.ReadStringToNull();

            if (header == "UnityWeb" || header == "UnityRaw" || header == "\xFA\xFA\xFA\xFA\xFA\xFA\xFA\xFA")
            {
                ver1 = b_Stream.ReadInt32();
                ver2 = b_Stream.ReadStringToNull();
                ver3 = b_Stream.ReadStringToNull();
                if (ver1 < 6)
                {
                    int bundleSize = b_Stream.ReadInt32();
                }
                else
                {
                    long bundleSize = b_Stream.ReadInt64();
                    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 (header)
                {
                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 EndianStream(SevenZip.Compression.LZMA.SevenZipHelper.StreamDecompress(new MemoryStream(lzmaBuffer)), EndianType.BigEndian))
                    {
                        getFiles(lzmaStream, 0);
                    }
                    break;
                }

                case "UnityRaw":
                {
                    getFiles(b_Stream, offset);
                    break;
                }
                }
            }
            else if (header == "UnityFS")
            {
                ver1 = b_Stream.ReadInt32();
                ver2 = b_Stream.ReadStringToNull();
                ver3 = b_Stream.ReadStringToNull();
                long bundleSize = b_Stream.ReadInt64();
            }
        }
Example #4
0
        public AssetsFile(string fileName, EndianStream fileStream)
        {
            //if (memFile != null) { Stream = new EndianStream(memFile, endianType); }
            //else { Stream = new EndianStream(File.OpenRead(fileName), endianType); }
            a_Stream = fileStream;

            filePath = fileName;
            int tableSize = a_Stream.ReadInt32();
            int dataEnd   = a_Stream.ReadInt32();

            fileGen = a_Stream.ReadInt32();
            int dataOffset = a_Stream.ReadInt32();

            sharedAssetsList[0].fileName = Path.GetFileName(fileName); //reference itself because sharedFileIDs start from 1

            switch (fileGen)
            {
            case 6:    //2.5.0 - 2.6.1
            {
                a_Stream.Position  = (dataEnd - tableSize);
                a_Stream.Position += 1;
                break;
            }

            case 7:    //3.0.0 beta
            {
                a_Stream.Position  = (dataEnd - tableSize);
                a_Stream.Position += 1;
                m_Version          = a_Stream.ReadStringToNull();
                break;
            }

            case 8:    //3.0.0 - 3.4.2
            {
                a_Stream.Position  = (dataEnd - tableSize);
                a_Stream.Position += 1;
                m_Version          = a_Stream.ReadStringToNull();
                platform           = a_Stream.ReadInt32();
                break;
            }

            case 9:                     //3.5.0 - 4.6.x
            {
                a_Stream.Position += 4; //azero
                m_Version          = a_Stream.ReadStringToNull();
                platform           = a_Stream.ReadInt32();
                break;
            }

            case 14:                    //5.0.0 beta and final
            case 15:                    //5.0.1 and up
            {
                a_Stream.Position += 4; //azero
                m_Version          = a_Stream.ReadStringToNull();
                platform           = a_Stream.ReadInt32();
                baseDefinitions    = a_Stream.ReadBoolean();
                break;
            }

            default:
            {
                //MessageBox.Show("Unsupported version!", "AssetStudio Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            }

            if (platform > 255 || platform < 0)
            {
                byte[] b32 = BitConverter.GetBytes(platform);
                Array.Reverse(b32);
                platform = BitConverter.ToInt32(b32, 0);
                //endianType = EndianType.LittleEndian;
                a_Stream.endian = EndianType.LittleEndian;
            }

            switch (platform)
            {
            case -2: platformStr = "Unt Package"; break;

            case 4: platformStr = "OSX"; break;

            case 5: platformStr = "PC"; break;

            case 6: platformStr = "Web"; break;

            case 7: platformStr = "Web streamed"; break;

            case 9: platformStr = "iOS"; break;

            case 10: platformStr = "PS3"; break;

            case 11: platformStr = "Xbox 360"; break;

            case 13: platformStr = "Android"; break;

            case 16: platformStr = "Google NaCl"; break;

            case 21: platformStr = "WP8"; break;

            case 25: platformStr = "Linux"; break;
            }

            int baseCount = a_Stream.ReadInt32();

            for (int i = 0; i < baseCount; i++)
            {
                if (fileGen < 14)
                {
                    int    classID  = a_Stream.ReadInt32();
                    string baseType = a_Stream.ReadStringToNull();
                    string baseName = a_Stream.ReadStringToNull();
                    a_Stream.Position += 20;
                    int memberCount = a_Stream.ReadInt32();

                    StringBuilder cb = new StringBuilder();
                    for (int m = 0; m < memberCount; m++)
                    {
                        readBase(cb, 1);
                    }

                    var aClass = new ClassStrStruct()
                    {
                        ID = classID, Text = (baseType + " " + baseName), members = cb.ToString()
                    };
                    aClass.SubItems.Add(classID.ToString());
                    ClassStructures.Add(classID, aClass);
                }
                else
                {
                    readBase5();
                }
            }

            if (fileGen >= 7 && fileGen < 14)
            {
                a_Stream.Position += 4;
            }                                                          //azero

            int assetCount = a_Stream.ReadInt32();

            #region asset preload table
            string assetIDfmt = "D" + assetCount.ToString().Length.ToString(); //format for unique ID

            for (int i = 0; i < assetCount; i++)
            {
                //each table entry is aligned individually, not the whole table
                if (fileGen >= 14)
                {
                    a_Stream.AlignStream(4);
                }

                AssetPreloadData asset = new AssetPreloadData();
                if (fileGen < 14)
                {
                    asset.m_PathID = a_Stream.ReadInt32();
                }
                else
                {
                    asset.m_PathID = a_Stream.ReadInt64();
                }
                asset.Offset       = a_Stream.ReadInt32();
                asset.Offset      += dataOffset;
                asset.Size         = a_Stream.ReadInt32();
                asset.Type1        = a_Stream.ReadInt32();
                asset.Type2        = a_Stream.ReadUInt16();
                a_Stream.Position += 2;
                if (fileGen >= 15)
                {
                    byte unknownByte = a_Stream.ReadByte();
                    //this is a single byte, not an int32
                    //the next entry is aligned after this
                    //but not the last!
                    if (unknownByte != 0)
                    {
                        bool investigate = true;
                    }
                }

                if (UnityClassID.Names[asset.Type2] != null)
                {
                    asset.TypeString = UnityClassID.Names[asset.Type2];
                }

                asset.uniqueID = i.ToString(assetIDfmt);

                asset.exportSize = asset.Size;
                asset.sourceFile = this;

                preloadTable.Add(asset.m_PathID, asset);

                #region read BuildSettings to get version for 2.x files
                if (asset.Type2 == 141 && fileGen == 6)
                {
                    long nextAsset = a_Stream.Position;

                    BuildSettings BSettings = new BuildSettings(asset);
                    m_Version = BSettings.m_Version;

                    a_Stream.Position = nextAsset;
                }
                #endregion
            }
            #endregion

            buildType = m_Version.Split(new string[] { ".", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" }, StringSplitOptions.RemoveEmptyEntries);
            string[] strver = (m_Version.Split(new string[] { ".", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "\n" }, StringSplitOptions.RemoveEmptyEntries));
            version = Array.ConvertAll(strver, int.Parse);

            if (fileGen >= 14)
            {
                //this looks like a list of assets that need to be preloaded in memory before anytihng else
                int someCount = a_Stream.ReadInt32();
                for (int i = 0; i < someCount; i++)
                {
                    int num1 = a_Stream.ReadInt32();
                    a_Stream.AlignStream(4);
                    long m_PathID = a_Stream.ReadInt64();
                }
            }

            int sharedFileCount = a_Stream.ReadInt32();
            for (int i = 0; i < sharedFileCount; i++)
            {
                UnityShared shared = new UnityShared();
                shared.aName       = a_Stream.ReadStringToNull();
                a_Stream.Position += 20;
                string sharedFileName = a_Stream.ReadStringToNull(); //relative path
                shared.fileName = sharedFileName.Replace("/", "\\");
                sharedAssetsList.Add(shared);
            }
        }