<reponame>Sparus42/SerpentAI
from serpent.utilities import is_linux, is_macos, is_windows


class WindowControllerError(BaseException):
    pass


class WindowController:

    def __init__(self):
        self.adapter = self._load_adapter()()

    def locate_window(self, name):
        return self.adapter.locate_window(name)

    def move_window(self, window_id, x, y):
        self.adapter.move_window(window_id, x, y)

    def resize_window(self, window_id, width, height):
        self.adapter.resize_window(window_id, width, height)

    def focus_window(self, window_id):
        self.adapter.focus_window(window_id)

    def is_window_focused(self, window_id):
        return self.adapter.is_window_focused(window_id)

    def get_focused_window_name(self):
        return self.adapter.get_focused_window_name()

    def get_window_geometry(self, window_id):
        return self.adapter.get_window_geometry(window_id)

    def read_memory(self, address, size=4, pointer_offsets=None, add_base=True, allow_partial=False):
        '''Currently only implemented on Windows.

        -Inputs-
        Address: Hex value, find using Cheat Engine
        Size: Number of bytes, usually 4 but if not just copy whatever works in CE
        Pointer Offsets: A list of hex memory pointer offsets, find using Cheat Engine. If empty, will assume address is not a pointer
        Add Base: Adds the base address of the program. Used to update the address when game is restarted, will almost always be true
        Allow Partial: Doesn't throw an error if the memory can only be paritially read

        -Output-
        Value: Binary representation of the memory's value, use int.from_bytes() or similar method to convert
        '''

        return self.adapter.read_memory(address, size, pointer_offsets, add_base, allow_partial)

    def write_memory(self, value, address, size=4, pointer_offsets=None, add_base=True, allow_partial=False):
        '''Currently only implemented on Windows.

        -Inputs-
        Value: Binary representation of the memory's value, use int.to_bytes() or similar method to convert
        Address: Hex value, find using Cheat Engine
        Size: Number of bytes, usually 4 but if not just copy whatever works in CE
        Pointer Offsets: A list of hex memory pointer offsets, find using Cheat Engine. If empty, will assume address is not a pointer
        Add Base: Adds the base address of the program. Used to update the address when game is restarted, will almost always be true
        Allow Partial: Doesn't throw an error if the memory can only be paritially written
        '''

        self.adapter.write_memory(value, address, size, pointer_offsets, add_base, allow_partial)

    def _load_adapter(self):
        if is_linux():
            from serpent.window_controllers.linux_window_controller import LinuxWindowController
            return LinuxWindowController
        elif is_macos():
            from serpent.window_controllers.darwin_window_controller import DarwinWindowController
            return DarwinWindowController
        elif is_windows():
            from serpent.window_controllers.win32_window_controller import Win32WindowController
            return Win32WindowController

from serpent.utilities import is_linux, is_macos, is_windows


class WindowControllerError(BaseException):
    pass


class WindowController:

    def __init__(self):
        self.adapter = self._load_adapter()()

    def locate_window(self, name):
        return self.adapter.locate_window(name)

    def move_window(self, window_id, x, y):
        self.adapter.move_window(window_id, x, y)

    def resize_window(self, window_id, width, height):
        self.adapter.resize_window(window_id, width, height)

    def focus_window(self, window_id):
        self.adapter.focus_window(window_id)

    def is_window_focused(self, window_id):
        return self.adapter.is_window_focused(window_id)

    def get_focused_window_name(self):
        return self.adapter.get_focused_window_name()

    def get_window_geometry(self, window_id):
        return self.adapter.get_window_geometry(window_id)

    def read_memory(self, address, size=4, pointer_offsets=None, add_base=True, allow_partial=False):
        '''Currently only implemented on Windows.

        -Inputs-
        Address: Hex value, find using Cheat Engine
        Size: Number of bytes, usually 4 but if not just copy whatever works in CE
        Pointer Offsets: A list of hex memory pointer offsets, find using Cheat Engine. If empty, will assume address is not a pointer
        Add Base: Adds the base address of the program. Used to update the address when game is restarted, will almost always be true
        Allow Partial: Doesn't throw an error if the memory can only be paritially read

        -Output-
        Value: Binary representation of the memory's value, use int.from_bytes() or similar method to convert
        '''

        return self.adapter.read_memory(address, size, pointer_offsets, add_base, allow_partial)

    def write_memory(self, value, address, size=4, pointer_offsets=None, add_base=True, allow_partial=False):
        '''Currently only implemented on Windows.

        -Inputs-
        Value: Binary representation of the memory's value, use int.to_bytes() or similar method to convert
        Address: Hex value, find using Cheat Engine
        Size: Number of bytes, usually 4 but if not just copy whatever works in CE
        Pointer Offsets: A list of hex memory pointer offsets, find using Cheat Engine. If empty, will assume address is not a pointer
        Add Base: Adds the base address of the program. Used to update the address when game is restarted, will almost always be true
        Allow Partial: Doesn't throw an error if the memory can only be paritially written
        '''

        self.adapter.write_memory(value, address, size, pointer_offsets, add_base, allow_partial)

    def _load_adapter(self):
        if is_linux():
            from serpent.window_controllers.linux_window_controller import LinuxWindowController
            return LinuxWindowController
        elif is_macos():
            from serpent.window_controllers.darwin_window_controller import DarwinWindowController
            return DarwinWindowController
        elif is_windows():
            from serpent.window_controllers.win32_window_controller import Win32WindowController
            return Win32WindowController
Currently only implemented on Windows.          -Inputs-         Address: Hex value, find using Cheat Engine         Size: Number of bytes, usually 4 but if not just copy whatever works in CE         Pointer Offsets: A list of hex memory pointer offsets, find using Cheat Engine. If empty, will assume address is not a pointer         Add Base: Adds the base address of the program. Used to update the address when game is restarted, will almost always be true         Allow Partial: Doesn't throw an error if the memory can only be paritially read          -Output-         Value: Binary representation of the memory's value, use int.from_bytes() or similar method to convert Currently only implemented on Windows.          -Inputs-         Value: Binary representation of the memory's value, use int.to_bytes() or similar method to convert         Address: Hex value, find using Cheat Engine         Size: Number of bytes, usually 4 but if not just copy whatever works in CE         Pointer Offsets: A list of hex memory pointer offsets, find using Cheat Engine. If empty, will assume address is not a pointer         Add Base: Adds the base address of the program. Used to update the address when game is restarted, will almost always be true         Allow Partial: Doesn't throw an error if the memory can only be paritially written