TiledMapObjectTemplateImporter.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System;
  2. using System.IO;
  3. using System.Xml.Serialization;
  4. using Microsoft.Xna.Framework.Content.Pipeline;
  5. using MonoGame.Extended.Content.Tiled;
  6. namespace MonoGame.Extended.Content.Pipeline.Tiled
  7. {
  8. [ContentImporter(".tx", DefaultProcessor = "TiledMapObjectTemplateProcessor", DisplayName = "Tiled Map Object Template Importer - MonoGame.Extended")]
  9. public class TiledMapObjectTemplateImporter : ContentImporter<TiledMapObjectTemplateContent>
  10. {
  11. public override TiledMapObjectTemplateContent Import(string filePath, ContentImporterContext context)
  12. {
  13. try
  14. {
  15. if (filePath == null)
  16. throw new ArgumentNullException(nameof(filePath));
  17. ContentLogger.Logger = context.Logger;
  18. ContentLogger.Log($"Importing '{filePath}'");
  19. var template = DeserializeTileMapObjectTemplateContent(filePath, context);
  20. ContentLogger.Log($"Imported '{filePath}'");
  21. return template;
  22. }
  23. catch (Exception e)
  24. {
  25. context.Logger.LogImportantMessage(e.StackTrace);
  26. return null;
  27. }
  28. }
  29. private static TiledMapObjectTemplateContent DeserializeTileMapObjectTemplateContent(string filePath, ContentImporterContext context)
  30. {
  31. using (var reader = new StreamReader(filePath))
  32. {
  33. var templateSerializer = new XmlSerializer(typeof(TiledMapObjectTemplateContent));
  34. var template = (TiledMapObjectTemplateContent)templateSerializer.Deserialize(reader);
  35. if (!string.IsNullOrWhiteSpace(template.Tileset?.Source))
  36. {
  37. template.Tileset.Source = Path.Combine(Path.GetDirectoryName(filePath), template.Tileset.Source);
  38. ContentLogger.Log($"Adding dependency '{template.Tileset.Source}'");
  39. // We depend on this tileset.
  40. context.AddDependency(template.Tileset.Source);
  41. }
  42. return template;
  43. }
  44. }
  45. }
  46. }