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

Commit

Permalink
1.9.6更新
Browse files Browse the repository at this point in the history
- 修复getPlayerList的内存泄露
- 修复registerCommand函数回调
- 新增PyEntity::getGameMode函数
- 新增PyBlockInstance::getPos/getDimensionId函数
- 修复PyEntity在MobHurtEvent中空指针获取字符串为空指针的问题
- 修改部分源文件
  • Loading branch information
twoone3 committed Feb 3, 2022
1 parent 9ed4e1b commit d537c8d
Show file tree
Hide file tree
Showing 20 changed files with 379 additions and 390 deletions.
12 changes: 5 additions & 7 deletions BDSpyrunner.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,17 @@ copy $(OutputPath)$(ProjectName).pdb ..\BDS\plugins\$(ProjectName).pdb
<ClCompile Include="mod\NBT.cpp" />
<ClCompile Include="mod\PyItemStack.cpp" />
<ClCompile Include="mod\PyBlockInstance.cpp" />
<ClCompile Include="mod\Tool.cpp" />
<ClCompile Include="mod\CPython.cpp" />
<ClCompile Include="mod\PyUtils.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="mod\magic_enum.hpp" />
<ClInclude Include="mod\Module.h" />
<ClInclude Include="mod\Event.h" />
<ClInclude Include="mod\NBT.h" />
<ClInclude Include="mod\Tool.h" />
<ClInclude Include="mod\PyEntity.h" />
<ClInclude Include="mod\CPython.h" />
<ClInclude Include="mod\PyItemStack.h" />
<ClInclude Include="mod\PyBlockInstance.h" />
<ClInclude Include="mod\Main.h" />
<ClInclude Include="mod\PyUtils.h" />
<ClInclude Include="mod\Version.h" />
<ClInclude Include="mod\Common.h" />
</ItemGroup>
<ItemGroup>
<Library Include="lib\python37.lib" />
Expand Down
18 changes: 6 additions & 12 deletions BDSpyrunner.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -39,39 +39,33 @@
<ClCompile Include="mod\PyBlockInstance.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="mod\Tool.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="mod\CPython.cpp">
<ClCompile Include="mod\PyUtils.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="mod\Tool.h">
<ClInclude Include="mod\Main.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="mod\Event.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="mod\PyEntity.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="mod\Module.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="mod\CPython.h">
<ClInclude Include="mod\PyUtils.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="mod\NBT.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="mod\PyItemStack.h">
<ClInclude Include="mod\Version.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="mod\PyBlockInstance.h">
<ClInclude Include="mod\Common.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="mod\Version.h">
<ClInclude Include="mod\magic_enum.hpp">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
Expand Down
87 changes: 0 additions & 87 deletions mod/CPython.cpp

This file was deleted.

15 changes: 7 additions & 8 deletions mod/Tool.h → mod/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,14 @@
#include <MC/ListTag.hpp>
#include <MC/CompoundTag.hpp>
#include <MC/IntArrayTag.hpp>
/*JSON*/
#include <third-party/Nlohmann/fifo_json.hpp>

using json_t = nlohmann::detail::value_t;

//字符串转JSON,本插件采用 https://json.nlohmann.me 的JSON库
fifo_json CompoundTagToJson(std::string_view str);

bool IsPlayer(Actor* ptr);
inline bool IsPlayer(Actor* ptr) {
if (ptr == nullptr)
return false;
if (ptr->getEntityTypeId() != 319)
return false;
return true;
}

inline Logger logger("BDSpyrunner");

87 changes: 27 additions & 60 deletions mod/Event.cpp
Original file line number Diff line number Diff line change
@@ -1,82 +1,37 @@
#include "Event.h"
#include "Tool.h"
#include "Common.h"
#include "Module.h"

