Skip to content

Commit

Permalink
allow loading zip files
Browse files Browse the repository at this point in the history
  • Loading branch information
griffi-gh committed Feb 1, 2024
1 parent 683ac4f commit 78288bc
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
13 changes: 13 additions & 0 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion yarge-frontend-sdl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ bincode = "1.3"
anyhow = "1.0"
dirs = { version = "5.0", optional = true }
dark-light = { version = "1.0", optional = true }
zip = { version = "0.6", default-features = false, features = ["deflate"], optional = true }

[target.'cfg(windows)'.dependencies]
windows = { version = "0.52", optional = true }
Expand All @@ -25,12 +26,13 @@ windows = { version = "0.52", optional = true }
winres = { version = "0.1", optional = true }

[features]
default = ["global_config", "hidpi", "system-theme"]
default = ["global_config", "hidpi", "system-theme", "archive"]
production = ["windows-icon"]
global_config = ["dep:dirs"]
hidpi = ["dep:windows", "windows/Win32_UI_HiDpi"]
system-theme = ["dep:dark-light"]
windows-icon = ["dep:winres"]
archive = ["dep:zip"]

[package.metadata.deb]
maintainer = "griffi-gh <[email protected]>"
Expand Down
29 changes: 26 additions & 3 deletions yarge-frontend-sdl/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#![cfg_attr(target_os = "windows", cfg_attr(feature = "production", windows_subsystem = "windows"))]

use yarge_core::{
consts::{WIDTH as GB_WIDTH, HEIGHT as GB_HEIGHT},
Gameboy,
Key as GbKey,
consts::{WIDTH as GB_WIDTH, HEIGHT as GB_HEIGHT}
YargeError
};
use sdl2::{
pixels::{PixelFormatEnum, Color},
Expand All @@ -12,7 +13,7 @@ use sdl2::{
render::BlendMode,
};
use clap::Parser;
use std::{path::PathBuf, time::Instant};
use std::{io::Read, path::PathBuf, time::Instant};

mod audio;
mod menu;
Expand Down Expand Up @@ -69,6 +70,28 @@ impl Default for URStorage {
}
}

/// Load ROM file, with support for ZIP files
pub(crate) fn load_rom_helper(gb: &mut Gameboy, data: &[u8]) -> Result<(), YargeError> {
//TODO handle failures more gracefully
#[cfg(feature = "archive")]
if data[0..2] == [0x50, 0x4B] {
println!("looks like a zip file");
let cursor = std::io::Cursor::new(data);
let mut zip = zip::ZipArchive::new(cursor).unwrap();
if zip.is_empty() {
panic!("zip is empty");
}
if zip.file_names().count() > 1 {
panic!("zip contains more than one file");
}
let mut buf = vec![];
zip.by_index(0).unwrap().read_to_end(&mut buf).unwrap();
gb.load_rom(&buf)?;
return Ok(())
}
gb.load_rom(data)
}

fn main() {
//Set dpi aware flag on windows
#[cfg(all(windows, feature = "hidpi"))] {
Expand Down Expand Up @@ -103,7 +126,7 @@ fn main() {
//Load the ROM file
if let Some(path) = args.rom_path.as_ref() {
let rom = std::fs::read(path).expect("Failed to load the ROM file");
gb.load_rom(&rom).expect("Invalid ROM file");
load_rom_helper(&mut gb, &rom).expect("Invalid ROM file");
config.last_rom = Some(path.into());
}

Expand Down
2 changes: 1 addition & 1 deletion yarge-frontend-sdl/src/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ impl Menu {
pub fn load_file(&mut self, path: PathBuf, gb: &mut Gameboy, config: &Configuration) {
let data = fs::read(path).unwrap();
gb.reset();
gb.load_rom(&data[..]).unwrap();
crate::load_rom_helper(gb, &data[..]).unwrap();
SaveManager::load_idk(gb, config.save_slot);
SaveManager::save(gb, config.save_slot).unwrap(); //Create save file
}
Expand Down

0 comments on commit 78288bc

Please sign in to comment.