Beispiel #1
0
        void SerializeField(SerializedAssets serializedAssets, object[] serializeOptions, NodeBase scriptNode, List <Action <NodeBase> > postSerializeActions, FieldInfo field)
        {
            Type fieldType = field.FieldType;

            //Serialize public primitive members (yes we know, string is a not actually a primitive, but we pretend it is...)
            if (fieldType.IsPrimitive || fieldType == typeof(string))
            {
                UnitySerializerBase primitiveSerializer = new UnitySerializePrimitive(field.GetValue(this.parentObject), fieldType, field.Name, false, this.rootNode);
                primitiveSerializer.Serialize(serializedAssets, serializeOptions, scriptNode, postSerializeActions);
            }
            else if (typeof(IList).IsAssignableFrom(field.FieldType))
            {
                //Convoluted way to make sure we have an array of System.Object.
                IEnumerable          collection       = field.GetValue(this.parentObject) as IEnumerable;
                List <System.Object> intermediateList = new List <System.Object>();

                foreach (System.Object item in collection)
                {
                    intermediateList.Add(item);
                }

                System.Object[] values = intermediateList.ToArray();

                if (values != null && values.Length > 0)
                {
                    UnitySerializerBase collectionSerializer = new UnitySerializeCollection(values, values[0].GetType(), parentObject, field.Name, rootNode);
                    collectionSerializer.Serialize(serializedAssets, serializeOptions, scriptNode, postSerializeActions);
                }
            }
            else if (fieldType.IsClass || fieldType.IsLayoutSequential)
            {
                //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 + "." + fieldType.Name + "Serialization";
                Type     serializerType = assembly.GetType(serializerName, false);

                System.Object val = field.GetValue(this.parentObject);

                if (serializerType != null)
                {
                    object     serializerClass = Activator.CreateInstance(serializerType, Convert.ChangeType(val, field.FieldType), field.Name, false, this.rootNode);
                    MethodInfo serializeMethod = serializerType.GetMethod("Serialize");

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

                    serializeMethod.Invoke(serializerClass, parameters);
                }
                else
                {
                    UnitySerializerBase classSerialize = new UnitySerializeClass(field.GetValue(this.parentObject), fieldType, field.Name, false, this.rootNode);
                    classSerialize.Serialize(serializedAssets, serializeOptions, scriptNode, postSerializeActions);
                }
            }
        }
        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.SCRIPT, referenceID, null, this.script.GetType().Name.ToString());

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

            JObject metaData = new JObject();

            metaData["scriptname"] = this.script.GetType().ToString();

            //Get fields using reflection
            FieldInfo[] fields = script.GetType().GetFields();

            for (int i = 0; i < fields.Length; i++)
            {
                if (fields[i].IsPublic && !fields[i].IsStatic)
                {
                    FieldInfo currentField = fields[i];
                    Type      valueType    = currentField.FieldType;

                    //Find matching deserializer using reflection and field type as string or similar.
                    if (valueType.IsPrimitive || valueType == typeof(string))
                    {
                        UnitySerializerBase primitiveSerializer = new UnitySerializePrimitive(currentField.GetValue(script), valueType, currentField.Name, false, this.rootNode);
                        primitiveSerializer.Serialize(serializedAssets, serializeOptions, scriptNode, postSerializeActions);
                    }
                    else if (typeof(IList).IsAssignableFrom(fields[i].FieldType))
                    {
                        //Convoluted way to make sure we have an array of System.Object.
                        IEnumerable          collection       = currentField.GetValue(script) as IEnumerable;
                        List <System.Object> intermediateList = new List <System.Object>();

                        foreach (System.Object item in collection)
                        {
                            intermediateList.Add(item);
                        }

                        System.Object[] values = intermediateList.ToArray();

                        if (values != null && values.Length > 0)
                        {
                            UnitySerializerBase collectionSerializer = new UnitySerializeCollection(values, values[0].GetType(), script, currentField.Name, rootNode);
                            collectionSerializer.Serialize(serializedAssets, serializeOptions, scriptNode, postSerializeActions);
                        }
                    }
                    else if (valueType.IsClass || valueType.IsLayoutSequential)
                    {
                        //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 + "." + valueType.Name + "Serialization";
                        Type          serializerType = assembly.GetType(serializerName, false);
                        System.Object val            = fields[i].GetValue(this.script);

                        //If we have implemented a custom serializer for this type we invoke it and let it serialize relevant data.
                        if (serializerType != null)
                        {
                            object     serializerClass = Activator.CreateInstance(serializerType, Convert.ChangeType(val, valueType), currentField.Name, false, this.rootNode);
                            MethodInfo serializeMethod = serializerType.GetMethod("Serialize");

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

                            serializeMethod.Invoke(serializerClass, parameters);
                        }
                        else
                        {
                            //Use generic serializer incase not found.
                            UnitySerializerBase classSerialize = new UnitySerializeClass(currentField.GetValue(this.script), valueType, currentField.Name, false, this.rootNode);
                            classSerialize.Serialize(serializedAssets, serializeOptions, scriptNode, postSerializeActions);
                        }
                    }
                }
            }

            AssetResource resource = new AssetResource(false);

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

            serializedAssets.AddResource(referenceID, PCFResourceType.SCRIPT, 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.COLLECTION, referenceID, null, this.type.Name.ToString());

            objNode.AddChildNode(componentNode);

            List <UInt32> collectionIDs = new List <UInt32>();

            if (this.type.IsPrimitive || this.type == typeof(string))
            {
                //Try to serialize the collection using primitive data serialization.
                for (int i = 0; i < this.values.Length; i++)
                {
                    UnitySerializerBase primitiveSerializer = new UnitySerializePrimitive(this.values[i], this.values[i].GetType(), "", true, this.rootNode);
                    primitiveSerializer.Serialize(serializedAssets, serializeOptions, componentNode, postSerializeActions);

                    collectionIDs.Add(primitiveSerializer.GetReferenceID());
                }
            }
            else
            {
                //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);

                //See if there is a custom serializer defined for this type.
                if (serializerType != null)
                {
                    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 referenceIDMethod = serializerType.GetMethod("GetReferenceID");

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

                        serializeMethod.Invoke(serializerClass, parameters);

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

                        collectionIDs.Add(refID);
                    }
                }
                //Fallback to generic class serializer.
                else
                {
                    for (int i = 0; i < this.values.Length; i++)
                    {
                        System.Object obj = this.values[i];

                        UnitySerializerBase classSerialize = new UnitySerializeClass(obj, this.type, "", true, this.rootNode);
                        classSerialize.Serialize(serializedAssets, serializeOptions, componentNode, postSerializeActions);

                        collectionIDs.Add(classSerialize.GetReferenceID());
                    }
                }
            }

            AssetResource resource = new AssetResource(false);

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

            serializedAssets.AddResource(referenceID, PCFResourceType.COLLECTION, 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.ANIMATION, referenceID, null);

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

            //Get fields using reflection
            PropertyInfo[] properties = animation.GetType().GetProperties();

            for (int i = 0; i < properties.Length; i++)
            {
                if (properties[i].CanWrite)
                {
                    PropertyInfo currentProperty = properties[i];
                    Type         valueType       = currentProperty.PropertyType;

                    //Find matching deserializer using reflection and field type as string or similar.
                    if (valueType.IsPrimitive || valueType == typeof(string))
                    {
                        UnitySerializerBase primitiveSerializer = new UnitySerializePrimitive(currentProperty.GetValue(animation, null), valueType, currentProperty.Name, false, this.rootNode);
                        primitiveSerializer.Serialize(serializedAssets, serializeOptions, componentNode, postSerializeActions);
                    }
                    else if (typeof(IList).IsAssignableFrom(properties[i].PropertyType))
                    {
                        //Convoluted way to make sure we have an array of System.Object.
                        IEnumerable          collection       = currentProperty.GetValue(animation, null) as IEnumerable;
                        List <System.Object> intermediateList = new List <System.Object>();

                        foreach (System.Object item in collection)
                        {
                            intermediateList.Add(item);
                        }

                        System.Object[] values = intermediateList.ToArray();

                        if (values != null && values.Length > 0)
                        {
                            UnitySerializerBase collectionSerializer = new UnitySerializeCollection(values, values[0].GetType(), animation, currentProperty.Name, rootNode);
                            collectionSerializer.Serialize(serializedAssets, serializeOptions, componentNode, postSerializeActions);
                        }
                    }
                    else if (valueType.IsClass || valueType.IsLayoutSequential)
                    {
                        //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 + "." + valueType.Name + "Serialization";
                        Type          serializerType = assembly.GetType(serializerName, false);
                        System.Object val            = properties[i].GetValue(this.animation, null);

                        //If we have implemented a custom serializer for this type we invoke it and let it serialize relevant data.
                        if (serializerType != null)
                        {
                            object     serializerClass = Activator.CreateInstance(serializerType, Convert.ChangeType(val, valueType), currentProperty.Name, false, this.rootNode);
                            MethodInfo serializeMethod = serializerType.GetMethod("Serialize");

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

                            serializeMethod.Invoke(serializerClass, parameters);
                        }
                        else
                        {
                            //Use generic serializer incase not found.
                            UnitySerializerBase classSerialize = new UnitySerializeClass(currentProperty.GetValue(this.animation, null), valueType, currentProperty.Name, false, this.rootNode);
                            classSerialize.Serialize(serializedAssets, serializeOptions, componentNode, postSerializeActions);
                        }
                    }
                }
            }

            //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.ANIMATION, resource);
        }