Пример #1
0
        public override void Deserialize(Dictionary <PCFResourceType, DataBlockBase> dataBlocks,
                                         UnityNodeBase parentNode,
                                         ResourceResponse resourceResponse,
                                         List <Action <UnityNodeBase> > postInstallActions,
                                         bool optimizedLoad)
        {
            if (!this.isDeserialized)
            {
                ResourceBlock dataBlock = dataBlocks[resourceType] as ResourceBlock;
                AssetResource resource  = dataBlock.GetResource(this.referenceID);

                byte[] metaDataBuffer         = resource.GetMetaData();
                SerializedFieldData fieldData = ProtocolBufferSerializer.DeserializeFieldData(metaDataBuffer);
                string assemblyName           = ProtocolBufferSerializer.GetAssemblyName(fieldData.assemblyType);
                string scriptTypeName         = fieldData.typeName;
                string fieldName = fieldData.fieldName;

                Type scriptType = null;

                //Qualify type check with assembly name, GetType only looks in current assembly otherwise.
                if (!string.IsNullOrEmpty(assemblyName))
                {
                    scriptType = Type.GetType(scriptTypeName + ", " + assemblyName);
                }
                else
                {
                    scriptType = Type.GetType(scriptTypeName);
                }

                if (scriptType != null)
                {
                    Gradient gradient = new Gradient();

                    PropertySetter colorKeySetter = new PropertySetter("colorKeys", (System.Object val) => { gradient.colorKeys = val as GradientColorKey[]; });
                    PropertySetter alphaKeySetter = new PropertySetter("alphaKeys", (System.Object val) => { gradient.alphaKeys = val as GradientAlphaKey[]; });

                    List <PropertySetter> customValueSetters = new List <PropertySetter>();
                    customValueSetters.Add(colorKeySetter);
                    customValueSetters.Add(alphaKeySetter);

                    FieldDeserializer fieldDeserializer = new FieldDeserializer(customValueSetters, gradient);
                    ResourceResponse  response          = new ResourceResponse();
                    response.SetFieldDeserializer(fieldDeserializer);

                    for (int i = 0; i < this.ChildNodes.Count; i++)
                    {
                        UnityNodeBase child = this.ChildNodes[i];

                        child.Deserialize(dataBlocks, this, response, postInstallActions, optimizedLoad);
                    }

                    if (resourceResponse != null)
                    {
                        resourceResponse.GetFieldDeserializer.SetField(fieldName, gradient);
                    }
                }

                this.isDeserialized = true;
            }
        }
Пример #2
0
        public override void Deserialize(Dictionary <PCFResourceType, DataBlockBase> dataBlocks,
                                         UnityNodeBase parentNode,
                                         ResourceResponse resourceResponse,
                                         List <Action <UnityNodeBase> > postInstallActions,
                                         bool optimizedLoad)
        {
            if (!this.isDeserialized)
            {
                this.animator = parentNode.GetGameObject().AddComponent <UnityEngine.Animator>();

                ResourceBlock dataBlock = dataBlocks[resourceType] as ResourceBlock;
                AssetResource resource  = dataBlock.GetResource(this.referenceID);

                byte[]  metaDataBuffer = resource.GetMetaData();
                JObject metaData       = JObject.Parse(System.Text.Encoding.UTF8.GetString(metaDataBuffer));

                this.animator.applyRootMotion = metaData["applyRootMotion"].ToObject <bool>();
                UInt32 avatarID = metaData.Value <UInt32>("avatarReferenceID");

                for (int i = 0; i < this.ChildNodes.Count; i++)
                {
                    UnityNodeBase child = this.ChildNodes[i];

                    ResourceResponse avatarResponse = new ResourceResponse(avatarID, (ResourceResponse response) =>
                    {
                        this.animator.avatar = response.GetAvatarRequest;
                    });

                    child.Deserialize(dataBlocks, this, avatarResponse, postInstallActions, optimizedLoad);
                }

                this.isDeserialized = true;
            }
        }
Пример #3
0
        public override void Serialize(SerializedAssets serializedAssets, object[] serializeOptions, NodeBase objNode, List <Action <NodeBase> > postSerializeActions)
        {
            //Make sure this is always 36 characters long.
            this.referenceID = this.rootNode.GenerateID();

            ComponentNode componentNode = new ComponentNode(PCFResourceType.TRANSFORM, referenceID, null, this.transform.name);

            //Component nodes must always be parented to objNodes.
            objNode.AddChildNode(componentNode);

            Vector3[] vectors = new Vector3[3];
            vectors[0] = this.transform.localPosition;
            vectors[1] = this.transform.localRotation.eulerAngles;
            vectors[2] = this.transform.localScale;

            byte[] bytes = WriteVector3ArrayToBytes(vectors);

            //Create serialized asset by converting data to a bytearray and give it to the constructor.
            AssetResource resource = new AssetResource(false);

            resource.Serialize(referenceID, MetaDataType.UNKOWN, null, bytes);

            serializedAssets.AddResource(referenceID, PCFResourceType.TRANSFORM, resource);

            //Nodes store their resource when serializing
            componentNode.SetSerializer(this);
        }
