static TextureSetFile ReadInternal(XElement root, string?documentPath, HashSet <string>?previousReads = null) { previousReads ??= new HashSet <string>(); if (documentPath != null) { if (previousReads.Contains(documentPath)) { throw new InvalidOperationException("Circular reference in include files detected"); } } var defaultName = "Unnamed"; string basePath; if (!string.IsNullOrWhiteSpace(documentPath)) { defaultName = Path.GetFileNameWithoutExtension(Path.GetFileName(documentPath)); basePath = Path.GetDirectoryName(documentPath) ?? Environment.CurrentDirectory; } else { basePath = Environment.CurrentDirectory; } var width = (int?)root.AttributeLocal("width") ?? 32; var height = (int?)root.AttributeLocal("height") ?? 32; var textureType = TextureParserExtensions.ParseTextureType(root, (string?)root.AttributeLocal("type"), TileType.Grid); var name = root.AttributeLocal("name")?.Value ?? defaultName; var loaderContext = new TexturePackLoaderContext(basePath, textureType, width, height); var collections = ReadContent(root, loaderContext); var includes = ParseIncludeFiles(root); var metaData = root.ElementLocal("metadata"); var file = new TextureSetFile { Name = name, Width = width, Height = height, TileType = textureType, Properties = ParseMetaDataProperties(metaData) }; var x = includes.Select(f => ParseIncludeFile(previousReads, f, basePath)); foreach (var includedFile in x) { if (includedFile != null) { file.IncludeFiles.Add(includedFile); } } file.Collections.AddRange(collections); file.SourcePath = documentPath; ParseFormattingInfo(root, file.FormattingMetaData); return(file); }
IEnumerable <ITextureFile <TTile> > ReadContent(XElement root, TexturePackLoaderContext context, HashSet <FilePath> path) { var includes = from e in root.Elements() where e.Name.LocalName == "include" where e.AttributeLocal("file") != null select ReadInclude(e, context, path); var c = from e2 in root.Elements() where e2.Name.LocalName == "collection" select ReadCollection(e2, context); var retval = new List <ITextureFile <TTile> >(); foreach (var include in includes) { retval.AddRange(include); } retval.AddRange(c); return(retval); }
static IEnumerable <TileTextureCollection> ReadContent(XElement root, TexturePackLoaderContext context) { var collections = from e in root.Elements() where e.Name.LocalName == "collection" select ReadCollection(e, context); return(collections); }
public static TextureGrid ReadGrid(XElement grid, TexturePackLoaderContext context) { var name = (string?)grid.AttributeLocal("name") ?? "Unnamed Grid"; var x = (int?)grid.AttributeLocal("x") ?? throw new XmlParseException("Required attribute 'x' not found", grid); var y = (int?)grid.AttributeLocal("y") ?? throw new XmlParseException("Required attribute 'y' not found", grid); var width = (int?)grid.AttributeLocal("cell-width") ?? (int?)grid.AttributeLocal("width"); var height = (int?)grid.AttributeLocal("cell-height") ?? (int?)grid.AttributeLocal("height"); var border = (int?)grid.AttributeLocal("cell-spacing") ?? (int?)grid.AttributeLocal("border") ?? 0; var anchorX = (int?)grid.AttributeLocal("anchor-x"); var anchorY = (int?)grid.AttributeLocal("anchor-y"); var gridValue = new TextureGrid { Name = name, X = x, Y = y, CellWidth = width, CellHeight = height, AnchorX = anchorX, AnchorY = anchorY, CellSpacing = border }; var metadata = grid.ElementLocal("metadata"); if (metadata != null) { gridValue.MatcherType = ParseMatchType(metadata, (string?)metadata.AttributeLocal("matcher-type"), MatcherType.Basic); gridValue.Pattern = (string?)metadata.AttributeLocal("pattern"); gridValue.Width = (int?)metadata.AttributeLocal("grid-width"); gridValue.Height = (int?)metadata.AttributeLocal("grid-height"); ProcessCellMapElements(gridValue, metadata); } var tiles = from e in grid.Elements() where e.Name.LocalName == "tile" select ReadTiles(e); gridValue.Tiles.AddRange(tiles); ParseFormattingInfo(grid, gridValue.FormattingMetaData); return(gridValue); }
public static TileTextureCollection ReadCollection(XElement root, TexturePackLoaderContext context) { var id = (string?)root.AttributeLocal("id") ?? "Unnamed Collection"; var grids = from e in root.Elements() where e.Name.LocalName == "grid" select ReadGrid(e, context); var retval = new TileTextureCollection(); retval.Id = id; retval.Grids.AddRange(grids); ParseFormattingInfo(root, retval.FormattingMetaData); var lastExportLocation = (string?)root.ElementLocal("metadata")?.Attribute("last-export-location"); retval.LastExportLocation = lastExportLocation; return(retval); }
IEnumerable <ITextureFile <TTile> > ReadInclude(XElement includeDirective, TexturePackLoaderContext context, HashSet <FilePath> path) { var file = (string)includeDirective.AttributeLocal("file"); if (file == null) { return(new List <ITextureFile <TTile> >()); } var targetPath = fileSystem.CombinePath(context.BasePath, file); if (path.Contains(targetPath)) { throw new TexturePackLoaderException( $"Circular reference in include files or duplicate include while evaluating path {targetPath}", includeDirective); } path.Add(targetPath); using (var stream = fileSystem.Read(targetPath)) { var doc = XDocument.Load(stream); var root = doc.Root; var width = (int?)root.AttributeLocal("width") ?? context.Width; var height = (int?)root.AttributeLocal("height") ?? context.Height; var textureType = ParseTextureType((string)root.AttributeLocal("type"), context.TextureType); if (textureType != context.TextureType) { throw new TexturePackLoaderException( $"Include file '{targetPath}' is using a '{textureType}' tile type.", includeDirective); } var loaderContext = context.Create(width, height); return(ReadContent(root, loaderContext, path)); } }
ITextureFile <TTile> ReadCollection(XElement c, TexturePackLoaderContext context) { var image = c.AttributeLocal("id"); if (image == null) { throw new Exception(); } var textureName = (string)fileSystem.CombinePath(context.BasePath, image.Value); var grids = from e in c.Elements() where e.Name.LocalName == "grid" select ParseGrid(e, context); return(new GridTextureFile <TTile, TTexture, TRawTexture>(textureName, new IntDimension(context.Width, context.Height), tileProducer, contentLoader, grids.ToArray())); }
TileGrid ParseGrid(XElement grid, TexturePackLoaderContext context) { var halfCell = (bool?)grid.Attribute("half-cell-hint") ?? false; var defaultWidth = halfCell ? context.Width / 2 : context.Width; var defaultHeight = halfCell ? context.Height / 2 : context.Height; var x = (int)grid.AttributeLocal("x"); var y = (int)grid.AttributeLocal("y"); var width = (int?)grid.AttributeLocal("cell-width") ?? (int?)grid.AttributeLocal("width") ?? defaultWidth; var height = (int?)grid.AttributeLocal("cell-height") ?? (int?)grid.AttributeLocal("height") ?? defaultHeight; var anchorX = (int?)grid.AttributeLocal("anchor-x") ?? width / 2; var anchorY = (int?)grid.AttributeLocal("anchor-y") ?? height - defaultHeight / 2; var border = (int?)grid.AttributeLocal("cell-spacing") ?? (int?)grid.AttributeLocal("border") ?? 0; var tiles = from e in grid.Elements() where e.Name.LocalName == "tile" select ParseTile(e); return(new TileGrid(width, height, x, y, anchorX, anchorY, border, border, tiles.ToArray())); }