1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use ron::de::from_reader;
use ron::ser::{to_string_pretty, PrettyConfig};
use serde::{Deserialize, Serialize};
use std::fs::OpenOptions;
use std::io::Write;
use crate::KeybindsRaw;
#[derive(Serialize, Deserialize)]
#[cfg(feature = "bg")]
pub struct Config {
def_file: Option<String>,
img_file: Option<String>,
img_scaled: bool,
colors: [(u8, u8, u8); 6],
t_font: String,
s_font: String,
font_size: (u16, u16),
binds: KeybindsRaw,
}
#[derive(Serialize, Deserialize)]
#[cfg(not(feature = "bg"))]
pub struct Config {
def_file: Option<String>,
colors: [(u8, u8, u8); 6],
t_font: String,
s_font: String,
font_size: (u16, u16),
binds: KeybindsRaw,
}
impl Config {
pub fn open() -> Result<Self, String> {
let file = OpenOptions::new()
.read(true)
.write(true)
.create(true)
.open("assets/mist.cfg")
.map_err(|e| e.to_string())?;
let cfg: Self = from_reader(&file).unwrap_or(Config::default());
return Ok(cfg);
}
pub fn file(&self) -> Option<&String> {
self.def_file.as_ref()
}
#[cfg(feature = "bg")]
pub fn img(&self) -> Option<&String> {
self.img_file.as_ref()
}
#[cfg(feature = "bg")]
pub fn img_scaled(&self) -> bool {
self.img_scaled
}
pub fn set_file(&mut self, file: &String) {
self.def_file = Some(file.to_owned());
}
pub fn tfont(&self) -> &str {
&self.t_font
}
pub fn sfont(&self) -> &str {
&self.s_font
}
pub fn fsize(&self) -> (u16, u16) {
self.font_size
}
pub fn color_list(&self) -> [(u8, u8, u8); 6] {
self.colors
}
pub fn save(&self) -> Result<(), String> {
let mut file = OpenOptions::new()
.write(true)
.open("assets/mist.cfg")
.map_err(|e| e.to_string())?;
let string = to_string_pretty(self, PrettyConfig::new()).map_err(|e| e.to_string())?;
file.write(&string.as_bytes()).map_err(|e| e.to_string())?;
Ok(())
}
pub fn binds(&self) -> &KeybindsRaw {
&self.binds
}
}
#[cfg(feature = "bg")]
impl Default for Config {
fn default() -> Config {
Config {
def_file: None,
img_file: None,
img_scaled: false,
colors: [
(0, 255, 0),
(255, 0, 0),
(255, 90, 90),
(135, 255, 125),
(255, 255, 0),
(0, 0, 0),
],
t_font: "assets/segoe-ui-bold.ttf".to_owned(),
s_font: "assets/segoe-ui-bold.ttf".to_owned(),
font_size: (60, 25),
binds: KeybindsRaw::default(),
}
}
}
#[cfg(not(feature = "bg"))]
impl Default for Config {
fn default() -> Config {
Config {
def_file: None,
colors: [
(0, 255, 0),
(255, 0, 0),
(255, 90, 90),
(135, 255, 125),
(255, 255, 0),
(0, 0, 0),
],
t_font: "assets/segoe-ui-bold.ttf".to_owned(),
s_font: "assets/segoe-ui-bold.ttf".to_owned(),
font_size: (60, 25),
binds: KeybindsRaw::default(),
}
}
}