def test_base_should_be_able_to_discover_installed_plugins_for_a_specified_pluggable_with_selection_passed_along( ): offshoot.config["allow"]["config"] = False offshoot.config["allow"]["libraries"] = False offshoot.config["allow"]["callbacks"] = False offshoot.config["modules"].append("pluggable") TestPlugin.install() class_mapping = offshoot.discover("TestPluggable", selection="123") assert isinstance(class_mapping, dict) assert len(class_mapping) == 0 class_mapping = offshoot.discover("TestPluggable", selection=["123"]) assert isinstance(class_mapping, dict) assert len(class_mapping) == 0 class_mapping = offshoot.discover( "TestPluggable", selection=["TestPluginPluggableExpected"]) assert isinstance(class_mapping, dict) assert len(class_mapping) == 1 assert "TestPluginPluggableExpected" in class_mapping assert inspect.isclass(class_mapping["TestPluginPluggableExpected"]) TestPlugin.uninstall() offshoot.config["allow"]["config"] = True offshoot.config["allow"]["libraries"] = True offshoot.config["allow"]["callbacks"] = True
async def onJoin(self, details): self.redis_client = await self._initialize_redis_client() game_class_name = await self.redis_client.get("SERPENT:GAME") game_class = offshoot.discover("Game")[game_class_name.decode("utf-8")] game = game_class() game.launch(dry_run=True) backend_string = config["input_controller"]["backend"] backend = InputControllers.NATIVE_WIN32 if is_windows( ) else InputControllers.PYAUTOGUI if backend_string != "DEFAULT": try: backend = InputControllers[backend_string] except KeyError: pass self.input_controller = InputController(game=game, backend=backend) while True: payload = await self.redis_client.brpop( config["input_controller"]["redis_key"]) payload = pickle.loads(payload[1]) input_controller_func_string, *args, kwargs = payload getattr(self.input_controller, input_controller_func_string)(*args, **kwargs) await asyncio.sleep(0.01)
def play(self, game_agent_class_name=None): if not self.is_launched: raise GameError(f"Game '{self.__class__.__name__}' is not running...") game_agent_class = offshoot.discover("GameAgent").get(game_agent_class_name) if game_agent_class is None: raise GameError("The provided Game Agent class name does not map to an existing class...") game_agent = game_agent_class( game=self, input_controller=InputController(game_window_id=self.window_id) ) self.start_frame_grabber() time.sleep(1) subprocess.call(shlex.split(f"xdotool windowactivate {self.window_id}")) while True: frame = self.grab_latest_frame() try: game_agent.on_frame(frame) except Exception as e: raise e time.sleep(0.1)
def initialize_game(game_name): game_class_name = f"Serpent{game_name}Game" game_class_mapping = offshoot.discover("Game") game_class = game_class_mapping.get(game_class_name) if game_class is None: raise Exception( f"Game '{game_name}' wasn't found. Make sure the plugin is installed." ) game = game_class() return game
def play(game_name, game_agent_name, frame_handler=None): game = initialize_game(game_name) game.launch(dry_run=True) game_agent_class_mapping = offshoot.discover("GameAgent", selection=game_agent_name) game_agent_class = game_agent_class_mapping.get(game_agent_name) if game_agent_class is None: raise Exception( f"Game Agent '{game_agent_name}' wasn't found. Make sure the plugin is installed." ) game.play(game_agent_class_name=game_agent_name, frame_handler=frame_handler)
def play(self, game_agent_class_name=None, frame_handler=None, **kwargs): if not self.is_launched: raise GameError( f"Game '{self.__class__.__name__}' is not running...") game_agent_class = offshoot.discover("GameAgent").get( game_agent_class_name, GameAgent) if game_agent_class is None: raise GameError( "The provided Game Agent class name does not map to an existing class..." ) game_agent = game_agent_class( game=self, input_controller=InputController(game=self)) self.start_frame_grabber() self.redis_client.delete(config["frame_grabber"]["redis_key"]) while self.redis_client.llen( config["frame_grabber"]["redis_key"]) == 0: time.sleep(0.1) self.window_controller.focus_window(self.window_id) while True: self.game_frame_limiter.start() game_frame = self.grab_latest_frame() try: if self.is_focused: game_agent.on_game_frame(game_frame, frame_handler=frame_handler, **kwargs) else: serpent.utilities.clear_terminal() print("PAUSED\n") game_agent.on_pause(frame_handler=frame_handler, **kwargs) time.sleep(1) except Exception as e: raise e # print(e) # time.sleep(0.1) self.game_frame_limiter.stop_and_delay()
def play(self, game_agent_class_name=None): if not self.is_launched: raise GameError( f"Game '{self.__class__.__name__}' is not running...") game_agent_class = offshoot.discover("GameAgent").get( game_agent_class_name) if game_agent_class is None: raise GameError( "The provided Game Agent class name does not map to an existing class..." ) game_agent = game_agent_class( game=self, input_controller=InputController(game_window_id=self.window_id)) self.start_frame_grabber() self.redis_client.delete(config["frame_grabber"]["redis_key"]) while self.redis_client.llen( config["frame_grabber"]["redis_key"]) == 0: time.sleep(0.1) subprocess.call( shlex.split(f"xdotool windowactivate {self.window_id}")) while True: self.game_frame_limiter.start() game_frame = self.grab_latest_frame() try: if self.is_focused: game_agent.on_game_frame(game_frame) else: subprocess.call(["clear"]) print("PAUSED") time.sleep(1) except Exception as e: raise e # print(e) # time.sleep(0.1) self.game_frame_limiter.stop_and_delay()
def play(self, game_agent_class_name=None): if not self.is_launched: raise GameError(f"Game '{self.__class__.__name__}' is not running...") game_agent_class = offshoot.discover("GameAgent").get(game_agent_class_name) if game_agent_class is None: raise GameError("The provided Game Agent class name does not map to an existing class...") game_agent = game_agent_class( game=self, input_controller=InputController(game_window_id=self.window_id) ) self.start_frame_grabber() self.redis_client.delete(config["frame_grabber"]["redis_key"]) while self.redis_client.llen(config["frame_grabber"]["redis_key"]) == 0: time.sleep(0.1) subprocess.call(shlex.split(f"xdotool windowactivate {self.window_id}")) while True: self.game_frame_limiter.start() game_frame = self.grab_latest_frame() try: if self.is_focused: game_agent.on_game_frame(game_frame) else: subprocess.call(["clear"]) print("PAUSED") time.sleep(1) except Exception as e: raise e # print(e) # time.sleep(0.1) self.game_frame_limiter.stop_and_delay()
def test_base_should_be_able_to_discover_installed_plugins_for_a_specified_pluggable( ): offshoot.config["allow"]["config"] = False offshoot.config["allow"]["libraries"] = False offshoot.config["allow"]["callbacks"] = False offshoot.config["modules"].append("pluggable") TestPlugin.install() class_mapping = offshoot.discover("TestPluggable", globals()) assert isinstance(class_mapping, dict) assert len(class_mapping) == 0 assert inspect.isclass(TestPluginPluggableExpected) TestPlugin.uninstall() offshoot.config["allow"]["config"] = True offshoot.config["allow"]["libraries"] = True offshoot.config["allow"]["callbacks"] = True
def play(self, game_agent_class_name="GameAgent", frame_handler=None, **kwargs): if not self.is_launched: raise GameError( f"Game '{self.__class__.__name__}' is not running...") self.start_crossbar() time.sleep(3) self.start_input_controller() game_agent_class = offshoot.discover( "GameAgent", selection=game_agent_class_name).get(game_agent_class_name, GameAgent) if game_agent_class is None: raise GameError( "The provided Game Agent class name does not map to an existing class..." ) game_agent = game_agent_class(game=self, input_controller=InputController( game=self, backend=self.input_controller), **kwargs) # Look if we need to auto-append PNG to frame transformation pipeline based on given frame_handler png_frame_handlers = ["RECORD"] if frame_handler in png_frame_handlers and self.frame_transformation_pipeline_string is not None: if not self.frame_transformation_pipeline_string.endswith("|PNG"): self.frame_transformation_pipeline_string += "|PNG" self.start_frame_grabber() self.redis_client.delete(config["frame_grabber"]["redis_key"]) while self.redis_client.llen( config["frame_grabber"]["redis_key"]) == 0: time.sleep(0.1) self.window_controller.focus_window(self.window_id) # Override FPS Config? if frame_handler == "RECORD": self.game_frame_limiter = GameFrameLimiter(fps=10) try: while True: self.game_frame_limiter.start() game_frame, game_frame_pipeline = self.grab_latest_frame() try: if self.is_focused: self.pause_callback_fired = False game_agent.on_game_frame(game_frame, game_frame_pipeline, frame_handler=frame_handler, **kwargs) else: if not self.pause_callback_fired: print("PAUSED\n") game_agent.on_pause(frame_handler=frame_handler, **kwargs) self.pause_callback_fired = True time.sleep(1) except Exception as e: raise e # print(e) # time.sleep(0.1) self.game_frame_limiter.stop_and_delay() except Exception as e: raise e finally: self.stop_frame_grabber() self.stop_input_controller() self.stop_crossbar()
import offshoot offshoot.discover("Game", globals())
import offshoot offshoot.discover("GameAgent", globals())
#!/usr/bin/env python import os import sys import shutil import subprocess import shlex import offshoot from serpent.utilities import * # from serpent.utilities import clear_terminal, display_serpent_logo # Add the current working directory to sys.path to discover user plugins! sys.path.insert(0, os.getcwd()) game_class_mapping = offshoot.discover("Game") game_agent_class_mapping = offshoot.discover("GameAgent") VERSION = "0.1.0b1" valid_commands = [ "setup", "grab_frames", "launch", "play", "generate", "activate", "deactivate", "plugins", "train", "capture",
from shapes.square import Square # Discover plugins import offshoot offshoot.discover("Shape", globals())