Skip to content

Commit

Permalink
Refactor application and move all parts into separate creates
Browse files Browse the repository at this point in the history
  • Loading branch information
artemijan committed Dec 16, 2024
1 parent 672d0e9 commit 956f97c
Show file tree
Hide file tree
Showing 118 changed files with 656 additions and 654 deletions.
104 changes: 68 additions & 36 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 1 addition & 62 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,63 +1,2 @@
[package]
name = "L2Shablya"
version = "0.1.0"
edition = "2021"
publish = false

[[bin]]
name = "login"
path = "src/login.rs"

[[bin]]
name = "game"
path = "src/game.rs"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lints.rust]
unsafe_code = "forbid"
unused = { level = "allow", priority = -1 }

[workspace]
members = [".", "entities", "migration"]

[profile.dev]
debug = true


[dependencies]
migration = { path = "migration" }
entities = { path = "entities" }
encoding = "^0.2.33"
rand = "^0.8.5"
rand_core = "^0.6.4"
anyhow = "^1.0.92"
thiserror = "2.0.6"
log = "^0.4.22"
serde = { version = "^1.0.214", features = ["derive"] }
num = "^0.4.3"
num-traits = "^0.2.19"
openssl = "^0.10.68"
tokio = { version = "^1.41.0", features = ["full"] }
sqlx = { version = "^0.8.2", features = [
"postgres",
"sqlite",
"runtime-tokio-rustls",
"sqlx-macros",
"chrono"
] }
futures = "^0.3.31"
async-trait = "^0.1.83"
argon2 = "^0.5.3"
serde_yaml = "^0.9.34"
strum = { version = "^0.26.3", features = ["derive"] }
dotenvy = "^0.15.7"
blowfish = "^0.9.1"
num_enum = "^0.7.3"
uuid = { version = "^1.11.0", features = ["v4"] }
dashmap = "6.1.0"
chrono = "0.4.38"
pnet = "0.35.0"
reqwest = { version = "0.12.9", features = ["blocking"] }
tracing = "0.1.41"
tracing-subscriber = "0.3.19"
sea-orm = { version = "1.1.2" }
members = ["entities", "game", "l2-core", "login", "migration"]
6 changes: 5 additions & 1 deletion entities/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
pub mod entities;
use sea_orm::DatabaseConnection;

pub mod entities;

pub type DBPool = DatabaseConnection;
18 changes: 18 additions & 0 deletions game/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "game"
version = "0.1.0"
edition = "2021"
publish = false
[[bin]]
name = "game"
path = "src/main.rs"
[dependencies]
entities = { path = "../entities" }
l2-core = { path = "../l2-core" }
anyhow = "^1.0.92"
num = "^0.4.3"
num-traits = "^0.2.19"
tokio = { version = "^1.41.0", features = ["full"] }
async-trait = "^0.1.83"
tracing = "0.1.41"
tracing-subscriber = "0.3.19"
4 changes: 2 additions & 2 deletions src/game_server/controller.rs → game/src/controller.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::common::traits::IpBan;
use l2_core::traits::IpBan;
use std::sync::Arc;
use crate::common::config::gs::GSServer;
use l2_core::config::gs::GSServer;

