コード例 #1
0
        private static byte[] ReadSerializedShader(List <ClassMember> members, EndianStream a_Stream)
        {
            var offsets             = new List <uint>();
            var compressedLengths   = new List <uint>();
            var decompressedLengths = new List <uint>();

            for (int i = 0; i < members.Count; i++)
            {
                var member     = members[i];
                var level      = member.Level;
                var varTypeStr = member.Type;
                if (member.Name == "offsets")
                {
                    var offsets_size = a_Stream.ReadInt32();
                    for (int j = 0; j < offsets_size; j++)
                    {
                        offsets.Add(a_Stream.ReadUInt32());
                    }
                    var compressedLengths_size = a_Stream.ReadInt32();
                    for (int j = 0; j < compressedLengths_size; j++)
                    {
                        compressedLengths.Add(a_Stream.ReadUInt32());
                    }
                    var decompressedLengths_size = a_Stream.ReadInt32();
                    for (int j = 0; j < decompressedLengths_size; j++)
                    {
                        decompressedLengths.Add(a_Stream.ReadUInt32());
                    }
                    var compressedBlob     = a_Stream.ReadBytes(a_Stream.ReadInt32());
                    var decompressedStream = new MemoryStream();
                    for (int j = 0; j < offsets.Count; j++)
                    {
                        var compressedBytes = new byte[compressedLengths[j]];
                        Array.Copy(compressedBlob, offsets[j], compressedBytes, 0, compressedLengths[j]);
                        var decompressedBytes = new byte[decompressedLengths[j]];
                        using (var mstream = new MemoryStream(compressedBytes))
                        {
                            var decoder = new Lz4DecoderStream(mstream);
                            decoder.Read(decompressedBytes, 0, (int)decompressedLengths[j]);
                            decoder.Dispose();
                        }
                        decompressedStream.Write(decompressedBytes, 0, decompressedBytes.Length);
                    }
                    var decompressedBlob = decompressedStream.ToArray();
                    return(decompressedBlob);
                }
                var align = (member.Flag & 0x4000) != 0;
                if (varTypeStr == "SInt8")//sbyte
                {
                    a_Stream.ReadSByte();
                }
                else if (varTypeStr == "UInt8")//byte
                {
                    a_Stream.ReadByte();
                }
                else if (varTypeStr == "short" || varTypeStr == "SInt16")//Int16
                {
                    a_Stream.ReadInt16();
                }
                else if (varTypeStr == "UInt16" || varTypeStr == "unsigned short")//UInt16
                {
                    a_Stream.ReadUInt16();
                }
                else if (varTypeStr == "int" || varTypeStr == "SInt32")//Int32
                {
                    a_Stream.ReadInt32();
                }
                else if (varTypeStr == "UInt32" || varTypeStr == "unsigned int")//UInt32
                {
                    a_Stream.ReadUInt32();
                }
                else if (varTypeStr == "long long" || varTypeStr == "SInt64")//Int64
                {
                    a_Stream.ReadInt64();
                }
                else if (varTypeStr == "UInt64" || varTypeStr == "unsigned long long")//UInt64
                {
                    a_Stream.ReadUInt64();
                }
                else if (varTypeStr == "float")//float
                {
                    a_Stream.ReadSingle();
                }
                else if (varTypeStr == "double")//double
                {
                    a_Stream.ReadDouble();
                }
                else if (varTypeStr == "bool")//bool
                {
                    a_Stream.ReadBoolean();
                }
                else if (varTypeStr == "string")//string
                {
                    a_Stream.ReadAlignedString(a_Stream.ReadInt32());
                    i += 3;                     //skip
                }
                else if (varTypeStr == "Array") //Array
                {
                    if ((members[i - 1].Flag & 0x4000) != 0)
                    {
                        align = true;
                    }
                    var size  = a_Stream.ReadInt32();
                    var array = AssetPreloadData.ReadArray(members, level, i);
                    for (int j = 0; j < size; j++)
                    {
                        ReadSerializedShader(array, a_Stream);
                    }
                    i += array.Count + 1;//skip
                }
                else
                {
                    align = false;
                }
                if (align)
                {
                    a_Stream.AlignStream(4);
                }
            }
            return(null);
        }
