Add settings for EGS and SteamgridDB

This commit is contained in:
Philip Kristoffersen
2021-09-25 00:36:09 +02:00
parent 60dd680c33
commit 99079dcaff
13 changed files with 307 additions and 66 deletions
+85 -23
View File
@@ -1,39 +1,101 @@
use super::{EpicGamesLauncherSettings, ManifestItem};
use std::env::{self, VarError};
use std::error::Error;
use std::fs::{DirEntry, File};
use std::io::BufReader;
use std::path::Path;
use std::error::Error;
use super::ManifestItem;
pub fn get_egs_manifests() -> Result<Vec<ManifestItem>, Box<dyn Error>> {
let manifest_dir_path = get_manifest_dir_path()?;
use failure::*;
#[derive(Debug, Fail)]
pub enum EpicGamesManifestsError {
#[fail(display = "Path to EpicGamesLauncher not defined, it must be defined on linux")]
PathNotDefined,
#[fail(
display = "EpicGamesLauncher path: {} could not be found. Try to specify a different path for the EpicGamesLauncher.",
path
)]
PathNotFound { path: String },
#[fail(
display = "Could not read EpicGamesLauncher manifest directory at {} error: {}",
path, error
)]
ReadDirError { path: String, error: std::io::Error },
}
pub fn get_egs_manifests(
settings: &EpicGamesLauncherSettings,
) -> Result<Vec<ManifestItem>, EpicGamesManifestsError> {
use EpicGamesManifestsError::*;
let manifest_dir_path = get_manifest_dir_path(settings)?;
let manifest_dir_result = std::fs::read_dir(&manifest_dir_path);
if let Err(err) = manifest_dir_result {
println!("Could not find manifest directory: {}", manifest_dir_path);
return Result::Err(Box::new(err));
match manifest_dir_result {
Ok(manifest_dir) => {
let manifests = manifest_dir
.filter_map(|dir| dir.ok())
.filter_map(get_manifest_item)
.filter(is_game_installed);
Ok(manifests.collect())
}
Err(err) => Err(ReadDirError {
error: err,
path: manifest_dir_path,
}),
}
let manifest_dir = manifest_dir_result?;
let manifests = manifest_dir
.filter_map(|dir| dir.ok())
.filter_map(get_manifest_item)
.filter(is_game_installed);
Ok(manifests.collect())
}
fn get_manifest_dir_path() -> Result<String, VarError> {
let key = "SYSTEMDRIVE";
let system_drive = env::var(key)?;
Ok(format!(
"{system_drive}//ProgramData//Epic//EpicGamesLauncher//Data//Manifests",
system_drive = system_drive
))
fn get_manifest_dir_path(
settings: &EpicGamesLauncherSettings,
) -> Result<String, EpicGamesManifestsError> {
use EpicGamesManifestsError::*;
if let Some(location) = &settings.location {
let path = Path::new(location).join("Data").join("Manifests");
if path.exists() {
return Ok(path.to_str().unwrap().to_string());
} else {
return Err(PathNotFound {
path: path.to_str().unwrap().to_string(),
});
}
} else {
#[cfg(target_os = "linux")]
{
//No path defined for epic gamestore, and we cannot guess on linux
return Err(PathNotDefined);
}
#[cfg(target_os = "windows")]
{
let key = "SYSTEMDRIVE";
let system_drive =
env::var(key).expect("We are on windows, we must know what the SYSTEMDRIVE is");
let path = Path::new(system_drive.as_str())
.join("ProgramData")
.join("Epic")
.join("EpicGamesLauncher")
.join("Data")
.join("Manifests");
if path.exists() {
return Ok(path.to_str().unwrap().to_string());
} else {
return Err(PathNotFound {
path: path.to_str().unwrap().to_string(),
});
}
}
}
}
fn is_game_installed(manifest:&ManifestItem) -> bool{
Path::new(manifest.manifest_location.as_str()).exists()
fn is_game_installed(manifest: &ManifestItem) -> bool {
Path::new(manifest.manifest_location.as_str()).exists()
}
fn get_manifest_item(dir_entry: DirEntry) -> Option<ManifestItem> {
fn get_manifest_item(dir_entry: DirEntry) -> Option<ManifestItem> {
if let Some(extension) = dir_entry.path().extension() {
if extension.eq("item") {
if let Ok(file) = File::open(dir_entry.path()) {