feat: Implement platform-specific persistence for renderer settings and save games
Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
@@ -21,7 +21,7 @@ class FlutterRendererSettingsPersistence extends RendererSettingsPersistence
|
||||
final String? _filePath;
|
||||
String? _resolvedPath;
|
||||
|
||||
Future<String> _getFilePath() async {
|
||||
String get filePath {
|
||||
if (_resolvedPath != null) {
|
||||
return _resolvedPath!;
|
||||
}
|
||||
@@ -29,10 +29,7 @@ class FlutterRendererSettingsPersistence extends RendererSettingsPersistence
|
||||
_resolvedPath = _filePath;
|
||||
return _resolvedPath!;
|
||||
}
|
||||
// Resolve platform app-support directory.
|
||||
final String home =
|
||||
Platform.environment['HOME'] ?? Platform.environment['APPDATA'] ?? '.';
|
||||
_resolvedPath = '$home/.wolf3d_settings.json';
|
||||
_resolvedPath = '$platformConfigDir/settings.json';
|
||||
return _resolvedPath!;
|
||||
}
|
||||
|
||||
@@ -40,8 +37,7 @@ class FlutterRendererSettingsPersistence extends RendererSettingsPersistence
|
||||
Future<String?> readRaw() async {
|
||||
if (kIsWeb) return null;
|
||||
try {
|
||||
final String path = await _getFilePath();
|
||||
final File f = File(path);
|
||||
final File f = File(filePath);
|
||||
if (!f.existsSync()) return null;
|
||||
return await f.readAsString();
|
||||
} catch (_) {
|
||||
@@ -53,10 +49,27 @@ class FlutterRendererSettingsPersistence extends RendererSettingsPersistence
|
||||
Future<void> writeRaw(String json) async {
|
||||
if (kIsWeb) return;
|
||||
try {
|
||||
final String path = await _getFilePath();
|
||||
await File(path).writeAsString(json, flush: true);
|
||||
await File(filePath).writeAsString(json, flush: true);
|
||||
} catch (_) {
|
||||
// Best-effort.
|
||||
}
|
||||
}
|
||||
|
||||
String get platformConfigDir {
|
||||
if (Platform.isLinux) {
|
||||
final String xdg = Platform.environment['XDG_CONFIG_HOME'] ?? '';
|
||||
final String home = Platform.environment['HOME'] ?? '.';
|
||||
return xdg.isNotEmpty ? '$xdg/wolf3d' : '$home/.config/wolf3d';
|
||||
}
|
||||
if (Platform.isMacOS) {
|
||||
final String home = Platform.environment['HOME'] ?? '.';
|
||||
return '$home/Library/Application Support/wolf3d';
|
||||
}
|
||||
if (Platform.isWindows) {
|
||||
final String appData = Platform.environment['APPDATA'] ?? '.';
|
||||
return '$appData/wolf3d';
|
||||
}
|
||||
final String home = Platform.environment['HOME'] ?? '.';
|
||||
return '$home/.config/wolf3d';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,9 +24,7 @@ class FlutterSaveGamePersistence implements SaveGamePersistence {
|
||||
return _resolvedDirectoryPath!;
|
||||
}
|
||||
|
||||
final String home =
|
||||
Platform.environment['HOME'] ?? Platform.environment['APPDATA'] ?? '.';
|
||||
_resolvedDirectoryPath = '$home/.wolf3d_saves';
|
||||
_resolvedDirectoryPath = '${_platformConfigDir()}/saves';
|
||||
return _resolvedDirectoryPath!;
|
||||
}
|
||||
|
||||
@@ -94,3 +92,21 @@ class FlutterSaveGamePersistence implements SaveGamePersistence {
|
||||
return '$dirPath/SAVEGAM$normalizedSlot.${version.fileExtension}';
|
||||
}
|
||||
}
|
||||
|
||||
String _platformConfigDir() {
|
||||
if (Platform.isLinux) {
|
||||
final String xdg = Platform.environment['XDG_CONFIG_HOME'] ?? '';
|
||||
final String home = Platform.environment['HOME'] ?? '.';
|
||||
return xdg.isNotEmpty ? '$xdg/wolf3d' : '$home/.config/wolf3d';
|
||||
}
|
||||
if (Platform.isMacOS) {
|
||||
final String home = Platform.environment['HOME'] ?? '.';
|
||||
return '$home/Library/Application Support/wolf3d';
|
||||
}
|
||||
if (Platform.isWindows) {
|
||||
final String appData = Platform.environment['APPDATA'] ?? '.';
|
||||
return '$appData/wolf3d';
|
||||
}
|
||||
final String home = Platform.environment['HOME'] ?? '.';
|
||||
return '$home/.config/wolf3d';
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user