Example #1
0
    //---------------------------------------------------------------------------------
    //  DeserializeListElement
    //---------------------------------------------------------------------------------
    private static object DeserializeListElement(XMLInStream stream, Type type)
    {
        switch (type.ToString())
        {
        case "System.String":
            string resS;
            stream.Content(out resS);
            return(resS);

        case "System.Single":
            float resF;
            stream.Content(out resF);
            return(resF);

        case "System.Int32":
            int resI;
            stream.Content(out resI);
            return(resI);

        case "System.Boolean":
            bool resB;
            stream.Content(out resB);
            return(resB);

        case "UnityEngine.Vector3":
            Vector3 resV;
            stream.Content(out resV);
            return(resV);

        case "UnityEngine.Quaternion":
            Quaternion resQ;
            stream.Content(out resQ);
            return(resQ);

        case "UnityEngine.Color":
            Color resC;
            stream.Content(out resC);
            return(resC);

        case "UnityEngine.Rect":
            Rect resR;
            stream.Content(out resR);
            return(resR);

        case "UnityEngine.Vector2":
            Vector2 resV2;
            stream.Content(out resV2);
            return(resV2);

        default:
            object created = DeserializeObject(stream, type);
            return(created);
        }
    }
Example #2
0
    //---------------------------------------------------------------------------------
    //  DeserializeObject
    //---------------------------------------------------------------------------------
    private static object DeserializeObject(XMLInStream stream, Type type)
    {
        object data = Activator.CreateInstance(type);

        System.Reflection.FieldInfo[] fieldInfo = type.GetFields();
        foreach (System.Reflection.FieldInfo info in fieldInfo)
        {
            switch (info.FieldType.ToString())
            {
            case "System.String":
                string resS;
                if (stream.Has(info.Name))
                {
                    stream.Content(info.Name, out resS);
                    info.SetValue(data, resS);
                }
                break;

            case "System.Single":
                float resF;
                if (stream.Has(info.Name))
                {
                    stream.Content(info.Name, out resF);
                    info.SetValue(data, resF);
                }
                break;

            case "System.Int32":
                int resI;
                if (stream.Has(info.Name))
                {
                    stream.Content(info.Name, out resI);
                    info.SetValue(data, resI);
                }
                break;

            case "System.Boolean":
                bool resB;
                if (stream.Has(info.Name))
                {
                    stream.Content(info.Name, out resB);
                    info.SetValue(data, resB);
                }
                break;

            case "UnityEngine.Vector3":
                Vector3 resV;
                if (stream.Has(info.Name))
                {
                    stream.Content(info.Name, out resV);
                    info.SetValue(data, resV);
                }
                break;

            case "UnityEngine.Quaternion":
                Quaternion resQ;
                if (stream.Has(info.Name))
                {
                    stream.Content(info.Name, out resQ);
                    info.SetValue(data, resQ);
                }
                break;

            case "UnityEngine.Color":
                Color resC;
                if (stream.Has(info.Name))
                {
                    stream.Content(info.Name, out resC);
                    info.SetValue(data, resC);
                }
                break;

            case "UnityEngine.Rect":
                Rect resR;
                if (stream.Has(info.Name))
                {
                    stream.Content(info.Name, out resR);
                    info.SetValue(data, resR);
                }
                break;

            case "UnityEngine.Vector2":
                Vector2 resV2;
                if (stream.Has(info.Name))
                {
                    stream.Content(info.Name, out resV2);
                    info.SetValue(data, resV2);
                }
                break;

            default:
                if (stream.Has(info.Name))
                {
                    if (info.FieldType.IsEnum) // Enum
                    {
                        string e;
                        stream.Content(info.Name, out e);
                        info.SetValue(data, Enum.Parse(info.FieldType, e));
                    }
                    else if (info.FieldType.IsGenericType) // can only be a List
                    {
                        Type       containedType = info.FieldType.GetGenericArguments()[0];
                        Type       typeList      = typeof(List <>);
                        Type       actualType    = typeList.MakeGenericType(containedType);
                        MethodInfo addMethod     = actualType.GetMethod("Add");
                        object     list          = Activator.CreateInstance(actualType);
                        stream.Start(info.Name).List("item", delegate(XMLInStream stream2){
                            object o = DeserializeListElement(stream2, containedType);
                            addMethod.Invoke(list, new object[] { o });
                        }).End();
                        info.SetValue(data, list);
                    }
                    else if (info.FieldType.IsArray) // Array
                    {
                        Type       containedType = info.FieldType.GetElementType();
                        Type       typeList      = typeof(List <>);
                        Type       actualType    = typeList.MakeGenericType(containedType);
                        MethodInfo addMethod     = actualType.GetMethod("Add");
                        MethodInfo toArrayMethod = actualType.GetMethod("ToArray");
                        object     list          = Activator.CreateInstance(actualType);
                        stream.Start(info.Name).List("item", delegate(XMLInStream stream2){
                            object o = DeserializeListElement(stream2, containedType);
                            addMethod.Invoke(list, new object[] { o });
                        }).End();
                        object array = toArrayMethod.Invoke(list, new object[] {});
                        info.SetValue(data, array);
                    }
                    else // Embedded Object
                    {
                        stream.Start(info.Name);
                        object created = DeserializeObject(stream, info.FieldType);
                        stream.End();
                        info.SetValue(data, created);
                    }
                }
                break;
            }
        }
        return(data);
    }