Пример #4
0
        public override void Deserialize(Dictionary <PCFResourceType, DataBlockBase> dataBlocks,
                                         UnityNodeBase parentNode,
                                         ResourceResponse resourceResponse,
                                         List <Action <UnityNodeBase> > postInstallActions,
                                         bool optimizedLoad)
        {
            if (!this.isDeserialized)
            {
                this.light = parentNode.GetGameObject().AddComponent <Light>();

                ResourceBlock dataBlock = dataBlocks[resourceType] as ResourceBlock;
                AssetResource resource  = dataBlock.GetResource(this.referenceID);

                byte[] bytes = resource.GetResourceData();

                int     count = (bytes.Length / 4);
                float[] data  = new float[count];

                for (int i = 0; i < count; i++)
                {
                    data[i] = BitConverter.ToSingle(bytes, (i * 4));
                }

                light.color     = new Color(data[0], data[1], data[2], data[3]);
                light.type      = (LightType)Enum.ToObject(typeof(LightType), (int)data[4]);
                light.intensity = data[5];

                this.isDeserialized = true;
            }
        }
        public override void Deserialize(Dictionary <PCFResourceType, DataBlockBase> dataBlocks,
                                         UnityNodeBase parentNode,
                                         ResourceResponse resourceResponse,
                                         List <Action <UnityNodeBase> > postInstallActions,
                                         bool optimizedLoad)
        {
            //Deserialize pointed node id.
            ResourceBlock dataBlock = dataBlocks[resourceType] as ResourceBlock;
            AssetResource resource  = dataBlock.GetResource(this.referenceID);

            UInt32 pointedNodeID = BitConverter.ToUInt32(resource.GetResourceData(), 0);

            UnityNodeBase currentParent = parentNode;

            while (currentParent.ParentNode != null)
            {
                currentParent = currentParent.ParentNode;
            }

            UnityNodeBase referencedNode = FindNodeWithID(currentParent, pointedNodeID);

            if (referencedNode is UnityMaterialNode)
            {
                UnityMaterialNode materialNode = referencedNode as UnityMaterialNode;

                Material mat = materialNode.GetMaterial();

                ResourceResponse request = resourceResponse.CanHandle(pointedNodeID);
                if (request != null)
                {
                    request.HandleMaterialResponse(mat);
                }
            }
        }
Пример #6
0
        public override void Serialize(SerializedAssets serializedAssets, object[] serializeOptions, NodeBase objNode, List <Action <NodeBase> > postSerializeActions)
        {
            //Make sure this is always 36 characters long.
            this.referenceID = this.rootNode.GenerateID();

            ComponentNode scriptNode = new ComponentNode(PCFResourceType.CLASS, referenceID, null, this.type.Name.ToString());

            //Parent top level scripts to ObjectNode.
            objNode.AddChildNode(scriptNode);

            FieldInfo[] fields = this.type.GetFields();

            for (int i = 0; i < fields.Length; i++)
            {
                if (fields[i].IsPublic && !fields[i].IsStatic)
                {
                    SerializeField(serializedAssets, serializeOptions, scriptNode, postSerializeActions, fields[i]);
                }
            }

            AssetResource resource = new AssetResource(false);

            byte[] metaDataBuffer = ProtocolBufferSerializer.SerializeFieldData(this.type, this.fieldName, this.arrayItem, this.type.Assembly);
            resource.Serialize(this.referenceID, MetaDataType.PROTOBUF, metaDataBuffer, null);

            serializedAssets.AddResource(referenceID, PCFResourceType.CLASS, resource);
        }
        public override void Serialize(SerializedAssets serializedAssets, object[] serializeOptions, NodeBase objNode, List <Action <NodeBase> > postSerializeActions)
        {
            //Make sure this is always 36 characters long.
            this.referenceID = this.rootNode.GenerateID();

            ComponentNode componentNode = new ComponentNode(PCFResourceType.ANIMATOR, referenceID, null);

            //Component nodes must always be parented to objNodes.
            objNode.AddChildNode(componentNode);

            //Serialize child node ( the avatar )
            //We serialize as reference.
            UnitySerializeAvatar avatarReferenceSerializer = new UnitySerializeAvatar(this.animator, this.parentGO, this.rootNode);

            avatarReferenceSerializer.Serialize(serializedAssets, serializeOptions, componentNode, postSerializeActions);

            JObject metaData = new JObject();

            //Serialize apply root motion variable in the metadata.
            metaData["applyRootMotion"] = this.animator.applyRootMotion;

            //Serialize avatarReference in the metadata.
            metaData["avatarReferenceID"] = avatarReferenceSerializer.GetReferenceID();

            //Create serialized asset by converting data to a bytearray and give it to the constructor.
            AssetResource resource = new AssetResource(false);

            byte[] metaDataBuffer = System.Text.Encoding.UTF8.GetBytes(metaData.ToString(Formatting.None));
            resource.Serialize(referenceID, MetaDataType.JSON, metaDataBuffer, null);

            serializedAssets.AddResource(referenceID, PCFResourceType.ANIMATOR, resource);

            //Nodes store their resource when serializing
            componentNode.SetSerializer(this);
        }
        public override void Serialize(SerializedAssets serializedAssets, object[] serializeOptions, NodeBase objNode, List <Action <NodeBase> > postSerializeActions)
        {
            if (File.Exists(this.internalBundlePath))
            {
                //Make sure this is always 36 characters long.
                this.referenceID = this.rootNode.GenerateID();

                ComponentNode componentNode = new ComponentNode(PCFResourceType.INTERNALBUNDLE, referenceID, null, platform);

                //Material nodes can and are most likely to be children of other component nodes.
                objNode.AddChildNode(componentNode);

                JObject metaData = new JObject();
                metaData["platform"] = platform;
                metaData["contents"] = contents;

                byte[] serializedBundle = File.ReadAllBytes(internalBundlePath);

                //Create serialized asset by converting data to a bytearray and give it to the constructor.
                AssetResource resource = new AssetResource(false);

                byte[] metaDataBuffer = System.Text.Encoding.UTF8.GetBytes(metaData.ToString(Formatting.None));
                resource.Serialize(referenceID, MetaDataType.JSON, metaDataBuffer, serializedBundle);

                serializedAssets.AddResource(referenceID, PCFResourceType.INTERNALBUNDLE, resource);
            }
        }
        public override void Serialize(SerializedAssets serializedAssets, object[] serializeOptions, NodeBase objNode, List <Action <NodeBase> > postSerializeActions)
        {
            //Make sure this is always 36 characters long.
            this.referenceID = this.rootNode.GenerateID();

            ComponentNode componentNode = new ComponentNode(PCFResourceType.MESH, referenceID, null);

            //Component nodes must always be parented to objNodes.
            objNode.AddChildNode(componentNode);

            //Serialize mesh into a byte array.
            byte[] bytes = MeshSerializeUtilities.WriteMesh(this.meshFilter.sharedMesh, true, false);

            //Serialize Material
            UnitySerializeMaterial materialSerializer = new UnitySerializeMaterial(this.meshRenderer.sharedMaterial, this.rootNode);

            materialSerializer.Serialize(serializedAssets, serializeOptions, componentNode, postSerializeActions);

            //Make sure mesh knows that material it needs.
            JObject metaData = new JObject();

            metaData["materialID"] = materialSerializer.GetPointerID();

            //Create serialized asset by converting data to a bytearray and give it to the constructor.
            AssetResource resource = new AssetResource(false);

            byte[] metaDataBuffer = System.Text.Encoding.UTF8.GetBytes(metaData.ToString(Formatting.None));
            resource.Serialize(referenceID, MetaDataType.JSON, metaDataBuffer, bytes);

            serializedAssets.AddResource(referenceID, PCFResourceType.MESH, resource);

            //Nodes store their resource when serializing
            componentNode.SetSerializer(this);
        }
