Esempio n. 1
0
        private static TiledMapContent DeserializeTiledMapContent(string mapFilePath, ContentImporterContext context)
        {
            using (var reader = new StreamReader(mapFilePath))
            {
                var mapSerializer = new XmlSerializer(typeof(TiledMapContent));
                var map           = (TiledMapContent)mapSerializer.Deserialize(reader);

                map.FilePath = mapFilePath;

                for (var i = 0; i < map.Tilesets.Count; i++)
                {
                    var tileset = map.Tilesets[i];

                    if (!string.IsNullOrWhiteSpace(tileset.Source))
                    {
                        tileset.Source = Path.Combine(Path.GetDirectoryName(mapFilePath), tileset.Source);
                        ContentLogger.Log($"Adding dependency for {tileset.Source}");
                        // We depend on the tileset. If the tileset changes, the map also needs to rebuild.
                        context.AddDependency(tileset.Source);
                    }
                    else
                    {
                        tileset.Image.Source = Path.Combine(Path.GetDirectoryName(mapFilePath), tileset.Image.Source);
                        ContentLogger.Log($"Adding dependency for {tileset.Image.Source}");
                        context.AddDependency(tileset.Image.Source);
                    }
                }

                ImportLayers(context, map.Layers, Path.GetDirectoryName(mapFilePath));

                map.Name = mapFilePath;
                return(map);
            }
        }
Esempio n. 2
0
        private static void ImportLayers(ContentImporterContext context, List <TiledMapLayerContent> layers, string path)
        {
            for (var i = 0; i < layers.Count; i++)
            {
                if (layers[i] is TiledMapImageLayerContent imageLayer)
                {
                    imageLayer.Image.Source = Path.Combine(path, imageLayer.Image.Source);
                    ContentLogger.Log($"Adding dependency for '{imageLayer.Image.Source}'");

                    // Tell the pipeline that we depend on this image and need to rebuild the map if the image changes.
                    // (Maybe the image is a different size)
                    context.AddDependency(imageLayer.Image.Source);
                }
                if (layers[i] is TiledMapObjectLayerContent objectLayer)
                {
                    foreach (var obj in objectLayer.Objects)
                    {
                        if (!String.IsNullOrWhiteSpace(obj.TemplateSource))
                        {
                            obj.TemplateSource = Path.Combine(path, obj.TemplateSource);
                            ContentLogger.Log($"Adding dependency for '{obj.TemplateSource}'");
                            // Tell the pipeline that we depend on this template and need to rebuild the map if the template changes.
                            // (Templates are loaded into objects on process, so all objects which depend on the template file
                            //  need the change to the template)
                            context.AddDependency(obj.TemplateSource);
                        }
                    }
                }
                if (layers[i] is TiledMapGroupLayerContent groupLayer)
                {
                    // Yay recursion!
                    ImportLayers(context, groupLayer.Layers, path);
                }
            }
        }
        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.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.GetDirectoryName(filePath)}/{obj.TemplateSource}";
                            ContentLogger.Log($"Adding dependency '{obj.TemplateSource}'");

                            // We depend on the template.
                            context.AddDependency(obj.TemplateSource);
                        }
                    }
                }

                return(tileset);
            }
        }
