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(fire_atlas): Encoded option to load json instead of .fa #2649

Merged
merged 8 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
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