Create configs in server's folder, fix xtask for asset loading

This commit is contained in:
xeon 2024-04-15 18:10:02 +03:00
parent 133ac7bbc8
commit d5f2e42ac9
9 changed files with 27 additions and 39 deletions

View file

@ -1,5 +1,5 @@
[workspace] [workspace]
members = ["gameserver", "proto", "sdkserver", "xtask"] members = ["common", "gameserver", "proto", "sdkserver", "xtask"]
resolver = "2" resolver = "2"
[workspace.package] [workspace.package]
@ -51,6 +51,7 @@ tracing-subscriber = { version = "0.3.18", features = [
] } ] }
tracing-bunyan-formatter = "0.3.9" tracing-bunyan-formatter = "0.3.9"
common = { path = "common/" }
proto = { path = "proto/" } proto = { path = "proto/" }
[profile.release] [profile.release]

6
common/Cargo.toml Normal file
View file

@ -0,0 +1,6 @@
[package]
name = "common"
edition = "2021"
version.workspace = true
[dependencies]

1
common/src/lib.rs Normal file
View file

@ -0,0 +1 @@
pub mod util;

8
common/src/util.rs Normal file
View file

@ -0,0 +1,8 @@
pub fn load_or_create_config(path: &str, defaults: &str) -> String {
if let Ok(data) = std::fs::read_to_string(path) {
data
} else {
std::fs::write(path, defaults).unwrap();
defaults.to_string()
}
}

View file

@ -4,6 +4,8 @@ edition = "2021"
version.workspace = true version.workspace = true
[dependencies] [dependencies]
common.workspace = true
ansi_term.workspace = true ansi_term.workspace = true
anyhow.workspace = true anyhow.workspace = true
atomic_refcell.workspace = true atomic_refcell.workspace = true

View file

@ -1,3 +1,4 @@
use common::util::load_or_create_config;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use serde::Deserialize; use serde::Deserialize;
use serde_json::from_str; use serde_json::from_str;
@ -6,25 +7,7 @@ const DEFAULT_GLOBALS: &str = include_str!("../../globals.json");
lazy_static! { lazy_static! {
pub static ref INSTANCE: Globals = { pub static ref INSTANCE: Globals = {
let local_config = std::path::Path::new("globals.json"); let data = load_or_create_config("globals.json", DEFAULT_GLOBALS);
let data = if local_config.exists() {
std::fs::read_to_string("globals.json").unwrap()
} else {
let config = dirs::config_dir()
.expect("No config directory found")
.join("hkrpg-gameserver");
std::fs::create_dir_all(&config).unwrap();
let env = config.join("globals.json");
if !env.exists() {
std::fs::write(&env, DEFAULT_GLOBALS).unwrap();
}
std::fs::read_to_string(&env).unwrap()
};
from_str(&data).unwrap() from_str(&data).unwrap()
}; };
} }

View file

@ -4,6 +4,8 @@ version = "0.1.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
common.workspace = true
anyhow.workspace = true anyhow.workspace = true
env_logger.workspace = true env_logger.workspace = true

View file

@ -1,5 +1,6 @@
use std::collections::HashMap; use std::collections::HashMap;
use common::util::load_or_create_config;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use serde::Deserialize; use serde::Deserialize;
use serde_json::from_str; use serde_json::from_str;
@ -16,25 +17,7 @@ pub struct VersionConfig {
lazy_static! { lazy_static! {
pub static ref INSTANCE: HashMap<String, VersionConfig> = { pub static ref INSTANCE: HashMap<String, VersionConfig> = {
let local_config = std::path::Path::new("versions.json"); let data = load_or_create_config("versions.json", DEFAULT_VERSIONS);
let data = if local_config.exists() {
std::fs::read_to_string("versions.json").unwrap()
} else {
let config = dirs::config_dir()
.expect("No config directory found")
.join("hkrpg-sdkserver");
std::fs::create_dir_all(&config).unwrap();
let env = config.join("versions.json");
if !env.exists() {
std::fs::write(&env, DEFAULT_VERSIONS).unwrap();
}
std::fs::read_to_string(&env).unwrap()
};
from_str(&data).unwrap() from_str(&data).unwrap()
}; };
} }

View file

@ -23,6 +23,7 @@ fn spawn_servers(release: bool) -> Result<(), Box<dyn std::error::Error>> {
let tx1 = tx.clone(); let tx1 = tx.clone();
let handle1 = thread::spawn(move || { let handle1 = thread::spawn(move || {
let mut gameserver = Command::new("cargo") let mut gameserver = Command::new("cargo")
.current_dir("gameserver")
.arg("run") .arg("run")
.arg("--bin") .arg("--bin")
.arg("gameserver") .arg("gameserver")
@ -38,6 +39,7 @@ fn spawn_servers(release: bool) -> Result<(), Box<dyn std::error::Error>> {
let handle2 = thread::spawn(move || { let handle2 = thread::spawn(move || {
let mut sdkserver = Command::new("cargo") let mut sdkserver = Command::new("cargo")
.current_dir("sdkserver")
.arg("run") .arg("run")
.arg("--bin") .arg("--bin")
.arg("sdkserver") .arg("sdkserver")