Adding AssetsCache.readBinaryFile

This commit is contained in:
Erick Zanardo
2020-05-11 12:14:23 -03:00
parent 55dc0a2af3
commit d7b964d17c
4 changed files with 45 additions and 8 deletions

View File

@@ -2,6 +2,7 @@
## [next] ## [next]
- Updated the doc structure and minor language fixes - Updated the doc structure and minor language fixes
- Adding AssetsCache.readBinaryFile
## 0.20.2 ## 0.20.2
- Fix text component bug with anchor being applied twice - Fix text component bug with anchor being applied twice

View File

@@ -68,3 +68,8 @@
!**/ios/**/default.pbxuser !**/ios/**/default.pbxuser
!**/ios/**/default.perspectivev3 !**/ios/**/default.perspectivev3
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages !/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages
lib/generated_plugin_registrant.dart
macos/
test/
web/

View File

@@ -5,6 +5,7 @@ import 'package:flame/components/animation_component.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
void main() async { void main() async {
WidgetsFlutterBinding.ensureInitialized();
final Size size = await Flame.util.initialDimensions(); final Size size = await Flame.util.initialDimensions();
runApp(MyGame(size).widget); runApp(MyGame(size).widget);
} }

View File

@@ -1,31 +1,61 @@
import 'package:flutter/services.dart' show rootBundle; import 'package:flutter/services.dart' show rootBundle;
import 'dart:typed_data';
/// A class that loads, and cache files /// A class that loads, and cache files
/// ///
/// it automatically looks for files on the assets folder /// it automatically looks for files on the assets folder
class AssetsCache { class AssetsCache {
Map<String, String> textFiles = {}; final Map<String, _Asset> _files = {};
/// Removes the file from the cache /// Removes the file from the cache
void clear(String file) { void clear(String file) {
textFiles.remove(file); _files.remove(file);
} }
/// Removes all the files from the cache /// Removes all the files from the cache
void clearCache() { void clearCache() {
textFiles.clear(); _files.clear();
} }
/// Reads a file from assets folder /// Reads a file from assets folder
Future<String> readFile(String fileName) async { Future<String> readFile(String fileName) async {
if (!textFiles.containsKey(fileName)) { if (!_files.containsKey(fileName)) {
textFiles[fileName] = await _readFile(fileName); _files[fileName] = await _readFile(fileName);
} }
return textFiles[fileName]; assert(_files[fileName] is _StringAsset, '"${fileName}" is not a String Asset');
return _files[fileName].value;
} }
Future<String> _readFile(String fileName) async { /// Reads a binary file from assets folder
return await rootBundle.loadString('assets/$fileName'); Future<List<int>> readBinaryFile(String fileName) async {
if (!_files.containsKey(fileName)) {
_files[fileName] = await _readBinary(fileName);
}
assert(_files[fileName] is _BinaryAsset, '"${fileName}" is not a Binary Asset');
return _files[fileName].value;
}
Future<_StringAsset> _readFile(String fileName) async {
final string = await rootBundle.loadString('assets/$fileName');
return _StringAsset()..value = string;
}
Future<_BinaryAsset> _readBinary(String fileName) async {
final data = await rootBundle.load('assets/$fileName');
final Uint8List list = Uint8List.view(data.buffer);
final bytes = List.from(list).cast<int>();
return _BinaryAsset()..value = bytes;
} }
} }
class _Asset <T> {
T value;
}
class _StringAsset extends _Asset<String>{}
class _BinaryAsset extends _Asset<List<int>>{}