Skip to content

Commit

Permalink
moving to strongly typed configs
Browse files Browse the repository at this point in the history
  • Loading branch information
Rhelvetican committed Jul 7, 2024
1 parent dfd325b commit 8ff7f28
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 73 deletions.
69 changes: 69 additions & 0 deletions common/src/cfg.rs
Original file line number Diff line number Diff line change
@@ -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<Self> {
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<Self> {
let value = read_json("./config/server.json")["userConfig"].clone();
Ok(from_value(value)?)
}
}
2 changes: 2 additions & 0 deletions common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
mod cfg;
mod fs;
mod json;

pub use self::{
cfg::{RestorePrevState, ServerConfig, UserConfig},
fs::mkfile,
json::{read_json, write_json},
};
68 changes: 0 additions & 68 deletions launcher/src/config.rs
Original file line number Diff line number Diff line change
@@ -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<Self> {
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<Self> {
let value = read_json("./config/server.json")["userConfig"].clone();
Ok(from_value(value)?)
}
}
2 changes: 1 addition & 1 deletion launcher/src/main.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion launcher/src/scripts.rs
Original file line number Diff line number Diff line change
@@ -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<String> {
Expand Down
5 changes: 2 additions & 3 deletions terraps/src/game/background.rs
Original file line number Diff line number Diff line change
@@ -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();
Expand Down

0 comments on commit 8ff7f28

Please sign in to comment.