-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: convert everything to be async
- added support for gpu driver specific tweaks
- Loading branch information
1 parent
2603db9
commit 36efb19
Showing
15 changed files
with
1,061 additions
and
256 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,182 +1,35 @@ | ||
use std::collections::HashMap; | ||
|
||
use reqwest::{StatusCode, Url}; | ||
use serde::Deserialize; | ||
use protontweaks_api::app::App; | ||
|
||
use crate::utils::protontricks; | ||
|
||
const PROTONTWEAKS_DB: &str = env!("PROTONTWEAKS_DB"); | ||
|
||
#[derive(Debug, Deserialize)] | ||
pub struct App { | ||
pub id: String, | ||
pub name: String, | ||
pub tweaks: Tweaks, | ||
pub issues: Vec<Issue>, | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
pub struct Tweaks { | ||
pub tricks: Vec<String>, | ||
pub env: HashMap<String, String>, | ||
pub settings: TweakSettings, | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
pub struct TweakSettings { | ||
pub gamemode: Option<bool>, | ||
pub mangohud: Option<bool>, | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
pub struct Issue { | ||
pub description: String, | ||
pub solution: String, | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
pub struct AppsList { | ||
pub sha: String, | ||
pub short_sha: String, | ||
pub apps: Vec<MiniApp>, | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
pub struct MiniApp { | ||
pub id: String, | ||
pub name: String, | ||
} | ||
|
||
pub fn url(path: &str) -> Url { | ||
let url = Url::parse(PROTONTWEAKS_DB).unwrap(); | ||
|
||
return url.join(&format!("{path}.json")).unwrap(); | ||
} | ||
|
||
pub fn apps_list() -> AppsList { | ||
let url = url("apps"); | ||
|
||
debug!("Requesting apps from '{url}'..."); | ||
|
||
let response = reqwest::blocking::get(url).unwrap(); | ||
|
||
trace!("Apps received!"); | ||
|
||
response.json::<AppsList>().unwrap() | ||
} | ||
|
||
pub fn apps() -> Vec<MiniApp> { | ||
apps_list().apps | ||
} | ||
|
||
pub fn list_ids() -> Vec<String> { | ||
apps_list().apps.iter().map(|x| x.id.to_owned()).collect() | ||
} | ||
|
||
pub fn try_get(app_id: &str) -> Result<App, String> { | ||
let url = url(app_id); | ||
|
||
debug!("Requesting file from '{url}'..."); | ||
|
||
let response = reqwest::blocking::get(url).map_err(|e| { | ||
warn!("{}", e.to_string()); | ||
e.to_string() | ||
})?; | ||
|
||
if response.status() == StatusCode::NOT_FOUND { | ||
let message = format!("App {app_id} does not exist!"); | ||
info!("{message}"); | ||
return Err(message); | ||
} | ||
|
||
trace!("App received!"); | ||
|
||
let mut app = response | ||
.json::<App>() | ||
.map_err(|_| "Failed to parse app info".to_string())?; | ||
|
||
app.id = app_id.to_string(); | ||
|
||
Ok(app) | ||
} | ||
|
||
pub fn get(app_id: &str) -> App { | ||
try_get(app_id).unwrap() | ||
} | ||
|
||
/// Applies the app and returns a result of whether it was successful | ||
pub fn try_apply(app: &App) -> Result<(u32, u32), String> { | ||
pub async fn try_apply(app: &App) -> Result<(u32, u32), String> { | ||
let tweaks = app.flatten().await; | ||
trace!("App ID: {}; Name: {}", app.id, app.name); | ||
|
||
if app.tweaks.tricks.len() == 0 { | ||
if tweaks.tricks.len() == 0 { | ||
warn!("No tricks were found for {} -> {}", app.id, app.name); | ||
return Ok((0, 0)); | ||
} | ||
|
||
trace!("Installing tricks for {} -> {}", app.id, app.name); | ||
let tweaks_applied = protontricks::install::components(&app.id, &app.tweaks.tricks)?; | ||
let tweaks_applied = protontricks::install::components(&app.id, &tweaks.tricks).await?; | ||
|
||
return Ok((tweaks_applied, app.tweaks.tricks.len().try_into().unwrap())); | ||
return Ok((tweaks_applied, tweaks.tricks.len().try_into().unwrap())); | ||
} | ||
|
||
/// Applies the app and panics if a failure occurs. | ||
pub fn apply(app: &App) -> (u32, u32) { | ||
return try_apply(app).unwrap(); | ||
pub async fn apply(app: &App) -> (u32, u32) { | ||
return try_apply(app).await.unwrap(); | ||
} | ||
|
||
/// Applies the app, if an error occurs it simply logs it and returns that no tweaks were applied | ||
pub fn apply_safe(app: &App) -> (u32, u32) { | ||
match try_apply(app) { | ||
pub async fn apply_safe(app: &App) -> (u32, u32) { | ||
match try_apply(app).await { | ||
Ok(result) => result, | ||
Err(e) => { | ||
error!("{e}"); | ||
(0, app.tweaks.tricks.len().try_into().unwrap()) | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::apps::{apps_list, get, list_ids, url}; | ||
|
||
#[test] | ||
fn url_should_create_a_apps_url() { | ||
assert_eq!( | ||
url("apps").to_string(), | ||
"https://api.protontweaks.com/apps.json" | ||
); | ||
} | ||
|
||
#[test] | ||
fn apps_list_should_return_the_tweaks_list() { | ||
let apps_list = apps_list(); | ||
|
||
assert!( | ||
apps_list.apps.len() > 0, | ||
"Expected to receive a list of valid tweaked apps!" | ||
); | ||
} | ||
|
||
#[test] | ||
fn list_ids_should_return_the_tweaks_list() { | ||
let ids = list_ids(); | ||
|
||
assert!( | ||
ids.len() > 0, | ||
"Expected to receive a list of valid tweaked apps!" | ||
); | ||
} | ||
|
||
#[test] | ||
fn get_should_return_the_app_info() { | ||
let expected_id = "644930"; | ||
let app = get(expected_id); | ||
|
||
assert_eq!(app.id, expected_id); | ||
assert_eq!(app.issues.len(), 1); | ||
assert_eq!(app.tweaks.tricks.len(), 1); | ||
assert_eq!(app.tweaks.env.len(), 0); | ||
assert_eq!(app.tweaks.settings.gamemode, None); | ||
assert_eq!(app.tweaks.settings.mangohud, None); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.