Skip to content

Commit

Permalink
Merge pull request #7 from Mooling0602/2.2.1
Browse files Browse the repository at this point in the history
Update main brunch to 2.2.1
  • Loading branch information
Mooling0602 authored Sep 14, 2024
2 parents a53b7c0 + 74bc172 commit 02e2c2a
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 31 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
- 中文
- [English](https://github.com/Mooling0602/MatrixSync-MCDR/blob/main/README_en_us.md)
- [English](https://github.com/Mooling0602/MatrixSync-MCDR/blob/2.2.1/README_en_us.md)

# MatrixSync-MCDR
一个MCDR(全称[MCDReforged](https://mcdreforged.com/))插件,用于同步Matrix群组和《我的世界》服务器的线上游戏之间的消息。
Expand All @@ -8,7 +8,9 @@

开发过程中用到的项目:[matrix-nio](https://pypi.org/project/matrix-nio/)

当前分支版本:主线@2.2.0
当前分支版本:已发布@2.2.1

请注意,插件在加载时会阻塞MCDR主线程,如果MCDR被阻塞并且无法继续正常启动,请检查强制结束所有进程并检查插件配置是否正确、Matrix根服务器是否在线等或禁用插件后重新启动。

## 用法
从release下载最新版本,在MCDReforged的启动环境中安装好需要的Python依赖,然后扔到plugins文件夹里面即可。
Expand All @@ -18,11 +20,11 @@
>
> 依赖软件包`zip`
>
> 在终端上运行`git clone https://github.com/Mooling0602/MatrixSync-MCDR.git`,然后进入`MatrixSync-MCDR`目录下并运行`pack_plugin.sh`(记得给文件设置可执行权限)
> 在终端上运行`git clone -b 2.2.1 https://github.com/Mooling0602/MatrixSync-MCDR.git`,然后进入`MatrixSync-MCDR`目录下并运行`pack_plugin.sh`(记得给文件设置可执行权限)
>
> 若无法正常访问GitHub,可以运行`git clone https://mirror.ghproxy.com/https://github.com/Mooling0602/MatrixSync-MCDR.git`
> 若无法正常访问GitHub,可以运行`git clone -b 2.2.1 https://mirror.ghproxy.com/https://github.com/Mooling0602/MatrixSync-MCDR.git`
>
> 懒人用命令:`git clone https://mirror.ghproxy.com/https://github.com/Mooling0602/MatrixSync-MCDR.git && cd MatrixSync-MCDR && chmod +x pack_plugin.sh && ./pack_plugin.sh`
> 懒人用命令:`git clone -b 2.2.1 https://mirror.ghproxy.com/https://github.com/Mooling0602/MatrixSync-MCDR.git && cd MatrixSync-MCDR && chmod +x pack_plugin.sh && ./pack_plugin.sh`
>
> 正常情况下,请不要修改脚本内容及所用配置(config.ini)
Expand Down
6 changes: 4 additions & 2 deletions README_en_us.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
- [中文](https://github.com/Mooling0602/MatrixSync-MCDR/blob/main/README.md)
- [中文](https://github.com/Mooling0602/MatrixSync-MCDR/blob/2.2.1/README.md)
- English

# MatrixSync-MCDR
Expand All @@ -10,7 +10,9 @@ The following project is used in the development process: [matrix-nio](https://p

Thanks for ChatGPT and Google Translate's help to translate the content from Chinese, if anything wrong, please issue to feedback or PR to `/lang`.

Present branch version: main@2.2.0
Present branch version: released@2.2.1

Please note that the plugin will block the MCDR main thread during loading. If MCDR is blocked and cannot continue its normal startup, please check by forcibly terminating all processes and verifying whether the plugin configuration is correct, whether the Matrix root server is online, etc., or restart after disabling the plugin.

## Usage
Download the latest version from the release, install the necessary Python dependencies in the MCDReforged startup environment, and then throw it into the plugins folder.
Expand Down
4 changes: 2 additions & 2 deletions config.ini
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Do not edit this file!
# 不要修改这个文件!
[framework]
ver=4
ver=2
[main]
ver=2.2.0
ver=2.2.1
[release]
test=0
4 changes: 2 additions & 2 deletions lang/en_us.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ matrix_sync:
error: Please check your account, password and network conditions, you can issue in GitHub for any help.
manual_sync:
start_sync: Message sync started!
error: Message sync is already running, do not execute this command in cases not special such as non-reloading!
error: Message sync is already running, do not restart it in cases not special such as non-reloading!
sync_tips:
server_stopping: MC Server is stopping now...
server_started: MC Server started successfully!
Expand All @@ -28,4 +28,4 @@ matrix_sync:
tr:
hs: Homeserver
ac: Account
cs: Console
cs: Console
2 changes: 1 addition & 1 deletion lang/zh_cn.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ matrix_sync:
error: 请检查你的账号密码以及网络情况,你可以在GitHub提出issue以获取任何帮助。
manual_sync:
start_sync: 已开始消息同步!
error: 消息同步已在运行,非重载等特殊情况不要执行此命令
error: 消息同步已在运行,非重载等特殊情况不要再次尝试启动
sync_tips:
server_stopping: "[×]服务器正在关闭……"
server_started: "[√]服务器已启动"
Expand Down
27 changes: 13 additions & 14 deletions matrix_sync/entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@
from matrix_sync.reporter import formater, sendMsg
from mcdreforged.api.all import *

# Framwork ver: 2.2.0-stable
# Framwork ver: 2.2.1-stable
psi = ServerInterface.psi()
asyncLock = asyncio.Lock()
threadLock = threading.Lock()
tLock = threading.Lock()
cleaned = False
sync_task = None
asyncio_loop = None

def on_load(server: PluginServerInterface, old):
load_config()
Expand All @@ -38,25 +36,27 @@ def on_load(server: PluginServerInterface, old):

# Manually run sync processes.
def manualSync():
if not asyncLock.locked():
asyncio.run(start_room_msg())
if not tLock.locked():
start_room_msg()
return psi.rtr("matrix_sync.manual_sync.start_sync")
else:
return psi.rtr("matrix_sync.manual_sync.error")

# Automatically run sync processes.
def on_server_startup(server: PluginServerInterface):
clientStatus = matrix_sync.client.clientStatus
if not asyncLock.locked():
if not tLock.locked():
if clientStatus:
message = psi.rtr("matrix_sync.sync_tips.server_started")
asyncio.run(sendMsg(message))
asyncio.run(start_room_msg())
start_room_msg()
else:
server.logger.info(server.rtr("matrix_sync.manual_sync.error"))

async def start_room_msg():
async with asyncLock:
await on_room_msg()
@new_thread('MatrixReceiver')
def start_room_msg():
with tLock:
asyncio.run(on_room_msg())

async def on_room_msg():
global sync_task
Expand Down Expand Up @@ -89,7 +89,7 @@ def on_server_stop(server: PluginServerInterface, server_return_code: int):
if sync_task is not None:
sync_task.cancel()
try:
asyncio.wait_for(sync_task, timeout=5)
pass
except asyncio.TimeoutError:
server.logger.warning("Timed out waiting for sync_task to finish.")
except asyncio.CancelledError:
Expand All @@ -105,13 +105,12 @@ def on_unload(server: PluginServerInterface):
if sync_task is not None:
sync_task.cancel()
try:
asyncio.wait_for(sync_task, timeout=5)
pass
except asyncio.TimeoutError:
server.logger.warning("Timed out waiting for sync_task to finish.")
except asyncio.CancelledError:
pass
sync_task = None
lock_is_None = matrix_sync.config.lock_is_None
if not lock_is_None:
threadLock.release()
server.logger.info(server.rtr("matrix_sync.on_unload"))
13 changes: 12 additions & 1 deletion matrix_sync/receiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import matrix_sync.config
from matrix_sync.reporter import sendMsg
from mcdreforged.api.all import *
from nio import AsyncClient, MatrixRoom, RoomMessageText
from nio import AsyncClient, MatrixRoom, RoomMessageText, SyncResponse, SyncError

psi = ServerInterface.psi()

Expand All @@ -22,6 +22,15 @@ async def message_callback(room: MatrixRoom, event: RoomMessageText) -> None:
if transfer:
psi.broadcast(f"{roomMsg}")

def on_sync_response(response: SyncResponse):
global server_is_online
server_is_online = True

def on_sync_error(response: SyncError):
global server_is_online
if response.status_code >= 500:
server_is_online = False

async def getMsg() -> None:
user_id = matrix_sync.config.user_id
homeserver = matrix_sync.config.homeserver
Expand All @@ -35,6 +44,8 @@ async def getMsg() -> None:
client.user_id = user_id
client.device_id = device_id

client.add_response_callback(on_sync_response, SyncResponse)
client.add_response_callback(on_sync_error, SyncError)
client.add_event_callback(message_callback, RoomMessageText)

try:
Expand Down
4 changes: 2 additions & 2 deletions matrix_sync/reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ def formater(server: PluginServerInterface, info: Info):
gameMsg = f"<{console_tr}> {msg_content}"
else:
option = psi.rtr("matrix_sync.on_console.commands")
gameMsg = f"* {console_tr} {option} -> {info.content}"
gameMsg = f"[!] {console_tr} {option} -> {info.content}"
if info.content == "stop":
gameMsg = psi.rtr("matrix_sync.sync_tips.server_stopping")
else:
if info.content.startswith("!!"):
option = psi.rtr("matrix_sync.on_console.commands")
gameMsg = f"* {info.player} {option} -> {info.content}"
gameMsg = f"[!] {info.player} {option} -> {info.content}"
report = False
clientStatus = matrix_sync.client.clientStatus
if clientStatus:
Expand Down
4 changes: 2 additions & 2 deletions mcdreforged.plugin.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"id": "matrix_sync",
"version": "2.2.0",
"version": "2.2.1",
"name": "MatrixSync",
"description": {
"en_us": "Sync messages between online game and Matrix groups.",
Expand All @@ -12,4 +12,4 @@
"author": "Mooling0602",
"link": "https://github.com/Mooling0602/MatrixSync-MCDR",
"entrypoint": "matrix_sync.entry"
}
}

0 comments on commit 02e2c2a

Please sign in to comment.