using namespace std;
//事件回调助手,初始化对象将申请GIL
class Callbacker {
public:
Callbacker(EventCode t) :
type_(t), arg_(nullptr), gil_() {
Callbacker(EventCode t) {
type_ = t;
arg_ = nullptr;
}
~Callbacker() {
//if (arg_ == nullptr)
// logger.error("意外的空指针");
//logger.info("{}", PyObjectToStr(arg_));
//Py_XDECREF(arg_);
}
//事件回调
bool callback() {
bool intercept = true;
bool pass = true;
//如果没有则跳过
auto& cbs = g_callback_functions[type_];
for (auto cb : cbs) {
PyObject* result = _PyObject_FastCall(cb, &arg_, 1);
Py_PrintErrors();
if (result == Py_False)
intercept = false;
Py_XDECREF(result);
PyCaller pc;
pass = pc.call(cb, arg_) != Py_False;
}
//Py_XDECREF(arg_);
return intercept;
return pass;
}
Callbacker& setArg(PyObject* arg) {
arg_ = arg;
return *this;
}
Callbacker& insert(string_view key, PyObject* item) {
template<typename Arg>
Callbacker& insert(string_view key, Arg item) {
if (arg_ == nullptr)
arg_ = PyDict_New();
PyDict_SetItemString(arg_, key.data(), item);
Py_DECREF(item);
PyObject* obj = ToPyObject(item);
PyDict_SetItemString(arg_, key.data(), obj);
Py_DECREF(obj);
return *this;
}
Callbacker& insert(string_view key, string_view item) {
return insert(key, StrToPyUnicode(item));
}
Callbacker& insert(string_view key, Actor* item) {
return insert(key, ToPyEntity(item));
}
Callbacker& insert(string_view key, ItemStack* item) {
return insert(key, ToPyItemStack(item));
}
Callbacker& insert(string_view key, const BlockPos& item) {
return insert(key, ToList(item));
}
Callbacker& insert(string_view key, BlockInstance& item) {
return insert(key, ToPyBlockInstance(&item));
}
Callbacker& insert(string_view key, const Vec3& item) {
return insert(key, ToList(item));
}
Callbacker& insert(string_view key, short item) {
return insert(key, PyLong_FromLong(item));
}
Callbacker& insert(string_view key, int item) {
return insert(key, PyLong_FromLong(item));
}
Callbacker& insert(string_view key, unsigned item) {
return insert(key, PyLong_FromUnsignedLong(item));
}
Callbacker& insert(string_view key, long long item) {
return insert(key, PyLong_FromLongLong(item));
}
Callbacker& insert(string_view key, unsigned long long item) {
return insert(key, PyLong_FromUnsignedLongLong(item));
}
Callbacker& insert(string_view key, float item) {
return insert(key, PyLong_FromDouble(item));
}
private:
EventCode type_;
PyObject* arg_;
Expand Down Expand Up @@ -254,6 +209,9 @@ void EnableEventListener(EventCode code) {
EVENT_END;
break;
case EventCode::onWitherBossDestroy:
EVENT_BEGIN(WitherBossDestroyEvent);
//TODO: AABB and WitherBoss
EVENT_END;
break;
case EventCode::onPlaceBlock:
EVENT_BEGIN(PlayerPlaceBlockEvent);
Expand All @@ -265,19 +223,28 @@ void EnableEventListener(EventCode code) {
break;
case EventCode::onOpenContainer:
EVENT_BEGIN(PlayerOpenContainerEvent);
//TODO: container
EVENT_INSERT(BlockInstance);
//TODO: EVENT_INSERT(Container);
EVENT_INSERT(Player);
EVENT_END;
break;
case EventCode::onCloseContainer:
EVENT_BEGIN(PlayerCloseContainerEvent);
//TODO: container
EVENT_INSERT(BlockInstance);
//TODO: EVENT_INSERT(Container);
EVENT_INSERT(Player);
EVENT_END;
break;
case EventCode::onContainerChange:
EVENT_BEGIN(ContainerChangeEvent);
EVENT_INSERT(Actor);
EVENT_INSERT(BlockInstance);
//TODO: EVENT_INSERT(Container);
EVENT_INSERT(NewItemStack);
EVENT_INSERT(Player);
EVENT_INSERT(PreviousItemStack);
EVENT_INSERT(Slot);
EVENT_END;
break;
case EventCode::onOpenContainerScreen:
break;
Expand Down
32 changes: 30 additions & 2 deletions mod/Main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "Module.h"
#include "Main.h"
#include "Common.h"
#include "Module.h"
#include "NBT.h"
#include "Version.h"

Expand All @@ -8,6 +10,15 @@ using namespace std;

namespace fs = filesystem;

//字符串转JSON,本插件采用 https://json.nlohmann.me 的JSON库
fifo_json StrToJson(std::string_view str) {
try { return fifo_json::parse(str); }
catch (const std::exception& e) {
logger.error("Parsing JSON failed! {}", e.what());
return nullptr;
}
}

//初始化Python类
void PyClassInit() {
if (PyType_Ready(&PyEntity_Type) < 0)
Expand Down Expand Up @@ -74,6 +85,23 @@ THook(int, "main", int argc, char* argv[], char* envp[]) {
return true;
}
);
//命令监听
Event::PlayerCmdEvent::subscribe(
[](Event::PlayerCmdEvent e) {
for (auto& [cmd, data] : g_commands) {
if (e.mCommand == cmd) {
PyGILGuard _gil;
PyObject* p = ToPyObject(e.mPlayer);
PyObject* result = _PyObject_FastCall(data.second, &p, 1);
Py_PrintErrors();
Py_DECREF(p);
Py_XDECREF(result);
return false;
}
}
return true;
}
);
return original(argc, argv, envp);
}
//Dll主函数
Expand Down Expand Up @@ -167,7 +195,7 @@ static Json AccessUrlForJson(const wchar_t* url) {
} while (size);
InternetCloseHandle(handle2);
InternetCloseHandle(hSession);
return CompoundTagToJson(data);
return StrToJson(data);
}
//访问url
static void AccessUrlForFile(const wchar_t* url, string_view filename) {
Expand Down
7 changes: 7 additions & 0 deletions mod/Main.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once
#include <third-party/Nlohmann/fifo_json.hpp>

using json_t = nlohmann::detail::value_t;

//字符串转JSON,本插件采用 https://json.nlohmann.me 的JSON库
fifo_json StrToJson(std::string_view str);
Loading

0 comments on commit d537c8d

Please sign in to comment.