Skip to content

Commit c2e4e97

Browse files
committed
fix crash if failed to extract cached resource
1 parent 67cc142 commit c2e4e97

File tree

5 files changed

+47
-26
lines changed

5 files changed

+47
-26
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "geode"
3-
version = "2.2.3"
3+
version = "2.2.4"
44
authors = ["HJfod <[email protected]>", "Camila314 <[email protected]>"]
55
edition = "2021"
66
build = "build.rs"

src/util/bmfont.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ fn extract_from_cache(
322322
if !shut_up {
323323
info!("Extracting '{}' from cache", path_name);
324324
}
325-
cache_bundle.extract_cached_into(
325+
cache_bundle.try_extract_cached_into(
326326
path_name,
327327
&working_dir.join(path.file_name().unwrap().to_str().unwrap()),
328328
);

src/util/cache.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,29 @@ pub struct CacheBundle {
2121
}
2222

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

2931
// Read cached file to buffer
3032
let mut buf: Vec<u8> = Vec::new();
31-
cached_file.read_to_end(&mut buf).unwrap();
33+
let Ok(_) = cached_file.read_to_end(&mut buf) else {
34+
return false;
35+
};
3236

3337
// Write buffer into output directory, same file name
34-
std::fs::write(output, buf).unwrap();
38+
std::fs::write(output, buf).is_ok()
3539
}
3640

3741
CacheBundleSource::Directory(dir) => {
3842
if dir.join(name) != *output {
39-
std::fs::copy(dir.join(name), output).unwrap();
43+
std::fs::copy(dir.join(name), output).is_ok()
44+
}
45+
else {
46+
false
4047
}
4148
}
4249
}

src/util/spritesheet.rs

+32-18
Original file line numberDiff line numberDiff line change
@@ -198,33 +198,28 @@ fn initialize_spritesheet_bundle(
198198
);
199199
}
200200

201-
fn extract_from_cache(
201+
fn try_extract_from_cache(
202202
path: &Path,
203203
working_dir: &Path,
204204
cache_bundle: &mut CacheBundle,
205205
shut_up: bool,
206-
) {
206+
) -> bool {
207207
let path_name = path.to_str().unwrap();
208208
if !shut_up {
209209
info!("Extracting '{}' from cache", path_name);
210210
}
211-
cache_bundle.extract_cached_into(
211+
cache_bundle.try_extract_cached_into(
212212
path_name,
213213
&working_dir.join(path.file_name().unwrap().to_str().unwrap()),
214-
);
214+
)
215215
}
216216

217-
pub fn get_spritesheet_bundles(
217+
fn try_extract_bundles_from_cache(
218218
sheet: &SpriteSheet,
219219
working_dir: &Path,
220220
cache: &mut Option<CacheBundle>,
221-
mod_info: &ModFileInfo,
222221
shut_up: bool,
223-
) -> SheetBundles {
224-
if !shut_up {
225-
info!("Fetching spritesheet {}", sheet.name.bright_yellow());
226-
}
227-
222+
) -> Option<SheetBundles> {
228223
if let Some(cache_bundle) = cache {
229224
// Cache found
230225
if let Some(p) = cache_bundle.cache.fetch_spritesheet_bundles(sheet) {
@@ -234,18 +229,37 @@ pub fn get_spritesheet_bundles(
234229
let bundles = SheetBundles::new(p.to_path_buf());
235230

236231
// Extract all files
237-
extract_from_cache(&bundles.sd.png, working_dir, cache_bundle, shut_up);
238-
extract_from_cache(&bundles.sd.plist, working_dir, cache_bundle, shut_up);
239-
extract_from_cache(&bundles.hd.png, working_dir, cache_bundle, shut_up);
240-
extract_from_cache(&bundles.hd.plist, working_dir, cache_bundle, shut_up);
241-
extract_from_cache(&bundles.uhd.png, working_dir, cache_bundle, shut_up);
242-
extract_from_cache(&bundles.uhd.plist, working_dir, cache_bundle, shut_up);
232+
try_extract_from_cache(&bundles.sd.png, working_dir, cache_bundle, shut_up).then_some(())?;
233+
try_extract_from_cache(&bundles.sd.plist, working_dir, cache_bundle, shut_up).then_some(())?;
234+
try_extract_from_cache(&bundles.hd.png, working_dir, cache_bundle, shut_up).then_some(())?;
235+
try_extract_from_cache(&bundles.hd.plist, working_dir, cache_bundle, shut_up).then_some(())?;
236+
try_extract_from_cache(&bundles.uhd.png, working_dir, cache_bundle, shut_up).then_some(())?;
237+
try_extract_from_cache(&bundles.uhd.plist, working_dir, cache_bundle, shut_up).then_some(())?;
243238

244239
done!("Fetched {} from cache", sheet.name.bright_yellow());
245-
return bundles;
240+
return Some(bundles);
246241
}
247242
}
243+
None
244+
}
245+
246+
pub fn get_spritesheet_bundles(
247+
sheet: &SpriteSheet,
248+
working_dir: &Path,
249+
cache: &mut Option<CacheBundle>,
250+
mod_info: &ModFileInfo,
251+
shut_up: bool,
252+
) -> SheetBundles {
253+
if !shut_up {
254+
info!("Fetching spritesheet {}", sheet.name.bright_yellow());
255+
}
248256

257+
if let Some(cached) = try_extract_bundles_from_cache(
258+
sheet, working_dir, cache, shut_up
259+
) {
260+
return cached;
261+
}
262+
249263
if !shut_up {
250264
info!("Sheet is not cached, building from scratch");
251265
}

0 commit comments

Comments
 (0)