From a1627a8795592efcdf0210386b1f2c9437e81c2e Mon Sep 17 00:00:00 2001 From: Hexiro <42787085+Hexiro@users.noreply.github.com> Date: Thu, 8 Sep 2022 10:45:23 -0400 Subject: [PATCH] refactor: use more modular system for working with / adding plugins --- pyboy/plugins/auto_pause.py | 2 +- pyboy/plugins/base_plugin.pxd | 2 +- pyboy/plugins/base_plugin.py | 27 +-- pyboy/plugins/debug.py | 4 +- pyboy/plugins/disable_input.py | 2 +- pyboy/plugins/manager.pxd | 17 +- pyboy/plugins/manager.py | 278 +++++++++------------------ pyboy/plugins/manager_gen.py | 187 ------------------ pyboy/plugins/record_replay.py | 4 +- pyboy/plugins/rewind.py | 2 +- pyboy/plugins/screen_recorder.py | 2 +- pyboy/plugins/screenshot_recorder.py | 2 +- pyboy/plugins/window_dummy.py | 4 +- pyboy/plugins/window_headless.py | 4 +- pyboy/plugins/window_open_gl.py | 4 +- pyboy/plugins/window_sdl2.py | 4 +- 16 files changed, 128 insertions(+), 417 deletions(-) delete mode 100644 pyboy/plugins/manager_gen.py diff --git a/pyboy/plugins/auto_pause.py b/pyboy/plugins/auto_pause.py index 656ef73f3..5b7166e86 100644 --- a/pyboy/plugins/auto_pause.py +++ b/pyboy/plugins/auto_pause.py @@ -17,5 +17,5 @@ def handle_events(self, events): events.append(WindowEvent.UNPAUSE) return events - def enabled(self): + def _enabled(self): return self.pyboy_argv.get("autopause") diff --git a/pyboy/plugins/base_plugin.pxd b/pyboy/plugins/base_plugin.pxd index fdcd6eca1..9858fb749 100644 --- a/pyboy/plugins/base_plugin.pxd +++ b/pyboy/plugins/base_plugin.pxd @@ -24,7 +24,7 @@ cdef class PyBoyPlugin: cdef void post_tick(self) cdef str window_title(self) cdef void stop(self) - cpdef bint enabled(self) + cpdef bint _enabled(self) cdef class PyBoyWindowPlugin(PyBoyPlugin): diff --git a/pyboy/plugins/base_plugin.py b/pyboy/plugins/base_plugin.py index 56da6c80f..c531ac050 100644 --- a/pyboy/plugins/base_plugin.py +++ b/pyboy/plugins/base_plugin.py @@ -23,6 +23,7 @@ try: from cython import compiled + cythonmode = compiled except ImportError: cythonmode = False @@ -38,11 +39,13 @@ def __init__(self, pyboy, mb, pyboy_argv): self.pyboy = pyboy self.mb = mb self.pyboy_argv = pyboy_argv + self.enabled = self._enabled() def __cinit__(self, pyboy, mb, pyboy_argv, *args, **kwargs): self.pyboy = pyboy self.mb = mb self.pyboy_argv = pyboy_argv + self.enabled = self._enabled() def handle_events(self, events): return events @@ -56,7 +59,7 @@ def window_title(self): def stop(self): pass - def enabled(self): + def _enabled(self): return True @@ -64,7 +67,7 @@ class PyBoyWindowPlugin(PyBoyPlugin): def __init__(self, pyboy, mb, pyboy_argv, *args, **kwargs): super().__init__(pyboy, mb, pyboy_argv, *args, **kwargs) - if not self.enabled(): + if not self.enabled: return scale = pyboy_argv.get("scale") @@ -107,7 +110,7 @@ def __init__(self, *args, game_area_section=(0, 0, 32, 32), game_area_wrap_aroun self.game_area_wrap_around = game_area_wrap_around width = self.game_area_section[2] - self.game_area_section[0] height = self.game_area_section[3] - self.game_area_section[1] - self._cached_game_area_tiles_raw = array("B", [0xFF] * (width*height*4)) + self._cached_game_area_tiles_raw = array("B", [0xFF] * (width * height * 4)) self.saved_state = io.BytesIO() @@ -115,9 +118,9 @@ 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)) else: v = memoryview(self._cached_game_area_tiles_raw).cast("I") - self._cached_game_area_tiles = [v[i:i + height] for i in range(0, height * width, height)] + self._cached_game_area_tiles = [v[i : i + height] for i in range(0, height * width, height)] - def enabled(self): + def _enabled(self): return self.pyboy_argv.get("game_wrapper") and self.pyboy.cartridge_title() == self.cartridge_title def post_tick(self): @@ -194,15 +197,15 @@ def _game_area_tiles(self): if self.game_area_wrap_around: self._cached_game_area_tiles = np.ndarray(shape=(height, width), dtype=np.uint32) for y in range(height): - SCX = scanline_parameters[(yy+y) * 8][0] // 8 - SCY = scanline_parameters[(yy+y) * 8][1] // 8 + SCX = scanline_parameters[(yy + y) * 8][0] // 8 + SCY = scanline_parameters[(yy + y) * 8][1] // 8 for x in range(width): - _x = (xx+x+SCX) % 32 - _y = (yy+y+SCY) % 32 + _x = (xx + x + SCX) % 32 + _y = (yy + y + SCY) % 32 self._cached_game_area_tiles[y][x] = self.tilemap_background.tile_identifier(_x, _y) else: self._cached_game_area_tiles = np.asarray( - self.tilemap_background[xx:xx + width, yy:yy + height], dtype=np.uint32 + self.tilemap_background[xx : xx + width, yy : yy + height], dtype=np.uint32 ) self._tile_cache_invalid = False return self._cached_game_area_tiles @@ -251,7 +254,7 @@ def _game_area_np(self, observation_type="tiles"): def _sum_number_on_screen(self, x, y, length, blank_tile_identifier, tile_identifier_offset): number = 0 - for i, x in enumerate(self.tilemap_background[x:x + length, y]): + for i, x in enumerate(self.tilemap_background[x : x + length, y]): if x != blank_tile_identifier: - number += (x+tile_identifier_offset) * (10**(length - 1 - i)) + number += (x + tile_identifier_offset) * (10 ** (length - 1 - i)) return number diff --git a/pyboy/plugins/debug.py b/pyboy/plugins/debug.py index 30d7eebed..e6a3b39f7 100644 --- a/pyboy/plugins/debug.py +++ b/pyboy/plugins/debug.py @@ -85,7 +85,7 @@ class Debug(PyBoyWindowPlugin): def __init__(self, pyboy, mb, pyboy_argv): super().__init__(pyboy, mb, pyboy_argv) - if not self.enabled(): + if not self.enabled: return self.cgb = mb.cgb @@ -256,7 +256,7 @@ def stop(self): if self.sdl2_event_pump: sdl2.SDL_Quit() - def enabled(self): + def _enabled(self): if self.pyboy_argv.get("debug"): if not sdl2: logger.error("Failed to import sdl2, needed for debug window") diff --git a/pyboy/plugins/disable_input.py b/pyboy/plugins/disable_input.py index 1e59cce58..49f56c9a9 100644 --- a/pyboy/plugins/disable_input.py +++ b/pyboy/plugins/disable_input.py @@ -11,5 +11,5 @@ class DisableInput(PyBoyPlugin): def handle_events(self, events): return [] - def enabled(self): + def _enabled(self): return self.pyboy_argv.get("no_input") diff --git a/pyboy/plugins/manager.pxd b/pyboy/plugins/manager.pxd index 8ad523071..634fa89b1 100644 --- a/pyboy/plugins/manager.pxd +++ b/pyboy/plugins/manager.pxd @@ -19,8 +19,8 @@ from pyboy.plugins.screenshot_recorder cimport ScreenshotRecorder from pyboy.plugins.game_wrapper_super_mario_land cimport GameWrapperSuperMarioLand from pyboy.plugins.game_wrapper_tetris cimport GameWrapperTetris from pyboy.plugins.game_wrapper_kirby_dream_land cimport GameWrapperKirbyDreamLand -# imports end from pyboy.plugins.base_plugin cimport PyBoyPlugin, PyBoyWindowPlugin +# imports end @@ -42,22 +42,9 @@ cdef class PluginManager: cdef public GameWrapperSuperMarioLand game_wrapper_super_mario_land cdef public GameWrapperTetris game_wrapper_tetris cdef public GameWrapperKirbyDreamLand game_wrapper_kirby_dream_land - cdef bint window_sdl2_enabled - cdef bint window_open_gl_enabled - cdef bint window_headless_enabled - cdef bint window_dummy_enabled - cdef bint debug_enabled - cdef bint disable_input_enabled - cdef bint auto_pause_enabled - cdef bint record_replay_enabled - cdef bint rewind_enabled - cdef bint screen_recorder_enabled - cdef bint screenshot_recorder_enabled - cdef bint game_wrapper_super_mario_land_enabled - cdef bint game_wrapper_tetris_enabled - cdef bint game_wrapper_kirby_dream_land_enabled # plugin_cdef end + cdef void add_plugin(self, PyBoyPlugin plugin) cdef list handle_events(self, list) cdef void post_tick(self) cdef void _post_tick_windows(self) diff --git a/pyboy/plugins/manager.py b/pyboy/plugins/manager.py index 20ec1b8e9..d10012b29 100644 --- a/pyboy/plugins/manager.py +++ b/pyboy/plugins/manager.py @@ -4,20 +4,24 @@ # # imports -from pyboy.plugins.window_sdl2 import WindowSDL2 # isort:skip -from pyboy.plugins.window_open_gl import WindowOpenGL # isort:skip -from pyboy.plugins.window_headless import WindowHeadless # isort:skip -from pyboy.plugins.window_dummy import WindowDummy # isort:skip -from pyboy.plugins.debug import Debug # isort:skip -from pyboy.plugins.disable_input import DisableInput # isort:skip -from pyboy.plugins.auto_pause import AutoPause # isort:skip -from pyboy.plugins.record_replay import RecordReplay # isort:skip -from pyboy.plugins.rewind import Rewind # isort:skip -from pyboy.plugins.screen_recorder import ScreenRecorder # isort:skip -from pyboy.plugins.screenshot_recorder import ScreenshotRecorder # isort:skip -from pyboy.plugins.game_wrapper_super_mario_land import GameWrapperSuperMarioLand # isort:skip -from pyboy.plugins.game_wrapper_tetris import GameWrapperTetris # isort:skip -from pyboy.plugins.game_wrapper_kirby_dream_land import GameWrapperKirbyDreamLand # isort:skip +from pyboy.plugins.window_sdl2 import WindowSDL2 # isort:skip +from pyboy.plugins.window_open_gl import WindowOpenGL # isort:skip +from pyboy.plugins.window_headless import WindowHeadless # isort:skip +from pyboy.plugins.window_dummy import WindowDummy # isort:skip +from pyboy.plugins.debug import Debug # isort:skip +from pyboy.plugins.disable_input import DisableInput # isort:skip +from pyboy.plugins.auto_pause import AutoPause # isort:skip +from pyboy.plugins.record_replay import RecordReplay # isort:skip +from pyboy.plugins.rewind import Rewind # isort:skip +from pyboy.plugins.screen_recorder import ScreenRecorder # isort:skip +from pyboy.plugins.screenshot_recorder import ScreenshotRecorder # isort:skip +from pyboy.plugins.base_plugin import PyBoyGameWrapper # isort:skip +from pyboy.plugins.base_plugin import PyBoyPlugin # isort:skip +from pyboy.plugins.base_plugin import PyBoyWindowPlugin # isort: skip +from pyboy.plugins.game_wrapper_super_mario_land import GameWrapperSuperMarioLand # isort:skip +from pyboy.plugins.game_wrapper_tetris import GameWrapperTetris # isort:skip +from pyboy.plugins.game_wrapper_kirby_dream_land import GameWrapperKirbyDreamLand # isort:skip + # imports end @@ -44,104 +48,92 @@ def parser_arguments(): class PluginManager: def __init__(self, pyboy, mb, pyboy_argv): self.pyboy = pyboy - # plugins_enabled self.window_sdl2 = WindowSDL2(pyboy, mb, pyboy_argv) - self.window_sdl2_enabled = self.window_sdl2.enabled() self.window_open_gl = WindowOpenGL(pyboy, mb, pyboy_argv) - self.window_open_gl_enabled = self.window_open_gl.enabled() self.window_headless = WindowHeadless(pyboy, mb, pyboy_argv) - self.window_headless_enabled = self.window_headless.enabled() self.window_dummy = WindowDummy(pyboy, mb, pyboy_argv) - self.window_dummy_enabled = self.window_dummy.enabled() self.debug = Debug(pyboy, mb, pyboy_argv) - self.debug_enabled = self.debug.enabled() self.disable_input = DisableInput(pyboy, mb, pyboy_argv) - self.disable_input_enabled = self.disable_input.enabled() self.auto_pause = AutoPause(pyboy, mb, pyboy_argv) - self.auto_pause_enabled = self.auto_pause.enabled() self.record_replay = RecordReplay(pyboy, mb, pyboy_argv) - self.record_replay_enabled = self.record_replay.enabled() self.rewind = Rewind(pyboy, mb, pyboy_argv) - self.rewind_enabled = self.rewind.enabled() self.screen_recorder = ScreenRecorder(pyboy, mb, pyboy_argv) - self.screen_recorder_enabled = self.screen_recorder.enabled() self.screenshot_recorder = ScreenshotRecorder(pyboy, mb, pyboy_argv) - self.screenshot_recorder_enabled = self.screenshot_recorder.enabled() + self.game_wrapper_super_mario_land = GameWrapperSuperMarioLand(pyboy, mb, pyboy_argv) - self.game_wrapper_super_mario_land_enabled = self.game_wrapper_super_mario_land.enabled() self.game_wrapper_tetris = GameWrapperTetris(pyboy, mb, pyboy_argv) - self.game_wrapper_tetris_enabled = self.game_wrapper_tetris.enabled() self.game_wrapper_kirby_dream_land = GameWrapperKirbyDreamLand(pyboy, mb, pyboy_argv) - self.game_wrapper_kirby_dream_land_enabled = self.game_wrapper_kirby_dream_land.enabled() + + # all plugins + self.plugins: list[PyBoyPlugin] = [ + self.window_sdl2, + self.window_open_gl, + self.window_headless, + self.window_dummy, + self.debug, + self.disable_input, + self.auto_pause, + self.record_replay, + self.rewind, + self.screen_recorder, + self.screenshot_recorder, + self.game_wrapper_super_mario_land, + self.game_wrapper_tetris, + self.game_wrapper_kirby_dream_land, + ] + + # only window plugins + self.window_plugins: list[PyBoyWindowPlugin] = [ + self.window_sdl2, + self.window_open_gl, + self.window_headless, + self.window_dummy, + self.debug, + ] + + # only game wrappers + self.game_wrappers: list[PyBoyGameWrapper] = [ + self.game_wrapper_super_mario_land, + self.game_wrapper_tetris, + self.game_wrapper_kirby_dream_land, + ] + + self.active_game_wrapper: PyBoyGameWrapper | None = None + # plugins_enabled end + def add_plugin(self, plugin): + if isinstance(plugin, PyBoyGameWrapper): + self.game_wrappers.append(plugin) + elif isinstance(plugin, PyBoyWindowPlugin): + self.window_plugins.append(plugin) + self.plugins.append(plugin) + def gamewrapper(self): # gamewrapper - if self.game_wrapper_super_mario_land_enabled: - return self.game_wrapper_super_mario_land - if self.game_wrapper_tetris_enabled: - return self.game_wrapper_tetris - if self.game_wrapper_kirby_dream_land_enabled: - return self.game_wrapper_kirby_dream_land + if self.active_game_wrapper: + return self.active_game_wrapper + for game_wrapper in self.game_wrappers: + if game_wrapper.enabled: + self.active_game_wrapper = game_wrapper + return game_wrapper # gamewrapper end return None def handle_events(self, events): # foreach windows events = [].handle_events(events) - if self.window_sdl2_enabled: - events = self.window_sdl2.handle_events(events) - if self.window_open_gl_enabled: - events = self.window_open_gl.handle_events(events) - if self.window_headless_enabled: - events = self.window_headless.handle_events(events) - if self.window_dummy_enabled: - events = self.window_dummy.handle_events(events) - if self.debug_enabled: - events = self.debug.handle_events(events) - # foreach end - # foreach plugins events = [].handle_events(events) - if self.disable_input_enabled: - events = self.disable_input.handle_events(events) - if self.auto_pause_enabled: - events = self.auto_pause.handle_events(events) - if self.record_replay_enabled: - events = self.record_replay.handle_events(events) - if self.rewind_enabled: - events = self.rewind.handle_events(events) - if self.screen_recorder_enabled: - events = self.screen_recorder.handle_events(events) - if self.screenshot_recorder_enabled: - events = self.screenshot_recorder.handle_events(events) - if self.game_wrapper_super_mario_land_enabled: - events = self.game_wrapper_super_mario_land.handle_events(events) - if self.game_wrapper_tetris_enabled: - events = self.game_wrapper_tetris.handle_events(events) - if self.game_wrapper_kirby_dream_land_enabled: - events = self.game_wrapper_kirby_dream_land.handle_events(events) + for plugin in self.plugins: + if plugin.enabled: + events = plugin.handle_events(events) # foreach end return events def post_tick(self): # foreach plugins [].post_tick() - if self.disable_input_enabled: - self.disable_input.post_tick() - if self.auto_pause_enabled: - self.auto_pause.post_tick() - if self.record_replay_enabled: - self.record_replay.post_tick() - if self.rewind_enabled: - self.rewind.post_tick() - if self.screen_recorder_enabled: - self.screen_recorder.post_tick() - if self.screenshot_recorder_enabled: - self.screenshot_recorder.post_tick() - if self.game_wrapper_super_mario_land_enabled: - self.game_wrapper_super_mario_land.post_tick() - if self.game_wrapper_tetris_enabled: - self.game_wrapper_tetris.post_tick() - if self.game_wrapper_kirby_dream_land_enabled: - self.game_wrapper_kirby_dream_land.post_tick() + for plugin in self.plugins: + if plugin.enabled: + plugin.post_tick() # foreach end self._post_tick_windows() @@ -149,131 +141,47 @@ def post_tick(self): def _set_title(self): # foreach windows [].set_title(self.pyboy.window_title) - if self.window_sdl2_enabled: - self.window_sdl2.set_title(self.pyboy.window_title) - if self.window_open_gl_enabled: - self.window_open_gl.set_title(self.pyboy.window_title) - if self.window_headless_enabled: - self.window_headless.set_title(self.pyboy.window_title) - if self.window_dummy_enabled: - self.window_dummy.set_title(self.pyboy.window_title) - if self.debug_enabled: - self.debug.set_title(self.pyboy.window_title) + for window_plugin in self.window_plugins: + if window_plugin.enabled: + window_plugin.set_title(self.pyboy.window_title) # foreach end pass def _post_tick_windows(self): # foreach windows [].post_tick() - if self.window_sdl2_enabled: - self.window_sdl2.post_tick() - if self.window_open_gl_enabled: - self.window_open_gl.post_tick() - if self.window_headless_enabled: - self.window_headless.post_tick() - if self.window_dummy_enabled: - self.window_dummy.post_tick() - if self.debug_enabled: - self.debug.post_tick() + for window_plugin in self.window_plugins: + if window_plugin.enabled: + window_plugin.post_tick() # foreach end - pass def frame_limiter(self, speed): if speed <= 0: return # foreach windows done = [].frame_limiter(speed), if done: return - if self.window_sdl2_enabled: - done = self.window_sdl2.frame_limiter(speed) - if done: - return - if self.window_open_gl_enabled: - done = self.window_open_gl.frame_limiter(speed) - if done: - return - if self.window_headless_enabled: - done = self.window_headless.frame_limiter(speed) - if done: - return - if self.window_dummy_enabled: - done = self.window_dummy.frame_limiter(speed) - if done: - return - if self.debug_enabled: - done = self.debug.frame_limiter(speed) - if done: - return + for window_plugin in self.window_plugins: + if window_plugin.enabled: + done = window_plugin.frame_limiter(speed) + if done: + return # foreach end def window_title(self): title = "" # foreach windows title += [].window_title() - if self.window_sdl2_enabled: - title += self.window_sdl2.window_title() - if self.window_open_gl_enabled: - title += self.window_open_gl.window_title() - if self.window_headless_enabled: - title += self.window_headless.window_title() - if self.window_dummy_enabled: - title += self.window_dummy.window_title() - if self.debug_enabled: - title += self.debug.window_title() - # foreach end - # foreach plugins title += [].window_title() - if self.disable_input_enabled: - title += self.disable_input.window_title() - if self.auto_pause_enabled: - title += self.auto_pause.window_title() - if self.record_replay_enabled: - title += self.record_replay.window_title() - if self.rewind_enabled: - title += self.rewind.window_title() - if self.screen_recorder_enabled: - title += self.screen_recorder.window_title() - if self.screenshot_recorder_enabled: - title += self.screenshot_recorder.window_title() - if self.game_wrapper_super_mario_land_enabled: - title += self.game_wrapper_super_mario_land.window_title() - if self.game_wrapper_tetris_enabled: - title += self.game_wrapper_tetris.window_title() - if self.game_wrapper_kirby_dream_land_enabled: - title += self.game_wrapper_kirby_dream_land.window_title() + for plugin in self.plugins: + if plugin.enabled: + title += plugin.window_title() # foreach end return title def stop(self): # foreach windows [].stop() - if self.window_sdl2_enabled: - self.window_sdl2.stop() - if self.window_open_gl_enabled: - self.window_open_gl.stop() - if self.window_headless_enabled: - self.window_headless.stop() - if self.window_dummy_enabled: - self.window_dummy.stop() - if self.debug_enabled: - self.debug.stop() - # foreach end - # foreach plugins [].stop() - if self.disable_input_enabled: - self.disable_input.stop() - if self.auto_pause_enabled: - self.auto_pause.stop() - if self.record_replay_enabled: - self.record_replay.stop() - if self.rewind_enabled: - self.rewind.stop() - if self.screen_recorder_enabled: - self.screen_recorder.stop() - if self.screenshot_recorder_enabled: - self.screenshot_recorder.stop() - if self.game_wrapper_super_mario_land_enabled: - self.game_wrapper_super_mario_land.stop() - if self.game_wrapper_tetris_enabled: - self.game_wrapper_tetris.stop() - if self.game_wrapper_kirby_dream_land_enabled: - self.game_wrapper_kirby_dream_land.stop() + for plugin in self.plugins: + if plugin.enabled: + plugin.stop() # foreach end pass def handle_breakpoint(self): - if self.debug_enabled: + if self.debug.enabled: self.debug.handle_breakpoint() diff --git a/pyboy/plugins/manager_gen.py b/pyboy/plugins/manager_gen.py deleted file mode 100644 index 4710abdf4..000000000 --- a/pyboy/plugins/manager_gen.py +++ /dev/null @@ -1,187 +0,0 @@ -# -# License: See LICENSE.md file -# GitHub: https://github.com/Baekalfen/PyBoy -# -import re - -# Plugins and priority! -# E.g. DisableInput first -windows = ["WindowSDL2", "WindowOpenGL", "WindowHeadless", "WindowDummy", "Debug"] -game_wrappers = ["GameWrapperSuperMarioLand", "GameWrapperTetris", "GameWrapperKirbyDreamLand"] -plugins = [ - "DisableInput", "AutoPause", "RecordReplay", "Rewind", "ScreenRecorder", "ScreenshotRecorder" -] + game_wrappers -all_plugins = windows + plugins - - -def to_snake_case(s): - s1 = re.sub("(.)([A-Z][a-z]+)", r"\1_\2", s) - return re.sub("([a-z0-9])([A-Z])", r"\1_\2", s1).lower() - - -def skip_lines(iterator, stop): - # Skip old lines - while True: - if next(line_iter).strip().startswith(stop): - break - - -if __name__ == "__main__": - out_lines = [] - with open("manager.py", "r") as f: - line_iter = iter(f.readlines()) - while True: - line = next(line_iter, None) - if line is None: - break - - # Find place to inject - if line.strip().startswith("# foreach"): - - lines = [line.strip() + "\n"] - indentation = " " * line.index("# foreach") - - skip_lines(line_iter, "# foreach end") - - _, foreach, plugin_type, fun = line.strip().split(" ", 3) - for p in eval(plugin_type): - p_name = to_snake_case(p) - lines.append(f"if self.{p_name}_enabled:\n") - # lines.append(f" {var_name} = self.{p_name}\n") - for sub_fun in fun.split(", "): - sub_fun = sub_fun.replace("[]", f"self.{p_name}") - lines.append(f" {sub_fun}\n") - - lines.append("# foreach end\n") - out_lines.extend([indentation + l for l in lines]) - elif line.strip().startswith("# plugins_enabled"): - - lines = [line.strip() + "\n"] - indentation = " " * line.index("# plugins_enabled") - - skip_lines(line_iter, "# plugins_enabled end") - - for p in all_plugins: - p_name = to_snake_case(p) - lines.append(f"self.{p_name} = {p}(pyboy, mb, pyboy_argv)\n") - lines.append(f"self.{p_name}_enabled = self.{p_name}.enabled()\n") - - lines.append("# plugins_enabled end\n") - out_lines.extend([indentation + l for l in lines]) - elif line.strip().startswith("# yield_plugins"): - - lines = [line.strip() + "\n"] - indentation = " " * line.index("# yield_plugins") - - skip_lines(line_iter, "# yield_plugins end") - - for p in all_plugins: - p_name = to_snake_case(p) - lines.append(f"yield {p}.argv\n") - - lines.append("# yield_plugins end\n") - out_lines.extend([indentation + l for l in lines]) - elif line.strip().startswith("# imports"): - - lines = [line.strip() + "\n"] - indentation = " " * line.index("# imports") - - skip_lines(line_iter, "# imports end") - - for p in all_plugins: - p_name = to_snake_case(p) - lines.append(f"from pyboy.plugins.{p_name} import {p} # isort:skip\n") - - lines.append("# imports end\n") - out_lines.extend([indentation + l for l in lines]) - elif line.strip().startswith("# gamewrapper"): - - lines = [line.strip() + "\n"] - indentation = " " * line.index("# gamewrapper") - - skip_lines(line_iter, "# gamewrapper end") - - for p in game_wrappers: - p_name = to_snake_case(p) - lines.append(f"if self.{p_name}_enabled: return self.{p_name}\n") - - lines.append("# gamewrapper end\n") - out_lines.extend([indentation + l for l in lines]) - else: - out_lines.append(line) - - with open("manager.py", "w") as f: - f.writelines(out_lines) - - out_lines = [] - with open("manager.pxd", "r") as f: - line_iter = iter(f.readlines()) - while True: - line = next(line_iter, None) - if line is None: - break - - # Find place to inject - if line.strip().startswith("# plugin_cdef"): - - lines = [line.strip() + "\n"] - indentation = " " * line.index("# plugin_cdef") - - skip_lines(line_iter, "# plugin_cdef end") - - for p in all_plugins: - p_name = to_snake_case(p) - lines.append(f"cdef public {p} {p_name}\n") - - for p in all_plugins: - p_name = to_snake_case(p) - lines.append(f"cdef bint {p_name}_enabled\n") - - lines.append("# plugin_cdef end\n") - out_lines.extend([indentation + l for l in lines]) - elif line.strip().startswith("# imports"): - - lines = [line.strip() + "\n"] - indentation = " " * line.index("# imports") - - skip_lines(line_iter, "# imports end") - - for p in all_plugins: - p_name = to_snake_case(p) - lines.append(f"from pyboy.plugins.{p_name} cimport {p}\n") - - lines.append("# imports end\n") - out_lines.extend([indentation + l for l in lines]) - else: - out_lines.append(line) - - with open("manager.pxd", "w") as f: - f.writelines(out_lines) - - out_lines = [] - with open("__init__.py", "r") as f: - line_iter = iter(f.readlines()) - while True: - line = next(line_iter, None) - if line is None: - break - - # Find place to inject - if line.strip().startswith("# docs exclude"): - - lines = [line.strip() + "\n"] - indentation = " " * line.index("# docs exclude") - - skip_lines(line_iter, "# docs exclude end") - - for p in (set(all_plugins) - set(game_wrappers)) | set(["manager", "manager_gen"]): - p_name = to_snake_case(p) - lines.append(f"\"{p_name}\": False,\n") - - lines.append("# docs exclude end\n") - out_lines.extend([indentation + l for l in lines]) - else: - out_lines.append(line) - - with open("__init__.py", "w") as f: - f.writelines(out_lines) diff --git a/pyboy/plugins/record_replay.py b/pyboy/plugins/record_replay.py index 778aeb4e7..c3bc294f6 100644 --- a/pyboy/plugins/record_replay.py +++ b/pyboy/plugins/record_replay.py @@ -22,7 +22,7 @@ class RecordReplay(PyBoyPlugin): def __init__(self, *args): super().__init__(*args) - if not self.enabled(): + if not self.enabled: return if not self.pyboy_argv.get("loadstate"): @@ -52,7 +52,7 @@ def stop(self): self.recorded_input, ) - def enabled(self): + def _enabled(self): return self.pyboy_argv.get("record_input") diff --git a/pyboy/plugins/rewind.py b/pyboy/plugins/rewind.py index cc3eae197..58f9992f7 100644 --- a/pyboy/plugins/rewind.py +++ b/pyboy/plugins/rewind.py @@ -73,7 +73,7 @@ def handle_events(self, events): self.pyboy.set_emulation_speed(int(self.rewind_speed)) return events - def enabled(self): + def _enabled(self): return self.pyboy_argv.get("rewind") diff --git a/pyboy/plugins/screen_recorder.py b/pyboy/plugins/screen_recorder.py index 07965c353..d12cd256a 100644 --- a/pyboy/plugins/screen_recorder.py +++ b/pyboy/plugins/screen_recorder.py @@ -72,7 +72,7 @@ def save(self, path=None, fps=60): logger.error("Screen recording failed: no frames") self.frames = [] - def enabled(self): + def _enabled(self): if Image is None: logger.warning(f"{__name__}: Missing dependency \"Pillow\". Recording disabled") return False diff --git a/pyboy/plugins/screenshot_recorder.py b/pyboy/plugins/screenshot_recorder.py index d1d8e0b50..a822d7b50 100644 --- a/pyboy/plugins/screenshot_recorder.py +++ b/pyboy/plugins/screenshot_recorder.py @@ -41,7 +41,7 @@ def save(self, path=None): logger.info("Screenshot saved in {}".format(path)) - def enabled(self): + def _enabled(self): if Image is None: logger.warning(f"{__name__}: Missing dependency \"Pillow\". Screenshots disabled") return False diff --git a/pyboy/plugins/window_dummy.py b/pyboy/plugins/window_dummy.py index efed0f5cb..c478f9ef1 100644 --- a/pyboy/plugins/window_dummy.py +++ b/pyboy/plugins/window_dummy.py @@ -14,7 +14,7 @@ class WindowDummy(PyBoyWindowPlugin): def __init__(self, pyboy, mb, pyboy_argv): super().__init__(pyboy, mb, pyboy_argv) - if not self.enabled(): + if not self.enabled: return pyboy._rendering(False) @@ -22,7 +22,7 @@ def __init__(self, pyboy, mb, pyboy_argv): 'This window type does not support frame-limiting. `pyboy.set_emulation_speed(...)` will have no effect, as it\'s always running at full speed.' ) - def enabled(self): + def _enabled(self): return self.pyboy_argv.get("window_type") == "dummy" def set_title(self, title): diff --git a/pyboy/plugins/window_headless.py b/pyboy/plugins/window_headless.py index 4fce9b7e7..74f941b7e 100644 --- a/pyboy/plugins/window_headless.py +++ b/pyboy/plugins/window_headless.py @@ -14,14 +14,14 @@ class WindowHeadless(PyBoyWindowPlugin): def __init__(self, pyboy, mb, pyboy_argv): super().__init__(pyboy, mb, pyboy_argv) - if not self.enabled(): + if not self.enabled: return logger.warning( 'This window type does not support frame-limiting. `pyboy.set_emulation_speed(...)` will have no effect, as it\'s always running at full speed.' ) - def enabled(self): + def _enabled(self): return self.pyboy_argv.get("window_type") == "headless" def set_title(self, title): diff --git a/pyboy/plugins/window_open_gl.py b/pyboy/plugins/window_open_gl.py index 7d4e6406d..6149dee02 100644 --- a/pyboy/plugins/window_open_gl.py +++ b/pyboy/plugins/window_open_gl.py @@ -35,7 +35,7 @@ class WindowOpenGL(PyBoyWindowPlugin): def __init__(self, pyboy, mb, pyboy_argv): super().__init__(pyboy, mb, pyboy_argv) - if not self.enabled(): + if not self.enabled: return if not glutInit(): @@ -141,7 +141,7 @@ def _gldraw(self): glDrawPixels(COLS, ROWS, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, buf) glFlush() - def enabled(self): + def _enabled(self): if self.pyboy_argv.get("window_type") == "OpenGL": if opengl_enabled: return True diff --git a/pyboy/plugins/window_sdl2.py b/pyboy/plugins/window_sdl2.py index a0c80195d..f51e00680 100644 --- a/pyboy/plugins/window_sdl2.py +++ b/pyboy/plugins/window_sdl2.py @@ -153,7 +153,7 @@ class WindowSDL2(PyBoyWindowPlugin): def __init__(self, pyboy, mb, pyboy_argv): super().__init__(pyboy, mb, pyboy_argv) - if not self.enabled(): + if not self.enabled: return sdl2.SDL_Init(sdl2.SDL_INIT_VIDEO | sdl2.SDL_INIT_GAMECONTROLLER) @@ -193,7 +193,7 @@ def post_tick(self): sdl2.SDL_RenderPresent(self._sdlrenderer) sdl2.SDL_RenderClear(self._sdlrenderer) - def enabled(self): + def _enabled(self): if self.pyboy_argv.get("window_type") in ("SDL2", None): if not sdl2: logger.error("Failed to import sdl2, needed for sdl2 window")