Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: TsxProviderBase with test function #72

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,15 @@ Load a TMX file into a string by any means, and then pass the string to TileMapP
final TiledMap mapTmx = TileMapParser.parseTmx(tmxBody);
```

If your tmx file includes a external tsx reference, you have to add a CustomParser
If your tmx file includes a external tsx reference, you have to add a CustomParser. This can either be done by using extending the TsxProviderBase, which can match multiple files, or by extending TsxProvider, which only matches on file by its name.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
If your tmx file includes a external tsx reference, you have to add a CustomParser. This can either be done by using extending the TsxProviderBase, which can match multiple files, or by extending TsxProvider, which only matches on file by its name.
If your tmx file includes a external tsx reference, you have to add a CustomParser. This can either be done by extending the TsxProviderBase, which can match multiple files, or by extending TsxProvider, which only matches on file by its name.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops missed that on 😅

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like we don't have the markdown linter activated here, the max line length on markdown should be 100.

```dart
class CustomTsxProvider extends TsxProvider {
class MultipleTsxProvider extends TsxProviderBase {
@override
bool checkProvidable(String filename) => ["external1.tsx", "external2.tsx"].contains(filename);

@override
Parser? getCachedSource(String filename) => null;

@override
Parser getSource(String fileName) {
final xml = File(fileName).readAsStringSync();
Expand All @@ -40,10 +46,27 @@ class CustomTsxProvider extends TsxProvider {
}
}
```
And use it in the parseTmx method

```dart
class SingleTsxProvider extends TsxProvider {
@override
String get filename => "external.tsx";

@override
Parser? getCachedSource() => null;

@override
Parser getSource(String _) {
final xml = File(filename).readAsStringSync();
final node = XmlDocument.parse(xml).rootElement;
return XmlParser(node);
}
}
```
And use it in the parseTmx method. Keep in mind that the first TsxProvider that can provide a source is used!
```dart
final String tmxBody = /* ... */;
final TiledMap mapTmx = TileMapParser.parseTmx(tmxBody, tsx: CustomTsxProvider());
final TiledMap mapTmx = TileMapParser.parseTmx(tmxBody, tsxProviders: [SingleTsxProvider(), MultipleTsxProvider()]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Split up line with trailing comma


```

Expand Down
2 changes: 1 addition & 1 deletion packages/tiled/lib/src/tile_map_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class TileMapParser {
///
/// Accepts an optional list of external TsxProviders for external tilesets
/// referenced in the map file.
static TiledMap parseTmx(String xml, {List<TsxProvider>? tsxList}) {
static TiledMap parseTmx(String xml, {List<TsxProviderBase>? tsxList}) {
final xmlElement = XmlDocument.parse(xml).rootElement;
if (xmlElement.name.local != 'map') {
throw 'XML is not in TMX format';
Expand Down
4 changes: 2 additions & 2 deletions packages/tiled/lib/src/tiled_map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ class TiledMap {
);
}

factory TiledMap.parse(Parser parser, {List<TsxProvider>? tsxList}) {
factory TiledMap.parse(Parser parser, {List<TsxProviderBase>? tsxList}) {
final backgroundColorHex = parser.getStringOrNull('backgroundcolor');
final backgroundColor = parser.getColorOrNull('backgroundcolor');
final compressionLevel = parser.getInt('compressionlevel', defaults: -1);
Expand Down Expand Up @@ -317,7 +317,7 @@ class TiledMap {
return Tileset.parse(tilesetData);
}
final matchingTsx = tsxList.where(
(tsx) => tsx.filename == tilesetSource,
(tsx) => tsx.checkProvidable(tilesetSource),
);

return Tileset.parse(
Expand Down
6 changes: 3 additions & 3 deletions packages/tiled/lib/src/tileset/tileset.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class Tileset {
tileCount = this.tiles.length;
}

factory Tileset.parse(Parser parser, {TsxProvider? tsx}) {
factory Tileset.parse(Parser parser, {TsxProviderBase? tsx}) {
final backgroundColor = parser.getStringOrNull('backgroundcolor');
final columns = parser.getIntOrNull('columns');
final firstGid = parser.getIntOrNull('firstgid');
Expand Down Expand Up @@ -169,10 +169,10 @@ class Tileset {
return result;
}

void _checkIfExternalTsx(String? source, TsxProvider? tsx) {
void _checkIfExternalTsx(String? source, TsxProviderBase? tsx) {
if (tsx != null && source != null) {
final tileset = Tileset.parse(
tsx.getCachedSource() ?? tsx.getSource(source),
tsx.getCachedSourceBase(source) ?? tsx.getSourceBase(source),
);
// Copy attributes if not null
backgroundColor = tileset.backgroundColor ?? backgroundColor;
Expand Down
34 changes: 30 additions & 4 deletions packages/tiled/lib/src/tsx_provider.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,44 @@
part of tiled;

/// abstract class to be implemented for an external tileset data provider.
abstract class TsxProvider {
abstract class TsxProviderBase {
/// Given a filename this function should check whether this Provider
/// can provide this source.
bool checkProvidable(String filename);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The word "check" in checkProvidable seems to indicate that this method is going to perform some validation on the given filename. How does canProvide sound?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that does sound better 👍


/// Retrieves the external tileset data given the tileset filename.
Parser getSourceBase(String filename);

/// Used when provider implementations cache the data. Returns the cached
/// data for the external tileset by filename.
Parser? getCachedSourceBase(String filename);
}

/// abstract convenience class to be implemented for an external tileset data
/// provider, which only provides one file and can therefore be matched by the
/// filename alone.
abstract class TsxProvider extends TsxProviderBase {
/// Unique filename for the tileset to be loaded. This should match the
/// 'source' property in the map.tmx file.
/// 'source' property in the map.tmx file. This is used to check if this
/// Provider can provide a source!
String get filename;

/// Retrieves the external tileset data given the tileset filename.
Parser getSource(String filename);
@override
bool checkProvidable(String filename) => filename == this.filename;

/// Used when provider implementations cache the data. Returns the cached
/// data for the exernal tileset.
Parser? getCachedSource();

@override
Parser? getCachedSourceBase(String _) => getCachedSource();

/// Retrieves the external tileset data given the tileset filename.
Parser getSource(String filename);

@override
Parser getSourceBase(String filename) => getSource(filename);

/// Parses a file returning a [TsxProvider].
static Future<TsxProvider> parse(String key) {
throw UnimplementedError();
Expand Down
Loading