Beispiel #1
0
        public static BaseNode CloneWithoutChildren(BaseNode node)
        {
            if (node == null)
            {
                return(null);
            }
            var newNode = Activator.CreateInstance(node.GetType()) as BaseNode;

            foreach (var fi in ReactTypeRegister.GetFields(node))
            {
                fi.SetValue(newNode, fi.GetValue(node));
            }
            return(newNode);
        }
Beispiel #2
0
        public static List <FieldInfo> GetFields(BaseNode node)
        {
            if (finfos == null)
            {
                finfos = new Dictionary <System.Type, List <FieldInfo> >();
            }
            var T = node.GetType();
            List <FieldInfo> fields;

            if (finfos.TryGetValue(T, out fields))
            {
                return(fields);
            }
            fields = (from i in T.GetFields()
                      where (
                          i.IsPublic &&
                          ReactTypeRegister.IsSupportedParameter(i.FieldType))
                      select i).ToList();
            finfos[T] = fields;
            return(fields);
        }
Beispiel #3
0
 public void Serialize(BaseNode node)
 {
     if (node == null)
     {
         tokens.Add("null");
     }
     else
     {
         tokens.Add("new");
         tokens.Add(node.GetType().AssemblyQualifiedName);
         tokens.Add("{");
         foreach (var fi in ReactTypeRegister.GetFields(node))
         {
             tokens.Add(fi.Name);
             var o = fi.GetValue(node);
             if (fi.FieldType.IsSubclassOf(typeof(UnityEngine.Object)))
             {
                 references.Add(o as UnityEngine.Object);
             }
             else
             {
                 tokens.Add(ConvertToRepr(o));
             }
         }
         tokens.Add("}");
         var parent = node as IParentNode;
         if (parent != null)
         {
             tokens.Add("[");
             foreach (var child in parent.GetChildren())
             {
                 Serialize(child);
             }
             tokens.Add("]");
         }
     }
 }
Beispiel #4
0
        public static BaseNode Clone(BaseNode node)
        {
            if (node == null)
            {
                return(null);
            }
            var newNode = Activator.CreateInstance(node.GetType()) as BaseNode;

            foreach (var fi in ReactTypeRegister.GetFields(node))
            {
                fi.SetValue(newNode, fi.GetValue(node));
            }
            var nodeParent = node as IParentNode;

            if (nodeParent != null)
            {
                var newNodeParent = newNode as IParentNode;
                foreach (var c in nodeParent.GetChildren())
                {
                    newNodeParent.Add(Clone(c));
                }
            }
            return(newNode);
        }
Beispiel #5
0
        public BaseNode Deserialize()
        {
            IsMissingTypes = false;
            var      queue      = new Queue <string>(tokens);
            var      objects    = new Queue <UnityEngine.Object>(references);
            var      parents    = new Stack <IParentNode>();
            Type     type       = null;
            BaseNode instance   = null;
            BaseNode lastParent = null;

            while (queue.Count > 0)
            {
                var cmd = queue.Dequeue();
                switch (cmd)
                {
                case "null":
                    Debug.LogError("WTF");
                    break;

                case "new":
                    var typeName = queue.Dequeue();
                    type = ReactTypeRegister.Find(typeName);
                    if (type == null)
                    {
                        missingTypes.Add(typeName);
                        IsMissingTypes = true;
                        Debug.Log("Missing Type: " + typeName);
                        return(null);
                    }
                    instance = Activator.CreateInstance(type) as BaseNode;
                    if (parents.Count > 0)
                    {
                        parents.Peek().Add(instance);
                    }
                    break;

                case "{":
                    var fcmd = queue.Dequeue();
                    while (fcmd != "}")
                    {
                        var fi = type.GetField(fcmd);
                        if (fi == null)
                        {
                            Debug.LogError(fcmd);
                        }
                        else
                        {
                            if (fi.FieldType.IsSubclassOf(typeof(UnityEngine.Object)))
                            {
                                fi.SetValue(instance, objects.Dequeue());
                            }
                            else
                            {
                                fi.SetValue(instance, ConvertToValue(fi.FieldType, queue.Dequeue(), objects));
                            }
                        }
                        fcmd = queue.Dequeue();
                    }
                    break;

                case "[":
                    parents.Push((IParentNode)instance);
                    break;

                case "]":
                    lastParent = (BaseNode)parents.Pop();
                    break;
                }
            }
            return(lastParent);
        }