feat: Add file selector support and enhance game data directory management
Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
@@ -22,7 +22,7 @@ class DefaultGameDataDirectoryPersistence {
|
||||
return _resolvedPath!;
|
||||
}
|
||||
|
||||
_resolvedPath = '$platformConfigDir/data_source.json';
|
||||
_resolvedPath = '$platformConfigDir/settings.json';
|
||||
return _resolvedPath!;
|
||||
}
|
||||
|
||||
@@ -61,18 +61,36 @@ class DefaultGameDataDirectoryPersistence {
|
||||
try {
|
||||
final normalized = directoryPath?.trim();
|
||||
final file = File(filePath);
|
||||
Map<String, Object?> existingPayload = <String, Object?>{};
|
||||
|
||||
if (await file.exists()) {
|
||||
try {
|
||||
final String raw = await file.readAsString();
|
||||
final Object? decoded = jsonDecode(raw);
|
||||
if (decoded is Map<String, Object?>) {
|
||||
existingPayload = decoded;
|
||||
}
|
||||
} catch (_) {
|
||||
// Ignore malformed existing content.
|
||||
}
|
||||
}
|
||||
|
||||
await file.parent.create(recursive: true);
|
||||
if (normalized == null || normalized.isEmpty) {
|
||||
if (await file.exists()) {
|
||||
await file.delete();
|
||||
existingPayload.remove('dataDirectory');
|
||||
if (existingPayload.isEmpty) {
|
||||
if (await file.exists()) {
|
||||
await file.delete();
|
||||
}
|
||||
return;
|
||||
}
|
||||
final payload = jsonEncode(existingPayload);
|
||||
await file.writeAsString(payload, flush: true);
|
||||
return;
|
||||
}
|
||||
|
||||
final payload = jsonEncode(<String, Object>{
|
||||
'dataDirectory': normalized,
|
||||
});
|
||||
existingPayload['dataDirectory'] = normalized;
|
||||
final payload = jsonEncode(existingPayload);
|
||||
await file.writeAsString(payload, flush: true);
|
||||
} catch (_) {
|
||||
// Best-effort only.
|
||||
|
||||
@@ -9,11 +9,27 @@ class NoGameDataScreen extends StatelessWidget {
|
||||
const NoGameDataScreen({
|
||||
super.key,
|
||||
this.configuredDataDirectory,
|
||||
this.onPickGameDataDirectory,
|
||||
this.onPickGameDataFiles,
|
||||
this.isLoadingGameData = false,
|
||||
this.pickerError,
|
||||
});
|
||||
|
||||
/// Previously configured external game-data directory, if any.
|
||||
final String? configuredDataDirectory;
|
||||
|
||||
/// Invoked when the user requests selecting a game-data directory.
|
||||
final Future<void> Function()? onPickGameDataDirectory;
|
||||
|
||||
/// Invoked when the user requests selecting one or more data files.
|
||||
final Future<void> Function()? onPickGameDataFiles;
|
||||
|
||||
/// Whether the host is currently reloading after picker selection.
|
||||
final bool isLoadingGameData;
|
||||
|
||||
/// Optional picker/reload error shown to the user.
|
||||
final String? pickerError;
|
||||
|
||||
static Color _colorFromVgaIndex(int index) {
|
||||
final int packed = ColorPalette.vga32Bit[index]; // 0xAABBGGRR
|
||||
final int r = packed & 0xFF;
|
||||
@@ -64,7 +80,7 @@ class NoGameDataScreen extends StatelessWidget {
|
||||
const SizedBox(height: 16),
|
||||
Text(
|
||||
'No game files were discovered.\n\n'
|
||||
'Select a game-data directory in setup.',
|
||||
'Select a game-data directory, or select one or more game-data files.',
|
||||
style: TextStyle(
|
||||
color: _bodyColor,
|
||||
fontSize: 15,
|
||||
@@ -81,6 +97,58 @@ class NoGameDataScreen extends StatelessWidget {
|
||||
fontWeight: FontWeight.w600,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Wrap(
|
||||
spacing: 12,
|
||||
runSpacing: 12,
|
||||
children: [
|
||||
ElevatedButton(
|
||||
onPressed: isLoadingGameData
|
||||
? null
|
||||
: onPickGameDataDirectory,
|
||||
child: Text(
|
||||
isLoadingGameData
|
||||
? 'Loading data...'
|
||||
: 'Select data directory',
|
||||
),
|
||||
),
|
||||
ElevatedButton(
|
||||
onPressed: isLoadingGameData
|
||||
? null
|
||||
: onPickGameDataFiles,
|
||||
child: Text(
|
||||
isLoadingGameData
|
||||
? 'Loading data...'
|
||||
: 'Select data files',
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
if (isLoadingGameData)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 10),
|
||||
child: Text(
|
||||
'Scanning selected locations...',
|
||||
style: TextStyle(
|
||||
color: _bodyColor,
|
||||
fontSize: 13,
|
||||
height: 1.3,
|
||||
),
|
||||
),
|
||||
),
|
||||
if (pickerError != null && pickerError!.trim().isNotEmpty)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 12),
|
||||
child: Text(
|
||||
pickerError!.trim(),
|
||||
style: TextStyle(
|
||||
color: _emphasisColor,
|
||||
fontSize: 13,
|
||||
height: 1.3,
|
||||
fontWeight: FontWeight.w600,
|
||||
),
|
||||
),
|
||||
),
|
||||
if (configuredDataDirectory != null &&
|
||||
configuredDataDirectory!.trim().isNotEmpty)
|
||||
Padding(
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
library;
|
||||
|
||||
import 'dart:collection';
|
||||
|
||||
import 'package:file_selector/file_selector.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:wolf_3d_flutter/wolf_3d_flutter.dart';
|
||||
|
||||
/// Minimal app shell that binds a prepared [Wolf3dFlutterEngine] instance to
|
||||
/// host screens.
|
||||
class Wolf3dApp extends StatelessWidget {
|
||||
class Wolf3dApp extends StatefulWidget {
|
||||
/// Shared initialized facade that owns game data, input, and audio services.
|
||||
final Wolf3dFlutterEngine engine;
|
||||
|
||||
@@ -14,12 +17,124 @@ class Wolf3dApp extends StatelessWidget {
|
||||
required this.engine,
|
||||
});
|
||||
|
||||
@override
|
||||
State<Wolf3dApp> createState() => _Wolf3dAppState();
|
||||
}
|
||||
|
||||
class _Wolf3dAppState extends State<Wolf3dApp> {
|
||||
bool _isLoadingGameData = false;
|
||||
String? _pickerError;
|
||||
|
||||
Future<void> _pickGameDataDirectory() async {
|
||||
setState(() {
|
||||
_isLoadingGameData = true;
|
||||
_pickerError = null;
|
||||
});
|
||||
|
||||
try {
|
||||
final String? directoryPath = await getDirectoryPath(
|
||||
confirmButtonText: 'Use this folder',
|
||||
);
|
||||
|
||||
if (directoryPath == null || directoryPath.trim().isEmpty) {
|
||||
return;
|
||||
}
|
||||
|
||||
await widget.engine.init(directory: directoryPath.trim());
|
||||
} catch (error) {
|
||||
setState(() {
|
||||
_pickerError = 'Unable to load selected directory: $error';
|
||||
});
|
||||
} finally {
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_isLoadingGameData = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _pickGameDataFiles() async {
|
||||
setState(() {
|
||||
_isLoadingGameData = true;
|
||||
_pickerError = null;
|
||||
});
|
||||
|
||||
try {
|
||||
final List<XFile> selectedFiles = await openFiles();
|
||||
|
||||
if (selectedFiles.isEmpty) {
|
||||
return;
|
||||
}
|
||||
|
||||
final LinkedHashSet<String> directories = LinkedHashSet<String>();
|
||||
for (final XFile file in selectedFiles) {
|
||||
final String directory = _directoryFromFilePath(file.path);
|
||||
if (directory.isNotEmpty) {
|
||||
directories.add(directory);
|
||||
}
|
||||
}
|
||||
|
||||
if (directories.isEmpty) {
|
||||
setState(() {
|
||||
_pickerError = 'Selected files do not expose local filesystem paths.';
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
final List<String> orderedDirectories = directories.toList(
|
||||
growable: false,
|
||||
);
|
||||
await widget.engine.init(
|
||||
directory: orderedDirectories.first,
|
||||
additionalDirectories: orderedDirectories.skip(1),
|
||||
);
|
||||
} catch (error) {
|
||||
setState(() {
|
||||
_pickerError = 'Unable to load selected files: $error';
|
||||
});
|
||||
} finally {
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_isLoadingGameData = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
String _directoryFromFilePath(String path) {
|
||||
final String trimmedPath = path.trim();
|
||||
if (trimmedPath.isEmpty) {
|
||||
return '';
|
||||
}
|
||||
|
||||
final int slashIndex = trimmedPath.lastIndexOf('/');
|
||||
final int backslashIndex = trimmedPath.lastIndexOf(r'\');
|
||||
final int separatorIndex = slashIndex > backslashIndex
|
||||
? slashIndex
|
||||
: backslashIndex;
|
||||
|
||||
if (separatorIndex < 0) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (separatorIndex == 0) {
|
||||
return trimmedPath[0];
|
||||
}
|
||||
|
||||
return trimmedPath.substring(0, separatorIndex);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return engine.availableGames.isEmpty
|
||||
return widget.engine.availableGames.isEmpty
|
||||
? NoGameDataScreen(
|
||||
configuredDataDirectory: engine.configuredDataDirectory,
|
||||
configuredDataDirectory: widget.engine.configuredDataDirectory,
|
||||
onPickGameDataDirectory: _pickGameDataDirectory,
|
||||
onPickGameDataFiles: _pickGameDataFiles,
|
||||
isLoadingGameData: _isLoadingGameData,
|
||||
pickerError: _pickerError,
|
||||
)
|
||||
: GameScreen(wolf3d: engine);
|
||||
: GameScreen(wolf3d: widget.engine);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,6 +83,7 @@ class Wolf3dFlutterEngine extends Wolf3dEngine {
|
||||
/// Initializes the engine by loading available game data.
|
||||
Future<Wolf3dFlutterEngine> init({
|
||||
String? directory,
|
||||
Iterable<String>? additionalDirectories,
|
||||
}) async {
|
||||
await desktop_windowing_support.ensureDesktopWindowingInitialized();
|
||||
await audio.init();
|
||||
@@ -128,21 +129,37 @@ class Wolf3dFlutterEngine extends Wolf3dEngine {
|
||||
}
|
||||
}
|
||||
|
||||
// On non-web platforms, also scan the local filesystem for user-supplied
|
||||
// data folders so the host can pick up extra versions automatically.
|
||||
if (!kIsWeb && resolvedDirectory != null) {
|
||||
try {
|
||||
final externalGames = await WolfensteinLoader.discover(
|
||||
directoryPath: resolvedDirectory,
|
||||
recursive: true,
|
||||
);
|
||||
for (var entry in externalGames.entries) {
|
||||
if (!availableGames.any((g) => g.version == entry.key)) {
|
||||
availableGames.add(entry.value);
|
||||
}
|
||||
// On non-web platforms, also scan local filesystem locations for
|
||||
// user-supplied data folders so the host can pick up extra versions.
|
||||
final Set<String> directoriesToScan = <String>{};
|
||||
if (resolvedDirectory != null && resolvedDirectory.isNotEmpty) {
|
||||
directoriesToScan.add(resolvedDirectory);
|
||||
}
|
||||
|
||||
if (additionalDirectories != null) {
|
||||
for (final String directoryPath in additionalDirectories) {
|
||||
final String trimmedPath = directoryPath.trim();
|
||||
if (trimmedPath.isNotEmpty) {
|
||||
directoriesToScan.add(trimmedPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!kIsWeb && directoriesToScan.isNotEmpty) {
|
||||
for (final String directoryPath in directoriesToScan) {
|
||||
try {
|
||||
final externalGames = await WolfensteinLoader.discover(
|
||||
directoryPath: directoryPath,
|
||||
recursive: true,
|
||||
);
|
||||
for (var entry in externalGames.entries) {
|
||||
if (!availableGames.any((g) => g.version == entry.key)) {
|
||||
availableGames.add(entry.value);
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint("External discovery failed: $e");
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint("External discovery failed: $e");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user