def convert_replay(replay_path: str, out_folder: str) -> Dict: ''' Parses the replay file intro proto and pandas. Returns a dictionary with keys ['proto', 'pandas'] with the paths to each file. ''' id = os.path.splitext(os.path.basename(replay_path))[0] proto_path = os.path.join(out_folder, '{}.pts'.format(id)) pandas_path = os.path.join(out_folder, '{}.gzip'.format(id)) temp_path = tempfile.mktemp(suffix='.json') _json = carball.decompile_replay(replay_path, temp_path) game = Game() game.initialize(loaded_json=_json) analysis = AnalysisManager(game) analysis.create_analysis() with open(proto_path, 'wb') as f: analysis.write_proto_out_to_file(f) with open(pandas_path, 'wb') as f: analysis.write_pandas_out_to_file(f) return {'proto': proto_path, 'pandas': pandas_path}
def processReplays(self, replay, outputfile): self.outputfile = outputfile _json = carball.decompile_replay(replay) game = Game() player = Player() # Initialize and perform default analysis game.initialize(loaded_json=_json) analysis = AnalysisManager(game) analysis.create_analysis() # Get protobuf data to analyze proto_game = analysis.get_protobuf_data() # Readability self.meta = proto_game.game_metadata self.team = proto_game.teams self.players = proto_game.players self.getMetaInfo() self.getPlayerInfo() self.getTeamInfo() self.outputJSON()
def parse_files(abs_path, spell_check, playlist_filter, single_player): temp_output_dir = abs_path + "/TempJSON/" replay_dir = abs_path print("Engine: verifying temp directory") sys.stdout.flush() print("Engine: parsing folder: " + replay_dir) for replay_file in get_files(replay_dir): print("Engine: Analyzing replay: " + replay_file) sys.stdout.flush() _json = carball.decompile_replay(replay_dir + "/" + replay_file, output_path=temp_output_dir + 'decompiled_replay.json', overwrite=True) game = Game() game.initialize(loaded_json=_json) analysis = AnalysisManager(game) analysis.create_analysis() raw_json = MessageToJson(analysis.protobuf_game) data = json.loads(raw_json) f = open(temp_output_dir + "lastfile.json", "w+") f.write(raw_json) f.close() print("Engine: sending data from " + replay_file + " to Builder") sys.stdout.flush() Builder(data, spell_check, playlist_filter, single_player)
def parseReplayToGameData(replayID): replayPath = os.path.join("replays", "%s.replay" % replayID) print("Loading...\n\t%s" % replayPath) json = carball.decompile_replay(replayPath) game = Game() game.initialize(loaded_json=json) # Parses action-related properties into their actual actions ControlsCreator().get_controls(game) return game
def decompile(self, parser='boxcars', logTime=False, startTime=time.time(), onlyHeader=False): try: if parser == 'boxcars': if onlyHeader: args = ['rrrocket.exe', self.replayPath] else: args = ['rrrocket.exe', '-n', self.replayPath] output = Popen(args, stdout=PIPE).communicate()[0].decode() _json = json.loads(output) else: _json = carball.decompile_replay(self.replayPath, self.jsonPath, overwrite=True) os.remove(self.jsonPath) if logTime: print('Decompile time: ' + str(time.time() - startTime)) return _json except: print('Could not decompile replay') return None
import carball from carball.json_parser.game import Game from carball.analysis.analysis_manager import AnalysisManager import sys import os import json _json_name = output_path = sys.argv[2] + '.tmp' _json = carball.decompile_replay(sys.argv[1], _json_name, overwrite=True) game = Game() game.initialize(loaded_json=_json) analysis = AnalysisManager(game) analysis.create_analysis(calculate_intensive_events=False) _output_tmp_name = sys.argv[2] + "-" + 'tmp' + ".json" with open(_output_tmp_name, 'w') as out: analysis.write_json_out_to_file(out) with open(_output_tmp_name) as tmp: data = json.load(tmp) time = data['gameMetadata']['time'] _output_name = sys.argv[2] + "-" + time + ".json" os.rename(_output_tmp_name, _output_name) if os.path.exists(_json_name): os.remove(_json_name) else:
output_path = "./parsed_replays/" # include trailing slash input_paths = [ "./replay_files/1v1s/Syracks/", "./replay_files/1v1s/Kevpert/", ] print(f"Saving all replay data into {output_path}") for input_path in input_paths: input_path = os.path.normpath(input_path) print(f"Taking replays from {input_path}/") prefix = os.path.basename(os.path.normpath(input_path)) + '_' print(f"Saving files with prefix '{prefix}'") input_files = os.listdir(input_path) for filename in input_files: if filename.endswith('.replay'): name = filename[0:filename.find('.replay')] output = output_path + prefix + name + '.json' try: _json = carball.decompile_replay(f"{input_path}/{filename}", output_path=output, overwrite=True) except: print( f"Something went wrong reading \"{filename}\". Omitting.") num_bad_files += 1 print(f"Finished parsing {len(input_files) - num_bad_files} replays.") print(f" --> {num_bad_files} files failed to parse. See above for filesnames.")
def createDataFromReplay(filepath, outputPath, jsonPath, save_json=True): carball.decompile_replay(filepath, output_path=jsonPath, overwrite=True) createAndSaveReplayTrainingDataFromJSON(jsonPath, outputFileName=outputPath) if not save_json: os.remove(jsonPath)
def decompile(filename): file = carball.decompile_replay(config.REPLAYS_DIR + filename + '.replay', output_path=config.JSON_DIR + filename + '.json', overwrite=True) return file
def compile_to_json(): carball.decompile_replay( os.path.abspath( 'test/replays/516E50BF491D3ECE8D1E9FAD08C00F9B.replay'), os.path.abspath('test/replays/516E50BF491D3ECE8D1E9FAD08C00F9B.json'), overwrite=False)