Skip to content

Commit

Permalink
Print public chat messages
Browse files Browse the repository at this point in the history
  • Loading branch information
ChillerDragon committed Aug 25, 2024
1 parent 5f418d7 commit df35642
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/bss/teeworlds_state.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
; client struct array to store player information
tw_clients resb TW_CLIENT_SIZE * MAX_CLIENTS


29 changes: 29 additions & 0 deletions src/client_info.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
; TW_CLIENT_ID_OFFSET equ 0
; TW_CLIENT_NAME_OFFSET equ TW_CLIENT_ID_OFFSET + 4
; TW_CLIENT_CLAN_OFFSET equ TW_CLIENT_NAME_OFFSET + MAX_NAME_ARRAY_SIZE
; TW_CLIENT_SIZE equ TW_CLIENT_CLAN_OFFSET + MAX_CLAN_ARRAY_SIZE

set_client_info:
; set_client_info [rax] [rdi]
; rax = client id
; needs unpacker to be at team field of the SV_CLIENTINFO msg
; https://chillerdragon.github.io/teeworlds-protocol/07/game_messages.html#NETMSGTYPE_SV_CLIENTINFO_team
push_registers

; rsi is pointer into clients array
mov rbx, rax
imul rbx, TW_CLIENT_SIZE
lea rsi, [tw_clients + rbx]

; pop team
call get_int

; name
call get_string
mov rdi, rax
lea rax, [rsi+TW_CLIENT_NAME_OFFSET]
call str_copy

pop_registers
ret

13 changes: 13 additions & 0 deletions src/data/teeworlds.asm
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,19 @@ CHUNKFLAG_RESEND equ 0b1000_0000
CHUNK_SYSTEM equ 0b0000_0001
CHUNK_GAME equ 0b0000_0000

UTF8_BYTE_LENGTH equ 4

MAX_NAME_LENGTH equ 16
MAX_NAME_ARRAY_SIZE equ MAX_NAME_LENGTH * UTF8_BYTE_LENGTH + 1

MAX_CLAN_LENGTH equ 12
MAX_CLAN_ARRAY_SIZE equ MAX_CLAN_LENGTH * UTF8_BYTE_LENGTH + 1

MAX_SKIN_LENGTH equ 12
MAX_SKIN_ARRAY_SIZE equ MAX_SKIN_LENGTH * UTF8_BYTE_LENGTH + 1

MAX_CLIENTS equ 64

GAME_NETVERSION db "0.7 802f1be60a05665f", 0
CLIENT_VERSION equ 0x0705

Expand Down
6 changes: 6 additions & 0 deletions src/data/teeworlds_state.asm
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@ input_prev_weapon dd 0

ack_game_tick dd -1

local_client_id dd -1

TW_CLIENT_ID_OFFSET equ 0
TW_CLIENT_NAME_OFFSET equ TW_CLIENT_ID_OFFSET + 4
TW_CLIENT_CLAN_OFFSET equ TW_CLIENT_NAME_OFFSET + MAX_NAME_ARRAY_SIZE
TW_CLIENT_SIZE equ TW_CLIENT_CLAN_OFFSET + MAX_CLAN_ARRAY_SIZE
38 changes: 38 additions & 0 deletions src/on_chat.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
on_chat:
; on_chat [rax]
push_registers

; mode
call get_int

; client id
call get_int
mov r8, rax

; target id
call get_int
mov r9, rax

; message
call get_string
mov r10, rax

print_label s_chat

; get author name
mov rbx, r8
imul rbx, TW_CLIENT_SIZE
lea rsi, [tw_clients + rbx + TW_CLIENT_NAME_OFFSET]
print_c_str rsi

call print_colon
call print_space

; print message
print_c_str r10

call print_newline

pop_registers
ret

20 changes: 18 additions & 2 deletions src/on_game.asm
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ on_game_msg_sv_broadcast:
on_game_msg_sv_chat:
; on_game_msg_sv_chat [rax]
; rax = message payload
print_label s_chat
call print_newline
call on_chat
jmp on_game_message_end

on_game_msg_sv_team:
Expand Down Expand Up @@ -186,6 +185,23 @@ on_game_msg_sv_serversettings:
on_game_msg_sv_clientinfo:
; on_game_msg_sv_clientinfo [rax]
; rax = message payload

; client id
call get_int
mov rcx, rax

; local
call get_int
cmp rax, 1
jne .skip_local
.local_client_info:
mov dword [local_client_id], ecx
.skip_local:

; client id
mov rax, rcx
call set_client_info

jmp on_game_message_end

on_game_msg_sv_clientdrop:
Expand Down
3 changes: 3 additions & 0 deletions src/teeworlds_asmr.asm
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ section .bss
%include "src/bss/buffers.asm"
%include "src/bss/huffman.asm"
%include "src/bss/unpacker.asm"
%include "src/bss/teeworlds_state.asm"
section .text

%include "src/macros.asm"
Expand Down Expand Up @@ -151,6 +152,8 @@ section .text
%include "src/on_snap.asm"
%include "src/console/console.asm"
%include "src/client.asm"
%include "src/client_info.asm"
%include "src/on_chat.asm"

print_udp:
print_label s_got_udp
Expand Down
3 changes: 3 additions & 0 deletions tests/assert.asm
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ section .bss
%include "src/bss/buffers.asm"
%include "src/bss/huffman.asm"
%include "src/bss/unpacker.asm"
%include "src/bss/teeworlds_state.asm"

; holds the path to the current source code file
; of the test as a null terminated C string
Expand Down Expand Up @@ -84,6 +85,8 @@ section .text
%include "src/on_snap.asm"
%include "src/console/console.asm"
%include "src/client.asm"
%include "src/client_info.asm"
%include "src/on_chat.asm"

%macro init_test 1
; init_test [__FILE__]
Expand Down

0 comments on commit df35642

Please sign in to comment.