Ejemplo n.º 1
0
        public void PatchTo(PatchContext context, ref object obj, IObjectNode patch)
        {
            if (obj == null)
            {
                return;
            }
            if (patch.IsNull)
            {
                return;
            }
            if (patch.Type != NodeType.Complex)
            {
                return;
            }
            if (obj.GetType() != this.type)
            {
                return;
            }

            foreach (var node in patch.Children)
            {
                if (node.Type != NodeType.Int)
                {
                    continue;
                }
                if (!propDict.TryGetValue(node.Name, out var prop))
                {
                    continue;
                }
                prop.SetValue(obj, node.Value);
            }
        }
Ejemplo n.º 2
0
 public void UseContext(PatchContext context)
 {
     foreach (var f in this.patcherFactories)
     {
         f.UseContext(context);
     }
 }
Ejemplo n.º 3
0
            public void SetValue(PatchContext context, object parent, IObjectNode patch)
            {
                var instance = this.FieldInfo.GetValue(parent);

                this.Patcher.PatchTo(context, ref instance, patch);
                this.FieldInfo.SetValue(parent, instance);
            }
Ejemplo n.º 4
0
        public IObjectNode PatchFrom(PatchContext context, object obj, string name)
        {
            if (obj == null)
            {
                return(null);
            }
            if (obj.GetType() != this.type)
            {
                return(null);
            }

            return(new ComplexObjectNode(name, false, CreateObjectNodes(obj)));
        }
Ejemplo n.º 5
0
        public void PatchTo(PatchContext context, ref object obj, IObjectNode patch)
        {
            if (patch.Type != NodeType.Complex)
            {
                return;
            }
            if (patch.IsNull)
            {
                obj = null;
                return;
            }

            var unityPatchContext = context.Get <UnityPatchContext>();

            var(typeName, id, referenceTo) = FindMetaInfo(patch);
            if (referenceTo >= 0)
            {
                var temp = unityPatchContext.FindObject(referenceTo);
                if (temp != null && this.type.IsAssignableFrom(temp.GetType()))
                {
                    obj = temp;
                }
                return;
            }

            if (string.IsNullOrEmpty(typeName))
            {
                typeName = this.type.Name;
            }

            if (obj == null || obj.GetType().Name != typeName)
            {
                obj = ScriptableObject.CreateInstance(typeName);
                if (obj == null)
                {
                    return;
                }
            }

            if (id >= 0)
            {
                unityPatchContext.Register(id, obj);
            }

            var patcher = GetPatcher(obj.GetType(), this.patcherRegistry);

            patcher?.PatchTo(context, ref obj, patch);
        }
Ejemplo n.º 6
0
        public IObjectNode PatchFrom(PatchContext context, object obj, string name)
        {
            if (obj == null)
            {
                if (this.nodeType == NodeType.String)
                {
                    return(new PrimitiveObjectNode(NodeType.String, name, null));
                }

                return(null);
            }

            if (obj.GetType().ToNodeType() != this.nodeType)
            {
                return(null);
            }

            return(new PrimitiveObjectNode(this.nodeType, name, obj));
        }
Ejemplo n.º 7
0
        public IObjectNode PatchFrom(PatchContext context, object obj, string name)
        {
            if (obj == null)
            {
                return(new ComplexObjectNode(name, true, null));
            }

            var unityPatchContext = context.Get <UnityPatchContext>();
            var id = unityPatchContext.FindReferenceId(obj);

            if (id >= 0)
            {
                return(new ComplexObjectNode(name, false, new IObjectNode[]
                {
                    new PrimitiveObjectNode(NodeType.Int, ReferenceToNodeName, id),
                }));
            }
            id = unityPatchContext.Register(obj);

            var scriptableObjectType = obj?.GetType() ?? this.type;
            var patcher = GetPatcher(scriptableObjectType, this.patcherRegistry);
            var patch   = patcher?.PatchFrom(context, obj, name);

            if (patch == null)
            {
                return(null);
            }
            if (patch.IsNull)
            {
                return(patch);
            }

            var childrenWithType = new List <IObjectNode>(patch.Children.Count + 2);

            childrenWithType.Add(new PrimitiveObjectNode(NodeType.String, TypeNodeName, scriptableObjectType.Name));
            childrenWithType.Add(new PrimitiveObjectNode(NodeType.Int, ReferenceIdNodeName, id));
            childrenWithType.AddRange(patch.Children);
            return(new ComplexObjectNode(name, false, childrenWithType));
        }
Ejemplo n.º 8
0
        public void PatchTo(PatchContext context, ref object obj, IObjectNode patch)
        {
            // string is only nullable primitive type.
            if (obj == null && this.nodeType != NodeType.String)
            {
                return;
            }
            if (this.nodeType != patch.Type)
            {
                return;
            }

            if (patch.IsNull)
            {
                if (this.nodeType == NodeType.String)
                {
                    obj = null;
                }
                return;
            }

            obj = patch.Value;
        }
 public void UseContext(PatchContext context)
 {
 }
Ejemplo n.º 10
0
 public void UseContext(PatchContext context)
 {
     context.Use <UnityPatchContext>(new UnityPatchContext());
 }
Ejemplo n.º 11
0
 public void PatchTo(PatchContext context, ref object obj, IObjectNode patch)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 12
0
 public IObjectNode PatchFrom(PatchContext context, object obj, string name)
 {
     return(null);
 }