添加“添加快捷方式”功能

This commit is contained in:
cxykevin 2024-02-10 12:55:33 +08:00
parent dd2b6410d2
commit 6df3c9a197
4 changed files with 2097 additions and 3 deletions

View File

@ -17,6 +17,10 @@
# | 4 | from, to | link <path> |
# +----+--------------+--------------------------+
# | 5 | command | start |
# +----+--------------+--------------------------+
# | 6 |path,icon,name| make shortcut on Desktop |
# +----+--------------+--------------------------+
# | 7 | path | add <path> to %PATH% |
# '----+--------------+--------------------------'
@ -28,6 +32,7 @@ import log
import uuid
import shutil
import subprocess
import makelnk
def force_copy(pkg_path, pkg_name, data_list, froms, tos):
@ -101,7 +106,7 @@ def start(pkg_name, data_list, cmd):
def save_action(actions_list):
tmp_filename = str(uuid.uuid1())+".tmp" # hind pickle file
tmp_filename = "PEinjector.tmp"
log.info(f"save action list to {config.TEMP_DIR.replace(
'{TEMP}', os.environ['TEMP'].replace("\\", "/"))+'/'+tmp_filename}")
with open(config.TEMP_DIR.replace("{TEMP}", os.environ["TEMP"])+"/"+tmp_filename, "wb") as file:
@ -149,4 +154,11 @@ def do_action(actions_list):
except Exception as exp:
actionlogs = actionlogs + \
f" start \"{i[1]}\" failed: {repr(exp)}\r\n"
elif i[0] == 6:
try:
makelnk.makelnk(i[3],
i[1].replace("\\", "/"), i[2].replace("\\", "/"))
except:
actionlogs = actionlogs + \
f" make shortcut \"{i[1]}\" failed: {repr(exp)}\r\n"
return actionlogs

View File

@ -174,6 +174,8 @@ def load_package(pkg_name):
shutil.copytree(
pkg_path+"/"+i["from"], data_dir+"/"+i["to"])
add_action_head = False
if "load" in file_json:
open_symlink = config.USE_SYMLINK
if "symlink" in file_json["load"] and file_json["load"] == False:
@ -183,6 +185,8 @@ def load_package(pkg_name):
if event not in file_json["load"]["mode"]:
continue
actions[event].append((0, pkg_name))
if event == "onload":
add_action_head = True
if type(file_json["load"]["mode"][event]) != list:
log.warn(f"load moudle [{pkg_name}] failed: " +
f"Load commands syntax error on {event} (must be a list)")
@ -241,6 +245,22 @@ def load_package(pkg_name):
else:
log.warn(f"load moudle [{pkg_name}] warning: " +
f"Load commands syntax error on {event}[{j+1}] (unknown type), igrone")
if "start" in file_json:
if not add_action_head:
actions[event].append((0, pkg_name))
if "icon" in file_json["start"]:
for i in ("command", "name", "icon"):
if i not in file_json["start"]["icon"]:
log.warn(f"load moudle [{pkg_name}] warning: " +
f"Load icon syntax error (lost \"{i}\"), igrone")
if len(file_json["start"]["icon"]["icon"]) < 2 or file_json["start"]["icon"]["icon"][1] != ':':
file_json["start"]["icon"]["icon"] = pkg_path + \
"/"+file_json["start"]["icon"]["icon"]
if len(file_json["start"]["icon"]["command"]) < 2 or file_json["start"]["icon"]["command"][1] != ':':
file_json["start"]["icon"]["command"] = pkg_path + \
"/"+file_json["start"]["icon"]["command"]
actions["onload"].append(
(6, file_json["start"]["icon"]["command"], file_json["start"]["icon"]["icon"], file_json["start"]["icon"]["name"]))
def load():
@ -257,11 +277,10 @@ def load():
retvar = load_package(packs)
if retvar == 0:
loaderr_pkgs.append(packs)
filename = action.save_action(actions["onload"])
action.save_action(actions["onload"])
alog = action.do_action(actions["onboot"])
with open(config.ACTIONLOGPATH.replace("{DISK}", utils.find_disk()), "w") as file:
file.write(alog)
os.system(f'..\\env\\pythonw.exe hook.py "{filename}"')
except Exception as exp:
log.break_err("Exception \n"+str(traceback.format_exc(exp)))
raise exp

24
src/makelnk.py Normal file
View File

@ -0,0 +1,24 @@
import os
import log
import pylnk3
# Thank the project "pylnk3"
sysdrive = "X:"
if not os.path.exists(sysdrive+"\\"):
sysdrive = "C:"
username = "Default"
for i in os.listdir(f"{sysdrive}\\Users"):
if i not in ("Default", "Default User", "Public") and os.path.exists(f"{sysdrive}\\Users\\{i}\\Desktop"):
username = i
def makelnk(name, exepath, iconpath):
print(f"{sysdrive}\\Users\\{username}" +
f"\\Desktop\\{name}.lnk")
log.info(f"make shortcut \"{name}\"")
pylnk3.for_file(exepath,
lnk_name=f"{sysdrive}\\Users\\{username}" +
f"\\Desktop\\{name}.lnk",
icon_file=iconpath
)

2039
tool/pylnk3.py Normal file

File diff suppressed because it is too large Load Diff