#[derive(Clone, Debug)]
pub struct Controller {
Expand Down
26 changes: 9 additions & 17 deletions src/game_server/handlers.rs → game/src/handlers.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
use crate::common::dto::OutboundConnection;
use crate::common::errors::Packet;
use crate::common::traits::handlers::{OutboundHandler, PacketHandler};
use crate::common::traits::Shutdown;
use crate::crypt::login::Encryption;
use crate::database::DBPool;
use crate::game_server::controller::Controller;
use crate::common::config::gs::GSServer;
use crate::game_server::lsp_factory::build_ls_packet;
use l2_core::dto::OutboundConnection;
use l2_core::errors::Packet;
use l2_core::traits::handlers::{OutboundHandler, PacketHandler};
use l2_core::traits::Shutdown;
use l2_core::crypt::login::Encryption;
use entities::DBPool;
use crate::controller::Controller;
use l2_core::config::gs::GSServer;
use crate::lsp_factory::build_ls_packet;
use anyhow::{bail, Error};
use std::sync::Arc;
use std::time::SystemTime;
use tokio::net::tcp::{OwnedReadHalf, OwnedWriteHalf};
use tokio::net::TcpStream;
use tokio::sync::{Mutex, Notify};
Expand All @@ -27,16 +26,11 @@ pub struct LoginHandler {
shutdown_notifier: Arc<Notify>,
timeout: u8,
blowfish: Encryption,
connection_start_time: SystemTime,
}
impl LoginHandler {
pub fn set_blowfish(&mut self, blowfish: Encryption) {
self.blowfish = blowfish;
}
pub fn reset_blowfish(&mut self) {
let cfg = self.controller.get_cfg();
self.blowfish = Encryption::from_u8_key(cfg.blowfish_key.as_bytes());
}
}
impl Shutdown for LoginHandler {
fn get_shutdown_listener(&self) -> Arc<Notify> {
Expand All @@ -58,7 +52,6 @@ impl PacketHandler for LoginHandler {
}

fn new(stream: TcpStream, db_pool: DBPool, controller: Arc<Self::ControllerType>) -> Self {
let connection_start_time = SystemTime::now();
let (tcp_reader, tcp_writer) = stream.into_split();
let cfg = controller.get_cfg();
Self {
Expand All @@ -69,7 +62,6 @@ impl PacketHandler for LoginHandler {
db_pool,
blowfish: Encryption::from_u8_key(cfg.blowfish_key.as_bytes()),
timeout: cfg.client.timeout,
connection_start_time,
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/game_server/lsp_factory.rs → game/src/lsp_factory.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use tracing::error;
use super::handlers::LoginHandler;
use crate::common::packets::common::{GSLoginFail, HandleablePacket, ReadablePacket};
use crate::common::packets::gs_2_ls::ChangePassword;
use crate::common::packets::ls_2_gs::{
use l2_core::packets::common::{GSLoginFail, ReadablePacket};
use l2_core::packets::gs_2_ls::ChangePassword;
use l2_core::packets::ls_2_gs::{
AuthGS, InitLS, KickPlayer, PlayerAuthResponse, RequestChars,
};
use crate::packets::HandleablePacket;

pub fn build_ls_packet(
data: &[u8],
Expand Down
23 changes: 15 additions & 8 deletions src/game.rs → game/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
use crate::common::traits::server::Server;
use crate::game_server::controller::Controller;
use crate::game_server::handlers::LoginHandler;
use crate::game_server::GameServer;
use crate::controller::Controller;
use crate::handlers::LoginHandler;
use l2_core::config::gs::GSServer;
use l2_core::traits::server::Server;
use std::sync::Arc;

mod common;
mod crypt;
mod database;
mod game_server;
mod controller;
mod handlers;
mod lsp_factory;
mod packets;

pub struct GameServer;

impl Server for GameServer {
type ConfigType = GSServer;
type ControllerType = Controller;
}

///
/// # Panics
Expand Down
14 changes: 14 additions & 0 deletions game/src/packets/handleable/change_password.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use l2_core::packets::{error::PacketRun, gs_2_ls::ChangePassword};
use crate::{
handlers::LoginHandler,
};
use async_trait::async_trait;
use crate::packets::HandleablePacket;

#[async_trait]
impl HandleablePacket for ChangePassword {
type HandlerType = LoginHandler;
async fn handle(&self, _: &mut Self::HandlerType) -> Result<(), PacketRun> {
todo!()
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
use crate::common::packets::common::GSStatus;
use crate::common::packets::error::PacketRun;
use crate::common::packets::gs_2_ls::{GSStatusUpdate, PlayerInGame};
use crate::common::packets::write::SendablePacketBuffer;
use crate::common::traits::handlers::PacketHandler;
use crate::{
common::packets::{common::HandleablePacket, ls_2_gs},
game_server::handlers::LoginHandler,
};
use crate::handlers::LoginHandler;
use async_trait::async_trait;
use l2_core::packets::error::PacketRun;
use l2_core::packets::gs_2_ls::{GSStatusUpdate, PlayerInGame};
use l2_core::packets::ls_2_gs;
use l2_core::traits::handlers::PacketHandler;
use tracing::{info, instrument};
use crate::packets::HandleablePacket;

#[async_trait]
impl HandleablePacket for ls_2_gs::AuthGS {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use async_trait::async_trait;

use crate::common::packets::error::PacketRun;
use l2_core::packets::common::GSLoginFail;
use l2_core::packets::error::PacketRun;
use crate::{
common::packets::common::{GSLoginFail, HandleablePacket},
game_server::handlers::LoginHandler,
handlers::LoginHandler,
};
use crate::packets::HandleablePacket;

#[async_trait]
impl HandleablePacket for GSLoginFail {
type HandlerType = LoginHandler;
async fn handle(&self, gs: &mut Self::HandlerType) -> Result<(), PacketRun> {
async fn handle(&self, _: &mut Self::HandlerType) -> Result<(), PacketRun> {
Err(PacketRun {
msg: Some(format!(
"Failed to register on Login server{:?}",
Expand Down
Loading

0 comments on commit 956f97c

Please sign in to comment.