2025-03-30 21:42:46 -04:00
|
|
|
use kawari::RECEIVE_BUFFER_SIZE;
|
2025-03-30 10:34:42 -04:00
|
|
|
use kawari::common::GameData;
|
2025-03-21 19:56:16 -04:00
|
|
|
use kawari::common::custom_ipc::CustomIpcData;
|
|
|
|
use kawari::common::custom_ipc::CustomIpcSegment;
|
|
|
|
use kawari::common::custom_ipc::CustomIpcType;
|
2025-03-22 16:47:21 -04:00
|
|
|
use kawari::config::get_config;
|
2025-03-17 17:17:19 -04:00
|
|
|
use kawari::lobby::LobbyConnection;
|
2025-03-22 17:32:00 -04:00
|
|
|
use kawari::lobby::ipc::{ClientLobbyIpcData, ServerLobbyIpcSegment};
|
2025-03-21 21:26:32 -04:00
|
|
|
use kawari::lobby::send_custom_world_packet;
|
2025-03-17 17:22:09 -04:00
|
|
|
use kawari::oodle::OodleNetwork;
|
2025-03-18 19:49:52 -04:00
|
|
|
use kawari::packet::ConnectionType;
|
2025-03-22 17:32:00 -04:00
|
|
|
use kawari::packet::{PacketState, SegmentType, send_keep_alive};
|
2025-03-15 20:36:39 -04:00
|
|
|
use tokio::io::AsyncReadExt;
|
|
|
|
use tokio::net::TcpListener;
|
2025-03-08 13:51:50 -05:00
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
|
|
|
tracing_subscriber::fmt::init();
|
|
|
|
|
2025-03-22 16:47:21 -04:00
|
|
|
let config = get_config();
|
2025-03-08 13:51:50 -05:00
|
|
|
|
2025-03-22 16:47:21 -04:00
|
|
|
let addr = config.lobby.get_socketaddr();
|
|
|
|
|
|
|
|
let listener = TcpListener::bind(addr).await.unwrap();
|
|
|
|
|
2025-03-29 20:05:20 -04:00
|
|
|
tracing::info!("Server started on {addr}");
|
2025-03-08 13:51:50 -05:00
|
|
|
|
2025-03-30 10:34:42 -04:00
|
|
|
let mut game_data = GameData::new();
|
|
|
|
let world_name = game_data.get_world_name(config.world.world_id);
|
2025-03-22 17:00:21 -04:00
|
|
|
|
2025-03-08 13:51:50 -05:00
|
|
|
loop {
|
|
|
|
let (socket, _) = listener.accept().await.unwrap();
|
|
|
|
|
2025-03-16 17:43:29 -04:00
|
|
|
let state = PacketState {
|
2025-03-09 11:07:01 -04:00
|
|
|
client_key: None,
|
2025-03-17 17:22:09 -04:00
|
|
|
clientbound_oodle: OodleNetwork::new(),
|
|
|
|
serverbound_oodle: OodleNetwork::new(),
|
2025-03-09 11:07:01 -04:00
|
|
|
};
|
2025-03-08 16:08:25 -05:00
|
|
|
|
2025-03-17 16:58:48 -04:00
|
|
|
let mut connection = LobbyConnection {
|
|
|
|
socket,
|
|
|
|
state,
|
|
|
|
session_id: None,
|
2025-03-21 19:56:16 -04:00
|
|
|
stored_character_creation_name: String::new(),
|
2025-03-22 17:32:00 -04:00
|
|
|
world_name: world_name.clone(),
|
2025-03-17 16:58:48 -04:00
|
|
|
};
|
2025-03-15 20:36:39 -04:00
|
|
|
|
2025-03-08 13:51:50 -05:00
|
|
|
tokio::spawn(async move {
|
2025-03-30 21:42:46 -04:00
|
|
|
let mut buf = vec![0; RECEIVE_BUFFER_SIZE];
|
2025-03-08 13:51:50 -05:00
|
|
|
loop {
|
2025-03-15 20:36:39 -04:00
|
|
|
let n = connection
|
|
|
|
.socket
|
|
|
|
.read(&mut buf)
|
|
|
|
.await
|
|
|
|
.expect("Failed to read data!");
|
2025-03-08 13:51:50 -05:00
|
|
|
|
2025-03-08 14:38:31 -05:00
|
|
|
if n != 0 {
|
2025-03-15 20:36:39 -04:00
|
|
|
let (segments, _) = connection.parse_packet(&buf[..n]).await;
|
2025-03-08 23:24:09 -05:00
|
|
|
for segment in &segments {
|
|
|
|
match &segment.segment_type {
|
|
|
|
SegmentType::InitializeEncryption { phrase, key } => {
|
2025-03-22 17:32:00 -04:00
|
|
|
connection.initialize_encryption(phrase, key).await
|
2025-03-08 23:24:09 -05:00
|
|
|
}
|
|
|
|
SegmentType::Ipc { data } => match &data.data {
|
2025-03-16 17:43:29 -04:00
|
|
|
ClientLobbyIpcData::ClientVersionInfo {
|
2025-03-08 23:24:09 -05:00
|
|
|
session_id,
|
|
|
|
version_info,
|
2025-03-16 17:43:29 -04:00
|
|
|
..
|
2025-03-08 23:24:09 -05:00
|
|
|
} => {
|
|
|
|
tracing::info!(
|
|
|
|
"Client {session_id} ({version_info}) logging in!"
|
|
|
|
);
|
|
|
|
|
2025-03-17 16:58:48 -04:00
|
|
|
connection.session_id = Some(session_id.clone());
|
2025-03-09 11:01:06 -04:00
|
|
|
|
2025-03-15 20:36:39 -04:00
|
|
|
connection.send_account_list().await;
|
2025-03-16 15:20:55 -04:00
|
|
|
|
|
|
|
// request an update
|
|
|
|
//connection.send_error(*sequence, 1012, 13101).await;
|
2025-03-08 23:24:09 -05:00
|
|
|
}
|
2025-03-16 17:43:29 -04:00
|
|
|
ClientLobbyIpcData::RequestCharacterList { sequence } => {
|
2025-03-22 17:32:00 -04:00
|
|
|
connection.send_lobby_info(*sequence).await
|
2025-03-08 23:24:09 -05:00
|
|
|
}
|
2025-03-16 18:22:15 -04:00
|
|
|
ClientLobbyIpcData::LobbyCharacterAction(character_action) => {
|
2025-03-22 17:32:00 -04:00
|
|
|
connection.handle_character_action(character_action).await
|
2025-03-13 22:22:02 -04:00
|
|
|
}
|
2025-03-16 17:43:29 -04:00
|
|
|
ClientLobbyIpcData::RequestEnterWorld {
|
2025-03-09 11:01:06 -04:00
|
|
|
sequence,
|
2025-03-21 19:56:16 -04:00
|
|
|
content_id,
|
2025-03-09 11:01:06 -04:00
|
|
|
} => {
|
2025-03-21 19:56:16 -04:00
|
|
|
tracing::info!("Client is joining the world with {content_id}");
|
|
|
|
|
|
|
|
let our_actor_id;
|
|
|
|
|
|
|
|
// find the actor id for this content id
|
|
|
|
// NOTE: This is NOT the ideal solution. I theorize the lobby server has it's own records with this information.
|
|
|
|
{
|
|
|
|
let ipc_segment = CustomIpcSegment {
|
|
|
|
unk1: 0,
|
|
|
|
unk2: 0,
|
|
|
|
op_code: CustomIpcType::GetActorId,
|
|
|
|
server_id: 0,
|
|
|
|
timestamp: 0,
|
|
|
|
data: CustomIpcData::GetActorId {
|
|
|
|
content_id: *content_id,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2025-03-21 21:26:32 -04:00
|
|
|
let response_segment =
|
|
|
|
send_custom_world_packet(ipc_segment).await.unwrap();
|
2025-03-21 19:56:16 -04:00
|
|
|
|
|
|
|
match &response_segment.data {
|
|
|
|
CustomIpcData::ActorIdFound { actor_id } => {
|
|
|
|
our_actor_id = *actor_id;
|
|
|
|
}
|
2025-03-21 21:26:32 -04:00
|
|
|
_ => panic!("Unexpected custom IPC packet type here!"),
|
2025-03-21 19:56:16 -04:00
|
|
|
}
|
|
|
|
}
|
2025-03-09 11:01:06 -04:00
|
|
|
|
2025-03-21 19:56:16 -04:00
|
|
|
connection
|
|
|
|
.send_enter_world(*sequence, *content_id, our_actor_id)
|
|
|
|
.await;
|
2025-03-09 11:01:06 -04:00
|
|
|
}
|
2025-03-08 23:24:09 -05:00
|
|
|
},
|
|
|
|
SegmentType::KeepAlive { id, timestamp } => {
|
2025-03-16 17:43:29 -04:00
|
|
|
send_keep_alive::<ServerLobbyIpcSegment>(
|
2025-03-15 20:36:39 -04:00
|
|
|
&mut connection.socket,
|
|
|
|
&mut connection.state,
|
2025-03-18 19:49:52 -04:00
|
|
|
ConnectionType::Lobby,
|
2025-03-15 20:36:39 -04:00
|
|
|
*id,
|
|
|
|
*timestamp,
|
|
|
|
)
|
|
|
|
.await
|
2025-03-08 23:24:09 -05:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
panic!("The server is recieving a response packet!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2025-03-08 14:38:31 -05:00
|
|
|
}
|
2025-03-08 13:51:50 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|