Esempio n. 1
0
def analyze_replay_file(replay_path: str,
                        output_path: str,
                        overwrite=True,
                        controls: ControlsCreator = None,
                        sanity_check: SanityChecker = None):
    """
    Decompile and analyze a replay file.

    :param replay_path: Path to replay file
    :param output_path: Path to write JSON
    :param overwrite: If to overwrite JSON (suggest True if speed is not an issue)
    :param controls: Generate controls from the replay using our best guesses (ALPHA)
    :param sanity_check: Run sanity check to make sure we analyzed correctly (BETA)
    :return: AnalysisManager of game with analysis.
    """
    _json = decompile_replay(replay_path, output_path, overwrite=overwrite)
    game = Game(loaded_json=_json)
    # get_controls(game)  # TODO: enable and optimise.
    if controls is not None:
        controls.get_controls(game)
    if sanity_check is not None:
        sanity_check.check_game(game)
    analysis = AnalysisManager(game)
    analysis.create_analysis()

    return analysis
Esempio n. 2
0
def analyze_replay_file(replay_path: str, output_path: str = None, overwrite=True, controls: ControlsCreator = None,
                        sanity_check: SanityChecker = None, analysis_per_goal=False, rattletrap_path: str = None):
    """
    Decompile and analyze a replay file.

    :param replay_path: Path to replay file
    :param output_path: Path to write JSON
    :param overwrite: If to overwrite JSON (suggest True if speed is not an issue)
    :param controls: Generate controls from the replay using our best guesses (ALPHA)
    :param sanity_check: Run sanity check to make sure we analyzed correctly (BETA)
    :param analysis_per_goal: Runs the analysis per a goal instead of the replay as a whole
    :param rattletrap_path: Custom location for rattletrap executable. Path to folder.
    :param force_full_analysis: If True full analysis will be performed even if checks say it should not.
    :return: AnalysisManager of game with analysis.
    """
    _json = decompile_replay(replay_path, output_path=output_path, overwrite=overwrite, rattletrap_path=rattletrap_path)
    game = Game()
    game.initialize(loaded_json=_json)
    # get_controls(game)  # TODO: enable and optimise.
    if sanity_check is not None:
        sanity_check.check_game(game)
    if analysis_per_goal:
        analysis = PerGoalAnalysis(game)
    else:
        analysis = AnalysisManager(game)
    analysis.create_analysis()

    if controls is not None:
        controls.get_controls(game)

    return analysis
Esempio n. 3
0
def analyze_replay_file(replay_path: str,
                        output_path: str = None,
                        overwrite=True,
                        controls: ControlsCreator = None,
                        sanity_check: SanityChecker = None,
                        analysis_per_goal=False,
                        rattletrap_path: str = None,
                        logging_level=logging.NOTSET,
                        calculate_intensive_events: bool = False):
    """
    Decompile and analyze a replay file.

    :param replay_path: Path to replay file
    :param output_path: Path to write JSON
    :param overwrite: If to overwrite JSON (suggest True if speed is not an issue)
    :param controls: Generate controls from the replay using our best guesses (ALPHA)
    :param sanity_check: Run sanity check to make sure we analyzed correctly (BETA)
    :param analysis_per_goal: Runs the analysis per a goal instead of the replay as a whole
    :param rattletrap_path: Custom location for rattletrap executable. Path to folder.
    :param force_full_analysis: If True full analysis will be performed even if checks say it should not.
    :param logging_level: Sets the logging level globally across carball
    :param calculate_intensive_events: Indicates if expensive calculations should run to include additional stats.
    :return: AnalysisManager of game with analysis.
    """

    if logging_level != logging.NOTSET:
        logging.getLogger('carball').setLevel(logging_level)

    _json = decompile_replay(replay_path,
                             output_path=output_path,
                             overwrite=overwrite,
                             rattletrap_path=rattletrap_path)
    game = Game()
    game.initialize(loaded_json=_json)
    # get_controls(game)  # TODO: enable and optimise.
    if sanity_check is not None:
        sanity_check.check_game(game)
    if analysis_per_goal:
        analysis = PerGoalAnalysis(game)
    else:
        analysis = AnalysisManager(game)
    analysis.create_analysis(
        calculate_intensive_events=calculate_intensive_events)

    if controls is not None:
        controls.get_controls(game)

    return analysis