Example #3
0
	void ExampleAddressBook()
  {
    // Let's serialize a simple address book

    XMLOutStream outStream = new XMLOutStream();
    outStream.Start("book")
              .Content("version", 1)
              .Start("entry")
                .Content("name", "Mike")
                .Content("age", 24)
                .Attribute("already-met", true)
                .Attribute("married", true)
              .End()
              .Start("entry")
                .Content("name", "John")
                .Content("age", 32)
                .Attribute("already-met", false)
              .End()
            .End();

    string serialized = outStream.Serialize();

    // serialized outputs this XML structure:
    //
    //
    //    <book>
    //      <entry already-met="true" married=true>
    //        <name>Mike</name>
    //        <age>24</age>
    //      </entry>
    //      <entry already-met="false">
    //        <name>John</name>
    //        <age>32</age>
    //      </entry>
    //    </book>
    //    
    //    

    // Deserialize it

    XMLInStream inStream = new XMLInStream(serialized); // the XML root (here 'book' is automatically entered to parse the content)
    int version;
    List<AddressBookEntry> entries = new List<AddressBookEntry>();
    inStream.Content("version", out version)
            .List("entry", delegate(XMLInStream entryStream){
      string name;
      int age;
      bool alreadyMet;
      bool married = false;
      entryStream.Content("name", out name)
                 .Content("age", out age)
                 .Attribute("already-met", out alreadyMet)
                 .AttributeOptional("married", ref married);
      entries.Add(new AddressBookEntry(name, age, alreadyMet, married));
    });

    // Now version and entries are set

    Debug.Log("SERIALIZED XML STRING: " + serialized);
    string result = "";
    foreach(AddressBookEntry entry in entries)
      result += entry.ToString() + " ";
    Debug.Log("XML DESERIALIZATION of " + entries.Count + " entries: " + result);
  }
