From 14e47f900785d1860c53a37b47a741e0629b4372 Mon Sep 17 00:00:00 2001 From: projectitis Date: Sun, 27 Aug 2023 13:49:51 +1200 Subject: [PATCH 1/4] Fixed loading JSON array files --- packages/flame/lib/src/cache/assets_cache.dart | 4 ++-- packages/flame/test/_resources/test_1.json | 16 ++++++++++++++++ packages/flame/test/_resources/test_2.json | 16 ++++++++++++++++ packages/flame/test/cache/assets_cache_test.dart | 10 ++++++++-- 4 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 packages/flame/test/_resources/test_1.json create mode 100644 packages/flame/test/_resources/test_2.json diff --git a/packages/flame/lib/src/cache/assets_cache.dart b/packages/flame/lib/src/cache/assets_cache.dart index ef8ecffb875..9bbe4cd91be 100644 --- a/packages/flame/lib/src/cache/assets_cache.dart +++ b/packages/flame/lib/src/cache/assets_cache.dart @@ -47,9 +47,9 @@ class AssetsCache { } /// Reads a json file from the assets folder. - Future> readJson(String fileName) async { + Future readJson(String fileName) async { final content = await readFile(fileName); - return jsonDecode(content) as Map; + return jsonDecode(content); } Future<_StringAsset> _readFile(String fileName) async { diff --git a/packages/flame/test/_resources/test_1.json b/packages/flame/test/_resources/test_1.json new file mode 100644 index 00000000000..7635aea094e --- /dev/null +++ b/packages/flame/test/_resources/test_1.json @@ -0,0 +1,16 @@ +[ + 1, + 2.0, + true, + null, + "Hello", + { + "key": "value" + }, + [ + 1, + 2.0, + "three", + false + ] +] diff --git a/packages/flame/test/_resources/test_2.json b/packages/flame/test/_resources/test_2.json new file mode 100644 index 00000000000..6f278066097 --- /dev/null +++ b/packages/flame/test/_resources/test_2.json @@ -0,0 +1,16 @@ +{ + "int": 1, + "double": 2.0, + "bool": true, + "null": null, + "string": "Hello", + "object": { + "key": "value" + }, + "array": [ + 1, + 2.0, + "three", + false + ] +} diff --git a/packages/flame/test/cache/assets_cache_test.dart b/packages/flame/test/cache/assets_cache_test.dart index 9b5970df529..c1cfd56d17a 100644 --- a/packages/flame/test/cache/assets_cache_test.dart +++ b/packages/flame/test/cache/assets_cache_test.dart @@ -26,12 +26,18 @@ void main() { ); }); - test('readJson', () async { + test('readJson with object root', () async { final assetsCache = AssetsCache(prefix: ''); - final file = await assetsCache.readJson(fixture('chopper.json').path); + final file = await assetsCache.readJson(fixture('test_1.json').path); expect(file, isA>()); }); + test('readJson with array root', () async { + final assetsCache = AssetsCache(prefix: ''); + final file = await assetsCache.readJson(fixture('test_2.json').path); + expect(file, isA>()); + }); + test('readBinaryFile', () async { final assetsCache = AssetsCache(prefix: ''); final fileName = fixture('cave_ace.fa').path; From 03be710e89cf2cdd2e496224c32595f434512dc1 Mon Sep 17 00:00:00 2001 From: projectitis Date: Sun, 27 Aug 2023 13:56:50 +1200 Subject: [PATCH 2/4] Fix test --- packages/flame/test/cache/assets_cache_test.dart | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/flame/test/cache/assets_cache_test.dart b/packages/flame/test/cache/assets_cache_test.dart index c1cfd56d17a..2079d05575c 100644 --- a/packages/flame/test/cache/assets_cache_test.dart +++ b/packages/flame/test/cache/assets_cache_test.dart @@ -26,16 +26,16 @@ void main() { ); }); - test('readJson with object root', () async { + test('readJson with array root', () async { final assetsCache = AssetsCache(prefix: ''); final file = await assetsCache.readJson(fixture('test_1.json').path); - expect(file, isA>()); + expect(file, isA>()); }); - test('readJson with array root', () async { + test('readJson with object root', () async { final assetsCache = AssetsCache(prefix: ''); final file = await assetsCache.readJson(fixture('test_2.json').path); - expect(file, isA>()); + expect(file, isA>()); }); test('readBinaryFile', () async { From 49806f11ace491c23e76259b34e3635cea12f5f0 Mon Sep 17 00:00:00 2001 From: projectitis Date: Sun, 27 Aug 2023 14:07:24 +1200 Subject: [PATCH 3/4] Fix example --- examples/lib/stories/animations/aseprite_example.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/lib/stories/animations/aseprite_example.dart b/examples/lib/stories/animations/aseprite_example.dart index aef17b1636e..9dcde131a06 100644 --- a/examples/lib/stories/animations/aseprite_example.dart +++ b/examples/lib/stories/animations/aseprite_example.dart @@ -10,7 +10,7 @@ class AsepriteExample extends FlameGame { @override Future onLoad() async { final image = await images.load('animations/chopper.png'); - final jsonData = await assets.readJson('images/animations/chopper.json'); + final jsonData = await assets.readJson('images/animations/chopper.json') as Map; final animation = SpriteAnimation.fromAsepriteData(image, jsonData); final spriteSize = Vector2.all(200); final animationComponent = SpriteAnimationComponent( From e152f6a54d45fc9ff54e481a770e92603d38855f Mon Sep 17 00:00:00 2001 From: projectitis Date: Sun, 27 Aug 2023 14:12:27 +1200 Subject: [PATCH 4/4] Linting example --- examples/lib/stories/animations/aseprite_example.dart | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/lib/stories/animations/aseprite_example.dart b/examples/lib/stories/animations/aseprite_example.dart index 9dcde131a06..4dda8e3e0da 100644 --- a/examples/lib/stories/animations/aseprite_example.dart +++ b/examples/lib/stories/animations/aseprite_example.dart @@ -10,7 +10,8 @@ class AsepriteExample extends FlameGame { @override Future onLoad() async { final image = await images.load('animations/chopper.png'); - final jsonData = await assets.readJson('images/animations/chopper.json') as Map; + final jsonData = await assets.readJson('images/animations/chopper.json') + as Map; final animation = SpriteAnimation.fromAsepriteData(image, jsonData); final spriteSize = Vector2.all(200); final animationComponent = SpriteAnimationComponent(