Support for Nested combat messages and JSPatch Notify from files #2

Merged
xeon merged 10 commits from nested-msgs into master 2024-09-14 09:05:18 +00:00
20 changed files with 32 additions and 37 deletions
Showing only changes of commit 708962becd - Show all commits

View file

@ -4,7 +4,7 @@ pub trait TomlConfig: DeserializeOwned {
const DEFAULT_TOML: &str; const DEFAULT_TOML: &str;
} }
pub fn load_or_create<'a, C>(path: &str) -> C pub fn load_or_create<C>(path: &str) -> C
where where
C: DeserializeOwned + TomlConfig, C: DeserializeOwned + TomlConfig,
{ {

View file

@ -4,7 +4,7 @@ pub fn unix_timestamp() -> u64 {
SystemTime::now() SystemTime::now()
.duration_since(UNIX_EPOCH) .duration_since(UNIX_EPOCH)
.unwrap() .unwrap()
.as_secs() as u64 .as_secs()
} }
pub fn unix_timestamp_ms() -> u64 { pub fn unix_timestamp_ms() -> u64 {

View file

@ -26,10 +26,7 @@ macro_rules! impl_from_data {
impl Component for Attribute { impl Component for Attribute {
fn set_pb_data(&self, pb: &mut shorekeeper_protocol::EntityPb) { fn set_pb_data(&self, pb: &mut shorekeeper_protocol::EntityPb) {
pb.living_status = self pb.living_status = (if self.is_alive() { LivingStatus::Alive } else { LivingStatus::Dead })
.is_alive()
.then_some(LivingStatus::Alive)
.unwrap_or(LivingStatus::Dead)
.into(); .into();
pb.component_pbs.push(EntityComponentPb { pb.component_pbs.push(EntityComponentPb {

View file

@ -59,6 +59,6 @@ macro_rules! query_components {
}), }),
)*) )*)
}) })
.unwrap_or_else(|| ($( crate::ident_as_none!($comp), )*)) .unwrap_or_else(|| ($( $crate::ident_as_none!($comp), )*))
}; };
} }

View file