Esempio n. 4
0
def __test_replays(BASE_DIR):
    import logging

    ROOT_DIR = os.path.dirname(BASE_DIR)
    OUTPUT_DIR = os.path.join(ROOT_DIR, 'output')

    logging.basicConfig(level=logging.INFO)
    logger = logging.getLogger(__name__)
    MOVE_WORKING = True
    DEBUGGING = True
    success = 0
    failure = 0
    create_dir(OUTPUT_DIR)
    sanity_check = SanityChecker()
    sanity_check = None

    for filepath in glob.iglob(ROOT_DIR + '/**carball/replays/*.replay', recursive=True):
        logger.info('decompiling %s', filepath)
        if "output" in filepath:
            continue
        base_file = os.path.basename(filepath)
        json_path = os.path.join(OUTPUT_DIR, 'replays/decompiled/{}'.format(base_file.replace(".replay", ".json")))
        proto_path = os.path.join(OUTPUT_DIR, 'replays/protos/{}'.format(base_file.replace(".replay", ".pts")))
        pandas_path = os.path.join(OUTPUT_DIR, 'replays/pandas/{}'.format(base_file.replace(".replay", ".gzip")))

        create_dir(os.path.dirname(json_path))
        create_dir(os.path.dirname(proto_path))
        create_dir(os.path.dirname(pandas_path))

        if DEBUGGING:
            try:
                analysis_manager = analyze_replay_file(filepath, json_path,
                                                       controls=ControlsCreator(), analysis_per_goal=False,
                                                       sanity_check=sanity_check)
                id = analysis_manager.protobuf_game.game_metadata.id
                with open(os.path.join(OUTPUT_DIR, id + 'game.json'), 'w') as f:
                    f.write(MessageToJson(analysis_manager.protobuf_game))
                    bytes = PandasManager.write_pandas_to_buffer_for_tooling(analysis_manager.get_data_frame(),
                                                           analysis_manager.get_protobuf_data().players)
                    with open(pandas_path, 'wb') as fo:
                        fo.write(bytes)

                data_frame = PandasManager.safe_read_pandas_to_memory(BytesIO(analysis_manager.df_bytes))
                logger.info('length of decoded pandas %i', len(data_frame))
            except subprocess.CalledProcessError as e:
                traceback.print_exc()
        else:
            try:
                analysis_manager = analyze_replay_file(filepath, json_path)
                with open(proto_path, 'wb') as fo:
                    analysis_manager.write_proto_out_to_file(fo)
                with gzip.open(pandas_path, 'wb') as fo:
                    analysis_manager.write_pandas_out_to_file(fo)
                if MOVE_WORKING:
                    shutil.move(filepath, os.path.join('replays', 'working', filepath))
                success += 1
            except Exception as e:
                traceback.print_exc()
                failure += 1
    if not DEBUGGING:
        if float(success + failure) == 0:
            print("NO REPLAYS WERE RUN.")
            print("Need files in: " + BASE_DIR)
        ratio = success / float(success + failure)
        print('success ratio:', ratio)
Esempio n. 5
0
def __test_replays(BASE_DIR):
    import logging

    ROOT_DIR = os.path.dirname(BASE_DIR)
    OUTPUT_DIR = os.path.join(ROOT_DIR, 'output')

    logging.basicConfig(level=logging.DEBUG)
    logger = logging.getLogger(__name__)
    MOVE_WORKING = True
    DEBUGGING = True
    success = 0
    failure = 0
    create_dir(OUTPUT_DIR)

    for filepath in glob.iglob(ROOT_DIR + '/**/*.replay', recursive=True):
        logger.info('decompiling %s', filepath)
        base_file = os.path.basename(filepath)
        json_path = os.path.join(
            OUTPUT_DIR, 'replays/decompiled/{}'.format(
                base_file.replace(".replay", ".json")))
        proto_path = os.path.join(
            OUTPUT_DIR,
            'replays/protos/{}'.format(base_file.replace(".replay", ".pts")))
        pandas_path = os.path.join(
            OUTPUT_DIR,
            'replays/pandas/{}'.format(base_file.replace(".replay", ".gzip")))

        create_dir(os.path.dirname(json_path))
        create_dir(os.path.dirname(proto_path))
        create_dir(os.path.dirname(pandas_path))

        if DEBUGGING:
            try:
                analysis_manager = analyze_replay_file(
                    filepath,
                    json_path,
                    sanity_check=SanityChecker(),
                    controls=ControlsCreator())
                with open(os.path.join(OUTPUT_DIR, 'game.json'), 'w') as f:
                    f.write(MessageToJson(analysis_manager.protobuf_game))
            except subprocess.CalledProcessError as e:
                traceback.print_exc()
        else:
            try:
                analysis_manager = analyze_replay_file(filepath, json_path)
                with open(proto_path, 'wb') as fo:
                    analysis_manager.write_proto_out_to_file(fo)
                with gzip.open(pandas_path, 'wb') as fo:
                    analysis_manager.write_pandas_out_to_file(fo)
                if MOVE_WORKING:
                    shutil.move(filepath,
                                os.path.join('replays', 'working', filepath))
                success += 1
            except Exception as e:
                traceback.print_exc()
                failure += 1
    if not DEBUGGING:
        if float(success + failure) == 0:
            print("NO REPLAYS WERE RUN.")
            print("Need files in: " + BASE_DIR)
        ratio = success / float(success + failure)
        print('success ratio:', ratio)