Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ venv/
# Ignore build files
build/
dist/
nuclearesrpc.spec
nuclearesrpc.spec

# Ignore log files
*.log
69 changes: 52 additions & 17 deletions nuclearesrpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,37 @@
import requests
import pypresence
import subprocess
import argparse
import logging
import traceback
import ctypes
from typing import Dict
import ctypes


__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 = {
"CORE_TEMP": float,
"GENERATOR_0_KW": float,
Expand All @@ -27,7 +54,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,
{
Expand All @@ -37,7 +64,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


Expand All @@ -54,24 +81,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:
Expand All @@ -81,12 +108,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)
Expand All @@ -113,15 +140,15 @@ 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)
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:
Expand All @@ -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)
Binary file added requirements.txt
Binary file not shown.