Content Pipeline:
- started implementing XmlImporter
This commit is contained in:
parent
61ba3497f2
commit
fa180a58b8
@ -108,6 +108,7 @@
|
|||||||
<Compile Include="IContentProcessor.cs" />
|
<Compile Include="IContentProcessor.cs" />
|
||||||
<Compile Include="Importer\TextureImporter.cs" />
|
<Compile Include="Importer\TextureImporter.cs" />
|
||||||
<Compile Include="Importer\WavImporter.cs" />
|
<Compile Include="Importer\WavImporter.cs" />
|
||||||
|
<Compile Include="Importer\XmlImporter.cs" />
|
||||||
<Compile Include="InvalidContentException.cs" />
|
<Compile Include="InvalidContentException.cs" />
|
||||||
<Compile Include="NamedValueDictionary.cs" />
|
<Compile Include="NamedValueDictionary.cs" />
|
||||||
<Compile Include="OpaqueDataDictionary.cs" />
|
<Compile Include="OpaqueDataDictionary.cs" />
|
||||||
@ -187,6 +188,7 @@
|
|||||||
<Compile Include="Tasks\ImporterManager.cs" />
|
<Compile Include="Tasks\ImporterManager.cs" />
|
||||||
<Compile Include="Tasks\ProcessorManager.cs" />
|
<Compile Include="Tasks\ProcessorManager.cs" />
|
||||||
<Compile Include="VideoContent.cs" />
|
<Compile Include="VideoContent.cs" />
|
||||||
|
<Compile Include="Serialization\XmlReaderExtensions.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\ANX.Framework\ANX.Framework.csproj">
|
<ProjectReference Include="..\ANX.Framework\ANX.Framework.csproj">
|
||||||
|
84
ANX.Framework.Content.Pipeline/Importer/XmlImporter.cs
Normal file
84
ANX.Framework.Content.Pipeline/Importer/XmlImporter.cs
Normal file
@ -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<object>
|
||||||
|
{
|
||||||
|
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 \"<XnaContent>\" or \"<AnxContent>\"?");
|
||||||
|
|
||||||
|
//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<string, object>();
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,23 @@
|
|||||||
|
using System;
|
||||||
|
using System.Xml;
|
||||||
|
|
||||||
|
namespace ANX.Framework.Content.Pipeline.Serialization
|
||||||
|
{
|
||||||
|
public static class XmlReaderExtensions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Checks if the current document contains the given element.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="xmlReader"></param>
|
||||||
|
/// <param name="name">Name of the element</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user