From 793fe6b89f6a12248fd878e9ce8fc49b25f08d23 Mon Sep 17 00:00:00 2001 From: trunghq3101 Date: Thu, 27 Oct 2022 21:22:26 +0700 Subject: [PATCH] fix: template not parsed --- .../tiled/lib/src/objects/tiled_object.dart | 11 ++--- packages/tiled/lib/src/parser.dart | 40 +++++++++++++++++-- packages/tiled/lib/src/tile_map_parser.dart | 11 ++++- packages/tiled/lib/src/tiled_map.dart | 1 + 4 files changed, 53 insertions(+), 10 deletions(-) diff --git a/packages/tiled/lib/src/objects/tiled_object.dart b/packages/tiled/lib/src/objects/tiled_object.dart index 239b1bb..2b8e006 100644 --- a/packages/tiled/lib/src/objects/tiled_object.dart +++ b/packages/tiled/lib/src/objects/tiled_object.dart @@ -44,7 +44,7 @@ part of tiled; /// Can contain at most one: , (since 0.9), /// (since 1.1), , , (since 1.0) class TiledObject { - int id; + int? id; String name; String type; int? gid; @@ -59,7 +59,7 @@ class TiledObject { bool point; bool rectangle; - Template? template; + Future? template; Text? text; bool visible; @@ -72,7 +72,7 @@ class TiledObject { String get class_ => type; TiledObject({ - required this.id, + this.id, this.name = '', this.type = '', this.gid, @@ -105,7 +105,7 @@ class TiledObject { final width = parser.getDouble('width', defaults: 0); final rotation = parser.getDouble('rotation', defaults: 0); final visible = parser.getBool('visible', defaults: true); - final id = parser.getInt('id'); + final id = parser.getIntOrNull('id'); final gid = parser.getIntOrNull('gid'); final name = parser.getString('name', defaults: ''); @@ -126,7 +126,8 @@ class TiledObject { (xml) => xml.getChildren('point').isNotEmpty, ); final text = parser.getSingleChildOrNullAs('text', Text.parse); - final template = parser.getSingleChildOrNullAs('template', Template.parse); + final template = + parser.getExternalPropertyOrNull('template', Template.parse); final properties = parser.getProperties(); final polygon = parsePointList(parser, 'polygon'); diff --git a/packages/tiled/lib/src/parser.dart b/packages/tiled/lib/src/parser.dart index 3cd278a..45d25b6 100644 --- a/packages/tiled/lib/src/parser.dart +++ b/packages/tiled/lib/src/parser.dart @@ -9,7 +9,12 @@ class ParsingException implements Exception { class XmlParser extends Parser { final XmlElement element; - XmlParser(this.element); + final Future Function(String key)? tsxProviderFunction; + + XmlParser( + this.element, { + this.tsxProviderFunction, + }); @override String? getStringOrNull(String name, {String? defaults}) { @@ -21,7 +26,7 @@ class XmlParser extends Parser { return element.children .whereType() .where((e) => e.name.local == name) - .map(XmlParser.new) + .map(_newChild) .toList(); } @@ -29,7 +34,7 @@ class XmlParser extends Parser { return element.children .whereType() .where((e) => names.contains(e.name.local)) - .map(XmlParser.new) + .map(_newChild) .toList(); } @@ -40,6 +45,28 @@ class XmlParser extends Parser { ) { return xml(this); } + + @override + Future getExternalPropertyOrNull( + String name, + T Function(Parser) parser, + ) async { + final fileName = getStringOrNull(name); + if (fileName == null) { + return null; + } + final tsxProvider = await tsxProviderFunction?.call(fileName); + final result = tsxProvider?.getCachedSource() ?? tsxProvider?.getSource(''); + if (result == null) { + return null; + } + return parser(result); + } + + XmlParser _newChild(XmlElement element) => XmlParser( + element, + tsxProviderFunction: tsxProviderFunction, + ); } class JsonParser extends Parser { @@ -255,4 +282,11 @@ abstract class Parser { } return result; } + + Future getExternalPropertyOrNull( + String name, + T Function(Parser) parser, + ) async { + return null; + } } diff --git a/packages/tiled/lib/src/tile_map_parser.dart b/packages/tiled/lib/src/tile_map_parser.dart index 7c9b032..b270b08 100644 --- a/packages/tiled/lib/src/tile_map_parser.dart +++ b/packages/tiled/lib/src/tile_map_parser.dart @@ -10,12 +10,19 @@ class TileMapParser { /// /// Accepts an optional list of external TsxProviders for external tilesets /// referenced in the map file. - static TiledMap parseTmx(String xml, {List? tsxList}) { + static TiledMap parseTmx( + String xml, { + List? tsxList, + Future Function(String key)? tsxProviderFunction, + }) { final xmlElement = XmlDocument.parse(xml).rootElement; if (xmlElement.name.local != 'map') { throw 'XML is not in TMX format'; } - final parser = XmlParser(xmlElement); + final parser = XmlParser( + xmlElement, + tsxProviderFunction: tsxProviderFunction, + ); return TiledMap.parse(parser, tsxList: tsxList); } } diff --git a/packages/tiled/lib/src/tiled_map.dart b/packages/tiled/lib/src/tiled_map.dart index e1a4e29..9b59282 100644 --- a/packages/tiled/lib/src/tiled_map.dart +++ b/packages/tiled/lib/src/tiled_map.dart @@ -145,6 +145,7 @@ class TiledMap { return TileMapParser.parseTmx( contents, tsxList: tsxProviders.isEmpty ? null : tsxProviders, + tsxProviderFunction: tsxProviderFunction, ); }