Skip to content

Commit

Permalink
Merge pull request #17 from dark0dave/fix/walk
Browse files Browse the repository at this point in the history
fix(walk): Fixed walked depth and add skip option
  • Loading branch information
dark0dave authored Oct 9, 2023
2 parents 554219e + 44b9da6 commit 4acd287
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 16 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ Usage: mod_installer [OPTIONS] --log-file <LOG_FILE> \
--mod-directories <MOD_DIRECTORIES>

Options:
--log-file <LOG_FILE> Full path to target log [env: LOG_FILE=]
-g, --game-directory <GAME_DIRECTORY> Full path to game directory [env: GAME_DIRECTORY=]
-w, --weidu-binary <WEIDU_BINARY> Full Path to weidu binary [env: WEIDU_BINARY=]
-m, --mod-directories <MOD_DIRECTORIES> Full Path to mod directories [env: MOD_DIRECTORIES=]
--log-file <LOG_FILE> Full path to target log [env: LOG_FILE]
-g, --game-directory <GAME_DIRECTORY> Full path to game directory [env: GAME_DIRECTORY]
-w, --weidu-binary <WEIDU_BINARY> Full Path to weidu binary [env: WEIDU_BINARY]
-m, --mod-directories <MOD_DIRECTORIES> Full Path to mod directories [env: MOD_DIRECTORIES]
-l, --language <LANGUAGE> Game Language [default: en_US]
-d, --depth <DEPTH> Depth to walk folder structure [default: 3]
-s, --skip-installed Compare against installed weidu log, note this is best effort
-h, --help Print help
-V, --version Print version
```
Expand Down
10 changes: 9 additions & 1 deletion src/args.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::path::{Path, PathBuf};

use clap::Parser;
use clap::{ArgAction, Parser};

#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
Expand Down Expand Up @@ -32,6 +32,14 @@ pub struct Args {
/// Game Language
#[clap(short, long, default_value = "en_US")]
pub language: String,

/// Depth to walk folder structure
#[clap(long, short, default_value = "3")]
pub depth: usize,

/// Compare against installed weidu log, note this is best effort
#[clap(long, short, action=ArgAction::SetFalse)]
pub skip_installed: bool,
}

fn parse_absolute_path(arg: &str) -> Result<PathBuf, String> {
Expand Down
31 changes: 28 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,38 @@ fn main() {
);
let args = Args::parse();

create_weidu_log_if_not_exists(&args.game_directory);
let installed_log_path = create_weidu_log_if_not_exists(&args.game_directory);

let mods = parse_weidu_log(args.log_file);
let number_of_mods_found = mods.len();
let mods_to_be_installed = if args.skip_installed {
let installed_mods = parse_weidu_log(installed_log_path);
mods.iter()
.filter_map(|weidu_mod| {
if !installed_mods.contains(weidu_mod) {
Some(weidu_mod.clone())
} else {
None
}
})
.collect()
} else {
mods
};

log::debug!(
"Number of mods found: {}, Number of mods to be installed: {}",
number_of_mods_found,
mods_to_be_installed.len()
);

let mut mod_folder_cache = HashMap::new();
for weidu_mod in parse_weidu_log(args.log_file) {
for weidu_mod in mods_to_be_installed {
let mod_folder = mod_folder_cache
.entry(weidu_mod.tp_file.clone())
.or_insert_with(|| search_mod_folders(&args.mod_directories, &weidu_mod.clone()));
.or_insert_with(|| {
search_mod_folders(&args.mod_directories, &weidu_mod.clone(), args.depth)
});

log::debug!("Found mod folder {:?}, for mod {:?}", mod_folder, weidu_mod);

Expand Down
19 changes: 12 additions & 7 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ use walkdir::WalkDir;

use crate::mod_component::ModComponent;

pub fn create_weidu_log_if_not_exists(game_directory: &Path) {
pub fn create_weidu_log_if_not_exists(game_directory: &Path) -> PathBuf {
let weidu_log_file = game_directory.join("weidu").with_extension("log");
if !weidu_log_file.exists() {
File::create(weidu_log_file).unwrap();
File::create(weidu_log_file.clone()).unwrap();
}
weidu_log_file
}

pub fn mod_folder_present_in_game_directory(game_directory: &Path, mod_name: &str) -> bool {
Expand All @@ -28,10 +29,14 @@ pub fn copy_mod_folder(game_directory: &Path, mod_folder: &Path) {
}
}

pub fn search_mod_folders(folder_directories: &[PathBuf], weidu_mod: &ModComponent) -> PathBuf {
pub fn search_mod_folders(
folder_directories: &[PathBuf],
weidu_mod: &ModComponent,
depth: usize,
) -> PathBuf {
let mod_folder_locations = folder_directories
.iter()
.find_map(|mod_folder| find_mod_folder(weidu_mod, mod_folder));
.find_map(|mod_folder| find_mod_folder(weidu_mod, mod_folder, depth));

if let Some(mod_folder) = mod_folder_locations {
mod_folder
Expand All @@ -41,10 +46,10 @@ pub fn search_mod_folders(folder_directories: &[PathBuf], weidu_mod: &ModCompone
}
}

fn find_mod_folder(mod_component: &ModComponent, mod_dir: &Path) -> Option<PathBuf> {
fn find_mod_folder(mod_component: &ModComponent, mod_dir: &Path, depth: usize) -> Option<PathBuf> {
WalkDir::new(mod_dir)
.follow_links(true)
.max_depth(4)
.max_depth(depth)
.into_iter()
.find_map(|entry| match entry {
Ok(entry)
Expand Down Expand Up @@ -77,7 +82,7 @@ mod tests {
lang: "0".to_string(),
component: "0".to_string(),
};
let mod_folder = find_mod_folder(&mod_component, Path::new("fixtures/mods"));
let mod_folder = find_mod_folder(&mod_component, Path::new("fixtures/mods"), 3);

let expected =
Path::new(&format!("fixtures/mods/mod_a/{}", mod_component.name)).to_path_buf();
Expand Down
2 changes: 1 addition & 1 deletion src/weidu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub fn get_user_input() -> String {
}

fn generate_args(weidu_mod: &ModComponent, language: &str) -> Vec<String> {
format!("{mod_name}/{mod_tp_file} --quick-log --yes --ask-only {component} --use-lang {game_lang} --language {mod_lang}", mod_name = weidu_mod.name, mod_tp_file = weidu_mod.tp_file, component = weidu_mod.component, mod_lang = weidu_mod.lang, game_lang = language).split(' ').map(|x|x.to_string()).collect()
format!("{mod_name}/{mod_tp_file} --yes --ask-only {component} --use-lang {game_lang} --language {mod_lang}", mod_name = weidu_mod.name, mod_tp_file = weidu_mod.tp_file, component = weidu_mod.component, mod_lang = weidu_mod.lang, game_lang = language).split(' ').map(|x|x.to_string()).collect()
}

pub fn install(
Expand Down

0 comments on commit 4acd287

Please sign in to comment.