lil bit more refactoring

This commit is contained in:
2025-03-16 13:13:06 +01:00
parent 2cd9d585e6
commit c32101c238
2 changed files with 29 additions and 19 deletions

View File

@@ -184,27 +184,15 @@ class ModList {
}
}
// List of mod ids
List<String> sort() {
final sortedMods = activeMods.keys.toList();
sortedMods.sort((a, b) {
final aIndex = mods[a]!.loadBefore.length;
final bIndex = mods[b]!.loadBefore.length;
return aIndex.compareTo(bIndex);
});
return sortedMods;
}
List<List<String>> checkIncompatibilities() {
List<List<String>> conflicts = [];
List<String> activeModIds = activeMods.keys.toList();
// Only check each pair once
for (int i = 0; i < activeModIds.length; i++) {
String modId = activeModIds[i];
for (final modId in activeModIds) {
Mod mod = mods[modId]!;
for (String incompId in mod.incompatibilities) {
for (final incompId in mod.incompatibilities) {
// Only process if other mod is active and we haven't checked this pair yet
if (activeMods.containsKey(incompId) && modId.compareTo(incompId) < 0) {
conflicts.add([modId, incompId]);
@@ -217,7 +205,7 @@ class ModList {
/// Generate a load order for active mods
List<String> generateLoadOrder() {
// Check for incompatibilities first
List<List<String>> conflicts = checkIncompatibilities();
final conflicts = checkIncompatibilities();
if (conflicts.isNotEmpty) {
throw Exception(
"Incompatible mods selected: ${conflicts.map((c) => "${c[0]} and ${c[1]}").join(', ')}",
@@ -225,19 +213,23 @@ class ModList {
}
// Reset all marks for topological sort
for (Mod mod in mods.values) {
for (final mod in mods.values) {
mod.visited = false;
mod.mark = false;
mod.position = -1;
}
List<String> result = [];
final result = <String>[];
int position = 0;
// Topological sort
void visit(Mod mod) {
if (!mod.enabled) {
mod.visited = true;
return;
}
if (mod.mark) {
List<String> cyclePath =
final cyclePath =
mods.values.where((m) => m.mark).map((m) => m.name).toList();
throw Exception(
"Cyclic dependency detected: ${cyclePath.join(' -> ')}",

View File

@@ -50,13 +50,26 @@ void main() {
id: 'disabledDummy',
enabled: false,
),
'yuuuge': dummyMod.copyWith(
name: 'Yuuuge',
id: 'yuuuge',
enabled: true,
size: 1000000,
),
'smol': dummyMod.copyWith(
name: 'Smol',
id: 'smol',
enabled: true,
size: 1,
),
};
final dummyList = ModList(configPath: configPath, modsPath: modsRoot);
dummyList.mods.addAll(dummyMods);
for (final mod in dummyMods.keys) {
dummyList.activeMods[mod] = true;
dummyList.setEnabled(mod, true);
}
dummyList.setEnabled('disabledDummy', false);
final sortedMods = dummyList.generateLoadOrder();
group('Test sorting', () {
@@ -74,5 +87,10 @@ void main() {
final disabledIndex = sortedMods.indexOf('disabledDummy');
expect(disabledIndex, isNegative);
});
test("yuuuge mod should load before smol", () {
final smolIndex = sortedMods.indexOf('smol');
final yuuugeIndex = sortedMods.indexOf('yuuuge');
expect(yuuugeIndex, lessThan(smolIndex));
});
});
}