Skip to content

Commit

Permalink
[emucontrol] Add the ability to clear the download cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Krusty/Benediction committed Aug 18, 2024
1 parent 9446ee6 commit 52383f7
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
Empty file modified cpclib-basm/pgo.sh
100644 → 100755
Empty file.
Empty file modified cpclib-bdasm/tries.sh
100644 → 100755
Empty file.
19 changes: 14 additions & 5 deletions cpclib-runner/src/delegated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,26 @@ pub struct DelegateApplicationDescription {
pub compile: Option<Box<dyn Fn(&Utf8Path) -> Result<(), String>>>
}


pub fn base_cache_foder() -> Utf8PathBuf {
let proj_dirs = ProjectDirs::from("net.cpcscene", "benediction", "bnd build").unwrap();
Utf8Path::from_path(proj_dirs.cache_dir()).unwrap().to_owned()
}

pub fn clear_base_cache_folder() -> std::io::Result<()> {
std::fs::remove_dir_all(base_cache_foder())
}

impl DelegateApplicationDescription {
pub fn is_cached(&self) -> bool {
self.cache_folder().exists()
}

pub fn cache_folder(&self) -> Utf8PathBuf {
let proj_dirs = ProjectDirs::from("net.cpcscene", "benediction", "bnd build").unwrap();
let base_cache = proj_dirs.cache_dir();
let base_cache = base_cache_foder();

if !base_cache.exists() {
std::fs::create_dir_all(base_cache);
std::fs::create_dir_all(&base_cache).unwrap();
}

base_cache.join(self.folder).try_into().unwrap()
Expand All @@ -46,8 +55,7 @@ impl DelegateApplicationDescription {
// get the file
let dest = self.cache_folder();

println!(">> Download file");
let resp = self.download().unwrap();
let resp = self.download().map_err(|e| format!("Unable to download the expected file. {}", e.to_string()))?;
let mut input = resp.into_reader();

// uncompress it
Expand Down Expand Up @@ -91,6 +99,7 @@ impl DelegateApplicationDescription {
}

fn download(&self) -> Result<Response, ureq::Error> {
println!(">> Download file {}", self.download_url);
ureq::get(self.download_url).call()
}
}
Expand Down
20 changes: 19 additions & 1 deletion cpclib-runner/src/emucontrol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use xcap::Window;
#[cfg(windows)]
use fs_extra;

use crate::delegated::DelegatedRunner;
use crate::delegated::{clear_base_cache_folder, DelegatedRunner};
use crate::runner::emulator::Emulator;
use crate::runner::runner::RunnerWithClap;
use crate::runner::Runner;
Expand Down Expand Up @@ -594,6 +594,9 @@ pub struct Cli {
#[arg(short, long, action = ArgAction::SetTrue, help = "Keep the emulator open after the interaction")]
keepemulator: bool,

#[arg(short, long, action = ArgAction::SetTrue, help = "Clear the cache folder")]
clear_cache: bool,

#[command(subcommand)]
command: Commands
}
Expand Down Expand Up @@ -665,6 +668,13 @@ impl RunnerWithClap for EmuControlledRunner {


pub fn handle_arguments(mut cli: Cli) -> Result<(), String> {

if cli.clear_cache {
clear_base_cache_folder()
.map_err(|e| format!("Unable to clear the cache folder. {}", e.to_string()))?;
}


let builder = EmulatorConf::builder()
.maybe_drive_a(cli.drive_a.clone())
.maybe_drive_b(cli.drive_b.clone());
Expand All @@ -676,6 +686,14 @@ pub fn handle_arguments(mut cli: Cli) -> Result<(), String> {
Emu::Cpcec => Emulator::Cpcec(Default::default())
};


{ // ensure emulator is isntalled
let conf = emu.configuration();
if ! conf.is_cached() {
conf.install()?;
}
}

// setup emulator
// TODO do it conditionally
// copy the non standard roms
Expand Down

0 comments on commit 52383f7

Please sign in to comment.