1
Fork 0
mirror of https://github.com/redstrate/Kawari.git synced 2025-04-23 15:47:45 +00:00
kawari/src/config.rs

266 lines
6.3 KiB
Rust
Raw Normal View History

use std::{
net::{IpAddr, SocketAddr},
str::FromStr,
};
2023-10-05 12:09:05 -04:00
use serde::{Deserialize, Serialize};
/// Configuration for the admin server.
2023-10-05 12:09:05 -04:00
#[derive(Serialize, Deserialize)]
pub struct AdminConfig {
pub port: u16,
pub listen_address: String,
}
2024-06-29 14:06:44 -04:00
impl Default for AdminConfig {
fn default() -> Self {
Self {
port: 5800,
listen_address: "127.0.0.1".to_string(),
}
}
}
impl AdminConfig {
/// Returns the configured IP address & port as a `SocketAddr`.
pub fn get_socketaddr(&self) -> SocketAddr {
SocketAddr::from((
IpAddr::from_str(&self.listen_address).expect("Invalid IP address format in config!"),
self.port,
))
}
}
/// Configuration for the frontier server.
#[derive(Serialize, Deserialize)]
pub struct FrontierConfig {
pub port: u16,
pub listen_address: String,
pub worlds_open: bool,
2024-06-29 14:06:44 -04:00
pub login_open: bool,
}
impl Default for FrontierConfig {
fn default() -> Self {
Self {
port: 5857,
listen_address: "127.0.0.1".to_string(),
worlds_open: true,
login_open: true,
}
}
}
impl FrontierConfig {
/// Returns the configured IP address & port as a `SocketAddr`.
pub fn get_socketaddr(&self) -> SocketAddr {
SocketAddr::from((
IpAddr::from_str(&self.listen_address).expect("Invalid IP address format in config!"),
self.port,
))
}
}
/// Configuration for the lobby server.
#[derive(Serialize, Deserialize)]
pub struct LobbyConfig {
pub port: u16,
pub listen_address: String,
}
impl Default for LobbyConfig {
fn default() -> Self {
Self {
port: 7000,
listen_address: "127.0.0.1".to_string(),
}
}
}
impl LobbyConfig {
/// Returns the configured IP address & port as a `SocketAddr`.
pub fn get_socketaddr(&self) -> SocketAddr {
SocketAddr::from((
IpAddr::from_str(&self.listen_address).expect("Invalid IP address format in config!"),
self.port,
))
}
}
2024-05-11 13:41:00 -04:00
/// Configuration for the login server.
#[derive(Serialize, Deserialize)]
pub struct LoginConfig {
pub port: u16,
pub listen_address: String,
}
impl Default for LoginConfig {
fn default() -> Self {
Self {
port: 6700,
listen_address: "127.0.0.1".to_string(),
}
}
}
impl LoginConfig {
/// Returns the configured IP address & port as a `SocketAddr`.
pub fn get_socketaddr(&self) -> SocketAddr {
SocketAddr::from((
IpAddr::from_str(&self.listen_address).expect("Invalid IP address format in config!"),
self.port,
))
}
}
/// Configuration for the patch server.
#[derive(Serialize, Deserialize)]
pub struct PatchConfig {
pub port: u16,
pub listen_address: String,
}
impl Default for PatchConfig {
fn default() -> Self {
Self {
port: 6900,
listen_address: "127.0.0.1".to_string(),
}
}
}
impl PatchConfig {
/// Returns the configured IP address & port as a `SocketAddr`.
pub fn get_socketaddr(&self) -> SocketAddr {
SocketAddr::from((
IpAddr::from_str(&self.listen_address).expect("Invalid IP address format in config!"),
self.port,
))
}
}
/// Configuration for the web server.
#[derive(Serialize, Deserialize)]
pub struct WebConfig {
pub port: u16,
pub listen_address: String,
}
impl Default for WebConfig {
fn default() -> Self {
Self {
port: 5801,
listen_address: "127.0.0.1".to_string(),
}
}
}
impl WebConfig {
/// Returns the configured IP address & port as a `SocketAddr`.
pub fn get_socketaddr(&self) -> SocketAddr {
SocketAddr::from((
IpAddr::from_str(&self.listen_address).expect("Invalid IP address format in config!"),
self.port,
))
}
}
/// Configuration for the world server.
#[derive(Serialize, Deserialize)]
pub struct WorldConfig {
pub port: u16,
pub listen_address: String,
/// See the World Excel sheet.
pub world_id: u16,
}
impl Default for WorldConfig {
fn default() -> Self {
Self {
port: 7100,
listen_address: "127.0.0.1".to_string(),
world_id: 63, // Gilgamesh
}
}
}
impl WorldConfig {
/// Returns the configured IP address & port as a `SocketAddr`.
pub fn get_socketaddr(&self) -> SocketAddr {
SocketAddr::from((
IpAddr::from_str(&self.listen_address).expect("Invalid IP address format in config!"),
self.port,
))
}
}
/// Global and all-encompassing config.
/// Settings that affect all servers belong here.
#[derive(Serialize, Deserialize)]
pub struct Config {
2024-05-11 13:41:00 -04:00
#[serde(default = "default_supported_platforms")]
pub supported_platforms: Vec<String>,
#[serde(default)]
2025-03-08 13:27:41 -05:00
pub boot_patches_location: String,
#[serde(default)]
pub game_location: String,
#[serde(default)]
pub admin: AdminConfig,
#[serde(default)]
pub frontier: FrontierConfig,
#[serde(default)]
pub lobby: LobbyConfig,
#[serde(default)]
pub login: LoginConfig,
#[serde(default)]
pub patch: PatchConfig,
#[serde(default)]
pub web: WebConfig,
#[serde(default)]
pub world: WorldConfig,
}
impl Default for Config {
fn default() -> Self {
Self {
boot_patches_location: String::new(),
2025-03-08 13:27:41 -05:00
supported_platforms: default_supported_platforms(),
game_location: String::new(),
admin: AdminConfig::default(),
frontier: FrontierConfig::default(),
lobby: LobbyConfig::default(),
login: LoginConfig::default(),
patch: PatchConfig::default(),
web: WebConfig::default(),
world: WorldConfig::default(),
}
}
2024-05-11 13:41:00 -04:00
}
impl Config {
pub fn supports_platform(&self, platform: &String) -> bool {
self.supported_platforms.contains(platform)
}
}
fn default_supported_platforms() -> Vec<String> {
vec!["win32".to_string()]
}
pub fn get_config() -> Config {
if let Ok(data) = std::fs::read_to_string("config.yaml") {
serde_yaml_ng::from_str(&data).expect("Failed to parse")
2024-05-11 13:41:00 -04:00
} else {
Config::default()
}
2025-03-08 13:27:41 -05:00
}