rpfm_extensions/diagnostics/
config.rs1use getset::{Getters, MutGetters};
14use serde_derive::{Serialize, Deserialize};
15
16use std::{fmt, fmt::Display};
17use std::path::Path;
18
19use rpfm_lib::games::GameInfo;
20
21use crate::diagnostics::*;
22
23#[derive(Debug, Clone, Default, Getters, MutGetters, Serialize, Deserialize)]
29#[getset(get = "pub", get_mut = "pub")]
30pub struct ConfigDiagnostic {
31 results: Vec<ConfigDiagnosticReport>
32}
33
34#[derive(Debug, Clone, Getters, MutGetters, Serialize, Deserialize)]
36#[getset(get = "pub", get_mut = "pub")]
37pub struct ConfigDiagnosticReport {
38 report_type: ConfigDiagnosticReportType,
39}
40
41#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
42pub enum ConfigDiagnosticReportType {
43 DependenciesCacheNotGenerated,
44 DependenciesCacheOutdated,
45 DependenciesCacheCouldNotBeLoaded(String),
46 IncorrectGamePath,
47}
48
49impl ConfigDiagnosticReport {
54 pub fn new(report_type: ConfigDiagnosticReportType) -> Self {
55 Self {
56 report_type
57 }
58 }
59}
60
61impl DiagnosticReport for ConfigDiagnosticReport {
62 fn message(&self) -> String {
63 match self.report_type {
64 ConfigDiagnosticReportType::DependenciesCacheNotGenerated => "Dependency Cache not generated for the currently selected game.".to_owned(),
65 ConfigDiagnosticReportType::DependenciesCacheOutdated => "Dependency Cache for the selected game is outdated and could not be loaded.".to_owned(),
66 ConfigDiagnosticReportType::DependenciesCacheCouldNotBeLoaded(_) => "Dependency Cache couldn't be loaded for the game selected, due to errors reading the game's folder.".to_owned(),
67 ConfigDiagnosticReportType::IncorrectGamePath => "Game Path for the current Game Selected is incorrect.".to_owned(),
68 }
69 }
70
71 fn level(&self) -> DiagnosticLevel {
72 match self.report_type {
73 ConfigDiagnosticReportType::DependenciesCacheNotGenerated => DiagnosticLevel::Error,
74 ConfigDiagnosticReportType::DependenciesCacheOutdated => DiagnosticLevel::Error,
75 ConfigDiagnosticReportType::DependenciesCacheCouldNotBeLoaded(_) => DiagnosticLevel::Error,
76 ConfigDiagnosticReportType::IncorrectGamePath => DiagnosticLevel::Error,
77 }
78 }
79}
80
81impl Display for ConfigDiagnosticReportType {
82 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
83 Display::fmt(match self {
84 Self::DependenciesCacheNotGenerated => "DependenciesCacheNotGenerated",
85 Self::DependenciesCacheOutdated => "DependenciesCacheOutdated",
86 Self::DependenciesCacheCouldNotBeLoaded(_) => "DependenciesCacheCouldNotBeLoaded",
87 Self::IncorrectGamePath => "IncorrectGamePath",
88 }, f)
89 }
90}
91
92impl ConfigDiagnostic {
93
94 pub fn check(dependencies: &Dependencies, game_info: &GameInfo, game_path: &Path) -> Option<DiagnosticType> {
96 let mut diagnostic = ConfigDiagnostic::default();
97
98 let exe_path = game_info.executable_path(game_path).filter(|path| path.is_file());
100 if exe_path.is_none() {
101 diagnostic.results_mut().push(ConfigDiagnosticReport::new(ConfigDiagnosticReportType::IncorrectGamePath));
102 }
103
104 else if !dependencies.is_vanilla_data_loaded(false) {
106 diagnostic.results_mut().push(ConfigDiagnosticReport::new(ConfigDiagnosticReportType::DependenciesCacheNotGenerated));
107 }
108
109 else {
111 match dependencies.needs_updating(game_info, game_path) {
112 Ok(needs_updating) => {
113 if needs_updating {
114 diagnostic.results_mut().push(ConfigDiagnosticReport::new(ConfigDiagnosticReportType::DependenciesCacheOutdated));
115 }
116 }
117
118 Err(error) => {
119 diagnostic.results_mut().push(ConfigDiagnosticReport::new(ConfigDiagnosticReportType::DependenciesCacheCouldNotBeLoaded(error.to_string())));
120 }
121 }
122 }
123
124 if !diagnostic.results().is_empty() {
125 Some(DiagnosticType::Config(diagnostic))
126 } else { None }
127 }
128}