92 lines
4.5 KiB
Python
92 lines
4.5 KiB
Python
"""应用页:快捷启动 + 虚拟机。"""
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import subprocess
|
|
|
|
from PySide6.QtWidgets import QFileDialog, QMessageBox
|
|
|
|
from .main import Page
|
|
|
|
ROWS = [("游戏", 55, "游戏路径"), ("模组", 117, "模组路径"), ("插件", 179, "插件路径"), ("其它", 241, "其它路径")]
|
|
|
|
|
|
class StartPage(Page):
|
|
key = "start"
|
|
|
|
def build(self) -> None:
|
|
win = self.win
|
|
self.fields = {}
|
|
for text, y, key in ROWS:
|
|
self.lbl(text, 109, y - 6, 40, 26)
|
|
e = self.edit(179, y, 470, 26, "", "双击输入框以选择文件")
|
|
e.setReadOnly(True)
|
|
e.mouseDoubleClickEvent = lambda ev, ed=e, k=key: self._pick(ed, k)
|
|
self.fields[key] = e
|
|
self.btn("启动", 1304, y, 70, 32, lambda ed=e: self._launch(ed))
|
|
|
|
self.lbl("虚拟机", 99, 337, 60, 26)
|
|
self.vmrun = self.edit(179, 310, 470, 27, "", "请选择VMware Workstation目录下的'vmrun.exe'文件")
|
|
self.vmrun.setReadOnly(True)
|
|
self.vmrun.mouseDoubleClickEvent = lambda ev: self._pick_file(self.vmrun, "vmrun路径", "vmrun.exe")
|
|
self.vmx = self.edit(179, 375, 470, 27, "", "请选择服务端虚拟机目录下扩展名为vmx的文件")
|
|
self.vmx.setReadOnly(True)
|
|
self.vmx.mouseDoubleClickEvent = lambda ev: self._pick_file(self.vmx, "虚拟机路径", "*.vmx")
|
|
|
|
self.btn("开机", 1304, 298, 70, 32, lambda: self._vm("start"), kind="Primary")
|
|
self.btn("关机", 1304, 340, 70, 32, lambda: self._vm("stop"))
|
|
self.btn("备份", 1304, 382, 70, 32, lambda: self._vm("snapshot"))
|
|
|
|
self.lbl("建议不要禁用VMware的开机自启,否则需要手动开启VMware后才能使用", 504, 442, 470, 18, dim=True)
|
|
self.lbl("路径尽量不要有中文,当然有中文或许也能执行,不保证", 559, 490, 400, 18, dim=True)
|
|
self.lbl("虚拟机相关的使用方法(无需打开VMware、SSH终端等): - 先配置好vmrun和vmx文件路径,点击\"开机\"按钮并等待一段时间 "
|
|
"(可通过\"测试SSH连接\"、 \"测试MySQL连接\"等方式判断是否完成开机) - 再前往\"SSH\"页面,点击\"一键启动\"来启动相关服务 "
|
|
"- 等待几分钟各服务均启动完成后(可通过\"查询所有服务状态\"按钮进行查询),便可进入游戏了",
|
|
88, 538, 1290, 50, dim=True)
|
|
|
|
# ------------------------------------------------------------------
|
|
def _pick(self, entry, key: str) -> None:
|
|
path, _ = QFileDialog.getOpenFileName(self, "选择程序", "", "应用|*.exe|所有文件|*.*")
|
|
if path:
|
|
entry.setText(path)
|
|
self.win.cfg.set("快捷启动", key, path)
|
|
self.win.cfg.save()
|
|
|
|
def _pick_file(self, entry, key: str, pattern: str) -> None:
|
|
path, _ = QFileDialog.getOpenFileName(self, "选择文件", "", f"{pattern}|{pattern}|所有文件|*.*")
|
|
if path:
|
|
entry.setText(path)
|
|
self.win.cfg.set("快捷启动", key, path)
|
|
self.win.cfg.save()
|
|
|
|
def _launch(self, entry) -> None:
|
|
path = entry.text().strip()
|
|
if not path or not os.path.isfile(path):
|
|
self.win.toast_msg(f"文件不存在:{path}", "error")
|
|
return
|
|
try:
|
|
os.startfile(path)
|
|
self.win.log(f"启动成功:{path}")
|
|
except Exception as exc:
|
|
self.win.toast_msg(f"启动时发生错误:{exc}", "error")
|
|
|
|
def _vm(self, action: str) -> None:
|
|
vmrun = self.vmrun.text().strip()
|
|
vmx = self.vmx.text().strip()
|
|
if not vmrun or not vmx:
|
|
self.win.toast_msg("请先配置 vmrun 和 vmx 路径", "warn")
|
|
return
|
|
if action == "stop" and QMessageBox.question(self, "提示", "确定要关闭虚拟机吗?") != QMessageBox.Yes:
|
|
return
|
|
if action == "snapshot" and QMessageBox.question(
|
|
self, "提示", "确定要备份虚拟机吗?\n这将会删除之前用本工具创建的快照,并拍摄新的快照") != QMessageBox.Yes:
|
|
return
|
|
cmd = {"start": [vmrun, "start", vmx, "nogui"],
|
|
"stop": [vmrun, "stop", vmx, "soft"],
|
|
"snapshot": [vmrun, "snapshot", vmx, "clone-snapshot"]}[action]
|
|
try:
|
|
subprocess.Popen(cmd, creationflags=getattr(subprocess, "CREATE_NO_WINDOW", 0))
|
|
self.win.log(f"vmrun {' '.join(action)} 已执行")
|
|
except Exception as exc:
|
|
self.win.toast_msg(f"vmrun 执行失败:{exc}", "error")
|