lil bit more refactoring
This commit is contained in:
@@ -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>> checkIncompatibilities() {
|
||||||
List<List<String>> conflicts = [];
|
List<List<String>> conflicts = [];
|
||||||
List<String> activeModIds = activeMods.keys.toList();
|
List<String> activeModIds = activeMods.keys.toList();
|
||||||
|
|
||||||
// Only check each pair once
|
// Only check each pair once
|
||||||
for (int i = 0; i < activeModIds.length; i++) {
|
for (final modId in activeModIds) {
|
||||||
String modId = activeModIds[i];
|
|
||||||
Mod mod = mods[modId]!;
|
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
|
// Only process if other mod is active and we haven't checked this pair yet
|
||||||
if (activeMods.containsKey(incompId) && modId.compareTo(incompId) < 0) {
|
if (activeMods.containsKey(incompId) && modId.compareTo(incompId) < 0) {
|
||||||
conflicts.add([modId, incompId]);
|
conflicts.add([modId, incompId]);
|
||||||
@@ -217,7 +205,7 @@ class ModList {
|
|||||||
/// Generate a load order for active mods
|
/// Generate a load order for active mods
|
||||||
List<String> generateLoadOrder() {
|
List<String> generateLoadOrder() {
|
||||||
// Check for incompatibilities first
|
// Check for incompatibilities first
|
||||||
List<List<String>> conflicts = checkIncompatibilities();
|
final conflicts = checkIncompatibilities();
|
||||||
if (conflicts.isNotEmpty) {
|
if (conflicts.isNotEmpty) {
|
||||||
throw Exception(
|
throw Exception(
|
||||||
"Incompatible mods selected: ${conflicts.map((c) => "${c[0]} and ${c[1]}").join(', ')}",
|
"Incompatible mods selected: ${conflicts.map((c) => "${c[0]} and ${c[1]}").join(', ')}",
|
||||||
@@ -225,19 +213,23 @@ class ModList {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Reset all marks for topological sort
|
// Reset all marks for topological sort
|
||||||
for (Mod mod in mods.values) {
|
for (final mod in mods.values) {
|
||||||
mod.visited = false;
|
mod.visited = false;
|
||||||
mod.mark = false;
|
mod.mark = false;
|
||||||
mod.position = -1;
|
mod.position = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
List<String> result = [];
|
final result = <String>[];
|
||||||
int position = 0;
|
int position = 0;
|
||||||
|
|
||||||
// Topological sort
|
// Topological sort
|
||||||
void visit(Mod mod) {
|
void visit(Mod mod) {
|
||||||
|
if (!mod.enabled) {
|
||||||
|
mod.visited = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (mod.mark) {
|
if (mod.mark) {
|
||||||
List<String> cyclePath =
|
final cyclePath =
|
||||||
mods.values.where((m) => m.mark).map((m) => m.name).toList();
|
mods.values.where((m) => m.mark).map((m) => m.name).toList();
|
||||||
throw Exception(
|
throw Exception(
|
||||||
"Cyclic dependency detected: ${cyclePath.join(' -> ')}",
|
"Cyclic dependency detected: ${cyclePath.join(' -> ')}",
|
||||||
|
@@ -50,13 +50,26 @@ void main() {
|
|||||||
id: 'disabledDummy',
|
id: 'disabledDummy',
|
||||||
enabled: false,
|
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);
|
final dummyList = ModList(configPath: configPath, modsPath: modsRoot);
|
||||||
dummyList.mods.addAll(dummyMods);
|
dummyList.mods.addAll(dummyMods);
|
||||||
for (final mod in dummyMods.keys) {
|
for (final mod in dummyMods.keys) {
|
||||||
dummyList.activeMods[mod] = true;
|
dummyList.setEnabled(mod, true);
|
||||||
}
|
}
|
||||||
|
dummyList.setEnabled('disabledDummy', false);
|
||||||
final sortedMods = dummyList.generateLoadOrder();
|
final sortedMods = dummyList.generateLoadOrder();
|
||||||
|
|
||||||
group('Test sorting', () {
|
group('Test sorting', () {
|
||||||
@@ -74,5 +87,10 @@ void main() {
|
|||||||
final disabledIndex = sortedMods.indexOf('disabledDummy');
|
final disabledIndex = sortedMods.indexOf('disabledDummy');
|
||||||
expect(disabledIndex, isNegative);
|
expect(disabledIndex, isNegative);
|
||||||
});
|
});
|
||||||
|
test("yuuuge mod should load before smol", () {
|
||||||
|
final smolIndex = sortedMods.indexOf('smol');
|
||||||
|
final yuuugeIndex = sortedMods.indexOf('yuuuge');
|
||||||
|
expect(yuuugeIndex, lessThan(smolIndex));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user