Пример #10
0
        public override void Deserialize(Dictionary <PCFResourceType, DataBlockBase> dataBlocks,
                                         UnityNodeBase parentNode,
                                         ResourceResponse resourceResponse,
                                         List <Action <UnityNodeBase> > postInstallActions,
                                         bool optimizedLoad)
        {
            if (!this.isDeserialized)
            {
                this.camera = parentNode.GetGameObject().AddComponent <Camera>();

                ResourceBlock dataBlock = dataBlocks[resourceType] as ResourceBlock;
                AssetResource resource  = dataBlock.GetResource(this.referenceID);

                byte[] bytes = resource.GetResourceData();

                int     count = (bytes.Length / 4);
                float[] data  = new float[count];

                for (int i = 0; i < count; i++)
                {
                    data[i] = BitConverter.ToSingle(bytes, (i * 4));
                }

                camera.backgroundColor = new Color(data[0], data[1], data[2], data[3]);
                camera.fieldOfView     = data[4];
                camera.aspect          = data[5];

                this.isDeserialized = true;
            }
        }
Пример #11
0
 public void ReplaceResource(UInt32 referenceID, AssetResource newResource)
 {
     if (this.resourceDatabase.ContainsKey(referenceID))
     {
         this.resourceDatabase.Remove(referenceID);
         this.resourceDatabase.Add(referenceID, newResource);
     }
 }
        public override void Deserialize(Dictionary <PCFResourceType, DataBlockBase> dataBlocks,
                                         UnityNodeBase parentNode,
                                         ResourceResponse resourceResponse,
                                         List <Action <UnityNodeBase> > postInstallActions,
                                         bool optimizedLoad)
        {
            if (!this.isDeserialized)
            {
                ResourceBlock dataBlock = dataBlocks[resourceType] as ResourceBlock;
                AssetResource resource  = dataBlock.GetResource(this.referenceID);

                if (resource != null)
                {
                    byte[] metaDataBuffer = resource.GetMetaData();
                    SerializedCollectionData collectionData = ProtocolBufferSerializer.DeserializeCollectionData(metaDataBuffer);
                    string assemblyName   = ProtocolBufferSerializer.GetAssemblyName(collectionData.assemblyType);
                    string scriptTypeName = collectionData.typeName;
                    string fieldName      = collectionData.fieldName;
                    int    itemCount      = collectionData.count;
                    Type   scriptType     = null;

                    //Qualify type check with assembly name, GetType only looks in current assembly otherwise.
                    if (!string.IsNullOrEmpty(assemblyName))
                    {
                        scriptType = Type.GetType(scriptTypeName + ", " + assemblyName);
                    }
                    else
                    {
                        scriptType = Type.GetType(scriptTypeName);
                    }

                    if (scriptType != null)
                    {
                        Array array = Array.CreateInstance(scriptType, itemCount);

                        if (array != null)
                        {
                            FieldDeserializer arrayDeserializer = new FieldDeserializer(array);
                            ResourceResponse  response          = new ResourceResponse();
                            response.SetFieldDeserializer(arrayDeserializer);

                            foreach (UnityNodeBase node in this.ChildNodes)
                            {
                                node.Deserialize(dataBlocks, this, response, postInstallActions, optimizedLoad);
                                arrayDeserializer.IncrementIndex();
                            }
                        }

                        if (resourceResponse != null)
                        {
                            resourceResponse.GetFieldDeserializer.SetField(fieldName, array);
                        }
                    }
                }

                this.isDeserialized = true;
            }
        }
        public override void Serialize(SerializedAssets serializedAssets, object[] serializeOptions, NodeBase objNode, List <Action <NodeBase> > postSerializeActions)
        {
            //Make sure this is always 36 characters long.
            this.referenceID = this.rootNode.GenerateID();

            ComponentNode componentNode = new ComponentNode(PCFResourceType.SKINNEDMESH, referenceID, null);

            //Component nodes must always be parented to objNodes.
            objNode.AddChildNode(componentNode);

            byte[] bytes = MeshSerializeUtilities.WriteMesh(this.skinnedMesh.sharedMesh, true, true);

            //Serialize Material
            UnitySerializeMaterial materialSerializer = new UnitySerializeMaterial(this.skinnedMesh.sharedMaterial, this.rootNode);

            materialSerializer.Serialize(serializedAssets, serializeOptions, componentNode, postSerializeActions);

            //Make sure mesh knows that material it needs.
            JObject metaData = new JObject();

            metaData["materialID"] = materialSerializer.GetPointerID();
            metaData["rootBone"]   = skinnedMesh.rootBone.name;

            if (skinnedMesh.probeAnchor != null)
            {
                metaData["probeAnchor"] = skinnedMesh.probeAnchor.name;
            }

            metaData["quality"] = (int)skinnedMesh.quality;

            JArray bones = new JArray();

            for (int i = 0; i < skinnedMesh.bones.Length; i++)
            {
                bones.Add(skinnedMesh.bones[i].name);
            }
            metaData["bones"] = bones;

            JArray blendShapeWeights = new JArray();

            for (int i = 0; i < skinnedMesh.sharedMesh.blendShapeCount; i++)
            {
                blendShapeWeights.Add(skinnedMesh.GetBlendShapeWeight(i));
            }
            metaData["blendShapeWeights"] = blendShapeWeights;

            //Create serialized asset by converting data to a bytearray and give it to the constructor.
            AssetResource resource = new AssetResource(false);

            byte[] metaDataBuffer = System.Text.Encoding.UTF8.GetBytes(metaData.ToString(Formatting.None));
            resource.Serialize(referenceID, MetaDataType.JSON, metaDataBuffer, bytes);

            serializedAssets.AddResource(referenceID, PCFResourceType.SKINNEDMESH, resource);

            //Nodes store their resource when serializing
            componentNode.SetSerializer(this);
        }
