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

Commit

Permalink
1.8.0 update, adapt 1.17.32.02, remove Entity::crash, remove bds vers…
Browse files Browse the repository at this point in the history
…ion checking, remove SymCall's error reporting, isPlayer fixed, remove Level::getAllPlayers, add Level::forEachPlayer, Player::getUuid and Player:_getXuid fiexd. when loading plugins, filename is convert to UTF8.
  • Loading branch information
twoone3l committed Oct 1, 2021
1 parent 6ea3608 commit 7c9c9bd
Show file tree
Hide file tree
Showing 13 changed files with 91 additions and 101 deletions.
50 changes: 30 additions & 20 deletions DllMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ constexpr const wchar_t* USER_AGENT = L"Mozilla/5.0 (Windows NT 6.3; Trident/7.0
using namespace std;
using namespace filesystem;

#pragma region Function Define
static vector<PyObject*> g_plugins;

#pragma region Function
//注入时事件
void Init();
//Dll入口函数
Expand Down Expand Up @@ -135,7 +137,7 @@ static void CheckPluginVersion() {
return;
cout << "[BDSpyrunner] Checking plugin version..." << endl;
Json info = AccessUrlForJson(L"https://api.github.com/repos/twoone-3/BDSpyrunner/releases/latest");
if (info["tag_name"] == VERSION_STRING) {
if (info["tag_name"] == PYR_VERSION) {
cout << "[BDSpyrunner] Your plugin version is latest." << endl;
return;
}
Expand All @@ -152,12 +154,12 @@ static void CheckPluginVersion() {
}
//事件回调
template <typename... Args>
static bool EventCallBack(EventCode e, const char* format, Args... args) {
static bool EventCallBack(const EventCode e, const char* format, Args... args) {
bool intercept = true;
Py_BEGIN_CALL;
for (PyObject* cb : g_callback_functions[e]) {
PyObject* result = PyObject_CallFunction(cb, format, args...);
PyErr_Print();
PrintPythonError();
if (result == Py_False)
intercept = false;
}
Expand Down Expand Up @@ -216,6 +218,8 @@ HOOK(ChangeSettingCommand_setup, void, "?setup@ChangeSettingCommand@@SAXAEAVComm
}
original(_this);
}
#pragma endregion
#pragma region Listener
//开服完成
HOOK(onServerStarted, void, "?startServerThread@ServerInstance@@QEAAXXZ",
uintptr_t _this) {
Expand Down Expand Up @@ -253,7 +257,7 @@ HOOK(onConsoleInput, bool, "??$inner_enqueue@$0A@AEBV?$basic_string@DU?$char_tra
cout << '>';
return false;
}
if (EventCallBack(EventCode::onConsoleInput, "O", ToPyUnicode(*cmd)))
if (EventCallBack(EventCode::onConsoleInput, "O", StringToPyUnicode(*cmd)))
return original(_this, cmd);
return false;
}
Expand Down Expand Up @@ -298,7 +302,7 @@ HOOK(onUseItem, bool, "?useItemOn@GameMode@@UEAA_NAEAVItemStack@@AEBVBlockPos@@E
//放置方块
HOOK(onPlaceBlock, bool, "?mayPlace@BlockSource@@QEAA_NAEBVBlock@@AEBVBlockPos@@EPEAVActor@@_N@Z",
BlockSource* _this, Block* b, BlockPos* bp, unsigned char a4, Actor* p, bool _bool) {
if (isPlayer(p)) {
if (IsPlayer(p)) {
BlockLegacy* bl = b->getBlockLegacy();
short bid = bl->getBlockItemID();
string bn = bl->getBlockName();
Expand All @@ -316,7 +320,7 @@ HOOK(onPlaceBlock, bool, "?mayPlace@BlockSource@@QEAA_NAEBVBlock@@AEBVBlockPos@@
//破坏方块
HOOK(onDestroyBlock, bool, "?checkBlockDestroyPermissions@BlockSource@@QEAA_NAEAVActor@@AEBVBlockPos@@AEBVItemStackBase@@_N@Z",
BlockSource* _this, Actor* a1, BlockPos* a2, ItemStack* a3, bool a4) {
if (isPlayer(a1)) {
if (IsPlayer(a1)) {
BlockLegacy* bl = _this->getBlock(a2)->getBlockLegacy();
short bid = bl->getBlockItemID();
string bn = bl->getBlockName();
Expand Down Expand Up @@ -500,7 +504,7 @@ HOOK(onInputCommand, void, "?handle@ServerNetworkHandler@@UEAAXAEBVNetworkIdenti
if (data != g_commands.end() && data->second.second) {
Py_BEGIN_CALL;
PyObject_CallFunction(data->second.second, "O", ToEntity(p));
PyErr_Print();
PrintPythonError();
Py_END_CALL;
return;
}
Expand Down Expand Up @@ -630,7 +634,7 @@ HOOK(onScoreChanged, void, "?onScoreChanged@ServerScoreboard@@UEAAXAEBUScoreboar
//耕地破坏
HOOK(onFallBlockTransform, void, "?transformOnFall@FarmBlock@@UEBAXAEAVBlockSource@@AEBVBlockPos@@PEAVActor@@M@Z",
uintptr_t _this, BlockSource* a1, BlockPos* a2, Actor* p, uintptr_t a4) {
if (isPlayer(p)) {
if (IsPlayer(p)) {
if (!EventCallBack(EventCode::onFallBlockTransform,
"{s:O,s:O,s:i}",
"player", ToEntity(p),
Expand Down Expand Up @@ -714,7 +718,7 @@ HOOK(onRide, bool, "?canAddRider@Actor@@UEBA_NAEAV1@@Z",
return original(a1, a2);
return false;
}
//放入取出物品展示框的物品(未测试)
//放入取出物品展示框的物品
HOOK(onUseFrameBlock, bool, "?use@ItemFrameBlock@@UEBA_NAEAVPlayer@@AEBVBlockPos@@E@Z",
uintptr_t _this, Player* a2, BlockPos* a3) {
if (EventCallBack(EventCode::onUseFrameBlock,
Expand All @@ -726,7 +730,7 @@ HOOK(onUseFrameBlock, bool, "?use@ItemFrameBlock@@UEBA_NAEAVPlayer@@AEBVBlockPos
return original(_this, a2, a3);
return false;
}
//点击物品展示框(未测试)
//点击物品展示框
HOOK(onUseFrameBlocka, bool, "?attack@ItemFrameBlock@@UEBA_NPEAVPlayer@@AEBVBlockPos@@@Z",
uintptr_t _this, Player* a2, BlockPos* a3) {
if (EventCallBack(EventCode::onUseFrameBlock,
Expand All @@ -750,7 +754,7 @@ HOOK(onSneak, void, "?sendActorSneakChanged@ActorEventCoordinator@@QEAAXAEAVActo
if (EventCallBack(EventCode::onSneak, "O", ToEntity(a1)))
return original(_this, a1, a2);
}
//火势蔓延(未测试)
//火势蔓延
HOOK(onFireSpread, bool, "?_trySpawnBlueFire@FireBlock@@AEBA_NAEAVBlockSource@@AEBVBlockPos@@@Z",
uintptr_t _this, BlockSource* bs, BlockPos* bp) {
BlockLegacy* bl = bs->getBlock(bp)->getBlockLegacy();
Expand Down Expand Up @@ -779,7 +783,7 @@ HOOK(onBlockInteracted, void, "?onBlockInteractedWith@VanillaServerGameplayEvent
))
return original(_this, pl, bp);
}
//方块被爆炸破坏(未测试)
//方块被爆炸破坏
HOOK(onBlockExploded, void, "?onExploded@Block@@QEBAXAEAVBlockSource@@AEBVBlockPos@@PEAVActor@@@Z",
Block* _this, BlockSource* bs, BlockPos* bp, Actor* actor) {
BlockLegacy* bl = bs->getBlock(bp)->getBlockLegacy();
Expand Down Expand Up @@ -812,17 +816,23 @@ HOOK(onUseSingBlock, uintptr_t, "?use@SignBlock@@UEBA_NAEAVPlayer@@AEBVBlockPos@
return 0;
}
#pragma endregion

void Init() {
//如果目录不存在创建目录
if (!exists(PLUGIN_PATH))
create_directory(PLUGIN_PATH);
if (!exists(CACHE_PATH))
create_directory(CACHE_PATH);
//检测服务端版本
if (!GetBDSVersion()._Starts_with("1.17.31.01"))
Py_FatalError("[BDSpyrunner] The server version isn't the latest version, unknown problems may occur if you continue to use it");
//if (!GetBDSVersion()._Starts_with("1.17.31.01"))
// Py_FatalError("[BDSpyrunner] The server version isn't the latest version, unknown problems may occur if you continue to use it");

//将plugins/py加入模块搜索路径
Py_SetPath((wstring(PLUGIN_PATH L";") + Py_GetPath()).c_str());
{
wstring py_path(PLUGIN_PATH L";");
py_path.append(Py_GetPath());
Py_SetPath(py_path.c_str());
}
#if 0
//预初始化3.8+
PyPreConfig cfg;
Expand All @@ -848,12 +858,12 @@ void Init() {
if (name.front() == '_')
continue;
cout << "[BDSpyrunner] Loading " << name << endl;
PyImport_Import(ToPyUnicode(name));
PyErr_Print();
g_plugins.push_back(PyImport_Import(StringToPyUnicode(name)));
PrintPythonError();
}
}
//释放当前线程
PyEval_SaveThread();
//输出版本号信息
cout << "[BDSpyrunner] " << VERSION_STRING << " loaded, © 2021 twoone3." << endl;
}
cout << "[BDSpyrunner] " << PYR_VERSION << " loaded, © 2021 twoone3." << endl;
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
![Liscense](https://img.shields.io/github/license/twoone-3/BDSpyrunner)
![Downloads](https://img.shields.io/github/downloads/twoone-3/BDSpyrunner/total)
![Release](https://img.shields.io/github/v/release/twoone-3/BDSpyrunner)
![BDS](https://img.shields.io/badge/support--BDS--version-1.17.31.01-blue)
![BDS](https://img.shields.io/badge/support--BDS--version-1.17.32.02-blue)

[简体中文](README_ZH.md) | English
# Introduction
Expand Down
2 changes: 1 addition & 1 deletion README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
![Liscense](https://img.shields.io/github/license/twoone-3/BDSpyrunner)
![Downloads](https://img.shields.io/github/downloads/twoone-3/BDSpyrunner/total)
![Release](https://img.shields.io/github/v/release/twoone-3/BDSpyrunner)
![BDS](https://img.shields.io/badge/support--BDS--version-1.17.31.01-blue)
![BDS](https://img.shields.io/badge/support--BDS--version-1.17.32.02-blue)

简体中文 | [English](README.md)
# 简介
Expand Down
23 changes: 10 additions & 13 deletions mc/Actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ string Actor::getNameTag() {

//设置生物名称信息

void Actor::setNameTag(const string&name) {
void Actor::setNameTag(const string& name) {
VirtualCall(0x1F8, this, &name);
}

Expand Down Expand Up @@ -79,7 +79,7 @@ ItemStack* Actor::getArmor(int slot) {
//获取实体类型

unsigned Actor::getEntityTypeId() {
return VirtualCall<unsigned>(0x520, this);
return VirtualCall<unsigned>(0x558, this);
//return SymCall<unsigned>("?getEntityTypeId@Actor@@UEBA?AW4ActorType@@XZ", this);
}

Expand Down Expand Up @@ -210,15 +210,15 @@ void Actor::kill() {
string Player::getUuid() {//IDA ServerNetworkHandler::_createNewPlayer 222
string p;
SymCall<string&>("?asString@UUID@mce@@QEBA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ",
this + 3000, &p);
uintptr_t(this) + 2976, &p);
return p;
}

//根据地图信息获取玩家xuid

string& Player::getXuid() {
return SymCall<string&>("?getPlayerXUID@Level@@UEBAAEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@AEBVUUID@mce@@@Z",
getLevel(), this + 3000);
Global<Level>::data, uintptr_t(this) + 2976);
}

//获取网络标识符
Expand Down Expand Up @@ -349,15 +349,6 @@ void Player::sendPacket(uintptr_t pkt) {
this, pkt);
}

//使玩家客户端崩溃
void Player::crash() {
uintptr_t pkt = createPacket(58);
FETCH(int, pkt + 14) = 0;
FETCH(int, pkt + 15) = 0;
FETCH(bool, pkt + 48) = 1;
sendPacket(pkt);
}

unsigned Player::sendModalFormRequestPacket(const string& str) {
static unsigned id = 0;
uintptr_t pkt = createPacket(100);
Expand Down Expand Up @@ -422,3 +413,9 @@ void Player::sendSetScorePacket(char type, const vector<ScorePacketInfo>& slot)
FETCH(vector<ScorePacketInfo>, pkt + 56) = slot;
sendPacket(pkt);
}

bool IsPlayer(Actor* ptr) {
if (ptr && ptr->getEntityTypeId() == 319)
return true;
return false;
}
5 changes: 3 additions & 2 deletions mc/Actor.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ struct Actor {
};
struct Mob : Actor {};
struct Player : Mob {
//获取玩家uuid
std::string getUuid();
//根据地图信息获取玩家xuid
std::string& getXuid();
Expand Down Expand Up @@ -110,8 +111,6 @@ struct Player : Mob {
void sendInventroy();
//刷新区块
void resendAllChunks();
//崩溃客户端
void crash();
//发送数据包
void sendPacket(uintptr_t pkt);
unsigned sendModalFormRequestPacket(const std::string& str);
Expand All @@ -123,3 +122,5 @@ struct Player : Mob {
void sendsetDisplayObjectivePacket(const std::string& title, const std::string& name = "name");
void sendSetScorePacket(char type, const std::vector<ScorePacketInfo>& slot);
};
//是否为玩家
bool IsPlayer(Actor* ptr);
9 changes: 5 additions & 4 deletions mc/Level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ BlockSource* Level::getBlockSource(int did) {
return FETCH(BlockSource*, d + 96);//IDA Level::tickEntities 120
}

void Level::forEachPlayer(const std::function<bool(Player*)>& fn) {
SymCall("?forEachPlayer@Level@@UEBAXV?$function@$$A6A_NAEBVPlayer@@@Z@std@@@Z",
this, &fn);
}

Scoreboard* Level::getScoreBoard() {
return FETCH(Scoreboard*, this + 8600);//IDA Level::getScoreboard
}
Expand All @@ -37,10 +42,6 @@ Player* Level::getPlayerByXuid(const string& xuid) {
this, &xuid);
}

vector<Player*> Level::getAllPlayers() {
return FETCH(vector<Player*>, this + 112);//IDA Level::forEachPlayer
}

BlockPalette* Level::getBlockPalette() {
return FETCH(BlockPalette*, this + 2120);
}
Expand Down
3 changes: 2 additions & 1 deletion mc/Level.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once
#include <vector>
#include <string>
#include <functional>
#include "Position.h"

struct Actor;
Expand All @@ -18,12 +19,12 @@ struct Spawner {
struct Level {
//获取方块源 没这个维度返回空指针
BlockSource* getBlockSource(int did);
void forEachPlayer(const std::function<bool(Player*)>&);
Scoreboard* getScoreBoard();
unsigned getSeed();
std::string getPlayerNames();
Actor* fetchEntity(uintptr_t id);
Player* getPlayerByXuid(const std::string& xuid);
std::vector<Player*> getAllPlayers();
BlockPalette* getBlockPalette();
Spawner* getSpawner();
};
22 changes: 1 addition & 21 deletions mc/tool.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ inline ReturnType VirtualCall(uintptr_t off, void* _this, Args... args) {
// call a function by symbol string
template<typename ReturnType = void, typename... Args>
inline ReturnType SymCall(const char* sym, Args... args) {
void* found = SYM(sym);
if (!found)
std::cerr << "Failed to call " << sym << std::endl;
return reinterpret_cast<ReturnType(*)(Args...)>(found)(args...);
return reinterpret_cast<ReturnType(*)(Args...)>(SYM(sym))(std::forward<Args>(args)...);
}
// replace the function
inline void* SymHook(const char* sym, void* org, void* hook) {
Expand All @@ -55,20 +52,3 @@ struct Global {
inline std::string GetBDSVersion() {
return SymCall<std::string>("?getServerVersionString@Common@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ");
}

//字符串哈希
inline constexpr size_t Hash(const char* s) {
unsigned h = 0;
for (; *s; ++s)
h = 5 * h + *s;
return static_cast<size_t>(h);
}

//是否为玩家
inline bool isPlayer(void* ptr) {
for (auto p : Global<Level>::data->getAllPlayers()) {
if (ptr == p)
return true;
}
return false;
}
11 changes: 11 additions & 0 deletions mod/CPython.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,14 @@
#pragma comment(lib,"lib/python37.lib")
#define PY_SSIZE_T_CLEAN
#include "../include/Python.h"

//×Ö·û´®×ªUnicode
inline PyObject* StringToPyUnicode(std::string_view str) {
return PyUnicode_FromStringAndSize(str.data(), str.length());
}
//´òÓ¡´íÎóÐÅÏ¢
inline void PrintPythonError() {
if (PyErr_Occurred()) {
PyErr_Print();
}
}
Loading

0 comments on commit 7c9c9bd

Please sign in to comment.