Files
MonoGame.Extended/Source/MonoGame.Extended.Content.Pipeline/Tiled/TiledMapObjectTemplateImporter.cs
T
Dylan Wilson e962cec448 A mix between Tiled and GUI stuff (#553)
* mostly restored the tiled importer unit tests

* code cleanup

* code cleanup

* almost got tiled maps serialization independent of the content pipeline

* refactored the tiled map content importer so that tiled maps can be loaded at runtime

* code cleanup

* removed nuspec files

* experimenting with an XML based GUI markup parser

* a few more markup parser features

* finally got a working open file dialog

* experimenting with markup bindings

* attached properties

* working on the gui stuff again

* working on a better textbox

* multiline textboxing

* new text box is really coming along

* removed the content explorer experiment

* restored the old gui demo back to the way it was before (for now)
2018-10-09 22:18:48 +10:00

55 lines
1.7 KiB
C#

using Microsoft.Xna.Framework.Content.Pipeline;
using System;
using System.IO;
using System.Xml.Serialization;
using MonoGame.Extended.Tiled.Serialization;
namespace MonoGame.Extended.Content.Pipeline.Tiled
{
[ContentImporter(".tx", DefaultProcessor = "TiledMapObjectTemplateProcessor", DisplayName = "Tiled Map Object Template Importer - MonoGame.Extended")]
public class TiledMapObjectTemplateImporter : ContentImporter<TiledMapObjectTemplateContent>
{
public override TiledMapObjectTemplateContent Import(string filePath, ContentImporterContext context)
{
try
{
if (filePath == null)
throw new ArgumentNullException(nameof(filePath));
ContentLogger.Logger = context.Logger;
ContentLogger.Log($"Importing '{filePath}'");
var template = DeserializeTileMapObjectTemplateContent(filePath, context);
ContentLogger.Log($"Imported '{filePath}'");
return template;
}
catch (Exception e)
{
context.Logger.LogImportantMessage(e.StackTrace);
return null;
}
}
private static TiledMapObjectTemplateContent DeserializeTileMapObjectTemplateContent(string filePath, ContentImporterContext context)
{
using (var reader = new StreamReader(filePath))
{
var templateSerializer = new XmlSerializer(typeof(TiledMapObjectTemplateContent));
var template = (TiledMapObjectTemplateContent)templateSerializer.Deserialize(reader);
if (!string.IsNullOrWhiteSpace(template.Tileset?.Source))
{
template.Tileset.Source = $"{Path.GetDirectoryName(filePath)}/{template.Tileset.Source}";
ContentLogger.Log($"Adding dependency '{template.Tileset.Source}'");
// We depend on this tileset.
context.AddDependency(template.Tileset.Source);
}
return template;
}
}
}
}