Пример #14
0
        public override void Deserialize(Dictionary <PCFResourceType, DataBlockBase> dataBlocks,
                                         UnityNodeBase parentNode,
                                         ResourceResponse resourceResponse,
                                         List <Action <UnityNodeBase> > postInstallActions,
                                         bool optimizedLoad)
        {
            if (!this.isDeserialized)
            {
                ResourceBlock dataBlock = dataBlocks[resourceType] as ResourceBlock;
                AssetResource resource  = dataBlock.GetResource(this.referenceID);

                byte[] bytes = resource.GetResourceData();

                int       vectorCount = (bytes.Length / 12);
                Vector3[] vectors     = new Vector3[vectorCount];

                for (int i = 0; i < vectorCount; i++)
                {
                    vectors[i] = new Vector3(
                        BitConverter.ToSingle(bytes, (i * 12)),
                        BitConverter.ToSingle(bytes, (i * 12) + 4),
                        BitConverter.ToSingle(bytes, (i * 12) + 8)
                        );
                }

                GameObject parentGO = parentNode.GetGameObject();

                //Gameobject creates a transform itself when its created, we only need to get the component.
                if (parentGO != null)
                {
                    this.transform = parentGO.GetComponent <Transform>();
                }

                if (this.transform != null)
                {
                    //Get the node above the parentNode and parent to it.
                    Transform parentTransform = parentNode.ParentNode.GetTransform();

                    //Parent this transform to the object above it, if one exists.
                    if (parentTransform != null)
                    {
                        this.transform.SetParent(parentTransform);
                    }

                    //Set the attributes after we have parented the transform.
                    this.transform.localPosition = vectors[0];
                    this.transform.localRotation = Quaternion.Euler(vectors[1]);
                    this.transform.localScale    = vectors[2];
                }

                this.isDeserialized = true;
            }
        }
        public override void Serialize(SerializedAssets serializedAssets, object[] serializeOptions, NodeBase objNode, List <Action <NodeBase> > postserializeActions)
        {
            if (this.audioClip == null)
            {
                Debug.Log("Missing audio clip! If this is intentional ignore this message.");
                return;
            }

                        #if UNITY_EDITOR
            //Make sure this is always 36 characters long.
            this.referenceID = this.rootNode.GenerateID();

            AudioSerializeOpts serializeOption = null;
            for (int i = 0; i < serializeOptions.Length; i++)
            {
                object opt = serializeOptions[i];

                if (opt is AudioSerializeOpts)
                {
                    serializeOption = opt as AudioSerializeOpts;
                    break;
                }
            }

            if (serializeOption == null)
            {
                return;
            }

            ComponentNode scriptNode = new ComponentNode(PCFResourceType.AUDIO, referenceID, null, typeof(UnityEngine.AudioClip).Name.ToString());

            objNode.AddChildNode(scriptNode);

            bool   streamed  = true;
            byte[] audioData = serializeOption.PackageAudio(audioClip, serializedAssets, this.referenceID, ref streamed);

            JObject metaData = new JObject();
            metaData["fieldName"]  = this.fieldName;
            metaData["arrayItem"]  = this.arrayItem;
            metaData["name"]       = this.audioClip.name;
            metaData["sampleRate"] = this.audioClip.frequency;
            metaData["channels"]   = this.audioClip.channels;
            metaData["samples"]    = this.audioClip.samples * this.audioClip.channels;
            metaData["streamed"]   = streamed;

            byte[] metaDataBuffer = System.Text.Encoding.UTF8.GetBytes(metaData.ToString(Formatting.None));

            AssetResource resource = new AssetResource(true);
            resource.Serialize(referenceID, MetaDataType.JSON, metaDataBuffer, audioData);

            serializedAssets.AddResource(referenceID, PCFResourceType.AUDIO, resource);
                        #endif
        }
        public override void Serialize(SerializedAssets serializedAssets, object[] serializeOptions, NodeBase objNode, List <Action <NodeBase> > postSerializeActions)
        {
                        #if UNITY_EDITOR
            //Make sure this is always 36 characters long.
            this.referenceID = this.rootNode.GenerateID();

            ComponentNode componentNode = new ComponentNode(PCFResourceType.TEXTURE, referenceID, null);

            //Component nodes must always be parented to objNodes.
            objNode.AddChildNode(componentNode);

            if (this.texture != null)
            {
                TextureSerializeOpts serializeOption = null;
                for (int i = 0; i < serializeOptions.Length; i++)
                {
                    object opt = serializeOptions[i];

                    if (opt is TextureSerializeOpts)
                    {
                        serializeOption = opt as TextureSerializeOpts;
                        break;
                    }
                }

                if (serializeOption == null)
                {
                    return;
                }

                //Create serialized asset by converting data to a bytearray and give it to the constructor.
                AssetResource resource = new AssetResource(false);

                TextureDataFormat format      = TextureDataFormat.Empty;
                byte[]            textureData = serializeOption.PackageTexture(this.texture, serializedAssets, referenceID, ref format);

                JObject metaData = new JObject();
                metaData["width"]         = this.texture.width;
                metaData["height"]        = this.texture.height;
                metaData["textureFormat"] = (int)format;

                byte[] metaDataBuffer = System.Text.Encoding.UTF8.GetBytes(metaData.ToString(Formatting.None));

                resource.Serialize(referenceID, MetaDataType.JSON, metaDataBuffer, textureData);

                serializedAssets.AddResource(referenceID, PCFResourceType.TEXTURE, resource);

                //Nodes store their resource when serializing
                componentNode.SetSerializer(this);
            }
                        #endif
        }
