Skip to content
This repository has been archived by the owner on Feb 12, 2023. It is now read-only.

Commit

Permalink
1.9.5更新,修复事件回调逻辑错误,新增onCmdBlockExecute,修复PyBlockInstance未初始化问题
Browse files Browse the repository at this point in the history
  • Loading branch information
twoone3l committed Feb 1, 2022
1 parent f6c7473 commit 7951a94
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 77 deletions.
137 changes: 69 additions & 68 deletions mod/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,75 @@ using namespace std;

namespace fs = filesystem;

#pragma region Function
//初始化Python类
void PyClassInit() {
if (PyType_Ready(&PyEntity_Type) < 0)
Py_FatalError("Can't initialize entity type");
if (PyType_Ready(&PyItemStack_Type) < 0)
Py_FatalError("Can't initialize value type");
if (PyType_Ready(&PyBlockInstance_Type) < 0)
Py_FatalError("Can't initialize value type");
}
//将Python解释器初始化插入bds主函数
THook(int, "main", int argc, char* argv[], char* envp[]) {
//如果目录不存在创建目录
if (!fs::exists(PLUGIN_PATH))
fs::create_directory(PLUGIN_PATH);
//设置模块搜索路径
wstring plugins_path =
PLUGIN_PATH L";"
PLUGIN_PATH "Dlls;"
PLUGIN_PATH "Lib"
;
plugins_path.append(Py_GetPath());
Py_SetPath(plugins_path.c_str());
#if 0
//预初始化3.8+
PyPreConfig cfg;
PyPreConfig_InitPythonConfig(&cfg);
cfg.utf8_mode = 1;
cfg.configure_locale = 0;
Py_PreInitialize(&cfg);
#endif
//增加一个模块
PyImport_AppendInittab("mc", McInit);
//初始化解释器
Py_Initialize();
//输出版本号信息
logger.info("{} loaded.", PYR_VERSION);
//初始化类型
PyClassInit();
//启用线程支持
PyEval_InitThreads();
for (auto& info : fs::directory_iterator(PLUGIN_PATH)) {
if (info.path().extension() == ".py") {
string name(info.path().stem().u8string());
//忽略以'_'开头的文件
if (name.front() == '_') {
logger.info("Ignoring {}", name);
continue;
}
else {
logger.info("Loading {}", name);
PyImport_ImportModule(name.c_str());
Py_PrintErrors();
}
}
}
//启动子线程前执行,释放PyEval_InitThreads获得的全局锁,否则子线程可能无法获取到全局锁。
PyEval_ReleaseThread(PyThreadState_Get());
//注册命令监听
Event::RegCmdEvent::subscribe(
[](Event::RegCmdEvent e) {
for (auto& [cmd, des] : g_commands) {
e.mCommandRegistry->registerCommand(cmd, des.first.c_str(), CommandPermissionLevel::Any, { CommandFlagValue::None }, { static_cast<CommandFlagValue>(0x80) });
}
return true;
}
);
return original(argc, argv, envp);
}
//Dll主函数
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved) {
hinstDLL;
lpReserved;
Expand Down Expand Up @@ -146,70 +214,3 @@ static void CheckPluginVersion() {
exit(0);
}
#endif
#pragma endregion
void PyClassInit() {
if (PyType_Ready(&PyEntity_Type) < 0)
Py_FatalError("Can't initialize entity type");
if (PyType_Ready(&PyItemStack_Type) < 0)
Py_FatalError("Can't initialize value type");
}
//将Python解释器初始化插入bds主函数
THook(int, "main",
int argc, char* argv[], char* envp[]) {
//如果目录不存在创建目录
if (!fs::exists(PLUGIN_PATH))
fs::create_directory(PLUGIN_PATH);
//设置模块搜索路径
wstring plugins_path =
PLUGIN_PATH L";"
PLUGIN_PATH "Dlls;"
PLUGIN_PATH "Lib"
;
plugins_path.append(Py_GetPath());
Py_SetPath(plugins_path.c_str());
#if 0
//预初始化3.8+
PyPreConfig cfg;
PyPreConfig_InitPythonConfig(&cfg);
cfg.utf8_mode = 1;
cfg.configure_locale = 0;
Py_PreInitialize(&cfg);
#endif
//增加一个模块
PyImport_AppendInittab("mc", McInit);
//初始化解释器
Py_Initialize();
//输出版本号信息
logger.info("{} loaded.", PYR_VERSION);
//初始化类型
PyClassInit();
//启用线程支持
PyEval_InitThreads();
for (auto& info : fs::directory_iterator(PLUGIN_PATH)) {
if (info.path().extension() == ".py") {
string name(info.path().stem().u8string());
//忽略以'_'开头的文件
if (name.front() == '_') {
logger.info("Ignoring {}", name);
continue;
}
else {
logger.info("Loading {}", name);
PyImport_ImportModule(name.c_str());
Py_PrintErrors();
}
}
}
//启动子线程前执行,释放PyEval_InitThreads获得的全局锁,否则子线程可能无法获取到全局锁。
PyEval_ReleaseThread(PyThreadState_Get());
//注册命令监听
Event::RegCmdEvent::subscribe(
[](Event::RegCmdEvent e) {
for (auto& [cmd, des] : g_commands) {
e.mCommandRegistry->registerCommand(cmd, des.first.c_str(), CommandPermissionLevel::Any, { CommandFlagValue::None }, { static_cast<CommandFlagValue>(0x80) });
}
return true;
}
);
return original(argc, argv, envp);
}
10 changes: 3 additions & 7 deletions mod/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,11 @@ static PyObject* setListener(PyObject*, PyObject* args) {
auto event_code = magic_enum::enum_cast<EventCode>(event_name);
if (!event_code.has_value())
Py_RETURN_ERROR_FORMAT("Invalid Listener key words %s", event_name);
//在全局回调表中查找code
auto funcs = g_callback_functions.find(event_code.value());
//如果是第一次设置则订阅监听器
if (funcs == g_callback_functions.end()) {
g_callback_functions[event_code.value()];
//如果监听器未启用,则启用
if (g_callback_functions.find(event_code.value()) == g_callback_functions.end())
EnableEventListener(event_code.value());
}
//添加回调函数
funcs->second.push_back(func);
g_callback_functions[event_code.value()].push_back(func);
Py_RETURN_NONE;
}
//设置指令说明
Expand Down
4 changes: 2 additions & 2 deletions mod/Version.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#pragma once
constexpr unsigned PYR_VERSION_MAJOR = 1;
constexpr unsigned PYR_VERSION_MINOR = 9;
constexpr unsigned PYR_VERSION_MICRO = 4;
constexpr const char* PYR_VERSION = "v1.9.4";
constexpr unsigned PYR_VERSION_MICRO = 5;
constexpr const char* PYR_VERSION = "v1.9.5";

0 comments on commit 7951a94

Please sign in to comment.