From f839e4374dcf7881b1a55e420aa464584fcdcf15 Mon Sep 17 00:00:00 2001 From: Thomas Prescott Date: Sun, 30 Aug 2020 10:05:44 -0500 Subject: [PATCH] check 64213 first, then 23074, then fallback to 8080 --- requirements.txt | 3 ++- romloader.py | 31 ++++++++++++++++++++++++++++--- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/requirements.txt b/requirements.txt index 00bbcce..9775374 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ PyYAML>=3.13 -py2snes>=1.0.2 \ No newline at end of file +py2snes>=1.0.2 +psutil \ No newline at end of file diff --git a/romloader.py b/romloader.py index 9d7770f..560404c 100644 --- a/romloader.py +++ b/romloader.py @@ -5,8 +5,11 @@ import yaml import py2snes +import psutil import asyncio +import socket + def show_exception_and_exit(exc_type, exc_value, tb): import traceback @@ -14,6 +17,7 @@ def show_exception_and_exit(exc_type, exc_value, tb): input("Press key to exit.") sys.exit(-1) + sys.excepthook = show_exception_and_exit @@ -24,7 +28,8 @@ def show_exception_and_exit(exc_type, exc_value, tb): with open(scriptpath + "\\romloader.yaml") as configfile: try: config = yaml.load(configfile) - print("loading config file at " + os.path.abspath(scriptpath + "\\romloader.yaml")) + print("loading config file at " + + os.path.abspath(scriptpath + "\\romloader.yaml")) except yaml.YAMLError as e: print(e) sys.exit(1) @@ -33,7 +38,8 @@ def show_exception_and_exit(exc_type, exc_value, tb): with open("romloader.yaml") as configfile: try: config = yaml.load(configfile) - print("loading config file at " + os.path.abspath("romloader.yaml")) + print("loading config file at " + + os.path.abspath("romloader.yaml")) except yaml.YAMLError as e: print(e) sys.exit(1) @@ -74,7 +80,20 @@ async def main(): # initiate connection to the websocket server snes = py2snes.snes() - await snes.connect() + + a_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + + if is_open(64213): + address = 'ws://localhost:64213' + elif is_open(23074): + address = 'ws://localhost:23074' + elif is_open(8080): + address = 'ws://localhost:8080' + else: + raise Exception( + 'Unable to connect to a suitable port! Please ensure qusb2nes is listening on 64213, 23074, or 8080!') + + await snes.connect(address=address) devicelist = await snes.DeviceList() @@ -133,6 +152,7 @@ def get_destination(rule, romname): name = romname return path, name + def get_comm_device(devicelist): print('----------------------------') for idx, device in enumerate(devicelist): @@ -140,6 +160,11 @@ def get_comm_device(devicelist): dst_idx = int(input('What device? ')) return devicelist[dst_idx] + +def is_open(port: int): + return port in [i.laddr.port for i in psutil.net_connections()] + + if __name__ == '__main__': loop = asyncio.get_event_loop() loop.run_until_complete(main())