Пример #17
0
        public void AddResource(UInt32 referenceID, AssetResource resource)
        {
            if (this.resourceDatabase == null)
            {
                this.resourceDatabase = new Dictionary <UInt32, AssetResource>();
            }

            this.resourceDatabase.Add(referenceID, resource);

            //Tracks how many resources is added to this block.
            //that way we can anticipate collection size when deserializing. (Optimization)
            this.objectCount++;
        }
Пример #18
0
        public void AddResource(uint refID, PCFResourceType resourceType, AssetResource resource)
        {
            if (this.dataBlocks.ContainsKey(resourceType))
            {
                ResourceBlock dataBlock = this.dataBlocks[resourceType] as ResourceBlock;
                dataBlock.AddResource(refID, resource);
            }
            else
            {
                ResourceBlock block = new ResourceBlock(resourceType);
                block.AddResource(refID, resource);

                this.dataBlocks.Add(resourceType, block);
            }
        }
Пример #19
0
        public override void Serialize(SerializedAssets serializedAssets, object[] serializeOptions, NodeBase objNode, List <Action <NodeBase> > postSerializeActions)
        {
            //Make sure this is always 36 characters long.
            this.referenceID = this.rootNode.GenerateID();

            byte[] bytes = null;

            if (this.type == typeof(string))
            {
                if (this.value != null)
                {
                    bytes = System.Text.Encoding.UTF8.GetBytes((string)this.value);
                }
            }
            else if (this.type == typeof(int))
            {
                bytes = BitConverter.GetBytes((int)this.value);
            }
            else if (this.type == typeof(float))
            {
                bytes = BitConverter.GetBytes((float)this.value);
            }
            else if (this.type == typeof(double))
            {
                bytes = BitConverter.GetBytes((double)this.value);
            }
            else if (this.type == typeof(bool))
            {
                bytes = BitConverter.GetBytes((bool)this.value);
            }
            else if (this.type == typeof(byte[]))
            {
                bytes = (byte[])this.value;
            }

            if (bytes != null)
            {
                ComponentNode componentNode = new ComponentNode(PCFResourceType.PRIMITIVE, referenceID, null, this.type.Name.ToString());
                objNode.AddChildNode(componentNode);

                AssetResource resource = new AssetResource(false);

                byte[] metaDataBuffer = ProtocolBufferSerializer.SerializeFieldData(this.type, this.fieldName, this.arrayItem, this.type.Assembly);
                resource.Serialize(this.referenceID, MetaDataType.PROTOBUF, metaDataBuffer, bytes);

                serializedAssets.AddResource(referenceID, PCFResourceType.PRIMITIVE, resource);
            }
        }
