コード例 #1
0
ファイル: AssetsFile.cs プロジェクト: veloting/AssetStudio
        public AssetsFile(string fullName, EndianBinaryReader reader)
        {
            this.reader   = reader;
            filePath      = fullName;
            fileName      = Path.GetFileName(fullName);
            upperFileName = fileName.ToUpper();
            try
            {
                //SerializedFile::ReadHeader
                header = new SerializedFileHeader();
                header.m_MetadataSize = reader.ReadUInt32();
                header.m_FileSize     = reader.ReadUInt32();
                header.m_Version      = reader.ReadUInt32();
                header.m_DataOffset   = reader.ReadUInt32();

                if (header.m_Version >= 9)
                {
                    header.m_Endianess = reader.ReadByte();
                    header.m_Reserved  = reader.ReadBytes(3);
                    m_FileEndianess    = (EndianType)header.m_Endianess;
                }
                else
                {
                    reader.Position = header.m_FileSize - header.m_MetadataSize;
                    m_FileEndianess = (EndianType)reader.ReadByte();
                }

                //SerializedFile::ReadMetadata
                if (m_FileEndianess == EndianType.LittleEndian)
                {
                    reader.endian = EndianType.LittleEndian;
                }
                if (header.m_Version >= 7)
                {
                    unityVersion = reader.ReadStringToNull();
                }
                if (header.m_Version >= 8)
                {
                    m_TargetPlatform = (BuildTarget)reader.ReadInt32();
                    if (!Enum.IsDefined(typeof(BuildTarget), m_TargetPlatform))
                    {
                        m_TargetPlatform = BuildTarget.UnknownPlatform;
                    }
                }
                platformStr = m_TargetPlatform.ToString();
                if (header.m_Version >= 13)
                {
                    m_EnableTypeTree = reader.ReadBoolean();
                }

                //Read types
                int typeCount = reader.ReadInt32();
                m_Types = new List <SerializedType>(typeCount);
                for (int i = 0; i < typeCount; i++)
                {
                    m_Types.Add(ReadSerializedType());
                }

                if (header.m_Version >= 7 && header.m_Version < 14)
                {
                    var bigIDEnabled = reader.ReadInt32();
                }

                //Read Objects
                int objectCount = reader.ReadInt32();

                var assetIDfmt = "D" + objectCount.ToString().Length; //format for unique ID

                m_Objects = new Dictionary <long, ObjectInfo>(objectCount);
                for (int i = 0; i < objectCount; i++)
                {
                    var objectInfo = new ObjectInfo();
                    if (header.m_Version < 14)
                    {
                        objectInfo.m_PathID = reader.ReadInt32();
                    }
                    else
                    {
                        reader.AlignStream(4);
                        objectInfo.m_PathID = reader.ReadInt64();
                    }
                    objectInfo.byteStart  = reader.ReadUInt32();
                    objectInfo.byteStart += header.m_DataOffset;
                    objectInfo.byteSize   = reader.ReadUInt32();
                    objectInfo.typeID     = reader.ReadInt32();
                    if (header.m_Version < 16)
                    {
                        objectInfo.classID        = reader.ReadUInt16();
                        objectInfo.serializedType = m_Types.Find(x => x.classID == objectInfo.typeID);
                        objectInfo.isDestroyed    = reader.ReadUInt16();
                    }
                    else
                    {
                        var type = m_Types[objectInfo.typeID];
                        objectInfo.serializedType = type;
                        objectInfo.classID        = type.classID;
                    }
                    if (header.m_Version == 15 || header.m_Version == 16)
                    {
                        var stripped = reader.ReadByte();
                    }
                    m_Objects.Add(objectInfo.m_PathID, objectInfo);

                    //Create AssetPreloadData
                    var asset = new AssetPreloadData(this, objectInfo, i.ToString(assetIDfmt));
                    preloadTable.Add(asset.m_PathID, asset);

                    #region read BuildSettings to get version for version 2.x files
                    if (asset.Type == ClassIDType.BuildSettings && header.m_Version == 6)
                    {
                        var nextAsset = reader.Position;

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

                        reader.Position = nextAsset;
                    }
                    #endregion
                }

                if (header.m_Version >= 11)
                {
                    int scriptCount = reader.ReadInt32();
                    m_ScriptTypes = new List <LocalSerializedObjectIdentifier>(scriptCount);
                    for (int i = 0; i < scriptCount; i++)
                    {
                        var m_ScriptType = new LocalSerializedObjectIdentifier();
                        m_ScriptType.localSerializedFileIndex = reader.ReadInt32();
                        if (header.m_Version < 14)
                        {
                            m_ScriptType.localIdentifierInFile = reader.ReadInt32();
                        }
                        else
                        {
                            reader.AlignStream(4);
                            m_ScriptType.localIdentifierInFile = reader.ReadInt64();
                        }
                        m_ScriptTypes.Add(m_ScriptType);
                    }
                }

                int externalsCount = reader.ReadInt32();
                m_Externals = new List <FileIdentifier>(externalsCount);
                for (int i = 0; i < externalsCount; i++)
                {
                    var m_External = new FileIdentifier();
                    if (header.m_Version >= 6)
                    {
                        var tempEmpty = reader.ReadStringToNull();
                    }
                    if (header.m_Version >= 5)
                    {
                        m_External.guid = new Guid(reader.ReadBytes(16));
                        m_External.type = reader.ReadInt32();
                    }
                    m_External.pathName = reader.ReadStringToNull();
                    m_External.fileName = Path.GetFileName(m_External.pathName);
                    m_Externals.Add(m_External);
                }

                if (header.m_Version >= 5)
                {
                    //var userInformation = reader.ReadStringToNull();
                }

                buildType = Regex.Replace(unityVersion, @"\d", "").Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries);
                var firstVersion = int.Parse(unityVersion.Split('.')[0]);
                version = Regex.Matches(unityVersion, @"\d").Cast <Match>().Select(m => int.Parse(m.Value)).ToArray();
                if (firstVersion > 5)//2017 and up
                {
                    var nversion = new int[version.Length - 3];
                    nversion[0] = firstVersion;
                    Array.Copy(version, 4, nversion, 1, version.Length - 4);
                    version = nversion;
                }

                valid = true;
            }
            catch
            {
            }
        }
