From fa180a58b8781b5c07e77dab97a25e9c30ecbae2 Mon Sep 17 00:00:00 2001 From: "SND\\eagleeyestudios_cp" Date: Fri, 28 Sep 2012 20:57:30 +0000 Subject: [PATCH] Content Pipeline: - started implementing XmlImporter --- .../ANX.Framework.Content.Pipeline.csproj | 2 + .../Importer/XmlImporter.cs | 84 +++++++++++++++++++ .../Serialization/XmlReaderExtensions.cs | 23 +++++ 3 files changed, 109 insertions(+) create mode 100644 ANX.Framework.Content.Pipeline/Importer/XmlImporter.cs create mode 100644 ANX.Framework.Content.Pipeline/Serialization/XmlReaderExtensions.cs diff --git a/ANX.Framework.Content.Pipeline/ANX.Framework.Content.Pipeline.csproj b/ANX.Framework.Content.Pipeline/ANX.Framework.Content.Pipeline.csproj index 886abfe4..3d225655 100644 --- a/ANX.Framework.Content.Pipeline/ANX.Framework.Content.Pipeline.csproj +++ b/ANX.Framework.Content.Pipeline/ANX.Framework.Content.Pipeline.csproj @@ -108,6 +108,7 @@ + @@ -187,6 +188,7 @@ + diff --git a/ANX.Framework.Content.Pipeline/Importer/XmlImporter.cs b/ANX.Framework.Content.Pipeline/Importer/XmlImporter.cs new file mode 100644 index 00000000..d0a43e2b --- /dev/null +++ b/ANX.Framework.Content.Pipeline/Importer/XmlImporter.cs @@ -0,0 +1,84 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Xml; +using ANX.Framework.Content.Pipeline.Serialization; +using ANX.Framework.NonXNA.Development; + +namespace ANX.Framework.Content.Pipeline.Importer +{ + [ContentImporter("ANX XML Importer", ".xml")] + [Developer("SilentWarrior / Eagle Eye Studios")] + [PercentageComplete(20)] + public class XmlImporter : ContentImporter + { + public override object Import(string filename, ContentImporterContext context) + { + object result; + context.Logger.LogMessage("Checking if file exists."); + if (!File.Exists(filename)) + throw new InvalidContentException("The XML file \"" + filename + "\" could not be found."); + + context.Logger.LogMessage("Starting analysis of the XML file."); + using (XmlReader xml = XmlReader.Create(filename)) + { + //Check if XML contains XnaContent or AnxContent root element + context.Logger.LogMessage("Checking for root element."); + if (!xml.CheckForElement("XnaContent") && !xml.CheckForElement("AnxContent")) + throw new InvalidContentException("The given XML file does not contain Xna or Anx readable content! Did you forget the \"\" or \"\"?"); + + //Check which type is beeing described in the XML + var type = GetType(xml, context.Logger); + + //Create an instance of that type and fill it with the appropriate stuff + result = ReadObject(xml, type, context.Logger); + } + return result; + } + + private Type GetType(XmlReader xml, ContentBuildLogger logger) + { + logger.LogMessage("Moving reader to type attribute of the first asset node."); + xml.ReadStartElement("Asset"); + xml.MoveToAttribute("Type"); + logger.LogMessage("Trying to read a type from the Xml file"); + var type = Type.GetType(xml.ReadContentAsString()); + if (type == null) + throw new InvalidContentException("Error during deserialization: Type is null. Is there a valid Type attribute in the Asset section of your Xml file?"); + logger.LogImportantMessage("Type is" + type); + return type; + } + + private object ReadObject(XmlReader reader, Type type, ContentBuildLogger logger) + { + //TODO: Get all other elements from the xml file and add them to a dictionary. + var props = new Dictionary(); + while (!reader.EOF) + { + if (reader.NodeType == XmlNodeType.Element) + { + if (!reader.HasValue && !reader.IsEmptyElement) //we have an element without value and that is not empty, that's suspicious! + { + //Is it really empty? Maybe it's a list or a dictionary? + System.Diagnostics.Debugger.Break(); + } + else + props.Add(reader.Name, reader.ReadElementContentAsObject()); + } + } + + System.Diagnostics.Debugger.Break(); + //TODO: Get all public properties of the class via reflection + + //Activate instance + var result = Activator.CreateInstance(type); + + //TODO: Check if there is a pendant for every entry + + //TODO: Match every property with its value + + //Return the whole construct + return result; + } + } +} diff --git a/ANX.Framework.Content.Pipeline/Serialization/XmlReaderExtensions.cs b/ANX.Framework.Content.Pipeline/Serialization/XmlReaderExtensions.cs new file mode 100644 index 00000000..54daf2b4 --- /dev/null +++ b/ANX.Framework.Content.Pipeline/Serialization/XmlReaderExtensions.cs @@ -0,0 +1,23 @@ +using System; +using System.Xml; + +namespace ANX.Framework.Content.Pipeline.Serialization +{ + public static class XmlReaderExtensions + { + /// + /// Checks if the current document contains the given element. + /// + /// + /// Name of the element + /// + public static bool CheckForElement(this XmlReader xmlReader, string name) + { + if (string.IsNullOrEmpty(name)) + { + throw new ArgumentException("Element name can not be null!"); + } + return xmlReader.MoveToContent() == XmlNodeType.Element && xmlReader.Name == name; + } + } +}