diff --git a/BDSpyrunner/BDSpyrunner.vcxproj b/BDSpyrunner/BDSpyrunner.vcxproj index c0c63b07..b008d55d 100644 --- a/BDSpyrunner/BDSpyrunner.vcxproj +++ b/BDSpyrunner/BDSpyrunner.vcxproj @@ -182,6 +182,9 @@ + + + diff --git a/BDSpyrunner/BDSpyrunner.vcxproj.filters b/BDSpyrunner/BDSpyrunner.vcxproj.filters index 4541ae20..9ee5343e 100644 --- a/BDSpyrunner/BDSpyrunner.vcxproj.filters +++ b/BDSpyrunner/BDSpyrunner.vcxproj.filters @@ -104,4 +104,7 @@ {237bf3e8-b292-4ca0-b55a-d5f6832b0cb0} + + + \ No newline at end of file diff --git a/BDSpyrunner/Header File/Actor.h b/BDSpyrunner/Header File/Actor.h index 1e62a02f..1e9c6952 100644 --- a/BDSpyrunner/Header File/Actor.h +++ b/BDSpyrunner/Header File/Actor.h @@ -14,6 +14,10 @@ struct ScorePacketInfo; struct Actor { //获取生物名称信息 std::string getNameTag(); + //设置生物名称信息 + void setNameTag(const std::string&); + //设置生物名称是否可见 + void setNameTagVisible(bool visible); //获取生物当前所处维度ID int getDimensionId(); //获取生物当前所在坐标 @@ -67,8 +71,6 @@ struct Player : Mob { std::string getUuid(); //根据地图信息获取玩家xuid std::string& getXuid(); - //重设服务器玩家名 - void setName(const std::string& name); //获取网络标识符 NetworkIdentifier* getClientId(); //获取背包 diff --git a/BDSpyrunner/Header File/PyEntity.h b/BDSpyrunner/Header File/PyEntity.h index c15fafa4..72a6728e 100644 --- a/BDSpyrunner/Header File/PyEntity.h +++ b/BDSpyrunner/Header File/PyEntity.h @@ -1,4 +1,5 @@ #pragma once +#include #define PY_SSIZE_T_CLEAN #include "include/Python.h" @@ -14,6 +15,8 @@ Py_END_ALLOW_THREADS;\ if (!_has_gil)PyGILState_Release(_gil_state) +//转换std::string为PyUnicode +PyObject* toPyUnicode(const std::string&); struct Actor; struct Player; // 实体类型 diff --git a/BDSpyrunner/Header File/global.h b/BDSpyrunner/Header File/global.h index 172895a6..5524cba1 100644 --- a/BDSpyrunner/Header File/global.h +++ b/BDSpyrunner/Header File/global.h @@ -1,9 +1,4 @@ #pragma once -#include -#include -#include - -typedef struct _object PyObject; //指令队列 extern struct SPSCQueue* g_command_queue; //网络处理 @@ -14,9 +9,3 @@ extern struct Level* g_level; extern struct RakPeer* g_rak_peer; //计分板 extern struct Scoreboard* g_scoreboard; -//Py函数表 -extern std::unordered_map> g_callback_functions; -//注册命令 -extern std::unordered_map> g_commands; -//伤害 -extern int g_damage; diff --git a/BDSpyrunner/Source File/Actor.cpp b/BDSpyrunner/Source File/Actor.cpp index 6ea747ce..85912f6b 100644 --- a/BDSpyrunner/Source File/Actor.cpp +++ b/BDSpyrunner/Source File/Actor.cpp @@ -9,6 +9,18 @@ string Actor::getNameTag() { return SymCall("?getNameTag@Actor@@UEBAAEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ", this); } +//设置生物名称信息 + +void Actor::setNameTag(const std::string&name) { + VirtualCall(0x1F8, this, &name); +} + +//设置生物名称是否可见 + +void Actor::setNameTagVisible(bool visible) { + SymCall("?setNameTagVisible@Actor@@UEAAX_N@Z", this, visible); +} + //获取生物当前所处维度ID int Actor::getDimensionId() { @@ -201,13 +213,6 @@ string& Player::getXuid() { getLevel(), this + 3000); } -//重设服务器玩家名 - -void Player::setName(const string& name) { - SymCall("?setName@Player@@UEAAXAEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z", - this, &name); -} - //获取网络标识符 NetworkIdentifier* Player::getClientId() { diff --git a/BDSpyrunner/Source File/PyEntity.cpp b/BDSpyrunner/Source File/PyEntity.cpp index 18d92864..349b6442 100644 --- a/BDSpyrunner/Source File/PyEntity.cpp +++ b/BDSpyrunner/Source File/PyEntity.cpp @@ -8,6 +8,9 @@ #include using namespace std; +PyObject* toPyUnicode(const string& str) { + return PyUnicode_FromStringAndSize(str.c_str(), str.length()); +} Actor* PyEntity_AsActor(PyObject* self) { if (reinterpret_cast(self)->actor) return reinterpret_cast(self)->actor; @@ -48,8 +51,7 @@ PyObject* PyEntity_Str(PyObject* self) { Actor* a = PyEntity_AsActor(self); if (!a) return nullptr; - string name = a->getNameTag(); - return PyUnicode_FromStringAndSize(name.c_str(), name.length()); + return toPyUnicode(a->getNameTag()); } //哈希 @@ -86,8 +88,7 @@ PyObject* PyEntity_GetName(PyObject* self, void*) { Actor* a = PyEntity_AsActor(self); if (!a) return nullptr; - string name = a->getNameTag(); - return PyUnicode_FromStringAndSize(name.c_str(), name.length()); + return toPyUnicode(a->getNameTag()); } int PyEntity_SetName(PyObject* self, PyObject* arg, void*) { @@ -95,7 +96,7 @@ int PyEntity_SetName(PyObject* self, PyObject* arg, void*) { Player* p = PyEntity_AsPlayer(self); if (!p) return -1; - p->setName(PyUnicode_AsUTF8(arg)); + p->setNameTag(PyUnicode_AsUTF8(arg)); return 0; } return PyErr_BadArgument(), -1; @@ -106,7 +107,7 @@ PyObject* PyEntity_GetUuid(PyObject* self, void*) { Player* p = PyEntity_AsPlayer(self); if (!p) return nullptr; - return PyUnicode_FromString(p->getUuid().c_str()); + return toPyUnicode(p->getUuid()); } //获取XUID @@ -114,7 +115,7 @@ PyObject* PyEntity_GetXuid(PyObject* self, void*) { Player* p = PyEntity_AsPlayer(self); if (!p) return nullptr; - return PyUnicode_FromString(p->getXuid().c_str()); + return toPyUnicode(p->getXuid()); } //获取坐标 @@ -166,7 +167,7 @@ PyObject* PyEntity_GetTypeName(PyObject* self, void*) { Actor* a = PyEntity_AsActor(self); if (!a) return nullptr; - return PyUnicode_FromString(a->getEntityTypeName().c_str()); + return toPyUnicode(a->getEntityTypeName()); } //获取nbt数据 @@ -174,8 +175,7 @@ PyObject* PyEntity_GetNBTInfo(PyObject* self, void*) { Actor* a = PyEntity_AsActor(self); if (!a) return nullptr; - string str = CompoundTagtoJson(a->save()).dump(4); - return PyUnicode_FromStringAndSize(str.c_str(), str.length()); + return toPyUnicode(CompoundTagtoJson(a->save()).dump(4)); } //获取生命值 @@ -240,8 +240,7 @@ PyObject* PyEntity_GetDeviceId(PyObject* self, void*) { Player* p = PyEntity_AsPlayer(self); if (!p) return nullptr; - string str = p->getDeviceId(); - return PyUnicode_FromStringAndSize(str.c_str(), str.length()); + return toPyUnicode(p->getDeviceId()); } //获取设备类型 @@ -257,8 +256,7 @@ PyObject* PyEntity_GetIP(PyObject* self, void*) { Player* p = PyEntity_AsPlayer(self); if (!p) return nullptr; - string str = g_rak_peer->getSystemAddress(p->getClientId()).toString(); - return PyUnicode_FromStringAndSize(str.c_str(), str.length()); + return toPyUnicode(g_rak_peer->getSystemAddress(p->getClientId()).toString()); } //获取/设置玩家所有物品 @@ -286,8 +284,7 @@ PyObject* PyEntity_GetAllItem(PyObject* self, PyObject*) { value["OffHand"] = CompoundTagtoJson(p->getOffHand()->save()); value["Hand"] = CompoundTagtoJson(p->getSelectedItem()->save()); - string str = value.dump(4); - return PyUnicode_FromStringAndSize(str.c_str(), str.length()); + return toPyUnicode(value.dump(4)); } PyObject* PyEntity_SetAllItem(PyObject* self, PyObject* args) { @@ -606,7 +603,7 @@ PyObject* PyEntity_GetTags(PyObject* self, PyObject*) { span tags = a->getTags(); PyObject* list = PyList_New(0); for (size_t i = 0; i < tags.size; i++) { - PyList_Append(list, PyUnicode_FromString(tags.data[i].c_str())); + PyList_Append(list, toPyUnicode(tags.data[i])); } return list; } diff --git a/BDSpyrunner/Source File/global.cpp b/BDSpyrunner/Source File/global.cpp index 5fdfff1c..c3e4101c 100644 --- a/BDSpyrunner/Source File/global.cpp +++ b/BDSpyrunner/Source File/global.cpp @@ -1,6 +1,5 @@ #include -using namespace std; //指令队列 SPSCQueue* g_command_queue = nullptr; //网络处理 @@ -10,10 +9,4 @@ Level* g_level = nullptr; //网络 RakPeer* g_rak_peer = nullptr; //计分板 -Scoreboard* g_scoreboard = nullptr; -//Py函数表 -unordered_map> g_callback_functions; -//注册命令 -unordered_map> g_commands; -//伤害 -int g_damage = 0; +Scoreboard* g_scoreboard = nullptr; \ No newline at end of file diff --git a/BDSpyrunner/Source File/mod.cpp b/BDSpyrunner/Source File/mod.cpp index aa48ba28..2c15eb23 100644 --- a/BDSpyrunner/Source File/mod.cpp +++ b/BDSpyrunner/Source File/mod.cpp @@ -21,6 +21,12 @@ using namespace std; #pragma region Function Define +//Py鍑芥暟琛 +static unordered_map> g_callback_functions; +//娉ㄥ唽鍛戒护 +static unordered_map> g_commands; +//浼ゅ +static int g_damage = 0; //娉ㄥ叆鏃朵簨浠 static void init(); Json StringtoJson(string_view str) { @@ -591,14 +597,14 @@ static PyObject* PyAPI_runcmd(PyObject*, PyObject* args) { static PyObject* PyAPI_setListener(PyObject*, PyObject* args) { const char* name = ""; PyObject* func; if (PyArg_ParseTuple(args, "sO:setListener", &name, &func)) { - auto found = events.find(name); + auto it = events.find(name); if (!PyFunction_Check(func)) { Py_RETURN_ERROR("Parameter 2 is not callable"); } - if (found == events.end()) { + if (it == events.end()) { Py_RETURN_ERROR("Invalid Listener key words"); } - g_callback_functions[found->second].push_back(func); + g_callback_functions[it->second].push_back(func); } Py_RETURN_NONE; } @@ -608,7 +614,7 @@ static PyObject* PyAPI_setCommandDescription(PyObject*, PyObject* args) { const char* des = ""; PyObject* callback = nullptr; if (PyArg_ParseTuple(args, "ss|O:setCommandDescription", &cmd, &des, &callback)) { - g_commands.insert({ cmd, { des, callback } }); + g_commands[cmd] = { des, callback }; } Py_RETURN_NONE; } @@ -634,8 +640,7 @@ static PyObject* PyAPI_getPlayerList(PyObject*, PyObject*) { } //淇敼鐢熺墿鍙椾激鐨勪激瀹冲 static PyObject* PyAPI_setDamage(PyObject*, PyObject* args) { - if (PyArg_ParseTuple(args, "i:setDamage", &g_damage)) { - } + PyArg_ParseTuple(args, "i:setDamage", &g_damage); Py_RETURN_NONE; } static PyObject* PyAPI_setServerMotd(PyObject*, PyObject* args) { @@ -701,8 +706,7 @@ static PyObject* PyAPI_getStructure(PyObject*, PyObject* args) { StructureTemplate st("tmp"); st.fillFromWorld(bs, &start, &ss); - string str = CompoundTagtoJson(st.save()).dump(4); - return PyUnicode_FromStringAndSize(str.c_str(), str.length()); + return toPyUnicode(CompoundTagtoJson(st.save()).dump(4)); } Py_RETURN_NONE; } @@ -840,7 +844,7 @@ void init() { if (name.front() == '_') continue; cout << "[BDSpyrunner] loading " << name << endl; - PyImport_Import(PyUnicode_FromStringAndSize(name.c_str(), name.length())); + PyImport_Import(toPyUnicode(name)); PyErr_Print(); } }