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]
members = ["gameserver", "proto", "sdkserver", "xtask"]
members = ["common", "gameserver", "proto", "sdkserver", "xtask"]
resolver = "2"
[workspace.package]
@ -51,6 +51,7 @@ tracing-subscriber = { version = "0.3.18", features = [
] }
tracing-bunyan-formatter = "0.3.9"
common = { path = "common/" }
proto = { path = "proto/" }
[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
[dependencies]
common.workspace = true
ansi_term.workspace = true
anyhow.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 serde::Deserialize;
use serde_json::from_str;
@ -6,25 +7,7 @@ const DEFAULT_GLOBALS: &str = include_str!("../../globals.json");
lazy_static! {
pub static ref INSTANCE: Globals = {
let local_config = std::path::Path::new("globals.json");
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()
};
let data = load_or_create_config("globals.json", DEFAULT_GLOBALS);
from_str(&data).unwrap()
};
}

View file

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

View file

@ -1,5 +1,6 @@
use std::collections::HashMap;
use common::util::load_or_create_config;
use lazy_static::lazy_static;
use serde::Deserialize;
use serde_json::from_str;
@ -16,25 +17,7 @@ pub struct VersionConfig {
lazy_static! {
pub static ref INSTANCE: HashMap<String, VersionConfig> = {
let local_config = std::path::Path::new("versions.json");
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()
};
let data = load_or_create_config("versions.json", DEFAULT_VERSIONS);
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 handle1 = thread::spawn(move || {
let mut gameserver = Command::new("cargo")
.current_dir("gameserver")
.arg("run")
.arg("--bin")
.arg("gameserver")
@ -38,6 +39,7 @@ fn spawn_servers(release: bool) -> Result<(), Box<dyn std::error::Error>> {
let handle2 = thread::spawn(move || {
let mut sdkserver = Command::new("cargo")
.current_dir("sdkserver")
.arg("run")
.arg("--bin")
.arg("sdkserver")