コード例 #2
0
        private void readBase(List <ClassMember> cb, int level)
        {
            string varType = a_Stream.ReadStringToNull();
            string varName = a_Stream.ReadStringToNull();
            //a_Stream.Position += 20;
            int size          = a_Stream.ReadInt32();
            int index         = a_Stream.ReadInt32();
            int isArray       = a_Stream.ReadInt32();
            int num0          = a_Stream.ReadInt32();
            int flag          = a_Stream.ReadInt32();
            int childrenCount = a_Stream.ReadInt32();

            //Debug.WriteLine(baseFormat + " " + baseName + " " + childrenCount);
            cb.Add(new ClassMember()
            {
                Level = level - 1,
                Type  = varType,
                Name  = varName,
                Size  = size,
                Flag  = flag
            });
            for (int i = 0; i < childrenCount; i++)
            {
                readBase(cb, level + 1);
            }
        }
コード例 #3
0
        public AssetsFile(string fullName, EndianStream fileStream)
        {
            a_Stream = fileStream;
            filePath = fullName;
            fileName = Path.GetFileName(fullName);
            try
            {
                int tableSize = a_Stream.ReadInt32();
                int dataEnd   = a_Stream.ReadInt32();
                fileGen = a_Stream.ReadInt32();
                uint dataOffset = a_Stream.ReadUInt32();
                sharedAssetsList[0].fileName = Path.GetFileName(fullName); //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 - 5.4
                case 16:                    //??.. no sure
                case 17:                    //5.5.0 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 Unity version!" + fileGen, "Unity Studio 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 = "Unity 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 19:
                    platformStr = "CollabPreview";
                    break;

                case 21:
                    platformStr = "WP8";
                    break;

                case 25:
                    platformStr = "Linux";
                    break;

                case 29:
                    platformStr = "Wii U";
                    break;

                default:
                    platformStr = "Unknown Platform";
                    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();

                        var cb = new List <ClassMember>();
                        for (int m = 0; m < memberCount; m++)
                        {
                            readBase(cb, 1);
                        }

                        var aClass = new ClassStruct()
                        {
                            ID = classID, Text = (baseType + " " + baseName), members = cb
                        };
                        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; //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.ReadUInt32();
                    asset.Offset += dataOffset;
                    asset.Size    = a_Stream.ReadInt32();
                    if (fileGen > 15)
                    {
                        int index = a_Stream.ReadInt32();
                        asset.Type1 = classIDs[index][0];
                        asset.Type2 = (ushort)classIDs[index][1];
                    }
                    else
                    {
                        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;
                        }
                    }

                    string typeString;
                    if (ClassIDReference.Names.TryGetValue(asset.Type2, out typeString))
                    {
                        asset.TypeString = typeString;
                    }
                    else
                    {
                        asset.TypeString = "Unknown Type " + asset.Type2;
                    }

                    asset.uniqueID = i.ToString(assetIDfmt);

                    asset.sourceFile = this;

                    preloadTable.Add(asset.m_PathID, asset);

                    #region read BuildSettings to get version for unity 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(buildTypeSplit, StringSplitOptions.RemoveEmptyEntries);
                var strver = from Match m in Regex.Matches(m_Version, @"[0-9]") select m.Value;
                version = Array.ConvertAll(strver.ToArray(), int.Parse);
                if (version[0] == 2 && version[1] == 0 && version[2] == 1 && version[3] == 7)//2017.x
                {
                    var nversion = new int[version.Length - 3];
                    nversion[0] = 2017;
                    Array.Copy(version, 4, nversion, 1, version.Length - 4);
                    version = nversion;
                }
                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);
                }
                valid = true;
            }
            catch
            {
            }
        }
