sc4
Version:
A command line utility for automating SimCity 4 modding tasks & modifying savegames
46 lines (45 loc) • 1.49 kB
JavaScript
// # sort-load-order.js
// # createLoadComparator()
export default function createLoadComparator() {
const table = new HashTable();
return function compare(a, b) {
let ha = table.getHash(a);
let hb = table.getHash(b);
// .sc4* always get loaded before .dat
let ea = ha.at(-1).endsWith('.DAT') ? 1 : -1;
let eb = hb.at(-1).endsWith('.DAT') ? 1 : -1;
let diff = ea - eb;
if (diff !== 0)
return diff;
// Next we'll look at the folder structure.
let min = Math.min(ha.length, hb.length) - 1;
let i = 0;
for (; i < min; i++) {
let a = ha[i];
let b = hb[i];
if (a === b)
continue;
return a < b ? -1 : 1;
}
// If we haven't made a decision by now, it means either both files are
// in the same folder, or one folder is a subfolder of the other one.
if (ha.length === hb.length) {
let da = ha[i].endsWith('.DAT') ? 1 : -1;
let db = hb[i].endsWith('.DAT') ? 1 : -1;
return da - db || ha[i] < hb[i] ? -1 : 1;
}
else {
return ha.length < hb.length ? -1 : 1;
}
};
}
class HashTable {
table = Object.create(null);
getHash(file) {
let arr = this.table[file];
if (arr)
return arr;
let parts = file.toUpperCase().split(/[\/\\]/);
return arr = this.table[file] = parts;
}
}