44import requests
55import pypresence
66import subprocess
7+ import argparse
8+ import logging
9+ import traceback
10+ import ctypes
711from typing import Dict
812import ctypes
913
1014
15+ __version__ = "v2.0.0"
16+ parser = argparse .ArgumentParser (
17+ prog = "Nucleares Presence Client" ,
18+ description = "Nucleares Rich Presence Client for Discord" ,
19+ epilog = f"Version { __version__ } "
20+ )
21+ parser .add_argument ("-v" , "--version" , action = "store_true" )
22+ parser .add_argument ("-d" , "--debug" , action = "store_true" )
23+ params = parser .parse_args ()
24+ logging .basicConfig (
25+ level = "DEBUG" if params .debug else "INFO" ,
26+ filename = "debug.log" if params .debug else None
27+ )
28+
29+
30+ if params .version :
31+ print ("Nucleares Rich Presence Client" )
32+ print (f"Client Version: { __version__ } " )
33+ print (f"PyPresence Version: { pypresence .__version__ } " )
34+ input ("Press Enter to exit..." )
35+ sys .exit (0 )
36+
37+
1138VARIABLE_TYPES = {
1239 "CORE_TEMP" : float ,
1340 "GENERATOR_0_KW" : float ,
@@ -27,7 +54,7 @@ def get_all_vars(srv_url: str) -> Dict[str, float | str]:
2754 """
2855 results = {}
2956 for key , typeof in VARIABLE_TYPES .items ():
30- print (f"Getting key { key } , looking for type { typeof } " )
57+ logging . debug (f"Getting key { key } , looking for type { typeof } " )
3158 res = requests .get (
3259 srv_url ,
3360 {
@@ -37,7 +64,7 @@ def get_all_vars(srv_url: str) -> Dict[str, float | str]:
3764 try :
3865 results [key ] = typeof (res .text )
3966 except ValueError :
40- print (f"Conversion of '{ res .text } ' to type { typeof } failed" )
67+ logging . error (f"Conversion of '{ res .text } ' to type { typeof } failed" )
4168 return results
4269
4370
@@ -54,24 +81,24 @@ def find_nucleares() -> psutil.Process | None:
5481
5582
5683if len (sys .argv ) > 1 :
57- if sys .argv [ 1 ]. endswith ( "Nucleares.exe" ) :
58- # Client is launching through steam, we are expected to launch it on Steam's behalf
59- game_exec = subprocess . Popen ( sys . argv [ 1 ])
60-
84+ for obj in sys .argv :
85+ if obj . endswith ( "Nucleares.exe" ):
86+ # Client is launching through steam, we are expected to launch it on Steam's behalf
87+ game_exec = subprocess . Popen ( obj )
6188
6289ctypes .windll .user32 .MessageBoxW (0 , "Remember to turn on the WebServer, else NuclearesRPC will not work!" , "Reminder!" , 64 )
6390cid = 1331101603649818786
6491presence = pypresence .Presence (cid , pipe = 0 )
65- print ("Locating running Nucleares executable..." )
92+ logging . info ("Locating running Nucleares executable..." )
6693proc = find_nucleares ()
6794while proc is None :
6895 proc = find_nucleares ()
6996 time .sleep (5 )
70- print ("Found: " + str (proc .pid ))
97+ logging . debug ("Found: " + str (proc .pid ))
7198starttime = time .time ()
7299
73100
74- print ( "Looking for webserver..." )
101+ logging . info ( "Waiting for webserver..." )
75102port = "8785"
76103url = "http://localhost:" + port + "/"
77104while 1 :
@@ -81,12 +108,12 @@ def find_nucleares() -> psutil.Process | None:
81108 break
82109 except requests .ConnectionError as e :
83110 time .sleep (5 )
84- print ("Webserver is live, firing up RPC..." )
111+ logging . info ("Webserver is live, firing up RPC..." )
85112
86113
87114mission = False
88115presence .connect ()
89- print ("Connected. Press Ctrl+C to Exit" )
116+ logging . info ("Connected. Press Ctrl+C to Exit" )
90117while 1 :
91118 try :
92119 dvars = get_all_vars (url )
@@ -113,15 +140,15 @@ def find_nucleares() -> psutil.Process | None:
113140 state = status ,
114141 large_image = "nucleares"
115142 )
116- #print (
117- # f"Sent Update: Core = {dvars['CORE_TEMP']} - Total Pwr = {pwr} - Panic = {dvars['CORE_IMMINENT_FUSION']}",
143+ logging . debug (
144+ f"Sent Update: Core = { dvars ['CORE_TEMP' ]} - Total Pwr = { pwr } - Panic = { dvars ['CORE_IMMINENT_FUSION' ]} " ,
118145 # f"- Rods: {dvars['RODS_POS_ORDERED']}"
119- # )
146+ )
120147 time .sleep (15 )
121148 except requests .ConnectionError :
122- print ("Webserver connection lost, trying to re-establish..." )
149+ logging . warning ("Webserver connection lost, trying to re-establish..." )
123150 if find_nucleares () is None :
124- print ("Nucleares is closed, RPC will close..." )
151+ logging . info ("Nucleares is closed, RPC will close..." )
125152 presence .close ()
126153 sys .exit (0 )
127154 while 1 :
@@ -131,5 +158,13 @@ def find_nucleares() -> psutil.Process | None:
131158 break
132159 except requests .ConnectionError as e :
133160 time .sleep (5 )
134- print ("Connected!" )
161+ logging . info ("Connected!" )
135162 continue
163+
164+ except Exception as e :
165+ logging .critical ("Client has run into an unexpected error and cannot continue" )
166+ logging .critical ("NuclearesRPC has crashed." , exc_info = e )
167+ logging .critical ("Please send this error when asking for support" )
168+ presence .close ()
169+ input ("Press Enter to exit..." )
170+ sys .exit (1 )
0 commit comments