diff options
Diffstat (limited to 'src/config.rs')
-rw-r--r-- | src/config.rs | 51 |
1 files changed, 44 insertions, 7 deletions
diff --git a/src/config.rs b/src/config.rs index 61db1c7..e1049d1 100644 --- a/src/config.rs +++ b/src/config.rs | |||
@@ -6,12 +6,14 @@ use std::{ | |||
6 | 6 | ||
7 | use serde::{Deserialize, Serialize}; | 7 | use serde::{Deserialize, Serialize}; |
8 | 8 | ||
9 | use crate::{db::db_setup, error::MLE}; | 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, | ||
16 | pub defaults: Defaults, | ||
15 | pub apis: Apis, | 17 | pub apis: Apis, |
16 | } | 18 | } |
17 | 19 | ||
@@ -20,13 +22,20 @@ pub struct Apis { | |||
20 | pub modrinth: String, | 22 | pub modrinth: String, |
21 | } | 23 | } |
22 | 24 | ||
25 | #[derive(Debug, Clone, Serialize, Deserialize)] | ||
26 | pub struct Defaults { | ||
27 | pub modloader: Modloader, | ||
28 | pub version: VersionLevel, | ||
29 | } | ||
30 | |||
23 | impl Cfg { | 31 | impl Cfg { |
24 | pub fn init(path: Option<String>) -> MLE<Self> { | 32 | pub async fn init(path: Option<String>) -> MLE<Self> { |
25 | let configfile = match path.clone() { | 33 | let configfile = match path.clone() { |
26 | Some(p) => String::from(p), | 34 | Some(p) => String::from(p), |
27 | None => dirs::config_dir() | 35 | None => dirs::config_dir() |
28 | .unwrap() | 36 | .unwrap() |
29 | .join("modlist.toml") | 37 | .join("modlist") |
38 | .join("config.toml") | ||
30 | .to_string_lossy() | 39 | .to_string_lossy() |
31 | .to_string(), | 40 | .to_string(), |
32 | }; | 41 | }; |
@@ -50,12 +59,20 @@ impl Cfg { | |||
50 | create_cache(&config.cache)?; | 59 | create_cache(&config.cache)?; |
51 | }; | 60 | }; |
52 | //Check database | 61 | //Check database |
53 | //TODO check file | ||
54 | let datafile = format!("{}/data.db", config.data); | 62 | let datafile = format!("{}/data.db", config.data); |
55 | match File::open(&datafile) { | 63 | match File::open(&datafile) { |
56 | Ok(..) => (), | 64 | Ok(..) => (), |
57 | Err(..) => create_database(&datafile)?, | 65 | Err(..) => create_database(&datafile)?, |
58 | }; | 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 | } | ||
59 | Ok(config) | 76 | Ok(config) |
60 | } | 77 | } |
61 | } | 78 | } |
@@ -64,14 +81,24 @@ fn create_config(path: &str) -> MLE<()> { | |||
64 | print!("No config file found, create default"); | 81 | print!("No config file found, create default"); |
65 | //Force flush of stdout, else print! doesn't print instantly | 82 | //Force flush of stdout, else print! doesn't print instantly |
66 | std::io::stdout().flush()?; | 83 | std::io::stdout().flush()?; |
84 | let cache_dir = dirs::cache_dir() | ||
85 | .unwrap() | ||
86 | .join("modlist") | ||
87 | .to_string_lossy() | ||
88 | .to_string(); | ||
67 | let default_cfg = Cfg { | 89 | let default_cfg = Cfg { |
68 | //TODO get home dir | 90 | data: cache_dir.clone(), |
69 | data: String::from("$HOME/.cache/modlist/"), | 91 | cache: format!("{}/cache", cache_dir), |
70 | cache: String::from("$HOME/.cache/modlist/cache"), | 92 | versions: cache_dir.clone(), |
93 | defaults: Defaults { | ||
94 | modloader: Modloader::Fabric, | ||
95 | version: VersionLevel::Release | ||
96 | }, | ||
71 | apis: Apis { | 97 | apis: Apis { |
72 | modrinth: String::from("https://api.modrinth.com/v2/"), | 98 | modrinth: String::from("https://api.modrinth.com/v2/"), |
73 | }, | 99 | }, |
74 | }; | 100 | }; |
101 | create_dir_all(path.split("config.toml").collect::<Vec<&str>>()[0])?; | ||
75 | let mut file = File::create(path)?; | 102 | let mut file = File::create(path)?; |
76 | file.write_all(toml::to_string(&default_cfg)?.as_bytes())?; | 103 | file.write_all(toml::to_string(&default_cfg)?.as_bytes())?; |
77 | println!(" ✓"); | 104 | println!(" ✓"); |
@@ -98,3 +125,13 @@ fn create_cache(path: &str) -> MLE<()> { | |||
98 | println!(" ✓"); | 125 | println!(" ✓"); |
99 | Ok(()) | 126 | Ok(()) |
100 | } | 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 | } | ||