Skip to content

Commit

Permalink
fix crash if failed to extract cached resource
Browse files Browse the repository at this point in the history
  • Loading branch information
HJfod committed Apr 3, 2023
1 parent 67cc142 commit c2e4e97
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "geode"
version = "2.2.3"
version = "2.2.4"
authors = ["HJfod <[email protected]>", "Camila314 <[email protected]>"]
edition = "2021"
build = "build.rs"
Expand Down
2 changes: 1 addition & 1 deletion src/util/bmfont.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ fn extract_from_cache(
if !shut_up {
info!("Extracting '{}' from cache", path_name);
}
cache_bundle.extract_cached_into(
cache_bundle.try_extract_cached_into(
path_name,
&working_dir.join(path.file_name().unwrap().to_str().unwrap()),
);
Expand Down
17 changes: 12 additions & 5 deletions src/util/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,29 @@ pub struct CacheBundle {
}

impl CacheBundle {
pub fn extract_cached_into(&mut self, name: &str, output: &PathBuf) {
pub fn try_extract_cached_into(&mut self, name: &str, output: &PathBuf) -> bool {
match &mut self.src {
CacheBundleSource::Archive(archive) => {
let mut cached_file = archive.by_name(name).unwrap();
let Ok(mut cached_file) = archive.by_name(name) else {
return false;
};

// Read cached file to buffer
let mut buf: Vec<u8> = Vec::new();
cached_file.read_to_end(&mut buf).unwrap();
let Ok(_) = cached_file.read_to_end(&mut buf) else {
return false;
};

// Write buffer into output directory, same file name
std::fs::write(output, buf).unwrap();
std::fs::write(output, buf).is_ok()
}

CacheBundleSource::Directory(dir) => {
if dir.join(name) != *output {
std::fs::copy(dir.join(name), output).unwrap();
std::fs::copy(dir.join(name), output).is_ok()
}
else {
false
}
}
}
Expand Down
50 changes: 32 additions & 18 deletions src/util/spritesheet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,33 +198,28 @@ fn initialize_spritesheet_bundle(
);
}

fn extract_from_cache(
fn try_extract_from_cache(
path: &Path,
working_dir: &Path,
cache_bundle: &mut CacheBundle,
shut_up: bool,
) {
) -> bool {
let path_name = path.to_str().unwrap();
if !shut_up {
info!("Extracting '{}' from cache", path_name);
}
cache_bundle.extract_cached_into(
cache_bundle.try_extract_cached_into(
path_name,
&working_dir.join(path.file_name().unwrap().to_str().unwrap()),
);
)
}

pub fn get_spritesheet_bundles(
fn try_extract_bundles_from_cache(
sheet: &SpriteSheet,
working_dir: &Path,
cache: &mut Option<CacheBundle>,
mod_info: &ModFileInfo,
shut_up: bool,
) -> SheetBundles {
if !shut_up {
info!("Fetching spritesheet {}", sheet.name.bright_yellow());
}

) -> Option<SheetBundles> {
if let Some(cache_bundle) = cache {
// Cache found
if let Some(p) = cache_bundle.cache.fetch_spritesheet_bundles(sheet) {
Expand All @@ -234,18 +229,37 @@ pub fn get_spritesheet_bundles(
let bundles = SheetBundles::new(p.to_path_buf());

// Extract all files
extract_from_cache(&bundles.sd.png, working_dir, cache_bundle, shut_up);
extract_from_cache(&bundles.sd.plist, working_dir, cache_bundle, shut_up);
extract_from_cache(&bundles.hd.png, working_dir, cache_bundle, shut_up);
extract_from_cache(&bundles.hd.plist, working_dir, cache_bundle, shut_up);
extract_from_cache(&bundles.uhd.png, working_dir, cache_bundle, shut_up);
extract_from_cache(&bundles.uhd.plist, working_dir, cache_bundle, shut_up);
try_extract_from_cache(&bundles.sd.png, working_dir, cache_bundle, shut_up).then_some(())?;
try_extract_from_cache(&bundles.sd.plist, working_dir, cache_bundle, shut_up).then_some(())?;
try_extract_from_cache(&bundles.hd.png, working_dir, cache_bundle, shut_up).then_some(())?;
try_extract_from_cache(&bundles.hd.plist, working_dir, cache_bundle, shut_up).then_some(())?;
try_extract_from_cache(&bundles.uhd.png, working_dir, cache_bundle, shut_up).then_some(())?;
try_extract_from_cache(&bundles.uhd.plist, working_dir, cache_bundle, shut_up).then_some(())?;

done!("Fetched {} from cache", sheet.name.bright_yellow());
return bundles;
return Some(bundles);
}
}
None
}

pub fn get_spritesheet_bundles(
sheet: &SpriteSheet,
working_dir: &Path,
cache: &mut Option<CacheBundle>,
mod_info: &ModFileInfo,
shut_up: bool,
) -> SheetBundles {
if !shut_up {
info!("Fetching spritesheet {}", sheet.name.bright_yellow());
}

if let Some(cached) = try_extract_bundles_from_cache(
sheet, working_dir, cache, shut_up
) {
return cached;
}

if !shut_up {
info!("Sheet is not cached, building from scratch");
}
Expand Down

0 comments on commit c2e4e97

Please sign in to comment.