Skip to content

Commit

Permalink
代码重构
Browse files Browse the repository at this point in the history
	新增:   .github/workflows/build.yml
	修改:   config.ini
	修改:   doc/introduction.md
	新增:   matrix_sync/commands.py
	修改:   matrix_sync/entry.py
	新增:   matrix_sync/globals.py
	新增:   matrix_sync/sync/__init__.py
	移动:    matrix_sync/receiver.py -> matrix_sync/sync/receiver.py
	移动:    matrix_sync/reporter.py -> matrix_sync/sync/reporter.py
	修改:   mcdreforged.plugin.json
	修改:   requirements.txt
  • Loading branch information
Mooling0602 committed Nov 6, 2024
1 parent b686182 commit c64fc81
Show file tree
Hide file tree
Showing 11 changed files with 141 additions and 86 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -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 文件
2 changes: 1 addition & 1 deletion config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
[framework]
ver=1
[main]
ver=2.3.1
ver=2.4.0
[release]
test=1
2 changes: 2 additions & 0 deletions doc/introduction.md
Original file line number Diff line number Diff line change
@@ -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.
77 changes: 77 additions & 0 deletions matrix_sync/commands.py
Original file line number Diff line number Diff line change
@@ -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
90 changes: 7 additions & 83 deletions matrix_sync/entry.py
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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(
Expand All @@ -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):
Expand All @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions matrix_sync/globals.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions matrix_sync/sync/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import receiver
from . import reporter
4 changes: 3 additions & 1 deletion matrix_sync/receiver.py → matrix_sync/sync/receiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion mcdreforged.plugin.json
Original file line number Diff line number Diff line change
@@ -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.",
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
matrix-nio
aiofiles
asyncio
mcdreforged

0 comments on commit c64fc81

Please sign in to comment.