Skip to content

Commit 02b7cc4

Browse files
committed
correct cp77 mod load order
1 parent 88e9538 commit 02b7cc4

File tree

1 file changed

+46
-8
lines changed

1 file changed

+46
-8
lines changed

src/lib.rs

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::collections::HashMap;
22
use std::error::Error;
33
use std::fs::{self, File};
4-
use std::io::{self, BufRead, Read, Seek, Write};
4+
use std::io::{self, BufRead, BufReader, Read, Seek, Write};
55
use std::path::{Path, PathBuf};
66
use std::{env, vec};
77

@@ -526,6 +526,15 @@ fn match_desc_version(desc: &str) -> Option<String> {
526526
None
527527
}
528528

529+
fn read_file_to_vec(file_path: &PathBuf) -> io::Result<Vec<String>> {
530+
let file = File::open(file_path)?;
531+
let reader = BufReader::new(file);
532+
533+
let lines: Vec<String> = reader.lines().map_while(Result::ok).collect();
534+
535+
Ok(lines)
536+
}
537+
529538
pub fn gather_cp77_mods<P>(root: &P, game_version: &Option<String>) -> Vec<PluginData>
530539
where
531540
P: AsRef<Path>,
@@ -543,10 +552,43 @@ where
543552
}
544553
}
545554

546-
if let Ok(plugins) = fs::read_dir(archive_path) {
547-
let mut entries = plugins
555+
if let Ok(plugins) = fs::read_dir(&archive_path) {
556+
let mut mods: Vec<PathBuf> = plugins
548557
.map(|res| res.map(|e| e.path()))
549558
.filter_map(Result::ok)
559+
.collect::<Vec<_>>();
560+
561+
// load order
562+
mods.sort_by(|a, b| {
563+
a.to_string_lossy()
564+
.as_bytes()
565+
.cmp(b.to_string_lossy().as_bytes())
566+
});
567+
568+
// load according to modlist.txt
569+
let mut final_order: Vec<PathBuf> = vec![];
570+
let modlist_name = "modlist.txt";
571+
if let Ok(lines) = read_file_to_vec(&archive_path.join(modlist_name)) {
572+
for name in lines {
573+
let file_name = archive_path.join(name);
574+
if mods.contains(&file_name) {
575+
final_order.push(file_name.to_owned());
576+
}
577+
}
578+
// add remaining mods last
579+
for m in mods.iter() {
580+
if !final_order.contains(m) {
581+
final_order.push(m.to_path_buf());
582+
}
583+
}
584+
} else {
585+
final_order = mods;
586+
}
587+
588+
// TODO Redmods
589+
590+
let vms = final_order
591+
.iter()
550592
.filter_map(|e| {
551593
if !e.is_dir() {
552594
if let Some(os_ext) = e.extension() {
@@ -571,11 +613,7 @@ where
571613
})
572614
.collect::<Vec<_>>();
573615

574-
// TODO CP77 support modlist
575-
576-
// TODO CP77 gather REDmods from mods/<NAME>
577-
entries.sort_by_key(|e| e.name.clone());
578-
return entries;
616+
return vms;
579617
}
580618

581619
vec![]

0 commit comments

Comments
 (0)