Skip to content

Commit

Permalink
fix: core-specific save state screenshots
Browse files Browse the repository at this point in the history
  • Loading branch information
goweiwen committed Jun 7, 2024
1 parent c194753 commit e563c4c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
3 changes: 2 additions & 1 deletion crates/allium-menu/src/allium_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,13 @@ impl AlliumMenu<DefaultPlatform> {
self.display.load(self.display.bounding_box().into())?;
self.view.set_should_draw();
}
Command::SaveStateScreenshot { path, slot } => {
Command::SaveStateScreenshot { path, core, slot } => {
if self.display.pop() {
self.display.load(self.display.bounding_box().into())?;
self.display.flush()?;
let mut hasher = Sha256::new();
hasher.update(path);
hasher.update(core);
hasher.update(slot.to_le_bytes());
let hash = hasher.finalize();
let base32 = encode(base32::Alphabet::Crockford, &hash);
Expand Down
22 changes: 19 additions & 3 deletions crates/allium-menu/src/view/ingame_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ where
commands
.send(Command::SaveStateScreenshot {
path: self.path.canonicalize()?.to_string_lossy().to_string(),
core: self.res.get::<GameInfo>().core.clone(),
slot,
})
.await?;
Expand Down Expand Up @@ -257,6 +258,7 @@ where
commands
.send(Command::SaveStateScreenshot {
path: self.path.canonicalize()?.to_string_lossy().to_string(),
core: self.res.get::<GameInfo>().core.clone(),
slot: -1,
})
.await?;
Expand Down Expand Up @@ -306,14 +308,28 @@ where
.to_string_lossy()
.to_string();
let slot = self.retroarch_info.as_ref().unwrap().state_slot.unwrap();

let mut hasher = Sha256::new();
hasher.update(path);
hasher.update(&path);
hasher.update(&self.res.get::<GameInfo>().core);
hasher.update(slot.to_le_bytes());
let hash = hasher.finalize();
let base32 = encode(base32::Alphabet::Crockford, &hash);
let file_name = format!("{}.png", base32);
let path = ALLIUM_SCREENSHOTS_DIR.join(file_name);
self.image.set_path(Some(path));
let mut screenshot_path = ALLIUM_SCREENSHOTS_DIR.join(file_name);

// Previously, the hash did not include the core name. We try looking for that path as well.
if !screenshot_path.exists() {
let mut hasher = Sha256::new();
hasher.update(&path);
hasher.update(slot.to_le_bytes());
let hash = hasher.finalize();
let base32 = encode(base32::Alphabet::Crockford, &hash);
let file_name = format!("{}.png", base32);
screenshot_path = ALLIUM_SCREENSHOTS_DIR.join(file_name);
}

self.image.set_path(Some(screenshot_path));
}
}

Expand Down
6 changes: 5 additions & 1 deletion crates/common/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ pub enum Command {
Search(String),
Toast(String, Option<Duration>),
PopulateDb,
SaveStateScreenshot { path: String, slot: i8 },
SaveStateScreenshot {
path: String,
core: String,
slot: i8,
},
}

#[derive(Debug, Clone)]
Expand Down
8 changes: 7 additions & 1 deletion crates/common/src/game_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ use crate::constants::{ALLIUM_GAMES_DIR, ALLIUM_GAME_INFO, ALLIUM_SCRIPTS_DIR};
pub struct GameInfo {
/// Display name of the game.
pub name: String,
/// Path to the game rom.
/// Path to the game rom. This is used to generate the screenshot name.
pub path: PathBuf,
/// Core used to run the game. This is used to generate the screenshot name.
pub core: String,
/// Command to run the core.
pub command: String,
/// Arguments to pass to the core to run the game.
Expand All @@ -39,6 +41,7 @@ impl Default for GameInfo {
Self {
name: String::new(),
path: PathBuf::new(),
core: String::new(),
command: String::new(),
args: Vec::new(),
has_menu: false,
Expand All @@ -52,9 +55,11 @@ impl Default for GameInfo {

impl GameInfo {
/// Create a new GameInfo object.
#[allow(clippy::too_many_arguments)]
pub fn new(
name: String,
path: PathBuf,
core: String,
image: Option<PathBuf>,
command: String,
args: Vec<String>,
Expand All @@ -66,6 +71,7 @@ impl GameInfo {
Self {
name,
path,
core,
command,
args,
has_menu,
Expand Down

0 comments on commit e563c4c

Please sign in to comment.