Skip to content

Commit

Permalink
Support packing ints into 0.6 packets
Browse files Browse the repository at this point in the history
  • Loading branch information
ChillerDragon committed Dec 19, 2024
1 parent 7453b29 commit 6f571b3
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 16 deletions.
33 changes: 33 additions & 0 deletions src/multi_protocol.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
; abstraction layer for supporting both 0.6 and 0.7 at the same time
; basically a bunch of getters that give the right values
; depending on the current version

get_recv_buf:
; returns into rax either `udp_recv_buf` or `udp_recv6_buf`
; depending on the current connection version
mov al, byte [connection_version]
cmp al, 7
je .version7
.version6:
mov rax, udp_recv6_buf
jmp .end
.version7:
mov rax, udp_recv_buf
.end:
ret

get_packet_header_len:
; returns into rax the packet header length
; which is 3 for 0.6 and 7 for 0.7
;
push r8
mov r8d, PACKET_HEADER_LEN
mov al, byte [connection_version]
cmp al, 7
je .version7
mov r8d, PACKET6_HEADER_LEN
.version7:
._: ; clear label for debugger
mov rax, r8
pop r8
ret
11 changes: 9 additions & 2 deletions src/packet.asm
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,15 @@
mov rax, 0
mov eax, r9d

; r8 is packet header len depending on connection version
; do not overwrite this anywhere in this function!
push rax
call get_packet_header_len
mov r8d, eax
pop rax

mov dword edx, [udp_payload_index]
lea rdi, [udp_send_buf + PACKET_HEADER_LEN + edx]
lea rdi, [udp_send_buf + r8d + edx]

; _pack_int [rax] [rdi]
; rax = integer
Expand All @@ -136,7 +143,7 @@

; write start
mov dword edx, [udp_payload_index]
lea rdi, [udp_send_buf + PACKET_HEADER_LEN + edx]
lea rdi, [udp_send_buf + r8d + edx]

; rax is the returned write end pointer
; calculate amount of bytes written
Expand Down
15 changes: 1 addition & 14 deletions src/teeworlds_asmr.asm
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ section .text
%include "src/send_game.asm"
%include "src/receive_control.asm"
%include "src/receive_control6.asm"
%include "src/multi_protocol.asm"
%include "src/system.asm"
%include "src/string.asm"
%include "src/packet_header.asm"
Expand All @@ -159,20 +160,6 @@ section .text
%include "src/client_info.asm"
%include "src/on_chat.asm"

get_recv_buf:
; returns into rax either `udp_recv_buf` or `udp_recv6_buf`
; depending on the current connection version
mov al, byte [connection_version]
cmp al, 7
je .version7
.version6:
mov rax, udp_recv6_buf
jmp .end
.version7:
mov rax, udp_recv_buf
.end:
ret

print_udp:
print_label s_got_udp
; hexdump
Expand Down
1 change: 1 addition & 0 deletions tests/assert.asm
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ section .text
%include "src/send_game.asm"
%include "src/receive_control.asm"
%include "src/receive_control6.asm"
%include "src/multi_protocol.asm"
%include "src/system.asm"
%include "src/string.asm"
%include "src/packet_header.asm"
Expand Down

0 comments on commit 6f571b3

Please sign in to comment.