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); }
public static XElement GenerateRoot(TextureSetFile file) { var e = new XElement(Namespace + "tileset"); if (!string.IsNullOrEmpty(file.Name)) { e.Add(new XAttribute("name", file.Name)); } e.Add(new XAttribute("width", file.Width)); e.Add(new XAttribute("height", file.Height)); e.Add(new XAttribute("type", file.TileType)); var metaData = GenerateFormattingMetaData(file.FormattingMetaData); foreach (var pair in file.Properties) { if (!string.IsNullOrEmpty(pair.Key) && !string.IsNullOrEmpty(pair.Value)) { metaData.Add(new XElement(Namespace + "property", new XAttribute("name", pair.Key), pair.Value)); } } e.Add(metaData); foreach (var includeFile in file.IncludeFiles) { var fn = includeFile.SourcePath; if (fn != null) { e.Add(new XElement(Namespace + "include", new XAttribute("file", Path.GetRelativePath(file.BasePath, fn)))); } } e.AddRange(file.Collections.Select(GenerateCollection)); return(e); }
public static XDocument GenerateXml(TextureSetFile file) { return(new XDocument(new XDeclaration("1.0", "UTF-8", "yes"), GenerateRoot(file))); }