Files
flutter_lua_runtime/lib/runtime/packages/stable_package_store.dart
2026-06-07 22:53:58 +08:00

87 lines
2.6 KiB
Dart

import 'dart:convert';
import 'dart:io';
import 'package:path/path.dart' as p;
import 'package:path_provider/path_provider.dart';
import '../game/runtime_options.dart';
import 'game_package.dart';
import 'game_package_manifest.dart';
class StablePackageStore {
const StablePackageStore({
RuntimeOptions runtimeOptions = const RuntimeOptions(),
}) : _runtimeOptions = runtimeOptions;
final RuntimeOptions _runtimeOptions;
Future<Directory> cacheRoot() async {
final support = await getApplicationSupportDirectory();
final root = Directory(p.join(support.path, 'flame_lua_packages'));
root.createSync(recursive: true);
return root;
}
Future<Directory> versionDirectory(String gameId, String version) async {
final root = await cacheRoot();
return Directory(p.join(root.path, gameId, version));
}
Future<void> markStable(GamePackage package) async {
if (package.source != GamePackageSource.file) {
return;
}
final marker = await _markerFile(package.manifest.gameId);
marker.createSync(recursive: true);
final previous = await stablePackage(package.manifest.gameId);
final data = {
'current': package.rootPath,
if (previous != null && previous.rootPath != package.rootPath)
'previous': previous.rootPath,
};
marker.writeAsStringSync(const JsonEncoder.withIndent(' ').convert(data));
}
Future<GamePackage?> stablePackage(String gameId) async {
final marker = await _markerFile(gameId);
if (!marker.existsSync()) {
return null;
}
final data =
jsonDecode(await marker.readAsString()) as Map<String, Object?>;
return _packageFromPath(data['current']);
}
Future<GamePackage?> previousStablePackage(String gameId) async {
final marker = await _markerFile(gameId);
if (!marker.existsSync()) {
return null;
}
final data =
jsonDecode(await marker.readAsString()) as Map<String, Object?>;
return _packageFromPath(data['previous']);
}
Future<File> _markerFile(String gameId) async {
final root = await cacheRoot();
return File(p.join(root.path, gameId, 'stable.json'));
}
GamePackage? _packageFromPath(Object? pathValue) {
if (pathValue is! String || pathValue.isEmpty) {
return null;
}
final manifestFile = File(p.join(pathValue, 'manifest.json'));
if (!manifestFile.existsSync()) {
return null;
}
return GamePackage.file(
rootPath: pathValue,
manifest: GamePackageManifest.fromJsonString(
manifestFile.readAsStringSync(),
),
runtimeLuaRoot: _runtimeOptions.runtimeLuaRoot,
);
}
}