Ejemplo n.º 1
0
        public object Parse(TypeNode typeNode, ArchiveBinaryReader reader)
        {
            object result = null;

            switch (typeNode.TypeFlag)
            {
            case "bool":
                result = reader.ReadBoolean();
                break;

            case "SInt8":
                result = reader.ReadSByte();
                break;

            case "char":
            case "UInt8":
                result = reader.ReadByte();
                break;

            case "short":
            case "SInt16":
                result = reader.ReadInt16();
                break;

            case "unsigned short":
            case "UInt16":
                result = reader.ReadUInt16();
                break;

            case "int":
            case "SInt32":
                result = reader.ReadInt32();
                break;

            case "unsigned int":
            case "UInt32":
            case "Type*":
                result = reader.ReadUInt32();
                break;

            case "long long":
            case "SInt64":
                result = reader.ReadInt64();
                break;

            case "unsigned long long":
            case "UInt64":
                result = reader.ReadUInt64();
                break;

            case "float":
                result = reader.ReadSingle();
                break;

            case "double":
                result = reader.ReadDouble();
                break;

            case "Quaternionf":
            {
                result = reader.ReadQuaternion();
                break;
            }

            case "float4":
            case "Vector4f":
            {
                result = reader.ReadVector4();
                break;
            }

            case "float3":
            case "Vector3f":
            {
                result = reader.ReadVector3();
                break;
            }

            case "float2":
            case "Vector2f":
            {
                result = reader.ReadVector2();
                break;
            }

            case "ColorRGBA":
            {
                if (typeNode.Version == 2)
                {
                    result = reader.ReadColor32();
                }
                else
                {
                    result = reader.ReadColor();
                }
                break;
            }

            case "Matrix4x4f":
            {
                result = reader.ReadMatrix4x4();
                break;
            }

            case "Hash128":
            {
                result = reader.ReadHash128();
                break;
            }

            case "string":
            {
                result = reader.ReadString();
                break;
            }

            case "vector":
            case "staticvector":
            case "set":
            {
                var valueTypeNode = typeNode.Children[0];
                result = this.Parse(valueTypeNode, reader);
                break;
            }

            case "map":
            {
                var pairTypeNode  = typeNode.Children[0].Children[1];
                var keyTypeNode   = pairTypeNode.Children[0];
                var valueTypeNode = pairTypeNode.Children[1];

                var size = reader.ReadInt32();
                Map map  = new Map(typeNode);
                for (int i = 0; i < size; i++)
                {
                    var key   = this.Parse(keyTypeNode, reader);
                    var value = this.Parse(valueTypeNode, reader);
                    map.Add(key, value);
                }
                result = map;
                break;
            }

            case "Array":
            {
                var valueTypeNode = typeNode.Children[1];
                var size          = reader.ReadInt32();
                result = this.ParseArray(valueTypeNode, size, reader);
                break;
            }

            case "PPtr":
            {
                var fileID = reader.ReadInt32();
                var pathID = reader.ReadInt64();
                result = new PPtr(fileID, pathID, typeNode.TypeName);
                break;
            }

            case "TypelessData":
            {
                var size = reader.ReadInt32();
                result = new TypelessData(reader.ReadBytes(size));
                break;
            }

            case "StreamedResource":
            {
                var source           = reader.ReadString();
                var offset           = reader.ReadUInt64();
                var size             = reader.ReadUInt64();
                var streamedResource = new StreamedResource(source, offset, size);
                result = streamedResource;
                break;
            }

            case "AssetBundle":
            {
                AssetBundle bundle = new AssetBundle(((TypeTree)typeNode).Archive);
                bundle.FullName = reader.ReadString();

                var         size         = reader.ReadInt32();
                List <PPtr> preloadTable = new List <PPtr>(size);
                for (int i = 0; i < size; i++)
                {
                    PPtr pptr = new PPtr(reader.ReadInt32(), reader.ReadInt64(), "PPtr<Object>");
                    preloadTable.Add(pptr);
                }

                bundle.Preloads.AddRange(preloadTable);

                size = reader.ReadInt32();
                List <AssetPair> container = new List <AssetPair>(size);
                for (int i = 0; i < size; i++)
                {
                    var first        = reader.ReadString();
                    var preloadIndex = reader.ReadInt32();
                    var preloadSize  = reader.ReadInt32();
                    var pptr         = new PPtr(reader.ReadInt32(), reader.ReadInt64(), "PPtr<Object>");
                    var pair         = new AssetPair(first, new Objects.AssetInfo(preloadIndex, preloadSize, pptr));
                    container.Add(pair);
                }
                bundle.Container.AddRange(container);

                bundle.MainAsset = new Objects.AssetInfo(reader.ReadInt32(), reader.ReadInt32(), new PPtr(reader.ReadInt32(), reader.ReadInt64(), "PPtr<Object>"));

                bundle.RuntimeCompatibility = reader.ReadUInt32();
                bundle.Name = reader.ReadString();

                size = reader.ReadInt32();
                List <string> dependencies = new List <string>(size);
                for (int i = 0; i < size; i++)
                {
                    dependencies.Add(reader.ReadString());
                }
                bundle.Dependencies.AddRange(dependencies);

                bundle.IsStreamed = reader.ReadBoolean();
                result            = bundle;
                break;
            }

            case "PreloadData":
            {
                PreloadData preloadData = new PreloadData(((TypeTree)typeNode).Archive);

                preloadData.Name = reader.ReadString();

                var         size         = reader.ReadInt32();
                List <PPtr> preloadTable = new List <PPtr>(size);
                for (int i = 0; i < size; i++)
                {
                    PPtr pptr = new PPtr(reader.ReadInt32(), reader.ReadInt64(), "PPtr<Object>");
                    preloadTable.Add(pptr);
                }

                preloadData.Preloads.AddRange(preloadTable);

                size = reader.ReadInt32();
                List <string> dependencies = new List <string>(size);
                for (int i = 0; i < size; i++)
                {
                    dependencies.Add(reader.ReadString());
                }
                preloadData.Dependencies.AddRange(dependencies);
                result = preloadData;
                break;
            }

            case "AssetBundleManifest":
            {
                Objects.AssetBundleManifest obj = new Objects.AssetBundleManifest((TypeTree)typeNode);
                foreach (TypeNode childNode in typeNode.Children)
                {
                    var key        = childNode.FieldName;
                    var childValue = this.Parse(childNode, reader);
                    obj[key] = childValue;
                }
                result = obj;
                break;
            }

            default:
            {
                DynamicObject obj = typeNode is TypeTree ? new UnityDynamicObject((TypeTree)typeNode) : new DynamicObject(typeNode);
                foreach (TypeNode childNode in typeNode.Children)
                {
                    var key        = childNode.FieldName;
                    var childValue = this.Parse(childNode, reader);
                    obj[key] = childValue;
                }
                result = obj;
                break;
            }
            }

            if (typeNode.IsAlign)
            {
                reader.Align(4);
            }

            return(result);
        }
