public static object GetObject(BinaryNode currNode)
    {
        string nodeAttr = GameSerializer.GetNodeAttr(currNode, "NULL");
        object obj      = null;

        if (nodeAttr != null)
        {
            return(obj);
        }
        string nodeAttr2 = GameSerializer.GetNodeAttr(currNode, "Type");
        string nodeAttr3 = GameSerializer.GetNodeAttr(currNode, "V");
        string nodeAttr4 = GameSerializer.GetNodeAttr(currNode, "JT");

        if ("Arr".Equals(nodeAttr4))
        {
            if (nodeAttr2 != null)
            {
                string typeStr = nodeAttr2.Replace("[]", string.Empty);
                Type   type    = GameSerializer.GetType(typeStr);
                if (type == null)
                {
                    Debug.LogError("Array type " + nodeAttr2 + " create failed!");
                    return(null);
                }
                Array array = Array.CreateInstance(type, currNode.GetChildNum());
                for (int i = 0; i < array.get_Length(); i++)
                {
                    array.SetValue(GameSerializer.GetObject(currNode.GetChild(i)), i);
                }
                obj = array;
            }
        }
        else if ("Cus".Equals(nodeAttr4))
        {
            if (nodeAttr2 != null)
            {
                Type type2 = GameSerializer.GetType(nodeAttr2);
                ICustomizedObjectSerializer objectSerlizer = GameSerializer.GetObjectSerlizer(type2);
                if (objectSerlizer != null && objectSerlizer is ICustomInstantiate)
                {
                    obj = ((ICustomInstantiate)objectSerlizer).Instantiate(currNode);
                }
                else
                {
                    obj = GameSerializer.CreateInstance(type2);
                }
                if (obj == null)
                {
                    return(null);
                }
                if (objectSerlizer != null)
                {
                    objectSerlizer.ObjectDeserialize(ref obj, currNode);
                }
            }
        }
        else if ("Enum".Equals(nodeAttr4))
        {
            if (nodeAttr2 != null)
            {
                Type type3 = GameSerializer.GetType(nodeAttr2);
                obj = Enum.ToObject(type3, int.Parse(nodeAttr3));
            }
        }
        else if ("Pri".Equals(nodeAttr4))
        {
            if (nodeAttr2 != null)
            {
                obj = Convert.ChangeType(nodeAttr3, GameSerializer.GetType(nodeAttr2));
            }
        }
        else if ("Ref".Equals(nodeAttr4))
        {
            Object gameObjectFromPath = GameSerializer.GetGameObjectFromPath(nodeAttr3, nodeAttr2);
            if (gameObjectFromPath != null)
            {
                if (gameObjectFromPath is GameObject)
                {
                    if (nodeAttr2 != null)
                    {
                        string pureType = GameSerializer.GetPureType(nodeAttr2);
                        if (!"GameObject".Equals(pureType))
                        {
                            obj = (gameObjectFromPath as GameObject).GetComponent(pureType);
                            if (obj == null)
                            {
                                Debug.LogError("No " + pureType + " component found in " + nodeAttr3);
                            }
                        }
                        else
                        {
                            obj = gameObjectFromPath;
                        }
                    }
                }
                else
                {
                    obj = gameObjectFromPath;
                }
            }
            else
            {
                obj = null;
                Debug.LogError("Load gameobject " + nodeAttr3 + " failed!");
            }
        }
        else if ("Com".Equals(nodeAttr4))
        {
            obj = GameSerializer.CreateInstance(nodeAttr2);
            if (obj == null)
            {
                return(null);
            }
            MemberInfo[] members = obj.GetType().GetMembers();
            for (int j = 0; j < members.Length; j++)
            {
                if (GameSerializer.IsMINeedExport(members[j]))
                {
                    BinaryNode binaryNode = currNode.SelectSingleNode(members[j].get_Name());
                    if (binaryNode != null)
                    {
                        try
                        {
                            object @object = GameSerializer.GetObject(binaryNode);
                            if (binaryNode != null && @object != null)
                            {
                                GameSerializer.SetMIValue(members[j], obj, @object);
                            }
                        }
                        catch (Exception ex)
                        {
                            Debug.LogError(string.Concat(new object[]
                            {
                                "Set field value failed! Field ",
                                members[j].get_Name(),
                                " in ",
                                obj.GetType(),
                                "e:",
                                ex
                            }));
                        }
                    }
                }
            }
        }
        return(obj);
    }
