Exemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="surrogate"></param>
        /// <param name="obj"></param>
        /// <param name="firstChild"></param>
        /// <returns></returns>
        protected object DeserializeSurrogate(ISerializationSurrogate surrogate, Type objType, int objId, XmlNode firstChild, XmlElement rootElement, object standin)
        {
            //NOTE: Technically this isn't necessary because we can be sure it'll
            //never be called on Components. The GameObject surrogate will handle
            //deserializing components and any loose references will be skipped
            //until the deference phase at the end of deserialization.
            //However, in case serialization wasn't done properly (components were
            //serialized in-place rather than defered to GameObject serialization),
            //then we need to ensure we simply skip this in such a case.
            if (standin == null)
            {
                if (objType.IsSubclassOf(typeof(Component)))
                {
                    return(null);
                }
                if (objType.IsSubclassOf(typeof(Component[])))
                {
                    return(null);
                }
                if (objType == typeof(Component[]))
                {
                    return(null);
                }
            }

            SerializationInfo  info    = new SerializationInfo(objType, new DummyConverter());
            DeserializeContext context = new DeserializeContext();

            context.Deserializer             = this;
            context.ObjId                    = objId;
            context.ElementBeingParsed       = rootElement;
            context.ObjectType               = objType;
            context.deserializationObjCache  = this.deserializationObjCache;
            context.deserializationTypeCache = this.deserializationTypeCache;

            //float through the xml nodes and see what items we need to collect.
            for (XmlNode node = firstChild; node != null; node = node.NextSibling)
            {
                object val = DeserializeCore((XmlElement)node);
                if (val != null)
                {
                    info.AddValue(node.Name, val, val.GetType());
                }
                else
                {
                    info.AddValue(node.Name, null, typeof(object));
                }
            }

            //if the standin is null, we need to create an instance.
            //Otherwise, we were given an instance to work with.
            object obj = standin;

            if (obj == null)
            {
                if (SerializerBase.IsSameOrSubclass(typeof(MulticastDelegate), objType))
                {
                    obj = null;
                }
                else
                {
                    obj = CreateInstanceOfType(objType, false);
                    //if(SerializerBase.IsReferenceNull(obj))
                    //    return null;
                }
            }

            //we pass obj in but we also assign it back from the output, just in case
            //the surrogate decided to do something funky. There is also the simple
            //possibility that we couldn't create an instance of the object beforehand
            //so we are actually passing in 'null'.
            obj = surrogate.SetObjectData(obj,
                                          info,
                                          new StreamingContext(StreamingContextStates.File, context),
                                          this);

            if (SerializerBase.IsReferenceNull(obj))
            {
                Debug.LogWarning("The surrogate '" + surrogate.ToString() + "' could not create an instance of '" + objType.Name + "' for the element '" + firstChild.ParentNode.Name + "'.");
            }
            CacheObject(obj, objId);
            return(obj);
        }