Esempio n. 4
0
        public override TiledMapContentItem Import(string filePath, ContentImporterContext context)
        {
            try
            {
                if (filePath == null)
                {
                    throw new ArgumentNullException(nameof(filePath));
                }

                ContentLogger.Logger = context.Logger;
                ContentLogger.Log($"Importing '{filePath}'");

                var map = DeserializeTiledMapContent(filePath, context);

                if (map.Width > ushort.MaxValue || map.Height > ushort.MaxValue)
                {
                    throw new InvalidContentException($"The map '{filePath} is much too large. The maximum supported width and height for a Tiled map is {ushort.MaxValue}.");
                }

                ContentLogger.Log($"Imported '{filePath}'");

                return(new TiledMapContentItem(map));
            }
            catch (Exception e)
            {
                context.Logger.LogImportantMessage(e.StackTrace);
                throw;
            }
        }
        public override TiledMapContent Process(TiledMapContent map, ContentProcessorContext context)
        {
            ContentLogger.Logger = context.Logger;

            var previousWorkingDirectory = Environment.CurrentDirectory;
            var newWorkingDirectory      = Path.GetDirectoryName(map.FilePath);

            if (string.IsNullOrEmpty(newWorkingDirectory))
            {
                throw new NullReferenceException();
            }
            Environment.CurrentDirectory = newWorkingDirectory;

            foreach (var layer in map.Layers)
            {
                var imageLayer = layer as TiledMapImageLayerContent;
                if (imageLayer != null)
                {
                    ContentLogger.Log($"Processing image layer '{imageLayer.Name}'");

                    var model = CreateImageLayerModel(imageLayer);
                    layer.Models = new[]
                    {
                        model
                    };

                    ContentLogger.Log($"Processed image layer '{imageLayer.Name}'");
                    continue;
                }

                var tileLayer = layer as TiledMapTileLayerContent;
                if (tileLayer == null)
                {
                    continue;
                }

                var data            = tileLayer.Data;
                var encodingType    = data.Encoding ?? "xml";
                var compressionType = data.Compression ?? "xml";

                ContentLogger.Log(
                    $"Processing tile layer '{tileLayer.Name}': Encoding: '{encodingType}', Compression: '{compressionType}'");

                var tileData = DecodeTileLayerData(encodingType, tileLayer);
                var tiles    = CreateTiles(map.RenderOrder, map.Width, map.Height, tileData);
                tileLayer.Tiles = tiles;

                layer.Models = CreateTileLayerModels(map, tileLayer.Name, tiles).ToArray();

                ContentLogger.Log($"Processed tile layer '{tileLayer}': {tiles.Length} tiles");
            }

            Environment.CurrentDirectory = previousWorkingDirectory;

            return(map);
        }
        private static void ProcessLayers(TiledMapContentItem contentItem, TiledMapContent map, ContentProcessorContext context, List <TiledMapLayerContent> layers)
        {
            foreach (var layer in layers)
            {
                switch (layer)
                {
                case TiledMapImageLayerContent imageLayer:
                    ContentLogger.Log($"Processing image layer '{imageLayer.Name}'");
                    //var externalReference = new ExternalReference<Texture2DContent>(imageLayer.Image.Source);
                    var parameters = new OpaqueDataDictionary
                    {
                        { "ColorKeyColor", imageLayer.Image.TransparentColor },
                        { "ColorKeyEnabled", true }
                    };
                    //imageLayer.Image.ContentRef = context.BuildAsset<Texture2DContent, Texture2DContent>(externalReference, "", parameters, "", "");
                    contentItem.BuildExternalReference <Texture2DContent>(context, imageLayer.Image.Source, parameters);
                    ContentLogger.Log($"Processed image layer '{imageLayer.Name}'");
                    break;

                case TiledMapTileLayerContent tileLayer when tileLayer.Data.Chunks.Count > 0:
                    throw new NotSupportedException($"{map.FilePath} contains data chunks. These are currently not supported.");

                case TiledMapTileLayerContent tileLayer:
                    var data            = tileLayer.Data;
                    var encodingType    = data.Encoding ?? "xml";
                    var compressionType = data.Compression ?? "xml";

                    ContentLogger.Log($"Processing tile layer '{tileLayer.Name}': Encoding: '{encodingType}', Compression: '{compressionType}'");
                    var tileData = DecodeTileLayerData(encodingType, tileLayer);
                    var tiles    = CreateTiles(map.RenderOrder, map.Width, map.Height, tileData);
                    tileLayer.Tiles = tiles;
                    ContentLogger.Log($"Processed tile layer '{tileLayer}': {tiles.Length} tiles");
                    break;

                case TiledMapObjectLayerContent objectLayer:
                    ContentLogger.Log($"Processing object layer '{objectLayer.Name}'");

                    foreach (var obj in objectLayer.Objects)
                    {
                        TiledMapContentHelper.Process(obj, context);
                    }

                    ContentLogger.Log($"Processed object layer '{objectLayer.Name}'");
                    break;

                case TiledMapGroupLayerContent groupLayer:
                    ProcessLayers(contentItem, map, context, groupLayer.Layers);
                    break;
                }
            }
        }
Esempio n. 7
0
        private static TiledMapTilesetContent ImportTileset(string tilesetFilePath, XmlSerializer tilesetSerializer, TiledMapTilesetContent tileset)
        {
            TiledMapTilesetContent result;

            ContentLogger.Log($"Importing tileset '{tilesetFilePath}'");

            using (var file = new FileStream(tilesetFilePath, FileMode.Open))
            {
                var importedTileset = (TiledMapTilesetContent)tilesetSerializer.Deserialize(file);
                importedTileset.FirstGlobalIdentifier = tileset.FirstGlobalIdentifier;
                result = importedTileset;
            }

            ContentLogger.Log($"Imported tileset '{tilesetFilePath}'");

            return(result);
        }
        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.Combine(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);
            }
        }
        private static TiledMapContent DeserializeTiledMapContent(string filePath)
        {
            using (var reader = new StreamReader(filePath))
            {
                var mapSerializer = new XmlSerializer(typeof(TiledMapContent));
                var map           = (TiledMapContent)mapSerializer.Deserialize(reader);

                map.FilePath = filePath;

                var tilesetSerializer = new XmlSerializer(typeof(TiledMapTilesetContent));

                for (var i = 0; i < map.Tilesets.Count; i++)
                {
                    var tileset = map.Tilesets[i];

                    if (string.IsNullOrWhiteSpace(tileset.Source))
                    {
                        continue;
                    }
                    var directoryName   = Path.GetDirectoryName(filePath);
                    var tilesetLocation = tileset.Source.Replace('/', Path.DirectorySeparatorChar);

                    Debug.Assert(directoryName != null, "directoryName != null");
                    var tilesetFilePath = Path.Combine(directoryName, tilesetLocation);

                    ContentLogger.Log($"Importing tileset '{tilesetFilePath}'");

                    using (var file = new FileStream(tilesetFilePath, FileMode.Open))
                    {
                        map.Tilesets[i] = (TiledMapTilesetContent)tilesetSerializer.Deserialize(file);
                    }

                    ContentLogger.Log($"Imported tileset '{tilesetFilePath}'");
                }

                map.Name = filePath;

                return(map);
            }
        }
        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.
                //var externalReference = new ExternalReference<Texture2DContent>(tileset.Image.Source);
                var parameters = new OpaqueDataDictionary
                {
                    { "ColorKeyColor", tileset.Image.TransparentColor },
                    { "ColorKeyEnabled", true }
                };
                //tileset.Image.ContentRef = context.BuildAsset<Texture2DContent, Texture2DContent>(externalReference, "", parameters, "", "");
                contentItem.BuildExternalReference <Texture2DContent>(context, tileset.Image.Source, parameters);

                foreach (var tile in tileset.Tiles)
                {
                    foreach (var obj in tile.Objects)
                    {
                        TiledMapContentHelper.Process(obj, context);
                    }
                }

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

                return(contentItem);
            }
            catch (Exception ex)
            {
                context.Logger.LogImportantMessage(ex.Message);
                throw ex;
            }
        }
        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;
            }
        }