Style changes, and usage of embedded resources

This commit is contained in:
krnlException 2024-09-11 17:11:24 +02:00
parent eb22de169c
commit 44cbf5b90a
5 changed files with 53 additions and 25 deletions

View file

@ -26,4 +26,13 @@
</None> </None>
</ItemGroup> </ItemGroup>
<ItemGroup>
<None Remove="embedded-tilemap.tmx" />
<EmbeddedResource Include="embedded-tilemap.tmx" />
<None Remove="embedded-tileset.png" />
<EmbeddedResource Include="embedded-tileset.png" />
<None Remove="embedded-tileset.tsx" />
<EmbeddedResource Include="embedded-tileset.tsx" />
</ItemGroup>
</Project> </Project>

View file

@ -1,10 +1,11 @@
using DotTiled.Serialization; using System.Reflection;
using DotTiled.Serialization;
namespace DotTiled.Example; namespace DotTiled.Example;
class Program class Program
{ {
static void Main(string[] args) private static void Main()
{ {
Quick(); Quick();
Manual(); Manual();
@ -12,7 +13,7 @@ class Program
// QUICK START // QUICK START
// Automatic and easy way to load tilemaps. // Automatic and easy way to load tilemaps.
static void Quick() private static void Quick()
{ {
var loader = Loader.Default(); var loader = Loader.Default();
var map = loader.LoadMap("tilemap.tmx"); var map = loader.LoadMap("tilemap.tmx");
@ -25,7 +26,7 @@ class Program
// MANUAL // MANUAL
// Manually load a map, if you need to load from a custom source // Manually load a map, if you need to load from a custom source
static void Manual() private static void Manual()
{ {
using var mapFileReader = new StreamReader("tilemap.tmx"); using var mapFileReader = new StreamReader("tilemap.tmx");
var mapString = mapFileReader.ReadToEnd(); var mapString = mapFileReader.ReadToEnd();
@ -33,7 +34,7 @@ class Program
var map = mapReader.ReadMap(); var map = mapReader.ReadMap();
// Now do some other stuff with it... // Now do some other stuff with it...
StringProperty hello = (StringProperty)map.Properties.FirstOrDefault(property => property.Name == "hello"); StringProperty hello = map.GetProperty<StringProperty>("hello");
Console.WriteLine($"Layer 1 name: {map.Layers[0].Name}"); Console.WriteLine($"Layer 1 name: {map.Layers[0].Name}");
Console.WriteLine($"Property 'hello': {hello.Value}"); Console.WriteLine($"Property 'hello': {hello.Value}");
@ -43,36 +44,38 @@ class Program
Console.WriteLine($"Tileset 0 image 0 source: {tileset.Image.Value.Source.Value}"); Console.WriteLine($"Tileset 0 image 0 source: {tileset.Image.Value.Source.Value}");
} }
/* This function is responsible for loading all tilesets required by a tilemap, if you // This function is responsible for loading all tilesets required by a tilemap, if you
want to use a custom source. */ // want to use a custom source.
static Tileset ResolveTileset(string source) private static Tileset ResolveTileset(string source)
{ {
string tilesetPath = Path.Combine(Directory.GetCurrentDirectory(), source); // Resolve path to a tileset. // Read a file from assembly
using var tilesetFileReader = new StreamReader(tilesetPath); // Read tileset file itself. // You can use any other source for files, eg. compressed archive, or even file from internet.
var tilesetString = tilesetFileReader.ReadToEnd(); // You can replace this with any custom function using Stream? tilesetStream = Assembly.GetExecutingAssembly().GetManifestResourceStream($"DotTiled.Example.embedded-{source}")
// to load data from any source, eg. .zip file. ?? throw new FileLoadException($"{source} not found in assembly.");
using var tilesetReader = new TilesetReader(tilesetString, ResolveTileset, ResolveTemplate, ResolveCustomType); // Parse loaded tileset. string tilesetString = new StreamReader(tilesetStream).ReadToEnd();
using TilesetReader tilesetReader = new TilesetReader(tilesetString, ResolveTileset, ResolveTemplate, ResolveCustomType); // Parse loaded tileset.
return tilesetReader.ReadTileset(); // Return loaded tileset return tilesetReader.ReadTileset(); // Return loaded tileset
} }
// This is pretty similar to above, but instead it loads templates, not tilesets. // This is pretty similar to above, but instead it loads templates, not tilesets.
static Template ResolveTemplate(string source) private static Template ResolveTemplate(string source)
{ {
string templatePath = Path.Combine(Directory.GetCurrentDirectory(), source); using Stream? templateStream = Assembly.GetExecutingAssembly().GetManifestResourceStream($"DotTiled.Example.{source}")
using var templateFileReader = new StreamReader(templatePath); ?? throw new FileLoadException($"{source} not found in assembly.");
var templateString = templateFileReader.ReadToEnd(); string templateString = new StreamReader(templateStream).ReadToEnd();
using var templateReader = new TemplateReader(templateString, ResolveTileset, ResolveTemplate, ResolveCustomType);
using TemplateReader templateReader = new TemplateReader(templateString, ResolveTileset, ResolveTemplate, ResolveCustomType);
return templateReader.ReadTemplate(); return templateReader.ReadTemplate();
} }
static ICustomTypeDefinition ResolveCustomType(string name) private static ICustomTypeDefinition ResolveCustomType(string name)
{ {
CustomClassDefinition[] allDefinedTypes = ICustomTypeDefinition[] allDefinedTypes =
[ [
new CustomClassDefinition() { Name = "a" }, new CustomClassDefinition() { Name = "a" },
]; ];
return allDefinedTypes.FirstOrDefault(type => type.Name == name); return allDefinedTypes.FirstOrDefault(type => type.Name == name) ?? throw new InvalidOperationException();
} }
} }

View file

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.10" tiledversion="1.11.0" orientation="orthogonal" renderorder="right-down" width="10" height="10" tilewidth="16" tileheight="16" infinite="0" nextlayerid="5" nextobjectid="1">
<properties>
<property name="hello" value="Hello from DotTiled!"/>
</properties>
<tileset firstgid="1" source="tileset.tsx"/>
<layer id="1" name="Tile Layer 1" width="10" height="10">
<data encoding="base64">
AQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAA==
</data>
</layer>
</map>

Binary file not shown.

After

Width:  |  Height:  |  Size: 149 B

View file

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<tileset version="1.8" tiledversion="1.10.2" name="tileset" tilewidth="16" tileheight="16" tilecount="1" columns="1">
<image source="tileset.png" width="16" height="16"/>
</tileset>