优化loader写法

This commit is contained in:
cxykevin 2024-02-10 20:54:35 +08:00
parent 397486253b
commit b6505aef27
1 changed files with 18 additions and 25 deletions

View File

@ -37,19 +37,19 @@ def version_check(configuration: dict, pkg_name: str) -> int:
return 0 return 0
with open(f"{disk}/PEinjector/VERSION", 'r', encoding="utf-8") as f: with open(f"{disk}/PEinjector/VERSION", 'r', encoding="utf-8") as f:
version = f.readlines()[0].rstrip("\n\r") version = f.readlines()[0].rstrip("\n\r")
if "min" in configuration["compatibility"]["injector"]: if "min" in configuration["compatibility"]["injector"]:
plugver = configuration["compatibility"]["injector"]["min"] plugver = configuration["compatibility"]["injector"]["min"]
if __version_compare(version, plugver) == -1: if __version_compare(version, plugver) == -1:
log.warn(ERROR_MESSAGE.format(pkg_name, "low", plugver)) log.warn(ERROR_MESSAGE.format(pkg_name, "low", plugver))
return 5 return 5
if "max" in configuration["compatibility"]["injector"]: if "max" in configuration["compatibility"]["injector"]:
plugver = configuration["compatibility"]["injector"]["max"] plugver = configuration["compatibility"]["injector"]["max"]
if __version_compare(version, plugver) == 1: if __version_compare(version, plugver) == 1:
log.warn(ERROR_MESSAGE.format(pkg_name, "high", plugver)) log.warn(ERROR_MESSAGE.format(pkg_name, "high", plugver))
return 6 return 6
except Exception as e: except Exception as e:
log.warn(f"load moudle [{pkg_name}] failed: {e}") log.warn(f"load moudle [{pkg_name}] failed: {e}")
return 7 return 7
@ -57,21 +57,16 @@ def version_check(configuration: dict, pkg_name: str) -> int:
def file_check(file_json, pkg_name): def file_check(file_json, pkg_name):
try: try:
if "compatibility" in file_json: for i in file_json.get("compatibility", {}).get("file", {}).get("must", []):
if "file" in file_json["compatibility"]: if not os.path.exists(i):
if "must" in file_json["compatibility"]["file"]: log.warn(f"load moudle [{pkg_name}] failed: " +
for i in file_json["compatibility"]["file"]["must"]: f"Cannot find file: [{i}]")
if not os.path.exists(i): return 8
log.warn(f"load moudle [{pkg_name}] failed: " + for i in file_json.get("compatibility", {}).get("file", {}).get("mustnot", []):
f"Cannot find file: [{i}]") if os.path.exists(i):
return 8 log.warn(f"load moudle [{pkg_name}] failed: " +
if "mustnot" in file_json["compatibility"]["file"]: f"Find incompatible file: [{i}]")
for i in file_json["compatibility"]["file"]["mustnot"]: return 9
if os.path.exists(i):
log.warn(f"load moudle [{pkg_name}] failed: " +
f"Find incompatible file: [{i}]")
return 9
except: except:
log.warn(f"load moudle [{pkg_name}] failed: " + log.warn(f"load moudle [{pkg_name}] failed: " +
"Unknown error in file check") "Unknown error in file check")
@ -91,17 +86,15 @@ def load_package(pkg_name):
return 1 return 1
try: try:
with open(pkg_path+"/"+"manifest.json", "r", encoding="utf-8") as file: with open(pkg_path+"/"+"manifest.json", "r", encoding="utf-8") as file:
file_str = file.read() file_json = json.load(json.decoder.JSONDecodeError)
except json.decoder.JSONDecodeError:
log.warn(f"load moudle [{pkg_name}] failed: " +
"Json syntax error")
return 3
except Exception as exp: except Exception as exp:
log.warn(f"load moudle [{pkg_name}] failed: " + log.warn(f"load moudle [{pkg_name}] failed: " +
"Unknown error in read file ("+repr(exp)+")") "Unknown error in read file ("+repr(exp)+")")
return 2 return 2
try:
file_json = json.loads(file_str)
except:
log.warn(f"load moudle [{pkg_name}] failed: " +
"Json syntax error")
return 3
for i in ("version", "name", "author", "introduce"): for i in ("version", "name", "author", "introduce"):
if i not in file_json: if i not in file_json:
log.warn(f"load moudle [{pkg_name}] failed: " + log.warn(f"load moudle [{pkg_name}] failed: " +