From c3c9da94037bd81ad66c902ffefa2f7d7a84fe0b Mon Sep 17 00:00:00 2001 From: Storm Dragon Date: Wed, 22 Jan 2025 14:18:18 -0500 Subject: [PATCH] Back to trying to fix non-English encodings. --- Toby Doom Launcher.py | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/Toby Doom Launcher.py b/Toby Doom Launcher.py index 05f7ec5..78348c9 100755 --- a/Toby Doom Launcher.py +++ b/Toby Doom Launcher.py @@ -249,14 +249,23 @@ class SpeechHandler: # Read directly from stdout, no need for buffer attribute for line in process.stdout: try: - # Handle encoding by trying utf-8 first, then fallback to latin1 - try: - if isinstance(line, bytes): - lineStr = line.decode('utf-8').strip() - else: - lineStr = line.strip() - except UnicodeDecodeError: - lineStr = line.decode('latin1').strip() + # Try multiple encodings in order + encodings = ['utf-8', 'gbk', 'big5', 'latin1'] + lineStr = None + + for encoding in encodings: + try: + if isinstance(line, bytes): + lineStr = line.decode(encoding).strip() + else: + lineStr = line.strip() + break # Successfully decoded + except UnicodeDecodeError: + continue + + if lineStr is None: + print(f"Failed to decode line with any encoding", file=sys.stderr) + continue # Keep gzdoom's existing functionality of lines being printed to the console print(lineStr) @@ -269,7 +278,10 @@ class SpeechHandler: processedLine = self.process_line(lineStr) if processedLine: - self.speak(processedLine) + try: + self.speak(processedLine) + except Exception as e: + print(f"Error speaking line: {e}", file=sys.stderr) except Exception as e: print(f"Error processing line: {e}", file=sys.stderr)