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

Commit

Permalink
1.6.6更新,修复Player::getEntityTypeId不返回319的问题,新增命令注册回调,wiki将重写
Browse files Browse the repository at this point in the history
  • Loading branch information
twoone3l committed Aug 11, 2021
1 parent bead89a commit f253c47
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/msbuild.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ jobs:
- name: Release
uses: softprops/[email protected]
with:
files: ../test/bdxcore_mod/*
files: ../BDS/bdxcore_mod/*
10 changes: 5 additions & 5 deletions BDSpyrunner/BDSpyrunner.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,28 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v142</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
Expand Down Expand Up @@ -104,7 +104,7 @@
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
<OutDir>..\..\test\bdxcore_mod\</OutDir>
<OutDir>..\..\BDS\bdxcore_mod\</OutDir>
<IntDir>$(Configuration)\</IntDir>
<PublicIncludeDirectories>
</PublicIncludeDirectories>
Expand Down
4 changes: 2 additions & 2 deletions BDSpyrunner/BDSpyrunner.vcxproj.user
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
<LocalDebuggerWorkingDirectory>..\..\test</LocalDebuggerWorkingDirectory>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LocalDebuggerCommand>C:\Users\Administrator\Desktop\test\bedrock_server.exe</LocalDebuggerCommand>
<LocalDebuggerCommand>C:\Users\Administrator\Desktop\BDS\bedrock_server.exe</LocalDebuggerCommand>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
<LocalDebuggerWorkingDirectory>..\..\test</LocalDebuggerWorkingDirectory>
<LocalDebuggerWorkingDirectory>C:\Users\Administrator\Desktop\BDS\</LocalDebuggerWorkingDirectory>
</PropertyGroup>
</Project>
67 changes: 49 additions & 18 deletions BDSpyrunner/mod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
#define PY_SSIZE_T_CLEAN
#include "include/Python.h"

#define VERSION_STRING "1.6.5"
#define VERSION_NUMBER 205
#define VERSION_STRING "1.6.6"
#define VERSION_NUMBER 206
#define PLUGIN_PATH "plugins/py"
#define MODULE_NAME "mc"
#define Py_RETURN_ERROR(str) return PyErr_SetString(PyExc_Exception, str), nullptr

static_assert(sizeof(Actor) == 1);
#pragma region EventCode
enum class EventCode {
None,
Expand Down Expand Up @@ -123,7 +124,7 @@ static Scoreboard* _scoreboard = nullptr;
//Py函数表
static unordered_map<EventCode, vector<PyObject*>> _functions;
//注册命令
static vector<pair<string, string>> _commands;
static unordered_map<string, pair<const char*, PyObject*>> _commands;
//伤害
static int _damage;
}
Expand Down Expand Up @@ -183,17 +184,11 @@ static void safeCall(const function<void()>& fn) {
static bool EventCallBack(EventCode e, PyObject* val) {
bool result = true;
safeCall([&] {
vector<PyObject*>& List = _functions[e];
if (!List.empty()) {
for (PyObject* fn : List) {
//if (val) {
vector<PyObject*>& list = _functions[e];
if (!list.empty()) {
for (PyObject* fn : list) {
if (PyObject_CallFunction(fn, "O", val) == Py_False)
result = false;
//}
//else {
// if (_PyObject_CallNoArg(fn) == Py_False)
// result = false;
//}
PyErr_Print();
}
}
Expand Down Expand Up @@ -428,13 +423,33 @@ static PyObject* PyEntity_GetHealth(PyObject* self, void*) {
return nullptr;
return PyLong_FromLong(a->getHealth());
}
static int PyEntity_SetHealth(PyObject* self, PyObject* arg, void*) {
if (PyLong_Check(arg)) {
Actor* a = PyEntity_AsActor(self);
if (!a)
return -1;
a->setHealth(PyLong_AsLong(arg));
return 0;
}
return PyErr_BadArgument(), -1;
}
//获取最大生命值
static PyObject* PyEntity_GetMaxHealth(PyObject* self, void*) {
Actor* a = PyEntity_AsActor(self);
if (!a)
return nullptr;
return PyLong_FromLong(a->getMaxHealth());
}
static int PyEntity_SetMaxHealth(PyObject* self, PyObject* arg, void*) {
if (PyLong_Check(arg)) {
Actor* a = PyEntity_AsActor(self);
if (!a)
return -1;
a->setMaxHealth(PyLong_AsLong(arg));
return 0;
}
return PyErr_BadArgument(), -1;
}
//获取权限
static PyObject* PyEntity_GetPermissions(PyObject* self, void*) {
Player* p = PyEntity_AsPlayer(self);
Expand Down Expand Up @@ -488,8 +503,8 @@ static PyGetSetDef PyEntity_GetSet[]{
{"typeid", PyEntity_GetTypeID, nullptr, nullptr},
{"typename", PyEntity_GetTypeName, nullptr, nullptr},
{"nbt", PyEntity_GetNBTInfo, nullptr, nullptr},
{"health", PyEntity_GetHealth, nullptr, nullptr},
{"maxhealth", PyEntity_GetMaxHealth, nullptr, nullptr},
{"health", PyEntity_GetHealth, PyEntity_SetHealth, nullptr},
{"maxhealth", PyEntity_GetMaxHealth, PyEntity_SetMaxHealth, nullptr},
{"perm", PyEntity_GetPermissions, PyEntity_SetPermissions, nullptr},
{"deviceid", PyEntity_GetDeviceId, nullptr, nullptr},
{"deviceos", PyEntity_GetDeviceOS, nullptr, nullptr},
Expand Down Expand Up @@ -913,6 +928,13 @@ static PyObject* PyEntity_FromEntity(Actor* ptr) {
#pragma endregion
#pragma region Hook List
#if 0
HOOK(Actor_load, bool, "?load@Actor@@UEAA_NAEBVCompoundTag@@AEAVDataLoadHelper@@@Z",
Actor* _this, Tag* tag, struct DataLoadHelper* data) {
//cout << CompoundTagtoJson(tag).dump(4) << endl;
//cout << data << endl;

return original(_this, tag, data);
}
HOOK(Level_tick, void, "?tick@Level@@UEAAXXZ",
Level* _this) {
original(_this);
Expand Down Expand Up @@ -951,7 +973,7 @@ HOOK(ChangeSettingCommand_setup, void, "?setup@ChangeSettingCommand@@SAXAEAVComm
VA _this) {
for (auto& [cmd, des] : _commands) {
SymCall("?registerCommand@CommandRegistry@@QEAAXAEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@PEBDW4CommandPermissionLevel@@UCommandFlag@@3@Z",
_this, &cmd, des.c_str(), 0, 0, 0x80);
_this, &cmd, des.first, 0, 0, 0x80);
}
original(_this);
}
Expand Down Expand Up @@ -1073,7 +1095,7 @@ HOOK(onDestroyBlock, bool, "?checkBlockDestroyPermissions@BlockSource@@QEAA_NAEA
)
))
return false;
}
}
return original(_this, p, bp, a3, a4);
}
HOOK(onOpenChest, bool, "?use@ChestBlock@@UEBA_NAEAVPlayer@@AEBVBlockPos@@E@Z",
Expand Down Expand Up @@ -1233,6 +1255,14 @@ HOOK(onInputCommand, void, "?handle@ServerNetworkHandler@@UEAAXAEBVNetworkIdenti
Player* p = _this->_getServerPlayer(id, pkt);
if (p) {
const string& cmd = FETCH(string, pkt + 48);
auto data = _commands.find(cmd.c_str() + 1);
if (data != _commands.end()) {
safeCall([&] {
PyObject_CallFunction(data->second.second, "O", PyEntity_FromEntity(p));
PyErr_Print();
});
return;
}
bool res = EventCallBack(EventCode::onInputCommand,
Py_BuildValue("{s:O,s:s}",
"player", PyEntity_FromEntity(p),
Expand Down Expand Up @@ -1458,8 +1488,9 @@ static PyObject* PyAPI_setListener(PyObject*, PyObject* args) {
static PyObject* PyAPI_setCommandDescription(PyObject*, PyObject* args) {
const char* cmd = "";
const char* des = "";
if (PyArg_ParseTuple(args, "ss:setCommandDescription", &cmd, &des)) {
_commands.push_back({ cmd, des });
PyObject* callback = nullptr;
if (PyArg_ParseTuple(args, "ss|O:setCommandDescription", &cmd, &des, &callback)) {
_commands.insert({ cmd, { des, callback } });
}
Py_RETURN_NONE;
}
Expand Down
24 changes: 19 additions & 5 deletions BDSpyrunner/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ int HookFunction(void*, void*, void*);
//获取符号
extern "C" _declspec(dllimport)
void* GetServerSymbol(const char*);
//调用一个虚函数
//_this:类地址
//off:偏移量
//剩下的参数就不用传this了
template<typename ret = void, typename... Args>
static ret VirtualCall(VA off, void* _this, Args... args) {
return (*reinterpret_cast<ret(**)(void*, Args...)>(*reinterpret_cast<VA*>(_this) + off))(_this, args...);
}
//调用一个函数
template<typename ret = void, typename... Args>
static ret SymCall(const char* sym, Args... args) {
Expand Down Expand Up @@ -666,7 +674,8 @@ struct Actor {
}
//获取实体类型
unsigned getEntityTypeId() {
return SymCall<unsigned>("?getEntityTypeId@Actor@@UEBA?AW4ActorType@@XZ", this);
return VirtualCall<unsigned>(0x520, this);
//return SymCall<unsigned>("?getEntityTypeId@Actor@@UEBA?AW4ActorType@@XZ", this);
}
//获取查询用ID
VA getUniqueID() {
Expand Down Expand Up @@ -698,11 +707,16 @@ struct Actor {
int getMaxHealth() {
return SymCall<int>("?getMaxHealth@Actor@@QEBAHXZ", this);
}
void setHealth(int value, int max) {
VA hattr = ((*(VA(__fastcall**)(Actor*, void*))(*(VA*)this + 1552))(
this, SYM("?HEALTH@SharedAttributes@@2VAttribute@@B")));
void setHealth(int value) {
VA hattr = (*reinterpret_cast<VA(**)(Actor*, void*)>(*(VA*)this + 1552))
(this, SYM("?HEALTH@SharedAttributes@@2VAttribute@@B"));
FETCH(int, hattr + 132) = value;
FETCH(int, hattr + 128) = max;
//SymCall("?_setDirty@AttributeInstance@@AEAAXXZ", hattr);
}
void setMaxHealth(int value) {
VA hattr = (*reinterpret_cast<VA(**)(Actor*, void*)>(*(VA*)this + 1552))
(this, SYM("?HEALTH@SharedAttributes@@2VAttribute@@B"));
FETCH(int, hattr + 128) = value;
//SymCall("?_setDirty@AttributeInstance@@AEAAXXZ", hattr);
}
//获取副手
Expand Down

0 comments on commit f253c47

Please sign in to comment.