public void Deserialize(IntermediateReader input, ContentSerializerAttribute format, object collection) { if (input == null) { throw new ArgumentNullException("input"); } if (format == null) { throw new ArgumentNullException("format"); } this.ValidateCollectionType(collection); IXmlListItemSerializer xmlListItemSerializer = this.contentSerializer as IXmlListItemSerializer; if (xmlListItemSerializer != null) { XmlListReader xmlListReader = new XmlListReader(input); while (!xmlListReader.AtEnd) { this.addToCollection(collection, xmlListItemSerializer.Deserialize(xmlListReader)); } return; } ContentSerializerAttribute contentSerializerAttribute = new ContentSerializerAttribute(); contentSerializerAttribute.ElementName = format.CollectionItemName; while (input.MoveToElement(format.CollectionItemName)) { this.addToCollection(collection, input.ReadObject<object>(contentSerializerAttribute, this.contentSerializer)); } }
public void Setup() { attribute = new ContentSerializerAttribute(); attribute.AllowNull = true; attribute.ElementName = "Name"; attribute.FlattenContent = true; }
/// <summary> /// Deserializes a $safeitemname$ object from intermediate XML format. /// </summary> /// <param name="input">Location of the intermediate XML and various deserialization helpers.</param> /// <param name="format">Specifies the intermediate source XML format.</param> /// <param name="existingInstance">The strongly typed object containing the received data, or null if the /// deserializer should construct a new instance.</param> /// <returns>A deserialized $safeitemname$ instance.</returns> protected override $safeitemname$ Deserialize(IntermediateReader input, ContentSerializerAttribute format, $safeitemname$ existingInstance) { $safeitemname$ value = existingInstance ?? default($safeitemname$); // TODO deserialize $safeitemname$ fields & properties... return value; }
public ContentSerializerAttribute Clone() { var clone = new ContentSerializerAttribute (); clone.AllowNull = AllowNull; clone._collectionItemName = _collectionItemName; clone.ElementName = ElementName; clone.FlattenContent = FlattenContent; clone.Optional = Optional; clone.SharedResource = SharedResource; return clone; }
public ContentSerializerAttribute Clone() { var clone = new ContentSerializerAttribute(); clone.AllowNull = AllowNull; clone._collectionItemName = _collectionItemName; clone.ElementName = ElementName; clone.FlattenContent = FlattenContent; clone.Optional = Optional; clone.SharedResource = SharedResource; return(clone); }
public ContentSerializerAttribute Clone() { ContentSerializerAttribute clone = new ContentSerializerAttribute(); clone.allowNull = this.allowNull; clone.collectionItemName = this.collectionItemName; clone.elementName = this.elementName; clone.flattenContent = this.flattenContent; clone.hasCollectionItemName = this.hasCollectionItemName; clone.optional = this.optional; clone.sharedResource = this.sharedResource; return clone; }
public ContentSerializerAttribute Clone() { ContentSerializerAttribute clone = new ContentSerializerAttribute(); clone.allowNull = this.allowNull; clone.collectionItemName = this.collectionItemName; clone.elementName = this.elementName; clone.flattenContent = this.flattenContent; clone.hasCollectionItemName = this.hasCollectionItemName; clone.optional = this.optional; clone.sharedResource = this.sharedResource; return(clone); }
public void EqualsTest() { ContentSerializerAttribute clone = new ContentSerializerAttribute(); attribute.AllowNull = true; attribute.ElementName = "Name"; attribute.FlattenContent = true; Assert.AreNotEqual(this.attribute, clone, "#1"); clone = this.attribute.Clone(); Assert.AreNotSame(this.attribute, clone, "#2"); Assert.AreEqual(this.attribute, clone, "#3"); Assert.IsTrue(this.attribute != clone, "#4"); Assert.IsFalse(this.attribute.Equals(null), "#5"); int aa = 5; Assert.IsFalse(this.attribute.Equals(aa), "#6"); }
protected override MeshCollider Deserialize(IntermediateReader input, Microsoft.Xna.Framework.Content.ContentSerializerAttribute format, MeshCollider existingInstance) { MeshCollider collider = new MeshCollider(); ContentSerializerAttribute attr = new ContentSerializerAttribute(); attr.ElementName = "id"; // SET ID int id = input.ReadObject <int>(attr); Type type = typeof(UnityObject); FieldInfo fld = type.GetField("_id", BindingFlags.Instance | BindingFlags.NonPublic); fld.SetValue(collider, id); attr.ElementName = "isTrigger"; collider.isTrigger = input.ReadObject <bool>(attr); attr.ElementName = "triangles"; short[] triangles = input.ReadObject <short[]>(attr); attr.ElementName = "vertices"; Microsoft.Xna.Framework.Vector3[] vertices = input.ReadObject <Microsoft.Xna.Framework.Vector3[]>(attr); List <Triangle> tris = new List <Triangle>(); for (int i = 0; i < triangles.Length; i += 3) { if (vertices[triangles[i]].Y + vertices[triangles[i + 1]].Y + vertices[triangles[i + 2]].Y > 1) { Debug.Log(" Warning: " + ToString() + " has non zero Y in collider"); } Triangle tri = new Triangle( vertices[triangles[i]].X, vertices[triangles[i]].Z, vertices[triangles[i + 2]].X, vertices[triangles[i + 2]].Z, vertices[triangles[i + 1]].X, vertices[triangles[i + 1]].Z ); tris.Add(tri); } collider.vertices = EarclipDecomposer.PolygonizeTriangles(tris, int.MaxValue, 0); return(collider); }
protected internal override void Serialize(IntermediateWriter output, object value, ContentSerializerAttribute format) { if (output == null) { throw new ArgumentNullException("output"); } if (value == null) { throw new ArgumentNullException("value"); } if (!base.IsTargetType(value)) { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.WrongArgumentType, new object[] { base.TargetType, value.GetType() })); } output.Xml.WriteString(value.ToString()); }
protected override StaticBatchRenderer Deserialize(IntermediateReader input, Microsoft.Xna.Framework.Content.ContentSerializerAttribute format, StaticBatchRenderer existingInstance) { StaticBatchRenderer renderer = new StaticBatchRenderer(); /*ContentSerializerAttribute attr = new ContentSerializerAttribute(); * attr.ElementName = "id"; * * // SET ID * int id = input.ReadObject<int>(attr); * Type type = typeof(UnityObject); * FieldInfo fld = type.GetField("_id", BindingFlags.Instance | BindingFlags.NonPublic); * fld.SetValue(renderer, id); * * attr.ElementName = "materials"; * attr.CollectionItemName = "material"; * renderer.materials = input.ReadObject<Material[]>(attr); * * attr.ElementName = "triangles"; * renderer.indices = input.ReadObject<short[][]>(attr); * * attr.ElementName = "vertices"; * Microsoft.Xna.Framework.Vector3[] vs = input.ReadObject<Microsoft.Xna.Framework.Vector3[]>(attr); * * attr.ElementName = "uv"; * Microsoft.Xna.Framework.Vector2[] uv = input.ReadObject<Microsoft.Xna.Framework.Vector2[]>(attr); * renderer.boundingSphere = BoundingSphere.CreateFromPoints(vs); * * //VertexPositionNormalTexture[] buffer = new VertexPositionNormalTexture[vs.Length]; * //for (int i = 0; i < vs.Length; i++) * //{ * // Microsoft.Xna.Framework.Vector3 v = vs[i]; * // v.Y = -v.Y; * // Microsoft.Xna.Framework.Vector2 u = uv[i]; * // u.Y = 1 - u.Y; * // buffer[i] = new VertexPositionTexture(v, u); * //} * //renderer.vertices = buffer;*/ return(renderer); }
protected internal override object Deserialize(IntermediateReader input, ContentSerializerAttribute format, object existingInstance) { if (input == null) { throw new ArgumentNullException("input"); } string text = input.Xml.ReadContentAsString(); object result; try { result = Enum.Parse(base.TargetType, text); } catch (ArgumentException innerException) { throw input.CreateInvalidContentException(innerException, Resources.InvalidEnumValue, new object[] { text, base.TargetType }); } return result; }
/// <summary> /// Serializes an object to intermediate XML format. /// </summary> /// <param name="output">Specifies the intermediate XML location, and provides various serialization helpers.</param> /// <param name="value">The strongly typed object to be serialized.</param> /// <param name="format">Specifies the content format for this object.</param> protected override void Serialize(IntermediateWriter output, $safeitemname$ value, ContentSerializerAttribute format) { // TODO serialize $safeitemname$ fields & properties... }
/// <summary>Serializes an object to intermediate XML format.</summary> /// <param name="output">Specifies the intermediate XML location, and provides various serialization helpers.</param> /// <param name="value">The object to be serialized.</param> /// <param name="format">Specifies the content format for this object.</param> protected internal abstract void Serialize(IntermediateWriter output, object value, ContentSerializerAttribute format);
/// <summary>Deserializes an object from intermediate XML format.</summary> /// <param name="input">Location of the intermediate XML and various deserialization helpers.</param> /// <param name="format">Specifies the intermediate source XML format.</param> /// <param name="existingInstance">The object containing the received data, or null if the deserializer should construct a new instance.</param> protected internal abstract object Deserialize(IntermediateReader input, ContentSerializerAttribute format, object existingInstance);
private static ReadElement GetElementReader( ContentTypeReaderManager manager, MemberInfo member ) { PropertyInfo property = member as PropertyInfo; FieldInfo field = member as FieldInfo; if (property != null) { // Properties must have at least a getter. if (property.CanRead == false) { return(null); } // Skip over indexer properties if (property.GetIndexParameters().Length > 0) { return(null); } } // Are we explicitly asked to ignore this item? Attribute attr = Attribute.GetCustomAttribute( member, typeof(ContentSerializerIgnoreAttribute) ); if (attr != null) { return(null); } ContentSerializerAttribute contentSerializerAttribute = Attribute.GetCustomAttribute( member, typeof(ContentSerializerAttribute) ) as ContentSerializerAttribute; if (contentSerializerAttribute == null) { if (property != null) { /* There is no ContentSerializerAttribute, so non-public * properties cannot be deserialized. */ MethodInfo getMethod = property.GetGetMethod(true); if (getMethod != null && !getMethod.IsPublic) { return(null); } MethodInfo setMethod = property.GetSetMethod(true); if (setMethod != null && !setMethod.IsPublic) { return(null); } /* If the read-only property has a type reader, * and CanDeserializeIntoExistingObject is true, * then it is safe to deserialize into the existing object. */ if (!property.CanWrite) { ContentTypeReader typeReader = manager.GetTypeReader(property.PropertyType); if (typeReader == null || !typeReader.CanDeserializeIntoExistingObject) { return(null); } } } else { /* There is no ContentSerializerAttribute, so non-public * fields cannot be deserialized. */ if (!field.IsPublic) { return(null); } // evolutional: Added check to skip initialise only fields if (field.IsInitOnly) { return(null); } } } Action <object, object> setter; Type elementType; if (property != null) { elementType = property.PropertyType; if (property.CanWrite) { setter = (o, v) => property.SetValue(o, v, null); } else { setter = (o, v) => { }; } } else { elementType = field.FieldType; setter = field.SetValue; } if (contentSerializerAttribute != null && contentSerializerAttribute.SharedResource) { return((input, parent) => { Action <object> action = value => setter(parent, value); input.ReadSharedResource(action); }); } // We need to have a reader at this point. ContentTypeReader reader = manager.GetTypeReader(elementType); if (reader == null) { throw new ContentLoadException(string.Format( "Content reader could not be found for {0} type.", elementType.FullName )); } /* We use the construct delegate to pick the correct existing * object to be the target of deserialization. */ Func <object, object> construct = parent => null; if (property != null && !property.CanWrite) { construct = parent => property.GetValue(parent, null); } return((input, parent) => { object existing = construct(parent); object obj2 = input.ReadObject(reader, existing); setter(parent, obj2); }); }
private static ReadElement GetElementReader( ContentTypeReaderManager manager, MemberInfo member ) { PropertyInfo property = member as PropertyInfo; FieldInfo field = member as FieldInfo; // Properties must have public get and set if (property != null && (property.CanWrite == false || property.CanRead == false)) { return(null); } if (property != null && property.Name == "Item") { MethodInfo getMethod = property.GetGetMethod(); MethodInfo setMethod = property.GetSetMethod(); if ((getMethod != null && getMethod.GetParameters().Length > 0) || (setMethod != null && setMethod.GetParameters().Length > 0)) { /* This is presumably a property like this[indexer] and this * should not get involved in the object deserialization */ return(null); } } Attribute attr = Attribute.GetCustomAttribute( member, typeof(ContentSerializerIgnoreAttribute) ); if (attr != null) { return(null); } ContentSerializerAttribute contentSerializerAttribute = Attribute.GetCustomAttribute( member, typeof(ContentSerializerAttribute) ) as ContentSerializerAttribute; bool isSharedResource = false; if (contentSerializerAttribute != null) { isSharedResource = contentSerializerAttribute.SharedResource; } else { if (property != null) { MethodInfo getMethod = property.GetGetMethod(); if (getMethod == null || !getMethod.IsPublic) { return(null); } MethodInfo setMethod = property.GetSetMethod(); if (setMethod == null || !setMethod.IsPublic) { return(null); } } else { if (!field.IsPublic) { return(null); } // evolutional: Added check to skip initialise only fields if (field.IsInitOnly) { return(null); } /* Private fields can be serialized if they have * ContentSerializerAttribute added to them. */ if (field.IsPrivate && contentSerializerAttribute == null) { return(null); } } } Action <object, object> setter; ContentTypeReader reader; Type elementType; if (property != null) { elementType = property.PropertyType; reader = manager.GetTypeReader(property.PropertyType); setter = (o, v) => property.SetValue(o, v, null); } else { elementType = field.FieldType; reader = manager.GetTypeReader(field.FieldType); setter = field.SetValue; } if (isSharedResource) { return((input, parent) => { Action <object> action = value => setter(parent, value); input.ReadSharedResource(action); }); } Func <object> construct = () => null; if (elementType.IsClass && !elementType.IsAbstract) { ConstructorInfo defaultConstructor = elementType.GetDefaultConstructor(); if (defaultConstructor != null) { construct = () => defaultConstructor.Invoke(null); } } // Reading elements serialized as "object". if (reader == null && elementType == typeof(object)) { return((input, parent) => { object obj2 = input.ReadObject <object>(); setter(parent, obj2); }); } // evolutional: Fix. We can get here and still be NULL, exit gracefully if (reader == null) { return(null); } return((input, parent) => { object existing = construct(); object obj2 = input.ReadObject(reader, existing); setter(parent, obj2); }); }
private void Read( object parent, ContentReader input, MemberInfo member ) { PropertyInfo property = member as PropertyInfo; FieldInfo field = member as FieldInfo; // Properties must have public get and set if (property != null && (property.CanWrite == false || property.CanRead == false)) { return; } if (property != null && property.Name == "Item") { MethodInfo getMethod = property.GetGetMethod(); MethodInfo setMethod = property.GetSetMethod(); if ((getMethod != null && getMethod.GetParameters().Length > 0) || (setMethod != null && setMethod.GetParameters().Length > 0)) { /* This is presumably a property like this[indexer] and this * should not get involved in the object deserialization */ return; } } Attribute attr = Attribute.GetCustomAttribute(member, typeof(ContentSerializerIgnoreAttribute)); if (attr != null) { return; } Attribute attr2 = Attribute.GetCustomAttribute(member, typeof(ContentSerializerAttribute)); bool isSharedResource = false; if (attr2 != null) { ContentSerializerAttribute cs = attr2 as ContentSerializerAttribute; isSharedResource = cs.SharedResource; } else { if (property != null) { foreach (MethodInfo info in property.GetAccessors(true)) { if (info.IsPublic == false) { return; } } } else { if (!field.IsPublic) { return; } // Evolutional: Added check to skip initialise only fields if (field.IsInitOnly) { return; } } } ContentTypeReader reader = null; Type elementType = null; if (property != null) { reader = manager.GetTypeReader(elementType = property.PropertyType); } else { reader = manager.GetTypeReader(elementType = field.FieldType); } if (!isSharedResource) { object existingChildObject = CreateChildObject(property, field); object obj2; if (reader == null && elementType == typeof(object)) { // Reading elements serialized as "object" obj2 = input.ReadObject <object>(); } else { // Default /* Evolutional: Fix. We can get here and still be NULL, * exit gracefully */ if (reader == null) { return; } obj2 = input.ReadObject(reader, existingChildObject); } if (property != null) { property.SetValue(parent, obj2, null); } else { /* Private fields can be serialized if they have * ContentSerializerAttribute added to them */ if (field.IsPrivate == false || attr2 != null) { field.SetValue(parent, obj2); } } } else { Action <object> action = delegate(object value) { if (property != null) { property.SetValue(parent, value, null); } else { field.SetValue(parent, value); } }; input.ReadSharedResource(action); } }
public void Serialize(IntermediateWriter output, ContentSerializerAttribute format, object collection) { if (output == null) { throw new ArgumentNullException("output"); } if (format == null) { throw new ArgumentNullException("format"); } this.ValidateCollectionType(collection); IEnumerable enumerable = (IEnumerable)collection; if (this.contentSerializer is IXmlListItemSerializer) { ContentSerializerAttribute contentSerializerAttribute = new ContentSerializerAttribute(); contentSerializerAttribute.FlattenContent = true; bool flag = true; IEnumerator enumerator = enumerable.GetEnumerator(); try { while (enumerator.MoveNext()) { object current = enumerator.Current; if (flag) { flag = false; } else { output.Xml.WriteWhitespace(" "); } output.WriteRawObject<object>(current, contentSerializerAttribute, this.contentSerializer); } return; } finally { IDisposable disposable = enumerator as IDisposable; if (disposable != null) { disposable.Dispose(); } } } ContentSerializerAttribute contentSerializerAttribute2 = new ContentSerializerAttribute(); contentSerializerAttribute2.ElementName = format.CollectionItemName; foreach (object current2 in enumerable) { output.WriteObject<object>(current2, contentSerializerAttribute2, this.contentSerializer); } }
protected override void Serialize(IntermediateWriter output, StaticBatchRenderer value, Microsoft.Xna.Framework.Content.ContentSerializerAttribute format) { throw new NotImplementedException(); }