55
66import yaml
77import py2snes
8+ import psutil
89
910import asyncio
11+ import socket
12+
1013
1114def show_exception_and_exit (exc_type , exc_value , tb ):
1215 import traceback
1316 traceback .print_exception (exc_type , exc_value , tb )
1417 input ("Press key to exit." )
1518 sys .exit (- 1 )
1619
20+
1721sys .excepthook = show_exception_and_exit
1822
1923
@@ -24,7 +28,8 @@ def show_exception_and_exit(exc_type, exc_value, tb):
2428 with open (scriptpath + "\\ romloader.yaml" ) as configfile :
2529 try :
2630 config = yaml .load (configfile )
27- print ("loading config file at " + os .path .abspath (scriptpath + "\\ romloader.yaml" ))
31+ print ("loading config file at " +
32+ os .path .abspath (scriptpath + "\\ romloader.yaml" ))
2833 except yaml .YAMLError as e :
2934 print (e )
3035 sys .exit (1 )
@@ -33,7 +38,8 @@ def show_exception_and_exit(exc_type, exc_value, tb):
3338 with open ("romloader.yaml" ) as configfile :
3439 try :
3540 config = yaml .load (configfile )
36- print ("loading config file at " + os .path .abspath ("romloader.yaml" ))
41+ print ("loading config file at " +
42+ os .path .abspath ("romloader.yaml" ))
3743 except yaml .YAMLError as e :
3844 print (e )
3945 sys .exit (1 )
@@ -74,7 +80,20 @@ async def main():
7480
7581 # initiate connection to the websocket server
7682 snes = py2snes .snes ()
77- await snes .connect ()
83+
84+ a_socket = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
85+
86+ if is_open (64213 ):
87+ address = 'ws://localhost:64213'
88+ elif is_open (23074 ):
89+ address = 'ws://localhost:23074'
90+ elif is_open (8080 ):
91+ address = 'ws://localhost:8080'
92+ else :
93+ raise Exception (
94+ 'Unable to connect to a suitable port! Please ensure qusb2nes is listening on 64213, 23074, or 8080!' )
95+
96+ await snes .connect (address = address )
7897
7998 devicelist = await snes .DeviceList ()
8099
@@ -133,13 +152,19 @@ def get_destination(rule, romname):
133152 name = romname
134153 return path , name
135154
155+
136156def get_comm_device (devicelist ):
137157 print ('----------------------------' )
138158 for idx , device in enumerate (devicelist ):
139159 print (str (idx ) + ' - ' + device )
140160 dst_idx = int (input ('What device? ' ))
141161 return devicelist [dst_idx ]
142162
163+
164+ def is_open (port : int ):
165+ return port in [i .laddr .port for i in psutil .net_connections ()]
166+
167+
143168if __name__ == '__main__' :
144169 loop = asyncio .get_event_loop ()
145170 loop .run_until_complete (main ())
0 commit comments