Esempio n. 2
0
    public static object GetObject(BinaryNode currNode)
    {
        string nodeAttr = GetNodeAttr(currNode, "NULL");
        object o        = null;

        if (nodeAttr == null)
        {
            string typeStr = GetNodeAttr(currNode, "Type");
            string s       = GetNodeAttr(currNode, "V");
            string str4    = GetNodeAttr(currNode, "JT");
            if ("Arr".Equals(str4))
            {
                if (typeStr == null)
                {
                    return(o);
                }
                System.Type elementType = GetType(typeStr.Replace("[]", string.Empty));
                if (elementType == null)
                {
                    Debug.LogError("Array type " + typeStr + " create failed!");
                    return(null);
                }
                Array array = Array.CreateInstance(elementType, currNode.GetChildNum());
                for (int i = 0; i < array.Length; i++)
                {
                    array.SetValue(GetObject(currNode.GetChild(i)), i);
                }
                return(array);
            }
            if ("Cus".Equals(str4))
            {
                if (typeStr != null)
                {
                    System.Type type = GetType(typeStr);
                    ICustomizedObjectSerializer objectSerlizer = GetObjectSerlizer(type);
                    if ((objectSerlizer != null) && (objectSerlizer is ICustomInstantiate))
                    {
                        o = ((ICustomInstantiate)objectSerlizer).Instantiate(currNode);
                    }
                    else
                    {
                        o = CreateInstance(type);
                    }
                    if (o == null)
                    {
                        return(null);
                    }
                    if (objectSerlizer != null)
                    {
                        objectSerlizer.ObjectDeserialize(ref o, currNode);
                    }
                }
                return(o);
            }
            if ("Enum".Equals(str4))
            {
                if (typeStr != null)
                {
                    o = Enum.ToObject(GetType(typeStr), int.Parse(s));
                }
                return(o);
            }
            if ("Pri".Equals(str4))
            {
                if (typeStr != null)
                {
                    o = Convert.ChangeType(s, GetType(typeStr));
                }
                return(o);
            }
            if ("Ref".Equals(str4))
            {
                UnityEngine.Object gameObjectFromPath = GetGameObjectFromPath(s, typeStr);
                if (gameObjectFromPath != null)
                {
                    if (gameObjectFromPath is GameObject)
                    {
                        if (typeStr == null)
                        {
                            return(o);
                        }
                        string pureType = GetPureType(typeStr);
                        if (!"GameObject".Equals(pureType))
                        {
                            o = (gameObjectFromPath as GameObject).GetComponent(pureType);
                            if (o == null)
                            {
                                Debug.LogError("No " + pureType + " component found in " + s);
                            }
                            return(o);
                        }
                    }
                    return(gameObjectFromPath);
                }
                o = null;
                Debug.LogError("Load gameobject " + s + " failed!");
                return(o);
            }
            if ("Com".Equals(str4))
            {
                o = CreateInstance(typeStr);
                if (o == null)
                {
                    return(null);
                }
                MemberInfo[] members = o.GetType().GetMembers();
                for (int j = 0; j < members.Length; j++)
                {
                    if (IsMINeedExport(members[j]))
                    {
                        BinaryNode node = currNode.SelectSingleNode(members[j].Name);
                        if (node != null)
                        {
                            try
                            {
                                object obj4 = GetObject(node);
                                if ((node != null) && (obj4 != null))
                                {
                                    SetMIValue(members[j], o, obj4);
                                }
                            }
                            catch (Exception exception)
                            {
                                Debug.LogError(string.Concat(new object[] { "Set field value failed! Field ", members[j].Name, " in ", o.GetType(), "e:", exception }));
                            }
                        }
                    }
                }
            }
        }
        return(o);
    }