Add config files (and refactor parts of RTE)

This commit is contained in:
illegitimate-egg 2025-03-17 00:38:36 +00:00
parent fb48a3c0ba
commit 488712b71c
16 changed files with 200 additions and 35 deletions

1
.actrc
View File

@ -1 +0,0 @@
-P ubuntu-latest=ghcr.io/catthehacker/ubuntu:rust-latest

2
.cargo/config.toml Normal file
View File

@ -0,0 +1,2 @@
[target.'cfg(all())']
runner = "./.cargo/runner.sh"

2
.cargo/runner.sh Executable file
View File

@ -0,0 +1,2 @@
#!/bin/bash
cd ../rte && exec "$@"

84
Cargo.lock generated
View File

@ -125,6 +125,12 @@ dependencies = [
"powerfmt",
]
[[package]]
name = "equivalent"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f"
[[package]]
name = "flate2"
version = "1.1.0"
@ -146,6 +152,22 @@ dependencies = [
"wasi",
]
[[package]]
name = "hashbrown"
version = "0.15.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289"
[[package]]
name = "indexmap"
version = "2.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3954d50fe15b02142bf25d3b8bdadb634ec3948f103d04ffe3031bc8fe9d7058"
dependencies = [
"equivalent",
"hashbrown",
]
[[package]]
name = "instant"
version = "0.1.13"
@ -189,7 +211,9 @@ dependencies = [
"regex",
"rhai",
"rhai-rand",
"serde",
"simple_logger",
"toml",
]
[[package]]
@ -408,18 +432,18 @@ checksum = "6ea1a2d0a644769cc99faa24c3ad26b379b786fe7c36fd3c546254801650e6dd"
[[package]]
name = "serde"
version = "1.0.218"
version = "1.0.219"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e8dfc9d19bdbf6d17e22319da49161d5d0108e4188e8b680aef6299eed22df60"
checksum = "5f0e2c6ed6606019b4e29e69dbaba95b11854410e5347d525002456dbbb786b6"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.218"
version = "1.0.219"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f09503e191f4e797cb8aac08e9a4a4695c5edf6a2e70e376d961ddd5c969f82b"
checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00"
dependencies = [
"proc-macro2",
"quote",
@ -438,6 +462,15 @@ dependencies = [
"serde",
]
[[package]]
name = "serde_spanned"
version = "0.6.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87607cb1398ed59d48732e575a4c28a7a8ebf2454b964fe3f224f2afc07909e1"
dependencies = [
"serde",
]
[[package]]
name = "simple_logger"
version = "5.0.0"
@ -538,6 +571,40 @@ dependencies = [
"crunchy",
]
[[package]]
name = "toml"
version = "0.8.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cd87a5cdd6ffab733b2f74bc4fd7ee5fff6634124999ac278c35fc78c6120148"
dependencies = [
"serde",
"serde_spanned",
"toml_datetime",
"toml_edit",
]
[[package]]
name = "toml_datetime"
version = "0.6.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0dd7358ecb8fc2f8d014bf86f6f638ce72ba252a2c3a2572f2a795f1d23efb41"
dependencies = [
"serde",
]
[[package]]
name = "toml_edit"
version = "0.22.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "17b4795ff5edd201c7cd6dca065ae59972ce77d1b80fa0a84d94950ece7d1474"
dependencies = [
"indexmap",
"serde",
"serde_spanned",
"toml_datetime",
"winnow",
]
[[package]]
name = "unicode-ident"
version = "1.0.17"
@ -695,6 +762,15 @@ version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
[[package]]
name = "winnow"
version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e97b544156e9bebe1a0ffbc03484fc1ffe3100cbce3ffb17eac35f7cdd7ab36"
dependencies = [
"memchr",
]
[[package]]
name = "zerocopy"
version = "0.7.35"

View File

@ -13,11 +13,7 @@ flate2 = "1.0.30"
log = "0.4.26"
regex = "1.11.1"
rhai-rand = "0.1.6"
[dependencies.simple_logger]
version = "5.0.0"
features = ["threads", "nightly"]
[dependencies.rhai]
version = "1.21.0"
features = ["sync"]
serde = { version = "1.0.219", features = ["derive"] }
toml = "0.8.20"
simple_logger = { version = "5.0.0", features = ["threads", "nightly"] }
rhai = { version = "1.21.0", features = ["sync"] }

11
rte/config.toml Normal file
View File

@ -0,0 +1,11 @@
[server]
name = "mcrizzledizzle default"
motd = "For shits and giggles"
port = 25565
[world]
world = "world.wrld" # Custom world type, not interchangable
# Generation parameters, when a world is read these are ignored
size_x = 64
size_y = 32
size_z = 64

66
src/config.rs Normal file
View File

@ -0,0 +1,66 @@
use log::warn;
use serde::Deserialize;
use std::{
fs::File,
io::{Read, Write},
};
use crate::error::AppError;
#[derive(Clone, Debug, Deserialize)]
pub struct Config {
pub server: ServerConfig,
pub world: WorldConfig,
}
// Hmm hmm hmm hmm... the great, iconic, symbol of nobility. My sibilantic friend, ServerSonfig.
// Your hour has passed and
#[derive(Clone, Debug, Deserialize)]
pub struct ServerConfig {
pub port: u16,
pub name: String,
pub motd: String,
}
#[derive(Clone, Debug, Deserialize)]
pub struct WorldConfig {
pub world: String,
pub size_x: i16,
pub size_y: i16,
pub size_z: i16,
}
impl Config {
pub fn load() -> Result<Self, AppError> {
// Load the config file
let mut config_file = match File::open("config.toml") {
Ok(result) => result,
Err(_) => {
const CONFIG_FILE_DATA: &str = r#"[server]
name = "mcrizzledizzle default"
motd = "For shits and giggles"
port = 25565
[world]
world = "world.wrld" # Custom world type, not interchangable
# Generation parameters, when a world is read these are ignored
size_x = 64
size_y = 32
size_z = 64
"#;
warn!("No config file was present! Generating one now.");
let mut config_file = File::create("config.toml")?;
config_file.write_all(CONFIG_FILE_DATA.as_bytes())?;
File::open("config.toml").expect("Failed to create config.toml")
}
};
let mut config_data = String::new();
config_file
.read_to_string(&mut config_data)
.expect("Failed to read config file");
Ok(toml::from_str(&config_data).expect("Failed to deserialize config.toml"))
}
}

View File

@ -4,6 +4,7 @@ use std::net::{SocketAddr, TcpListener};
use std::sync::{Arc, Mutex};
mod command;
mod config;
mod error;
mod extensions;
mod network;
@ -11,6 +12,7 @@ mod player;
mod utils;
mod world;
use config::Config;
use error::AppError;
use extensions::{Extensions, PlayersWrapper, WorldWrapper};
use network::handle_client;
@ -27,22 +29,25 @@ fn main() {
}
fn run() -> Result<(), AppError> {
let config = Config::load()?;
let players: [Player; 255] = core::array::from_fn(|_| Player::default());
let players_arc = Arc::new(Mutex::new(players));
let world_instance: World = World::load()?;
let world_instance: World = World::load(&config.world)?;
let world_arc = Arc::new(Mutex::new(world_instance));
let addr = SocketAddr::from(([0, 0, 0, 0], 25565));
let addr = SocketAddr::from(([0, 0, 0, 0], config.server.port));
let listener = TcpListener::bind(addr)?;
let mut thread_number: u8 = 0;
let world_arc_clone_main_thread = Arc::clone(&world_arc);
let world_config_clone = config.clone().world;
ctrlc::set_handler(move || {
println!();
info!("SAVING");
World::save(world_arc_clone_main_thread.clone()).unwrap(); // Fortnite save the world
World::save(&world_config_clone, world_arc_clone_main_thread.clone()).unwrap(); // Fortnite save the world
std::process::exit(0);
})
.expect("Error handling control C, save on exit will not work");
@ -59,6 +64,7 @@ fn run() -> Result<(), AppError> {
let world_arc_clone = Arc::clone(&world_arc);
let extensions_arc_clone = Arc::clone(&extensions);
handle_client(
config.clone().server,
stream?,
thread_number,
players_arc_clone,

View File

@ -7,12 +7,14 @@ use std::thread::sleep;
use std::time::Duration;
use crate::command::handle_command;
use crate::config::ServerConfig;
use crate::extensions::{Event, EventType, Extensions};
use crate::player::{Player, PlayerStatus, SpecialPlayers};
use crate::utils::*;
use crate::world::World;
pub fn handle_client(
config: ServerConfig,
mut stream: TcpStream,
client_number: u8,
players_arc_clone: Arc<Mutex<[Player; 255]>>,
@ -90,7 +92,12 @@ pub fn handle_client(
current_player.pitch = 0;
current_player.operator = true;
let _ = bomb_server_details(&mut stream, current_player, &world_arc_clone);
let _ = bomb_server_details(
config.clone(),
&mut stream,
current_player,
&world_arc_clone,
);
for i in 0..immediate_join.len() {
if immediate_join[i] {

View File

@ -5,6 +5,7 @@ use std::io::prelude::*;
use std::net::TcpStream;
use std::sync::{Arc, Mutex};
use crate::config::ServerConfig;
use crate::error::AppError;
use crate::Player;
use crate::SpecialPlayers;
@ -32,16 +33,13 @@ pub fn client_disconnect(text: &str) -> Vec<u8> {
ret_val
}
pub fn server_identification(is_op: bool) -> Vec<u8> {
pub fn server_identification(config: ServerConfig, is_op: bool) -> Vec<u8> {
let mut ret_val: Vec<u8> = vec![];
ret_val.push(0x00);
ret_val.push(0x07);
let server_name = "Erm... what the sigma?";
ret_val.append(&mut to_mc_string(server_name).to_vec());
let server_motd = "Pragmatism not idealism";
ret_val.append(&mut to_mc_string(server_motd).to_vec());
ret_val.append(&mut to_mc_string(&config.name).to_vec());
ret_val.append(&mut to_mc_string(&config.motd).to_vec());
if is_op {
ret_val.push(0x64);
@ -212,12 +210,13 @@ pub fn send_level_data(world_arc_clone: &Arc<Mutex<World>>) -> Result<Vec<u8>, A
}
pub fn bomb_server_details(
config: ServerConfig,
stream: &mut TcpStream,
current_player: &Player,
world_arc_clone: &Arc<Mutex<World>>,
) -> Result<(), AppError> {
let mut compound_data: Vec<u8> = vec![];
compound_data.append(&mut server_identification(current_player.operator));
compound_data.append(&mut server_identification(config, current_player.operator));
compound_data.append(&mut init_level());

View File

@ -4,6 +4,7 @@ use std::sync::{Arc, Mutex};
use log::info;
use crate::config::WorldConfig;
use crate::error::AppError;
#[derive(Debug)]
@ -32,8 +33,8 @@ impl World {
world_dat
}
pub fn load() -> Result<Self, AppError> {
if fs::metadata("world.wrld").is_ok() {
pub fn load(config: &WorldConfig) -> Result<Self, AppError> {
if fs::metadata(&config.world).is_ok() {
let mut world: World = World {
size_x: 0,
size_y: 0,
@ -41,7 +42,7 @@ impl World {
data: Vec::new(),
};
let world_data_raw = fs::read("world.wrld")?;
let world_data_raw = fs::read(&config.world)?;
if world_data_raw.len() < 6 {
return Err(AppError::InvalidWorldFile);
}
@ -61,20 +62,20 @@ impl World {
}
world.data = world_data_raw[6..].to_vec();
info!("Loaded world {}", "world.wrld");
info!("Loaded world {}", &config.world);
Ok(world)
} else {
info!("Creating word {}", "world.wrld");
info!("Creating word {}", &config.world);
Ok(World {
size_x: 64,
size_y: 32,
size_z: 64,
data: World::build(64, 32, 64),
size_x: config.size_x,
size_y: config.size_y,
size_z: config.size_z,
data: World::build(config.size_x, config.size_y, config.size_z),
})
}
}
pub fn save(world_arc_clone: Arc<Mutex<World>>) -> Result<(), AppError> {
pub fn save(config: &WorldConfig, world_arc_clone: Arc<Mutex<World>>) -> Result<(), AppError> {
let mut to_write: Vec<u8> = Vec::new();
{
let mut world_dat = world_arc_clone.lock()?;
@ -88,7 +89,7 @@ impl World {
to_write.append(&mut world_dat.data);
}
let mut file = File::create("world.wrld")?;
let mut file = File::create(&config.world)?;
Ok(file.write_all(&to_write)?)
}
}

Binary file not shown.