コード例 #2
0
        public SerializedFile(FileReader reader, AssetsManager assetsManager)
        {
            this.assetsManager = assetsManager;
            this.reader        = reader;
            fullName           = reader.FullPath;
            fileName           = reader.FileName;

            // ReadHeader
            header = new SerializedFileHeader();
            header.m_MetadataSize = reader.ReadUInt32();
            header.m_FileSize     = reader.ReadUInt32();
            header.m_Version      = (SerializedFileFormatVersion)reader.ReadUInt32();
            header.m_DataOffset   = reader.ReadUInt32();

            if (header.m_Version >= SerializedFileFormatVersion.kUnknown_9)
            {
                header.m_Endianess = reader.ReadByte();
                header.m_Reserved  = reader.ReadBytes(3);
                m_FileEndianess    = header.m_Endianess;
            }
            else
            {
                reader.Position = header.m_FileSize - header.m_MetadataSize;
                m_FileEndianess = reader.ReadByte();
            }

            if (header.m_Version >= SerializedFileFormatVersion.kLargeFilesSupport)
            {
                header.m_MetadataSize = reader.ReadUInt32();
                header.m_FileSize     = reader.ReadInt64();
                header.m_DataOffset   = reader.ReadInt64();
                reader.ReadInt64(); // unknown
            }

            // ReadMetadata
            if (m_FileEndianess == 0)
            {
                reader.endian = EndianType.LittleEndian;
            }
            if (header.m_Version >= SerializedFileFormatVersion.kUnknown_7)
            {
                unityVersion = reader.ReadStringToNull();
                SetVersion(unityVersion);
            }
            if (header.m_Version >= SerializedFileFormatVersion.kUnknown_8)
            {
                m_TargetPlatform = (BuildTarget)reader.ReadInt32();
                if (!Enum.IsDefined(typeof(BuildTarget), m_TargetPlatform))
                {
                    m_TargetPlatform = BuildTarget.UnknownPlatform;
                }
            }
            if (header.m_Version >= SerializedFileFormatVersion.kHasTypeTreeHashes)
            {
                m_EnableTypeTree = reader.ReadBoolean();
            }

            // Read Types
            int typeCount = reader.ReadInt32();

            m_Types = new List <SerializedType>(typeCount);
            for (int i = 0; i < typeCount; i++)
            {
                m_Types.Add(ReadSerializedType(false));
            }

            if (header.m_Version >= SerializedFileFormatVersion.kUnknown_7 && header.m_Version < SerializedFileFormatVersion.kUnknown_14)
            {
                bigIDEnabled = reader.ReadInt32();
            }

            // Read Objects
            int objectCount = reader.ReadInt32();

            m_Objects  = new List <ObjectInfo>(objectCount);
            Objects    = new List <Object>(objectCount);
            ObjectsDic = new Dictionary <long, Object>(objectCount);
            for (int i = 0; i < objectCount; i++)
            {
                var objectInfo = new ObjectInfo();
                if (bigIDEnabled != 0)
                {
                    objectInfo.m_PathID = reader.ReadInt64();
                }
                else if (header.m_Version < SerializedFileFormatVersion.kUnknown_14)
                {
                    objectInfo.m_PathID = reader.ReadInt32();
                }
                else
                {
                    reader.AlignStream();
                    objectInfo.m_PathID = reader.ReadInt64();
                }

                if (header.m_Version >= SerializedFileFormatVersion.kLargeFilesSupport)
                {
                    objectInfo.byteStart = reader.ReadInt64();
                }
                else
                {
                    objectInfo.byteStart = reader.ReadUInt32();
                }

                objectInfo.byteStart += header.m_DataOffset;
                objectInfo.byteSize   = reader.ReadUInt32();
                objectInfo.typeID     = reader.ReadInt32();
                if (header.m_Version < SerializedFileFormatVersion.kRefactoredClassId)
                {
                    objectInfo.classID        = reader.ReadUInt16();
                    objectInfo.serializedType = m_Types.Find(x => x.classID == objectInfo.typeID);
                }
                else
                {
                    var type = m_Types[objectInfo.typeID];
                    objectInfo.serializedType = type;
                    objectInfo.classID        = type.classID;
                }
                if (header.m_Version < SerializedFileFormatVersion.kHasScriptTypeIndex)
                {
                    objectInfo.isDestroyed = reader.ReadUInt16();
                }
                if (header.m_Version >= SerializedFileFormatVersion.kHasScriptTypeIndex && header.m_Version < SerializedFileFormatVersion.kRefactorTypeData)
                {
                    var m_ScriptTypeIndex = reader.ReadInt16();
                    if (objectInfo.serializedType != null)
                    {
                        objectInfo.serializedType.m_ScriptTypeIndex = m_ScriptTypeIndex;
                    }
                }
                if (header.m_Version == SerializedFileFormatVersion.kSupportsStrippedObject || header.m_Version == SerializedFileFormatVersion.kRefactoredClassId)
                {
                    objectInfo.stripped = reader.ReadByte();
                }
                m_Objects.Add(objectInfo);
            }

            if (header.m_Version >= SerializedFileFormatVersion.kHasScriptTypeIndex)
            {
                int scriptCount = reader.ReadInt32();
                m_ScriptTypes = new List <LocalSerializedObjectIdentifier>(scriptCount);
                for (int i = 0; i < scriptCount; i++)
                {
                    var m_ScriptType = new LocalSerializedObjectIdentifier();
                    m_ScriptType.localSerializedFileIndex = reader.ReadInt32();
                    if (header.m_Version < SerializedFileFormatVersion.kUnknown_14)
                    {
                        m_ScriptType.localIdentifierInFile = reader.ReadInt32();
                    }
                    else
                    {
                        reader.AlignStream();
                        m_ScriptType.localIdentifierInFile = reader.ReadInt64();
                    }
                    m_ScriptTypes.Add(m_ScriptType);
                }
            }

            int externalsCount = reader.ReadInt32();

            m_Externals = new List <FileIdentifier>(externalsCount);
            for (int i = 0; i < externalsCount; i++)
            {
                var m_External = new FileIdentifier();
                if (header.m_Version >= SerializedFileFormatVersion.kUnknown_6)
                {
                    var tempEmpty = reader.ReadStringToNull();
                }
                if (header.m_Version >= SerializedFileFormatVersion.kUnknown_5)
                {
                    m_External.guid = new Guid(reader.ReadBytes(16));
                    m_External.type = reader.ReadInt32();
                }
                m_External.pathName = reader.ReadStringToNull();
                m_External.fileName = Path.GetFileName(m_External.pathName);
                m_Externals.Add(m_External);
            }

            if (header.m_Version >= SerializedFileFormatVersion.kSupportsRefObject)
            {
                int refTypesCount = reader.ReadInt32();
                m_RefTypes = new List <SerializedType>(refTypesCount);
                for (int i = 0; i < refTypesCount; i++)
                {
                    m_RefTypes.Add(ReadSerializedType(true));
                }
            }

            if (header.m_Version >= SerializedFileFormatVersion.kUnknown_5)
            {
                userInformation = reader.ReadStringToNull();
            }

            //reader.AlignStream(16);
        }
