| |
| import regex as re |
| import argparse |
| import os |
| SAVE = "/media/n/data/SteamLibrary/steamapps/compatdata/2379780/pfx/dosdevices/c:/users/steamuser/AppData/Roaming/Balatro/1/save.jkr" |
| |
| |
| def find_default(): |
| global SAVE |
| default = '/1/save.jkr' |
| import sys |
| OS = sys.platform |
| if OS == 'win32': |
| default = "%AppData%/Balatro" + default |
| elif OS == 'darwin': |
| default = "~/Library/Application Support/Balatro" + default |
| elif OS == 'linux': |
| default = "~/.local/share/Steam/steamapps/compatdata/2379780/pfx/drive_c/users/steamuser/AppData/Roaming/Balatro" + default |
| else: |
| print(f'OS={OS} not supported') |
| return |
| |
| default = os.path.expanduser(default) |
| if not os.path.exists(default): |
| print(f'default save path not found:{default}') |
| return |
| |
| SAVE = default |
| _set('SAVE', SAVE) |
| |
| |
| def _set(var_name='VAR', value=None, file=__file__, insert_able=True, ONLY_GLOBAL_VAR=True): |
| """ |
| Used to record var in self `.py` file |
| """ |
| self_py = '' |
| value = str(value) |
| re_newline = r'(?<=\n)' if ONLY_GLOBAL_VAR else '' |
| re_pattern = '' |
| re_repl = '' |
| if value.find('\n') != -1: |
| re_pattern = '"""((.|\n)*?)"""' |
| re_repl = f'{var_name} = """\n{value}\n"""\n' |
| else: |
| re_pattern = '"(.*?)"' |
| re_repl = f'{var_name} = "{value}"\n' |
| re_pattern = f'{re_newline}{var_name}={re_pattern}' |
| |
| with open(file, 'r', encoding='utf-8') as f: |
| self_py = f.read() |
| if re.search(re_pattern, self_py): |
| self_py = re.sub(re_pattern, re_repl, self_py) |
| else: |
| if insert_able: |
| self_py = re.sub(r'(?<=\n)', re_repl, self_py, count=1) |
| else: |
| print(f'{var_name} not found in {file}') |
| return |
| with open(file, 'w', encoding='utf-8') as f: |
| f.write(self_py) |
| |
| |
| def timestamp(): |
| import time |
| return time.strftime('%m-%d_%H:%M:%S') |
| |
| |
| def save(): |
| """copy SAVE to SAVE.bak""" |
| bak = f'{SAVE},{timestamp()}.bak' |
| with open(SAVE, 'rb') as f: |
| with open(bak, 'wb') as f2: |
| f2.write(f.read()) |
| print(f'💾 game saved to {SAVE}') |
| |
| |
| def list_saves(): |
| """list all save files""" |
| import glob |
| l = [] |
| for i in glob.glob(SAVE + '*.bak'): |
| l.append(i) |
| |
| l.sort(key=lambda x: os.path.getmtime(x), reverse=True) |
| for i in l: |
| print(i.split('/')[-1]) |
| return l |
| |
| |
| def load(): |
| """copy SAVE.bak to SAVE""" |
| bak = list_saves()[0] |
| if not os.path.exists(bak): |
| print(f'file not found: {bak}') |
| return |
| with open(bak, 'rb') as f: |
| with open(SAVE, 'wb') as f2: |
| f2.write(f.read()) |
| print(f'📂 game loaded from {bak.split('/')[-1]}') |
| |
| |
| def main(): |
| global SAVE |
| parse = argparse.ArgumentParser() |
| |
| parse.add_argument('-s', '--save', action='store_true', help='save game to file') |
| parse.add_argument('-l', '--load', action='store_true', help='load game from file') |
| parse.add_argument('-p', '--path', help='set default save path') |
| args = parse.parse_args() |
| if args.path: |
| SAVE = args.path |
| _set('SAVE', SAVE) |
| try: |
| if not SAVE or not os.path.exists(SAVE): |
| print(f'file not found: {SAVE}') |
| raise FileNotFoundError |
| except NameError or FileNotFoundError: |
| find_default() |
| finally: |
| if args.save: |
| save() |
| elif args.load: |
| load() |
| else: |
| print(f'current save file = {SAVE}') |
| list_saves() |
| |
| |
| if __name__ == '__main__': |
| main() |
本文作者:Nolca
本文链接:https://www.cnblogs.com/nolca/p/18665461
版权声明:本作品采用 收益分享revenue sharing 许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步