1
use std::collections::HashMap;
2

            
3
use serde::{Deserialize, Serialize};
4

            
5
use crate::system::{System, SystemTweaks};
6

            
7
#[derive(PartialEq, Debug, Deserialize, Serialize)]
8
pub struct TweakSettings {
9
    pub gamemode: Option<bool>,
10
    pub mangohud: Option<bool>,
11
}
12

            
13
#[derive(Debug, Deserialize, Serialize)]
14
pub struct Issue {
15
    pub description: String,
16
    pub solution: Option<String>,
17
}
18

            
19
#[derive(Debug, Deserialize, Serialize)]
20
pub struct Tweaks {
21
    pub tricks: Vec<String>,
22
    pub env: HashMap<String, String>,
23
    pub args: Vec<String>,
24
    pub settings: TweakSettings,
25
    pub system: System,
26
}
27

            
28
#[derive(Debug, Deserialize, Serialize)]
29
pub struct App {
30
    pub id: String,
31
    pub name: String,
32
    pub tweaks: Tweaks,
33
    pub issues: Vec<Issue>,
34
}
35

            
36
impl Clone for TweakSettings {
37
3
    fn clone(&self) -> Self {
38
3
        Self {
39
3
            gamemode: self.gamemode.clone(),
40
3
            mangohud: self.mangohud.clone(),
41
3
        }
42
3
    }
43
}
44

            
45
impl App {
46
    /// Utilizes system info to flatten out the App into a collection of applicable tweaks
47
3
    pub async fn flatten(&self) -> SystemTweaks {
48
3
        let mut env = self.tweaks.env.clone();
49
3
        let mut tricks = self.tweaks.tricks.clone();
50
3
        let mut args = self.tweaks.args.clone();
51
3
        let mut settings = self.tweaks.settings.clone();
52

            
53
3
        if let Some(gpu_tweaks) = self.tweaks.system.gpu_driver.get_tweaks().await {
54
            // gpu-level settings overwrite global settings
55
            env.extend(gpu_tweaks.env.clone());
56
            tricks.extend(gpu_tweaks.tricks.clone());
57
            args.extend(gpu_tweaks.args.clone());
58

            
59
            if let Some(gamemode) = gpu_tweaks.settings.gamemode {
60
                settings.gamemode = Some(gamemode);
61
            }
62

            
63
            if let Some(mangohud) = gpu_tweaks.settings.mangohud {
64
                settings.mangohud = Some(mangohud);
65
            }
66
3
        }
67

            
68
3
        SystemTweaks {
69
3
            env,
70
3
            args,
71
3
            tricks,
72
3
            settings,
73
3
        }
74
3
    }
75
}
76

            
77
#[cfg(test)]
78
mod tests {
79
    use crate::Protontweaks;
80

            
81
    #[tokio::test]
82
3
    async fn flatten() {
83
3
        let api = Protontweaks::new();
84

            
85
3
        let app = api.app("644930").await;
86

            
87
3
        assert_eq!(app.tweaks.tricks.len(), app.flatten().await.tricks.len());
88
3
    }
89
}