quectocraft/src/main.rs

70 lines
2.0 KiB
Rust
Raw Permalink Normal View History

2022-12-05 16:31:45 +00:00
use std::borrow::Cow;
use std::time::Duration;
use std::io::Write;
2022-12-02 23:27:53 +00:00
2022-12-05 16:31:45 +00:00
use chrono::Utc;
2022-12-04 06:06:21 +00:00
use env_logger::Env;
2022-12-17 20:45:43 +00:00
use log::{info, warn};
2022-12-02 23:27:53 +00:00
use mlua::Lua;
use network::NetworkServer;
2022-12-04 06:06:21 +00:00
use plugins::Plugins;
2022-12-02 23:27:53 +00:00
2022-12-17 20:45:43 +00:00
use crate::config::{load_config, LoginMode};
2022-12-17 05:10:01 +00:00
mod config;
2022-12-02 23:27:53 +00:00
mod plugins;
mod protocol;
mod network;
2022-12-17 05:10:01 +00:00
pub const VERSION: &str = std::env!("CARGO_PKG_VERSION");
2022-12-04 06:06:21 +00:00
2022-12-02 23:27:53 +00:00
fn main() {
2022-12-05 16:31:45 +00:00
env_logger::Builder::from_env(
Env::default().default_filter_or("info")
).format(|buf, record| {
2022-12-04 06:06:21 +00:00
2022-12-05 16:31:45 +00:00
let now = Utc::now().format("%Y-%m-%d %H:%M:%S");
let mut target = Cow::Borrowed(record.target());
if target.starts_with("quectocraft") {
target = Cow::Owned(record.target().replacen("quectocraft", "qc", 1));
}
let color = match record.level() {
log::Level::Error => "\x1b[31m",
log::Level::Warn => "\x1b[33m",
log::Level::Info => "\x1b[32m",
log::Level::Debug => "\x1b[37m",
log::Level::Trace => "\x1b[37m",
};
writeln!(buf, "\x1b[90m[\x1b[37m{} {color}{}\x1b[37m {}\x1b[90m]\x1b[0m {}", now, record.level(), target, record.args())
}).init();
2022-12-17 20:39:56 +00:00
info!("Starting Quectocraft version {}", VERSION);
2022-12-05 16:31:45 +00:00
2022-12-17 05:10:01 +00:00
let config = load_config().expect("Failed to load config");
2022-12-17 20:45:43 +00:00
match config.login {
LoginMode::Offline => warn!("Running in offline mode!"),
LoginMode::Velocity => info!("Running in velocity mode"),
}
2022-12-04 06:06:21 +00:00
2022-12-02 23:27:53 +00:00
let lua = Lua::new();
2022-12-04 06:06:21 +00:00
let mut plugins = Plugins::new(&lua).expect("Error initializing lua environment");
2022-12-05 16:31:45 +00:00
std::fs::create_dir_all("plugins").expect("Couldn't create the plugins directory");
2022-12-04 06:06:21 +00:00
plugins.load_plugins();
2022-12-02 23:27:53 +00:00
2022-12-17 20:39:56 +00:00
let mut server = NetworkServer::new(config, plugins);
2022-12-02 23:27:53 +00:00
let sleep_dur = Duration::from_millis(5);
let mut i = 0;
loop {
server.get_new_clients();
server.handle_connections();
if i % 1024 == 0 {
server.send_keep_alive();
i = 0;
}
i += 1;
2022-12-04 06:06:21 +00:00
std::thread::sleep(sleep_dur);
2022-12-02 23:27:53 +00:00
}
}