This repository has been archived on 2023-09-02. You can view files and clone it, but cannot push or open issues or pull requests.
csh/main.py

86 lines
2.3 KiB
Python
Raw Normal View History

2023-04-03 04:20:35 +02:00
from atexit import register
2023-04-01 16:11:56 +02:00
from sys import argv
2023-03-30 21:00:20 +02:00
from threading import Thread
2023-03-30 11:26:52 +02:00
2023-04-06 11:37:37 +02:00
from cheat import Cheat, sleep # type: ignore
2023-03-30 11:26:52 +02:00
2023-04-03 04:20:35 +02:00
2023-04-06 12:57:05 +02:00
def do_on_exit(cheats: Cheat, running: list) -> None:
2023-04-03 04:20:35 +02:00
"""Called when the program end"""
for fn in running:
2023-04-03 04:20:35 +02:00
try:
2023-04-06 12:57:05 +02:00
unloader = getattr(cheats, f"{fn}_unload")
2023-04-03 04:20:35 +02:00
except:
pass
else:
print(f"Unload {fn}...")
unloader()
2023-03-30 11:26:52 +02:00
if __name__ == "__main__":
2023-04-01 16:11:56 +02:00
c_id = []
args = {}
2023-04-04 11:16:49 +02:00
wanted_list = []
2023-04-01 16:11:56 +02:00
2023-05-05 16:04:06 +02:00
if len(argv) > 1:
2023-04-01 16:11:56 +02:00
# User wanna use offline offsets
if "--offline" in argv:
args["offline"] = True
# User gave a list of cheats
# Will bypass the interactive selection
2023-05-05 16:04:06 +02:00
wanted_list = [
j for j in [i for i in argv if i.startswith("--list=")][0][7:].split(",")
]
2023-04-01 16:11:56 +02:00
# Load cheat class
c = Cheat(**args)
2023-04-04 11:16:49 +02:00
# Load user list
c_id = [c.cheats_list.index(cheat) for cheat in wanted_list]
2023-04-01 16:11:56 +02:00
# Interactive selection
2023-05-05 16:04:06 +02:00
if c_id == []:
2023-04-01 16:11:56 +02:00
# Cheat list
2023-05-05 16:04:06 +02:00
print(
"You can run multiples cheat at once, separate your choices with a space."
)
2023-04-01 16:11:56 +02:00
print("Available cheats:")
for idx, cheat in enumerate(c.cheats_list):
print(f"#{idx + 1} - {cheat}")
while c_id == []:
try:
response = [int(i) for i in input("Enter ID: ").split(" ")]
2023-04-01 16:11:56 +02:00
for i in response:
2023-05-05 16:04:06 +02:00
if i > len(c.cheats_list) or i <= 0:
2023-04-01 16:26:55 +02:00
raise IndexError
c_id.append(i - 1)
2023-04-01 16:11:56 +02:00
except KeyboardInterrupt:
2023-04-01 16:26:55 +02:00
print("Bye")
2023-04-01 16:11:56 +02:00
exit(1)
2023-05-05 16:04:06 +02:00
except: # noqa: E722
2023-04-01 16:26:55 +02:00
c_id = []
2023-04-01 16:11:56 +02:00
print("Invalid ID.")
2023-03-30 21:00:20 +02:00
2023-04-06 12:54:55 +02:00
# Instanciate and run threads
running = []
for fn in [c.cheats_list[i] for i in c_id]:
2023-04-06 12:54:55 +02:00
# Set of incompatible cheat with fn
incompatible = set(c.incompatible[fn]).intersection(running)
2023-04-06 12:54:55 +02:00
if not len(incompatible):
running.append(fn)
print(f"Running {fn}...")
t = Thread(target=getattr(c, fn))
t.daemon = True
t.start()
else:
print(f"{fn} is incompatible with: {', '.join(incompatible)}")
2023-03-30 21:00:20 +02:00
2023-04-06 12:57:05 +02:00
register(do_on_exit, c, running)
2023-04-03 04:20:35 +02:00
2023-03-30 21:07:52 +02:00
# Don't close the main thread as cheats are daemons
2023-04-03 04:20:35 +02:00
while True:
sleep(1000000)