Skip to content

Simplify game resolver code and add support for platform specifiers #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 26 additions & 43 deletions src/game/import/ea.rs
Original file line number Diff line number Diff line change
@@ -1,53 +1,36 @@
use std::path::PathBuf;

use super::GameImporter;
use super::ImportOverrides;
use crate::error::Error;
use crate::game::error::GameError;
use crate::game::import::ImportBase;
use crate::game::registry::{ActiveDistribution, GameData};
use crate::ts::v1::models::ecosystem::GameDefPlatform;
use crate::game::registry::ActiveDistribution;
use crate::ts::v1::models::ecosystem::{GameDef, GamePlatform};
use crate::util::reg::{self, HKey};

pub struct EaImporter {
ident: String,
}

impl EaImporter {
pub fn new(ident: &str) -> EaImporter {
EaImporter {
ident: ident.into(),
}
}
}

impl GameImporter for EaImporter {
fn construct(self: Box<Self>, base: ImportBase) -> Result<GameData, Error> {
let subkey = format!("Software\\{}\\", self.ident.replace('.', "\\"));
let value = reg::get_value_at(HKey::LocalMachine, &subkey, "Install Dir")?;
pub fn get_gamedist(ident: &str, game_def: &GameDef, overrides: &ImportOverrides) -> Result<Option<ActiveDistribution>, Error> {

Check failure

Code scanning / clippy

? couldn't convert the error to error::Error Error

? couldn't convert the error to error::Error
let subkey = format!("Software\\{}\\", ident.replace('.', "\\"));
let value = reg::get_value_at(HKey::LocalMachine, &subkey, "Install Dir")?;

Check failure

Code scanning / clippy

? couldn't convert the error to error::Error Error

? couldn't convert the error to error::Error

Check failure

Code scanning / clippy

? couldn't convert the error to error::Error Error

? couldn't convert the error to error::Error

let game_dir = PathBuf::from(value);
let r2mm = base.game_def.r2modman.as_ref().expect(
"Expected a valid r2mm field in the ecosystem schema, got nothing. This is a bug.",
);
let game_dir = PathBuf::from(value);
let r2mm = game_def.r2modman.as_ref().expect(
"Expected a valid r2mm field in the ecosystem schema, got nothing. This is a bug.",
).first().unwrap();

let exe_path = base
.overrides
.custom_exe
.clone()
.or_else(|| super::find_game_exe(&r2mm.exe_names, &game_dir))
.ok_or_else(|| GameError::ExeNotFound {
possible_names: r2mm.exe_names.clone(),
base_path: game_dir.clone(),
})?;
let dist = ActiveDistribution {
dist: GameDefPlatform::Origin {
identifier: self.ident.to_string(),
},
game_dir: game_dir.to_path_buf(),
data_dir: game_dir.join(&r2mm.data_folder_name),
exe_path,
};
let exe_path = overrides
.custom_exe
.clone()
.or_else(|| super::find_game_exe(&r2mm.exe_names, &game_dir))
.ok_or_else(|| GameError::ExeNotFound {
possible_names: r2mm.exe_names.clone(),
base_path: game_dir.clone(),
})?;

Ok(super::construct_data(base, dist))
}
Ok(Some(ActiveDistribution {
dist: GamePlatform::Origin {
identifier: ident.to_string(),
},
game_dir: game_dir.to_path_buf(),
data_dir: game_dir.join(&r2mm.data_folder_name),
exe_path,
}))
}
126 changes: 55 additions & 71 deletions src/game/import/egs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

use serde::{Deserialize, Serialize};

use super::{GameImporter, ImportBase};
use super::ImportOverrides;
use crate::error::{Error, IoError};
use crate::game::error::GameError;
use crate::game::registry::{ActiveDistribution, GameData};
use crate::ts::v1::models::ecosystem::GameDefPlatform;
use crate::game::registry::ActiveDistribution;
use crate::ts::v1::models::ecosystem::{GameDef, GamePlatform};
use crate::util::reg::{self, HKey};

#[derive(Serialize, Deserialize, Debug)]
Expand All @@ -17,81 +17,65 @@
app_name: String,
}

pub struct EgsImporter {
ident: String,
}

