def yeet_packet(bad_packet_name): global original_tmcpr, temp_tmcpr, protocol_map_id print(f'Removing all packet named {bad_packet_name}') counter = 0 with open(original_tmcpr, 'rb') as of: with open(temp_tmcpr, 'wb') as tf: num = 0 processed = 0 while True: time_stamp = read_int(of) if time_stamp is None: break packet_length = read_int(of) packet_data = of.read(packet_length) packet = SARCPacket() packet.receive(packet_data) packet_id = packet.read_varint() packet_name = protocol_map_name[packet_id] if packet_name != bad_packet_name: write_int(tf, time_stamp) write_int(tf, packet_length) tf.write(packet_data) else: counter += 1 num += 1 processed += 8 + packet_length if num % 100000 == 0: print( f'packet: {num - counter}/{num}, file size: {round(100.0 * processed/os.path.getsize(original_tmcpr), 2)}%' ) update_tmcpr_on_editing_finished() print('Removed {} packets'.format(counter))
def fix_missing_player(): print( 'Fixing missing player (ignore all remove player action in PlayerInfo packet)' ) global original_tmcpr, temp_tmcpr, protocol_map_id with open(original_tmcpr, 'rb') as of: with open(temp_tmcpr, 'wb') as tf: while True: time_stamp = read_int(of) if time_stamp is None: break packet_length = read_int(of) packet_data = of.read(packet_length) packet = SARCPacket() packet.receive(packet_data) packet_id = packet.read_varint() bad = False if packet_id == protocol_map_id['Player Info']: action = packet.read_varint() if action == 4: bad = True if not bad: write_int(tf, time_stamp) write_int(tf, packet_length) tf.write(packet_data) update_tmcpr_on_editing_finished() print('Fixed')
def clear_weather(): global protocol_map_id counter = 0 global original_tmcpr, temp_tmcpr with open(original_tmcpr, 'rb') as of: with open(temp_tmcpr, 'wb') as tf: while True: time_stamp = read_int(of) if time_stamp is None: break packet_length = read_int(of) packet_data = of.read(packet_length) packet = SARCPacket() packet.receive(packet_data) packet_id = packet.read_varint() recorded = True if packet_id == protocol_map_id['Change Game State']: reason = packet.read_ubyte() if reason in [1, 2, 7, 8]: recorded = False if recorded: write_int(tf, time_stamp) write_int(tf, packet_length) tf.write(packet_data) else: counter += 1 update_tmcpr_on_editing_finished() print( 'Clear weather operation finished, deleted {} packets'.format(counter))
def yeet_entity(bad_entity_id): global original_tmcpr, temp_tmcpr, protocol_map_id print(f'Removing all packets caused by id {bad_entity_id}') counter = 0 blocked_entity_ids = [] with open(original_tmcpr, 'rb') as of: with open(temp_tmcpr, 'wb') as tf: num = 0 while True: time_stamp = read_int(of) if time_stamp is None: break packet_length = read_int(of) packet_data = of.read(packet_length) packet = SARCPacket() packet.receive(packet_data) packet_id = packet.read_varint() packet_name = protocol_map_name[packet_id] bad = False if packet_name == 'Spawn Mob': entity_id = packet.read_varint() entity_uuid = packet.read_uuid() entity_type = packet.read_byte() if entity_type == bad_entity_id: blocked_entity_ids.append(entity_id) bad = True elif packet_name == 'Destroy Entities': count = packet.read_varint() for i in range(count): entity_id = packet.read_varint() if entity_id in blocked_entity_ids: blocked_entity_ids.remove(entity_id) bad = True elif packet_name in constant.ENTITY_PACKETS: entity_id = packet.read_varint() if entity_id in blocked_entity_ids: bad = True if not bad: write_int(tf, time_stamp) write_int(tf, packet_length) tf.write(packet_data) else: counter += 1 num += 1 if num % 100000 == 0: print( f'packet: {num - counter}/{num}, file size: {round(100.0 * os.path.getsize(temp_tmcpr)/os.path.getsize(original_tmcpr), 2)}%' ) update_tmcpr_on_editing_finished() print('Removed {} packets'.format(counter))
def set_day_time(day_time): global protocol_map_id packet = SARCPacket() packet.write_varint(protocol_map_id['Time Update']) packet.write_long(0) # World Age packet.write_long( -day_time ) # If negative sun will stop moving at the Math.abs of the time success = False global original_tmcpr, temp_tmcpr with open(original_tmcpr, 'rb') as of: with open(temp_tmcpr, 'wb') as tf: while True: time_stamp = read_int(of) if time_stamp is None: break packet_length = read_int(of) packet_data = of.read(packet_length) if packet_length == 0: packet_data = packet.flush() success = True write_int(tf, time_stamp) write_int(tf, packet_length) tf.write(packet_data) update_tmcpr_on_editing_finished() print('Set daytime operation finished, success = {}'.format(success))
def fix_missing_time_update_after_dim_changed(): global original_tmcpr, temp_tmcpr, protocol_map_id print(f'fix missing time update after dim changed') counter = 0 time_update_packet = None with open(original_tmcpr, 'rb') as of: with open(temp_tmcpr, 'wb') as tf: num = 0 processed = 0 while True: time_stamp = read_int(of) if time_stamp is None: break packet_length = read_int(of) packet_data = of.read(packet_length) write_int(tf, time_stamp) write_int(tf, packet_length) tf.write(packet_data) packet = SARCPacket() packet.receive(packet_data) packet_id = packet.read_varint() packet_name = protocol_map_name[packet_id] if packet_name == 'Time Update': time_update_packet = bytes() time_update_packet += struct.pack('>i', time_stamp) time_update_packet += struct.pack('>i', packet_length) time_update_packet += packet_data elif packet_name == 'Respawn' and time_update_packet is not None: tf.write(time_update_packet) counter += 1 num += 1 processed += 8 + packet_length if num % 100000 == 0: print( f'processed file size: {round(100.0 * processed/os.path.getsize(original_tmcpr), 2)}%' ) update_tmcpr_on_editing_finished() print('Wrote {} time update packets after Respawn packet'.format(counter))
def analyze(): print('What' ' type of packet takes the most space') counter = {} global original_tmcpr, protocol_map_id, protocol_map_name entity_type_map = {} player_ids = [] with open('analyze.txt', 'w') as af: with open(original_tmcpr, 'rb') as of: all = 0 num = 0 while True: time_stamp = read_int(of) if time_stamp is None: break packet_length = read_int(of) packet_data = of.read(packet_length) packet = SARCPacket() packet.receive(packet_data) packet_id = packet.read_varint() packet_name = protocol_map_name[packet_id] af.write('#{}\t{}\t@ {}\n'.format(num, packet_name, time_stamp)) entity_type = None if packet_name == 'Spawn Player': entity_id = packet.read_varint() uuid = packet.read_uuid() if entity_id not in player_ids: player_ids.append(entity_id) elif packet_name == 'Spawn Mob' or packet_name == 'Spawn Object': entity_id = packet.read_varint() entity_uuid = packet.read_uuid() entity_type = packet.read_byte() entity_type_map[entity_id] = ('Mob' if packet_name == 'Spawn Mob' else 'Obj') + str(entity_type) entity_type = entity_type_map[entity_id] elif packet_name == 'Destroy Entities': count = packet.read_varint() for i in range(count): entity_id = packet.read_varint() if entity_id in player_ids: player_ids.remove(entity_id) elif packet_name in constant.ENTITY_PACKETS: entity_id = packet.read_varint() entity_type = entity_type_map.get(entity_id, '?') if entity_id in player_ids: entity_type = 'Player' if entity_type is not None: packet_name += ' (' + entity_type + ')' if packet_name not in counter: counter[packet_name] = 0 counter[packet_name] += 8 + packet_length all += 8 + packet_length num += 1 if num % 100000 == 0: print( f'scanned: {round(100.0 * all/os.path.getsize(original_tmcpr), 2)}%' ) arr = list(counter.items()) arr.sort(key=lambda x: x[1], reverse=True) for a in arr: print(f'{a[0]}: {round(a[1] / constant.BytePerMB, 5)}MB') print('Done')
def yeet_all_entity_except_player(): global original_tmcpr, temp_tmcpr, protocol_map_id print(f'Removing all non-player entity packets') counter = 0 player_ids = [] with open(original_tmcpr, 'rb') as of: with open(temp_tmcpr, 'wb') as tf: num = 0 processed = 0 while True: time_stamp = read_int(of) if time_stamp is None: break packet_length = read_int(of) packet_data = of.read(packet_length) packet = SARCPacket() packet.receive(packet_data) packet_id = packet.read_varint() packet_name = protocol_map_name[packet_id] bad = False if packet_name == 'Spawn Player': entity_id = packet.read_varint() uuid = packet.read_uuid() if entity_id not in player_ids: player_ids.append(entity_id) if packet_name == 'Spawn Mob': bad = True elif packet_name == 'Destroy Entities': count = packet.read_varint() has_player = False for i in range(count): entity_id = packet.read_varint() if entity_id in player_ids: player_ids.remove(entity_id) has_player = True if not has_player: bad = True elif packet_name in constant.ENTITY_PACKETS: entity_id = packet.read_varint() if entity_id not in player_ids: bad = True if not bad: write_int(tf, time_stamp) write_int(tf, packet_length) tf.write(packet_data) else: counter += 1 num += 1 processed += 8 + packet_length if num % 100000 == 0: print( f'packet: {num - counter}/{num}, file size: {round(100.0 * processed/os.path.getsize(original_tmcpr), 2)}%' ) update_tmcpr_on_editing_finished() print('Removed {} packets'.format(counter))