Ejemplo n.º 1
0
    def randomize(self):
        """ Load the rom, get the FE version, load the config based on version, edit the rom """

        # Load the rom and game config
        input_rom = self._app.line_edits["rom_edit"].text()
        try:
            with open(input_rom, "rb") as stream:
                self._rom_data = bytearray(stream.read())

        except IOError as error:
            self._app.labels["status"].setText(f"Status: Error! {error}")
            return

        self._version = get_fe_version(self._rom_data)
        self._app.labels["status"].setText(f"{self._version} detected")

        # Prompt for a version if not correctly detected
        if self._version == FEVersions.UNKNOWN:
            dialogs.VersionPrompt(self._app).exec_()
            self._version = CONFIG["fe_version"]

        self._game_config = CONFIG.get_game_config(self._version)

        try:
            # Perform all requested randomization
            self._randomize_all()
        # Disable broad except because the last thing I want is for the GUI to crash with
        # no helpful error message (especially for windows users
        # pylint: disable=broad-except
        except Exception as error:
            self._app.labels["status"].setText(f"Status: Error! {error}")
            return

        try:
            # Write out the final ROM giving it a piece of UUID
            path, ext = os.path.splitext(input_rom)
            rand = str(uuid.uuid4()).split("-")[0]
            output_rom = f"{path}-{rand}{ext}"
            with open(output_rom, "wb") as stream:
                stream.write(self._rom_data)

            # If this is FE7, include save data to bypass tutorial
            # and the several soft locking opportunities that come
            # with it.
            if self._version == FEVersions.FE7:
                output_sav = f"{path}-{rand}.sav"
                shutil.copy(f"{CONFIG_PATH}/FE7.sav", output_sav)

        except IOError as error:
            self._app.labels["status"].setText(f"Status: Error! {error}")
            return

        self._app.labels["status"].setText(
            f"Status: Successfully wrote {output_rom}")