From 8ff7f285b0313540ab360480427dd2190afe87ca Mon Sep 17 00:00:00 2001 From: Rh4096 Date: Sun, 7 Jul 2024 18:01:23 +0700 Subject: [PATCH] moving to strongly typed configs --- common/src/cfg.rs | 69 ++++++++++++++++++++++++++++++++++ common/src/lib.rs | 2 + launcher/src/config.rs | 68 --------------------------------- launcher/src/main.rs | 2 +- launcher/src/scripts.rs | 2 +- terraps/src/game/background.rs | 5 +-- 6 files changed, 75 insertions(+), 73 deletions(-) create mode 100644 common/src/cfg.rs diff --git a/common/src/cfg.rs b/common/src/cfg.rs new file mode 100644 index 0000000..4359c7b --- /dev/null +++ b/common/src/cfg.rs @@ -0,0 +1,69 @@ +use crate::read_json; +use anyhow::Result; +use serde::{Deserialize, Serialize}; +use serde_json::from_value; + +#[derive(Serialize, Deserialize)] +pub struct ServerConfig { + pub adaptive: bool, + #[serde(rename = "enableServer")] + pub enable_server: bool, + #[serde(rename = "forceUpdateExcel")] + pub force_update_excel: bool, + pub gadget: bool, + pub host: String, + #[serde(rename = "maintenanceMsg")] + pub maintenance_msg: String, + pub mode: String, + #[serde(rename = "noProxy")] + pub no_proxy: bool, + pub port: i64, + #[serde(rename = "useSu")] + pub use_su: bool, +} + +impl ServerConfig { + pub fn load() -> Result { + let value = read_json("./config/server.json")["server"].clone(); + Ok(from_value(value)?) + } +} + +#[derive(Serialize, Deserialize)] +pub struct RestorePrevState { + is2: bool, + #[serde(rename = "squadsAndFavs")] + sq_n_fav: bool, + ui: bool, +} + +#[derive(Serialize, Deserialize)] +pub struct UserConfig { + #[serde(rename = "activityMaxStartTs")] + pub act_max_start_ts: i64, + #[serde(rename = "activityMinStartTs")] + pub act_min_start_ts: i64, + pub background: String, + #[serde(rename = "fakeTime")] + pub fake_time: i64, + #[serde(rename = "forceEnableBattleReplay")] + pub force_battle_replay: bool, + #[serde(rename = "nickName")] + pub name: String, + #[serde(rename = "nickNumber")] + pub number: String, + #[serde(rename = "restorePreviousStates")] + pub restore_prev_state: RestorePrevState, + pub secretary: String, + #[serde(rename = "secretarySkinId")] + pub secretary_skin: String, + pub theme: String, + pub vision: bool, +} + +impl UserConfig { + pub fn load() -> Result { + let value = read_json("./config/server.json")["userConfig"].clone(); + Ok(from_value(value)?) + } +} diff --git a/common/src/lib.rs b/common/src/lib.rs index d0aa046..a152220 100644 --- a/common/src/lib.rs +++ b/common/src/lib.rs @@ -1,7 +1,9 @@ +mod cfg; mod fs; mod json; pub use self::{ + cfg::{RestorePrevState, ServerConfig, UserConfig}, fs::mkfile, json::{read_json, write_json}, }; diff --git a/launcher/src/config.rs b/launcher/src/config.rs index 8458248..8b13789 100644 --- a/launcher/src/config.rs +++ b/launcher/src/config.rs @@ -1,69 +1 @@ -use anyhow::Result; -use common_utils::read_json; -use serde::{Deserialize, Serialize}; -use serde_json::from_value; -#[derive(Serialize, Deserialize)] -pub struct ServerConfig { - pub adaptive: bool, - #[serde(rename = "enableServer")] - pub enable_server: bool, - #[serde(rename = "forceUpdateExcel")] - pub force_update_excel: bool, - pub gadget: bool, - pub host: String, - #[serde(rename = "maintenanceMsg")] - pub maintenance_msg: String, - pub mode: String, - #[serde(rename = "noProxy")] - pub no_proxy: bool, - pub port: i64, - #[serde(rename = "useSu")] - pub use_su: bool, -} - -impl ServerConfig { - pub fn load() -> Result { - let value = read_json("./config/server.json")["server"].clone(); - Ok(from_value(value)?) - } -} - -#[derive(Serialize, Deserialize)] -pub struct RestorePrevState { - is2: bool, - #[serde(rename = "squadsAndFavs")] - sq_n_fav: bool, - ui: bool, -} - -#[derive(Serialize, Deserialize)] -pub struct UserConfig { - #[serde(rename = "activityMaxStartTs")] - pub act_max_start_ts: i64, - #[serde(rename = "activityMinStartTs")] - pub act_min_start_ts: i64, - pub background: String, - #[serde(rename = "fakeTime")] - pub fake_time: i64, - #[serde(rename = "forceEnableBattleReplay")] - pub force_battle_replay: bool, - #[serde(rename = "nickName")] - pub name: String, - #[serde(rename = "nickNumber")] - pub number: String, - #[serde(rename = "restorePreviousStates")] - pub restore_prev_state: RestorePrevState, - pub secretary: String, - #[serde(rename = "secretarySkinId")] - pub secretary_skin: String, - pub theme: String, - pub vision: bool, -} - -impl UserConfig { - pub fn load() -> Result { - let value = read_json("./config/server.json")["userConfig"].clone(); - Ok(from_value(value)?) - } -} diff --git a/launcher/src/main.rs b/launcher/src/main.rs index 6f43cb4..7394300 100644 --- a/launcher/src/main.rs +++ b/launcher/src/main.rs @@ -1,6 +1,6 @@ use adb::Os; use anyhow::Result; -use config::ServerConfig; +use common_utils::ServerConfig; use frida::{DeviceManager, Frida, ScriptOption, ScriptRuntime, Session, SpawnOptions}; use scripts::{get_script, get_vision}; use std::process::Command; diff --git a/launcher/src/scripts.rs b/launcher/src/scripts.rs index 28f5c96..4c5cec1 100644 --- a/launcher/src/scripts.rs +++ b/launcher/src/scripts.rs @@ -1,5 +1,5 @@ -use crate::config::{ServerConfig, UserConfig}; use anyhow::Result; +use common_utils::{ServerConfig, UserConfig}; use std::{fs::File, io::Read}; pub fn get_script() -> Result { diff --git a/terraps/src/game/background.rs b/terraps/src/game/background.rs index 0ade241..b2ff293 100644 --- a/terraps/src/game/background.rs +++ b/terraps/src/game/background.rs @@ -1,11 +1,10 @@ -use axum::Json; -use serde_json::json; - use crate::{ constants::{config::CONFIG_JSON_PATH, user::USER_JSON_PATH}, utils::json::JSON, }; +use axum::Json; use common_utils::{read_json, write_json}; +use serde_json::json; pub async fn background_set_bg(Json(payload): JSON) -> JSON { let bg_id = payload["bgId"].clone();