Add documentation
This commit is contained in:
parent
25119b5ca9
commit
64cedcfa56
@ -1,5 +1,11 @@
|
||||
extern crate chrono;
|
||||
extern crate colorful;
|
||||
// logger.rs
|
||||
// author: Garen Tyler
|
||||
// description:
|
||||
// A global logger for Composition.
|
||||
// The Logger struct makes it easy to create useful server logs.
|
||||
|
||||
extern crate chrono; // Used because std::time sucks.
|
||||
extern crate toml; // Colorful console logging is fun.
|
||||
|
||||
use chrono::prelude::*;
|
||||
use colorful::{Color, Colorful};
|
||||
@ -7,13 +13,15 @@ use std::fs::File;
|
||||
use std::fs::OpenOptions;
|
||||
use std::io::prelude::*;
|
||||
|
||||
// Uses chrono::Local to get a timestamp in YYYY-MM-DD HH:MM:SS format, in local time.
|
||||
pub fn get_timestring() -> String {
|
||||
Local::now().format("%Y-%m-%d %H:%M:%S").to_string()
|
||||
}
|
||||
// Just a helper function to avoid repeating myself.
|
||||
pub fn log(logtype: &str, logmessage: &str) -> String {
|
||||
format!("{} [{}] - {}", get_timestring(), logtype, logmessage)
|
||||
}
|
||||
|
||||
// So I can do logger::new("log.txt") instead of logger::Logger::new("log.txt").
|
||||
pub fn new(logfile: &str) -> Logger {
|
||||
Logger::new(logfile)
|
||||
}
|
||||
@ -28,27 +36,33 @@ impl Logger {
|
||||
logfile: logfile.to_owned(),
|
||||
}
|
||||
}
|
||||
// For logging something important, like the server port.
|
||||
pub fn important(&self, s: &str) {
|
||||
let l = log("IMPORTANT", s);
|
||||
println!("{}", l.clone().color(Color::Green));
|
||||
self.append(&l);
|
||||
}
|
||||
// For everything that doesn't fit into any of the other categories.
|
||||
pub fn info(&self, s: &str) {
|
||||
let l = log("INFO", s);
|
||||
// Not sure whether I want normal logs to be user controlled color or white.
|
||||
// println!("{}", l.clone().color(Color::White));
|
||||
println!("{}", l.clone());
|
||||
self.append(&l);
|
||||
}
|
||||
// For warnings.
|
||||
pub fn warn(&self, s: &str) {
|
||||
let l = log("WARN", s);
|
||||
println!("{}", l.clone().color(Color::LightYellow));
|
||||
self.append(&l);
|
||||
}
|
||||
// For errors.
|
||||
pub fn error(&self, s: &str) {
|
||||
let l = log("ERROR", s);
|
||||
println!("{}", l.clone().color(Color::LightRed));
|
||||
self.append(&l);
|
||||
}
|
||||
// Append to the logfile.
|
||||
pub fn append(&self, s: &str) {
|
||||
let a = || -> std::io::Result<()> {
|
||||
let mut file = OpenOptions::new()
|
||||
@ -63,18 +77,21 @@ impl Logger {
|
||||
self.logger_error(&format!("Could not write to log file {}", self.logfile));
|
||||
}
|
||||
}
|
||||
// Clear the logfile. Adds an important note to the file that it was cleared.
|
||||
pub fn clear(&self) {
|
||||
if std::fs::remove_file(&self.logfile).is_err() {
|
||||
self.logger_error(&format!("Could not write to log file {}", self.logfile));
|
||||
}
|
||||
self.important(&format!("Cleared log file {}", self.logfile));
|
||||
}
|
||||
// Read the logfile into a String.
|
||||
pub fn read(&self) -> std::io::Result<String> {
|
||||
let mut f = File::open(&self.logfile)?;
|
||||
let mut buffer = String::new();
|
||||
f.read_to_string(&mut buffer)?;
|
||||
Ok(buffer)
|
||||
}
|
||||
// For a critical logger error. This doesn't append to the logfile.
|
||||
pub fn logger_error(&self, error_message: &str) {
|
||||
let l = log("ERROR", error_message);
|
||||
println!("{}", l.clone().color(Color::LightRed));
|
||||
|
@ -3,9 +3,7 @@
|
||||
|
||||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
extern crate ozelot;
|
||||
extern crate serde;
|
||||
extern crate toml;
|
||||
pub mod logger;
|
||||
pub mod mctypes;
|
||||
pub mod net;
|
||||
@ -24,11 +22,11 @@ fn main() {
|
||||
log.info("Network thread started");
|
||||
net::start_listening();
|
||||
});
|
||||
|
||||
// Loop the main thread for now.
|
||||
loop {}
|
||||
}
|
||||
|
||||
// Not in it's own config module because of name conflicts.
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct Config {
|
||||
pub port: u16,
|
||||
|
@ -1,6 +1,13 @@
|
||||
// mctypes.rs
|
||||
// author: Garen Tyler
|
||||
// description:
|
||||
// A recreation of all the types necessary in the protocol.
|
||||
// Directly taken from https://wiki.vg/Protocol#Data_types.
|
||||
|
||||
use std::io::prelude::*;
|
||||
use std::net::TcpStream;
|
||||
|
||||
// Helper functions.
|
||||
pub fn read_byte(t: &mut TcpStream) -> std::io::Result<u8> {
|
||||
let mut buffer = [0u8; 1];
|
||||
t.read_exact(&mut buffer)?;
|
||||
@ -25,6 +32,7 @@ pub fn get_bytes(v: Vec<u8>, l: usize) -> Box<[u8]> {
|
||||
}
|
||||
a.into_boxed_slice()
|
||||
}
|
||||
// Makes returning errors shorter.
|
||||
pub fn io_error(s: &str) -> std::io::Error {
|
||||
use std::io::{Error, ErrorKind};
|
||||
Error::new(ErrorKind::Other, s)
|
||||
|
@ -1,3 +1,8 @@
|
||||
// net.rs
|
||||
// author: Garen Tyler
|
||||
// description:
|
||||
// The module with everything to do with networkng.
|
||||
|
||||
use crate::{config, log};
|
||||
use std::net::{TcpListener, TcpStream};
|
||||
|
||||
@ -62,7 +67,7 @@ fn handle_client(t: TcpStream) -> std::io::Result<()> {
|
||||
"protocol": 578
|
||||
},
|
||||
"players": {
|
||||
"max": 2147483647,
|
||||
"max": 420,
|
||||
"online": 69,
|
||||
"sample": [
|
||||
{
|
||||
|
@ -1,3 +1,8 @@
|
||||
// old_net.rs
|
||||
// author: Garen Tyler
|
||||
// description:
|
||||
// This was the original implementation of net.rs. Only use as a reference.
|
||||
|
||||
pub static SERVER_LISTENER_ADDRESS: &str = "127.0.0.1:25565";
|
||||
pub static SOCKET_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(5);
|
||||
extern crate serde;
|
||||
|
@ -1,3 +1,9 @@
|
||||
// protocol.rs
|
||||
// author: Garen Tyler
|
||||
// description:
|
||||
// This module contains all the packet structs.
|
||||
// Not all of them are implemented, and new ones will be added as necessary.
|
||||
|
||||
use crate::mctypes::*;
|
||||
use std::net::TcpStream;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user