Skip to content

Commit 457498b

Browse files
authored
Go through regionPrios from config before regionMap (#247)
* Go through regionPrios from config before regionMap Appending regionMap to regionPrios before searching it will prioritise searching the config regionPrios first, and then if not found, move on to regionMap. May need to add a line to remove duplicates from this list as regions in regionPrios and regionMap may appear twice (removing the second occurrence should remove the regionMap appearence and leave the first one added via regionPrios) * Add an iteration through regionMap based on regionPrios to give new sortedMap order Made some fixes to ensure it compiles and also to do this is a more logical way (without duplication)
1 parent 805fb70 commit 457498b

1 file changed

Lines changed: 19 additions & 2 deletions

File tree

src/abstractscraper.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -626,13 +626,30 @@ void AbstractScraper::detectRegionFromFilename(const QFileInfo &info) {
626626
if (int leftParPos = fn.indexOf("("); leftParPos != -1) {
627627
// Autodetect region and append to region priorities
628628
QString regionString = fn.mid(leftParPos, fn.length());
629-
QListIterator<QPair<QString, QString>> iter(regionMap());
629+
// create a new version of regionMap to use for this run of the function
630+
QList<QPair<QString, QString>> sortedMap = regionMap();
631+
// iterate backwards through regionPrios (e.g., "jp", then "us", then "eu", for a regionPrios such as {"eu","us","jp"})
632+
for (int i = regionPrios.size() - 1; i >= 0; --i) {
633+
const QString &targetCode = regionPrios.at(i);
634+
// run through each pair in sortedMap and find if it is matched by the regionPrios entry (to the second entry of the pair)
635+
// if so, move it to the end of the list so that it is prioritised as I believe the logic below preprends anything closer to the end on a later iteration meaning it move closer to the start of sortedList after all iterations
636+
for (int j = 0; j < sortedMap.size(); ++j) {
637+
if (sortedMap.at(j).second == targetCode) {
638+
// move the match to the very last position
639+
sortedMap.move(j, sortedMap.size() - 1);
640+
// break the inner loop to move to the next priority item
641+
break;
642+
}
643+
}
644+
}
645+
// change the iteration here to sortedMap for the rest of the function
646+
QListIterator<QPair<QString, QString>> iter(sortedMap());
630647
while (iter.hasNext()) {
631648
QPair<QString, QString> e = iter.next();
632649
QStringList keys = e.first.split("|");
633650
for (const auto &k : keys) {
634651
if (regionString.contains(k, Qt::CaseInsensitive)) {
635-
// regionMap is sorted from bigger regions to smaller
652+
// regionMap is sorted from bigger regions to smaller (with regionPrios taken into account above and hence using sortedMap)
636653
// prepend() assures smaller regions get higher priority
637654
if (int idx = regionPrios.lastIndexOf(e.second); idx > -1) {
638655
regionPrios.removeAt(idx);

0 commit comments

Comments
 (0)