Skip to content

Commit

Permalink
refactor: Switch Shortcut::steam_id from a field to a method
Browse files Browse the repository at this point in the history
  • Loading branch information
CosmicHorrorDev committed Nov 6, 2024
1 parent 7ee85db commit 0f05557
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions src/shortcut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,34 +31,33 @@ pub struct Shortcut {
pub executable: String,
/// The directory that the application should be run in
pub start_dir: String,
/// The shortcut's Steam ID calculated from the executable path and app name
pub steam_id: u64,
}

impl Shortcut {
/// Calculates the shortcut's Steam ID from the executable and app name
pub fn new(app_id: u32, app_name: String, executable: String, start_dir: String) -> Self {
fn calculate_steam_id(executable: &[u8], app_name: &[u8]) -> u64 {
let algorithm = crc::Crc::<u32>::new(&crc::CRC_32_ISO_HDLC);

let mut digest = algorithm.digest();
digest.update(executable);
digest.update(app_name);

let top = digest.finalize() | 0x80000000;
((top as u64) << 32) | 0x02000000
}

let steam_id = calculate_steam_id(executable.as_bytes(), app_name.as_bytes());

Self {
app_id,
app_name,
executable,
start_dir,
steam_id,
}
}

/// The shortcut's Steam ID calculated from the executable path and app name
pub fn steam_id(&self) -> u64 {
let executable = self.executable.as_bytes();
let app_name = self.app_name.as_bytes();

let algorithm = crc::Crc::<u32>::new(&crc::CRC_32_ISO_HDLC);

let mut digest = algorithm.digest();
digest.update(executable);
digest.update(app_name);

let top = digest.finalize() | 0x80000000;
((top as u64) << 32) | 0x02000000
}
}

/// An [`Iterator`] over a Steam installation's [`Shortcut`]s
Expand Down Expand Up @@ -237,24 +236,29 @@ mod tests {
app_name: "Anki".into(),
executable: "\"anki\"".into(),
start_dir: "\"./\"".into(),
steam_id: 0xe89614fe02000000,
},
Shortcut {
app_id: 2492174738,
app_name: "LibreOffice Calc".into(),
executable: "\"libreoffice\"".into(),
start_dir: "\"./\"".into(),
steam_id: 0xdb01c79902000000,
},
Shortcut {
app_id: 3703025501,
app_name: "foo.sh".into(),
executable: "\"/usr/local/bin/foo.sh\"".into(),
start_dir: "\"/usr/local/bin/\"".into(),
steam_id: 0x9d55017302000000,
}
],
);
let steam_ids: Vec<_> = shortcuts
.iter()
.map(|shortcut| shortcut.steam_id())
.collect();
assert_eq!(
steam_ids,
[0xe89614fe02000000, 0xdb01c79902000000, 0x9d55017302000000,]
);

let contents = include_bytes!("../tests/sample_data/shortcuts_different_key_case.vdf");
let shortcuts = parse_shortcuts(contents).unwrap();
Expand All @@ -265,7 +269,6 @@ mod tests {
app_name: "Second Life".into(),
executable: "\"/Applications/Second Life Viewer.app\"".into(),
start_dir: "\"/Applications/\"".into(),
steam_id: 0xfdd972df02000000,
}]
);
}
Expand Down

0 comments on commit 0f05557

Please sign in to comment.