This repository was archived by the owner on Dec 18, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpacket.py
More file actions
183 lines (147 loc) · 11 KB
/
packet.py
File metadata and controls
183 lines (147 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#
#
# Created By Sam Stockstrom for Fun
# Project Started on June 25th 2024
# Based on Terraria 1.4.4.9
#
#
class Packet:
"""Class for a TNP Packet based on Terraria 1.4.4.9"""
def __init__(self, packet: bytes):
self.bytes = [bytes([byte]) for byte in packet]
"""Returns the packet bytes in a bytes list of bytes"""
self.plen = int.from_bytes(self.bytes[0], 'little')
"""Returns the payload length as given by the packet"""
self.len = len(self.bytes)
"""Returns the actual size of the packet"""
self.id = str(f"{format(int.from_bytes(self.bytes[1], 'little'), "X")}{format(int.from_bytes(self.bytes[2], 'little'), "X")}")
"""Returns the packet id in hex string"""
self.payload = self.Payload(self)
"""Returns the payload object of the packet"""
class Payload:
"""Class for a TNP Packet Payload based on Terraria 1.4.4.9"""
def __init__(self, packet):
match packet.id:
case '01':
self.description = "$01 — Connect Request (client → server)"
"""the description of the packet defined by terrafirma net"""
case '02':
self.description = "$02 — Fatal Error (client ← server)"
"""the description of the packet defined by terrafirma net"""
case '03':
self.description = "$03 — Connection Approved (client ← server)"
"""the description of the packet defined by terrafirma net"""
case '04':
self.description = "$04 — Player Appearance (client ↔ server)"
"""the description of the packet defined by terrafirma net"""
self.player_slot = int.from_bytes(packet.bytes[3], 'little')
"""Returns the player slot generated by the server"""
self.clothing_style = int.from_bytes(packet.bytes[4], 'little')
"""Returns the player's clothing style id"""
self.hair_style = int.from_bytes(packet.bytes[5], 'little')
"""Returns the player's hair style id"""
self.player_name_len = int.from_bytes(packet.bytes[6], 'little')
"""Returns the player's name length"""
player_name_char_array = []
for x in range(self.player_name_len):
letter = packet.bytes[7 + x]
player_name_char_array.append(letter.decode())
self.player_name = "".join(player_name_char_array)
"""Returns the player's name"""
hair_r = format(int.from_bytes(packet.bytes[7 + int.from_bytes(packet.bytes[6], 'little') + 4]), "X")
hair_g = format(int.from_bytes(packet.bytes[7 + int.from_bytes(packet.bytes[6], 'little') + 5]), "X")
hair_b = format(int.from_bytes(packet.bytes[7 + int.from_bytes(packet.bytes[6], 'little') + 6]), "X")
self.hair_color = f"#{hair_r}{hair_g}{hair_b}"
"""Returns the player's hair color in hex"""
skin_r = format(int.from_bytes(packet.bytes[7 + int.from_bytes(packet.bytes[6], 'little') + 7]), "X")
skin_g = format(int.from_bytes(packet.bytes[7 + int.from_bytes(packet.bytes[6], 'little') + 8]), "X")
skin_b = format(int.from_bytes(packet.bytes[7 + int.from_bytes(packet.bytes[6], 'little') + 9]), "X")
self.skin_color = f"#{skin_r}{skin_g}{skin_b}"
"""Returns the player's skin color in hex"""
eye_r = format(int.from_bytes(packet.bytes[7 + int.from_bytes(packet.bytes[6], 'little') + 10]), "X")
eye_g = format(int.from_bytes(packet.bytes[7 + int.from_bytes(packet.bytes[6], 'little') + 11]), "X")
eye_b = format(int.from_bytes(packet.bytes[7 + int.from_bytes(packet.bytes[6], 'little') + 12]), "X")
self.eye_color = f"#{eye_r}{eye_g}{eye_b}"
"""Returns the player's eye color in hex"""
undershirt_r = format(int.from_bytes(packet.bytes[7 + int.from_bytes(packet.bytes[6], 'little') + 13]), "X")
undershirt_g = format(int.from_bytes(packet.bytes[7 + int.from_bytes(packet.bytes[6], 'little') + 14]), "X")
undershirt_b = format(int.from_bytes(packet.bytes[7 + int.from_bytes(packet.bytes[6], 'little') + 15]), "X")
self.undershirt_color = f"#{undershirt_r}{undershirt_g}{undershirt_b}"
"""Returns the player's undershirt color in hex"""
shirt_r = format(int.from_bytes(packet.bytes[7 + int.from_bytes(packet.bytes[6], 'little') + 16]), "X")
shirt_g = format(int.from_bytes(packet.bytes[7 + int.from_bytes(packet.bytes[6], 'little') + 17]), "X")
shirt_b = format(int.from_bytes(packet.bytes[7 + int.from_bytes(packet.bytes[6], 'little') + 18]), "X")
self.shirt_color = f"#{shirt_r}{shirt_g}{shirt_b}"
"""Returns the player's shirt color in hex"""
pants_r = format(int.from_bytes(packet.bytes[7 + int.from_bytes(packet.bytes[6], 'little') + 19]), "X")
pants_g = format(int.from_bytes(packet.bytes[7 + int.from_bytes(packet.bytes[6], 'little') + 20]), "X")
pants_b = format(int.from_bytes(packet.bytes[7 + int.from_bytes(packet.bytes[6], 'little') + 21]), "X")
self.pants_color = f"#{pants_r}{pants_g}{pants_b}"
"""Returns the player's pants color in hex"""
boots_r = format(int.from_bytes(packet.bytes[7 + int.from_bytes(packet.bytes[6], 'little') + 22]), "X")
boots_g = format(int.from_bytes(packet.bytes[7 + int.from_bytes(packet.bytes[6], 'little') + 23]), "X")
boots_b = format(int.from_bytes(packet.bytes[7 + int.from_bytes(packet.bytes[6], 'little') + 24]), "X")
self.boots_color = f"#{boots_r}{boots_g}{boots_b}"
"""Returns the player's boot color in hex"""
# TODO: Double check this logic...
player_difficulty_int = int.from_bytes(packet.bytes[7 + int.from_bytes(packet.bytes[6], 'little') + 25])
player_difficulty = ""
match player_difficulty_int:
case 8:
player_difficulty = "Journey"
case 0:
player_difficulty = "Classic"
case 1:
player_difficulty = "Mediumcore"
case 2:
player_difficulty = "Hardcore"
case _:
player_difficulty = f"Unknown! Int: {player_difficulty_int}"
self.player_difficulty = player_difficulty
"""Returns the player's difficulty"""
self.last_byte = packet.bytes[7 + int.from_bytes(packet.bytes[6], 'little') + 26]
"""I don't really know what this does yet...."""
case '05':
self.description = "$05 — Set Inventory (client ↔ server)"
"""the description of the packet defined by terrafirma net"""
# TODO: This filters out the initial packet, may need to change this
if packet.len == 11:
self.player_slot = int.from_bytes(packet.bytes[3], 'little')
"""Returns the player slot generated by the server"""
self.item_slot = int.from_bytes(packet.bytes[4], 'little')
"""Returns the current item's item slot"""
self.unknown_x = int.from_bytes(packet.bytes[5], 'little')
"""Returns an unknown value"""
self.item_count = int.from_bytes(packet.bytes[6] + packet.bytes[7], 'little')
"""Returns the current item's item count"""
self.item_modifier_id = int.from_bytes(packet.bytes[8], 'little')
"""Returns the current item's modifier id"""
self.item_id = int.from_bytes(packet.bytes[9], 'little')
"""Returns the current item's item id"""
self.unknown_y = int.from_bytes(packet.bytes[10], 'little')
"""Returns an unknown value"""
case '06':
self.description = "Request World Information (client ← server)"
"""the description of the packet defined by terrafirma net"""
case '07':
self.description = "World Information (client ← server)"
"""the description of the packet defined by terrafirma net"""
self.gametime = int.from_bytes(packet.bytes[3] + packet.bytes[4] + packet.bytes[5] + packet.bytes[6], 'little', signed=True)
"""Returns the current time in the world (Int32)"""
self.daytime = bool.from_bytes(packet.bytes[7], 'little')
"""Returns true if it is daytime in the current world"""
self.moonphase = int.from_bytes(packet.bytes[8], 'little')
"""Returns the moon phase in the current world"""
# TODO: I left off here. 7/17/2024
case '025':
self.description = "$25 — Request Password (client ← server)"
"""the description of the packet defined by terrafirma net"""
case '026':
self.description = "$26 - Login with Password (client → server)"
"""the description of the packet defined by terrafirma net"""
case '044':
self.description = "$44 — \"Known\" Unknown (client → server)"
"""the description of the packet defined by terrafirma net"""
case _:
self.description = f"${packet.id} - Unknown"
"""the description of the packet defined by terrafirma net"""