injectorHub/ui/win.py

200 lines
6.6 KiB
Python
Raw Normal View History

2024-05-04 21:27:20 +08:00
import shutil
2024-05-03 09:08:46 +08:00
from tkinter import messagebox
from magictk import mtk
from logger.logger import *
2024-05-03 18:47:22 +08:00
from logger import config
2024-05-03 09:08:46 +08:00
from ui import nav
from ui import installer
2024-05-04 21:27:20 +08:00
from ui import homeui
2024-05-04 22:28:55 +08:00
from ui import injectimg
2024-05-03 09:08:46 +08:00
from lib import get_disk
2024-05-04 21:27:20 +08:00
from lib import vars
2024-05-03 09:08:46 +08:00
from ui.lang import load_lang, l
import sys
import getpass
import platform
import ctypes
import os
import locale
2024-05-03 18:47:22 +08:00
import traceback
import textwrap
2024-05-03 09:08:46 +08:00
nlang = locale.getdefaultlocale()[0].lower()
2024-05-03 18:47:22 +08:00
def check_args(arg, check):
if len(arg) >= len(check):
if (arg[:len(check)] == check):
return 1
return 0
DISPERMISSION = 0
DEBUG = 0
NOTRACE = 0
2024-05-04 21:27:20 +08:00
UPGRADE = 0
2024-05-04 22:07:05 +08:00
DEVICE = ""
SHOW_VERSION = 0
2024-05-03 18:47:22 +08:00
for i in sys.argv[1:]:
2024-05-04 22:07:05 +08:00
if (check_args(i, "/?") or check_args(i, "--help")):
print(""" injector Hub
Usage: hub [options]
Options:
--help Show this help info and exit
--version, -V Get hub version
--lang=<zh_cn/en_us> Set language
2024-05-09 22:30:35 +08:00
--install, -I Install PEinjector
2024-05-04 22:07:05 +08:00
--upgrade, -U Upgrade PEinjector
--device, -D Set device (mount path)
--clean Clean download cache
2024-05-09 22:30:35 +08:00
--show-all-img Show all images in inject page
2024-05-04 22:07:05 +08:00
Debug Options:
--debug Open debug mode (raise all errors)
--dis-permission-error Do not show permission error
--log-level=<level> Set log level (debug,info,warning,err)
--no-trace Do not trace OS version
""")
sys.exit()
elif (check_args(i, "--lang")):
2024-05-03 18:47:22 +08:00
nlang = i.split("=")[-1]
2024-05-04 21:27:20 +08:00
elif (check_args(i, "--dis-permission-error")):
2024-05-03 18:47:22 +08:00
DISPERMISSION = 1
2024-05-04 21:27:20 +08:00
elif (check_args(i, "--log-level")):
2024-05-03 18:47:22 +08:00
config.LOGLEVEL = i.split("=")[-1]
2024-05-04 21:27:20 +08:00
elif (check_args(i, "--debug")):
2024-05-03 18:47:22 +08:00
DEBUG = 1
2024-05-04 21:27:20 +08:00
elif (check_args(i, "--no-trace")):
2024-05-03 18:47:22 +08:00
NOTRACE = 1
2024-05-04 22:07:05 +08:00
elif (check_args(i, "--upgrade") or check_args(i, "-U")):
2024-05-04 21:27:20 +08:00
UPGRADE = 1
2024-05-09 22:30:35 +08:00
elif (check_args(i, "--install") or check_args(i, "-I")):
2024-05-04 22:07:05 +08:00
UPGRADE = 2
2024-05-09 22:30:35 +08:00
elif (check_args(i, "--show-all-img")):
vars.SHOWALLIMAGE = True
2024-05-04 22:07:05 +08:00
elif (check_args(i, "--device") or check_args(i, "-D")):
DEVICE = i.split("=")[-1]
elif (check_args(i, "--clean")):
if (os.path.exists("software")):
info("[win]clean install cache")
shutil.rmtree("software")
sys.exit()
elif (check_args(i, "--version") or check_args(i, "-V")):
SHOW_VERSION = 1
else:
print("Unknown Command: \""+i+"\"\nUse Command \"--help\" to get info")
sys.exit(128)
if (nlang+".toml" not in os.listdir("lang")):
nlang = "en_us"
2024-05-03 09:08:46 +08:00
def main():
2024-05-03 18:47:22 +08:00
log_init()
if (DEBUG == 1):
info("[win]< debug mode >")
if (NOTRACE == 0):
info("[trace]open trace")
trace_sys(f"[trace]OS={platform.system()}")
trace_sys(f"[trace]VER={platform.version()}")
trace_sys(f"[trace]CPU={platform.processor()}")
trace_sys(f"[trace]ARCH={str(platform.architecture()[0])}")
trace_sys(f"[trace]PYVER={platform.python_version()}")
trace_sys(f"[trace]PYIMP={platform.python_implementation()}")
load_lang(nlang)
disk_res = get_disk.find_hub_disk()
errno = disk_res[0]
2024-05-04 22:07:05 +08:00
if (DEVICE == ""):
root_disk = disk_res[1]
else:
root_disk = DEVICE
2024-05-03 18:47:22 +08:00
if (errno == 1):
messagebox.showerror("Injector Hub Error", root_disk)
sys.exit(1)
2024-05-04 22:07:05 +08:00
if (SHOW_VERSION == 1):
with open("VERSION", 'r') as file:
ver = file.read()
print(f" injector Hub \n\nHub Version: "+ver)
if (DEVICE == "" and errno == 2):
print("PEinjector Version: Not Found")
elif not (os.path.exists(root_disk+os.sep+"PEinjector"+os.sep+"VERSION")):
print("PEinjector Version: Not Found")
else:
with open(root_disk+os.sep+"PEinjector"+os.sep+"VERSION", 'r') as file:
pver = file.read()
print("PEinjector Version: "+pver)
sys.exit()
if (errno == 2 or UPGRADE == 1 or UPGRADE == 2):
2024-05-04 22:28:55 +08:00
if (UPGRADE == 1):
installer.UPGRADE = 1
else:
installer.UPGRADE = 0
2024-05-04 21:27:20 +08:00
if (UPGRADE == 0):
info("[win]cannot find installed disk")
2024-05-04 22:07:05 +08:00
elif (UPGRADE == 1):
2024-05-04 21:27:20 +08:00
info("[win]upgrade")
2024-05-03 18:47:22 +08:00
if DISPERMISSION == 0:
if (platform.system() == "Linux" and getpass.getuser() != "root"):
err("permission denied in linux")
messagebox.showerror("Injector Hub Error",
l("PermissionDeniedLinux"))
if (DEBUG == 0):
sys.exit(1)
else:
sys.exit()
if (platform.system() == "Windows" and ctypes.windll.shell32.IsUserAnAdmin() == False):
err("permission denied in windows")
messagebox.showerror("Injector Hub Error",
l("PermissionDeniedWin"))
if (DEBUG == 0):
sys.exit(1)
else:
sys.exit()
2024-05-04 21:27:20 +08:00
if (UPGRADE == 1):
if (os.path.exists("software")):
info("[win]clean install cache")
shutil.rmtree("software")
2024-05-03 18:47:22 +08:00
nav_list = [
{"icon": "lightning", "frame": installer.installer_ui, "side": "top"}
]
else:
2024-05-04 21:27:20 +08:00
vars.DISKMOUNT = root_disk
2024-05-03 18:47:22 +08:00
info(f"[win]found installed disk \"{root_disk}\"")
nav_list = [
2024-05-04 21:27:20 +08:00
{"icon": "home-filled", "frame": homeui.home_ui, "side": "top"},
2024-05-04 22:28:55 +08:00
{"icon": "dvd", "frame": injectimg.inject_ui, "side": "top"},
2024-05-03 18:47:22 +08:00
{"icon": "plugin", "frame": None, "side": "top"},
{"icon": "setting", "frame": None, "side": "bottom"}
]
info("[win]init window")
mtk.load_icon_pack("res/icon.pack")
main_win = mtk.Tk(title="Injector Hub", w=1000, h=800)
main_frame = mtk.Frame(main_win, w=10000)
nav.pack_nav(main_win, main_frame, nav_list)
main_frame.pack(side="left", fill="both", expand=True)
2024-05-03 09:08:46 +08:00
info("[win]mainloop")
main_win.mainloop()
2024-05-03 18:47:22 +08:00
def run():
try:
main()
except Exception as exp:
err("[errtrace] caught err:"+repr(exp))
for i in traceback.format_exc().split("\n"):
firstlen_flag = "* "
lastlen = ""
for j in textwrap.wrap(i, width=40):
err("[errtrace]"+" "+firstlen_flag+" " *
(len(lastlen)-len(lastlen.lstrip()))+j)
lastlen = " " * (len(lastlen)-len(lastlen.lstrip()))+j
firstlen_flag = " "
if (DEBUG == 1):
raise