private void _PreParse(string xmlContent, string filename, ref PrototypeParseParameters parameters, List <SerializedData> result) { ListPool <SerializedData> .GetIfNull(ref result); ListPool <ParsingError> .GetIfNull(ref errors); var xElement = XElement.Parse(xmlContent); // Validity checks if (!ParsingValidation.ContainerElementName(xElement, filename, errors) || !ParsingValidation.ContainerTypeAttribute(xElement, filename, errors)) { return; } // Get type XAttribute typeAttribute = xElement.Attribute(PrototypeContainerAttributeType); var type = GetSerializableTypeCacheFor(typeAttribute.Value, ref parameters); if (!ParsingValidation.TypeFound(xElement, typeAttribute, type, filename, errors)) { return; } // Iterate over nodes foreach (var xNode in xElement.Nodes()) { var elementType = type; var nodeXElement = xNode as XElement; // Validity checks if (!ParsingValidation.NodeIsElement(xNode, filename, errors) || !ParsingValidation.PrototypeElementName(nodeXElement, filename, errors)) { continue; } var elementTypeAttribute = nodeXElement.Attribute(PrototypeContainerAttributeType); if (!ReferenceEquals(elementTypeAttribute, null)) { elementType = GetSerializableTypeCacheFor(elementTypeAttribute.Value, ref parameters); if (!ParsingValidation.TypeFound(nodeXElement, elementTypeAttribute, elementType, filename, errors)) { continue; } } // Prepare var data = new SerializedData(elementType, nodeXElement, filename); result.Add(data); } }
/// <summary> /// Actually loads the data from the previous parse <see cref="ParseFields(List{ParsingError})"/>. /// It will for every field with string data deserialize this data using <see cref="IPrototypeDataSerializer"/>. /// /// For every sub-data field (<seealso cref="ParseFields(List{ParsingError})"/>) a <see cref="SerializedData"/> object is being written to <see cref="fields"/>. /// The sub-data object will have <see cref="PrepareParse(SerializableTypeCache, XElement, string)"/>, <see cref="ParseFields(List{ParsingError})"/> and <see cref="LoadFields(List{ParsingError}, PrototypeParserState)"/> called. /// </summary> public void LoadFields(List <ParsingError> errors, PrototypeParserState state) { foreach (var xNode in xElement.Nodes()) { if (!ParsingValidation.NodeIsElement(xNode, this.filename, errors)) // Malformed XML { continue; } var xElement = xNode as XElement; var elementName = xElement.Name.LocalName; // Field unknown? if (!ParsingValidation.FieldKnown(xElement, targetType, elementName, filename, errors)) { continue; } debug.Add(elementName, xElement); var fieldData = targetType.GetFieldData(elementName); var fieldType = fieldData.serializableTypeCache; bool isCollection = SerializedCollectionData.IsCollection(fieldData.fieldInfo.FieldType); IPrototypeDataSerializer serializer = PrototypeCaches.GetBestSerializerFor(fieldData.fieldInfo.FieldType); if (!ReferenceEquals(serializer, null)) { try { if (!ParsingValidation.SerializerWasFound(xElement, serializer, elementName, targetType == null ? null : targetType.type, fieldType == null ? null : fieldType.type, filename, errors)) { continue; } fields.Add(elementName, serializer.Deserialize(fieldData.fieldInfo.FieldType, xElement, state)); } catch (Exception ex) { errors.Add(new ParsingError(ParsingErrorSeverity.ERROR, filename, -1, "Serializer threw exception on field " + elementName + " on type " + targetType.type + ":\n\n" + ex.ToString() + "\n\nSkipping field!")); } } else if (isCollection) { var col = new SerializedCollectionData(fieldData.fieldInfo.FieldType, xElement, this.filename); col.ParseAndLoadData(errors, state); fields.Add(elementName, col); // Collection override action? var collectionOverrideAttrib = xElement.Attribute(PrototypeParser.PrototypeAttributeCollectionOverrideAction); if (!ReferenceEquals(collectionOverrideAttrib, null)) { collectionOverrideActions.Set(elementName, (CollectionOverrideAction)Enum.Parse(typeof(CollectionOverrideAction), collectionOverrideAttrib.Value)); } } else { // A known serializable // Prototype reference? if (fieldData.isPrototype) { fields.Add(elementName, new SerializedPrototypeReference() { identifier = xElement.Value as string }); } else { // Determine which type to serialize var targetType = fieldData.serializableTypeCache; string typeName = fieldData.fieldInfo.Name; // Check if element explicitly overwrites the type to support polymorphism // The field type might be some base class type and the xml overwrites this type with a class extending from the base var classAttrib = xElement.Attribute(PrototypeParser.PrototypeAttributeType); if (!ReferenceEquals(classAttrib, null)) { targetType = PrototypeCaches.GetSerializableTypeCacheFor(classAttrib.Value, state.parameters.standardNamespace); typeName = classAttrib.Value; } // Field not serializable? if (!ParsingValidation.DataFieldSerializerValid(xElement, targetType, typeName, elementName, filename, errors)) { continue; } // Resolve field name type var d = new SerializedData(targetType, xElement as XElement, this.filename); d.LoadFields(errors, state); fields.Add(elementName, d); } } } }