コード例 #1
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);
        }
コード例 #2
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));
        }