@ -24,7 +24,7 @@ impl World {
pub fn create_entity(&mut self) -> EntityBuilder { pub fn create_entity(&mut self) -> EntityBuilder {
let entity = self.entity_manager.create(); let entity = self.entity_manager.create();
EntityBuilder::builder(entity, self.components.entry(entity).or_insert(Vec::new())) EntityBuilder::builder(entity, self.components.entry(entity).or_default())
} }
pub fn is_in_world(&self, entity_id: i64) -> bool { pub fn is_in_world(&self, entity_id: i64) -> bool {

View file

@ -126,8 +126,7 @@ impl Player {
.unwrap() .unwrap()
.role_id_set .role_id_set
.iter() .iter()
.map(|id| self.role_list.iter().find(|r| r.role_id == *id)) .flat_map(|id| self.role_list.iter().find(|r| r.role_id == *id))
.flatten()
.collect() .collect()
} }

View file

@ -109,8 +109,7 @@ fn logic_thread_func(receiver: mpsc::Receiver<LogicInput>, load: Arc<AtomicUsize
let mut world = world.borrow_mut(); let mut world = world.borrow_mut();
let mut players = world let mut players = world
.player_ids() .player_ids()
.map(|id| state.players.get(id).map(|pl| pl.borrow_mut())) .flat_map(|id| state.players.get(id).map(|pl| pl.borrow_mut()))
.flatten()
.collect::<Box<_>>(); .collect::<Box<_>>();
super::systems::tick_systems(&mut world, &mut players); super::systems::tick_systems(&mut world, &mut players);

View file

@ -11,8 +11,7 @@ pub fn build_scene_add_on_init_data(world: &World) -> PlayerSceneAoiData {
let mut aoi_data = PlayerSceneAoiData::default(); let mut aoi_data = PlayerSceneAoiData::default();
for entity in entities { for entity in entities {
let mut pb = EntityPb::default(); let mut pb = EntityPb { id: entity.into(), ..Default::default() };
pb.id = entity.into();
world world
.get_entity_components(entity) .get_entity_components(entity)

View file

@ -867,7 +867,7 @@ impl<Output> Kcp<Output> {
} }
let mut ts_flush = self.ts_flush; let mut ts_flush = self.ts_flush;
let mut tm_packet = u32::max_value(); let mut tm_packet = u32::MAX;
if timediff(current, ts_flush) >= 10000 || timediff(current, ts_flush) < -10000 { if timediff(current, ts_flush) >= 10000 || timediff(current, ts_flush) < -10000 {
ts_flush = current; ts_flush = current;
@ -1142,7 +1142,7 @@ impl<Output: Write> Kcp<Output> {
let resent = if self.fastresend > 0 { let resent = if self.fastresend > 0 {
self.fastresend self.fastresend
} else { } else {
u32::max_value() u32::MAX
}; };
let rtomin = if !self.nodelay { self.rx_rto >> 3 } else { 0 }; let rtomin = if !self.nodelay { self.rx_rto >> 3 } else { 0 };
@ -1381,7 +1381,7 @@ impl<Output: AsyncWrite + Unpin + Send> Kcp<Output> {
let resent = if self.fastresend > 0 { let resent = if self.fastresend > 0 {
self.fastresend self.fastresend
} else { } else {
u32::max_value() u32::MAX
}; };
let rtomin = if !self.nodelay { self.rx_rto >> 3 } else { 0 }; let rtomin = if !self.nodelay { self.rx_rto >> 3 } else { 0 };

View file

@ -140,7 +140,7 @@ async fn on_login_request(
session.player_id = Some(player_id); session.player_id = Some(player_id);
response.code = ErrorCode::Success.into(); response.code = ErrorCode::Success.into();
response.timestamp = time_util::unix_timestamp() as i64; response.timestamp = time_util::unix_timestamp_ms() as i64;
tracing::info!( tracing::info!(
"login success, user_id: {}, player_id: {}", "login success, user_id: {}, player_id: {}",

View file

@ -155,11 +155,11 @@ impl Session {
fn next_message(&mut self) -> Option<Message> { fn next_message(&mut self) -> Option<Message> {
self.decoder.pop_with(|buf| { self.decoder.pop_with(|buf| {
Message::decode(&buf) Message::decode(buf)
.inspect_err(|err| { .inspect_err(|err| {
tracing::error!( tracing::error!(
"failed to decode a message, err: {err}, buf: {}", "failed to decode a message, err: {err}, buf: {}",
hex::encode(&buf) hex::encode(buf)
) )
}) })
.ok() .ok()

View file

@ -15,8 +15,7 @@ impl LengthFieldBasedDecoder {
pub fn input(&mut self, data: &[u8]) { pub fn input(&mut self, data: &[u8]) {
self.ensure_capacity(data.len()); self.ensure_capacity(data.len());
self.buffer[self.cur_index..self.cur_index + data.len()].copy_from_slice(data);
(&mut self.buffer[self.cur_index..self.cur_index + data.len()]).copy_from_slice(data);
self.cur_index += data.len(); self.cur_index += data.len();
} }

View file

@ -64,7 +64,7 @@ impl UdpServer {
conv_id, conv_id,
addr, addr,
self.socket.clone(), self.socket.clone(),
&self.protokey_helper, self.protokey_helper,
self.db.clone(), self.db.clone(),
); );
self.session_mgr.add(conv_id, session); self.session_mgr.add(conv_id, session);

View file

@ -11,13 +11,10 @@ pub async fn handle_login_api_call(
tracing::debug!("login requested"); tracing::debug!("login requested");
let user_data = parameters.user_data; let user_data = parameters.user_data;
let result = match login(&state, parameters).await { let result = login(&state, parameters).await.unwrap_or_else(|err| {
Ok(result) => result,
Err(err) => {
tracing::warn!("login: internal error occurred {err:?}"); tracing::warn!("login: internal error occurred {err:?}");
schema::LoginResult::error(-1, String::from("Internal server error")) schema::LoginResult::error(-1, String::from("Internal server error"))
} });
};
Json(result.with_user_data(user_data)) Json(result.with_user_data(user_data))
} }
@ -32,7 +29,7 @@ async fn login(state: &ServiceState, params: schema::LoginParameters) -> Result<
Some(account) => { Some(account) => {
if let Some(ban_time_stamp) = account.ban_time_stamp { if let Some(ban_time_stamp) = account.ban_time_stamp {
if time_util::unix_timestamp() < ban_time_stamp as u64 { if time_util::unix_timestamp() < ban_time_stamp as u64 {
return Ok(schema::LoginResult::banned(String::from("You're banned MF"), ban_time_stamp as i64)); return Ok(schema::LoginResult::banned(String::from("You're banned MF"), ban_time_stamp));
} }
} }

View file

@ -24,6 +24,12 @@ pub struct Application<S> {
impl Application<()> { impl Application<()> {
pub fn new() -> Self { pub fn new() -> Self {
Default::default()
}
}
impl Default for Application<()> {
fn default() -> Self {
Self { Self {
router: Router::new(), router: Router::new(),
state: (), state: (),

View file

@ -15,7 +15,7 @@ impl ServiceMessage {
w.write_u16::<LE>(self.rpc_id)?; w.write_u16::<LE>(self.rpc_id)?;
w.write_u16::<LE>(self.message_id)?; w.write_u16::<LE>(self.message_id)?;
w.write_u32::<LE>(self.data.len() as u32)?; w.write_u32::<LE>(self.data.len() as u32)?;
w.write(&self.data)?; w.write_all(&self.data)?;
Ok(()) Ok(())
} }

View file

@ -28,8 +28,7 @@ impl ServiceListener {
for message in data for message in data
.into_vec() .into_vec()
.into_iter() .into_iter()
.map(|b| ServiceMessage::decode(b.as_ref())) .flat_map(|b| ServiceMessage::decode(b.as_ref()))
.flatten()
{ {
let _ = sender.send(message).await; let _ = sender.send(message).await;
} }

View file

@ -15,7 +15,7 @@ pub fn main() {
let config_path = Path::new("proto/config.csv"); let config_path = Path::new("proto/config.csv");
if config_path.exists() { if config_path.exists() {
println!("cargo:rerun-if-changed={config_file}"); println!("cargo:rerun-if-changed={config_file}");
impl_proto_config(config_path, &Path::new("generated/proto_config.rs")).unwrap(); impl_proto_config(config_path, Path::new("generated/proto_config.rs")).unwrap();
} }
let proto_file = "proto/shorekeeper.proto"; let proto_file = "proto/shorekeeper.proto";

View file

@ -92,7 +92,7 @@ impl Message {
let recv_crc = r.read_u32::<LE>()?; let recv_crc = r.read_u32::<LE>()?;
let mut payload = vec![0u8; src.len() - r.position() as usize].into_boxed_slice(); let mut payload = vec![0u8; src.len() - r.position() as usize].into_boxed_slice();
r.read(&mut payload)?; let _ = r.read(&mut payload)?;
let calc_crc = crc32fast::hash(&payload); let calc_crc = crc32fast::hash(&payload);

View file

@ -187,6 +187,6 @@ fn encrypt_aes256_ecb_pkcs7(
) -> Result<Box<[u8]>, InvalidLength> { ) -> Result<Box<[u8]>, InvalidLength> {
let cipher = Aes256::new_from_slice(session_key)?; let cipher = Aes256::new_from_slice(session_key)?;
Ok(cipher Ok(cipher
.encrypt_padded_vec::<Pkcs7>(&data[..]) .encrypt_padded_vec::<Pkcs7>(data)
.into_boxed_slice()) .into_boxed_slice())
} }