diff options
Diffstat (limited to 'src/config.rs')
-rw-r--r-- | src/config.rs | 29 |
1 files changed, 26 insertions, 3 deletions
diff --git a/src/config.rs b/src/config.rs index cf27257..e1049d1 100644 --- a/src/config.rs +++ b/src/config.rs | |||
@@ -6,12 +6,13 @@ use std::{ | |||
6 | 6 | ||
7 | use serde::{Deserialize, Serialize}; | 7 | use serde::{Deserialize, Serialize}; |
8 | 8 | ||
9 | use crate::{db::db_setup, error::MLE, Modloader}; | 9 | use crate::{db::db_setup, error::MLE, Modloader, VersionLevel, check_game_versions}; |
10 | 10 | ||
11 | #[derive(Debug, Clone, Serialize, Deserialize)] | 11 | #[derive(Debug, Clone, Serialize, Deserialize)] |
12 | pub struct Cfg { | 12 | pub struct Cfg { |
13 | pub data: String, | 13 | pub data: String, |
14 | pub cache: String, | 14 | pub cache: String, |
15 | pub versions: String, | ||
15 | pub defaults: Defaults, | 16 | pub defaults: Defaults, |
16 | pub apis: Apis, | 17 | pub apis: Apis, |
17 | } | 18 | } |
@@ -24,10 +25,11 @@ pub struct Apis { | |||
24 | #[derive(Debug, Clone, Serialize, Deserialize)] | 25 | #[derive(Debug, Clone, Serialize, Deserialize)] |
25 | pub struct Defaults { | 26 | pub struct Defaults { |
26 | pub modloader: Modloader, | 27 | pub modloader: Modloader, |
28 | pub version: VersionLevel, | ||
27 | } | 29 | } |
28 | 30 | ||
29 | impl Cfg { | 31 | impl Cfg { |
30 | pub fn init(path: Option<String>) -> MLE<Self> { | 32 | pub async fn init(path: Option<String>) -> MLE<Self> { |
31 | let configfile = match path.clone() { | 33 | let configfile = match path.clone() { |
32 | Some(p) => String::from(p), | 34 | Some(p) => String::from(p), |
33 | None => dirs::config_dir() | 35 | None => dirs::config_dir() |
@@ -62,6 +64,15 @@ impl Cfg { | |||
62 | Ok(..) => (), | 64 | Ok(..) => (), |
63 | Err(..) => create_database(&datafile)?, | 65 | Err(..) => create_database(&datafile)?, |
64 | }; | 66 | }; |
67 | //Check versions | ||
68 | let versionfile = format!("{}/versions.json", config.versions); | ||
69 | match File::open(&versionfile) { | ||
70 | Ok(..) => (), | ||
71 | Err(..) => { | ||
72 | create_versions_dummy(&versionfile).await?; | ||
73 | check_game_versions(&versionfile, true).await?; | ||
74 | }, | ||
75 | } | ||
65 | Ok(config) | 76 | Ok(config) |
66 | } | 77 | } |
67 | } | 78 | } |
@@ -78,8 +89,10 @@ fn create_config(path: &str) -> MLE<()> { | |||
78 | let default_cfg = Cfg { | 89 | let default_cfg = Cfg { |
79 | data: cache_dir.clone(), | 90 | data: cache_dir.clone(), |
80 | cache: format!("{}/cache", cache_dir), | 91 | cache: format!("{}/cache", cache_dir), |
92 | versions: cache_dir.clone(), | ||
81 | defaults: Defaults { | 93 | defaults: Defaults { |
82 | modloader: Modloader::Fabric | 94 | modloader: Modloader::Fabric, |
95 | version: VersionLevel::Release | ||
83 | }, | 96 | }, |
84 | apis: Apis { | 97 | apis: Apis { |
85 | modrinth: String::from("https://api.modrinth.com/v2/"), | 98 | modrinth: String::from("https://api.modrinth.com/v2/"), |
@@ -112,3 +125,13 @@ fn create_cache(path: &str) -> MLE<()> { | |||
112 | println!(" ✓"); | 125 | println!(" ✓"); |
113 | Ok(()) | 126 | Ok(()) |
114 | } | 127 | } |
128 | |||
129 | async fn create_versions_dummy(path: &str) -> MLE<()> { | ||
130 | print!("No version file found, create dummy"); | ||
131 | //Force flush of stdout, else print! doesn't print instantly | ||
132 | std::io::stdout().flush()?; | ||
133 | |||
134 | File::create(path)?; | ||
135 | println!(" ✓"); | ||
136 | Ok(()) | ||
137 | } | ||