impl EgsImporter {
pub fn new(ident: &str) -> EgsImporter {
EgsImporter {
ident: ident.into(),
}
}
}
pub fn get_gamedist(ident: &str, game_def: &GameDef, overrides: &ImportOverrides) -> Result<Option<ActiveDistribution>, Error> {

Check failure

Code scanning / clippy

? couldn't convert the error to error::Error Error

? couldn't convert the error to error::Error
let game_label = game_def.label.clone();

impl GameImporter for EgsImporter {
fn construct(self: Box<Self>, base: ImportBase) -> Result<GameData, Error> {
let game_label = base.game_def.label.clone();
// There's a couple ways that we can retrieve the path of a game installed via EGS.
// 1. Parse LauncherInstalled.dat in C:/ProgramData/Epic/UnrealEngineLauncher/
// 2. Parse game manifest files in C:/ProgramData/Epic/EpicGamesLauncher/Data/Manifests
// I'm going to go for the second option.

// There's a couple ways that we can retrieve the path of a game installed via EGS.
// 1. Parse LauncherInstalled.dat in C:/ProgramData/Epic/UnrealEngineLauncher/
// 2. Parse game manifest files in C:/ProgramData/Epic/EpicGamesLauncher/Data/Manifests
// I'm going to go for the second option.
// Attempt to get the path of the EGS /Data directory from the registry.
let subkey = r#"Software\WOW64Node\Epic Games\EpicGamesLauncher"#;
let value = reg::get_value_at(HKey::LocalMachine, subkey, "AppDataPath")?;

Check failure

Code scanning / clippy

? couldn't convert the error to error::Error Error

? couldn't convert the error to error::Error

Check failure

Code scanning / clippy

? couldn't convert the error to error::Error Error

? couldn't convert the error to error::Error
let manifests_dir = PathBuf::from(value).join("Manifests");

// Attempt to get the path of the EGS /Data directory from the registry.
let subkey = r#"Software\WOW64Node\Epic Games\EpicGamesLauncher"#;
let value = reg::get_value_at(HKey::LocalMachine, subkey, "AppDataPath")?;
let manifests_dir = PathBuf::from(value).join("Manifests");

if !manifests_dir.exists() {
Err(IoError::DirNotFound(manifests_dir.clone()))?;
}
if !manifests_dir.exists() {
Err(IoError::DirNotFound(manifests_dir.clone()))?;
}

// Manifest files are JSON files with .item extensions.
let manifest_files = fs::read_dir(manifests_dir)
.unwrap()
.filter_map(|x| x.ok())
.map(|x| x.path())
.filter(|x| x.is_file() && x.extension().is_some())
.filter(|x| x.extension().unwrap() == "item")
.collect::<Vec<_>>();
// Manifest files are JSON files with .item extensions.
let manifest_files = fs::read_dir(manifests_dir)
.unwrap()
.filter_map(|x| x.ok())
.map(|x| x.path())
.filter(|x| x.is_file() && x.extension().is_some())
.filter(|x| x.extension().unwrap() == "item")
.collect::<Vec<_>>();

// Search for the manifest which contains the correct game AppName.
let game_dir = manifest_files
.into_iter()
.find_map(|x| {
let file_contents = fs::read_to_string(x).unwrap();
let manifest: PartialInstallManifest =
serde_json::from_str(&file_contents).unwrap();
// Search for the manifest which contains the correct game AppName.
let game_dir = manifest_files
.into_iter()
.find_map(|x| {
let file_contents = fs::read_to_string(x).unwrap();
let manifest: PartialInstallManifest =
serde_json::from_str(&file_contents).unwrap();

if manifest.app_name == self.ident {
Some(manifest.install_location)
} else {
None
}
})
.ok_or_else(|| GameError::NotFound(game_label.clone(), "EGS".to_string()))?;
if manifest.app_name == ident {
Some(manifest.install_location)
} else {
None
}
})
.ok_or_else(|| GameError::NotFound(game_label.clone(), "EGS".to_string()))?;

let r2mm = base.game_def.r2modman.as_ref().expect(
"Expected a valid r2mm field in the ecosystem schema, got nothing. This is a bug.",
);
let r2mm = game_def.r2modman.as_ref().expect(
"Expected a valid r2mm field in the ecosystem schema, got nothing. This is a bug.",
).first().unwrap();

let exe_path = base
.overrides
.custom_exe
.clone()
.or_else(|| super::find_game_exe(&r2mm.exe_names, &game_dir))
.ok_or_else(|| GameError::ExeNotFound {
possible_names: r2mm.exe_names.clone(),
base_path: game_dir.clone(),
})?;
let dist = ActiveDistribution {
dist: GameDefPlatform::Other,
game_dir: game_dir.to_path_buf(),
data_dir: game_dir.join(&r2mm.data_folder_name),
exe_path,
};
let exe_path = overrides
.custom_exe
.clone()
.or_else(|| super::find_game_exe(&r2mm.exe_names, &game_dir))
.ok_or_else(|| GameError::ExeNotFound {
possible_names: r2mm.exe_names.clone(),
base_path: game_dir.clone(),
})?;

Ok(super::construct_data(base, dist))
}
Ok(Some(ActiveDistribution {
dist: GamePlatform::Other,
game_dir: game_dir.to_path_buf(),
data_dir: game_dir.join(&r2mm.data_folder_name),
exe_path,
}))
}
103 changes: 43 additions & 60 deletions src/game/import/gamepass.rs
Original file line number Diff line number Diff line change
@@ -1,67 +1,50 @@
use std::path::PathBuf;

use super::{GameImporter, ImportBase};
use super::ImportOverrides;
use crate::error::Error;
use crate::game::error::GameError;
use crate::game::registry::{ActiveDistribution, GameData};
use crate::ts::v1::models::ecosystem::GameDefPlatform;
use crate::game::registry::ActiveDistribution;
use crate::ts::v1::models::ecosystem::{GameDef, GamePlatform};
use crate::util::reg::{self, HKey};

pub struct GamepassImporter {
ident: String,
}

impl GamepassImporter {
pub fn new(ident: &str) -> GamepassImporter {
GamepassImporter {
ident: ident.into(),
}
}
}

impl GameImporter for GamepassImporter {
fn construct(self: Box<Self>, base: ImportBase) -> Result<GameData, Error> {
let root = r#"Software\Microsoft\GamingServices\PackageRepository"#;

let uuid = reg::get_values_at(HKey::LocalMachine, &format!("{root}\\Package\\"))?
.into_iter()
.find(|x| x.key.starts_with(&self.ident))
.ok_or_else(|| {
GameError::NotFound(base.game_def.label.clone(), "Gamepass".to_string())
})?
.val
.replace('\"', "");

let game_root = reg::get_keys_at(HKey::LocalMachine, &format!("Root\\{}\\", uuid))?
.into_iter()
.next()
.ok_or_else(|| {
GameError::NotFound(base.game_def.label.clone(), "Gamepass".to_string())
})?;
let game_dir = PathBuf::from(reg::get_value_at(HKey::LocalMachine, &game_root, "Root")?);

let r2mm = base.game_def.r2modman.as_ref().expect(
"Expected a valid r2mm field in the ecosystem schema, got nothing. This is a bug.",
);

let exe_path = base
.overrides
.custom_exe
.clone()
.or_else(|| super::find_game_exe(&r2mm.exe_names, &game_dir))
.ok_or_else(|| GameError::ExeNotFound {
possible_names: r2mm.exe_names.clone(),
base_path: game_dir.clone(),
})?;
let dist = ActiveDistribution {
dist: GameDefPlatform::GamePass {
identifier: self.ident.to_string(),
},
game_dir: game_dir.to_path_buf(),
data_dir: game_dir.join(&r2mm.data_folder_name),
exe_path,
};

Ok(super::construct_data(base, dist))
}
pub fn get_gamedist(ident: &str, game_def: &GameDef, overrides: &ImportOverrides) -> Result<Option<ActiveDistribution>, Error> {

Check failure

Code scanning / clippy

? couldn't convert the error to error::Error Error

? couldn't convert the error to error::Error

Check failure

Code scanning / clippy

? couldn't convert the error to error::Error Error

? couldn't convert the error to error::Error

Check failure

Code scanning / clippy

? couldn't convert the error to error::Error Error

? couldn't convert the error to error::Error
let root = r#"Software\Microsoft\GamingServices\PackageRepository"#;
let r2mm = game_def.r2modman.as_ref().expect(
"Expected a valid r2mm field in the ecosystem schema, got nothing. This is a bug."
).first().unwrap();

let uuid = reg::get_values_at(HKey::LocalMachine, &format!("{root}\\Package\\"))?

Check failure

Code scanning / clippy

? couldn't convert the error to error::Error Error

? couldn't convert the error to error::Error

Check failure

Code scanning / clippy

? couldn't convert the error to error::Error Error

? couldn't convert the error to error::Error
.into_iter()
.find(|x| x.key.starts_with(ident))
.ok_or_else(|| {
GameError::NotFound(ident.into(), "Gamepass".to_string())
})?
.val
.replace('\"', "");

let game_root = reg::get_keys_at(HKey::LocalMachine, &format!("Root\\{}\\", uuid))?

Check failure

Code scanning / clippy

? couldn't convert the error to error::Error Error

? couldn't convert the error to error::Error

Check failure

Code scanning / clippy

? couldn't convert the error to error::Error Error

? couldn't convert the error to error::Error
.into_iter()
.next()
.ok_or_else(|| {
GameError::NotFound(ident.into(), "Gamepass".to_string())
})?;
let game_dir = PathBuf::from(reg::get_value_at(HKey::LocalMachine, &game_root, "Root")?);

Check failure

Code scanning / clippy

? couldn't convert the error to error::Error Error

? couldn't convert the error to error::Error

Check failure

Code scanning / clippy

? couldn't convert the error to error::Error Error

? couldn't convert the error to error::Error

let exe_path = overrides
.custom_exe
.clone()
.or_else(|| super::find_game_exe(&r2mm.exe_names, &game_dir))
.ok_or_else(|| GameError::ExeNotFound {
possible_names: r2mm.exe_names.clone(),
base_path: game_dir.clone(),
})?;

Ok(Some(ActiveDistribution {
dist: GamePlatform::XboxGamePass {
identifier: ident.to_string(),
},
game_dir: game_dir.to_path_buf(),
data_dir: game_dir.join(&r2mm.data_folder_name),
exe_path,
}))
}
Loading
Loading