Ejemplo n.º 2
0
        protected virtual void Load(ArchiveBinaryReader reader)
        {
            try
            {
                long startPos   = reader.BaseStream.Position;
                int  headerSize = reader.ReadInt32(true);
                var  fileSize   = reader.ReadInt32(true);
                this.Format          = reader.ReadInt32(true);
                this.assetDataOffset = reader.ReadUInt32(true);

                if (this.Format < 17)
                {
                    throw new NotSupportedException(string.Format("The AssetBundle's format not supported,format:{0}", this.Format));
                }

                bool bigEndian = reader.ReadBoolean();
                reader.IsBigEndian = bigEndian;
                reader.ReadBytes(3);
                this.Version        = reader.ReadCString();
                this.TargetPlatform = reader.ReadUInt32();

                //读取类型树
                var hasTypeTree = reader.ReadBoolean();
                if (!hasTypeTree)
                {
                    throw new NotSupportedException("Missing type tree, not supported");
                }

                Hash128 zero          = new Hash128(new byte[16]);
                var     typeTreeCount = reader.ReadInt32();
                for (var i = 0; i < typeTreeCount; i++)
                {
                    var typeId = reader.ReadInt32();
                    reader.ReadByte();
                    var scriptIndex    = reader.ReadInt16();
                    var hash           = reader.ReadHash128();
                    var propertiesHash = typeId == 114 ? reader.ReadHash128() : zero;
                    var tree           = new TypeTree(this, i, Enum.IsDefined(typeof(TypeID), typeId) ? (TypeID)typeId : TypeID.UnknownType, scriptIndex, hash, propertiesHash);
                    tree.Load(reader);
                    this.trees.Add(tree);
                }

                //读取对象信息
                var objectCount = reader.ReadInt32();
                List <ObjectItem> objectItems = new List <ObjectItem>();
                for (var i = 0; i < objectCount; i++)
                {
                    reader.Align(4);
                    var id       = reader.ReadInt64();
                    var offset   = reader.ReadInt32();
                    var size     = reader.ReadInt32();
                    var index    = reader.ReadInt32();
                    var typeTree = trees[index];
                    var item     = new ObjectItem(id, offset, size, typeTree);
                    objectItems.Add(item);
                }

                //自定义脚本预载表
                var scriptCount = reader.ReadInt32();
                for (int i = 0; i < scriptCount; i++)
                {
                    var fileID = reader.ReadInt32();
                    var pathID = reader.ReadInt64();
                    var pptr   = new PPtr(fileID, pathID, "PPtr<MonoScript>");
                    this.preloadScripts.Add(pptr);
                }

                //读取共享对象
                int refCount = reader.ReadInt32();
                for (int i = 1; i <= refCount; i++)
                {
                    var name     = reader.ReadCString();
                    var guid     = reader.ReadHash128();
                    var type     = reader.ReadInt32();
                    var fileName = reader.ReadCString();
                    fileName = string.IsNullOrEmpty(fileName) ? "" : fileName.ToLower();
                    var assetRef = new ArchiveRef(i, name, type, guid, fileName);
                    this.archiveRefs.Add(assetRef);
                }

                foreach (var item in objectItems)
                {
                    TypeID typeId = item.TypeTree.TypeID;
                    if (typeId != TypeID.AssetBundle && typeId != TypeID.PreloadData)
                    {
                        continue;
                    }

                    ObjectInfo info = new ObjectInfo(this, item.ID, item.TypeTree, item.Offset, item.Size, false);
                    if (info.TypeID == TypeID.AssetBundle)
                    {
                        AssetBundle assetBundle = info.GetObject <AssetBundle>();
                        this.AssetBundle   = assetBundle;
                        Bundle.AssetBundle = assetBundle;
                    }
                    else if (info.TypeID == TypeID.PreloadData)
                    {
                        PreloadData preloadData = info.GetObject <PreloadData>();
                        this.PreloadData = preloadData;
                    }
                }

                foreach (var item in objectItems)
                {
                    TypeID typeId = item.TypeTree.TypeID;
                    if (typeId == TypeID.AssetBundle || typeId == TypeID.PreloadData)
                    {
                        continue;
                    }

                    bool       isPublic = !this.Bundle.IsStreamed && this.AssetBundle != null && this.AssetBundle.IsPublic(item.ID);
                    ObjectInfo info     = new ObjectInfo(this, item.ID, item.TypeTree, item.Offset, item.Size, isPublic);
                    this.objects.Add(info.ID, info);

                    if (info.IsPotentialRedundancy)
                    {
                        reader.BaseStream.Seek(this.assetDataOffset + item.Offset, SeekOrigin.Begin);
                        FeatureInfo featureInfo = this.extractor.Extract(info, reader);
                        info.Fingerprint = new PropertiesFingerprint(info, featureInfo.References, featureInfo.PropertyHash);
                        info.Name        = featureInfo.Name;
                        info.Resources   = featureInfo.Resources;
                    }
                    else
                    {
                        info.Fingerprint = new IdentifierFingerprint(info.ID, this.Name);
                    }
                }
            }
            catch (Exception e)
            {
                Debug.LogErrorFormat("{0}", e);
            }
        }