コード例 #4
0
        private void readBundle(EndianStream b_Stream)
        {
            var signature = b_Stream.ReadStringToNull();

            if (signature == "UnityWeb" || signature == "UnityRaw" || signature == "\xFA\xFA\xFA\xFA\xFA\xFA\xFA\xFA")
            {
                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":
                {
                    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;
                }

                case "UnityRaw":
                {
                    getFiles(b_Stream, offset);
                    break;
                }
                }
            }
            else if (signature == "UnityFS")
            {
                format        = b_Stream.ReadInt32();
                versionPlayer = b_Stream.ReadStringToNull();
                versionEngine = b_Stream.ReadStringToNull();
                if (format == 6)
                {
                    ReadFormat6(b_Stream);
                }
            }
        }
コード例 #5
0
        private void ReadFormat6(EndianStream b_Stream, bool padding = false)
        {
            var bundleSize       = b_Stream.ReadInt64();
            int compressedSize   = b_Stream.ReadInt32();
            int uncompressedSize = b_Stream.ReadInt32();
            int flag             = b_Stream.ReadInt32();

            if (padding)
            {
                b_Stream.ReadByte();
            }
            byte[] blocksInfoBytes;
            if ((flag & 0x80) != 0)//at end of file
            {
                var position = b_Stream.Position;
                b_Stream.Position = b_Stream.BaseStream.Length - compressedSize;
                blocksInfoBytes   = b_Stream.ReadBytes(compressedSize);
                b_Stream.Position = position;
            }
            else
            {
                blocksInfoBytes = b_Stream.ReadBytes(compressedSize);
            }
            EndianStream blocksInfo;

            switch (flag & 0x3F)
            {
            default:    //None
            {
                blocksInfo = new EndianStream(new MemoryStream(blocksInfoBytes), EndianType.BigEndian);
                break;
            }

            case 1:    //LZMA
            {
                blocksInfo = new EndianStream(SevenZipHelper.StreamDecompress(new MemoryStream(blocksInfoBytes)), EndianType.BigEndian);
                break;
            }

            case 2:    //LZ4
            case 3:    //LZ4HC
            {
                byte[] uncompressedBytes = new byte[uncompressedSize];
                using (var mstream = new MemoryStream(blocksInfoBytes))
                {
                    var decoder = new Lz4DecoderStream(mstream);
                    decoder.Read(uncompressedBytes, 0, uncompressedSize);
                    decoder.Dispose();
                }
                blocksInfo = new EndianStream(new MemoryStream(uncompressedBytes), EndianType.BigEndian);
                break;
            }
                //case 4:LZHAM?
            }
            using (blocksInfo)
            {
                blocksInfo.Position = 0x10;
                int          blockcount = blocksInfo.ReadInt32();
                EndianStream assetsData;
                var          assetsDataStream = new MemoryStream();
                for (int i = 0; i < blockcount; i++)
                {
                    uncompressedSize = blocksInfo.ReadInt32();
                    compressedSize   = blocksInfo.ReadInt32();
                    flag             = blocksInfo.ReadInt16();
                    var compressedBytes = b_Stream.ReadBytes(compressedSize);
                    switch (flag & 0x3F)
                    {
                    default:    //None
                    {
                        assetsDataStream.Write(compressedBytes, 0, compressedSize);
                        break;
                    }

                    case 1:    //LZMA
                    {
                        var uncompressedBytes = new byte[uncompressedSize];
                        using (var mstream = new MemoryStream(compressedBytes))
                        {
                            var decoder = SevenZipHelper.StreamDecompress(mstream, uncompressedSize);
                            decoder.Read(uncompressedBytes, 0, uncompressedSize);
                            decoder.Dispose();
                        }
                        assetsDataStream.Write(uncompressedBytes, 0, uncompressedSize);
                        break;
                    }

                    case 2:    //LZ4
                    case 3:    //LZ4HC
                    {
                        var uncompressedBytes = new byte[uncompressedSize];
                        using (var mstream = new MemoryStream(compressedBytes))
                        {
                            var decoder = new Lz4DecoderStream(mstream);
                            decoder.Read(uncompressedBytes, 0, uncompressedSize);
                            decoder.Dispose();
                        }
                        assetsDataStream.Write(uncompressedBytes, 0, uncompressedSize);
                        break;
                    }
                        //case 4:LZHAM?
                    }
                }
                assetsData = new EndianStream(assetsDataStream, EndianType.BigEndian);
                using (assetsData)
                {
                    var entryinfo_count = blocksInfo.ReadInt32();
                    for (int i = 0; i < entryinfo_count; i++)
                    {
                        var memFile          = new MemoryAssetsFile();
                        var entryinfo_offset = blocksInfo.ReadInt64();
                        var entryinfo_size   = blocksInfo.ReadInt64();
                        var unknown          = blocksInfo.ReadInt32();
                        memFile.fileName    = blocksInfo.ReadStringToNull();
                        assetsData.Position = entryinfo_offset;
                        var buffer = new byte[entryinfo_size];
                        assetsData.Read(buffer, 0, (int)entryinfo_size);
                        memFile.memStream = new MemoryStream(buffer);
                        MemoryAssetsFileList.Add(memFile);
                    }
                }
            }
        }
