Content Compiler:

- Fixed Exception when Importer not set in loaded project.
This commit is contained in:
SND\eagleeyestudios_cp 2012-09-29 16:17:59 +00:00 committed by Konstantin Koch
parent 3d7ad3ae8f
commit a07d5c69b6
2 changed files with 89 additions and 17 deletions

View File

@ -1,13 +1,15 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Xml;
using ANX.Framework.Content.Pipeline.Serialization;
using ANX.Framework.NonXNA.Development;
using ANX.Framework.NonXNA.Reflection;
namespace ANX.Framework.Content.Pipeline.Importer
{
[ContentImporter("ANX XML Importer", ".xml")]
[ContentImporter(new string[] {".xml"})]
[Developer("SilentWarrior / Eagle Eye Studios")]
[PercentageComplete(20)]
public class XmlImporter : ContentImporter<object>
@ -25,7 +27,8 @@ namespace ANX.Framework.Content.Pipeline.Importer
//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>\"?");
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);
@ -39,32 +42,88 @@ namespace ANX.Framework.Content.Pipeline.Importer
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;
xml.ReadStartElement();
if (xml.MoveToContent() == XmlNodeType.Element && xml.Name == "Asset")
{
xml.MoveToAttribute("Type");
logger.LogMessage("Trying to read a type from the Xml file");
//Check every assembly in the current working dir for type
foreach (
var file in
Directory.GetFiles(Path.GetDirectoryName(Assembly.GetCallingAssembly().Location), "*.dll"))
{
try
{
Assembly assembly = Assembly.LoadFrom(file);
foreach (Type type in TypeHelper.SafelyExtractTypesFrom(assembly))
{
if (type.FullName == xml.ReadContentAsString())
{
logger.LogImportantMessage("Type is" + type);
return type;
}
}
}
catch (Exception ex)
{
System.Diagnostics.Debugger.Break(); //Go and check whats wrong
}
}
}
throw new InvalidContentException(
"Error during deserialization: Type is null. Is there a valid Type attribute in the Asset section of your Xml file and can the assembly conataining the type be found?");
}
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>();
string lastNode = "";
bool isDict = false;
Dictionary<string, object> dict = null;
string attrib = "";
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!
if (reader.Name != "Item" && reader.Name != "Key" && reader.Name != "Value" && reader.NodeType == XmlNodeType.Element)
{
//Is it really empty? Maybe it's a list or a dictionary?
System.Diagnostics.Debugger.Break();
if (isDict)
isDict = false;
if (!reader.IsEmptyElement)
{
try
{
props.Add(reader.Name, reader.ReadElementContentAsObject());
}
catch (Exception ex)
{
logger.LogWarning("", null, "Could not add element to properties.", ex);
}
}
}
else
props.Add(reader.Name, reader.ReadElementContentAsObject());
}
{
if (reader.Name == "Item" && reader.NodeType == XmlNodeType.Element)
{
if (!isDict)
{
isDict = true;
dict = new Dictionary<string, object>();
props.Add(lastNode, dict);
}
reader.ReadStartElement();
reader.ReadStartElement();
var key = reader.ReadContentAsString();
reader.ReadEndElement();
var typeBlah = ResolveType(attrib);
var value = reader.ReadContentAs(typeBlah, null);
reader.ReadEndElement();
dict.Add(key, value);
}
if (reader.NodeType == XmlNodeType.Attribute && reader.Name == "Type")
attrib = reader.ReadContentAsString();
}
lastNode = reader.Name;
reader.Read();
}
System.Diagnostics.Debugger.Break();
@ -80,5 +139,11 @@ namespace ANX.Framework.Content.Pipeline.Importer
//Return the whole construct
return result;
}
private Type ResolveType(string type)
{
return Type.GetType(type, true);
}
}
}

View File

@ -258,6 +258,13 @@ namespace ANX.ContentCompiler.GUI
TargetProfile = _contentProject.Profile,
CompressContent = false
};
foreach (var bI in _contentProject.BuildItems)
{
if (String.IsNullOrEmpty(bI.ImporterName))
{
bI.ImporterName = ImporterManager.GuessImporterByFileExtension(bI.SourceFilename);
}
}
try
{
foreach (var dir in _contentProject.BuildItems.Select(buildItem => Path.GetDirectoryName(buildItem.OutputFilename)).Where(dir => !Directory.Exists(dir)))