Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modular Plugin Manager #234

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyboy/plugins/auto_pause.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
2 changes: 1 addition & 1 deletion pyboy/plugins/base_plugin.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
27 changes: 15 additions & 12 deletions pyboy/plugins/base_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

try:
from cython import compiled

cythonmode = compiled
except ImportError:
cythonmode = False
Expand All @@ -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
Expand All @@ -56,15 +59,15 @@ def window_title(self):
def stop(self):
pass

def enabled(self):
def _enabled(self):
return True


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")
Expand Down Expand Up @@ -107,17 +110,17 @@ 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()

if cythonmode:
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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions pyboy/plugins/debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion pyboy/plugins/disable_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
17 changes: 2 additions & 15 deletions pyboy/plugins/manager.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -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



Expand All @@ -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)
Expand Down
Loading