diff --git a/.gitignore b/.gitignore index eccb786..53d87a2 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,8 @@ build /mods /.vscode /mum_list +/count +/work_l *.c *.bat diff --git a/app.py b/app.py index 91bab42..ad1bcaf 100644 --- a/app.py +++ b/app.py @@ -6,7 +6,7 @@ import runing_config import runing_tmp_data -def r_mods(): +def r_mods(pass_hash: list[str] = []): runing_config.g_mods_dir = common.get_config("g_mods_dir") while runing_config.g_mods_dir == "": print("g_mods_dir未设置, 请先设置g_mods_dir") @@ -35,6 +35,8 @@ def r_mods(): with open(mod_list, "a") as f: for mod in runing_tmp_data.file_hash_results: + if mod.hash_value in pass_hash: + continue f.write(mod.hash_value+" : "+mod.filepath.split(runing_config.MODS_DIR)[1]+"\n") if os.path.exists(g_mods_archive_dir+mod.hash_value): continue @@ -69,19 +71,23 @@ def w_mods(): with open(mod_list_path, "r") as f: mod_list = f.read().strip() - mod_list = mod_list.split("\n") - for mod in mod_list: - mod_hash = mod.split(":")[0].strip() - mod_name = mod.split(":")[1].strip() - # 可选流程,验证temp和archive内文件hash,待开发 - if os.path.exists(g_mods_temp_dir+mod_hash) and os.path.isfile(g_mods_temp_dir+mod_hash): - os.rename(g_mods_temp_dir+mod_hash, runing_config.MODS_DIR+mod_name) - elif os.path.exists(g_mods_archive_dir+mod_hash) and os.path.isfile(g_mods_archive_dir+mod_hash): - with open(g_mods_archive_dir+mod_hash, "rb") as src, open(runing_config.MODS_DIR+mod_name, "wb") as out: + mod_list = mod_list.split("\n") + for mod in mod_list: + mod_hash = mod.split(":")[0].strip() + mod_name = mod.split(":")[1].strip() + # 可选流程,验证temp和archive内文件hash,待开发 + if os.path.exists(runing_config.MODS_DIR+mod_name): + continue + if not os.path.exists(os.path.dirname(runing_config.MODS_DIR+mod_name)): + os.makedirs(os.path.dirname(runing_config.MODS_DIR+mod_name)) + if os.path.exists(g_mods_temp_dir+mod_hash) and os.path.isfile(g_mods_temp_dir+mod_hash): + os.rename(g_mods_temp_dir+mod_hash, runing_config.MODS_DIR+mod_name) + elif os.path.exists(g_mods_archive_dir+mod_hash) and os.path.isfile(g_mods_archive_dir+mod_hash): + with open(g_mods_archive_dir+mod_hash, "rb") as src, open(runing_config.MODS_DIR+mod_name, "wb") as out: + data = src.read(1024**2) + while data: + out.write(data) data = src.read(1024**2) - while data: - out.write(data) - data = src.read(1024**2) def menu(): @@ -92,71 +98,111 @@ def menu(): print("err!") time.sleep(3) exit(-2) # modlist文件被占用 + print(runing_config.menu) + match input("> "): + case runing_config.MENU_CODE.add_: + modhsah_l, _ = common.get_modlist() + r_mods(modhsah_l) + w_mods() + case runing_config.MENU_CODE.del_: + for path, dirs, files in os.walk(runing_config.MODS_DIR): + path = common.formatting_path(path) + for file in files: + os.remove(path+file) + w_mods() + case runing_config.MENU_CODE.re_: + os.remove(mod_list) + r_mods() + case runing_config.MENU_CODE.uninstall: + w_mods() + with open(runing_config.WORK_DIR + "mum_list/lock", "w") as f: f.write("1") + +runing_config.EXE_DIR = common.get_exe_dir() if common.get_config("eula") != "true": print("这是一个实验性质的程序,你需要具备mc mod加载、存档管理相关知识,出现问题概不负责\n本程序理论上不修改存档,但是如果mod转移出现问题仍然加载存档可能发生数据丢失,请积极备份") if not common.eula(): exit(0) exit_tips_flag = False - -runing_config.WORK_DIR = common.formatting_path(os.getcwd()) -runing_config.MODS_DIR = common.formatting_path(runing_config.WORK_DIR + "mods/") +def init_(): + runing_config.WORK_DIR = common.formatting_path(os.getcwd()) + runing_config.MODS_DIR = common.formatting_path(runing_config.WORK_DIR + "mods/") -if not (os.path.exists(runing_config.MODS_DIR) and os.path.isdir(runing_config.MODS_DIR)): - print("err!") - time.sleep(3) - exit(-1) # 未找到mods目录 + if os.path.exists(runing_config.WORK_DIR + "mum_list/lock"): + exit(0) -runing_config.VERSIONS_MODS_LIST_PATH = common.formatting_path(runing_config.WORK_DIR + "mum_list/") -if not os.path.exists(runing_config.VERSIONS_MODS_LIST_PATH): - os.mkdir(runing_config.VERSIONS_MODS_LIST_PATH) -elif not os.path.isdir(runing_config.VERSIONS_MODS_LIST_PATH): - print("err!") - time.sleep(3) - exit(-2) # mum_list目录被占用 + if not (os.path.exists(runing_config.MODS_DIR) and os.path.isdir(runing_config.MODS_DIR)): + print("err!") + time.sleep(3) + exit(-1) # 未找到mods目录 + + runing_config.VERSIONS_MODS_LIST_PATH = common.formatting_path(runing_config.WORK_DIR + "mum_list/") + if not os.path.exists(runing_config.VERSIONS_MODS_LIST_PATH): + os.mkdir(runing_config.VERSIONS_MODS_LIST_PATH) + elif not os.path.isdir(runing_config.VERSIONS_MODS_LIST_PATH): + print("err!") + time.sleep(3) + exit(-2) # mum_list目录被占用 + +init_() + +common.check_file(runing_config.EXE_DIR+"work_l", "") if len(sys.argv) >= 2 and sys.argv[1] == "gameexit": - runing_config.g_mods_dir = common.get_config("g_mods_dir") - g_mods_archive_dir = common.formatting_path(runing_config.g_mods_dir)+"archive/" - g_mods_temp_dir = common.formatting_path(runing_config.g_mods_dir)+"temp/" - mod_list_path = runing_config.VERSIONS_MODS_LIST_PATH + "modlist" - - if not (os.path.exists(mod_list_path) and os.path.isfile(mod_list_path)): - print("未找到mod list") - time.sleep(3) + if common.sub_game() > 0: exit(0) - if not (os.path.exists(g_mods_archive_dir) and os.path.isdir(g_mods_archive_dir)): - print("err!") - time.sleep(3) - exit(-3) # g_mods_archive_dir损坏/不正确 + with open(runing_config.EXE_DIR+"work_l", "r") as f: + work_l = f.read().strip() + work_l = work_l.split("\n") + for work in work_l: + os.chdir(work.strip()) + init_() + runing_config.g_mods_dir = common.get_config("g_mods_dir") + g_mods_archive_dir = common.formatting_path(runing_config.g_mods_dir)+"archive/" + g_mods_temp_dir = common.formatting_path(runing_config.g_mods_dir)+"temp/" + mod_list_path = runing_config.VERSIONS_MODS_LIST_PATH + "modlist" - if not os.path.exists(g_mods_temp_dir): - os.mkdir(g_mods_temp_dir) - elif not os.path.isdir(g_mods_temp_dir): - print("err!") - time.sleep(3) - exit(-2) # temp目录被占用 + if not (os.path.exists(mod_list_path) and os.path.isfile(mod_list_path)): + print("未找到mod list") + time.sleep(3) + exit(0) + if not (os.path.exists(g_mods_archive_dir) and os.path.isdir(g_mods_archive_dir)): + print("err!") + time.sleep(3) + exit(-3) # g_mods_archive_dir损坏/不正确 + + if not os.path.exists(g_mods_temp_dir): + os.mkdir(g_mods_temp_dir) + elif not os.path.isdir(g_mods_temp_dir): + print("err!") + time.sleep(3) + exit(-2) # temp目录被占用 + + with open(mod_list_path, "r") as f: + mod_list = f.read().strip() + mod_list = mod_list.split("\n") + for mod in mod_list: + mod_hash = mod.split(":")[0].strip() + mod_name = mod.split(":")[1].strip() + if not os.path.exists(g_mods_archive_dir+mod_hash): + os.rename(runing_config.MODS_DIR+mod_name, g_mods_archive_dir+mod_hash) + elif not os.path.isfile(g_mods_archive_dir+mod_hash): + print(f"E: {mod_name}在g_mods_archive_dir中被占用") + exit_tips_flag = True + elif not os.path.exists(g_mods_temp_dir+mod_hash): + os.rename(runing_config.MODS_DIR+mod_name, g_mods_temp_dir+mod_hash) + elif not os.path.isfile(g_mods_archive_dir+mod_hash): + print(f"W: {mod_name}在g_mods_temp_dir中被占用") + with open(runing_config.EXE_DIR+"work_l", "w") as f: f.write() + if exit_tips_flag: + input() + exit(0) + +common.add_game() +with open(runing_config.EXE_DIR+"work_l", "a") as f: + f.write(runing_config.WORK_DIR+"\n") - with open(mod_list_path, "r") as f: - mod_list = f.read().strip() - mod_list = mod_list.split("\n") - for mod in mod_list: - mod_hash = mod.split(":")[0].strip() - mod_name = mod.split(":")[1].strip() - if not os.path.exists(g_mods_archive_dir+mod_hash): - os.rename(runing_config.MODS_DIR+mod_name, g_mods_archive_dir+mod_hash) - elif not os.path.isfile(g_mods_archive_dir+mod_hash): - print(f"E: {mod_name}在g_mods_archive_dir中被占用") - exit_tips_flag = True - elif not os.path.exists(g_mods_temp_dir+mod_hash): - os.rename(runing_config.MODS_DIR+mod_name, g_mods_temp_dir+mod_hash) - elif not os.path.isfile(g_mods_archive_dir+mod_hash): - print(f"W: {mod_name}在g_mods_temp_dir中被占用") - if exit_tips_flag: - input() - exit(0) - if len(os.listdir(runing_config.MODS_DIR)) == 0: w_mods() else: diff --git a/common.py b/common.py index 5692b70..0f40427 100644 --- a/common.py +++ b/common.py @@ -20,10 +20,7 @@ def eula(): return False def w_config(config_name: str, config_v: str, create_config_flag: bool = False): - if getattr(sys, "frozen", False): - exe_dir = os.path.dirname(os.path.abspath(sys.executable)) - else: - exe_dir = os.path.dirname(os.path.abspath(__file__)) + exe_dir = runing_config.EXE_DIR config_path = formatting_path(os.path.dirname(exe_dir))+"config.json" if create_config_flag: @@ -39,19 +36,83 @@ def w_config(config_name: str, config_v: str, create_config_flag: bool = False): return True def get_config(config_name): - if getattr(sys, "frozen", False): - exe_dir = os.path.dirname(os.path.abspath(sys.executable)) - else: - exe_dir = os.path.dirname(os.path.abspath(__file__)) + exe_dir = runing_config.EXE_DIR config_path = formatting_path(exe_dir)+"config.json" if not os.path.exists(config_path): return "" elif not os.path.isfile(config_path): print("err") time.sleep(3) - exit(-1) # config.json文件被占用 + exit(runing_config.EXIT_CODE["te"]) # config.json文件被占用 with open(config_path, "r") as f: config_json = json.loads(f.read().strip()) config_json: dict return config_json.get(config_name, "") - return "" \ No newline at end of file + return "" + +def get_modlist(): + mod_list_path = runing_config.VERSIONS_MODS_LIST_PATH + "modlist" + if not os.path.exists(mod_list_path): + with open(mod_list_path, "w") as f: f.write("") + elif not os.path.isfile(mod_list_path): + print("err!") + time.sleep(3) + exit(-2) # mod_list文件被占用 + mod_hash_list = [] + mod_name_list = [] + with open(mod_list_path, "r") as f: + mod_list = f.read().strip() + mod_list = mod_list.split("\n") + for mod in mod_list: + mod_hash_list.append(mod.split(":")[0].strip()) + mod_name_list.append(mod.split(":")[1].strip()) + return mod_hash_list, mod_name_list + + +def add_game(c:int = 1): + exe_dir = runing_config.EXE_DIR + count = formatting_path(exe_dir)+"count" + if not os.path.exists(count): + with open(count, "w") as f: + f.write("0") + elif not os.path.isfile(count): + print("err") + time.sleep(3) + exit(runing_config.EXIT_CODE["te"]) # count文件被占用/文件类型错误 + with open(count, "r+") as f: + c = int(f.read().strip()) + 1 + f.seek(0) + f.write(str(c)) + +def sub_game(c:int = 1): + exe_dir = runing_config.EXE_DIR + count = formatting_path(exe_dir)+"count" + if not os.path.exists(count): + with open(count, "w") as f: + f.write("0") + return 0 + elif not os.path.isfile(count): + print("err") + time.sleep(3) + exit(runing_config.EXIT_CODE["te"]) # count文件被占用/文件类型错误 + with open(count, "r+") as f: + c = int(f.read().strip()) - 1 + f.seek(0) + f.write(str(c)) + return c + +def get_exe_dir(): + if getattr(sys, "frozen", False): + exe_dir = os.path.dirname(os.path.abspath(sys.executable)) + else: + exe_dir = os.path.dirname(os.path.abspath(__file__)) + return formatting_path(exe_dir) + +def check_file(file_path: str, def_): + if not os.path.exists(file_path): + with open(file_path, "w") as f: + f.write(def_) + elif not os.path.isfile(file_path): + print("err") + time.sleep(3) + exit(runing_config.EXIT_CODE["te"]) # 文件被占用/文件类型错误 \ No newline at end of file diff --git a/runing_config.py b/runing_config.py index b7e34ae..7374ab3 100644 --- a/runing_config.py +++ b/runing_config.py @@ -1,3 +1,5 @@ +from _in_package import typing + WORK_DIR: str = "./" MODS_DIR: str VERSIONS_MODS_LIST_PATH: str @@ -8,4 +10,22 @@ wait_c: int = 100 # nf: Not found # te: Type err -EXIT_CODE = {"ok": 0, "nf": -1, "te": -2} \ No newline at end of file +EXIT_CODE = {"ok": 0, "nf": -1, "te": -2} + +menu = """ +检测到mods文件改动 +请选择: +1. 添加到mum模组清单,并按照清单拉取模组 +2. 清除当前mods变更并按照清单拉取模组 +3. 按照当前mods目录重建模组清单(请确认你知道这会发生什么) +4. 按照清单拉取模组并取消管理 +""" + +class MenuCode(typing.NamedTuple): + add_: str = "1" + del_: str = "2" + re_: str = "3" + uninstall: str = "4" +MENU_CODE = MenuCode() + +EXE_DIR: str \ No newline at end of file