Skip to content

Commit f839e43

Browse files
committed
check 64213 first, then 23074, then fallback to 8080
1 parent 07a47b8 commit f839e43

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
PyYAML>=3.13
2-
py2snes>=1.0.2
2+
py2snes>=1.0.2
3+
psutil

romloader.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,19 @@
55

66
import yaml
77
import py2snes
8+
import psutil
89

910
import asyncio
11+
import socket
12+
1013

1114
def 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+
1721
sys.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+
136156
def 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+
143168
if __name__ == '__main__':
144169
loop = asyncio.get_event_loop()
145170
loop.run_until_complete(main())

0 commit comments

Comments
 (0)