Quake linux console support test 1
This commit is contained in:
parent
09d8099a2f
commit
18ebd785e2
1 changed files with 65 additions and 12 deletions
|
|
@ -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:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue