Skip to content

Commit fcca67d

Browse files
committed
refactor: 重构插件系统为独立进程模型
1 parent f69e073 commit fcca67d

File tree

17 files changed

+31
-1189
lines changed

17 files changed

+31
-1189
lines changed

src/main.py

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
import ms_toollib as ms
1313
import ctypes
1414
# from ctypes import wintypes
15-
from mp_plugins.context import AppContext
16-
from mp_plugins.events import *
17-
from mp_plugins import PluginManager
1815
from pathlib import Path
1916
from utils import get_paths, patch_env
2017

21-
os.environ["QT_FONT_DPI"] = "96"
22-
18+
# 插件系统(新)
19+
from plugin_manager import GameServerBridge
20+
from plugin_manager.app_paths import get_env_for_subprocess
21+
import subprocess
2322

23+
os.environ["QT_FONT_DPI"] = "96"
2424

2525

2626
def on_new_connection(localServer: QLocalServer):
@@ -150,31 +150,42 @@ def cli_check_file(file_path: str) -> int:
150150
localServer.newConnection.connect(
151151
lambda: on_new_connection(localServer=localServer)
152152
)
153-
env = patch_env()
154-
context = AppContext(name="Metasweeper", version="1.0.0", display_name="元扫雷",
155-
plugin_dir=(Path(get_paths()) /
156-
"plugins").as_posix(),
157-
app_dir=get_paths()
158-
)
159-
PluginManager.instance().context = context
160-
161-
PluginManager.instance().start(Path(get_paths()) / "plugins", env)
162153
mainWindow = mainWindowGUI.MainWindow()
163154
ui = mineSweeperGUI.MineSweeperGUI(mainWindow, sys.argv)
164155
ui.mainWindow.show()
165-
# ui.mainWindow.game_setting = ui.game_setting
156+
157+
# ── 启动 ZMQ Server + 插件管理器 ──
158+
game_server = GameServerBridge(ui)
159+
plugin_process = subprocess.Popen(
160+
[sys.executable, "-m", "plugin_manager", "--mode", "tray"],
161+
cwd=os.path.dirname(os.path.abspath(__file__)),
162+
env=get_env_for_subprocess(),
163+
)
164+
ui._plugin_process = plugin_process # 保存引用,防止被 GC
165+
166+
# 连接信号:插件发来的新游戏指令 → 主线程处理
167+
game_server.signals.new_game_requested.connect(lambda r, c, m: None) # TODO: 接入游戏逻辑
168+
169+
game_server.start()
166170

167171
# _translate = QtCore.QCoreApplication.translate
168172
hwnd = int(ui.mainWindow.winId())
169173

170174
SetWindowDisplayAffinity = ctypes.windll.user32.SetWindowDisplayAffinity
171175
ui.disable_screenshot = lambda: ... if SetWindowDisplayAffinity(
172176
hwnd, 0x00000011) else 1/0
173-
ui.enable_screenshot = lambda: ... if SetWindowDisplayAffinity(
174-
hwnd, 0x00000000) else 1/0
175-
app.aboutToQuit.connect(PluginManager.instance().stop)
177+
ui.enable_screenshot = lambda: (
178+
... if SetWindowDisplayAffinity(hwnd, 0x00000000) else 1 / 0
179+
)
180+
181+
def _cleanup():
182+
game_server.stop()
183+
if plugin_process.poll() is None:
184+
plugin_process.terminate()
185+
plugin_process.wait(timeout=5)
186+
187+
app.aboutToQuit.connect(_cleanup)
176188
sys.exit(app.exec_())
177-
...
178189
# except:
179190
# pass
180191

src/mineSweeperGUI.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@
3232
from mainWindowGUI import MainWindow
3333
from datetime import datetime
3434
from mineSweeperVideoPlayer import MineSweeperVideoPlayer
35-
from pluginDialog import PluginManagerUI
36-
from mp_plugins import PluginManager, PluginContext
37-
from mp_plugins.events import GameEndEvent
3835

3936

4037
class MineSweeperGUI(MineSweeperVideoPlayer):
@@ -560,16 +557,6 @@ def gameFinished(self):
560557
status = utils.GameBoardState(ms_board.game_board_state)
561558
if status == utils.GameBoardState.Win:
562559
self.dump_evf_file_data()
563-
event = GameEndEvent()
564-
data = msgspec.structs.asdict(event)
565-
for key in data:
566-
if hasattr(ms_board, key):
567-
if key == "raw_data":
568-
data[key] = base64.b64encode(
569-
ms_board.raw_data).decode("utf-8")
570-
data[key] = getattr(ms_board, key)
571-
event = GameEndEvent(**data)
572-
PluginManager.instance().send_event(event, response_count=0)
573560

574561
def gameWin(self): # 成功后改脸和状态变量,停时间
575562
self.timer_10ms.stop()
@@ -1425,5 +1412,4 @@ def closeEvent_(self):
14251412
self.record_setting.sync()
14261413

14271414
def action_OpenPluginDialog(self):
1428-
dialog = PluginManagerUI(PluginManager.instance().Get_Plugin_Names())
1429-
dialog.exec()
1415+
pass

src/mp_plugins/__init__.py

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/mp_plugins/base/__init__.py

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/mp_plugins/base/_data.py

Lines changed: 0 additions & 36 deletions
This file was deleted.

src/mp_plugins/base/config.py

Lines changed: 0 additions & 51 deletions
This file was deleted.

src/mp_plugins/base/context.py

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/mp_plugins/base/error.py

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/mp_plugins/base/event.py

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/mp_plugins/base/message.py

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)