Пример #20
0
        public override void Serialize(SerializedAssets serializedAssets, object[] serializeOptions, NodeBase objNode, List <Action <NodeBase> > postSerializeActions)
        {
            //Make sure this is always 36 characters long.
            this.referenceID = this.rootNode.GenerateID();

            //If the datatype is not a primitive look for a class specific serializer to invoke.
            Assembly      assembly       = Assembly.GetAssembly(this.GetType());
            string        namespaceName  = this.GetType().Namespace;
            string        serializerName = namespaceName + "." + this.type.Name + "Serialization";
            Type          serializerType = assembly.GetType(serializerName, false);
            List <UInt32> collectionIDs  = new List <UInt32>();

            //See if there is a custom serializer defined for this type.
            if (serializerType != null)
            {
                ComponentNode componentNode = new ComponentNode(PCFResourceType.POINTERCOLLECTION, referenceID, null, this.type.Name.ToString());
                objNode.AddChildNode(componentNode);

                for (int i = 0; i < this.values.Length; i++)
                {
                    System.Object obj = this.values[i];

                    object     serializerClass = Activator.CreateInstance(serializerType, Convert.ChangeType(obj, this.type), "", true, this.rootNode);
                    MethodInfo serializeMethod = serializerType.GetMethod("Serialize");
                    MethodInfo pointerIDMethod = serializerType.GetMethod("GetPointerID");

                    object[] parameters = new object[4];
                    parameters[0] = serializedAssets;
                    parameters[1] = serializeOptions;
                    parameters[2] = componentNode;
                    parameters[3] = postSerializeActions;

                    serializeMethod.Invoke(serializerClass, parameters);

                    UInt32 refID = (UInt32)pointerIDMethod.Invoke(serializerClass, null);

                    collectionIDs.Add(refID);
                }

                AssetResource resource = new AssetResource(false);

                byte[] metaDataBuffer = ProtocolBufferSerializer.SerializeCollectionData(this.type, this.fieldName, collectionIDs.Count, collectionIDs.ToArray(), this.type.Assembly);

                resource.Serialize(this.referenceID, MetaDataType.PROTOBUF, metaDataBuffer, null);

                serializedAssets.AddResource(referenceID, PCFResourceType.POINTERCOLLECTION, resource);
            }
        }
Пример #21
0
        public override void Deserialize(Dictionary <PCFResourceType, DataBlockBase> dataBlocks,
                                         UnityNodeBase parentNode,
                                         ResourceResponse resourceResponse,
                                         List <Action <UnityNodeBase> > postInstallActions,
                                         bool optimizedLoad)
        {
            if (!this.isDeserialized)
            {
                GameObject parentGameObject = parentNode.GetGameObject();

                this.meshFilter   = parentGameObject.AddComponent <MeshFilter>();
                this.meshRenderer = parentGameObject.AddComponent <MeshRenderer>();

                ResourceBlock dataBlock = dataBlocks[resourceType] as ResourceBlock;
                AssetResource resource  = dataBlock.GetResource(this.referenceID);

                byte[]  metaDataBuffer = resource.GetMetaData();
                JObject metaData       = JObject.Parse(System.Text.Encoding.UTF8.GetString(metaDataBuffer));
                UInt32  materialID     = metaData.Value <UInt32>("materialID");

                this.mesh = MeshSerializeUtilities.ReadMesh(resource.GetResourceData(), false);
                this.meshFilter.sharedMesh = this.mesh;

                if (optimizedLoad)
                {
                    //Free up cached ram memory for this mesh, disables access to mesh components like verts etc.
                    this.mesh.UploadMeshData(true);
                }

                for (int i = 0; i < this.ChildNodes.Count; i++)
                {
                    UnityNodeBase child = this.ChildNodes[i];

                    //Create a request to be send down the chain of children, see if any child will handle it.
                    ResourceResponse materialRequest = new ResourceResponse(materialID, (ResourceResponse response) => {
                        meshRenderer.material = response.GetMaterialRequest;
                    });

                    child.Deserialize(dataBlocks, this, materialRequest, postInstallActions, optimizedLoad);
                }

                this.isDeserialized = true;
            }
        }
Пример #22
0
        public override void Deserialize(Dictionary <PCFResourceType, DataBlockBase> dataBlocks,
                                         UnityNodeBase parentNode,
                                         ResourceResponse resourceResponse,
                                         List <Action <UnityNodeBase> > postInstallActions,
                                         bool optimizedLoad)
        {
            if (!this.isDeserialized)
            {
                ResourceBlock dataBlock = dataBlocks[resourceType] as ResourceBlock;
                AssetResource resource  = dataBlock.GetResource(this.referenceID);

                byte[]  metaDataBuffer = resource.GetMetaData();
                JObject metaData       = JObject.Parse(System.Text.Encoding.UTF8.GetString(metaDataBuffer));

                string scriptName = metaData["scriptname"].ToString();
                Type   scriptType = null;

                if (scriptMask == null)
                {
                    scriptType = Type.GetType(scriptName);
                }
                else if (this.scriptMask != null && this.scriptMask.Contains(scriptName))
                {
                    scriptType = Type.GetType(scriptName);
                }

                if (scriptType != null)
                {
                    this.script = parentNode.GetGameObject().AddComponent(scriptType) as MonoBehaviour;
                    FieldDeserializer fieldDeserializer = new FieldDeserializer(scriptType.GetFields(), this.script);

                    ResourceResponse response = new ResourceResponse();
                    response.SetFieldDeserializer(fieldDeserializer);

                    foreach (UnityNodeBase node in this.ChildNodes)
                    {
                        node.Deserialize(dataBlocks, this, response, postInstallActions, optimizedLoad);
                    }
                }

                this.isDeserialized = true;
            }
        }
