diff --git a/lib/mod_list_troubleshooter.dart b/lib/mod_list_troubleshooter.dart index 34f059a..e4ad8dc 100644 --- a/lib/mod_list_troubleshooter.dart +++ b/lib/mod_list_troubleshooter.dart @@ -12,6 +12,13 @@ import 'package:rimworld_modman/mod_list.dart'; /// /// These approaches help RimWorld mod users identify which mods are causing problems /// when many mods are installed. +class Move { + final int startIndex; + final int endIndex; + + Move({required this.startIndex, required this.endIndex}); +} + class ModListTroubleshooter { final ModList originalModList; ModList currentModList; @@ -25,78 +32,108 @@ class ModListTroubleshooter { _startIndex = 0, _endIndex = modList.activeMods.length; - ModList binaryForward() { + Move binaryForwardMove() { 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( - midIndex, - _endIndex, + move.startIndex, + move.endIndex, ); currentModList.disableAll(); currentModList.enableMods(subset); - _startIndex = midIndex; + _startIndex = move.startIndex; + _endIndex = move.endIndex; return currentModList; } ModList binaryBackward() { - final midIndex = ((_startIndex + _endIndex) / 2).ceil(); + final move = binaryBackwardMove(); final subset = originalModList.activeMods.keys.toList().sublist( - _startIndex, - midIndex, + move.startIndex, + move.endIndex, ); currentModList.disableAll(); currentModList.enableMods(subset); - _endIndex = midIndex; + _startIndex = move.startIndex; + _endIndex = move.endIndex; return currentModList; } // 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 - ModList linearForward({int stepSize = 20}) { + Move linearForwardMove({int stepSize = 20}) { + var start = _startIndex; + var end = _endIndex; // If we are not "in step" - if (_endIndex - _startIndex == stepSize) { + if (end - start == stepSize) { // Move the indices forward by the step size, step forward - _startIndex += stepSize; - _endIndex += stepSize; + start += stepSize; + end += stepSize; } else { - // Correct the end index to be in step - _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); + end = start + stepSize; } + 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( - _startIndex, - _endIndex, + move.startIndex, + move.endIndex, ); currentModList.disableAll(); currentModList.enableMods(subset); + _startIndex = move.startIndex; + _endIndex = move.endIndex; return currentModList; } ModList linearBackward({int stepSize = 20}) { - if (_endIndex - _startIndex == stepSize) { - _startIndex -= stepSize; - _endIndex -= stepSize; - } else { - _startIndex = _endIndex - stepSize; - } - if (_startIndex < 0) { - _startIndex = 0; - _endIndex = stepSize.clamp(0, originalModList.activeMods.length); - } - + final move = linearBackwardMove(stepSize: stepSize); final subset = originalModList.activeMods.keys.toList().sublist( - _startIndex, - _endIndex, + move.startIndex, + move.endIndex, ); currentModList.disableAll(); currentModList.enableMods(subset); + _startIndex = move.startIndex; + _endIndex = move.endIndex; return currentModList; }