Add option to only import installed games (#313)

This commit is contained in:
Philip Kristoffersen
2023-01-05 22:12:36 +01:00
committed by GitHub
parent ee76886422
commit 304e490c4c
2 changed files with 34 additions and 9 deletions
+16 -3
View File
@@ -1,6 +1,6 @@
use nom::{
bytes::{
complete::{take_until, take_while},
complete::{tag, take_until, take_while},
streaming::take,
},
character::is_alphanumeric,
@@ -11,6 +11,7 @@ use nom::{
pub(crate) struct NamesAndId {
pub(crate) name: String,
pub(crate) id: String,
pub(crate) installed: bool,
}
pub(crate) fn parse_db<'a>(content: &'a [u8]) -> nom::IResult<&'a [u8], Vec<NamesAndId>> {
@@ -26,12 +27,24 @@ fn parse_game(i: &[u8]) -> nom::IResult<&[u8], NamesAndId> {
.last()
.unwrap_or_default();
let id = String::from_utf8_lossy(id_bytes).to_string();
let (i, _taken) = take_until("IsInstalled")(i)?;
let (i, _taken) = tag("IsInstalled")(i)?;
let installed = match i.get(1) {
Some(1u8) => true,
_ => false,
};
let (i, _taken) = take_until("InstallSizeGroup")(i)?;
let (i, _taken) = take_until("Name")(i)?;
let (i, _taken) = take(4usize)(i)?;
let (i, _taken) = take_while(|b| !is_alphanumeric(b))(i)?;
let (i, name_bytes) = take_while(|b| b != 0)(i)?;
let name = String::from_utf8_lossy(name_bytes).to_string();
IResult::Ok((i, NamesAndId { id, name }))
IResult::Ok((
i,
NamesAndId {
id,
name,
installed,
},
))
}
+18 -6
View File
@@ -25,11 +25,15 @@ pub struct PlaynitePlatform {
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct PlayniteSettings {
pub enabled: bool,
pub installed_only: bool,
}
impl Default for PlayniteSettings {
fn default() -> Self {
Self { enabled: true }
Self {
enabled: true,
installed_only: true,
}
}
}
@@ -57,6 +61,12 @@ impl GamesPlatform for PlaynitePlatform {
fn render_ui(&mut self, ui: &mut egui::Ui) {
ui.heading("Playnite");
ui.checkbox(&mut self.settings.enabled, "Import from Playnite");
if self.settings.enabled {
ui.checkbox(
&mut self.settings.installed_only,
"Only import installed games",
);
}
}
}
@@ -78,11 +88,13 @@ impl PlaynitePlatform {
let games_bytes = std::fs::read(&games_file_path).unwrap();
let (_, games) = parse_db(&games_bytes).map_err(|e| eyre::eyre!(e.to_string()))?;
for game in games {
res.push(PlayniteGame {
id: game.id,
launcher_path: launcher_path.clone().into(),
name: game.name,
});
if game.installed || !self.settings.installed_only {
res.push(PlayniteGame {
id: game.id,
launcher_path: launcher_path.clone().into(),
name: game.name,
});
}
}
}
Ok(res)