Skip to content

Commit 2b7552e

Browse files
committed
feat(backend): add smart-default for default values in configuration structs
1 parent 6a97299 commit 2b7552e

File tree

3 files changed

+46
-138
lines changed

3 files changed

+46
-138
lines changed

src-tauri/Cargo.lock

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src-tauri/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ futures = "0.3.31"
6464
governor = { version = "0.8.1", features = ["std"] }
6565
strum = "0.27.1"
6666
strum_macros = "0.27.1"
67+
smart-default = "0.7.1"
6768

6869
[target."cfg(windows)".dependencies]
6970
winreg = "0.55.0"

src-tauri/src/launcher_config/models.rs

Lines changed: 33 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::{storage::Storage, utils::sys_info, EXE_DIR};
22
use partial_derive::Partial;
33
use serde::{Deserialize, Serialize};
4+
use smart_default::SmartDefault;
45
use std::path::PathBuf;
56
use strum_macros::Display;
67

@@ -56,30 +57,40 @@ pub enum ProxyType {
5657
structstruck::strike! {
5758
#[strikethrough[derive(Partial, Debug, PartialEq, Eq, Clone, Deserialize, Serialize)]]
5859
#[strikethrough[serde(rename_all = "camelCase", deny_unknown_fields)]]
60+
#[strikethrough[derive(SmartDefault)]]
61+
#[strikethrough[serde(default)]]
5962
pub struct GameConfig {
6063
pub game_java: struct GameJava {
64+
#[default = true]
6165
pub auto: bool,
6266
pub exec_path: String,
6367
},
6468
pub game_window: struct {
6569
pub resolution: struct {
70+
#[default = 1280]
6671
pub width: u32,
72+
#[default = 720]
6773
pub height: u32,
6874
pub fullscreen: bool,
6975
},
7076
pub custom_title: String,
7177
pub custom_info: String,
7278
},
7379
pub performance: struct {
80+
#[default = true]
7481
pub auto_mem_allocation: bool,
82+
#[default = 1024]
7583
pub min_mem_allocation: u32,
84+
#[default(ProcessPriority::Normal)]
7685
pub process_priority: ProcessPriority,
7786
},
7887
pub game_server: struct {
7988
pub auto_join: bool,
8089
pub server_url: String,
8190
},
91+
#[default = true]
8292
pub version_isolation: bool,
93+
#[default = "start-close"]
8394
pub launcher_visibility: String,
8495
pub display_game_log: bool,
8596
pub advanced_options: struct {
@@ -99,6 +110,7 @@ structstruck::strike! {
99110
},
100111
pub workaround: struct {
101112
pub no_jvm_args: bool,
113+
#[default = "full"]
102114
pub game_completness_check_policy: String,
103115
pub dont_check_jvm_validity: bool,
104116
pub dont_patch_natives: bool,
@@ -119,8 +131,11 @@ pub struct GameDirectory {
119131
structstruck::strike! {
120132
#[strikethrough[derive(Partial, Debug, PartialEq, Eq, Clone, Deserialize, Serialize)]]
121133
#[strikethrough[serde(rename_all = "camelCase", deny_unknown_fields)]]
134+
#[strikethrough[derive(SmartDefault)]]
135+
#[strikethrough[serde(default)]]
122136
pub struct LauncherConfig {
123137
pub basic_info: struct {
138+
#[default = "dev"]
124139
pub launcher_version: String,
125140
pub platform: String,
126141
pub arch: String,
@@ -132,11 +147,15 @@ structstruck::strike! {
132147
pub run_count: usize,
133148
pub appearance: struct AppearanceConfig {
134149
pub theme: struct {
150+
#[default = "blue"]
135151
pub primary_color: String,
152+
#[default = "light"]
136153
pub color_mode: String,
154+
#[default = "standard"]
137155
pub head_nav_style: String,
138156
},
139157
pub background: struct {
158+
#[default = "%built-in:Jokull"]
140159
pub choice: String,
141160
},
142161
pub accessibility: struct {
@@ -146,26 +165,33 @@ structstruck::strike! {
146165
},
147166
pub download: struct DownloadConfig {
148167
pub source: struct {
168+
#[default = "auto"]
149169
pub strategy: String,
150170
},
151171
pub transmission: struct {
172+
#[default = true]
152173
pub auto_concurrent: bool,
174+
#[default = 64]
153175
pub concurrent_count: usize,
176+
#[default = false]
154177
pub enable_speed_limit: bool,
178+
#[default = 1024]
155179
pub speed_limit_value: usize,
156180
},
157181
pub cache: struct {
158182
pub directory: PathBuf,
159183
},
160184
pub proxy: struct ProxyConfig {
161185
pub enabled: bool,
186+
#[default(ProxyType::Http)]
162187
pub selected_type: ProxyType,
163188
pub host: String,
164189
pub port: usize,
165190
}
166191
},
167192
pub general: struct GeneralConfig {
168193
pub general: struct {
194+
#[default(sys_info::get_mapped_locale())]
169195
pub language: String,
170196
},
171197
pub optional_functions: struct {
@@ -174,6 +200,7 @@ structstruck::strike! {
174200
},
175201
pub global_game_config: GameConfig,
176202
pub local_game_directories: Vec<GameDirectory>,
203+
#[default(_code="vec![\"https://mc.sjtu.cn/api-sjmcl/article\".to_string()]")]
177204
pub discover_source_endpoints: Vec<String>,
178205
pub extra_java_paths: Vec<String>,
179206
pub states: struct States {
@@ -182,171 +209,39 @@ structstruck::strike! {
182209
pub selected_instance_id: String,
183210
},
184211
pub accounts_page: struct {
212+
#[default = "grid"]
185213
pub view_type: String
186214
},
187215
pub all_games_page: struct {
216+
#[default = "list"]
188217
pub view_type: String
189218
},
190219
pub game_version_selector: struct {
220+
#[default(_code="vec![\"release\".to_string()]")]
191221
pub game_types: Vec<String>
192222
},
193223
pub instance_mods_page: struct {
224+
#[default([true, true])]
194225
pub accordion_states: [bool; 2],
195226
},
196227
pub instance_resourcepack_page: struct {
228+
#[default([true, true])]
197229
pub accordion_states: [bool; 2],
198230
},
199231
pub instance_worlds_page: struct {
232+
#[default([true, true])]
200233
pub accordion_states: [bool; 2],
201234
},
202235
}
203236
}
204237
}
205238

206-
impl Default for GameConfig {
207-
fn default() -> Self {
208-
Self {
209-
game_java: GameJava {
210-
auto: true,
211-
exec_path: "".to_string(),
212-
},
213-
game_window: GameWindow {
214-
resolution: Resolution {
215-
width: 1280,
216-
height: 720,
217-
fullscreen: false,
218-
},
219-
custom_title: "".to_string(),
220-
custom_info: "".to_string(),
221-
},
222-
performance: Performance {
223-
auto_mem_allocation: true,
224-
min_mem_allocation: 1024,
225-
process_priority: ProcessPriority::Normal,
226-
},
227-
game_server: GameServer {
228-
auto_join: false,
229-
server_url: "".to_string(),
230-
},
231-
version_isolation: true,
232-
launcher_visibility: "start-close".to_string(),
233-
display_game_log: false,
234-
advanced_options: AdvancedOptions { enabled: false },
235-
advanced: Advanced {
236-
custom_commands: CustomCommands {
237-
minecraft_argument: "".to_string(),
238-
precall_command: "".to_string(),
239-
wrapper_launcher: "".to_string(),
240-
post_exit_command: "".to_string(),
241-
},
242-
jvm: Jvm {
243-
args: "".to_string(),
244-
java_permanent_generation_space: 0,
245-
environment_variable: "".to_string(),
246-
},
247-
workaround: Workaround {
248-
no_jvm_args: false,
249-
game_completness_check_policy: "full".to_string(),
250-
dont_check_jvm_validity: false,
251-
dont_patch_natives: false,
252-
use_native_glfw: false,
253-
use_native_openal: false,
254-
},
255-
},
256-
}
257-
}
258-
}
259-
260239
impl Storage for LauncherConfig {
261240
fn file_path() -> PathBuf {
262241
EXE_DIR.join("sjmcl.conf.json")
263242
}
264243
}
265244

266-
impl Default for LauncherConfig {
267-
fn default() -> Self {
268-
Self {
269-
basic_info: BasicInfo {
270-
launcher_version: "dev".to_string(),
271-
platform: "".to_string(),
272-
arch: "".to_string(),
273-
os_type: "".to_string(),
274-
platform_version: "".to_string(),
275-
},
276-
mocked: false,
277-
run_count: 0,
278-
appearance: AppearanceConfig {
279-
theme: Theme {
280-
primary_color: "blue".to_string(),
281-
color_mode: "light".to_string(),
282-
head_nav_style: "standard".to_string(),
283-
},
284-
background: Background {
285-
choice: "%built-in:Jokull".to_string(),
286-
},
287-
accessibility: Accessibility {
288-
invert_colors: false,
289-
enhance_contrast: false,
290-
},
291-
},
292-
download: DownloadConfig {
293-
source: Source {
294-
strategy: "auto".to_string(),
295-
},
296-
transmission: Transmission {
297-
auto_concurrent: true,
298-
concurrent_count: 64,
299-
enable_speed_limit: false,
300-
speed_limit_value: 1024,
301-
},
302-
cache: Cache {
303-
directory: PathBuf::default(),
304-
},
305-
proxy: ProxyConfig {
306-
enabled: false,
307-
selected_type: ProxyType::Http,
308-
host: String::new(),
309-
port: 0,
310-
},
311-
},
312-
general: GeneralConfig {
313-
general: General {
314-
language: sys_info::get_mapped_locale(),
315-
},
316-
optional_functions: OptionalFunctions { discover: false },
317-
},
318-
global_game_config: GameConfig::default(),
319-
local_game_directories: vec![],
320-
discover_source_endpoints: vec!["https://mc.sjtu.cn/api-sjmcl/article".to_string()],
321-
extra_java_paths: vec![],
322-
states: States {
323-
shared: Shared {
324-
selected_player_id: "".to_string(),
325-
selected_instance_id: "".to_string(),
326-
},
327-
accounts_page: AccountsPage {
328-
view_type: "grid".to_string(),
329-
},
330-
all_games_page: AllGamesPage {
331-
view_type: "list".to_string(),
332-
},
333-
game_version_selector: GameVersionSelector {
334-
game_types: vec!["release".to_string()],
335-
},
336-
instance_mods_page: InstanceModsPage {
337-
accordion_states: [true, true],
338-
},
339-
instance_resourcepack_page: InstanceResourcepackPage {
340-
accordion_states: [true, true],
341-
},
342-
instance_worlds_page: InstanceWorldsPage {
343-
accordion_states: [true, true],
344-
},
345-
},
346-
}
347-
}
348-
}
349-
350245
#[derive(Debug, Display)]
351246
#[strum(serialize_all = "SCREAMING_SNAKE_CASE")]
352247
pub enum LauncherConfigError {

0 commit comments

Comments
 (0)