diff --git a/Makefile b/Makefile index 86333709924b04d2c9c6a626a9548e181581f7db..f71cdd29054858986c7ae35af1029e99e37617cc 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -dist\PEinjector.7z: src\main.py src\log.py src\action.py src\hook.pyw src\makelnk.py src\regwrite.py src\config.py src\loader.py src\utils.py root\VentoyAutoRun.bat +dist\PEinjector.7z: src\main.py src\log.py src\action\__init__.py src\action\copy.py src\action\do_action.py src\action\force_copy.py src\action\save_action.py src\hook.pyw src\makelnk.py src\regwrite.py src\config.py src\loader\ src\loader\__init__.py src\loader\file_check.py src\load_package.py src\version_check.py src\utils.py root\VentoyAutoRun.bat mkdir tmp\pkg\PEinjector\env mkdir tmp\pkg\PEinjector\src mkdir tmp\pkg\PEinjector\tool diff --git a/installer/install.py b/installer/install.py index bf541fea55a92a675efc6a5fbe4a662b5647251e..ac66a3f5ef033d8c9c2f586f296165bfa85ee4e8 100644 --- a/installer/install.py +++ b/installer/install.py @@ -8,6 +8,8 @@ import os import shutil import json +import platform +import string ############ Config ############ KEYWORD = "winpe" @@ -15,39 +17,80 @@ PYTHON = "python" DISK = "{disk}" ################################ -# Linux please run: mount /dev/sdX /mnt/disk --mkdir -# DISK = "/mnt/disk" - - +''' + Function Name : find_disk() + Purpose: Find the drive with Ventoy installed. + TODO : Check Linux based OS support +''' def find_disk(): # This code from dashedgeless - disk_list = "CDEFGHIJKABLMNOPQRSTUVWYZ" - for i in disk_list: - this_disk = i + ":" - if os.path.exists(this_disk + "/ventoy/ventoy.json"): - return this_disk - raise OSError( - "Cannot find the Ventoy disk!(try to run ventoyPlugson once)") + ventoy_drives = [] + if platform.system() == 'Linux': + mount_point = "/mnt/prinjector_mount_point" + if not os.path.exists(mount_point): + os.mkdir(mount_point) + elif len(os.listdir(mount_point)): + raise OSError("Mount point {} not empty!".format(mount_point)) + + i:int = 97 + while os.path.exists(f"/dev/sd{chr(i)}"): + j:int = 1 + this_disk = f"/dev/sd{chr(i)}{j}" + while os.path.exists(this_disk): + os.system(f"mount /dev/sd{chr(i)}{j} {mount_point}") + if os.path.exists(mount_point+"/ventoy/ventoy.json"): + ventoy_drives.append(this_disk) + j += 1 + i += 1 + + i:int = 0 + while os.path.exists(f"/dev/nvme{i}"): + j:int = 1 + while os.path.exists(f"/dev/nvme{i}n{j}"): + k:int = 1 + while os.path.exists(f"/dev/nvme{i}n{j}p{k}"): + os.system(f"mount /dev/nvme{i}n{j}p{k} {mount_point}") + if os.path.exists(mount_point+"/ventoy/ventoy.json"): + ventoy_drives.append(mount_point + "/ventoy/ventoy.json") + k += 1 + j += 1 + i += 1 + + elif platform.system() == 'Windows': + for i in "CDEFGHIJKABLMNOPQRSTUVWYZ": + this_disk = i+":" + if os.path.exists(this_disk + "/ventoy/ventoy.json"): + ventoy_drives.append(this_disk) + + else: + raise OSError(f"Unsupported operating system:{platform.system()}") + if len(ventoy_drives) == 0: + raise OSError( + "Cannot find the Ventoy disk!(try to run ventoyPlugson once)") + elif len(ventoy_drives) != 1: + for (index,value) in enumerate(ventoy_drives): + print(f"{i}:{ventoy_drives[i]}",end=" ") + return ventoy_drives[int(input("Please choose your Ventoy device:"))] DISK = DISK.replace("{disk}", find_disk()) -print("use disk: "+DISK) +print("Use disk: "+DISK) with open(DISK+"/ventoy/ventoy.json", "r") as file: - vtoycfg = json.load(file) + ventoy_cfg = json.load(file) searchroot = "" -searchlevel = 3 -if "control" in vtoycfg: - for i in vtoycfg["control"]: +search_level = 3 +if "control" in ventoy_cfg: + for i in ventoy_cfg["control"]: if "VTOY_DEFAULT_SEARCH_ROOT" in i: searchroot = i["VTOY_DEFAULT_SEARCH_ROOT"] if "VTOY_MAX_SEARCH_LEVEL" in i: if i["VTOY_MAX_SEARCH_LEVEL"] != "max": - searchlevel = int(i["VTOY_MAX_SEARCH_LEVEL"]) + search_level = int(i["VTOY_MAX_SEARCH_LEVEL"]) filelist = [] -def dfs(deep, dir): - if deep > searchlevel: +def dfs(deep, dir, search_level): + if deep > search_level: return lists = os.listdir(DISK+dir) for i in lists: @@ -55,30 +98,33 @@ def dfs(deep, dir): if ".iso" in i.lower() and KEYWORD in i.lower(): filelist.append(dir+"/"+i) else: - dfs(deep+1, dir+"/"+i) + dfs(deep+1, dir+"/"+i,search_level) -dfs(0, searchroot) +dfs(0, searchroot,search_level) -if "injection" in vtoycfg: - for i in vtoycfg["injection"]: +if "injection" in ventoy_cfg: + for i in ventoy_cfg["injection"]: if "image" in i: if i["image"] in filelist: filelist.remove(i["image"]) else: - vtoycfg["injection"] = [] -print("find iso:", filelist) + ventoy_cfg["injection"] = [] + +print("Found iso:", filelist) for i in filelist: - vtoycfg["injection"].append({ + ventoy_cfg["injection"].append({ "image": i, "archive": "/PEinjector/PEinjector.7z" }) -print("write config") + + +print("Writing config") with open(DISK+"/ventoy/ventoy.json", "w") as file: - file.write(json.dumps(vtoycfg)) + file.write(json.dumps(ventoy_cfg)) -print("copy file") +print("Copying files") if not os.path.exists(DISK+"/PEinjector"): os.mkdir(DISK+"/PEinjector") if os.path.exists(DISK+"/PEinjector/PEinjector.7z"): @@ -88,14 +134,17 @@ if not os.path.exists(DISK+"/PEinjector/disable.txt"): with open(DISK+"/PEinjector/disable.txt", "w") as file: pass -print("write version info") + +print("Writing version info") shutil.copyfile("version", DISK+"/PEinjector/VERSION") -print("make dir") + +print("Creating dir") if not os.path.exists(DISK+"/PEinjector/install"): os.mkdir(DISK+"/PEinjector/install") -print("install basic package") + +print("Installing basic package") if not os.path.exists(DISK+"/PEinjector/package"): os.mkdir(DISK+"/PEinjector/package") for i in os.listdir("package"): @@ -103,4 +152,5 @@ for i in os.listdir("package"): shutil.rmtree(DISK+"/PEinjector/package/"+i) shutil.copytree("package/"+i, DISK+"/PEinjector/package/"+i) -print("done") + +print("All done.") diff --git a/src/action/__init__.py b/src/action/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..7ed95f87fd9bc63e766f4bf1bb74bc4d7ebc055e --- /dev/null +++ b/src/action/__init__.py @@ -0,0 +1,40 @@ +##################################### +## PEinjector/action ## +##################################### + +# Actions ID Info: +# .----+--------------+--------------------------. +# | ID | Args | Actions | +# +====+==============+==========================+ +# | 0 | pkg_name | start load pkg (for log) | +# +----+--------------+--------------------------+ +# | 1 | path | mkdir | +# +----+--------------+--------------------------+ +# | 2 | from, to | copy | +# +----+--------------+--------------------------+ +# | 3 | from, to | link | +# +----+--------------+--------------------------+ +# | 4 | from, to | link | +# +----+--------------+--------------------------+ +# | 5 | command | start | +# +----+--------------+--------------------------+ +# | 6 |path,icon,name| make shortcut on Desktop | +# +----+--------------+--------------------------+ +# | 7 | path | add to %PATH% | +# +----+--------------+--------------------------+ +# | 8 | regpath | add file to reg | +# '----+--------------+--------------------------' + + +import os +import config +import utils +import pickle +import log +import shutil +import subprocess +import makelnk +import regwrite + +def start(pkg_name: str, data_list: list, cmd: str) -> list: + return [(5, cmd)] diff --git a/src/action/copy.py b/src/action/copy.py new file mode 100644 index 0000000000000000000000000000000000000000..dd8611ba196c2a6b43925faead2f0feb6951ccde --- /dev/null +++ b/src/action/copy.py @@ -0,0 +1,32 @@ + + +def copy(pkg_path: str, pkg_name: str, data_list: list, froms: str, tos: str) -> list: + froms = froms.replace("\\", "/") + if len(froms) > 1 and froms[-1] == '/': + froms = froms[:-1] + tos = tos.replace("\\", "/") + if len(tos) > 1 and tos[-1] == '/': + tos = tos[:-1] + datas = [] + data_dir = config.DATAPATH.format(DISK=utils.find_disk())+"/"+pkg_name + + data_check = [i["from"] for i in data_list] + + def search_dir(path: str): # dfs search + if froms+path in data_check: + datas.append( + (4, pkg_path+"/"+froms+path, data_dir+"/"+data_list[data_check.index(froms+path)]["to"])) + else: + datas.append((1, tos+path)) + for i in os.listdir(pkg_path+"/"+froms+path): + if os.path.isfile(pkg_path+"/"+froms+path+"/"+i): + if froms+path+"/"+i in data_check: + datas.append( + (3, pkg_path+"/"+froms+path+"/"+i, data_dir+data_list[data_check.index(froms+path+"/"+i)]["to"])) + else: + datas.append( + (3, pkg_path+"/"+froms+path+"/"+i, tos+path+"/"+i)) + else: + search_dir(path+"/"+i) + search_dir("") + return datas diff --git a/src/action.py b/src/action/do_action.py similarity index 35% rename from src/action.py rename to src/action/do_action.py index b8b7260b7b0cfdf92321a9cf490d8784dd98c4db..b9002d132ded3be8caad27d2cfdca34bcfebc473 100644 --- a/src/action.py +++ b/src/action/do_action.py @@ -1,119 +1,3 @@ -##################################### -## PEinjector/action ## -##################################### - -# Actions ID Info: -# .----+--------------+--------------------------. -# | ID | Args | Actions | -# +====+==============+==========================+ -# | 0 | pkg_name | start load pkg (for log) | -# +----+--------------+--------------------------+ -# | 1 | path | mkdir | -# +----+--------------+--------------------------+ -# | 2 | from, to | copy | -# +----+--------------+--------------------------+ -# | 3 | from, to | link | -# +----+--------------+--------------------------+ -# | 4 | from, to | link | -# +----+--------------+--------------------------+ -# | 5 | command | start | -# +----+--------------+--------------------------+ -# | 6 |path,icon,name| make shortcut on Desktop | -# +----+--------------+--------------------------+ -# | 7 | path | add to %PATH% | -# +----+--------------+--------------------------+ -# | 8 | regpath | add file to reg | -# '----+--------------+--------------------------' - - -import os -import config -import utils -import pickle -import log -import shutil -import subprocess -import makelnk -import regwrite - - -def force_copy(pkg_path: str, pkg_name: str, data_list: list, froms: str, tos: str) -> list[tuple]: - froms = froms.replace("\\", "/") - if len(froms) > 1 and froms[-1] == '/': - froms = froms[:-1] - tos = tos.replace("\\", "/") - if len(tos) > 1 and tos[-1] == '/': - tos = tos[:-1] - datas = [] - data_dir = config.DATAPATH.format(DISK=utils.find_disk())+"/"+pkg_name - - data_check = [i["from"] for i in data_list] - - def search_dir(path): - if froms+path in data_check: - datas.append( - (4, pkg_path+"/"+froms+path, data_dir+"/"+data_list[data_check.index(froms+path)]["to"])) - else: - datas.append((1, tos+path)) - for i in os.listdir(pkg_path+"/"+froms+path): - if os.path.isfile(pkg_path+"/"+froms+path+"/"+i): - if froms+path+"/"+i in data_check: - datas.append( - (3, pkg_path+"/"+froms+path+"/"+i, data_dir+data_list[data_check.index(froms+path+"/"+i)]["to"])) - else: - datas.append( - (2, pkg_path+"/"+froms+path+"/"+i, tos+path+"/"+i)) - else: - search_dir(path+"/"+i) - search_dir("") - return datas - - -def copy(pkg_path: str, pkg_name: str, data_list: list, froms: str, tos: str) -> list: - froms = froms.replace("\\", "/") - if len(froms) > 1 and froms[-1] == '/': - froms = froms[:-1] - tos = tos.replace("\\", "/") - if len(tos) > 1 and tos[-1] == '/': - tos = tos[:-1] - datas = [] - data_dir = config.DATAPATH.format(DISK=utils.find_disk())+"/"+pkg_name - - data_check = [i["from"] for i in data_list] - - def search_dir(path: str): # dfs search - if froms+path in data_check: - datas.append( - (4, pkg_path+"/"+froms+path, data_dir+"/"+data_list[data_check.index(froms+path)]["to"])) - else: - datas.append((1, tos+path)) - for i in os.listdir(pkg_path+"/"+froms+path): - if os.path.isfile(pkg_path+"/"+froms+path+"/"+i): - if froms+path+"/"+i in data_check: - datas.append( - (3, pkg_path+"/"+froms+path+"/"+i, data_dir+data_list[data_check.index(froms+path+"/"+i)]["to"])) - else: - datas.append( - (3, pkg_path+"/"+froms+path+"/"+i, tos+path+"/"+i)) - else: - search_dir(path+"/"+i) - search_dir("") - return datas - - -def start(pkg_name: str, data_list: list, cmd: str) -> list: - return [(5, cmd)] - - -def save_action(actions_list: list) -> None: - tmp_filename = "PEinjector.tmp" - log.info(f"save action list to {config.TEMP_DIR.format( - TEMP="X:\\PEinjector")+'\\'+tmp_filename}") - with open(config.TEMP_DIR.format(TEMP="X:\\PEinjector")+"\\"+tmp_filename, "wb") as file: - pickle.dump(actions_list, file) - return tmp_filename - - def do_action(actions_list: list) -> tuple: removeinfo = {} actionlogs = "----- Action Log Start -----\r\n" @@ -147,8 +31,8 @@ def do_action(actions_list: list) -> tuple: f"\"{i[2]}\" failed: {repr(exp)}\r\n" elif i[0] == 4: try: - assert os.system(f"mklink /J {i[1].replace( - '/', '\\')} {i[2].replace('/', '\\')} > nul") == 0, "mklink return not 0" + new_str:str = "\\" + assert os.system(f"mklink /J {i[1].replace('/',new_str)} {i[2].replace('/', new_str)} > nul") == 0, "mklink return not 0" except Exception as exp: actionlogs = actionlogs + \ f" link(J) \"{i[1]}\" to " + \ diff --git a/src/action/force_copy.py b/src/action/force_copy.py new file mode 100644 index 0000000000000000000000000000000000000000..98c1fbbace3faeae786b59c8bdcb8b133e7102ef --- /dev/null +++ b/src/action/force_copy.py @@ -0,0 +1,30 @@ +def force_copy(pkg_path: str, pkg_name: str, data_list: list, froms: str, tos: str) -> list[tuple]: + froms = froms.replace("\\", "/") + if len(froms) > 1 and froms[-1] == '/': + froms = froms[:-1] + tos = tos.replace("\\", "/") + if len(tos) > 1 and tos[-1] == '/': + tos = tos[:-1] + datas = [] + data_dir = config.DATAPATH.format(DISK=utils.find_disk())+"/"+pkg_name + + data_check = [i["from"] for i in data_list] + + def search_dir(path): + if froms+path in data_check: + datas.append( + (4, pkg_path+"/"+froms+path, data_dir+"/"+data_list[data_check.index(froms+path)]["to"])) + else: + datas.append((1, tos+path)) + for i in os.listdir(pkg_path+"/"+froms+path): + if os.path.isfile(pkg_path+"/"+froms+path+"/"+i): + if froms+path+"/"+i in data_check: + datas.append( + (3, pkg_path+"/"+froms+path+"/"+i, data_dir+data_list[data_check.index(froms+path+"/"+i)]["to"])) + else: + datas.append( + (2, pkg_path+"/"+froms+path+"/"+i, tos+path+"/"+i)) + else: + search_dir(path+"/"+i) + search_dir("") + return datas diff --git a/src/action/save_action.py b/src/action/save_action.py new file mode 100644 index 0000000000000000000000000000000000000000..e555379fc3373b59f07c0c5088e71099820e277f --- /dev/null +++ b/src/action/save_action.py @@ -0,0 +1,7 @@ +def save_action(actions_list: list) -> None: + tmp_filename = "PEinjector.tmp" + old_str:str = '\\' + log.info(f"save action list to {config.TEMP_DIR.replace('{TEMP}', os.environ['TEMP'].replace(old_str,'/'))+'/'+tmp_filename}") + with open(config.TEMP_DIR.replace("{TEMP}", os.environ["TEMP"])+"/"+tmp_filename, "wb") as file: + pickle.dump(actions_list, file) + return tmp_filename diff --git a/src/hook.pyw b/src/hook.pyw index 102f249b5fd932c74d5f9bca916dc758d5048e06..ef0b6e814b85e460c4a1a7f0466c5cfab45d5a49 100644 --- a/src/hook.pyw +++ b/src/hook.pyw @@ -43,7 +43,7 @@ print(logo) # 初始化日志系统 log.init() -log.info("hook start") +log.info("Hook start") try: # 等待动画 sys.stdout.write("\033[3A \033[37m") @@ -82,7 +82,7 @@ try: regwrite.refresh_path() # 加载完成 - log.info("done") + log.info("Done.") except Exception as exp: # 未知错误 log.break_err("Exception \n"+str(traceback.format_exc(exp))) raise exp diff --git a/src/loader/__init__.py b/src/loader/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..51ed7bfeed700b84cbe5cbfe422c078c3de14500 --- /dev/null +++ b/src/loader/__init__.py @@ -0,0 +1,42 @@ +##################################### +## PEinjector/loader ## +##################################### + +import utils +import os +import shutil +import log +import json +import traceback +import config +import action + +loaded_package = [] # 加载过包 +loaderr_pkgs = [] # 加载错误的包 +disk = "" # PEinjector 安装盘 +lists = [] # 软件包列表 + +def load(): + try: + global disk, lists + log.info("start load") + disk = utils.find_disk() + lists = os.listdir(f"{disk}/PEinjector/package") + # 读取禁用包列表 + with open(config.DISABLEPATH.format(DISK=disk), "r") as file: + disable_packages = [i.rstrip("\n\r") for i in file.readlines()] + for packs in lists: # 加载包 + if packs not in loaded_package and packs not in disable_packages: + log.info(f"load moudle [{packs}]") + retvar = load_package(packs) + if retvar == 0: + loaderr_pkgs.append(packs) + # 保存并执行action + action.save_action(actions["onload"]) + alog = action.do_action(actions["onboot"])[0] + # 写action日志 + with open(config.ACTIONLOGPATH.format(DISK=utils.find_disk()), "w") as file: + file.write(alog) + except Exception as exp: # 未知错误 + log.break_err("Exception \n"+str(traceback.format_exc(exp))) + raise exp diff --git a/src/loader/file_check.py b/src/loader/file_check.py new file mode 100644 index 0000000000000000000000000000000000000000..afb6b4e1fc68e8278ef9783d049c628a099c39c6 --- /dev/null +++ b/src/loader/file_check.py @@ -0,0 +1,22 @@ +def file_check(file_json: dict, pkg_name: str) -> int: + try: + for i in file_json.get("compatibility", {}).get("file", {}).get("must", []): + if not os.path.exists(i): # 找不到文件 + log.warn(f"load moudle [{pkg_name}] failed: " + + f"Cannot find file: [{i}]") + return 8 + for i in file_json.get("compatibility", {}).get("file", {}).get("mustnot", []): + if os.path.exists(i): # 找到不兼容文件 + log.warn(f"load moudle [{pkg_name}] failed: " + + f"Find incompatible file: [{i}]") + return 9 + for i in file_json.get("compatibility", {}).get("file", {}).get("loaded", []): + if os.path.exists(i): # 已经无需加载 + log.info(f"jump moudle [{pkg_name}]: " + + f"Find compatible file: [{i}]") + return -1 + except: # 未知错误 + log.warn(f"load moudle [{pkg_name}] failed: " + + "Unknown error in file check") + return 10 + return 0 diff --git a/src/loader.py b/src/loader/load_package.py similarity index 72% rename from src/loader.py rename to src/loader/load_package.py index c568badb8f69a10831a57478c1e3bfd2cfdb56d6..5c75c6424684ebb6f67f4c6cb8c52a5c45574f1a 100644 --- a/src/loader.py +++ b/src/loader/load_package.py @@ -1,86 +1,3 @@ -##################################### -## PEinjector/loader ## -##################################### - -import utils -import os -import shutil -import log -import json -import traceback -import config -import action - -loaded_package = [] # 加载过包 -loaderr_pkgs = [] # 加载错误的包 -disk = "" # PEinjector 安装盘 -lists = [] # 软件包列表 - - -def __version_parse(version: str) -> list: - return [int(i) for i in version.split(".")] - - -def __version_compare(ver1: str, ver2: str) -> int: - version1 = __version_parse(ver1) - version2 = __version_parse(ver2) - for (i, j) in zip(version1, version2): - if i != j: - return 1 if i > j else -1 - return 0 - - -def version_check(configuration: dict, pkg_name: str) -> int: - # 消息格式 - ERROR_MESSAGE = "load moudle [{}] failed: PEinjector version too {}, need [{}]" - try: - if not "compatibility" in configuration or not "injector" in configuration["compatibility"]: - return 0 - with open(f"{disk}/PEinjector/VERSION", 'r', encoding="utf-8") as f: - version = f.readlines()[0].rstrip("\n\r") - - if "min" in configuration["compatibility"]["injector"]: - plugver = configuration["compatibility"]["injector"]["min"] - if __version_compare(version, plugver) == -1: # 版本过低 - log.warn(ERROR_MESSAGE.format(pkg_name, "low", plugver)) - return 5 - - if "max" in configuration["compatibility"]["injector"]: - plugver = configuration["compatibility"]["injector"]["max"] - if __version_compare(version, plugver) == 1: # 版本或高 - log.warn(ERROR_MESSAGE.format(pkg_name, "high", plugver)) - return 6 - - except Exception as e: # 未知错误 - log.warn(f"load moudle [{pkg_name}] failed: {e}") - return 7 - return 0 - - -def file_check(file_json: dict, pkg_name: str) -> int: - try: - for i in file_json.get("compatibility", {}).get("file", {}).get("must", []): - if not os.path.exists(i): # 找不到文件 - log.warn(f"load moudle [{pkg_name}] failed: " + - f"Cannot find file: [{i}]") - return 8 - for i in file_json.get("compatibility", {}).get("file", {}).get("mustnot", []): - if os.path.exists(i): # 找到不兼容文件 - log.warn(f"load moudle [{pkg_name}] failed: " + - f"Find incompatible file: [{i}]") - return 9 - for i in file_json.get("compatibility", {}).get("file", {}).get("loaded", []): - if os.path.exists(i): # 已经无需加载 - log.info(f"jump moudle [{pkg_name}]: " + - f"Find compatible file: [{i}]") - return -1 - except: # 未知错误 - log.warn(f"load moudle [{pkg_name}] failed: " + - "Unknown error in file check") - return 10 - return 0 - - actions = {"onboot": [], "onload": []} @@ -293,29 +210,3 @@ def load_package(pkg_name: str) -> int: "/"+i # 添加 actions["onload"].append((8, i)) - - -def load(): - try: - global disk, lists - log.info("start load") - disk = utils.find_disk() - lists = os.listdir(f"{disk}/PEinjector/package") - # 读取禁用包列表 - with open(config.DISABLEPATH.format(DISK=disk), "r") as file: - disable_packages = [i.rstrip("\n\r") for i in file.readlines()] - for packs in lists: # 加载包 - if packs not in loaded_package and packs not in disable_packages: - log.info(f"load moudle [{packs}]") - retvar = load_package(packs) - if retvar == 0: - loaderr_pkgs.append(packs) - # 保存并执行action - action.save_action(actions["onload"]) - alog = action.do_action(actions["onboot"])[0] - # 写action日志 - with open(config.ACTIONLOGPATH.format(DISK=utils.find_disk()), "w") as file: - file.write(alog) - except Exception as exp: # 未知错误 - log.break_err("Exception \n"+str(traceback.format_exc(exp))) - raise exp diff --git a/src/loader/version_check.py b/src/loader/version_check.py new file mode 100644 index 0000000000000000000000000000000000000000..c8e566fb89736b673ae099cd5f0ad5dc6ed73099 --- /dev/null +++ b/src/loader/version_check.py @@ -0,0 +1,38 @@ +def __version_parse(version: str) -> list: + return [int(i) for i in version.split(".")] + + +def __version_compare(ver1: str, ver2: str) -> int: + version1 = __version_parse(ver1) + version2 = __version_parse(ver2) + for (i, j) in zip(version1, version2): + if i != j: + return 1 if i > j else -1 + return 0 + + +def version_check(configuration: dict, pkg_name: str) -> int: + # 消息格式 + ERROR_MESSAGE = "load moudle [{}] failed: PEinjector version too {}, need [{}]" + try: + if not "compatibility" in configuration or not "injector" in configuration["compatibility"]: + return 0 + with open(f"{disk}/PEinjector/VERSION", 'r', encoding="utf-8") as f: + version = f.readlines()[0].rstrip("\n\r") + + if "min" in configuration["compatibility"]["injector"]: + plugver = configuration["compatibility"]["injector"]["min"] + if __version_compare(version, plugver) == -1: # 版本过低 + log.warn(ERROR_MESSAGE.format(pkg_name, "low", plugver)) + return 5 + + if "max" in configuration["compatibility"]["injector"]: + plugver = configuration["compatibility"]["injector"]["max"] + if __version_compare(version, plugver) == 1: # 版本或高 + log.warn(ERROR_MESSAGE.format(pkg_name, "high", plugver)) + return 6 + + except Exception as e: # 未知错误 + log.warn(f"load moudle [{pkg_name}] failed: {e}") + return 7 + return 0 diff --git a/src/main.py b/src/main.py index 269312d368265d74ed0b8d6e454381c538eab1b6..898cc92aab62f3d935fdbd57cb2e1244dcf0f3b6 100644 --- a/src/main.py +++ b/src/main.py @@ -19,4 +19,4 @@ log.init() # This is written to avoid bad formatting. # Do not change it. -__import__("loader").load() +__import__("loader").load() \ No newline at end of file