From c64fc81e1140a1ca59c5b3b096dcd4f85e6b2624 Mon Sep 17 00:00:00 2001 From: Mooling0602 Date: Wed, 6 Nov 2024 15:11:42 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E9=87=8D=E6=9E=84=20=09?= =?UTF-8?q?=E6=96=B0=E5=A2=9E:=20=20=20.github/workflows/build.yml=20=09?= =?UTF-8?q?=E4=BF=AE=E6=94=B9:=20=20=20config.ini=20=09=E4=BF=AE=E6=94=B9:?= =?UTF-8?q?=20=20=20doc/introduction.md=20=09=E6=96=B0=E5=A2=9E:=20=20=20m?= =?UTF-8?q?atrix=5Fsync/commands.py=20=09=E4=BF=AE=E6=94=B9:=20=20=20matri?= =?UTF-8?q?x=5Fsync/entry.py=20=09=E6=96=B0=E5=A2=9E:=20=20=20matrix=5Fsyn?= =?UTF-8?q?c/globals.py=20=09=E6=96=B0=E5=A2=9E:=20=20=20matrix=5Fsync/syn?= =?UTF-8?q?c/=5F=5Finit=5F=5F.py=20=09=E7=A7=BB=E5=8A=A8:=20=20=20=20matri?= =?UTF-8?q?x=5Fsync/receiver.py=20->=20matrix=5Fsync/sync/receiver.py=20?= =?UTF-8?q?=09=E7=A7=BB=E5=8A=A8:=20=20=20=20matrix=5Fsync/reporter.py=20-?= =?UTF-8?q?>=20matrix=5Fsync/sync/reporter.py=20=09=E4=BF=AE=E6=94=B9:=20?= =?UTF-8?q?=20=20mcdreforged.plugin.json=20=09=E4=BF=AE=E6=94=B9:=20=20=20?= =?UTF-8?q?requirements.txt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build.yml | 37 ++++++++++++ config.ini | 2 +- doc/introduction.md | 2 + matrix_sync/commands.py | 77 +++++++++++++++++++++++++ matrix_sync/entry.py | 90 +++--------------------------- matrix_sync/globals.py | 10 ++++ matrix_sync/sync/__init__.py | 2 + matrix_sync/{ => sync}/receiver.py | 4 +- matrix_sync/{ => sync}/reporter.py | 0 mcdreforged.plugin.json | 2 +- requirements.txt | 1 + 11 files changed, 141 insertions(+), 86 deletions(-) create mode 100644 .github/workflows/build.yml create mode 100644 matrix_sync/commands.py create mode 100644 matrix_sync/globals.py create mode 100644 matrix_sync/sync/__init__.py rename matrix_sync/{ => sync}/receiver.py (98%) rename matrix_sync/{ => sync}/reporter.py (100%) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..90e8dfe --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,37 @@ +name: 自动打包插件 + +on: + push: + branches: + - main # 触发打包的分支,可以根据需要修改 + pull_request: + branches: + - main # 可以修改为触发的 PR 分支 + +jobs: + build: + runs-on: ubuntu-latest # 运行环境 + + steps: + - name: Checkout 代码 + uses: actions/checkout@v3 + + - name: 设置 Python 环境 + uses: actions/setup-python@v4 + with: + python-version: '3.12' # 设置 Python 版本 + + - name: 安装依赖 + run: | + python -m pip install --upgrade pip + pip install -r requirements.txt # 安装依赖 + + - name: 打包插件 + run: | + bash pack_plugin.sh + + - name: 上传插件 + uses: actions/upload-artifact@v3 + with: + name: plugin + path: /*.mcdr # 上传 .mcdr 文件 \ No newline at end of file diff --git a/config.ini b/config.ini index 83b64f4..e29cc9b 100644 --- a/config.ini +++ b/config.ini @@ -3,6 +3,6 @@ [framework] ver=1 [main] -ver=2.3.1 +ver=2.4.0 [release] test=1 diff --git a/doc/introduction.md b/doc/introduction.md index 259df3d..62b2b4b 100644 --- a/doc/introduction.md +++ b/doc/introduction.md @@ -1 +1,3 @@ A MCDR (full name "MCDReforged") plugin sync messages between online game and Matrix groups. + +Version 2.2.0 fixes many long-pending issues and officially uses the Apache-2.0 open source license from that release. \ No newline at end of file diff --git a/matrix_sync/commands.py b/matrix_sync/commands.py new file mode 100644 index 0000000..f1beb1a --- /dev/null +++ b/matrix_sync/commands.py @@ -0,0 +1,77 @@ +import asyncio +import json +import matrix_sync.config + +from mcdreforged.api.all import * +from matrix_sync.globals import * +from matrix_sync.token import get_tip_read +from matrix_sync.sync.receiver import getMsg + +psi = ServerInterface.psi() + +# Help tips. +def help() -> RTextList: + return RTextList( + psi.rtr("matrix_sync.help_tips.title") + "\n", + psi.rtr("matrix_sync.help_tips.start_command") + "\n", + psi.rtr("matrix_sync.help_tips.stop_command") + "\n", + psi.rtr("matrix_sync.help_tips.closetip_command") + "\n" + ) + +# Manually run sync processes. +def manualSync(): + if not tLock.locked(): + start_room_msg() + psi.say(psi.rtr("matrix_sync.manual_sync.start_tip")) + read = asyncio.run(get_tip_read()) + if not read: + return RTextList( + psi.rtr("matrix_sync.manual_sync.start_sync") + "\n", + psi.rtr("matrix_sync.old_msg_sync") + "\n", + psi.rtr("matrix_sync.old_msg_sync2") + "\n", + psi.rtr("matrix_sync.old_msg_sync3") + "\n", + psi.rtr("matrix_sync.old_msg_sync4") + "\n", + psi.rtr("matrix_sync.old_msg_sync5") + "\n" + ) + else: + return psi.rtr("matrix_sync.manual_sync.start_sync") + else: + return psi.rtr("matrix_sync.manual_sync.start_error") + +# Manually stop sync processes. +def stopSync(src): + global sync_task + if src.is_console: + try: + if sync_task is not None: + sync_task.cancel() + return psi.rtr("matrix_sync.manual_sync.stop_sync") + else: + return psi.rtr("matrix_sync.manual_sync.not_running") + except Exception: + return psi.rtr("matrix_sync.manual_sync.stop_error") + else: + return psi.rtr("matrix_sync.manual_sync.stop_denied") + +def closeTip(): + TOKEN_FILE = matrix_sync.config.TOKEN_FILE + with open(TOKEN_FILE, "r") as f: + existing_data = json.load(f) + existing_data["tip_read"] = True + with open(TOKEN_FILE, "w") as f: + json.dump(existing_data, f) + return psi.rtr("matrix_sync.on_tip_read") + +# Sub thread to receive room messages from matrix without block main MCDR thread. +@new_thread('MatrixReceiver') +def start_room_msg(): + with tLock: + asyncio.run(on_room_msg()) + +async def on_room_msg(): + global sync_task + if sync_task is not None and not sync_task.done(): + sync_task.cancel() + await sync_task + sync_task = asyncio.create_task(getMsg()) + await sync_task \ No newline at end of file diff --git a/matrix_sync/entry.py b/matrix_sync/entry.py index 177a261..9efb577 100644 --- a/matrix_sync/entry.py +++ b/matrix_sync/entry.py @@ -1,24 +1,17 @@ import asyncio -import threading -import json import matrix_sync.config import matrix_sync.client -import matrix_sync.receiver -import matrix_sync.reporter +import matrix_sync.sync.receiver +import matrix_sync.sync.reporter +from matrix_sync.globals import * from matrix_sync.client import init from matrix_sync.config import load_config, check_config -from matrix_sync.receiver import getMsg -from matrix_sync.reporter import sender -from matrix_sync.token import get_tip_read +from matrix_sync.commands import * +from matrix_sync.sync.reporter import sender from mcdreforged.api.all import * -# Framwork ver: 2.3.0-1 -psi = ServerInterface.psi() -tLock = threading.Lock() -lock_is_None = matrix_sync.config.lock_is_None -cleaned = False -sync_task = None +# Framwork ver: 2.4.0-1 def on_load(server: PluginServerInterface, old): load_config() @@ -29,11 +22,9 @@ def on_load(server: PluginServerInterface, old): server.unload_plugin("matrix_sync") else: init() + server.register_help_message("!!msync", help()) server.register_command( Literal('!!msync') - .runs( - lambda src: src.reply(help()) - ) .then( Literal('start') .runs( @@ -60,59 +51,6 @@ def on_load(server: PluginServerInterface, old): ) ) server.logger.info(psi.rtr("matrix_sync.init_tips.hotload_tip")) - -# Help tips. -def help() -> RTextList: - return RTextList( - psi.rtr("matrix_sync.help_tips.title") + "\n", - psi.rtr("matrix_sync.help_tips.start_command") + "\n", - psi.rtr("matrix_sync.help_tips.stop_command") + "\n", - psi.rtr("matrix_sync.help_tips.closetip_command") + "\n" - ) - -# Manually run sync processes. -def manualSync(): - if not tLock.locked(): - start_room_msg() - psi.say(psi.rtr("matrix_sync.manual_sync.start_tip")) - read = asyncio.run(get_tip_read()) - if not read: - return RTextList( - psi.rtr("matrix_sync.manual_sync.start_sync") + "\n", - psi.rtr("matrix_sync.old_msg_sync") + "\n", - psi.rtr("matrix_sync.old_msg_sync2") + "\n", - psi.rtr("matrix_sync.old_msg_sync3") + "\n", - psi.rtr("matrix_sync.old_msg_sync4") + "\n", - psi.rtr("matrix_sync.old_msg_sync5") + "\n" - ) - else: - return psi.rtr("matrix_sync.manual_sync.start_sync") - else: - return psi.rtr("matrix_sync.manual_sync.start_error") - -# Manually stop sync processes. -def stopSync(src): - global sync_task - if src.is_console: - try: - if sync_task is not None: - sync_task.cancel() - return psi.rtr("matrix_sync.manual_sync.stop_sync") - else: - return psi.rtr("matrix_sync.manual_sync.not_running") - except Exception: - return psi.rtr("matrix_sync.manual_sync.stop_error") - else: - return psi.rtr("matrix_sync.manual_sync.stop_denied") - -def closeTip(): - TOKEN_FILE = matrix_sync.config.TOKEN_FILE - with open(TOKEN_FILE, "r") as f: - existing_data = json.load(f) - existing_data["tip_read"] = True - with open(TOKEN_FILE, "w") as f: - json.dump(existing_data, f) - return psi.rtr("matrix_sync.on_tip_read") # Restart room message receiver, not recommend. # def restartSync(src): @@ -130,20 +68,6 @@ def on_server_startup(server: PluginServerInterface): else: server.logger.info(server.rtr("matrix_sync.manual_sync.start_error")) -# Sub thread to receive room messages from matrix without block main MCDR thread. -@new_thread('MatrixReceiver') -def start_room_msg(): - with tLock: - asyncio.run(on_room_msg()) - -async def on_room_msg(): - global sync_task - if sync_task is not None and not sync_task.done(): - sync_task.cancel() - await sync_task - sync_task = asyncio.create_task(getMsg()) - await sync_task - # Game message reporter def on_user_info(server: PluginServerInterface, info: Info): # formater(server, info) diff --git a/matrix_sync/globals.py b/matrix_sync/globals.py new file mode 100644 index 0000000..a162aa8 --- /dev/null +++ b/matrix_sync/globals.py @@ -0,0 +1,10 @@ +import threading +import matrix_sync.config + +from mcdreforged.api.all import * + +psi = ServerInterface.psi() +tLock = threading.Lock() +lock_is_None = matrix_sync.config.lock_is_None +cleaned = False +sync_task = None \ No newline at end of file diff --git a/matrix_sync/sync/__init__.py b/matrix_sync/sync/__init__.py new file mode 100644 index 0000000..bf6545f --- /dev/null +++ b/matrix_sync/sync/__init__.py @@ -0,0 +1,2 @@ +from . import receiver +from . import reporter \ No newline at end of file diff --git a/matrix_sync/receiver.py b/matrix_sync/sync/receiver.py similarity index 98% rename from matrix_sync/receiver.py rename to matrix_sync/sync/receiver.py index a2d8b49..02ed836 100644 --- a/matrix_sync/receiver.py +++ b/matrix_sync/sync/receiver.py @@ -4,11 +4,11 @@ import matrix_sync.config from matrix_sync.token import getToken, get_next_batch +from matrix_sync.globals import psi from mcdreforged.api.all import * from nio import AsyncClient, MatrixRoom, RoomMessageText, SyncResponse, SyncError from typing import Optional -psi = ServerInterface.psi() homeserver_online = True refresh = True next_batch = None @@ -78,6 +78,8 @@ async def getMsg() -> None: client.add_response_callback(on_sync_response, SyncResponse) client.add_response_callback(on_sync_error, SyncError) client.add_event_callback(message_callback, RoomMessageText) + + client.sync(timeout=5) try: if homeserver_online: diff --git a/matrix_sync/reporter.py b/matrix_sync/sync/reporter.py similarity index 100% rename from matrix_sync/reporter.py rename to matrix_sync/sync/reporter.py diff --git a/mcdreforged.plugin.json b/mcdreforged.plugin.json index 68746c8..f524b47 100644 --- a/mcdreforged.plugin.json +++ b/mcdreforged.plugin.json @@ -1,6 +1,6 @@ { "id": "matrix_sync", - "version": "2.3.2", + "version": "2.4.0", "name": "MatrixSync", "description": { "en_us": "Sync messages between online game and Matrix groups.", diff --git a/requirements.txt b/requirements.txt index 1c101fe..da9a80e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ matrix-nio aiofiles asyncio +mcdreforged \ No newline at end of file