mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-19 22:48:59 +08:00
Adding AssetsCache.readBinaryFile
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
## [next]
|
||||
- Updated the doc structure and minor language fixes
|
||||
- Adding AssetsCache.readBinaryFile
|
||||
|
||||
## 0.20.2
|
||||
- Fix text component bug with anchor being applied twice
|
||||
|
||||
5
docs/examples/animations/.gitignore
vendored
5
docs/examples/animations/.gitignore
vendored
@@ -68,3 +68,8 @@
|
||||
!**/ios/**/default.pbxuser
|
||||
!**/ios/**/default.perspectivev3
|
||||
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages
|
||||
|
||||
lib/generated_plugin_registrant.dart
|
||||
macos/
|
||||
test/
|
||||
web/
|
||||
|
||||
@@ -5,6 +5,7 @@ import 'package:flame/components/animation_component.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
void main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
final Size size = await Flame.util.initialDimensions();
|
||||
runApp(MyGame(size).widget);
|
||||
}
|
||||
|
||||
@@ -1,31 +1,61 @@
|
||||
import 'package:flutter/services.dart' show rootBundle;
|
||||
import 'dart:typed_data';
|
||||
|
||||
/// A class that loads, and cache files
|
||||
///
|
||||
/// it automatically looks for files on the assets folder
|
||||
class AssetsCache {
|
||||
Map<String, String> textFiles = {};
|
||||
final Map<String, _Asset> _files = {};
|
||||
|
||||
/// Removes the file from the cache
|
||||
void clear(String file) {
|
||||
textFiles.remove(file);
|
||||
_files.remove(file);
|
||||
}
|
||||
|
||||
/// Removes all the files from the cache
|
||||
void clearCache() {
|
||||
textFiles.clear();
|
||||
_files.clear();
|
||||
}
|
||||
|
||||
/// Reads a file from assets folder
|
||||
Future<String> readFile(String fileName) async {
|
||||
if (!textFiles.containsKey(fileName)) {
|
||||
textFiles[fileName] = await _readFile(fileName);
|
||||
if (!_files.containsKey(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 {
|
||||
return await rootBundle.loadString('assets/$fileName');
|
||||
/// Reads a binary file from assets folder
|
||||
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>>{}
|
||||
|
||||
Reference in New Issue
Block a user