Пример #23
0
        public override void Serialize(SerializedAssets serializedAssets, object[] serializeOptions, NodeBase objNode, List <Action <NodeBase> > postserializeActions)
        {
            //Make sure this is always 36 characters long.
            this.referenceID = this.rootNode.GenerateID();

            ComponentNode scriptNode = new ComponentNode(PCFResourceType.GRADIENT, referenceID, null, typeof(UnityEngine.Gradient).Name.ToString());

            objNode.AddChildNode(scriptNode);

            GradientColorKey[] colorKeys          = this.gradient.colorKeys;
            System.Object[]    convertedColorKeys = new System.Object[colorKeys.Length];

            for (int i = 0; i < colorKeys.Length; i++)
            {
                GradientColorKey colorKey = colorKeys[i];
                convertedColorKeys[i] = colorKey as System.Object;
            }

            UnitySerializeCollection colorKeySerializer = new UnitySerializeCollection(convertedColorKeys, typeof(UnityEngine.GradientColorKey), null, "colorKeys", rootNode);

            colorKeySerializer.Serialize(serializedAssets, serializeOptions, scriptNode, postserializeActions);


            GradientAlphaKey[] alphaKeys          = this.gradient.alphaKeys;
            System.Object[]    convertedAlphaKeys = new System.Object[alphaKeys.Length];

            for (int i = 0; i < alphaKeys.Length; i++)
            {
                GradientAlphaKey alphaKey = alphaKeys[i];
                convertedAlphaKeys[i] = alphaKey as System.Object;
            }

            UnitySerializeCollection alphaKeySerializer = new UnitySerializeCollection(convertedAlphaKeys, typeof(UnityEngine.GradientAlphaKey), null, "alphaKeys", rootNode);

            alphaKeySerializer.Serialize(serializedAssets, serializeOptions, scriptNode, postserializeActions);

            AssetResource resource = new AssetResource(false);

            byte[] metaDataBuffer = ProtocolBufferSerializer.SerializeFieldData(this.gradient.GetType(), this.fieldName, this.arrayItem, this.gradient.GetType().Assembly);
            resource.Serialize(this.referenceID, MetaDataType.PROTOBUF, metaDataBuffer, null);

            serializedAssets.AddResource(referenceID, PCFResourceType.GRADIENT, resource);
        }
        public override void Deserialize(Dictionary <PCFResourceType, DataBlockBase> dataBlocks,
                                         UnityNodeBase parentNode,
                                         ResourceResponse resourceResponse,
                                         List <Action <UnityNodeBase> > postInstallActions,
                                         bool optimizedLoad)
        {
            if (!this.isDeserialized)
            {
                ResourceBlock dataBlock = dataBlocks[resourceType] as ResourceBlock;
                AssetResource resource  = dataBlock.GetResource(this.referenceID);

                string  jsonString = System.Text.Encoding.UTF8.GetString(resource.GetMetaData());
                JObject jsonObject = JObject.Parse(jsonString);

                animationClip           = new AnimationClip();
                animationClip.name      = jsonObject.Value <string>("name");
                animationClip.frameRate = jsonObject.Value <float>("frameRate");
                animationClip.wrapMode  = (UnityEngine.WrapMode)jsonObject.Value <int>("wrapMode");
                animationClip.legacy    = true;

                SerializedAnimationClip serializedAnimationClip = ProtocolBufferSerializer.DeserializeAnimationClipData(resource.GetResourceData());

                foreach (int key in serializedAnimationClip.AnimationChannels.Keys)
                {
                    SerializedAnimationChannelName channel = (SerializedAnimationChannelName)key;

                    SerializedAnimationKeyFrame[] keyFrames = serializedAnimationClip.GetChannel(channel);

                    AnimationCurve curve = CreateAnimationCurve(serializedAnimationClip.PostWrapMode, serializedAnimationClip.PreWrapMode, keyFrames);

                    animationClip.SetCurve("", typeof(Transform), AnimationClipUtils.GetAnimationClipChannelName(channel), curve);
                }

                string fieldName = jsonObject.Value <string>("fieldName");
                if (resourceResponse != null)
                {
                    resourceResponse.GetFieldDeserializer.SetField(fieldName, animationClip);
                }

                this.isDeserialized = true;
            }
        }
        public override void Serialize(SerializedAssets serializedAssets, object[] serializeOptions, NodeBase objNode, List <Action <NodeBase> > postSerializeActions)
        {
            //Make sure this is always 36 characters long.
            this.referenceID = this.rootNode.GenerateID();

            ComponentNode componentNode = new ComponentNode(PCFResourceType.COLLIDER, referenceID, null);

            //Material nodes can and are most likely to be children of other component nodes.
            objNode.AddChildNode(componentNode);

            //Create serialized asset by converting data to a bytearray and give it to the constructor.
            AssetResource resource = new AssetResource(false);

            resource.Serialize(referenceID, MetaDataType.UNKOWN, null, null);

            serializedAssets.AddResource(referenceID, PCFResourceType.COLLIDER, resource);

            //Nodes store their resource when serializing
            componentNode.SetSerializer(this);
        }
