From 1ab102b26e0ff963554e2e1960269720da70b279 Mon Sep 17 00:00:00 2001 From: Jeremy Burns Date: Sun, 15 Aug 2021 17:00:05 -0600 Subject: [PATCH] Handle input process closing without crashing (unplugged controller) --- input_reader_win.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/input_reader_win.py b/input_reader_win.py index 81cfb90..b138d8d 100644 --- a/input_reader_win.py +++ b/input_reader_win.py @@ -12,7 +12,10 @@ def enqueue_output(exe_path): proc = Popen([exe_path], shell=True, stdout=PIPE) g_queue = Queue() while not g_shouldStop: - g_queue.put(proc.stdout.readline().decode('utf-8')) + if proc.poll() is None: + g_queue.put(proc.stdout.readline().decode('utf-8')) + else: + g_shouldStop = True proc.stdout.close() proc.kill() g_running = False @@ -40,9 +43,11 @@ def sample_input_reader(mario_inputs): has_line = True line = g_queue.get() if not has_line: + _sample_empty_inputs(mario_inputs) return vals = [int(x) for x in line.split()] if len(vals) < 5: + _sample_empty_inputs(mario_inputs) return mario_inputs.stickX = _read_axis(float(vals[0])) mario_inputs.stickY = _read_axis(float(vals[1])) @@ -50,6 +55,13 @@ def sample_input_reader(mario_inputs): mario_inputs.buttonB = vals[3] != 0 mario_inputs.buttonZ = vals[4] != 0 +def _sample_empty_inputs(mario_inputs): + mario_inputs.stickX = 0.0 + mario_inputs.stickY = 0.0 + mario_inputs.buttonA = False + mario_inputs.buttonB = False + mario_inputs.buttonZ = False + def _read_axis(val): val /= 32768.0 if val < 0.2 and val > -0.2: