-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrun.py
325 lines (298 loc) · 15.9 KB
/
run.py
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import os
import time
import json
import traceback
import utils
import select
import zipfile
import datetime
from packet import *
def run(config, email, password, debug, address):
access_token, uuid, user_name = utils.get_token(email, password)
clientbound, serverbound, protocol_version, mc_version = utils.generate_protocol_table(address)
connection = utils.login(address, protocol_version, debug, access_token, uuid, user_name)
print(clientbound)
start_time = int(time.time() * 1000)
last_player_movement = start_time
entity_packets = ['Entity', 'Entity Relative Move', 'Entity Look And Relative Move', 'Entity Look',
'Entity Teleport']
player_uuids = []
player_ids = []
blocked_entity_ids = []
opped_players = []
write_buffer = bytearray()
file_size = 0
afk_time = 0
last_t = 0
open('recording.tmcpr', 'w').close() # Cleans recording file
time.sleep(0.5)
utils.request_ops(connection, serverbound, protocol_version) # Request op list once
if 'Time Update' in utils.BAD_PACKETS:
utils.BAD_PACKETS.remove('Time Update')
# Main processing loop for incoming data.
while True:
ready_to_read = select.select([connection.socket], [], [], 0)[0]
if ready_to_read:
t = int(time.time() * 1000)
try:
packet_in = connection.receive_packet()
except IOError:
break
packet_recorded = int(t - start_time - afk_time).to_bytes(4, byteorder='big', signed=True)
packet_recorded += len(packet_in.received).to_bytes(4, byteorder='big', signed=True)
packet_recorded += packet_in.received
packet_id = packet_in.read_varint()
packet_name = clientbound[str(packet_id)]
if debug:
print('P Packet ' + hex(packet_id) + ': ' + packet_name)
# Answer keep aLive
if packet_name == 'Keep Alive (clientbound)':
packet_out = Packet()
packet_out.write_varint(serverbound['Keep Alive (serverbound)'])
if protocol_version > 338: # For some unnecessary reason keepalive changes to long after 1.12.1
id = packet_in.read_long()
packet_out.write_long(id)
else:
id = packet_in.read_varint()
packet_out.write_varint(id)
connection.send_packet(packet_out)
# Respawn when dead
if packet_name == 'Update Health':
health = packet_in.read_float()
food = packet_in.read_varint()
food_sat = packet_in.read_float()
if health == 0.0:
packet_out = Packet()
packet_out.write_varint(serverbound['Client Status'])
packet_out.write_varint(0)
connection.send_packet(packet_out)
# If configured set daytime once and ignore all further time updates
if (24000 > config['daytime'] > 0 and packet_name == 'Time Update' and not
utils.is_bad_packet(packet_name, config['minimal_packets'])):
print('Set daytime to: ' + str(config['daytime']))
packet_daytime = Packet()
packet_daytime.write_varint(
int(list(clientbound.keys())[list(clientbound.values()).index('Time Update')]))
world_age = packet_in.read_long()
packet_daytime.write_long(world_age)
packet_daytime.write_long(
-config['daytime']) # If negative sun will stop moving at the Math.abs of the time
packet_recorded = int(t - start_time).to_bytes(4, byteorder='big', signed=True)
packet_recorded += len(packet_daytime.received).to_bytes(4, byteorder='big', signed=True)
packet_recorded += packet_daytime.received
write_buffer += packet_recorded
utils.BAD_PACKETS.append('Time Update') # Ignore all further updates
# Remove weather if configured
if not config['weather'] and packet_name == 'Change Game State':
reason = packet_in.read_ubyte()
if reason == 1 or reason == 2:
packet_recorded = ''
# Teleport confirm
if packet_name == 'Player Position And Look (clientbound)':
x = packet_in.read_double()
y = packet_in.read_double()
z = packet_in.read_double()
yaw = packet_in.read_float()
pitch = packet_in.read_float()
flag = packet_in.read_byte()
teleport_id = packet_in.read_varint()
packet_out = Packet()
packet_out.write_varint(serverbound['Teleport Confirm'])
packet_out.write_varint(teleport_id)
connection.send_packet(packet_out)
# Update player list for metadata and player tracking
if packet_name == 'Spawn Player':
entity_id = packet_in.read_varint()
if entity_id not in player_ids:
player_ids.append(entity_id)
uuid = packet_in.read_uuid()
if uuid not in player_uuids:
player_uuids.append(uuid)
last_player_movement = int(time.time() * 1000)
# Keep track of spawned items and their ids
if ((config['remove_items'] or config['remove_bats']) and
(packet_name == 'Spawn Object' or packet_name == 'Spawn Mob')):
entity_id = packet_in.read_varint()
uuid = packet_in.read_uuid()
type = packet_in.read_byte()
if ((packet_name == 'Spawn Object' and type == 2 and protocol_version > 340) or
(packet_name == 'Spawn Mob' and ((type == 65 and protocol_version <= 340) or (type == 3 and protocol_version > 340)))):
blocked_entity_ids.append(entity_id)
packet_recorded = ''
# Remove item pickup animation packet
if config['remove_items'] and packet_name == 'Collect Item':
packet_recorded = ''
# Detecting player activity to continue recording and remove items or bats
if packet_name in entity_packets:
entity_id = packet_in.read_varint()
if config['recording'] and entity_id in player_ids:
last_player_movement = t
if entity_id in blocked_entity_ids:
recorded_packet = ''
# Record all "joining" or "leaving" tab updates to properly start recording players
# In 1.14.4 Player List Item changes to Player Info
if (protocol_version < 498 and packet_name == 'Player List Item') or (protocol_version >= 498 and packet_name == 'Player Info'):
action = packet_in.read_varint()
if config['recording'] and action == 0: # int(time.time() * 1000) - last_player_movement <= 5000 and
write_buffer += packet_recorded
player_number = packet_in.read_varint()
uuid = packet_in.read_uuid()
name = packet_in.read_utf()
# Handle chat and process ingame commands
if packet_name == 'Chat Message (clientbound)':
try: # For whatever reason there sometimes exists an empty chat packet..
chat = packet_in.read_utf()
chat = json.loads(chat)
if chat['translate'] == 'chat.type.text':
name = chat['with'][0]['hoverEvent']['value']['text'].split(':"')[1].split('"', 1)[0]
uuid = chat['with'][0]['hoverEvent']['value']['text'].split(':"')[2].split('"', 1)[0]
message = chat['with'][1]
if message == '!updateops':
utils.request_ops(connection, serverbound, protocol_version)
utils.send_chat_message(connection, serverbound, 'Updating OP list')
if name in opped_players:
print('<' + name + '(OP)> ' + message)
else:
print('<' + name + '> ' + message)
if (config['require_op'] and name in opped_players) or not config['require_op']:
if message == '!relog':
should_restart = True
print('Relogging...')
break
if message == '!stop':
should_restart = False
print('Stopping...')
break
if message == '!ping':
utils.send_chat_message(connection, serverbound, 'pong!')
if message == '!filesize':
utils.send_chat_message(connection, serverbound,
str(round(file_size / 1000000, 1)) + 'MB')
if message == '!time':
utils.send_chat_message(connection, serverbound,
'Recorded time: ' + utils.convert_millis(
t - start_time - afk_time))
if message == '!timeonline':
utils.send_chat_message(connection, serverbound,
'Time client was online: ' + utils.convert_millis(
t - start_time))
if message == '!move':
packet_out = Packet()
packet_out.write_varint(serverbound['Spectate'])
packet_out.write_uuid(uuid)
connection.send_packet(packet_out)
utils.send_chat_message(connection, serverbound, 'moved to ' + name)
if message == '!glow':
# Chat system got updated in 1.13
if protocol_version > 340:
utils.send_chat_message(connection, serverbound,
'/effect give @s minecraft:glowing 1000000 0 true')
else:
utils.send_chat_message(connection, serverbound,
'/effect @p minecraft:glowing 1000000 0 true')
except:
pass
# Process the requested list of opped players
if packet_name == 'Tab-Complete (clientbound)':
matches = []
if protocol_version <= 404:
count = packet_in.read_varint()
for i in range(count):
matches.append(packet_in.read_utf())
else:
id = packet_in.read_varint()
start = packet_in.read_varint()
length = packet_in.read_varint()
count = packet_in.read_varint()
for i in range(count):
matches.append(packet_in.read_utf())
has_tooltip = packet_in.read_bool()
if has_tooltip:
tooltip = packet_in.read_utf()
for match in matches:
if match not in opped_players:
opped_players.append(match)
# Actual recording
if (config['recording'] and t - last_player_movement <= 5000 and not
utils.is_bad_packet(packet_name, config['minimal_packets'])):
# To prevent constant writing to the disk a buffer of 8kb is used
if packet_recorded != '':
write_buffer += packet_recorded
if len(write_buffer) > 8192:
with open('recording.tmcpr', 'ab+') as replay_recording:
replay_recording.write(write_buffer)
if debug:
print('Recorded:' + str(write_buffer)[:80] + '...')
file_size += len(write_buffer)
write_buffer = bytearray()
# Prevent any kind of size increase due to packets beeing added to the buffer it gets cleared when not needed
if not config['recording'] and len(write_buffer) > 0:
write_buffer = bytearray()
# Increase afk timer when recording stopped, afk timer prevents afk time in replays
if config['recording'] and t - last_player_movement > 5000:
afk_time += t - last_t
last_t = t # Save last packet timestamp for afk delta
# Every 150mb the client restarts
if config['recording'] and file_size > 150000000:
print('Filesize limit reached!')
utils.send_chat_message(connection, serverbound, 'Filesize limit reached, restarting...')
should_restart = True
time.sleep(1)
break
# Every 5h recording the the client restarts
elif config['recording'] and t - start_time - afk_time > 1000 * 60 * 60 * 5:
print('5h recording reached!')
utils.send_chat_message(connection, serverbound, '5h recording reached, restarting...')
should_restart = True
time.sleep(1)
break
else:
time.sleep(0.0001) # Release to prevent 100% cpu usage
# Handling the disconnect
print('Disconnected')
if config['recording'] and len(write_buffer) > 0: # Finish writing if buffer not empty
with open('recording.tmcpr', 'ab+') as replay_recording:
replay_recording.write(write_buffer)
write_buffer = bytearray()
print('Time client was online: ' + utils.convert_millis(t - start_time))
if config['recording']:
print('Recorded time: ' + utils.convert_millis(t - start_time - afk_time))
# Create metadata file
with open('metaData.json', 'w') as json_file:
meta_data = {'singleplayer': 'false', 'serverName': address[0],
'duration': int(time.time() * 1000) - start_time - afk_time, 'date': int(time.time() * 1000),
'mcversion': mc_version, 'fileFormat': 'MCPR', 'generator': 'SARC',
'fileFormatVersion': utils.get_metadata_file_format(protocol_version),
'protocol': protocol_version, 'selfId': -1, 'players': player_uuids}
json.dump(meta_data, json_file)
# Creating .mcpr zipfile based on timestamp
print('Creating .mcpr file...')
date = datetime.datetime.today().strftime('SARC_%Y%m%d_%H_%S')
zipf = zipfile.ZipFile(date + '.mcpr', 'w', zipfile.ZIP_DEFLATED)
zipf.write('metaData.json')
zipf.write('recording.tmcpr')
os.remove('metaData.json')
os.remove('recording.tmcpr')
print('Finished!')
connection.close()
return should_restart
config, email, password = utils.load_config()
debug = config['debug_mode']
address = (config['ip'], int(config['port']))
while True:
try:
if not run(config, email, password, debug, address):
break
else:
print('Reconnecting...')
except Exception as e:
if debug:
print('Connection lost: ' + traceback.format_exc())
else:
print('Connection lost: ' + str(e))
if not config['auto_relog']:
break
else:
print('Reconnecting...')
time.sleep(3)
print('Ending...')