From 0b04060f9945fc6d2d8db2d9e825b65461505a15 Mon Sep 17 00:00:00 2001 From: Emma Lysne Date: Mon, 3 Feb 2025 00:24:55 -0800 Subject: [PATCH 1/3] Temporarily fix compatibility --- nuclearesrpc.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/nuclearesrpc.py b/nuclearesrpc.py index 8b689a1..f69bb49 100644 --- a/nuclearesrpc.py +++ b/nuclearesrpc.py @@ -5,6 +5,7 @@ import pypresence import subprocess from typing import Dict +import ctypes VARIABLE_TYPES = { @@ -13,7 +14,7 @@ "GENERATOR_1_KW": float, "GENERATOR_2_KW": float, "CORE_IMMINENT_FUSION": str, - "RODS_POS_ORDERED": float +# "RODS_POS_ORDERED": float } @@ -26,13 +27,17 @@ def get_all_vars(srv_url: str) -> Dict[str, float | str]: """ results = {} for key, typeof in VARIABLE_TYPES.items(): + print(f"Getting key {key}, looking for type {typeof}") res = requests.get( srv_url, { "Variable": key } ) - results[key] = typeof(res.text) + try: + results[key] = typeof(res.text) + except ValueError: + print(f"Conversion of '{res.text}' to type {typeof} failed") return results @@ -54,6 +59,7 @@ def find_nucleares() -> psutil.Process | None: game_exec = subprocess.Popen(sys.argv[1]) +ctypes.windll.user32.MessageBoxW(0, "Remember to turn on the WebServer, else NuclearesRPC will not work!", "Reminder!", 64) cid = 1331101603649818786 presence = pypresence.Presence(cid, pipe=0) print("Locating running Nucleares executable...") @@ -95,11 +101,11 @@ def find_nucleares() -> psutil.Process | None: status = "Generator Offline" if dvars["CORE_IMMINENT_FUSION"] == "TRUE": details = "Imminent Meltdown" - if (dvars["CORE_TEMP"] == 20 and dvars["RODS_POS_ORDERED"] == 87.5) or mission: - # I can give you my complete assurance that my work will be back to normal~ - mission = True - details = "This mission is too important" - status = "The intruder must be dealt with" + #if (dvars["CORE_TEMP"] == 20 and dvars["RODS_POS_ORDERED"] == 87.5) or mission: + # # I can give you my complete assurance that my work will be back to normal~ + # mission = True + # details = "This mission is too important" + # status = "The intruder must be dealt with" presence.update( pid=proc.pid, start=round(starttime), From 5e43af9539ddf82d69208b6703e5f82312b3293f Mon Sep 17 00:00:00 2001 From: Emma Lysne Date: Mon, 3 Feb 2025 14:13:17 -0800 Subject: [PATCH 2/3] Add argparsing, debug logging, internal versioning --- .gitignore | 5 +++- nuclearesrpc.py | 71 +++++++++++++++++++++++++++++++++++------------ requirements.txt | Bin 0 -> 562 bytes 3 files changed, 57 insertions(+), 19 deletions(-) create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore index 7c05345..ec768e5 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,7 @@ venv/ # Ignore build files build/ dist/ -nuclearesrpc.spec \ No newline at end of file +nuclearesrpc.spec + +# Ignore log files +*.log \ No newline at end of file diff --git a/nuclearesrpc.py b/nuclearesrpc.py index f69bb49..9a6b897 100644 --- a/nuclearesrpc.py +++ b/nuclearesrpc.py @@ -4,8 +4,34 @@ import requests import pypresence import subprocess -from typing import Dict +import argparse +import logging +import traceback import ctypes +from typing import Dict + + +__version__ = "v2.0.0" +parser = argparse.ArgumentParser( + prog="Nucleares Presence Client", + description="Nucleares Rich Presence Client for Discord", + epilog=f"Version {__version__}" +) +parser.add_argument("-v", "--version", action="store_true") +parser.add_argument("-d", "--debug", action="store_true") +params = parser.parse_args() +logging.basicConfig( + level="DEBUG" if params.debug else "INFO", + filename="debug.log" if params.debug else None +) + + +if params.version: + print("Nucleares Rich Presence Client") + print(f"Client Version: {__version__}") + print(f"PyPresence Version: {pypresence.__version__}") + input("Press Enter to exit...") + sys.exit(0) VARIABLE_TYPES = { @@ -27,7 +53,7 @@ def get_all_vars(srv_url: str) -> Dict[str, float | str]: """ results = {} for key, typeof in VARIABLE_TYPES.items(): - print(f"Getting key {key}, looking for type {typeof}") + logging.debug(f"Getting key {key}, looking for type {typeof}") res = requests.get( srv_url, { @@ -37,7 +63,7 @@ def get_all_vars(srv_url: str) -> Dict[str, float | str]: try: results[key] = typeof(res.text) except ValueError: - print(f"Conversion of '{res.text}' to type {typeof} failed") + logging.error(f"Conversion of '{res.text}' to type {typeof} failed") return results @@ -54,24 +80,24 @@ def find_nucleares() -> psutil.Process | None: if len(sys.argv) > 1: - if sys.argv[1].endswith("Nucleares.exe"): - # Client is launching through steam, we are expected to launch it on Steam's behalf - game_exec = subprocess.Popen(sys.argv[1]) - + for obj in sys.argv: + if obj.endswith("Nucleares.exe"): + # Client is launching through steam, we are expected to launch it on Steam's behalf + game_exec = subprocess.Popen(obj) ctypes.windll.user32.MessageBoxW(0, "Remember to turn on the WebServer, else NuclearesRPC will not work!", "Reminder!", 64) cid = 1331101603649818786 presence = pypresence.Presence(cid, pipe=0) -print("Locating running Nucleares executable...") +logging.info("Locating running Nucleares executable...") proc = find_nucleares() while proc is None: proc = find_nucleares() time.sleep(5) -print("Found: " + str(proc.pid)) +logging.debug("Found: " + str(proc.pid)) starttime = time.time() -print("Looking for webserver...") +logging.info("Waiting for webserver...") port = "8785" url = "http://localhost:" + port + "/" while 1: @@ -81,12 +107,12 @@ def find_nucleares() -> psutil.Process | None: break except requests.ConnectionError as e: time.sleep(5) -print("Webserver is live, firing up RPC...") +logging.info("Webserver is live, firing up RPC...") mission = False presence.connect() -print("Connected. Press Ctrl+C to Exit") +logging.info("Connected. Press Ctrl+C to Exit") while 1: try: dvars = get_all_vars(url) @@ -113,15 +139,16 @@ def find_nucleares() -> psutil.Process | None: state=status, large_image="nucleares" ) - #print( - # f"Sent Update: Core = {dvars['CORE_TEMP']} - Total Pwr = {pwr} - Panic = {dvars['CORE_IMMINENT_FUSION']}", + logging.debug( + f"Sent Update: Core = {dvars['CORE_TEMP']} - Total Pwr = {pwr} - Panic = {dvars['CORE_IMMINENT_FUSION']}", # f"- Rods: {dvars['RODS_POS_ORDERED']}" - #) + ) time.sleep(15) + raise Exception("Raising test exception") except requests.ConnectionError: - print("Webserver connection lost, trying to re-establish...") + logging.warning("Webserver connection lost, trying to re-establish...") if find_nucleares() is None: - print("Nucleares is closed, RPC will close...") + logging.info("Nucleares is closed, RPC will close...") presence.close() sys.exit(0) while 1: @@ -131,5 +158,13 @@ def find_nucleares() -> psutil.Process | None: break except requests.ConnectionError as e: time.sleep(5) - print("Connected!") + logging.info("Connected!") continue + + except Exception as e: + logging.critical("Client has run into an unexpected error and cannot continue") + logging.critical("NuclearesRPC has crashed.", exc_info=e) + logging.critical("Please send this error when asking for support") + presence.close() + input("Press Enter to exit...") + sys.exit(1) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000000000000000000000000000000000000..53f2eb7b116c0b4717cc9fc76b9b2d33120f38ce GIT binary patch literal 562 zcmZvZ%?^S<420)w;-g>$RJ`~wh=0Tt5SAEycy*?La4?%~!gf0ST0ZX-4jyOJNKxT} z9nTUAgh&`ym}7(sEL@BF0wc%FZ8L=u)-rifs$W0zByLD%t!UHd8=Cf zISy2&=B!LecfD79%W!2pQ&TW@z127B6-Q%obRN0nA*!U;G-!?9nPy`hhTavP=G{;s zN;%aNU1;i#=ASa)&PtvuXAVePlGD>6$9_VzrpqwXlXKPVq8$~sTajxfagVBpamAcO zzMh^m)BTZ_9^QCeuS+1d?XPo7Hf=%t@x*B$`vPK} BRCE9U literal 0 HcmV?d00001 From f09e43db049ff38d162f9b0bb44cdc396f1fb67e Mon Sep 17 00:00:00 2001 From: Emma Lysne Date: Mon, 3 Feb 2025 14:15:02 -0800 Subject: [PATCH 3/3] Remove test exception not removed in last commit --- nuclearesrpc.py | 1 - 1 file changed, 1 deletion(-) diff --git a/nuclearesrpc.py b/nuclearesrpc.py index 9a6b897..0c72f1f 100644 --- a/nuclearesrpc.py +++ b/nuclearesrpc.py @@ -144,7 +144,6 @@ def find_nucleares() -> psutil.Process | None: # f"- Rods: {dvars['RODS_POS_ORDERED']}" ) time.sleep(15) - raise Exception("Raising test exception") except requests.ConnectionError: logging.warning("Webserver connection lost, trying to re-establish...") if find_nucleares() is None: