mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-02 20:13:50 +08:00
76 lines
1.9 KiB
Dart
76 lines
1.9 KiB
Dart
import 'dart:convert';
|
|
import 'dart:typed_data';
|
|
|
|
import 'package:flutter/services.dart' show rootBundle;
|
|
|
|
/// A class that loads, and cache files
|
|
///
|
|
/// it automatically looks for files on the assets folder
|
|
class AssetsCache {
|
|
final Map<String, _Asset> _files = {};
|
|
|
|
/// Removes the file from the cache
|
|
void clear(String file) {
|
|
_files.remove(file);
|
|
}
|
|
|
|
/// Removes all the files from the cache
|
|
void clearCache() {
|
|
_files.clear();
|
|
}
|
|
|
|
/// Reads a file from assets folder
|
|
Future<String> readFile(String fileName) async {
|
|
if (!_files.containsKey(fileName)) {
|
|
_files[fileName] = await _readFile(fileName);
|
|
}
|
|
|
|
assert(
|
|
_files[fileName] is _StringAsset,
|
|
'"$fileName" is not a String Asset',
|
|
);
|
|
|
|
return _files[fileName].value as String;
|
|
}
|
|
|
|
/// 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 as List<int>;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> readJson(String fileName) async {
|
|
final String content = await readFile(fileName);
|
|
return jsonDecode(content) as Map<String, dynamic>;
|
|
}
|
|
|
|
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<int>.from(list);
|
|
return _BinaryAsset()..value = bytes;
|
|
}
|
|
}
|
|
|
|
class _Asset<T> {
|
|
T value;
|
|
}
|
|
|
|
class _StringAsset extends _Asset<String> {}
|
|
|
|
class _BinaryAsset extends _Asset<List<int>> {}
|