loadProfiles method

Future<void> loadProfiles()

Loads launcher profiles from disk.

Throws an exception if the launcher profiles file cannot be found or loaded. If no active profile is set, selects the most recently used profile.

Implementation

Future<void> loadProfiles() async {
  final launcherProfilesPath = p.join(gameDir, 'launcher_profiles.json');
  final profilesFile = File(launcherProfilesPath);

  if (!await profilesFile.exists()) {
    throw Exception(
      'Launcher profiles file not found at $launcherProfilesPath',
    );
  }

  try {
    final jsonString = await profilesFile.readAsString();
    final jsonMap = json.decode(jsonString) as Map<String, dynamic>;
    _profiles = LauncherProfiles.fromJson(jsonMap);

    if (_profiles!.profiles.isNotEmpty && _activeProfile == null) {
      Profile? mostRecentProfile;
      DateTime mostRecentTime = DateTime(1970);

      for (final profile in _profiles!.profiles.values) {
        final lastUsedTime = DateTime.parse(profile.lastUsed);
        if (lastUsedTime.isAfter(mostRecentTime)) {
          mostRecentTime = lastUsedTime;
          mostRecentProfile = profile;
        }
      }

      _activeProfile = mostRecentProfile ?? _profiles!.profiles.values.first;
    }
  } catch (e) {
    debugPrint('Error loading launcher profiles: $e');
    throw Exception('Failed to load launcher profiles: $e');
  }
}