From f8e9b9419ac4c09f20922ebdaa86824b2283b97f Mon Sep 17 00:00:00 2001 From: xz Date: Fri, 6 Nov 2026 15:36:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E7=A7=BB=E5=8A=A8mods?= =?UTF-8?q?=E5=88=B0g=5Fmods=5Fdir=E9=83=A8=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 13 ++++++++ LICENSE | 21 +++++++++++++ _in_package.py | 7 +++++ app.py | 77 ++++++++++++++++++++++++++++++++++++++++++++++ common.py | 57 ++++++++++++++++++++++++++++++++++ hash_.py | 31 +++++++++++++++++++ runing_config.py | 8 +++++ runing_tmp_data.py | 14 +++++++++ 8 files changed, 228 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 _in_package.py create mode 100644 app.py create mode 100644 common.py create mode 100644 hash_.py create mode 100644 runing_config.py create mode 100644 runing_tmp_data.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1e48aec --- /dev/null +++ b/.gitignore @@ -0,0 +1,13 @@ +__pycache__ +dist +test +build +*.spec +*.backup +/.venv +/mods +/.vscode +/mum_list + +*.c +*.bat \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..16b524f --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2026 xz + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/_in_package.py b/_in_package.py new file mode 100644 index 0000000..cc2353b --- /dev/null +++ b/_in_package.py @@ -0,0 +1,7 @@ +import sys +import os +import hashlib +import typing +# import collections +import time +import json \ No newline at end of file diff --git a/app.py b/app.py new file mode 100644 index 0000000..7dae5ab --- /dev/null +++ b/app.py @@ -0,0 +1,77 @@ +from _in_package import * + +import common +import hash_ +import runing_config +import runing_tmp_data + + +def r_mods(): + runing_config.g_mods_dir = common.get_config("g_mods_dir") + while runing_config.g_mods_dir == "": + print("g_mods_dir未设置, 请先设置g_mods_dir") + common.w_config("g_mods_dir", input("g_mods_dir: ")) + runing_config.g_mods_dir = common.get_config("g_mods_dir") + runing_config.g_mods_dir = common.formatting_path(runing_config.g_mods_dir) + + hash_.traverse_dir(runing_config.MODS_DIR) + hash_.hash_files_from_glist(hashlib.sha256) + + archive_dir = common.formatting_path(runing_config.g_mods_dir+"archive/") + mod_list = runing_config.VERSIONS_MODS_LIST_PATH + "modlist" + if not os.path.exists(archive_dir): + os.mkdir(archive_dir) + elif not os.path.isdir(archive_dir): + print("err!") + time.sleep(3) + exit(-2) # archive目录被占用 + + if not os.path.exists(mod_list): + with open(mod_list, "w") as f: f.write("") + elif not os.path.isfile(mod_list): + print("err!") + time.sleep(3) + exit(-2) # mod_list文件被占用 + + with open(mod_list, "a") as f: + for mod in runing_tmp_data.file_hash_results: + f.write(mod.hash_value+" : "+os.path.basename(mod.filepath)+"\n") + os.rename(mod.filepath, archive_dir+mod.hash_value) + +def w_mods(): + pass + +def menu(): + mod_list = runing_config.VERSIONS_MODS_LIST_PATH + "modlist" + if not os.path.exists(mod_list): + r_mods() + elif not os.path.isfile(mod_list): + print("err!") + time.sleep(3) + exit(-2) # modlist文件被占用 + +if common.get_config("eula") != "true": + print("这是一个实验性质的程序,你需要具备mc mod加载、存档管理相关知识,出现问题概不负责\n本程序理论上不修改存档,但是如果mod转移出现问题仍然加载存档可能发生数据丢失,请积极备份") + if not common.eula(): + exit(0) + +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目录 + +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 len(os.listdir(runing_config.MODS_DIR)) == 0: + w_mods() +else: + menu() diff --git a/common.py b/common.py new file mode 100644 index 0000000..fa6097c --- /dev/null +++ b/common.py @@ -0,0 +1,57 @@ +from _in_package import * +import runing_config + + +def formatting_path(path: str) -> str: + path = path.strip() + path = path.replace("\\", "/") + if path[-1] != "/": + path += "/" + return path + +def eula(): + print("请确认继续(Y/n): ") + user_in = input() + if user_in in ["", "y", "Y"]: + w_config("eula", "true", True) + return True + else: + exit(0) + 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__)) + config_path = formatting_path(os.path.dirname(exe_dir))+"config.json" + + if create_config_flag: + with open(config_path, "w") as f: + f.write(json.dumps({config_name: config_v})) + return True + else: + with open(config_path, "r") as f: + config_ = json.loads(f.read().strip()) + config_[config_name] = config_v + with open(config_path, "w") as f: + f.write(json.dumps(config_)) + 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__)) + config_path = formatting_path(os.path.dirname(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文件被占用 + 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 diff --git a/hash_.py b/hash_.py new file mode 100644 index 0000000..a16d064 --- /dev/null +++ b/hash_.py @@ -0,0 +1,31 @@ +from _in_package import * + +import common +import runing_config +import runing_tmp_data + +def traverse_dir(root_dir): + print("正在遍历mod文件ing...") + for path, dirs, files in os.walk(root_dir): + path = common.formatting_path(path) + for filename in files: + runing_tmp_data.file_path_list.append(path + filename) + print(f"完成\t总计 {len(runing_tmp_data.file_path_list)} 个文件") + + +def hash_files_from_glist(algorithm: function): + print("正在计算mod文件散列ing...") + i = 0 + p_old_i = -1 + for f_path in runing_tmp_data.file_path_list: + with open(f_path, "rb") as f: + f_hash = hashlib.file_digest(f, algorithm) + runing_tmp_data.file_hash_results.append(runing_tmp_data.FileHashResult(f_path, f_hash.hexdigest(), algorithm.__name__)) + runing_tmp_data.hash_to_paths[(f_hash.hexdigest(), algorithm.__name__)] = f_path + i += 1 + if i % runing_config.wait_c == 0: + p_old_i = i + print(f"\r{i}/{len(runing_tmp_data.file_path_list)}", end="") + if p_old_i != -1 and i != p_old_i: + print(f"\r{i}/{len(runing_tmp_data.file_path_list)}", end="") + print() \ No newline at end of file diff --git a/runing_config.py b/runing_config.py new file mode 100644 index 0000000..f36e129 --- /dev/null +++ b/runing_config.py @@ -0,0 +1,8 @@ +WORK_DIR: str = "./" +MODS_DIR: str +VERSIONS_MODS_LIST_PATH: str + +g_mods_dir: str = "" + +wait_c: int = 100 + diff --git a/runing_tmp_data.py b/runing_tmp_data.py new file mode 100644 index 0000000..620b83d --- /dev/null +++ b/runing_tmp_data.py @@ -0,0 +1,14 @@ +from _in_package import * + + +class FileHashResult(typing.NamedTuple): + filepath: str + hash_value: str + algorithm: str + + +file_path_list: list[str] = [] +file_hash_results: list[FileHashResult] = [] + +# hash_to_files: { (hash_value, algorithm): [filepath, ...] } +hash_to_paths: dict[tuple[str, str], str] = {}