Implement "move" to troubleshooter to simulate movement

This commit is contained in:
2025-03-17 09:27:14 +01:00
parent c27ae80b5e
commit 86a7c16194

View File

@@ -12,6 +12,13 @@ import 'package:rimworld_modman/mod_list.dart';
/// ///
/// These approaches help RimWorld mod users identify which mods are causing problems /// These approaches help RimWorld mod users identify which mods are causing problems
/// when many mods are installed. /// when many mods are installed.
class Move {
final int startIndex;
final int endIndex;
Move({required this.startIndex, required this.endIndex});
}
class ModListTroubleshooter { class ModListTroubleshooter {
final ModList originalModList; final ModList originalModList;
ModList currentModList; ModList currentModList;
@@ -25,78 +32,108 @@ class ModListTroubleshooter {
_startIndex = 0, _startIndex = 0,
_endIndex = modList.activeMods.length; _endIndex = modList.activeMods.length;
ModList binaryForward() { Move binaryForwardMove() {
final midIndex = (_startIndex + _endIndex) ~/ 2; final midIndex = (_startIndex + _endIndex) ~/ 2;
return Move(startIndex: midIndex, endIndex: _endIndex);
}
Move binaryBackwardMove() {
final midIndex = ((_startIndex + _endIndex) / 2).ceil();
return Move(startIndex: _startIndex, endIndex: midIndex);
}
ModList binaryForward() {
final move = binaryForwardMove();
final subset = originalModList.activeMods.keys.toList().sublist( final subset = originalModList.activeMods.keys.toList().sublist(
midIndex, move.startIndex,
_endIndex, move.endIndex,
); );
currentModList.disableAll(); currentModList.disableAll();
currentModList.enableMods(subset); currentModList.enableMods(subset);
_startIndex = midIndex; _startIndex = move.startIndex;
_endIndex = move.endIndex;
return currentModList; return currentModList;
} }
ModList binaryBackward() { ModList binaryBackward() {
final midIndex = ((_startIndex + _endIndex) / 2).ceil(); final move = binaryBackwardMove();
final subset = originalModList.activeMods.keys.toList().sublist( final subset = originalModList.activeMods.keys.toList().sublist(
_startIndex, move.startIndex,
midIndex, move.endIndex,
); );
currentModList.disableAll(); currentModList.disableAll();
currentModList.enableMods(subset); currentModList.enableMods(subset);
_endIndex = midIndex; _startIndex = move.startIndex;
_endIndex = move.endIndex;
return currentModList; return currentModList;
} }
// If the current selection is not equal to our proposed step size // If the current selection is not equal to our proposed step size
// We do not MOVE but instead just return the correct amount of mods from the start // We do not MOVE but instead just return the correct amount of mods from the start
ModList linearForward({int stepSize = 20}) { Move linearForwardMove({int stepSize = 20}) {
var start = _startIndex;
var end = _endIndex;
// If we are not "in step" // If we are not "in step"
if (_endIndex - _startIndex == stepSize) { if (end - start == stepSize) {
// Move the indices forward by the step size, step forward // Move the indices forward by the step size, step forward
_startIndex += stepSize; start += stepSize;
_endIndex += stepSize; end += stepSize;
} else { } else {
// Correct the end index to be in step end = start + stepSize;
_endIndex = _startIndex + stepSize;
}
if (_endIndex > originalModList.activeMods.length) {
// If we are at the end of the list, move the start index such that we return
// At most the step size amount of mods
_endIndex = originalModList.activeMods.length;
_startIndex = (_endIndex - stepSize).clamp(0, _endIndex);
} }
if (end > originalModList.activeMods.length) {
// If we are at the end of the list, move the start index such that we return
// At most the step size amount of mods
end = originalModList.activeMods.length;
start = (end - stepSize).clamp(0, end);
}
return Move(startIndex: start, endIndex: end);
}
Move linearBackwardMove({int stepSize = 20}) {
var start = _startIndex;
var end = _endIndex;
if (end - start == stepSize) {
start -= stepSize;
end -= stepSize;
} else {
start = end - stepSize;
}
if (start < 0) {
start = 0;
end = stepSize.clamp(0, originalModList.activeMods.length);
}
return Move(startIndex: start, endIndex: end);
}
ModList linearForward({int stepSize = 20}) {
final move = linearForwardMove(stepSize: stepSize);
final subset = originalModList.activeMods.keys.toList().sublist( final subset = originalModList.activeMods.keys.toList().sublist(
_startIndex, move.startIndex,
_endIndex, move.endIndex,
); );
currentModList.disableAll(); currentModList.disableAll();
currentModList.enableMods(subset); currentModList.enableMods(subset);
_startIndex = move.startIndex;
_endIndex = move.endIndex;
return currentModList; return currentModList;
} }
ModList linearBackward({int stepSize = 20}) { ModList linearBackward({int stepSize = 20}) {
if (_endIndex - _startIndex == stepSize) { final move = linearBackwardMove(stepSize: stepSize);
_startIndex -= stepSize;
_endIndex -= stepSize;
} else {
_startIndex = _endIndex - stepSize;
}
if (_startIndex < 0) {
_startIndex = 0;
_endIndex = stepSize.clamp(0, originalModList.activeMods.length);
}
final subset = originalModList.activeMods.keys.toList().sublist( final subset = originalModList.activeMods.keys.toList().sublist(
_startIndex, move.startIndex,
_endIndex, move.endIndex,
); );
currentModList.disableAll(); currentModList.disableAll();
currentModList.enableMods(subset); currentModList.enableMods(subset);
_startIndex = move.startIndex;
_endIndex = move.endIndex;
return currentModList; return currentModList;
} }