diff --git a/game.py b/game.py index 0944747..352ac3e 100644 --- a/game.py +++ b/game.py @@ -81,6 +81,7 @@ class Game: return def run(self): + title() if os.path.exists("debug.json"): bypass_tamper_check = False debug_config = None @@ -89,22 +90,28 @@ class Game: if content.strip(): debug_config = json.loads(content) if debug_config: - if debug_config.get("bypass_tamper_check", None): + if debug_config.get("bypass_tamper_check_message", None): if ( - debug_config["bypass_tamper_check"] + debug_config["bypass_tamper_check_message"] == data.text.BYPASS_TAMPER_CHECK_MESSAGE ): - bypass_tamper_check = True + if debug_config.get("bypass_tamper_check_enable", False): + bypass_tamper_check = True if debug_config.get("auto_load_savefile", None): - self.cat = data.save.load( - debug_config["auto_load_savefile"], - bypass_tamper_check=bypass_tamper_check, - ) - self.game_loop() - print(f"{self.cat.name} says bye") - return + try: + self.cat = data.save.load( + debug_config["auto_load_savefile"], + bypass_tamper_check=bypass_tamper_check, + ) + self.game_loop() + except: + print( + "There was an error loading the autoload savefile, please check your configuration." + ) + else: + print(f"{self.cat.name} says bye") + return - title() options = ["New Game", "Quit"] if os.path.exists("saves"): options.insert(0, "Load Game") diff --git a/systems/debug.py b/systems/debug.py new file mode 100644 index 0000000..0eee8f4 --- /dev/null +++ b/systems/debug.py @@ -0,0 +1,123 @@ +import os +import json +import data.save +import systems.ui # should cause circular import but doesn't, so not fixing it +import data.text + + +def fix_hash(current_cat): + data_dir = data.save.get_data_dir() + hash_path = os.path.join(data_dir, "dont hurt cats.json") + os.makedirs(data_dir, exist_ok=True) + if os.path.exists(hash_path): + with open(hash_path, "r") as f: + currentjson = json.load(f) + old_hash = currentjson[f"saves/{current_cat.name}.kitten"] + currentjson[f"saves/{current_cat.name}.kitten"] = data.save.hash_file( + f"saves/{current_cat.name}.kitten" + ) + print( + f"Old hash: {old_hash}. New hash: {currentjson[f"saves/{current_cat.name}.kitten"]}" + ) + with open(hash_path, "w") as f: + json.dump(currentjson, f) + + +def debug_menu(current_cat): + print("hi") + while True: + match systems.ui.select( + "choose ur way of breaking the game", + ["Breakpoint", "Fix hash", "Debug settings", "Back"], + hide_debug=True, + ): + case "Breakpoint": + breakpoint() + case "Fix hash": + fix_hash(current_cat) + print( + "If you see this message and theres no errors above I think it worked." + ) + case "Debug settings": + while True: + with open("debug.json", "r") as f: + config = json.load(f) + enabled = config.get("bypass_tamper_check_enable", False) + label = "Disable" if enabled else "Enable" + match systems.ui.select( + "choose a setting", + [ + systems.ui.Choice( + title=f"{label} hash bypass", + value="Toggle tamper check", + ), + "Configure auto savefile loading", + "Back", + ], + hide_debug=True, + ): + case "Toggle tamper check": + if ( + config["bypass_tamper_check_message"] + != data.text.BYPASS_TAMPER_CHECK_MESSAGE + ): + print( + "If you already did this, you may have typed it wrong." + ) + print( + "You need to set the message that you will not use this to cheat. Please type the following message EXACTLY." + ) + print(data.text.BYPASS_TAMPER_CHECK_MESSAGE) + response = systems.ui.text("Type here:") + config["bypass_tamper_check_message"] = response + config["bypass_tamper_check_enable"] = not enabled + with open("debug.json", "w") as f: + json.dump(config, f) + case "Configure auto savefile loading": + while True: + print( + "Currently loads:", + config.get("auto_load_savefile", "Unset"), + ) + match systems.ui.select( + "Please choose an option", + [ + "Change file to load", + "Unset auto savefile loading", + "Back", + ], + hide_debug=True, + ): + case "Back": + break + case "Change file to load": + print("Press enter to cancel") + file = systems.ui.filepath( + 'Please enter the path to your savefile (if it is in the normal "saves" folder, starts with saves/), eg: saves/example.kitten:', + default="", + ) + if file: + if os.path.exists(file): + config["auto_load_savefile"] = file + with open("debug.json", "w") as f: + json.dump(config, f) + else: + print("This is an invalid path!") + else: + print("Cancelled.") + case "Unset auto savefile loading": + try: + del config["auto_load_savefile"] + except KeyError: + print( + "The autoload was already unset! Nothing was changed." + ) + else: + with open("debug.json", "w") as f: + json.dump(config, f) + print("Done") + + case "Back": + break + case "Back": + break diff --git a/systems/ui.py b/systems/ui.py index 7fb7aa0..98fd27f 100644 --- a/systems/ui.py +++ b/systems/ui.py @@ -3,9 +3,12 @@ import questionary import sys import data.save import json -from data.cat import Cat +import data.cat +import data.text +import systems.debug -current_cat: Cat | None = None + +current_cat: data.cat.Cat | None = None def getch(): @@ -26,42 +29,6 @@ def getch(): termios.tcsetattr(fd, termios.TCSADRAIN, old) -def fix_hash(): - data_dir = data.save.get_data_dir() - hash_path = os.path.join(data_dir, "dont hurt cats.json") - os.makedirs(data_dir, exist_ok=True) - if os.path.exists(hash_path): - with open(hash_path, "r") as f: - currentjson = json.load(f) - currentjson[f"saves/{current_cat.name}.kitten"] = data.save.hash_file( - f"saves/{current_cat.name}.kitten" - ) - print( - f"Old hash: {currentjson.get(f"saves/{current_cat.name}.kitten")}. New hash: {currentjson[f"saves/{current_cat.name}.kitten"]}" - ) - with open(hash_path, "w") as f: - json.dump(currentjson, f) - - -def debug_menu(): - print("hi") - while True: - match select( - "choose ur way of breaking the game", - ["Breakpoint", "Fix hash", "Debug settings", "Back"], - hide_debug=True, - ): - case "Breakpoint": - breakpoint() - case "Fix hash": - fix_hash() - print( - "If you see this message and theres no errors above I think it worked." - ) - case "Back": - break - - def clear(): os.system("cls" if os.name == "nt" else "clear") @@ -89,7 +56,7 @@ def select(message, choices, hide_debug=False): choice = questionary.select(message, choices=choices, style=STYLE).ask() if choice != "Debug Menu": return choice - debug_menu() + systems.debug.debug_menu(current_cat) return select(message, choices) @@ -99,3 +66,7 @@ def text(message, default=""): def confirm(message, default=True): return questionary.confirm(message, default=default, style=STYLE).ask() + + +def filepath(message, default=True): + return questionary.path(message, default=default, style=STYLE).ask()