예제 #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 load(self, gamefile: str) -> None:
        self._gamefile = os.path.splitext(gamefile)[0] + ".json"
        if not os.path.isfile(self._gamefile):
            raise MissingGameInfosError(self)

        try:
            self._game = self._wrapped_env._game
        except AttributeError:
            self._game = Game.load(self._gamefile)
        self._wrapped_env.load(gamefile)
예제 #3
0
    def load(self, gamefile: str) -> None:
        self._wrapped_env.load(gamefile)
        self._gamefile = os.path.splitext(gamefile)[0] + ".json"
        try:
            self._game = self._wrapped_env._game
        except AttributeError:
            if not os.path.isfile(self._gamefile):
                raise MissingGameInfosError(self)

            self._game = Game.load(self._gamefile)

        self._game_progression = None
        self._inform7 = Inform7Game(self._game)
예제 #4
0
def extract_vocab_from_gamefile(gamefile: str) -> Set[str]:
    vocab = set()
    jsonfile = os.path.splitext(gamefile)[0] + ".json"
    if os.path.isfile(jsonfile):
        game = Game.load(jsonfile)
        vocab |= extract_vocab(game)

    if re.search(r"\.z[1-8]$", gamefile):
        # For Z-Machine games, extract vocab using Jericho.
        import jericho
        env = jericho.FrotzEnv(gamefile)
        vocab |= set(entry.word for entry in env.get_dictionary())

    return vocab
예제 #5
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
예제 #6
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
예제 #7
0
    def __init__(self, gamefile) -> None:
        """ Creates a GitGlulxML from the given gamefile
        """
        super().__init__()

        self.message_broker = ClientMessageBroker()

        self._gamefile = gamefile
        self._process = None

        # Load initial state of the game.
        filename, ext = os.path.splitext(gamefile)
        game_json = filename + ".json"

        if not os.path.isfile(game_json):
            raise MissingGameInfosError()

        self._state_tracking = False
        self._compute_intermediate_reward = False
        self.game = Game.load(game_json)
        self.game_state = None
예제 #8
0
    def __init__(self, gamefile: str) -> None:
        """ Creates a GitGlulxML from the given gamefile

        Args:
            gamefile: The name of the gamefile to load.
        """
        super().__init__()
        self._gamefile = gamefile
        self._process = None

        # Load initial state of the game.
        filename, ext = os.path.splitext(gamefile)
        game_json = filename + ".json"

        if not os.path.isfile(game_json):
            raise MissingGameInfosError()

        self._state_tracking = False
        self._compute_intermediate_reward = False
        self.game = Game.load(game_json)
        self.game_state = None
        self.extra_info = set()