Пример #26
0
        public override void Serialize(SerializedAssets serializedAssets, object[] serializeOptions, NodeBase objNode, List <Action <NodeBase> > postSerializeActions)
        {
            //Make sure this is always 36 characters long.
            this.referenceID = this.rootNode.GenerateID();

            AvatarSerializeOpts serializeOption = null;

            for (int i = 0; i < serializeOptions.Length; i++)
            {
                object opt = serializeOptions[i];

                if (opt is AvatarSerializeOpts)
                {
                    serializeOption = opt as AvatarSerializeOpts;
                    break;
                }
            }

            if (serializeOption == null)
            {
                return;
            }

            ComponentNode componentNode = new ComponentNode(PCFResourceType.AVATAR, referenceID, null);

            //Component nodes must always be parented to objNodes.
            objNode.AddChildNode(componentNode);

            JObject metaData = serializeOption.SerializeAvatar(this.parentGO, this.animator);

            //Create serialized asset by converting data to a bytearray and give it to the constructor.
            AssetResource resource = new AssetResource(false);

            byte[] metaDataBuffer = System.Text.Encoding.UTF8.GetBytes(metaData.ToString(Formatting.None));
            resource.Serialize(referenceID, MetaDataType.JSON, metaDataBuffer, null);

            serializedAssets.AddResource(referenceID, PCFResourceType.AVATAR, resource);

            //Nodes store their resource when serializing
            componentNode.SetSerializer(this);
        }
        public override void Serialize(SerializedAssets serializedAssets, object[] serializeOptions, NodeBase objNode, List <Action <NodeBase> > postserializeActions)
        {
            this.referenceID = this.rootNode.GenerateID();

            ComponentNode componentNode = new ComponentNode(PCFResourceType.TRANSFORMPOINTER, referenceID, null, this.transform.GetType().Name.ToString());

            objNode.AddChildNode(componentNode);

            //Find the node this node points to in the tree.
            NodeBase transformNode = FindTransformInTree(rootNode, this.transform.name);
            UInt32   referencedID  = transformNode.GetReferenceID();

            AssetResource resource = new AssetResource(false);

            byte[] metaDataBuffer = ProtocolBufferSerializer.SerializeFieldData(this.transform.GetType(), this.fieldName, this.arrayItem, this.transform.GetType().Assembly);
            resource.Serialize(this.referenceID, MetaDataType.PROTOBUF, metaDataBuffer, BitConverter.GetBytes(referencedID));

            serializedAssets.AddResource(referenceID, PCFResourceType.TRANSFORMPOINTER, resource);

            this.pointedID = transformNode.GetReferenceID();
        }
        public override void Deserialize(Dictionary <PCFResourceType, DataBlockBase> dataBlocks,
                                         UnityNodeBase parentNode,
                                         ResourceResponse resourceResponse,
                                         List <Action <UnityNodeBase> > postInstallActions,
                                         bool optimizedLoad)
        {
            if (!this.isDeserialized)
            {
                ResourceBlock dataBlock = dataBlocks[this.resourceType] as ResourceBlock;
                AssetResource resource  = dataBlock.GetResource(this.referenceID);

                ResourceResponse request = resourceResponse.CanHandle(GetReferenceID());
                if (request != null)
                {
                    byte[] bundleBytes = resource.GetResourceData();
                    this.internalBundle = AssetBundle.LoadFromMemory(bundleBytes);

                    request.HandleAssetBundleResponse(this.internalBundle);
                }

                this.isDeserialized = true;
            }
        }
        public override void Deserialize(Dictionary <PCFResourceType, DataBlockBase> dataBlocks,
                                         UnityNodeBase parentNode,
                                         ResourceResponse resourceResponse,
                                         List <Action <UnityNodeBase> > postInstallActions,
                                         bool optimizedLoad)
        {
            if (!this.isDeserialized)
            {
                //Deserialize pointed node id.
                ResourceBlock dataBlock = dataBlocks[resourceType] as ResourceBlock;
                AssetResource resource  = dataBlock.GetResource(this.referenceID);

                byte[] bytes = resource.GetResourceData();

                UInt32 referencedNodeID = BitConverter.ToUInt32(bytes, 0);
                //JObject metaData = JObject.Parse(System.Text.Encoding.UTF8.GetString(metaDataBuffer));

                //UInt32 referencedNodeID = metaData.Value<UInt32>("targetReferenceID");

                Dictionary <UInt32, UnityNodeBase> referencedNodes = resourceResponse.GetReferencedNodes;

                if (referencedNodes.ContainsKey(referencedNodeID))
                {
                    UnityNodeBase referencedNode = referencedNodes[referencedNodeID];

                    Transform transform = referencedNode.GetTransform();

                    if (transform != null)
                    {
                        resourceResponse.GetFieldDeserializer.SetArrayItem(transform);
                    }
                }

                this.isDeserialized = true;
            }
        }
Пример #30
0
        public override void Deserialize(Dictionary <PCFResourceType, DataBlockBase> dataBlocks,
                                         UnityNodeBase parentNode,
                                         ResourceResponse resourceResponse,
                                         List <Action <UnityNodeBase> > postInstallActions,
                                         bool optimizedLoad)
        {
            if (!this.isDeserialized)
            {
                ResourceBlock dataBlock = dataBlocks[resourceType] as ResourceBlock;
                AssetResource resource  = dataBlock.GetResource(this.referenceID);

                UInt32 pointedNodeID = BitConverter.ToUInt32(resource.GetResourceData(), 0);

                postInstallActions.Add((UnityNodeBase rootNode) => {
                    UnityNodeBase referencedNode = FindNodeWithID(rootNode, pointedNodeID);

                    if (referencedNode is UnityAnimationClipNode)
                    {
                        UnityAnimationClipNode animationClip = referencedNode as UnityAnimationClipNode;

                        AnimationClip animation = animationClip.GetAnimationClip();

                        string jsonString  = System.Text.Encoding.UTF8.GetString(resource.GetMetaData());
                        JObject jsonObject = JObject.Parse(jsonString);

                        string fieldName = jsonObject.Value <string>("fieldName");
                        if (resourceResponse != null)
                        {
                            resourceResponse.GetFieldDeserializer.SetField(fieldName, animation);
                        }
                    }
                });

                this.isDeserialized = true;
            }
        }