Example #4
0
    void ExampleAddressBook()
    {
        // Let's serialize a simple address book

        XMLOutStream outStream = new XMLOutStream();

        outStream.Start("book")
        .Content("version", 1)
        .Start("entry")
        .Content("name", "Mike")
        .Content("age", 24)
        .Attribute("already-met", true)
        .Attribute("married", true)
        .End()
        .Start("entry")
        .Content("name", "John")
        .Content("age", 32)
        .Attribute("already-met", false)
        .End()
        .End();

        string serialized = outStream.Serialize();

        // serialized outputs this XML structure:
        //
        //
        //    <book>
        //      <entry already-met="true" married=true>
        //        <name>Mike</name>
        //        <age>24</age>
        //      </entry>
        //      <entry already-met="false">
        //        <name>John</name>
        //        <age>32</age>
        //      </entry>
        //    </book>
        //
        //

        // Deserialize it

        XMLInStream             inStream = new XMLInStream(serialized); // the XML root (here 'book' is automatically entered to parse the content)
        int                     version;
        List <AddressBookEntry> entries = new List <AddressBookEntry>();

        inStream.Content("version", out version)
        .List("entry", delegate(XMLInStream entryStream){
            string name;
            int age;
            bool alreadyMet;
            bool married = false;
            entryStream.Content("name", out name)
            .Content("age", out age)
            .Attribute("already-met", out alreadyMet)
            .AttributeOptional("married", ref married);
            entries.Add(new AddressBookEntry(name, age, alreadyMet, married));
        });

        // Now version and entries are set

        Debug.Log("SERIALIZED XML STRING: " + serialized);
        string result = "";

        foreach (AddressBookEntry entry in entries)
        {
            result += entry.ToString() + " ";
        }
        Debug.Log("XML DESERIALIZATION of " + entries.Count + " entries: " + result);
    }
    private static object DeserializeObject(XMLInStream stream, Type type)
    {
        //IL_01ec: Unknown result type (might be due to invalid IL or missing references)
        //IL_021f: Unknown result type (might be due to invalid IL or missing references)
        //IL_0252: Unknown result type (might be due to invalid IL or missing references)
        //IL_0285: Unknown result type (might be due to invalid IL or missing references)
        //IL_02b8: Unknown result type (might be due to invalid IL or missing references)
        object obj = Activator.CreateInstance(type);

        FieldInfo[] fields = type.GetFields();
        FieldInfo[] array  = fields;
        Type        containedType;
        MethodInfo  addMethod;
        object      list;
        Type        containedType2;
        MethodInfo  addMethod2;
        object      list2;

        foreach (FieldInfo fieldInfo in array)
        {
            switch (fieldInfo.FieldType.ToString())
            {
            case "System.String":
                if (stream.Has(fieldInfo.Name))
                {
                    stream.Content(fieldInfo.Name, out string value12);
                    fieldInfo.SetValue(obj, value12);
                }
                break;

            case "System.Single":
                if (stream.Has(fieldInfo.Name))
                {
                    stream.Content(fieldInfo.Name, out float value7);
                    fieldInfo.SetValue(obj, value7);
                }
                break;

            case "System.Int32":
                if (stream.Has(fieldInfo.Name))
                {
                    stream.Content(fieldInfo.Name, out int value9);
                    fieldInfo.SetValue(obj, value9);
                }
                break;

            case "System.Boolean":
                if (stream.Has(fieldInfo.Name))
                {
                    stream.Content(fieldInfo.Name, out bool value5);
                    fieldInfo.SetValue(obj, value5);
                }
                break;

            case "UnityEngine.Vector3":
                if (stream.Has(fieldInfo.Name))
                {
                    stream.Content(fieldInfo.Name, out Vector3 value10);
                    fieldInfo.SetValue(obj, value10);
                }
                break;

            case "UnityEngine.Quaternion":
                if (stream.Has(fieldInfo.Name))
                {
                    stream.Content(fieldInfo.Name, out Quaternion value8);
                    fieldInfo.SetValue(obj, value8);
                }
                break;

            case "UnityEngine.Color":
                if (stream.Has(fieldInfo.Name))
                {
                    stream.Content(fieldInfo.Name, out Color value6);
                    fieldInfo.SetValue(obj, value6);
                }
                break;

            case "UnityEngine.Rect":
                if (stream.Has(fieldInfo.Name))
                {
                    stream.Content(fieldInfo.Name, out Rect value4);
                    fieldInfo.SetValue(obj, value4);
                }
                break;

            case "UnityEngine.Vector2":
                if (stream.Has(fieldInfo.Name))
                {
                    stream.Content(fieldInfo.Name, out Vector2 value11);
                    fieldInfo.SetValue(obj, value11);
                }
                break;

            default:
                if (stream.Has(fieldInfo.Name))
                {
                    if (fieldInfo.FieldType.IsEnum)
                    {
                        stream.Content(fieldInfo.Name, out string value);
                        fieldInfo.SetValue(obj, Enum.Parse(fieldInfo.FieldType, value));
                    }
                    else if (fieldInfo.FieldType.IsGenericType)
                    {
                        containedType = fieldInfo.FieldType.GetGenericArguments()[0];
                        Type typeFromHandle = typeof(List <>);
                        Type type2          = typeFromHandle.MakeGenericType(containedType);
                        addMethod = type2.GetMethod("Add");
                        list      = Activator.CreateInstance(type2);
                        stream.Start(fieldInfo.Name).List("item", delegate(XMLInStream stream2)
                        {
                            object obj3 = DeserializeListElement(stream2, containedType);
                            addMethod.Invoke(list, new object[1]
                            {
                                obj3
                            });
                        }).End();
                        fieldInfo.SetValue(obj, list);
                    }
                    else if (fieldInfo.FieldType.IsArray)
                    {
                        containedType2 = fieldInfo.FieldType.GetElementType();
                        Type typeFromHandle2 = typeof(List <>);
                        Type type3           = typeFromHandle2.MakeGenericType(containedType2);
                        addMethod2 = type3.GetMethod("Add");
                        MethodInfo method = type3.GetMethod("ToArray");
                        list2 = Activator.CreateInstance(type3);
                        stream.Start(fieldInfo.Name).List("item", delegate(XMLInStream stream2)
                        {
                            object obj2 = DeserializeListElement(stream2, containedType2);
                            addMethod2.Invoke(list2, new object[1]
                            {
                                obj2
                            });
                        }).End();
                        object value2 = method.Invoke(list2, new object[0]);
                        fieldInfo.SetValue(obj, value2);
                    }
                    else
                    {
                        stream.Start(fieldInfo.Name);
                        object value3 = DeserializeObject(stream, fieldInfo.FieldType);
                        stream.End();
                        fieldInfo.SetValue(obj, value3);
                    }
                }
                break;
            }
        }
        return(obj);
    }