コード例 #6
0
 private static void Read(StringBuilder sb, List <ClassMember> members, EndianStream a_Stream)
 {
     for (int i = 0; i < members.Count; i++)
     {
         var    member     = members[i];
         var    level      = member.Level;
         var    varTypeStr = member.Type;
         var    varNameStr = member.Name;
         object value      = null;
         var    align      = (member.Flag & 0x4000) != 0;
         var    append     = true;
         if (varTypeStr == "SInt8")//sbyte
         {
             value = a_Stream.ReadSByte();
         }
         else if (varTypeStr == "UInt8")//byte
         {
             value = a_Stream.ReadByte();
         }
         else if (varTypeStr == "short" || varTypeStr == "SInt16")//Int16
         {
             value = a_Stream.ReadInt16();
         }
         else if (varTypeStr == "UInt16" || varTypeStr == "unsigned short")//UInt16
         {
             value = a_Stream.ReadUInt16();
         }
         else if (varTypeStr == "int" || varTypeStr == "SInt32")//Int32
         {
             value = a_Stream.ReadInt32();
         }
         else if (varTypeStr == "UInt32" || varTypeStr == "unsigned int")//UInt32
         {
             value = a_Stream.ReadUInt32();
         }
         else if (varTypeStr == "long long" || varTypeStr == "SInt64")//Int64
         {
             value = a_Stream.ReadInt64();
         }
         else if (varTypeStr == "UInt64" || varTypeStr == "unsigned long long")//UInt64
         {
             value = a_Stream.ReadUInt64();
         }
         else if (varTypeStr == "float")//float
         {
             value = a_Stream.ReadSingle();
         }
         else if (varTypeStr == "double")//double
         {
             value = a_Stream.ReadDouble();
         }
         else if (varTypeStr == "bool")//bool
         {
             value = a_Stream.ReadBoolean();
         }
         else if (varTypeStr == "string")//string
         {
             append = false;
             var str = a_Stream.ReadAlignedString(a_Stream.ReadInt32());
             sb.AppendFormat("{0}{1} {2} = \"{3}\"\r\n", (new string('\t', level)), varTypeStr, varNameStr, str);
             i += 3;                     //skip
         }
         else if (varTypeStr == "Array") //Array
         {
             append = false;
             if ((members[i - 1].Flag & 0x4000) != 0)
             {
                 align = true;
             }
             sb.AppendFormat("{0}{1} {2}\r\n", (new string('\t', level)), varTypeStr, varNameStr);
             var size = a_Stream.ReadInt32();
             sb.AppendFormat("{0}{1} {2} = {3}\r\n", (new string('\t', level)), "int", "size", size);
             var array = ReadArray(members, level, i);
             for (int j = 0; j < size; j++)
             {
                 sb.AppendFormat("{0}[{1}]\r\n", (new string('\t', level + 1)), j);
                 Read(sb, array, a_Stream);
             }
             i += array.Count + 1;//skip
         }
         else
         {
             append = false;
             align  = false;
             sb.AppendFormat("{0}{1} {2}\r\n", (new string('\t', level)), varTypeStr, varNameStr);
         }
         if (append)
         {
             sb.AppendFormat("{0}{1} {2} = {3}\r\n", (new string('\t', level)), varTypeStr, varNameStr, value);
         }
         if (align)
         {
             a_Stream.AlignStream(4);
         }
     }
 }