From 3325493ea66dc5ae15ce8c75c0d19663253fb95d Mon Sep 17 00:00:00 2001 From: undefined Date: Sat, 10 Feb 2024 12:28:20 +0000 Subject: [PATCH] update src/loader.py. Signed-off-by: undefined --- src/loader.py | 75 +++++++++++++++++++++++---------------------------- 1 file changed, 33 insertions(+), 42 deletions(-) diff --git a/src/loader.py b/src/loader.py index 252364c..0eb97d8 100644 --- a/src/loader.py +++ b/src/loader.py @@ -17,53 +17,44 @@ disk = "" lists = [] -def version_check(file_json, pkg_name): - try: - if "compatibility" in file_json: - if "injector" in file_json["compatibility"]: - with open(f"{disk}/PEinjector/VERSION", "r", encoding="utf-8") as file: - version = file.readlines()[0].rstrip("\n\r") - if "min" in file_json["compatibility"]["injector"]: - plugver = file_json["compatibility"]["injector"]["min"] +def __version_parse(version: str) -> list: + return [int(i) for i in version.split(".")] - def vererr(): - log.warn(f"load moudle [{pkg_name}] failed: " + - f"PEinjector version too low, need [{plugver}]") - if int(plugver.split(".")[0]) > int(version.split(".")[0]): - vererr() - return 5 - elif int(plugver.split(".")[0]) == int(version.split(".")[0]): - if plugver.split(".")[1] > version.split(".")[1]: - vererr() - return 5 - elif int(plugver.split(".")[1]) == int(version.split(".")[1]): - if int(plugver.split(".")[2]) > int(version.split(".")[2]): - vererr() - return 5 - if "max" in file_json["compatibility"]["injector"]: - plugver = file_json["compatibility"]["injector"]["max"] - def vererr(): - log.warn(f"load moudle [{pkg_name}] failed: " + - f"PEinjector version too high, need [{plugver}]") - if int(plugver.split(".")[0]) < int(version.split(".")[0]): - vererr() - return 6 - elif int(plugver.split(".")[0]) == int(version.split(".")[0]): - if int(plugver.split(".")[1] < version.split(".")[1]): - vererr() - return 6 - elif plugver.split(".")[1] == version.split(".")[1]: - if int(plugver.split(".")[2] < version.split(".")[2]): - vererr() - return 6 - except: - log.warn(f"load moudle [{pkg_name}] failed: " + - "The version number is formatted incorrectly") - return 7 +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 + + def file_check(file_json, pkg_name): try: if "compatibility" in file_json: