-
-
Notifications
You must be signed in to change notification settings - Fork 327
/
TiledMapTilesetImporter.cs
65 lines (55 loc) · 2.09 KB
/
TiledMapTilesetImporter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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(".tsx", DefaultProcessor = "TiledMapTilesetProcessor", DisplayName = "Tiled Map Tileset Importer - MonoGame.Extended")]
public class TiledMapTilesetImporter : ContentImporter<TiledMapTilesetContentItem>
{
public override TiledMapTilesetContentItem Import(string filePath, ContentImporterContext context)
{
try
{
if (filePath == null)
throw new ArgumentNullException(nameof(filePath));
ContentLogger.Logger = context.Logger;
ContentLogger.Log($"Importing '{filePath}'");
var tileset = DeserializeTiledMapTilesetContent(filePath, context);
ContentLogger.Log($"Imported '{filePath}'");
return new TiledMapTilesetContentItem(tileset);
}
catch (Exception e)
{
context.Logger.LogImportantMessage(e.StackTrace);
throw;
}
}
private TiledMapTilesetContent DeserializeTiledMapTilesetContent(string filePath, ContentImporterContext context)
{
using (var reader = new StreamReader(filePath))
{
var tilesetSerializer = new XmlSerializer(typeof(TiledMapTilesetContent));
var tileset = (TiledMapTilesetContent)tilesetSerializer.Deserialize(reader);
tileset.Image.Source = Path.Combine(Path.GetDirectoryName(filePath), tileset.Image.Source);
ContentLogger.Log($"Adding dependency '{tileset.Image.Source}'");
context.AddDependency(tileset.Image.Source);
foreach (var tile in tileset.Tiles)
{
foreach (var obj in tile.Objects)
{
if (!string.IsNullOrWhiteSpace(obj.TemplateSource))
{
obj.TemplateSource = Path.Combine(Path.GetDirectoryName(filePath), obj.TemplateSource);
ContentLogger.Log($"Adding dependency '{obj.TemplateSource}'");
// We depend on the template.
context.AddDependency(obj.TemplateSource);
}
}
}
return tileset;
}
}
}
}