From 0280fb58e1c2ae4194543f318c9b88584a20039c Mon Sep 17 00:00:00 2001 From: mat Date: Tue, 21 Jun 2022 22:10:33 -0500 Subject: add serverbound move packets --- codegen/lib/code/packet.py | 64 +++++++++++++++++++++++++++++++--------------- 1 file changed, 43 insertions(+), 21 deletions(-) (limited to 'codegen/lib') diff --git a/codegen/lib/code/packet.py b/codegen/lib/code/packet.py index 2aabf39a..c282bdb4 100644 --- a/codegen/lib/code/packet.py +++ b/codegen/lib/code/packet.py @@ -1,7 +1,9 @@ +from typing import Optional from lib.code.utils import burger_type_to_rust_type, write_packet_file from lib.utils import padded_hex, to_snake_case, to_camel_case, get_dir_location from lib.mappings import Mappings import os +import re def make_packet_mod_rs_line(packet_id: int, packet_class_name: str): @@ -29,34 +31,19 @@ def generate_packet(burger_packets, mappings: Mappings, target_packet_id, target f'#[derive(Clone, Debug, McBuf, {to_camel_case(state)}Packet)]') uses.add(f'packet_macros::{{{to_camel_case(state)}Packet, McBuf}}') - obfuscated_class_name = packet['class'].split('.')[0].split('$')[0] + obfuscated_class_name = packet['class'].split('.')[0] class_name = mappings.get_class( - obfuscated_class_name).split('.')[-1].split('$')[0] + obfuscated_class_name).split('.')[-1] + if '$' in class_name: + class_name = class_name.replace('$', '') generated_packet_code.append( f'pub struct {to_camel_case(class_name)} {{') for instruction in packet.get('instructions', []): if instruction['operation'] == 'write': - obfuscated_field_name = instruction['field'] - if '.' in obfuscated_field_name or ' ' in obfuscated_field_name or '(' in obfuscated_field_name: - generated_packet_code.append(f'// TODO: {instruction}') - continue - field_name = mappings.get_field( - obfuscated_class_name, obfuscated_field_name) - if not field_name: - generated_packet_code.append( - f'// TODO: unknown field {instruction}') - continue - - field_type = instruction['type'] - field_type_rs, is_var, instruction_uses = burger_type_to_rust_type( - field_type) - if is_var: - generated_packet_code.append('#[var]') - generated_packet_code.append( - f'pub {to_snake_case(field_name)}: {field_type_rs},') - uses.update(instruction_uses) + burger_instruction_to_code( + instruction, generated_packet_code, mappings, obfuscated_class_name, uses) else: generated_packet_code.append(f'// TODO: {instruction}') continue @@ -213,6 +200,41 @@ def get_packets(direction: str, state: str): return packet_ids, packet_class_names +def burger_instruction_to_code(instruction: dict, generated_packet_code: list[str], mappings: Mappings, obfuscated_class_name: str, uses: set): + field_type = instruction['type'] + field_type_rs, is_var, instruction_uses = burger_type_to_rust_type( + field_type) + + obfuscated_field_name = instruction['field'] + if '.' in obfuscated_field_name or ' ' in obfuscated_field_name or '(' in obfuscated_field_name: + field_type_rs, obfuscated_field_name = burger_field_to_type( + obfuscated_field_name) + if not field_type_rs: + generated_packet_code.append(f'// TODO: {instruction}') + return + field_name = mappings.get_field( + obfuscated_class_name, obfuscated_field_name) or mappings.get_field( + obfuscated_class_name.split('$')[0], obfuscated_field_name) + if not field_name: + generated_packet_code.append( + f'// TODO: unknown field {instruction}') + return + + if is_var: + generated_packet_code.append('#[var]') + generated_packet_code.append( + f'pub {to_snake_case(field_name)}: {field_type_rs},') + uses.update(instruction_uses) + + +def burger_field_to_type(field) -> tuple[Optional[str], str]: + # match `(x) ? 1 : 0` + match = re.match(r'\((.*)\) \? 1 : 0', field) + if match: + return ('bool', match.group(1)) + return None, field + + def change_packet_ids(id_map: dict[int, int], direction: str, state: str): existing_packet_ids, existing_packet_class_names = get_packets( direction, state) -- cgit v1.2.3 From ce834aeca5d732a547b66554590232321e3d1829 Mon Sep 17 00:00:00 2001 From: mat Date: Thu, 23 Jun 2022 23:45:23 -0500 Subject: Fixes --- azalea-client/src/movement.rs | 2 +- azalea-crypto/src/lib.rs | 2 -- azalea-protocol/src/packets/game/mod.rs | 2 +- codegen/lib/code/packet.py | 3 ++- 4 files changed, 4 insertions(+), 5 deletions(-) (limited to 'codegen/lib') diff --git a/azalea-client/src/movement.rs b/azalea-client/src/movement.rs index 9f5cd27c..c9cd62e9 100644 --- a/azalea-client/src/movement.rs +++ b/azalea-client/src/movement.rs @@ -1,5 +1,5 @@ -use azalea_core::EntityPos; use crate::Client; +use azalea_core::EntityPos; use azalea_protocol::packets::game::serverbound_move_player_packet_pos_rot::ServerboundMovePlayerPacketPosRot; impl Client { diff --git a/azalea-crypto/src/lib.rs b/azalea-crypto/src/lib.rs index 85705883..a5e797e8 100644 --- a/azalea-crypto/src/lib.rs +++ b/azalea-crypto/src/lib.rs @@ -79,8 +79,6 @@ pub fn decrypt_packet(cipher: &mut Aes128CfbDec, packet: &mut [u8]) { cipher.decrypt_blocks_inout_mut(chunks); } - - #[cfg(test)] mod tests { use super::*; diff --git a/azalea-protocol/src/packets/game/mod.rs b/azalea-protocol/src/packets/game/mod.rs index 80cada9b..d3d6650c 100755 --- a/azalea-protocol/src/packets/game/mod.rs +++ b/azalea-protocol/src/packets/game/mod.rs @@ -105,13 +105,13 @@ declare_state_packets!( 0x3c: clientbound_rotate_head_packet::ClientboundRotateHeadPacket, 0x3d: clientbound_section_blocks_update_packet::ClientboundSectionBlocksUpdatePacket, 0x3f: clientbound_server_data_packet::ClientboundServerDataPacket, - 0x44: clientbound_set_entity_link_packet::ClientboundSetEntityLinkPacket, 0x47: clientbound_set_carried_item_packet::ClientboundSetCarriedItemPacket, 0x48: clientbound_set_chunk_cache_center_packet::ClientboundSetChunkCacheCenterPacket, 0x49: clientbound_update_view_distance_packet::ClientboundUpdateViewDistancePacket, 0x4a: clientbound_set_default_spawn_position_packet::ClientboundSetDefaultSpawnPositionPacket, 0x4b: clientbound_set_display_chat_preview_packet::ClientboundSetDisplayChatPreviewPacket, 0x4d: clientbound_set_entity_data_packet::ClientboundSetEntityDataPacket, + 0x4e: clientbound_set_entity_link_packet::ClientboundSetEntityLinkPacket, 0x4f: clientbound_entity_velocity_packet::ClientboundEntityVelocityPacket, 0x50: clientbound_set_equipment_packet::ClientboundSetEquipmentPacket, 0x51: clientbound_set_experience_packet::ClientboundSetExperiencePacket, diff --git a/codegen/lib/code/packet.py b/codegen/lib/code/packet.py index c282bdb4..b4ca8be7 100644 --- a/codegen/lib/code/packet.py +++ b/codegen/lib/code/packet.py @@ -29,7 +29,8 @@ def generate_packet(burger_packets, mappings: Mappings, target_packet_id, target uses = set() generated_packet_code.append( f'#[derive(Clone, Debug, McBuf, {to_camel_case(state)}Packet)]') - uses.add(f'packet_macros::{{{to_camel_case(state)}Packet, McBuf}}') + uses.add(f'packet_macros::{to_camel_case(state)}Packet') + uses.add(f'packet_buf::McBuf') obfuscated_class_name = packet['class'].split('.')[0] class_name = mappings.get_class( -- cgit v1.2.3 From 41f61bf9c11ab58af4c1bd97c2cb40110b272449 Mon Sep 17 00:00:00 2001 From: mat Date: Fri, 24 Jun 2022 23:54:31 -0500 Subject: i hate mutexes --- azalea-client/src/client.rs | 161 ++++++++++----------- azalea-client/src/movement.rs | 5 + azalea-entity/src/lib.rs | 4 +- .../game/clientbound_player_position_packet.rs | 2 +- azalea-protocol/src/packets/game/mod.rs | 2 + .../serverbound_accept_teleportation_packet.rs | 8 + codegen/lib/code/packet.py | 2 +- 7 files changed, 97 insertions(+), 87 deletions(-) create mode 100644 azalea-protocol/src/packets/game/serverbound_accept_teleportation_packet.rs (limited to 'codegen/lib') diff --git a/azalea-client/src/client.rs b/azalea-client/src/client.rs index 943e0f9f..364abef6 100644 --- a/azalea-client/src/client.rs +++ b/azalea-client/src/client.rs @@ -8,8 +8,10 @@ use azalea_protocol::{ game::{ clientbound_player_chat_packet::ClientboundPlayerChatPacket, clientbound_system_chat_packet::ClientboundSystemChatPacket, + serverbound_accept_teleportation_packet::ServerboundAcceptTeleportationPacket, serverbound_custom_payload_packet::ServerboundCustomPayloadPacket, - serverbound_keep_alive_packet::ServerboundKeepAlivePacket, GamePacket, + serverbound_keep_alive_packet::ServerboundKeepAlivePacket, + serverbound_move_player_packet_pos_rot::ServerboundMovePlayerPacketPosRot, GamePacket, }, handshake::client_intention_packet::ClientIntentionPacket, login::{ @@ -166,6 +168,9 @@ impl Client { let game_loop_state = client.state.clone(); + // if you get an error right here that means you're doing something with locks wrong + // read the error to see where the issue is + // you might be able to just drop the lock or put it in its own scope to fix tokio::spawn(Self::protocol_loop( conn.clone(), tx.clone(), @@ -352,94 +357,84 @@ impl Client { // TODO: reply with teleport confirm println!("Got player position packet {:?}", p); - let mut state_lock = state.lock()?; - let player_entity_id = state_lock.player.entity_id; - let world = state_lock.world.as_mut().unwrap(); - let player_entity = world - .mut_entity_by_id(player_entity_id) - .expect("Player entity doesn't exist"); - let delta_movement = &player_entity.delta; - - let is_x_relative = p.relative_arguments.x; - let is_y_relative = p.relative_arguments.y; - let is_z_relative = p.relative_arguments.z; - - let (delta_x, new_pos_x) = if is_x_relative { - player_entity.old_pos.x += p.x; - (delta_movement.x(), player_entity.pos().x + p.x) - } else { - player_entity.old_pos.x = p.x; - (0.0, p.x) - }; - let (delta_y, new_pos_y) = if is_y_relative { - player_entity.old_pos.y += p.y; - (delta_movement.y(), player_entity.pos().y + p.y) - } else { - player_entity.old_pos.y = p.y; - (0.0, p.y) - }; - let (delta_z, new_pos_z) = if is_z_relative { - player_entity.old_pos.z += p.z; - (delta_movement.z(), player_entity.pos().z + p.z) - } else { - player_entity.old_pos.z = p.z; - (0.0, p.z) - }; - - let mut y_rot = p.y_rot; - let mut x_rot = p.x_rot; - if p.relative_arguments.x_rot { - y_rot += player_entity.x_rot; - } - if p.relative_arguments.y_rot { - x_rot += player_entity.y_rot; - } + let (new_pos, y_rot, x_rot) = { + let mut state_lock = state.lock()?; + let player_entity_id = state_lock.player.entity_id; + let world = state_lock.world.as_mut().unwrap(); + let player_entity = world + .mut_entity_by_id(player_entity_id) + .expect("Player entity doesn't exist"); + let delta_movement = &player_entity.delta; + + let is_x_relative = p.relative_arguments.x; + let is_y_relative = p.relative_arguments.y; + let is_z_relative = p.relative_arguments.z; + + let (delta_x, new_pos_x) = if is_x_relative { + player_entity.old_pos.x += p.x; + (delta_movement.x(), player_entity.pos().x + p.x) + } else { + player_entity.old_pos.x = p.x; + (0.0, p.x) + }; + let (delta_y, new_pos_y) = if is_y_relative { + player_entity.old_pos.y += p.y; + (delta_movement.y(), player_entity.pos().y + p.y) + } else { + player_entity.old_pos.y = p.y; + (0.0, p.y) + }; + let (delta_z, new_pos_z) = if is_z_relative { + player_entity.old_pos.z += p.z; + (delta_movement.z(), player_entity.pos().z + p.z) + } else { + player_entity.old_pos.z = p.z; + (0.0, p.z) + }; + + let mut y_rot = p.y_rot; + let mut x_rot = p.x_rot; + if p.relative_arguments.x_rot { + y_rot += player_entity.x_rot; + } + if p.relative_arguments.y_rot { + x_rot += player_entity.y_rot; + } - player_entity.delta = PositionDelta { - xa: delta_x, - ya: delta_y, - za: delta_z, + player_entity.delta = PositionDelta { + xa: delta_x, + ya: delta_y, + za: delta_z, + }; + player_entity.set_rotation(y_rot, x_rot); + // TODO: minecraft sets "xo", "yo", and "zo" here but idk what that means + // so investigate that ig + let new_pos = EntityPos { + x: new_pos_x, + y: new_pos_y, + z: new_pos_z, + }; + world + .move_entity(player_entity_id, new_pos) + .expect("The player entity should always exist"); + + (new_pos, y_rot, x_rot) }; - player_entity.set_rotation(x_rot, y_rot); - // TODO: minecraft sets "xo", "yo", and "zo" here but idk what that means - // so investigate that ig - world - .move_entity( - player_entity_id, - EntityPos { - x: new_pos_x, - y: new_pos_y, - z: new_pos_z, - }, - ) - .expect("The player entity should always exist"); - - let mut state_lock = state.lock()?; - let player = &state_lock.player; - let player_entity_id = player.entity_id; - - let world = state_lock.world.as_mut().unwrap(); - world.move_entity( - player_entity_id, - EntityPos { - x: p.x, - y: p.y, - z: p.z, - }, - )?; - - conn.lock() - .await - .write(ServerboundAcceptTeleportationPacket {}.get()) + let mut conn_lock = conn.lock().await; + conn_lock + .write(ServerboundAcceptTeleportationPacket { id: p.id }.get()) .await; - conn.lock() - .await + conn_lock .write( ServerboundMovePlayerPacketPosRot { - identifier: ResourceLocation::new("brand").unwrap(), - // they don't have to know :) - data: "vanilla".into(), + x: new_pos.x, + y: new_pos.y, + z: new_pos.z, + y_rot, + x_rot, + // this is always false + on_ground: false, } .get(), ) diff --git a/azalea-client/src/movement.rs b/azalea-client/src/movement.rs index 0402b15b..f74d48df 100644 --- a/azalea-client/src/movement.rs +++ b/azalea-client/src/movement.rs @@ -5,7 +5,9 @@ use azalea_protocol::packets::game::serverbound_move_player_packet_pos_rot::Serv impl Client { /// Set the client's position to the given coordinates. pub async fn move_to(&mut self, new_pos: EntityPos) -> Result<(), String> { + println!("obtaining lock on state"); let mut state_lock = self.state.lock().unwrap(); + println!("obtained lock on state"); let world = state_lock.world.as_ref().unwrap(); @@ -18,7 +20,9 @@ impl Client { let world = state_lock.world.as_mut().unwrap(); world.move_entity(player_id, new_pos)?; + drop(state_lock); + println!("obtaining lock on conn"); self.conn .lock() .await @@ -34,6 +38,7 @@ impl Client { .get(), ) .await; + println!("obtained lock on conn"); Ok(()) } diff --git a/azalea-entity/src/lib.rs b/azalea-entity/src/lib.rs index 63c717d3..9436d753 100644 --- a/azalea-entity/src/lib.rs +++ b/azalea-entity/src/lib.rs @@ -42,9 +42,9 @@ impl Entity { self.pos = new_pos; } - pub fn set_rotation(&mut self, x_rot: f32, y_rot: f32) { - self.x_rot = x_rot % 360.0; + pub fn set_rotation(&mut self, y_rot: f32, x_rot: f32) { self.y_rot = y_rot.clamp(-90.0, 90.0) % 360.0; + self.x_rot = x_rot % 360.0; // TODO: minecraft also sets yRotO and xRotO to xRot and yRot ... but idk what they're used for so } } diff --git a/azalea-protocol/src/packets/game/clientbound_player_position_packet.rs b/azalea-protocol/src/packets/game/clientbound_player_position_packet.rs index 2c5504fa..29f7c1a3 100644 --- a/azalea-protocol/src/packets/game/clientbound_player_position_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_player_position_packet.rs @@ -14,7 +14,7 @@ pub struct ClientboundPlayerPositionPacket { /// Client should confirm this packet with Teleport Confirm containing the /// same Teleport ID. #[var] - pub id: i32, + pub id: u32, pub dismount_vehicle: bool, } diff --git a/azalea-protocol/src/packets/game/mod.rs b/azalea-protocol/src/packets/game/mod.rs index d3d6650c..3e492dec 100755 --- a/azalea-protocol/src/packets/game/mod.rs +++ b/azalea-protocol/src/packets/game/mod.rs @@ -49,6 +49,7 @@ pub mod clientbound_update_attributes_packet; pub mod clientbound_update_recipes_packet; pub mod clientbound_update_tags_packet; pub mod clientbound_update_view_distance_packet; +pub mod serverbound_accept_teleportation_packet; pub mod serverbound_chat_command_packet; pub mod serverbound_chat_preview_packet; pub mod serverbound_custom_payload_packet; @@ -63,6 +64,7 @@ use packet_macros::declare_state_packets; declare_state_packets!( GamePacket, Serverbound => { + 0x00: serverbound_accept_teleportation_packet::ServerboundAcceptTeleportationPacket, 0x03: serverbound_chat_command_packet::ServerboundChatCommandPacket, 0x05: serverbound_chat_preview_packet::ServerboundChatPreviewPacket, 0x0c: serverbound_custom_payload_packet::ServerboundCustomPayloadPacket, diff --git a/azalea-protocol/src/packets/game/serverbound_accept_teleportation_packet.rs b/azalea-protocol/src/packets/game/serverbound_accept_teleportation_packet.rs new file mode 100644 index 00000000..98a9f728 --- /dev/null +++ b/azalea-protocol/src/packets/game/serverbound_accept_teleportation_packet.rs @@ -0,0 +1,8 @@ +use azalea_buf::McBuf; +use packet_macros::GamePacket; + +#[derive(Clone, Debug, McBuf, GamePacket)] +pub struct ServerboundAcceptTeleportationPacket { + #[var] + pub id: u32, +} diff --git a/codegen/lib/code/packet.py b/codegen/lib/code/packet.py index b4ca8be7..de8254e7 100644 --- a/codegen/lib/code/packet.py +++ b/codegen/lib/code/packet.py @@ -30,7 +30,7 @@ def generate_packet(burger_packets, mappings: Mappings, target_packet_id, target generated_packet_code.append( f'#[derive(Clone, Debug, McBuf, {to_camel_case(state)}Packet)]') uses.add(f'packet_macros::{to_camel_case(state)}Packet') - uses.add(f'packet_buf::McBuf') + uses.add(f'azalea_buf::McBuf') obfuscated_class_name = packet['class'].split('.')[0] class_name = mappings.get_class( -- cgit v1.2.3