summaryrefslogtreecommitdiff
path: root/Plugins/MonoGame.Extended/source/MonoGame.Extended.Content.Pipeline/Tiled/TiledMapTilesetProcessor.cs
blob: 5682602f91dd64311ea28fef856fce728929624a (plain)
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
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
using System;

namespace MonoGame.Extended.Content.Pipeline.Tiled
{
	[ContentProcessor(DisplayName = "Tiled Map Tileset Processor - MonoGame.Extended")]
	public class TiledMapTilesetProcessor : ContentProcessor<TiledMapTilesetContentItem, TiledMapTilesetContentItem>
	{
		public override TiledMapTilesetContentItem Process(TiledMapTilesetContentItem contentItem, ContentProcessorContext context)
		{
			try
			{
			    var tileset = contentItem.Data;

			    ContentLogger.Logger = context.Logger;
				ContentLogger.Log($"Processing tileset '{tileset.Name}'");

				// Build the Texture2D asset and load it as it will be saved as part of this tileset file.
                if (tileset.Image is not null)
                    contentItem.BuildExternalReference<Texture2DContent>(context, tileset.Image);

				foreach (var tile in tileset.Tiles)
				{
				    foreach (var obj in tile.Objects)
				    {
				        TiledMapContentHelper.Process(obj, context);
				    }
                    if (tile.Image is not null)
                        contentItem.BuildExternalReference<Texture2DContent>(context, tile.Image);
				}

			    ContentLogger.Log($"Processed tileset '{tileset.Name}'");

				return contentItem;
			}
			catch (Exception ex)
			{
				context.Logger.LogImportantMessage(ex.Message);
				throw ex;
			}
		}
	}
}