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-17 17:17:19 -04:00
|
|
|
use kawari::lobby::LobbyConnection;
|
2025-03-16 17:43:29 -04:00
|
|
|
use kawari::lobby::ipc::{
|
2025-03-16 18:22:15 -04:00
|
|
|
CharacterDetails, ClientLobbyIpcData, LobbyCharacterActionKind, ServerLobbyIpcData,
|
2025-03-16 17:43:29 -04:00
|
|
|
ServerLobbyIpcSegment, ServerLobbyIpcType,
|
|
|
|
};
|
2025-03-17 17:22:09 -04:00
|
|
|
use kawari::oodle::OodleNetwork;
|
2025-03-21 19:56:16 -04:00
|
|
|
use kawari::packet::CompressionType;
|
2025-03-18 19:49:52 -04:00
|
|
|
use kawari::packet::ConnectionType;
|
2025-03-21 19:56:16 -04:00
|
|
|
use kawari::packet::parse_packet;
|
|
|
|
use kawari::packet::send_packet;
|
2025-03-16 17:43:29 -04:00
|
|
|
use kawari::packet::{PacketSegment, PacketState, SegmentType, send_keep_alive};
|
2025-03-16 18:23:04 -04:00
|
|
|
use kawari::{CONTENT_ID, WORLD_NAME};
|
2025-03-15 20:36:39 -04:00
|
|
|
use tokio::io::AsyncReadExt;
|
|
|
|
use tokio::net::TcpListener;
|
2025-03-21 19:56:16 -04:00
|
|
|
use tokio::net::TcpStream;
|
|
|
|
|
|
|
|
/// Sends a custom IPC packet to the world server, meant for private server-to-server communication.
|
|
|
|
/// Returns the first custom IPC segment returned.
|
|
|
|
async fn send_custom_world_packet(segment: CustomIpcSegment) -> Option<CustomIpcSegment> {
|
|
|
|
let mut stream = TcpStream::connect("127.0.0.1:7100").await.unwrap();
|
|
|
|
|
|
|
|
let mut packet_state = PacketState {
|
|
|
|
client_key: None,
|
|
|
|
serverbound_oodle: OodleNetwork::new(),
|
|
|
|
clientbound_oodle: OodleNetwork::new(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let segment: PacketSegment<CustomIpcSegment> = PacketSegment {
|
|
|
|
source_actor: 0,
|
|
|
|
target_actor: 0,
|
|
|
|
segment_type: SegmentType::CustomIpc { data: segment },
|
|
|
|
};
|
|
|
|
|
|
|
|
send_packet(
|
|
|
|
&mut stream,
|
|
|
|
&mut packet_state,
|
|
|
|
ConnectionType::None,
|
|
|
|
CompressionType::Uncompressed,
|
|
|
|
&[segment],
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let mut buf = [0; 2056];
|
|
|
|
let n = stream.read(&mut buf).await.expect("Failed to read data!");
|
|
|
|
|
|
|
|
println!("Got {n} bytes of response!");
|
|
|
|
|
|
|
|
let (segments, _) = parse_packet::<CustomIpcSegment>(&buf[..n], &mut packet_state).await;
|
|
|
|
|
|
|
|
match &segments[0].segment_type {
|
|
|
|
SegmentType::CustomIpc { data } => Some(data.clone()),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2025-03-08 13:51:50 -05:00
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
|
|
|
tracing_subscriber::fmt::init();
|
|
|
|
|
|
|
|
let listener = TcpListener::bind("127.0.0.1:7000").await.unwrap();
|
|
|
|
|
2025-03-16 14:51:18 -04:00
|
|
|
tracing::info!("Lobby server started on 127.0.0.1:7000");
|
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-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 {
|
|
|
|
let mut buf = [0; 2056];
|
|
|
|
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-11 21:52:46 -04:00
|
|
|
tracing::info!("read {} bytes", n);
|
|
|
|
|
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-15 20:36:39 -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-08 23:24:09 -05:00
|
|
|
tracing::info!("Client is requesting character list...");
|
|
|
|
|
2025-03-15 20:36:39 -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) => {
|
|
|
|
match &character_action.action {
|
|
|
|
LobbyCharacterActionKind::ReserveName => {
|
2025-03-13 22:22:02 -04:00
|
|
|
tracing::info!(
|
2025-03-16 18:22:15 -04:00
|
|
|
"Player is requesting {} as a new character name!",
|
|
|
|
character_action.name
|
2025-03-13 22:22:02 -04:00
|
|
|
);
|
|
|
|
|
2025-03-21 19:56:16 -04:00
|
|
|
// check with the world server if the name is available
|
|
|
|
let name_request = CustomIpcSegment {
|
|
|
|
unk1: 0,
|
|
|
|
unk2: 0,
|
|
|
|
op_code: CustomIpcType::CheckNameIsAvailable,
|
|
|
|
server_id: 0,
|
|
|
|
timestamp: 0,
|
|
|
|
data: CustomIpcData::CheckNameIsAvailable {
|
|
|
|
name: character_action.name.clone(),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
let name_response =
|
|
|
|
send_custom_world_packet(name_request)
|
|
|
|
.await
|
|
|
|
.expect("Failed to get name request packet!");
|
|
|
|
let CustomIpcData::NameIsAvailableResponse { free } =
|
|
|
|
&name_response.data
|
|
|
|
else {
|
|
|
|
panic!("Unexpedted custom IPC type!")
|
|
|
|
};
|
|
|
|
|
|
|
|
tracing::info!("Is name free? {free}");
|
|
|
|
|
|
|
|
// TODO: use read_bool_as
|
|
|
|
let free: bool = *free == 1u8;
|
|
|
|
|
|
|
|
if free {
|
|
|
|
connection.stored_character_creation_name =
|
|
|
|
character_action.name.clone();
|
2025-03-13 22:22:02 -04:00
|
|
|
|
2025-03-16 17:43:29 -04:00
|
|
|
let ipc = ServerLobbyIpcSegment {
|
2025-03-13 23:30:48 -04:00
|
|
|
unk1: 0,
|
|
|
|
unk2: 0,
|
2025-03-16 17:43:29 -04:00
|
|
|
op_code: ServerLobbyIpcType::CharacterCreated,
|
2025-03-13 23:30:48 -04:00
|
|
|
server_id: 0,
|
|
|
|
timestamp: 0,
|
2025-03-16 17:43:29 -04:00
|
|
|
data: ServerLobbyIpcData::CharacterCreated {
|
2025-03-16 18:22:15 -04:00
|
|
|
sequence: character_action.sequence + 1,
|
2025-03-16 18:15:19 -04:00
|
|
|
unk: 0x00010101,
|
2025-03-13 23:30:48 -04:00
|
|
|
details: CharacterDetails {
|
|
|
|
content_id: CONTENT_ID,
|
2025-03-16 18:22:15 -04:00
|
|
|
character_name: character_action
|
|
|
|
.name
|
|
|
|
.clone(),
|
2025-03-16 18:23:04 -04:00
|
|
|
origin_server_name: WORLD_NAME
|
2025-03-13 23:30:48 -04:00
|
|
|
.to_string(),
|
2025-03-16 18:23:04 -04:00
|
|
|
current_server_name: WORLD_NAME
|
2025-03-13 23:30:48 -04:00
|
|
|
.to_string(),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2025-03-15 20:36:39 -04:00
|
|
|
connection
|
|
|
|
.send_segment(PacketSegment {
|
|
|
|
source_actor: 0x0,
|
|
|
|
target_actor: 0x0,
|
|
|
|
segment_type: SegmentType::Ipc {
|
|
|
|
data: ipc,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.await;
|
2025-03-21 19:56:16 -04:00
|
|
|
} else {
|
|
|
|
let ipc = ServerLobbyIpcSegment {
|
|
|
|
unk1: 0,
|
|
|
|
unk2: 0,
|
|
|
|
op_code: ServerLobbyIpcType::LobbyError,
|
|
|
|
server_id: 0,
|
|
|
|
timestamp: 0,
|
|
|
|
data: ServerLobbyIpcData::LobbyError {
|
|
|
|
sequence: 0x03,
|
|
|
|
error: 0x0bdb,
|
|
|
|
exd_error_id: 0,
|
|
|
|
value: 0,
|
|
|
|
unk1: 0,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
let response_packet = PacketSegment {
|
|
|
|
source_actor: 0x0,
|
|
|
|
target_actor: 0x0,
|
|
|
|
segment_type: SegmentType::Ipc { data: ipc },
|
|
|
|
};
|
|
|
|
connection.send_segment(response_packet).await;
|
2025-03-13 23:30:48 -04:00
|
|
|
}
|
|
|
|
}
|
2025-03-16 18:22:15 -04:00
|
|
|
LobbyCharacterActionKind::Create => {
|
2025-03-13 23:30:48 -04:00
|
|
|
tracing::info!("Player is creating a new character!");
|
|
|
|
|
2025-03-21 19:56:16 -04:00
|
|
|
let our_actor_id;
|
|
|
|
let our_content_id;
|
|
|
|
|
|
|
|
// tell the world server to create this character
|
|
|
|
{
|
|
|
|
let ipc_segment = CustomIpcSegment {
|
|
|
|
unk1: 0,
|
|
|
|
unk2: 0,
|
|
|
|
op_code: CustomIpcType::RequestCreateCharacter,
|
|
|
|
server_id: 0,
|
|
|
|
timestamp: 0,
|
|
|
|
data: CustomIpcData::RequestCreateCharacter {
|
|
|
|
name: connection
|
|
|
|
.stored_character_creation_name
|
|
|
|
.clone(), // TODO: worth double-checking, but AFAIK we have to store it this way?
|
|
|
|
chara_make_json: character_action
|
|
|
|
.json
|
|
|
|
.clone(),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
let response_segment =
|
|
|
|
send_custom_world_packet(ipc_segment)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
match &response_segment.data {
|
|
|
|
CustomIpcData::CharacterCreated {
|
|
|
|
actor_id,
|
|
|
|
content_id,
|
|
|
|
} => {
|
|
|
|
our_actor_id = *actor_id;
|
|
|
|
our_content_id = *content_id;
|
|
|
|
}
|
|
|
|
_ => panic!(
|
|
|
|
"Unexpected custom IPC packet type here!"
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tracing::info!(
|
|
|
|
"Got new player info from world server: {our_content_id} {our_actor_id}"
|
|
|
|
);
|
2025-03-13 23:30:48 -04:00
|
|
|
|
|
|
|
// a slightly different character created packet now
|
|
|
|
{
|
2025-03-16 17:43:29 -04:00
|
|
|
let ipc = ServerLobbyIpcSegment {
|
2025-03-13 23:30:48 -04:00
|
|
|
unk1: 0,
|
|
|
|
unk2: 0,
|
2025-03-16 17:43:29 -04:00
|
|
|
op_code: ServerLobbyIpcType::CharacterCreated,
|
2025-03-13 23:30:48 -04:00
|
|
|
server_id: 0,
|
|
|
|
timestamp: 0,
|
2025-03-16 17:43:29 -04:00
|
|
|
data: ServerLobbyIpcData::CharacterCreated {
|
2025-03-16 18:22:15 -04:00
|
|
|
sequence: character_action.sequence + 1,
|
2025-03-16 18:15:19 -04:00
|
|
|
unk: 0x00020101,
|
2025-03-13 23:30:48 -04:00
|
|
|
details: CharacterDetails {
|
2025-03-21 19:56:16 -04:00
|
|
|
actor_id: our_actor_id,
|
|
|
|
content_id: our_content_id,
|
2025-03-16 18:22:15 -04:00
|
|
|
character_name: character_action
|
|
|
|
.name
|
|
|
|
.clone(),
|
2025-03-16 18:23:04 -04:00
|
|
|
origin_server_name: WORLD_NAME
|
2025-03-13 23:30:48 -04:00
|
|
|
.to_string(),
|
2025-03-16 18:23:04 -04:00
|
|
|
current_server_name: WORLD_NAME
|
2025-03-13 23:30:48 -04:00
|
|
|
.to_string(),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2025-03-15 20:36:39 -04:00
|
|
|
connection
|
|
|
|
.send_segment(PacketSegment {
|
|
|
|
source_actor: 0x0,
|
|
|
|
target_actor: 0x0,
|
|
|
|
segment_type: SegmentType::Ipc {
|
|
|
|
data: ipc,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.await;
|
2025-03-13 22:22:02 -04:00
|
|
|
}
|
|
|
|
}
|
2025-03-16 18:22:15 -04:00
|
|
|
LobbyCharacterActionKind::Rename => todo!(),
|
|
|
|
LobbyCharacterActionKind::Delete => todo!(),
|
|
|
|
LobbyCharacterActionKind::Move => todo!(),
|
|
|
|
LobbyCharacterActionKind::RemakeRetainer => todo!(),
|
|
|
|
LobbyCharacterActionKind::RemakeChara => todo!(),
|
|
|
|
LobbyCharacterActionKind::SettingsUploadBegin => todo!(),
|
|
|
|
LobbyCharacterActionKind::SettingsUpload => todo!(),
|
|
|
|
LobbyCharacterActionKind::WorldVisit => todo!(),
|
|
|
|
LobbyCharacterActionKind::DataCenterToken => todo!(),
|
|
|
|
LobbyCharacterActionKind::Request => todo!(),
|
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,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
let response_segment = send_custom_world_packet(ipc_segment).await.unwrap();
|
|
|
|
|
|
|
|
match &response_segment.data {
|
|
|
|
CustomIpcData::ActorIdFound { actor_id } => {
|
|
|
|
our_actor_id = *actor_id;
|
|
|
|
}
|
|
|
|
_ => panic!(
|
|
|
|
"Unexpected custom IPC packet type here!"
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|