loadWorlds method
Implementation
Future<void> loadWorlds() async {
Map<String, SetonixData> worlds = {};
final defaultFile = File('$worldDirectory/$defaultWorldName$worldSuffix');
if (await defaultFile.exists()) {
try {
final bytes = await defaultFile.readAsBytes();
worlds[defaultWorldName] = SetonixData.fromData(bytes);
} catch (e) {
log(
'Error loading default world from ${defaultFile.path}: $e',
level: LogLevel.warning,
);
}
} else {
worlds[defaultWorldName] = _buildDefaultWorld();
log('No default world found, creating new one', level: LogLevel.info);
}
final dir = Directory(worldDirectory);
if (await dir.exists()) {
await for (final file in dir.list(recursive: false)) {
if (file is! File || !file.path.endsWith(worldSuffix)) continue;
final name = file.path.substring(
dir.path.length + 1,
file.path.length - worldSuffix.length,
);
if (name == defaultWorldName) continue;
try {
final bytes = await file.readAsBytes();
worlds[name] = SetonixData.fromData(bytes);
} catch (e) {
log(
'Error loading world $name from ${file.path}: $e',
level: LogLevel.warning,
);
}
}
}
for (final entry in worlds.entries) {
final name = entry.key;
final data = entry.value;
final bloc = WorldBloc(data, this, name);
_worlds[name] = bloc;
await bloc.init();
log('Loaded world $name', level: LogLevel.info);
}
}