Skip to content

Commit

Permalink
feat(fire_atlas): Encoded option to load json instead of .fa (#2649)
Browse files Browse the repository at this point in the history
 I would like to suggest to add option to load atlas without compression as simple json file.
I found it useful to use for example in itch.io, where gzip encoder causes an error during decoding.
  • Loading branch information
Arenukvern authored Sep 18, 2023
1 parent 7a3d512 commit 5be6fc8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
32 changes: 26 additions & 6 deletions packages/flame_fire_atlas/lib/flame_fire_atlas.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<FireAtlas> 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<int> serialize() {
///
/// If [encoded] is set to true,
/// it will return a gzip compressed byte array,
/// otherwise it will return a string byte array.
List<int> 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) {
Expand All @@ -296,11 +312,15 @@ class FireAtlas {
return gzipBytes;
}

/// Reads a [FireAtlas] instance from a json file.
factory FireAtlas.deserializeJson(Map<String, dynamic> rawJson) =>
FireAtlas._fromJson(rawJson);

/// Reads a [FireAtlas] instance from a byte array.
factory FireAtlas.deserialize(List<int> bytes) {
factory FireAtlas.deserializeBytes(List<int> bytes) {
final unzippedBytes = GZipDecoder().decodeBytes(bytes);
final unzippedString = utf8.decode(unzippedBytes);
return FireAtlas._fromJson(
return FireAtlas.deserializeJson(
jsonDecode(unzippedString) as Map<String, dynamic>,
);
}
Expand Down
9 changes: 5 additions & 4 deletions packages/flame_fire_atlas/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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
2 changes: 1 addition & 1 deletion packages/flame_fire_atlas/test/flame_fire_atlas_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand Down

0 comments on commit 5be6fc8

Please sign in to comment.