From 55fcd5c5f4503442cd232647bcb88448765d450f Mon Sep 17 00:00:00 2001 From: Mads Ynddal <5528170+Baekalfen@users.noreply.github.com> Date: Thu, 4 Jan 2024 19:39:22 +0100 Subject: [PATCH] Change pyboy.cartridge_title() to property --- pyboy/plugins/base_plugin.py | 2 +- pyboy/plugins/game_wrapper_pokemon_gen1.py | 2 +- pyboy/plugins/screen_recorder.py | 2 +- pyboy/plugins/screenshot_recorder.py | 2 +- pyboy/pyboy.pxd | 1 + pyboy/pyboy.py | 23 +++++++++++----------- tests/test_game_wrapper_mario.py | 4 ++-- tests/test_replay.py | 2 +- tests/test_states.py | 2 +- tests/test_tetris_ai.py | 2 +- 10 files changed, 21 insertions(+), 21 deletions(-) diff --git a/pyboy/plugins/base_plugin.py b/pyboy/plugins/base_plugin.py index 663ff7688..86c634246 100644 --- a/pyboy/plugins/base_plugin.py +++ b/pyboy/plugins/base_plugin.py @@ -119,7 +119,7 @@ def __init__(self, *args, game_area_section=(0, 0, 32, 32), game_area_wrap_aroun self._cached_game_area_tiles = memoryview(self._cached_game_area_tiles_raw).cast("I", shape=(width, height)) def enabled(self): - return self.cartridge_title is None or self.pyboy.cartridge_title() == self.cartridge_title + return self.cartridge_title is None or self.pyboy.cartridge_title == self.cartridge_title def post_tick(self): raise NotImplementedError("post_tick not implemented in game wrapper") diff --git a/pyboy/plugins/game_wrapper_pokemon_gen1.py b/pyboy/plugins/game_wrapper_pokemon_gen1.py index 12fa4cd76..d2717082f 100644 --- a/pyboy/plugins/game_wrapper_pokemon_gen1.py +++ b/pyboy/plugins/game_wrapper_pokemon_gen1.py @@ -32,7 +32,7 @@ def __init__(self, *args, **kwargs): self.sprite_offset = 0x1000 def enabled(self): - return (self.pyboy.cartridge_title() == "POKEMON RED") or (self.pyboy.cartridge_title() == "POKEMON BLUE") + return (self.pyboy.cartridge_title == "POKEMON RED") or (self.pyboy.cartridge_title == "POKEMON BLUE") def post_tick(self): self._tile_cache_invalid = True diff --git a/pyboy/plugins/screen_recorder.py b/pyboy/plugins/screen_recorder.py index 8f38b5a14..68fd6b41b 100644 --- a/pyboy/plugins/screen_recorder.py +++ b/pyboy/plugins/screen_recorder.py @@ -55,7 +55,7 @@ def save(self, path=None, fps=60): directory = os.path.join(os.path.curdir, "recordings") if not os.path.exists(directory): os.makedirs(directory, mode=0o755) - path = os.path.join(directory, time.strftime(f"{self.pyboy.cartridge_title()}-%Y.%m.%d-%H.%M.%S.gif")) + path = os.path.join(directory, time.strftime(f"{self.pyboy.cartridge_title}-%Y.%m.%d-%H.%M.%S.gif")) if len(self.frames) > 0: self.frames[0].save( diff --git a/pyboy/plugins/screenshot_recorder.py b/pyboy/plugins/screenshot_recorder.py index b72cd4517..2cb37fd9d 100644 --- a/pyboy/plugins/screenshot_recorder.py +++ b/pyboy/plugins/screenshot_recorder.py @@ -35,7 +35,7 @@ def save(self, path=None): directory = os.path.join(os.path.curdir, "screenshots") if not os.path.exists(directory): os.makedirs(directory, mode=0o755) - path = os.path.join(directory, time.strftime(f"{self.pyboy.cartridge_title()}-%Y.%m.%d-%H.%M.%S.png")) + path = os.path.join(directory, time.strftime(f"{self.pyboy.cartridge_title}-%Y.%m.%d-%H.%M.%S.png")) self.pyboy.screen.image.save(path) diff --git a/pyboy/pyboy.pxd b/pyboy/pyboy.pxd index e7e7509e1..7d95fb2e3 100644 --- a/pyboy/pyboy.pxd +++ b/pyboy/pyboy.pxd @@ -55,6 +55,7 @@ cdef class PyBoy: cdef readonly TileMap tilemap_window cdef readonly object game_wrapper cdef readonly MemoryScanner memory_scanner + cdef readonly str cartridge_title cdef bint limit_emulationspeed cdef int emulationspeed, target_emulationspeed, save_target_emulationspeed diff --git a/pyboy/pyboy.py b/pyboy/pyboy.py index f23f33186..7cfff6b08 100644 --- a/pyboy/pyboy.py +++ b/pyboy/pyboy.py @@ -190,6 +190,17 @@ def __init__( Object for handling plugins in PyBoy """ + self.cartridge_title = self.mb.cartridge.gamename + """ + The title stored on the currently loaded cartridge ROM. The title is all upper-case ASCII and may + have been truncated to 11 characters. + + Returns + ------- + str : + Game title + """ + self.game_wrapper = self._plugin_manager.gamewrapper() """ Provides an instance of a game-specific or generic wrapper. The game is detected by the cartridge's hard-coded @@ -627,18 +638,6 @@ def set_emulation_speed(self, target_speed): logger.warning("The emulation speed might not be accurate when speed-target is higher than 5") self.target_emulationspeed = target_speed - def cartridge_title(self): - """ - Get the title stored on the currently loaded cartridge ROM. The title is all upper-case ASCII and may - have been truncated to 11 characters. - - Returns - ------- - str : - Game title - """ - return self.mb.cartridge.gamename - def __rendering(self, value): """ Disable or enable rendering diff --git a/tests/test_game_wrapper_mario.py b/tests/test_game_wrapper_mario.py index 1e2765363..238628cb2 100644 --- a/tests/test_game_wrapper_mario.py +++ b/tests/test_game_wrapper_mario.py @@ -19,7 +19,7 @@ def test_mario_basics(supermarioland_rom): pyboy = PyBoy(supermarioland_rom, window_type="null") pyboy.set_emulation_speed(0) - assert pyboy.cartridge_title() == "SUPER MARIOLAN" + assert pyboy.cartridge_title == "SUPER MARIOLAN" mario = pyboy.game_wrapper mario.start_game(world_level=(1, 1)) @@ -35,7 +35,7 @@ def test_mario_basics(supermarioland_rom): def test_mario_advanced(supermarioland_rom): pyboy = PyBoy(supermarioland_rom, window_type="null") pyboy.set_emulation_speed(0) - assert pyboy.cartridge_title() == "SUPER MARIOLAN" + assert pyboy.cartridge_title == "SUPER MARIOLAN" mario = pyboy.game_wrapper mario.start_game(world_level=(3, 2)) diff --git a/tests/test_replay.py b/tests/test_replay.py index 0bbdf158a..e3eb78ef6 100644 --- a/tests/test_replay.py +++ b/tests/test_replay.py @@ -142,7 +142,7 @@ def replay( recording ^= True if gif_destination: - move_gif(pyboy.cartridge_title(), gif_destination) + move_gif(pyboy.cartridge_title, gif_destination) if gif_hash is not None and not overwrite and sys.platform == "darwin": verify_file_hash(gif_destination, gif_hash) diff --git a/tests/test_states.py b/tests/test_states.py index 6d4042ac5..d7ebd9742 100644 --- a/tests/test_states.py +++ b/tests/test_states.py @@ -12,7 +12,7 @@ def test_load_save_consistency(tetris_rom): pyboy = PyBoy(tetris_rom, window_type="null") - assert pyboy.cartridge_title() == "TETRIS" + assert pyboy.cartridge_title == "TETRIS" pyboy.set_emulation_speed(0) pyboy.memory[NEXT_TETROMINO_ADDR] diff --git a/tests/test_tetris_ai.py b/tests/test_tetris_ai.py index 57252de9c..8ce64afee 100644 --- a/tests/test_tetris_ai.py +++ b/tests/test_tetris_ai.py @@ -131,7 +131,7 @@ def eval_network(epoch, child_index, child_model, record_to): directory = os.path.join(os.path.curdir, "recordings") if not os.path.exists(directory): os.makedirs(directory, mode=0o755) - path = os.path.join(directory, time.strftime(f"{pyboy.cartridge_title()}-%Y.%m.%d-%H.%M.%S.gif")) + path = os.path.join(directory, time.strftime(f"{pyboy.cartridge_title}-%Y.%m.%d-%H.%M.%S.gif")) frames[0].save( path,