From 18ebd785e2821b63bf265cba77318306d27d7323 Mon Sep 17 00:00:00 2001 From: edschuy95 Date: Tue, 20 Jan 2026 20:31:57 -0500 Subject: [PATCH] Quake linux console support test 1 --- RA3MP3Playback.py | 77 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 65 insertions(+), 12 deletions(-) diff --git a/RA3MP3Playback.py b/RA3MP3Playback.py index 2ed83d3..f9921b4 100644 --- a/RA3MP3Playback.py +++ b/RA3MP3Playback.py @@ -1,4 +1,5 @@ from pathlib import Path +import pty import threading import time import os @@ -348,6 +349,30 @@ def change_mode(): send_command(pty_proc,f"echo _musicmode {current_mode}") playlist = load_playlist(playlist_path) +def monitor_game_pty(master_fd): + buffer = b"" + + while not stop_flag.is_set(): + try: + data = os.read(master_fd, 1024) + except OSError: + break + + if not data: + break + + buffer += data + + while b"\n" in buffer: + line, buffer = buffer.split(b"\n", 1) + line = strip_ansi(line.decode(errors="ignore")).strip() + if line: + if "--debug" in sys.argv: + print(f"[GAME] {line}", flush=True) + if "--rawdebug" in sys.argv: + print(f"[GAME RAW] {repr(line)}", flush=True) + handle_game_line(line, pty_proc) + def monitor_game(proc): #subprocess version global serverstatus_sent @@ -489,8 +514,11 @@ def handle_game_line(line, pty_proc): def send_command(proc, cmd): #subprocess version try: - proc.stdin.write((cmd + "\r\n").encode()) - proc.stdin.flush() + if os.name == "nt": + proc.stdin.write((cmd + "\r\n").encode()) + proc.stdin.flush() + else: + os.write(master_fd, (cmd + "\n").encode()) except Exception as e: print(f"[DEBUG] Failed to send command: {e}") # ========================================================== @@ -523,19 +551,44 @@ def main(): watcher_thread = threading.Thread(target=track_watcher, daemon=True) watcher_thread.start() - # # Launch quake process via subprocess - pty_proc = subprocess.Popen( - [game_exe, "+set", "ttycon", "1"] + sys.argv[1:], - stdin=subprocess.PIPE, - stdout=subprocess.PIPE, - stderr=subprocess.STDOUT, - bufsize=0, # unbuffered - universal_newlines=False -) + # # # Launch quake process via subprocess + # pty_proc = subprocess.Popen( + # [game_exe, "+set", "ttycon", "1"] + sys.argv[1:], + # stdin=subprocess.PIPE, + # stdout=subprocess.PIPE, + # stderr=subprocess.STDOUT, + # bufsize=0, # unbuffered + # universal_newlines=False) + + if os.name == "nt": + pty_proc = subprocess.Popen( + [game_exe, "+set", "ttycon", "1"] + sys.argv[1:], + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + bufsize=0, + universal_newlines=False + ) + master_fd = None + else: + master_fd, slave_fd = pty.openpty() + + pty_proc = subprocess.Popen( + [game_exe, "+set", "ttycon", "1"] + sys.argv[1:], + stdin=slave_fd, + stdout=slave_fd, + stderr=slave_fd, + close_fds=True + ) + + os.close(slave_fd) # Monitor the game output try: - monitor_game(pty_proc) + if os.name == "nt": + monitor_game(pty_proc) + else: + monitor_game_pty(master_fd) except KeyboardInterrupt: print("Exiting...") finally: