use atexit instead of error handling

This commit is contained in:
Mylloon 2023-04-03 04:20:35 +02:00
parent 7cac16642b
commit 87a92b095b
Signed by: Anri
GPG key ID: A82D63DFF8D1317F

26
main.py
View file

@ -1,8 +1,22 @@
from atexit import register
from sys import argv from sys import argv
from threading import Thread from threading import Thread
from cheat import Cheat, sleep from cheat import Cheat, sleep
def do_on_exit():
"""Called when the program end"""
for fn in [c.cheats_list[i] for i in c_id]:
try:
unloader = getattr(c, f"{fn}_unload")
except:
pass
else:
print(f"Unload {fn}...")
unloader()
if __name__ == "__main__": if __name__ == "__main__":
c_id = [] c_id = []
args = {} args = {}
@ -49,16 +63,8 @@ if __name__ == "__main__":
t.daemon = True t.daemon = True
t.start() t.start()
register(do_on_exit)
# Don't close the main thread as cheats are daemons # Don't close the main thread as cheats are daemons
try:
while True: while True:
sleep(1000000) sleep(1000000)
except KeyboardInterrupt:
for fn in [c.cheats_list[i] for i in c_id]:
try:
unloader = getattr(c, f"{fn}_unload")
except:
pass
else:
print(f"Unload {fn}...")
unloader()