Exemplo n.º 1
0
    public void ComponentDeserialize(Component o, BinaryNode node)
    {
        Animation animation = o as Animation;

        for (int i = 0; i < node.GetChildNum(); i++)
        {
            BinaryNode child = node.GetChild(i);
            if (child.GetName() == "AniName")
            {
                string        nodeAttr = GameSerializer.GetNodeAttr(child, "Value");
                AnimationClip resource = (AnimationClip)GameSerializer.GetResource(nodeAttr, typeof(AnimationClip));
                if (null == resource)
                {
                    Debug.LogError("Cannot find Animation: " + nodeAttr);
                    return;
                }
                if ((nodeAttr != null) && (nodeAttr.Length != 0))
                {
                    animation.AddClip(resource, resource.name);
                }
            }
        }
    }
    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);
    }
    private static GameObject LoadRecursionOnce(GameObject parentGo, BinaryNode domNode)
    {
        GameObject gameObject = null;
        string     nodeAttr   = GameSerializer.GetNodeAttr(domNode, "ON");

        if (parentGo != null)
        {
            for (int i = 0; i < parentGo.transform.childCount; i++)
            {
                if (parentGo.transform.GetChild(i).name.Equals(nodeAttr))
                {
                    gameObject = parentGo.transform.GetChild(i).gameObject;
                    break;
                }
            }
        }
        if (gameObject == null)
        {
            string nodeAttr2 = GameSerializer.GetNodeAttr(domNode, "PFB");
            if (nodeAttr2 != null && nodeAttr2.get_Length() != 0)
            {
                object resource = GameSerializer.GetResource(nodeAttr2, typeof(GameObject));
                if (resource == null || !(resource is GameObject))
                {
                    Debug.LogError(nodeAttr2 + " 不存在或者类型错误,请重新导出该场景");
                    gameObject = new GameObject();
                }
                else
                {
                    GameObject gameObject2 = resource as GameObject;
                    bool       activeSelf  = gameObject2.activeSelf;
                    gameObject2.SetActive(false);
                    gameObject = (GameObject)Object.Instantiate(gameObject2);
                    gameObject2.SetActive(activeSelf);
                }
            }
            else
            {
                gameObject = new GameObject();
            }
        }
        Vector3 localScale = gameObject.transform.localScale;

        gameObject.name = GameSerializer.GetNodeAttr(domNode, "ON");
        if (parentGo != null)
        {
            gameObject.transform.parent = parentGo.transform;
        }
        gameObject.transform.localScale = localScale;
        GameSerializer.DeserializeObject(domNode, gameObject);
        gameObject.SetActive(false);
        GameSerializer.InitComponets(domNode, gameObject);
        for (int j = 0; j < domNode.GetChildNum(); j++)
        {
            BinaryNode child = domNode.GetChild(j);
            if (child.GetName() == "CHD")
            {
                GameSerializer.LoadRecursionOnce(gameObject, child);
            }
        }
        return(gameObject);
    }
    public object Instantiate(BinaryNode node)
    {
        int num = int.Parse(GameSerializer.GetNodeAttr(node, "Size"));

        return(new byte[num]);
    }