diff --git a/Cargo.toml b/Cargo.toml index 377de00..0d55d47 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,4 +26,6 @@ crc = "^1.0.0" bcrypt = "0.4" threadpool = "1.0" chrono = "*" +serde = "*" +ron = "*" diff --git a/patch.ron b/patch.ron new file mode 100644 index 0000000..45b8a61 --- /dev/null +++ b/patch.ron @@ -0,0 +1,5 @@ +( + path: "patchfiles", + ip: "127.0.0.1", + port: 11000, +) diff --git a/src/main.rs b/src/main.rs index 2518292..d5df569 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,7 +10,7 @@ mod ship; use std::thread; use std::time::SystemTime; -use patch::patch::{PatchServerState, generate_patch_tree}; +use patch::patch::{PatchServerState, generate_patch_tree, load_config}; use login::login::LoginServerState; use login::character::CharacterServerState; use ship::ship::ShipServerState; @@ -46,9 +46,10 @@ fn main() { let patch_thread = thread::spawn(|| { println!("[patch] starting server"); - let (patch_file_tree, patch_file_lookup) = generate_patch_tree("patchfiles/"); + let patch_config = load_config(); + let (patch_file_tree, patch_file_lookup) = generate_patch_tree(patch_config.path.as_str()); let patch_state = PatchServerState::new(patch_file_tree, patch_file_lookup); - common::mainloop::mainloop(patch_state, patch::patch::PATCH_PORT); + common::mainloop::mainloop(patch_state, patch_config.port); }); let thread_entity_gateway = entity_gateway.clone(); let auth_thread = thread::spawn(|| { diff --git a/src/patch/patch.rs b/src/patch/patch.rs index 427880e..7faf2d7 100644 --- a/src/patch/patch.rs +++ b/src/patch/patch.rs @@ -8,12 +8,12 @@ use crc::{crc32, Hasher32}; use libpso::{PacketParseError, PSOPacket}; use libpso::packet::patch::*; use libpso::crypto::pc::PSOPCCipher; +use ron::de::from_str; +use serde::Deserialize; use crate::common::network::{PacketNetworkError}; use crate::common::serverstate::{RecvServerPacket, SendServerPacket, ServerState, OnConnect, ClientId}; -pub const PATCH_PORT: u16 = 11000; - #[derive(Debug)] pub enum PatchError { PacketNetworkError(PacketNetworkError), @@ -371,3 +371,27 @@ impl Iterator for SendFileIterator { } } +#[derive(Debug, Deserialize)] +pub struct PatchConfig { + pub path: String, + pub ip: String, + pub port: u16, +} + +pub fn load_config() -> PatchConfig { + let ini_file = match fs::File::open(std::path::Path::new("patch.ron")) { + Err(err) => panic!("Failed to open patch.ron config file. \n{}", err), + Ok(ini_file) => ini_file, + }; + + let mut s = String::new(); + if let Err(err) = (&ini_file).read_to_string(&mut s) { + panic!("Failed to read patch.ron config file. \n{}", err); + } + + let config: PatchConfig = match from_str(s.as_str()) { + Ok(config) => config, + Err(err) => panic!("Failed to load values from patch.ron \n{}",err), + }; + config +} diff --git a/src/patch_main.rs b/src/patch_main.rs index 6ff6818..bf834d6 100644 --- a/src/patch_main.rs +++ b/src/patch_main.rs @@ -2,17 +2,21 @@ mod common; mod patch; -use crate::patch::patch::{PatchServerState, PatchTreeIterItem, generate_patch_tree, PATCH_PORT}; +use crate::patch::patch::{PatchServerState, PatchTreeIterItem, generate_patch_tree, load_config}; fn main() { println!("[patch] starting server"); - if let Err(_) = std::fs::read_dir("patchfiles/") { - if let Err(_) = std::fs::create_dir("patchfiles/") { - panic!("Could not create patchfiles directory!"); + let patch_config = load_config(); + + if let Err(_) = std::fs::read_dir(patch_config.path.as_str()) { + println!("Patch directory {} does not exist. Attempting to create it...", patch_config.path.as_str()); + if let Err(err) = std::fs::create_dir(patch_config.path.as_str()) { + panic!("Failed to create patch directory! \n{}", err); } } - let (patch_file_tree, patch_file_lookup) = generate_patch_tree("patchfiles/"); + + let (patch_file_tree, patch_file_lookup) = generate_patch_tree(patch_config.path.as_str()); println!("[patch] files to patch:"); let mut indent = 0; for item in patch_file_tree.flatten() { @@ -33,7 +37,7 @@ fn main() { } let patch_state = PatchServerState::new(patch_file_tree, patch_file_lookup); - common::mainloop::mainloop(patch_state, PATCH_PORT); + common::mainloop::mainloop(patch_state, patch_config.port); println!("[patch] exiting..."); }