From 6a9258fdcd8269b9b38bea84ee1d432939b902c6 Mon Sep 17 00:00:00 2001 From: jekky <11986158+jac3km4@users.noreply.github.com> Date: Sat, 10 Sep 2022 22:21:39 +0100 Subject: [PATCH] Custom bundle paths (#77) * Custom bundle support * Fixes * Cleanup * Fix file name * Fix mmap feature * Create mod archive in CI * CI fixes * Pick up cache dir path * Rearrange to fix log * Default to non-modded input bundle path * Align input paths --- .github/workflows/ci.yml | 17 +++++++- .github/workflows/release.yml | 1 + cli/src/main.rs | 2 +- resources/engine/config/cybercmd/scc.toml | 7 ++++ scc/src/main.rs | 48 +++++++++++++++-------- 5 files changed, 55 insertions(+), 20 deletions(-) create mode 100644 resources/engine/config/cybercmd/scc.toml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2102e42a..620f8baa 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,6 +1,6 @@ name: CI -on: [push, pull_request] +on: [push] env: CARGO_TERM_COLOR: always @@ -26,9 +26,22 @@ jobs: uses: actions-rs/cargo@v1 with: command: test + - run: mkdir staging + - name: Create the mod archive + working-directory: staging + run: | + mkdir -p ./engine/config/base + mkdir -p ./engine/config/cybercmd + mkdir -p ./engine/tools + mkdir -p ./r6/scripts + cp ../resources/engine/config/base/scripts.ini ./engine/config/base/ + cp ../resources/engine/config/cybercmd/scc.toml ./engine/config/cybercmd/ + cp ../target/release/scc.exe ./engine/tools/ + cp ../resources/r6/scripts/redscript.toml ./r6/scripts/ + 7z a -mx=9 -r ./redscript.zip * - name: Archive artifacts uses: actions/upload-artifact@v2 with: path: | - target/release/scc.exe target/release/redscript-cli.exe + ./staging/redscript.zip diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 4da99eb7..e02c89d8 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -36,6 +36,7 @@ jobs: mkdir -p ./engine/tools mkdir -p ./r6/scripts cp ../resources/engine/config/base/scripts.ini ./engine/config/base/ + cp ../resources/engine/config/cybercmd/scc.toml ./engine/config/cybercmd/ cp ../target/release/scc.exe ./engine/tools/ cp ../resources/r6/scripts/redscript.toml ./r6/scripts/ 7z a -mx=9 -r ./redscript.zip * diff --git a/cli/src/main.rs b/cli/src/main.rs index 413235a7..af8923b6 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -137,7 +137,7 @@ fn decompile(opts: DecompileOpts) -> Result<(), redscript_decompiler::error::Err if opts.dump_files { for entry in FileIndex::from_pool(pool).iter() { - let path = opts.output.as_path().join(&entry.path); + let path = opts.output.as_path().join(entry.path); std::fs::create_dir_all(path.parent().unwrap())?; let mut output = io::BufWriter::new(File::create(path)?); diff --git a/resources/engine/config/cybercmd/scc.toml b/resources/engine/config/cybercmd/scc.toml new file mode 100644 index 00000000..7ad3e129 --- /dev/null +++ b/resources/engine/config/cybercmd/scc.toml @@ -0,0 +1,7 @@ +[args] +scriptsBlobPath = "{game_dir}\\r6\\cache\\modded\\final.redscripts" + +[[tasks]] +command = "InvokeScc" +path = "{game_dir}\\r6\\scripts" +custom_cache_dir = "{game_dir}\\r6\\cache\\modded" diff --git a/scc/src/main.rs b/scc/src/main.rs index 76b8c8f2..2e998e90 100644 --- a/scc/src/main.rs +++ b/scc/src/main.rs @@ -20,18 +20,18 @@ use time::OffsetDateTime; fn main() -> Result<(), Error> { // the way cyberpunk passes CLI args is broken, this is a workaround - let args: Vec = std::env::args().skip(1).collect(); - match &args[..] { - [cmd, path_str, ..] if cmd == "-compile" => { + let mut iter = std::env::args().skip(1); + match (iter.next().as_deref(), iter.next()) { + (Some("-compile"), Some(path_str)) => { let script_dir = PathBuf::from(path_str.split('"').next().unwrap()); - let cache_dir = script_dir.parent().unwrap().join("cache"); + let r6_dir = script_dir.parent().unwrap(); // load manifest without fallback let manifest = ScriptManifest::load(&script_dir); // set up logger with an optional manifest setup_logger( - &cache_dir, + r6_dir, manifest .as_ref() .ok() @@ -48,9 +48,23 @@ fn main() -> Result<(), Error> { ScriptManifest::default() }); + let (cache_dir, bundle_dir_override) = match (iter.next().as_deref(), iter.next()) { + (Some("-customCacheDir"), Some(custom_path)) => { + log::info!("Custom cache directory provided: {}", custom_path); + let cache_dir = PathBuf::from(custom_path); + if !cache_dir.exists() { + std::fs::create_dir_all(&cache_dir)?; + (cache_dir, Some(r6_dir.join("cache"))) + } else { + (cache_dir, None) + } + } + _ => (r6_dir.join("cache"), None), + }; + let files = Files::from_dir(&script_dir, manifest.source_filter())?; - match load_scripts(&cache_dir, &files) { + match load_scripts(&cache_dir, bundle_dir_override.as_deref(), &files) { Ok(_) => { log::info!("Output successfully saved in {}", cache_dir.display()); } @@ -70,9 +84,7 @@ fn main() -> Result<(), Error> { Ok(()) } -fn setup_logger(cache_dir: &Path, include_date_in_filename: bool) -> Result<(), Error> { - let parent_dir = cache_dir.parent().unwrap(); - +fn setup_logger(r6_dir: &Path, include_date_in_filename: bool) -> Result<(), Error> { let log_file_name = if include_date_in_filename { const DATE_FORMAT: &[FormatItem] = format_description!("[year].[month].[day]_[hour]-[minute]-[second]"); let date = OffsetDateTime::now_utc().format(&DATE_FORMAT).unwrap(); @@ -81,7 +93,7 @@ fn setup_logger(cache_dir: &Path, include_date_in_filename: bool) -> Result<(), "redscript.log".to_owned() }; - let log_dir = &parent_dir.join("logs"); + let log_dir = &r6_dir.join("logs"); if !log_dir.exists() { fs::create_dir(log_dir)?; @@ -100,10 +112,12 @@ fn setup_logger(cache_dir: &Path, include_date_in_filename: bool) -> Result<(), Ok(()) } -fn load_scripts(cache_dir: &Path, files: &Files) -> Result<(), Error> { - let bundle_path = cache_dir.join("final.redscripts"); - let backup_path = cache_dir.join("final.redscripts.bk"); - let timestamp_path = cache_dir.join("redscript.ts"); +fn load_scripts(cache_dir: &Path, bundle_dir_override: Option<&Path>, files: &Files) -> Result<(), Error> { + let input_dir = bundle_dir_override.unwrap_or(cache_dir); + let bundle_input_path = input_dir.join("final.redscripts"); + let bundle_output_path = cache_dir.join("final.redscripts"); + let backup_path = input_dir.join("final.redscripts.bk"); + let timestamp_path = input_dir.join("redscript.ts"); let mut ts_lock = RwLock::new( OpenOptions::new() .read(true) @@ -112,7 +126,7 @@ fn load_scripts(cache_dir: &Path, files: &Files) -> Result<(), Error> { .open(×tamp_path)?, ); let mut ts_file = ts_lock.write()?; - let write_timestamp = CompileTimestamp::of_cache_file(&File::open(&bundle_path)?)?; + let write_timestamp = CompileTimestamp::of_cache_file(&File::open(&bundle_input_path)?)?; let saved_timestamp = CompileTimestamp::read(ts_file.deref_mut()).ok(); match saved_timestamp { @@ -124,7 +138,7 @@ fn load_scripts(cache_dir: &Path, files: &Files) -> Result<(), Error> { "Redscript cache file is not ours, copying it to {}", backup_path.display() ); - fs::copy(&bundle_path, &backup_path)?; + fs::copy(&bundle_input_path, &backup_path)?; } Some(_) if !backup_path.exists() => { log::warn!("A compiler timestamp was found but not the backup file, your installation might be corrupted, try removing redscript.ts and verifying game files"); @@ -144,7 +158,7 @@ fn load_scripts(cache_dir: &Path, files: &Files) -> Result<(), Error> { CompilationUnit::new(&mut bundle.pool, vec![])?.compile_and_report(files)?; - let mut file = File::create(&bundle_path)?; + let mut file = File::create(&bundle_output_path)?; bundle.save(&mut io::BufWriter::new(&mut file))?; file.sync_all()?;