예제 #1
0
def compile_game(game: Game,
                 game_name: Optional[str] = None,
                 metadata: Mapping = {},
                 game_logger: Optional[GameLogger] = None,
                 games_folder: str = "./gen_games",
                 force_recompile: bool = False,
                 file_type: str = ".ulx") -> str:
    """
    Compile a game.

    Arguments:
        game: Game object to compile.
        game_name: Name of the compiled file (without extension).
            If `None`, a unique name will be infered from the game object.
        metadata: (Deprecated) contains information about how the game
            object was generated.
        game_logger: Object used to log stats about generated games.
        games_folder: Path to the folder where the compiled game will
            be saved.
        force_recompile: If `True`, recompile game even if it already
            exists.
        file_type: Either .z8 (Z-Machine) or .ulx (Glulx).

    Returns:
        The path to compiled game.
    """
    game_name = game_name or game.metadata["uuid"]
    source = generate_inform7_source(game)
    maybe_mkdir(games_folder)

    if str2bool(os.environ.get("TEXTWORLD_FORCE_ZFILE", False)):
        file_type = ".z8"

    game_json = pjoin(games_folder, game_name + ".json")
    meta_json = pjoin(games_folder, game_name + ".meta")
    game_file = pjoin(games_folder, game_name + file_type)

    already_compiled = False  # Check if game is already compiled.
    if not force_recompile and os.path.isfile(game_file) and os.path.isfile(
            game_json):
        already_compiled = game == Game.load(game_json)
        msg = (
            "It's highly unprobable that two games with the same id have different structures."
            " That would mean the generator has been modified."
            " Please clean already generated games found in '{}'.".format(
                games_folder))
        assert already_compiled, msg

    if not already_compiled or force_recompile:
        json.dump(metadata, open(meta_json, 'w'))
        game.save(game_json)
        compile_inform7_game(source, game_file)

    if game_logger is not None:
        game_logger.collect(game)

    return game_file
예제 #2
0
def compile_game(game: Game, path: str, force_recompile: bool = False):
    """
    Compile a game.

    Arguments:
        game: Game object to compile.
        path: Path of the compiled game (.ulx or .z8). Also, the source (.ni)
              and metadata (.json) files will be saved along with it.
        force_recompile: If `True`, recompile game even if it already exists.

    Returns:
        The path to compiled game.
    """

    folder, filename = os.path.split(path)
    if not filename:
        filename = game.metadata.get("uuid", str(uuid.uuid4()))

    filename, ext = os.path.splitext(filename)
    if not ext:
        ext = ".ulx"  # Add default extension, if needed.

    if str2bool(os.environ.get("TEXTWORLD_FORCE_ZFILE", False)):
        ext = ".z8"

    source = generate_inform7_source(game)

    maybe_mkdir(folder)
    game_json = pjoin(folder, filename + ".json")
    game_file = pjoin(folder, filename + ext)

    already_compiled = False  # Check if game is already compiled.
    if not force_recompile and os.path.isfile(game_file) and os.path.isfile(
            game_json):
        already_compiled = game == Game.load(game_json)
        msg = (
            "It's highly unprobable that two games with the same id have different structures."
            " That would mean the generator has been modified."
            " Please clean already generated games found in '{}'.".format(
                folder))
        assert already_compiled, msg

    if not already_compiled or force_recompile:
        game.save(game_json)
        compile_inform7_game(source, game_file)

    return game_file
예제 #3
0
def compile_game(game: Game, options: Optional[GameOptions] = None):
    """
    Compile a game.

    Arguments:
        game: Game object to compile.
        options:
            For customizing the game generation (see
            :py:class:`textworld.GameOptions <textworld.generator.game.GameOptions>`
            for the list of available options).

    Returns:
        The path to compiled game.
    """
    options = options or GameOptions()

    folder, filename = os.path.split(options.path)
    if not filename:
        filename = game.metadata.get("uuid", str(uuid.uuid4()))

    filename, ext = os.path.splitext(filename)
    if not ext:
        ext = options.file_ext  # Add default extension, if needed.

    source = generate_inform7_source(game)

    maybe_mkdir(folder)
    game_json = pjoin(folder, filename + ".json")
    game_file = pjoin(folder, filename + ext)

    already_compiled = False  # Check if game is already compiled.
    if not options.force_recompile and os.path.isfile(
            game_file) and os.path.isfile(game_json):
        already_compiled = game == Game.load(game_json)
        msg = (
            "It's highly unprobable that two games with the same id have different structures."
            " That would mean the generator has been modified."
            " Please clean already generated games found in '{}'.".format(
                folder))
        assert already_compiled, msg

    if not already_compiled or options.force_recompile:
        game.save(game_json)
        compile_inform7_game(source, game_file)

    return game_file