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

Commit

Permalink
修改Player::setName为Acror::setNameTag并采用虚函数调用,将global简化,新增toPyUnicode函数…
Browse files Browse the repository at this point in the history
…,用于转换std::string和PyObject*,简化代码,删除历史提交记录
  • Loading branch information
twoone3l committed Aug 18, 2021
1 parent e65390d commit 1a4a0a4
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 54 deletions.
3 changes: 3 additions & 0 deletions BDSpyrunner/BDSpyrunner.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@
<ClInclude Include="Header File\PyEntity.h" />
<ClInclude Include="Header File\tool.h" />
</ItemGroup>
<ItemGroup>
<None Include="cpp.hint" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
Expand Down
3 changes: 3 additions & 0 deletions BDSpyrunner/BDSpyrunner.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,7 @@
<UniqueIdentifier>{237bf3e8-b292-4ca0-b55a-d5f6832b0cb0}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<None Include="cpp.hint" />
</ItemGroup>
</Project>
6 changes: 4 additions & 2 deletions BDSpyrunner/Header File/Actor.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ struct ScorePacketInfo;
struct Actor {
//获取生物名称信息
std::string getNameTag();
//设置生物名称信息
void setNameTag(const std::string&);
//设置生物名称是否可见
void setNameTagVisible(bool visible);
//获取生物当前所处维度ID
int getDimensionId();
//获取生物当前所在坐标
Expand Down Expand Up @@ -67,8 +71,6 @@ struct Player : Mob {
std::string getUuid();
//根据地图信息获取玩家xuid
std::string& getXuid();
//重设服务器玩家名
void setName(const std::string& name);
//获取网络标识符
NetworkIdentifier* getClientId();
//获取背包
Expand Down
3 changes: 3 additions & 0 deletions BDSpyrunner/Header File/PyEntity.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once
#include <string>
#define PY_SSIZE_T_CLEAN
#include "include/Python.h"

Expand All @@ -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;
// 实体类型
Expand Down
11 changes: 0 additions & 11 deletions BDSpyrunner/Header File/global.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
#pragma once
#include <unordered_map>
#include <vector>
#include <string>

typedef struct _object PyObject;
//指令队列
extern struct SPSCQueue* g_command_queue;
//网络处理
Expand All @@ -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<enum class EventCode, std::vector<PyObject*>> g_callback_functions;
//注册命令
extern std::unordered_map<std::string, std::pair<std::string, PyObject*>> g_commands;
//伤害
extern int g_damage;
19 changes: 12 additions & 7 deletions BDSpyrunner/Source File/Actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ string Actor::getNameTag() {
return SymCall<string&>("?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() {
Expand Down Expand Up @@ -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() {
Expand Down
31 changes: 14 additions & 17 deletions BDSpyrunner/Source File/PyEntity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
#include <ScoreBoard.h>

using namespace std;
PyObject* toPyUnicode(const string& str) {
return PyUnicode_FromStringAndSize(str.c_str(), str.length());
}
Actor* PyEntity_AsActor(PyObject* self) {
if (reinterpret_cast<PyEntity*>(self)->actor)
return reinterpret_cast<PyEntity*>(self)->actor;
Expand Down Expand Up @@ -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());
}

//哈希
Expand Down Expand Up @@ -86,16 +88,15 @@ 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*) {
if (PyUnicode_Check(arg)) {
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;
Expand All @@ -106,15 +107,15 @@ 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
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());
}

//获取坐标
Expand Down Expand Up @@ -166,16 +167,15 @@ 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数据
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));
}

//获取生命值
Expand Down Expand Up @@ -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());
}

//获取设备类型
Expand All @@ -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());
}

//获取/设置玩家所有物品
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -606,7 +603,7 @@ PyObject* PyEntity_GetTags(PyObject* self, PyObject*) {
span<string> 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;
}
Expand Down
9 changes: 1 addition & 8 deletions BDSpyrunner/Source File/global.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <global.h>

using namespace std;
//指令队列
SPSCQueue* g_command_queue = nullptr;
//网络处理
Expand All @@ -10,10 +9,4 @@ Level* g_level = nullptr;
//网络
RakPeer* g_rak_peer = nullptr;
//计分板
Scoreboard* g_scoreboard = nullptr;
//Py函数表
unordered_map<EventCode, vector<PyObject*>> g_callback_functions;
//注册命令
unordered_map<string, pair<string, PyObject*>> g_commands;
//伤害
int g_damage = 0;
Scoreboard* g_scoreboard = nullptr;
22 changes: 13 additions & 9 deletions BDSpyrunner/Source File/mod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@

using namespace std;
#pragma region Function Define
//Py函数表
static unordered_map<EventCode, vector<PyObject*>> g_callback_functions;
//注册命令
static unordered_map<string, pair<string, PyObject*>> g_commands;
//伤害
static int g_damage = 0;
//注入时事件
static void init();
Json StringtoJson(string_view str) {
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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();
}
}
Expand Down

0 comments on commit 1a4a0a4

Please sign in to comment.