diff --git a/packages/flame_fire_atlas/lib/flame_fire_atlas.dart b/packages/flame_fire_atlas/lib/flame_fire_atlas.dart index 1f80196bb68..8dcefee734e 100644 --- a/packages/flame_fire_atlas/lib/flame_fire_atlas.dart +++ b/packages/flame_fire_atlas/lib/flame_fire_atlas.dart @@ -270,24 +270,40 @@ class FireAtlas { } /// Loads the [FireAtlas] from an asset. + /// + /// Use [encoded] = false to load the asset from a json file. static Future loadAsset( String fileName, { AssetsCache? assets, Images? images, + bool encoded = true, }) async { final assetsCache = assets ?? Flame.assets; - - final bytes = await assetsCache.readBinaryFile(fileName); - final atlas = FireAtlas.deserialize(bytes); + final FireAtlas atlas; + if (encoded) { + final bytes = await assetsCache.readBinaryFile(fileName); + atlas = FireAtlas.deserializeBytes(bytes); + } else { + final json = await assetsCache.readJson(fileName); + atlas = FireAtlas.deserializeJson(json); + } await atlas.loadImage(images: images); return atlas; } /// Serializes this instances into a byte array. - List serialize() { + /// + /// If [encoded] is set to true, + /// it will return a gzip compressed byte array, + /// otherwise it will return a string byte array. + List serialize({bool encoded = true}) { final raw = jsonEncode(toJson()); final stringBytes = utf8.encode(raw); + if (!encoded) { + return stringBytes; + } + final gzipBytes = GZipEncoder().encode(stringBytes); if (gzipBytes == null) { @@ -296,11 +312,15 @@ class FireAtlas { return gzipBytes; } + /// Reads a [FireAtlas] instance from a json file. + factory FireAtlas.deserializeJson(Map rawJson) => + FireAtlas._fromJson(rawJson); + /// Reads a [FireAtlas] instance from a byte array. - factory FireAtlas.deserialize(List bytes) { + factory FireAtlas.deserializeBytes(List bytes) { final unzippedBytes = GZipDecoder().decodeBytes(bytes); final unzippedString = utf8.decode(unzippedBytes); - return FireAtlas._fromJson( + return FireAtlas.deserializeJson( jsonDecode(unzippedString) as Map, ); } diff --git a/packages/flame_fire_atlas/pubspec.yaml b/packages/flame_fire_atlas/pubspec.yaml index a43fd986b99..5aa9fa040ca 100644 --- a/packages/flame_fire_atlas/pubspec.yaml +++ b/packages/flame_fire_atlas/pubspec.yaml @@ -1,5 +1,6 @@ name: flame_fire_atlas -description: Easy to use texture atlases for the flame engine created with the fire atlas editor +description: Easy to use texture atlases for the flame engine created with the + fire atlas editor version: 1.3.8 homepage: https://github.com/flame-engine/flame/tree/main/packages/flame_fire_atlas funding: @@ -12,14 +13,14 @@ environment: flutter: ">=3.3.0" dependencies: - archive: ^3.3.7 + archive: ^3.3.9 flame: ^1.8.2 flutter: sdk: flutter dev_dependencies: - dartdoc: ^6.2.2 + dartdoc: ^6.3.0 flame_lint: ^1.1.0 flutter_test: sdk: flutter - mocktail: ^0.3.0 + mocktail: ^1.0.0 diff --git a/packages/flame_fire_atlas/test/flame_fire_atlas_test.dart b/packages/flame_fire_atlas/test/flame_fire_atlas_test.dart index d6a19703d79..159ea78b5e8 100644 --- a/packages/flame_fire_atlas/test/flame_fire_atlas_test.dart +++ b/packages/flame_fire_atlas/test/flame_fire_atlas_test.dart @@ -147,7 +147,7 @@ void main() { final bytes = atlas.serialize(); - final copy = FireAtlas.deserialize(bytes); + final copy = FireAtlas.deserializeBytes(bytes); expect(copy.id, atlas.id); });