コード例 #3
0
        public SerializedFile(AssetsManager assetsManager, string fullName, EndianBinaryReader reader)
        {
            this.assetsManager = assetsManager;
            this.reader        = reader;
            this.fullName      = fullName;
            fileName           = Path.GetFileName(fullName);
            upperFileName      = fileName.ToUpper();
            try
            {
                //ReadHeader
                header = new SerializedFileHeader();
                header.m_MetadataSize = reader.ReadUInt32();
                header.m_FileSize     = reader.ReadUInt32();
                header.m_Version      = reader.ReadUInt32();
                header.m_DataOffset   = reader.ReadUInt32();

                if (header.m_Version >= 9)
                {
                    header.m_Endianess = reader.ReadByte();
                    header.m_Reserved  = reader.ReadBytes(3);
                    m_FileEndianess    = (EndianType)header.m_Endianess;
                }
                else
                {
                    reader.Position = header.m_FileSize - header.m_MetadataSize;
                    m_FileEndianess = (EndianType)reader.ReadByte();
                }

                //ReadMetadata
                if (m_FileEndianess == EndianType.LittleEndian)
                {
                    reader.endian = EndianType.LittleEndian;
                }
                if (header.m_Version >= 7)
                {
                    unityVersion = reader.ReadStringToNull();
                    SetVersion(unityVersion);
                }
                if (header.m_Version >= 8)
                {
                    m_TargetPlatform = (BuildTarget)reader.ReadInt32();
                    if (!Enum.IsDefined(typeof(BuildTarget), m_TargetPlatform))
                    {
                        m_TargetPlatform = BuildTarget.UnknownPlatform;
                    }
                }
                if (header.m_Version >= 13)
                {
                    m_EnableTypeTree = reader.ReadBoolean();
                }

                //ReadTypes
                int typeCount = reader.ReadInt32();
                m_Types = new List <SerializedType>(typeCount);
                for (int i = 0; i < typeCount; i++)
                {
                    m_Types.Add(ReadSerializedType());
                }

                if (header.m_Version >= 7 && header.m_Version < 14)
                {
                    var bigIDEnabled = reader.ReadInt32();
                }

                //ReadObjects
                int objectCount = reader.ReadInt32();
                m_Objects = new List <ObjectInfo>(objectCount);
                for (int i = 0; i < objectCount; i++)
                {
                    var objectInfo = new ObjectInfo();
                    if (header.m_Version < 14)
                    {
                        objectInfo.m_PathID = reader.ReadInt32();
                    }
                    else
                    {
                        reader.AlignStream(4);
                        objectInfo.m_PathID = reader.ReadInt64();
                    }
                    objectInfo.byteStart  = reader.ReadUInt32();
                    objectInfo.byteStart += header.m_DataOffset;
                    objectInfo.byteSize   = reader.ReadUInt32();
                    objectInfo.typeID     = reader.ReadInt32();
                    if (header.m_Version < 16)
                    {
                        objectInfo.classID        = reader.ReadUInt16();
                        objectInfo.serializedType = m_Types.Find(x => x.classID == objectInfo.typeID);
                        var isDestroyed = reader.ReadUInt16();
                    }
                    else
                    {
                        var type = m_Types[objectInfo.typeID];
                        objectInfo.serializedType = type;
                        objectInfo.classID        = type.classID;
                    }
                    if (header.m_Version == 15 || header.m_Version == 16)
                    {
                        var stripped = reader.ReadByte();
                    }
                    m_Objects.Add(objectInfo);
                }

                if (header.m_Version >= 11)
                {
                    int scriptCount = reader.ReadInt32();
                    m_ScriptTypes = new List <LocalSerializedObjectIdentifier>(scriptCount);
                    for (int i = 0; i < scriptCount; i++)
                    {
                        var m_ScriptType = new LocalSerializedObjectIdentifier();
                        m_ScriptType.localSerializedFileIndex = reader.ReadInt32();
                        if (header.m_Version < 14)
                        {
                            m_ScriptType.localIdentifierInFile = reader.ReadInt32();
                        }
                        else
                        {
                            reader.AlignStream(4);
                            m_ScriptType.localIdentifierInFile = reader.ReadInt64();
                        }
                        m_ScriptTypes.Add(m_ScriptType);
                    }
                }

                int externalsCount = reader.ReadInt32();
                m_Externals = new List <FileIdentifier>(externalsCount);
                for (int i = 0; i < externalsCount; i++)
                {
                    var m_External = new FileIdentifier();
                    if (header.m_Version >= 6)
                    {
                        var tempEmpty = reader.ReadStringToNull();
                    }
                    if (header.m_Version >= 5)
                    {
                        m_External.guid = new Guid(reader.ReadBytes(16));
                        m_External.type = reader.ReadInt32();
                    }
                    m_External.pathName = reader.ReadStringToNull();
                    m_External.fileName = Path.GetFileName(m_External.pathName);
                    m_Externals.Add(m_External);
                }

                if (header.m_Version >= 5)
                {
                    //var userInformation = reader.ReadStringToNull();
                }

                valid = true;
            }
            catch
            {
                // ignored
            }
        }