Skip to content

Commit

Permalink
Made resource registration more relaxed
Browse files Browse the repository at this point in the history
  • Loading branch information
M-J-Murray committed Nov 6, 2018
1 parent 265f904 commit f8059ed
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
16 changes: 11 additions & 5 deletions MAMEToolkit/emulator/Console.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,25 @@ def readAll(self, timeout=0.5):
break
return lines

def writeln(self, command, expect_output=False, timeout=0.5):
def writeln(self, command, expect_output=False, timeout=0.5, raiseError=True):
self.process.stdin.write(command.encode("utf-8") + b'\n')
self.process.stdin.flush()
output = self.readAll(timeout=timeout)

if expect_output and len(output) == 0:
error = "Expected output but received nothing from emulator after '" + command + "'"
self.logger.error(error)
raise IOError(error)
if raiseError:
self.logger.error(error)
raise IOError(error)
else:
return None
if not expect_output and len(output) > 0:
error = "No output expected from command '" + command + "', but recieved: " + "\n".join(output)
self.logger.error(error)
raise IOError(error)
if raiseError:
self.logger.error(error)
raise IOError(error)
else:
return None
if expect_output:
return output

Expand Down
12 changes: 9 additions & 3 deletions MAMEToolkit/emulator/Emulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,20 @@ def create_lua_variables(self):
self.console.writeln('mem = manager:machine().devices[":maincpu"].spaces["program"]')
self.console.writeln('releaseQueue = {}')

def wait_for_resource_registration(self):
def wait_for_resource_registration(self, max_attempts=10):
screen_registered = False
program_registered = False
attempt = 0
while not screen_registered or not program_registered:
if not screen_registered:
screen_registered = self.console.writeln('print(manager:machine().screens[":screen"])', expect_output=True, timeout=3) is not "nil"
result = self.console.writeln('print(manager:machine().screens[":screen"])', expect_output=True, timeout=3, raiseError=False)
screen_registered = result is not None and result is not "nil"
if not program_registered:
program_registered = self.console.writeln('print(manager:machine().devices[":maincpu"].spaces["program"])', expect_output=True, timeout=3) is not "nil"
result = self.console.writeln('print(manager:machine().devices[":maincpu"].spaces["program"])', expect_output=True, timeout=3, raiseError=False)
program_registered = result is not None and result is not "nil"
if attempt == max_attempts:
raise EnvironmentError("Failed to register MAME resources!")
attempt += 1

# Gets the game screen width in pixels
def setup_screen_width(self):
Expand Down

0 comments on commit f8059ed

Please sign in to comment.