diff --git a/BDSpyrunner/BDSpyrunner.vcxproj b/BDSpyrunner/BDSpyrunner.vcxproj index e00ffa08..bf644a3c 100644 --- a/BDSpyrunner/BDSpyrunner.vcxproj +++ b/BDSpyrunner/BDSpyrunner.vcxproj @@ -137,7 +137,7 @@ true stdcpp17 $(SolutionDir)BDSpyrunner\Header File;%(AdditionalIncludeDirectories) - Async + Sync Console @@ -160,7 +160,6 @@ - @@ -178,7 +177,6 @@ - diff --git a/BDSpyrunner/BDSpyrunner.vcxproj.filters b/BDSpyrunner/BDSpyrunner.vcxproj.filters index fb5032ec..9b5945fc 100644 --- a/BDSpyrunner/BDSpyrunner.vcxproj.filters +++ b/BDSpyrunner/BDSpyrunner.vcxproj.filters @@ -7,9 +7,6 @@ Source File - - Source File - Source File @@ -80,9 +77,6 @@ Header File - - Header File - Header File diff --git a/BDSpyrunner/Header File/Level.h b/BDSpyrunner/Header File/Level.h index 67306de1..181e75e5 100644 --- a/BDSpyrunner/Header File/Level.h +++ b/BDSpyrunner/Header File/Level.h @@ -10,6 +10,7 @@ struct Scoreboard; struct BlockPalette; struct ItemActor; struct ItemStack; +struct SPSCQueue; struct Spawner { ItemActor* spawnItem(BlockSource* bs, ItemStack* item, Vec3* pos); }; diff --git a/BDSpyrunner/Header File/PyEntity.h b/BDSpyrunner/Header File/PyEntity.h index 913c5a63..ed652f17 100644 --- a/BDSpyrunner/Header File/PyEntity.h +++ b/BDSpyrunner/Header File/PyEntity.h @@ -1,5 +1,6 @@ #pragma once #include +#define PY_SSIZE_T_CLEAN #include "include/Python.h" #define Py_RETURN_ERROR(str) return PyErr_SetString(PyExc_Exception, str), nullptr diff --git a/BDSpyrunner/Header File/global.h b/BDSpyrunner/Header File/global.h deleted file mode 100644 index 5524cba1..00000000 --- a/BDSpyrunner/Header File/global.h +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once -//指令队列 -extern struct SPSCQueue* g_command_queue; -//网络处理 -extern struct ServerNetworkHandler* g_server_network_handler; -//世界 -extern struct Level* g_level; -//网络 -extern struct RakPeer* g_rak_peer; -//计分板 -extern struct Scoreboard* g_scoreboard; diff --git a/BDSpyrunner/Header File/tool.h b/BDSpyrunner/Header File/tool.h index 640e215b..e87c34e4 100644 --- a/BDSpyrunner/Header File/tool.h +++ b/BDSpyrunner/Header File/tool.h @@ -2,8 +2,6 @@ #pragma execution_character_set("utf-8") #include -#define SEH_BEGIN try { -#define SEH_END } catch (...) { cerr << "SEH exception was occured in file " __FILE__ ", funtion " __FUNCTION__ ", line " << __LINE__ << endl; } #define FETCH(type, ptr) (*reinterpret_cast(ptr)) #define SYM GetServerSymbol #define HOOK(name, ret, sym, ...) \ @@ -12,8 +10,7 @@ struct name { \ static func _hook; \ static func* original; \ }; \ -name::func* name::original = \ -*reinterpret_cast(SymHook(sym, name::_hook, &name::original));\ +name::func* name::original = *reinterpret_cast(SymHook(sym, name::_hook, &name::original));\ ret name::_hook(__VA_ARGS__) extern "C" { // provide Detours API @@ -37,6 +34,17 @@ ReturnType SymCall(const char* sym, Args... args) { } // replace the function void* SymHook(const char* sym, void* hook, void* org); +//全局变量 +template +struct Global { + static T* data; +}; +//Global::data = nullptr; +//Global::data = nullptr; +//Global::data = nullptr; +//Global::data = nullptr; +//Global<>::data = nullptr; + constexpr size_t Hash(const char* s); //创建包 uintptr_t createPacket(int type); diff --git a/BDSpyrunner/Source File/Actor.cpp b/BDSpyrunner/Source File/Actor.cpp index b300d5a2..0573e293 100644 --- a/BDSpyrunner/Source File/Actor.cpp +++ b/BDSpyrunner/Source File/Actor.cpp @@ -1,7 +1,7 @@ #include +#include #include #include -#include #include using namespace std; @@ -386,7 +386,7 @@ void Player::sendCommandRequestPacket(const string& cmd) { uintptr_t pkt = createPacket(77); FETCH(string, pkt + 48) = cmd; SymCall("?handle@ServerNetworkHandler@@UEAAXAEBVNetworkIdentifier@@AEBVCommandRequestPacket@@@Z", - g_server_network_handler, getClientId(), pkt); + Global::data, getClientId(), pkt); //p->sendPacket(pkt); } diff --git a/BDSpyrunner/Source File/PyEntity.cpp b/BDSpyrunner/Source File/PyEntity.cpp index 817bac50..5d16c637 100644 --- a/BDSpyrunner/Source File/PyEntity.cpp +++ b/BDSpyrunner/Source File/PyEntity.cpp @@ -1,6 +1,5 @@ #include #include -#include #include #include #include @@ -256,7 +255,7 @@ PyObject* PyEntity_GetIP(PyObject* self, void*) { Player* p = PyEntity_AsPlayer(self); if (!p) return nullptr; - return ToPyUnicode(g_rak_peer->getSystemAddress(p->getClientId()).toString()); + return ToPyUnicode(Global::data->getSystemAddress(p->getClientId()).toString()); } //获取/设置玩家所有物品 @@ -429,9 +428,9 @@ PyObject* PyEntity_GetScore(PyObject* self, PyObject* args) { Player* p = PyEntity_AsPlayer(self); if (!p) return nullptr; - Objective* testobj = g_scoreboard->getObjective(objname); + Objective* testobj = Global::data->getObjective(objname); if (testobj) { - auto id = g_scoreboard->getScoreboardId(p); + auto id = Global::data->getScoreboardId(p); auto score = testobj->getPlayerScore(id); return PyLong_FromLong(score->getCount()); } @@ -445,10 +444,10 @@ PyObject* PyEntity_ModifyScore(PyObject* self, PyObject* args) { Player* p = PyEntity_AsPlayer(self); if (!p) return nullptr; - Objective* testobj = g_scoreboard->getObjective(objname); + Objective* testobj = Global::data->getObjective(objname); if (testobj) { //mode:{set,add,remove} - g_scoreboard->modifyPlayerScore((ScoreboardId*)g_scoreboard->getScoreboardId(p), testobj, count, mode); + Global::data->modifyPlayerScore(Global::data->getScoreboardId(p), testobj, count, mode); } } Py_RETURN_NONE; @@ -535,7 +534,7 @@ PyObject* PyEntity_SetSidebar(PyObject* self, PyObject* args) { vector info; if (value.is_object()) for (auto& [key, val] : value.items()) { - ScorePacketInfo o(g_scoreboard->createScoreBoardId(key), + ScorePacketInfo o(Global::data->createScoreBoardId(key), val.get(), key); info.push_back(o); } diff --git a/BDSpyrunner/Source File/global.cpp b/BDSpyrunner/Source File/global.cpp deleted file mode 100644 index c3e4101c..00000000 --- a/BDSpyrunner/Source File/global.cpp +++ /dev/null @@ -1,12 +0,0 @@ -#include - -//指令队列 -SPSCQueue* g_command_queue = nullptr; -//网络处理 -ServerNetworkHandler* g_server_network_handler = nullptr; -//世界 -Level* g_level = nullptr; -//网络 -RakPeer* g_rak_peer = nullptr; -//计分板 -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 624512fa..c5cb7104 100644 --- a/BDSpyrunner/Source File/mod.cpp +++ b/BDSpyrunner/Source File/mod.cpp @@ -1,11 +1,11 @@ 锘//mod.cpp 鎻掍欢妯″潡 + //鎺掗櫎鏋佸皯浣跨敤鐨刉indows API #define WIN32_LEAN_AND_MEAN #include #include #include #include -#include #include #include #include @@ -18,21 +18,27 @@ constexpr auto VERSION_1 = 1; constexpr auto VERSION_2 = 7; -constexpr auto VERSION_3 = 3; +constexpr auto VERSION_3 = 4; constexpr auto PLUGIN_PATH = L"plugins/py"; constexpr auto MODULE_NAME = "mc"; using namespace std; - +#pragma region global //Py鍑芥暟琛 static unordered_map> g_callback_functions; //娉ㄥ唽鍛戒护 static unordered_map> g_commands; //浼ゅ static int g_damage = 0; +SPSCQueue* Global::data = nullptr; +RakPeer* Global::data = nullptr; +ServerNetworkHandler* Global::data = nullptr; +Level* Global::data = nullptr; +Scoreboard* Global::data = nullptr; +#pragma endregion #pragma region Function Define //娉ㄥ叆鏃朵簨浠 -static void init(); +void Init(); //瀛楃涓茶浆JSON锛屾湰鎻掍欢閲囩敤 https://json.nlohmann.me 鐨凧SON搴3.10.0鐗堟湰 Json StringtoJson(string_view str) { try { @@ -48,25 +54,24 @@ BOOL WINAPI DllMain(HMODULE, DWORD, LPVOID) { return TRUE; } //妫鏌ョ増鏈 -string getBDSVersion() { +string GetBDSVersion() { string version; SymCall("?getServerVersionString@Common@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ", &version); return version; } //杞瀛楃 -static wstring CharToWchar(const string& str) { - int len = MultiByteToWideChar(CP_ACP, 0, str.c_str(), static_cast(str.length()), NULL, 0); +static wstring CharToWchar(string_view str) { + int len = MultiByteToWideChar(CP_ACP, 0, str.data(), static_cast(str.length()), NULL, 0); wstring wstr; wstr.resize(len + 1); - MultiByteToWideChar(CP_ACP, 0, str.c_str(), static_cast(str.length()), wstr.data(), len); + MultiByteToWideChar(CP_ACP, 0, str.data(), static_cast(str.length()), wstr.data(), len); return wstr; } //浜嬩欢鍥炶皟 template static bool EventCallBack(EventCode e, const char* format, Args... args) { bool intercept = true; - SEH_BEGIN; Py_CALL_BEGIN; for (PyObject* cb : g_callback_functions[e]) { PyObject* result = PyObject_CallFunction(cb, format, args...); @@ -75,7 +80,6 @@ static bool EventCallBack(EventCode e, const char* format, Args... args) { intercept = false; } Py_CALL_END; - SEH_END; return intercept; } #pragma endregion @@ -96,38 +100,37 @@ HOOK(Level_tick, void, "?tick@Level@@UEAAXXZ", //灏哖ython瑙i噴鍣ㄥ垵濮嬪寲鎻掑叆bds涓诲嚱鏁 HOOK(BDS_Main, int, "main", int argc, char* argv[], char* envp[]) { - init(); - // 鎵ц main 鍑芥暟 + Init(); return original(argc, argv, envp); } //Level鐨勬瀯閫犲嚱鏁 HOOK(Level_construct, Level*, "??0Level@@QEAA@AEBV?$not_null@V?$NonOwnerPointer@VSoundPlayerInterface@@@Bedrock@@@gsl@@V?$unique_ptr@VLevelStorage@@U?$default_delete@VLevelStorage@@@std@@@std@@V?$unique_ptr@VLevelLooseFileStorage@@U?$default_delete@VLevelLooseFileStorage@@@std@@@4@AEAVIMinecraftEventing@@_NEAEAVScheduler@@V?$not_null@V?$NonOwnerPointer@VStructureManager@@@Bedrock@@@2@AEAVResourcePackManager@@AEBV?$not_null@V?$NonOwnerPointer@VIEntityRegistryOwner@@@Bedrock@@@2@V?$WeakRefT@UEntityRefTraits@@@@V?$unique_ptr@VBlockComponentFactory@@U?$default_delete@VBlockComponentFactory@@@std@@@4@V?$unique_ptr@VBlockDefinitionGroup@@U?$default_delete@VBlockDefinitionGroup@@@std@@@4@@Z", Level* _this, uintptr_t a1, uintptr_t a2, uintptr_t a3, uintptr_t a4, uintptr_t a5, uintptr_t a6, uintptr_t a7, uintptr_t a8, uintptr_t a9, uintptr_t a10, uintptr_t a11, uintptr_t a12, uintptr_t a13) { - return g_level = original(_this, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13); + return Global::data = original(_this, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13); } //SPSCQueue鐨勬瀯閫犲嚱鏁 HOOK(SPSCQueue_construct, SPSCQueue*, "??0?$SPSCQueue@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@$0CAA@@@QEAA@_K@Z", SPSCQueue* _this) { - return g_command_queue = original(_this); + return Global::data = original(_this); } //RakPeer鐨勬瀯閫犲嚱鏁 HOOK(RakPeer_construct, RakPeer*, "??0RakPeer@RakNet@@QEAA@XZ", RakPeer* _this) { //浼氭瀯閫犱袱娆★紝鍙栫涓娆″ - if (g_rak_peer == nullptr) - return g_rak_peer = original(_this); + if (Global::data == nullptr) + return Global::data = original(_this); return original(_this); } //ServerNetworkHandler鐨勬瀯閫犲嚱鏁 HOOK(ServerNetworkHandler_construct, uintptr_t, "??0ServerNetworkHandler@@QEAA@AEAVGameCallbacks@@AEBV?$NonOwnerPointer@VILevel@@@Bedrock@@AEAVNetworkHandler@@AEAVPrivateKeyManager@@AEAVServerLocator@@AEAVPacketSender@@AEAVAllowList@@PEAVPermissionsFile@@AEBVUUID@mce@@H_NAEBV?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@std@@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@HAEAVMinecraftCommands@@AEAVIMinecraftApp@@AEBV?$unordered_map@UPackIdVersion@@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@U?$hash@UPackIdVersion@@@3@U?$equal_to@UPackIdVersion@@@3@V?$allocator@U?$pair@$$CBUPackIdVersion@@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@std@@@3@@std@@AEAVScheduler@@V?$NonOwnerPointer@VTextFilteringProcessor@@@3@@Z", ServerNetworkHandler* _this, uintptr_t a1, uintptr_t a2, uintptr_t a3, uintptr_t a4, uintptr_t a5, uintptr_t a6, uintptr_t a7, uintptr_t a8, uintptr_t a9, uintptr_t a10, uintptr_t a11, uintptr_t a12, uintptr_t a13, uintptr_t a14, uintptr_t a15, uintptr_t a16, uintptr_t a17, uintptr_t a18, uintptr_t a19, uintptr_t a20) { - g_server_network_handler = _this; + Global::data = _this; return original(_this, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20); } //Scoreboard鐨勬瀯閫犲嚱鏁 HOOK(ServerScoreboard_construct, Scoreboard*, "??0ServerScoreboard@@QEAA@VCommandSoftEnumRegistry@@PEAVLevelStorage@@@Z", Scoreboard* _this, uintptr_t a2, uintptr_t a3) { - return g_scoreboard = original(_this, a2, a3); + return Global::data = original(_this, a2, a3); } //鏀瑰彉璁剧疆鍛戒护鐨勫缓绔嬶紝鐢ㄤ簬娉ㄥ唽鍛戒护 HOOK(ChangeSettingCommand_setup, void, "?setup@ChangeSettingCommand@@SAXAEAVCommandRegistry@@@Z", @@ -247,8 +250,8 @@ HOOK(onDestroyBlock, bool, "?checkBlockDestroyPermissions@BlockSource@@QEAA_NAEA json v2(toJson(st2.save())); //print(v2); bp->y++; - st2.placeInWorld(_this, g_level->getBlockPalette(), bp, &ss); - //st.placeInWorld(_this, g_level->getBlockPalette(), bp, &ss); + st2.placeInWorld(_this, Global::data->getBlockPalette(), bp, &ss); + //st.placeInWorld(_this, Global::data->getBlockPalette(), bp, &ss); #endif if (isPlayer(p)) { BlockLegacy* bl = _this->getBlock(bp)->getBlockLegacy(); @@ -340,10 +343,10 @@ HOOK(onContainerChange, void, "?containerContentChanged@LevelContainerModel@@UEA //鐜╁鏀诲嚮 HOOK(onAttack, bool, "?attack@Player@@UEAA_NAEAVActor@@AEBW4ActorDamageCause@@@Z", Player* p, Actor* a, struct ActorDamageCause* c) { - if (a) { - a->setNameTag("鍌婚"); - a->setNameTagVisible(true); - } + //if (a) { + // a->setNameTag("鍌婚"); + // a->setNameTagVisible(true); + //} if (EventCallBack(EventCode::onPlayerAttack, "{s:O,s:O}", "player", PyEntity_FromEntity(p), @@ -713,7 +716,7 @@ HOOK(onFireSpread, bool, "?_trySpawnBlueFire@FireBlock@@AEBA_NAEAVBlockSource@@A //鏂瑰潡浜や簰锛堥櫎绠卞瓙锛屽伐浣滃彴锛 HOOK(onBlockInteracted, void, "?onBlockInteractedWith@VanillaServerGameplayEventListener@@UEAA?AW4EventResult@@AEAVPlayer@@AEBVBlockPos@@@Z", uintptr_t _this, Player* pl, BlockPos* bp) { - BlockSource* bs = g_level->getBlockSource(pl->getDimensionId()); + BlockSource* bs = Global::data->getBlockSource(pl->getDimensionId()); BlockLegacy* bl = bs->getBlock(bp)->getBlockLegacy(); if (EventCallBack(EventCode::onBlockInteracted, "{s:O,s:[i,i,i],s:s,s:i,s:i}", @@ -759,8 +762,9 @@ HOOK(onUseSingBlock, uintptr_t, "?use@SignBlock@@UEBA_NAEAVPlayer@@AEBVBlockPos@ } #pragma endregion #pragma region API Function +namespace mc { //鏈灏忕増鏈姹 -static PyObject* PyAPI_minVersionRequire(PyObject*, PyObject* args) { +static PyObject* minVersionRequire(PyObject*, PyObject* args) { int v1, v2, v3; if (PyArg_ParseTuple(args, "iii:minVersionRequire", &v1, &v2, &v3)) { if (v1 > VERSION_1) @@ -773,11 +777,11 @@ static PyObject* PyAPI_minVersionRequire(PyObject*, PyObject* args) { Py_RETURN_NONE; } //鑾峰彇鐗堟湰锛堟憭寮冿級 -static PyObject* PyAPI_getBDSVersion(PyObject*, PyObject*) { - return ToPyUnicode(getBDSVersion()); +static PyObject* getBDSVersion(PyObject*, PyObject*) { + return ToPyUnicode(GetBDSVersion()); } //鎸囦护杈撳嚭 -static PyObject* PyAPI_logout(PyObject*, PyObject* args) { +static PyObject* logout(PyObject*, PyObject* args) { const char* msg = ""; if (PyArg_ParseTuple(args, "s:logout", &msg)) { SymCall("??$_Insert_string@DU?$char_traits@D@std@@_K@std@@YAAEAV?$basic_ostream@DU?$char_traits@D@std@@@0@AEAV10@QEBD_K@Z", @@ -786,17 +790,17 @@ static PyObject* PyAPI_logout(PyObject*, PyObject* args) { Py_RETURN_NONE; } //鎵ц鎸囦护 -static PyObject* PyAPI_runcmd(PyObject*, PyObject* args) { +static PyObject* runcmd(PyObject*, PyObject* args) { const char* cmd = ""; if (PyArg_ParseTuple(args, "s:runcmd", &cmd)) { - if (!g_command_queue) + if (!Global::data) Py_RETURN_ERROR("Command queue is not initialized"); - onConsoleInput::original(g_command_queue, cmd); + onConsoleInput::original(Global::data, cmd); } Py_RETURN_NONE; } //璁剧疆鐩戝惉 -static PyObject* PyAPI_setListener(PyObject*, PyObject* args) { +static PyObject* setListener(PyObject*, PyObject* args) { const char* name = ""; PyObject* func; if (PyArg_ParseTuple(args, "sO:setListener", &name, &func)) { auto it = events.find(name); @@ -811,7 +815,7 @@ static PyObject* PyAPI_setListener(PyObject*, PyObject* args) { Py_RETURN_NONE; } //璁剧疆鎸囦护璇存槑 -static PyObject* PyAPI_setCommandDescription(PyObject*, PyObject* args) { +static PyObject* setCommandDescription(PyObject*, PyObject* args) { const char* cmd = ""; const char* des = ""; PyObject* callback = nullptr; @@ -821,46 +825,47 @@ static PyObject* PyAPI_setCommandDescription(PyObject*, PyObject* args) { Py_RETURN_NONE; } //鑾峰彇鐜╁ -static PyObject* PyAPI_getPlayerByXuid(PyObject*, PyObject* args) { +static PyObject* getPlayerByXuid(PyObject*, PyObject* args) { const char* xuid = ""; if (PyArg_ParseTuple(args, "s:getPlayerByXuid", &xuid)) { - Player* p = g_level->getPlayerByXuid(xuid); + Player* p = Global::data->getPlayerByXuid(xuid); if (!p) Py_RETURN_ERROR("Failed to find player"); return PyEntity_FromEntity(p); } Py_RETURN_NONE; } -static PyObject* PyAPI_getPlayerList(PyObject*, PyObject*) { +static PyObject* getPlayerList(PyObject*, PyObject*) { PyObject* list = PyList_New(0); - if (!g_level) + if (!Global::data) Py_RETURN_ERROR("Level is not set"); - for (Player* p : g_level->getAllPlayers()) { + for (Player* p : Global::data->getAllPlayers()) { PyList_Append(list, PyEntity_FromEntity(p)); } return list; } //淇敼鐢熺墿鍙椾激鐨勪激瀹冲 -static PyObject* PyAPI_setDamage(PyObject*, PyObject* args) { +static PyObject* setDamage(PyObject*, PyObject* args) { PyArg_ParseTuple(args, "i:setDamage", &g_damage); Py_RETURN_NONE; } -static PyObject* PyAPI_setServerMotd(PyObject*, PyObject* args) { +static PyObject* setServerMotd(PyObject*, PyObject* args) { const char* name = ""; if (PyArg_ParseTuple(args, "s:setServerMotd", &name)) { - if (g_server_network_handler) - SymCall("?allowIncomingConnections@ServerNetworkHandler@@QEAAXAEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@_N@Z", - g_server_network_handler, name, true); - else + if (!Global::data) Py_RETURN_ERROR("Server did not finish loading"); + SymCall("?allowIncomingConnections@ServerNetworkHandler@@QEAAXAEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@_N@Z", + Global::data, name, true); } Py_RETURN_NONE; } //鏍规嵁鍧愭爣璁剧疆鏂瑰潡 -static PyObject* PyAPI_getBlock(PyObject*, PyObject* args) { +static PyObject* getBlock(PyObject*, PyObject* args) { BlockPos bp; int did; if (PyArg_ParseTuple(args, "iiii:getBlock", &bp.x, &bp.y, &bp.z, &did)) { - BlockSource* bs = g_level->getBlockSource(did); + if (!Global::data) + Py_RETURN_ERROR("Level is not set"); + BlockSource* bs = Global::data->getBlockSource(did); if (!bs) Py_RETURN_ERROR("Unknown dimension ID"); BlockLegacy* bl = bs->getBlock(&bp)->getBlockLegacy(); @@ -871,11 +876,13 @@ static PyObject* PyAPI_getBlock(PyObject*, PyObject* args) { } Py_RETURN_NONE; } -static PyObject* PyAPI_setBlock(PyObject*, PyObject* args) { +static PyObject* setBlock(PyObject*, PyObject* args) { const char* name = ""; BlockPos bp; int did; if (PyArg_ParseTuple(args, "siiii:setBlock", &name, &bp.x, &bp.y, &bp.z, &did)) { - BlockSource* bs = g_level->getBlockSource(did); + if (!Global::data) + Py_RETURN_ERROR("Level is not set"); + BlockSource* bs = Global::data->getBlockSource(did); if (!bs) Py_RETURN_ERROR("Unknown dimension ID"); Block* b = *reinterpret_cast(SYM((string("?m") + name + "@VanillaBlocks@@3PEBVBlock@@EB").c_str())); @@ -886,12 +893,14 @@ static PyObject* PyAPI_setBlock(PyObject*, PyObject* args) { Py_RETURN_NONE; } //鑾峰彇涓涓粨鏋 -static PyObject* PyAPI_getStructure(PyObject*, PyObject* args) { +static PyObject* getStructure(PyObject*, PyObject* args) { BlockPos pos1, pos2; int did; if (PyArg_ParseTuple(args, "iiiiiii:getStructure", &pos1.x, &pos1.y, &pos1.z, &pos2.x, &pos2.y, &pos2.z, &did)) { - BlockSource* bs = g_level->getBlockSource(did); + if (!Global::data) + Py_RETURN_ERROR("Level is not set"); + BlockSource* bs = Global::data->getBlockSource(did); if (!bs) Py_RETURN_ERROR("Unknown dimension ID"); BlockPos start{ @@ -912,12 +921,14 @@ static PyObject* PyAPI_getStructure(PyObject*, PyObject* args) { } Py_RETURN_NONE; } -static PyObject* PyAPI_setStructure(PyObject*, PyObject* args) { +static PyObject* setStructure(PyObject*, PyObject* args) { const char* data = ""; BlockPos pos; int did; if (PyArg_ParseTuple(args, "siiii:setStructure", &data, &pos.x, &pos.y, &pos.z, &did)) { - BlockSource* bs = g_level->getBlockSource(did); + if (!Global::data) + Py_RETURN_ERROR("Level is not set"); + BlockSource* bs = Global::data->getBlockSource(did); if (!bs) Py_RETURN_ERROR("Unknown dimension ID"); Json value = StringtoJson(data); @@ -932,7 +943,7 @@ static PyObject* PyAPI_setStructure(PyObject*, PyObject* args) { StructureSettings ss(&size, true, false); StructureTemplate st("tmp"); st.fromJson(value); - st.placeInWorld(bs, g_level->getBlockPalette(), &pos, &ss); + st.placeInWorld(bs, Global::data->getBlockPalette(), &pos, &ss); for (int x = 0; x != size.x; ++x) { for (int y = 0; y != size.y; ++y) { for (int z = 0; z != size.z; ++z) { @@ -945,60 +956,64 @@ static PyObject* PyAPI_setStructure(PyObject*, PyObject* args) { Py_RETURN_NONE; } //浜х敓鐖嗙偢 -static PyObject* PyAPI_explode(PyObject*, PyObject* args) { +static PyObject* explode(PyObject*, PyObject* args) { Vec3 pos; int did; float power; bool destroy; float range; bool fire; if (PyArg_ParseTuple(args, "fffifbfb:explode", &pos.x, &pos.y, &pos.z, &did, &power, &destroy, &range, &fire)) { - BlockSource* bs = g_level->getBlockSource(did); + if (!Global::data) + Py_RETURN_ERROR("Level is not set"); + BlockSource* bs = Global::data->getBlockSource(did); if (!bs) Py_RETURN_ERROR("Unknown dimension ID"); - onLevelExplode::original(g_level, bs, nullptr, pos, power, fire, destroy, range, true); + onLevelExplode::original(Global::data, bs, nullptr, pos, power, fire, destroy, range, true); } Py_RETURN_NONE; } //鐢熸垚鐗╁搧 -static PyObject* PyAPI_spawnItem(PyObject*, PyObject* args) { +static PyObject* spawnItem(PyObject*, PyObject* args) { const char* data = ""; Vec3 pos; int did; if (PyArg_ParseTuple(args, "sfffi:spawnItem", &data, &pos.x, &pos.y, &pos.z, &did)) { - BlockSource* bs = g_level->getBlockSource(did); + if (!Global::data) + Py_RETURN_ERROR("Level is not set"); + BlockSource* bs = Global::data->getBlockSource(did); if (!bs) Py_RETURN_ERROR("Unknown dimension ID"); ItemStack item(StringtoJson(data)); - g_level->getSpawner()->spawnItem(bs, &item, &pos); + Global::data->getSpawner()->spawnItem(bs, &item, &pos); cout << pos.toString() << endl; } Py_RETURN_NONE; } //妯″潡鏂规硶鍒楄〃 -static PyMethodDef PyAPI_Methods[]{ - {"minVersionRequire", PyAPI_minVersionRequire, METH_VARARGS, nullptr}, - {"getBDSVersion", PyAPI_getBDSVersion, METH_NOARGS, nullptr}, - {"logout", PyAPI_logout, METH_VARARGS, nullptr}, - {"runcmd", PyAPI_runcmd, METH_VARARGS, nullptr}, - {"setListener", PyAPI_setListener, METH_VARARGS, nullptr}, - {"setCommandDescription", PyAPI_setCommandDescription, METH_VARARGS, nullptr}, - {"getPlayerByXuid", PyAPI_getPlayerByXuid, METH_VARARGS, nullptr}, - {"getPlayerList", PyAPI_getPlayerList, METH_NOARGS, nullptr}, - {"setDamage", PyAPI_setDamage, METH_VARARGS, nullptr}, - {"setServerMotd", PyAPI_setServerMotd, METH_VARARGS, nullptr}, - {"getBlock", PyAPI_getBlock, METH_VARARGS, nullptr}, - {"setBlock", PyAPI_setBlock, METH_VARARGS, nullptr}, - {"getStructure", PyAPI_getStructure, METH_VARARGS, nullptr}, - {"setStructure", PyAPI_setStructure, METH_VARARGS, nullptr}, - {"explode", PyAPI_explode, METH_VARARGS, nullptr}, - {"spawnItem", PyAPI_spawnItem, METH_VARARGS, nullptr}, +static PyMethodDef Methods[]{ + {"minVersionRequire", minVersionRequire, METH_VARARGS, nullptr}, + {"getBDSVersion", getBDSVersion, METH_NOARGS, nullptr}, + {"logout", logout, METH_VARARGS, nullptr}, + {"runcmd", runcmd, METH_VARARGS, nullptr}, + {"setListener", setListener, METH_VARARGS, nullptr}, + {"setCommandDescription", setCommandDescription, METH_VARARGS, nullptr}, + {"getPlayerByXuid", getPlayerByXuid, METH_VARARGS, nullptr}, + {"getPlayerList", getPlayerList, METH_NOARGS, nullptr}, + {"setDamage", setDamage, METH_VARARGS, nullptr}, + {"setServerMotd", setServerMotd, METH_VARARGS, nullptr}, + {"getBlock", getBlock, METH_VARARGS, nullptr}, + {"setBlock", setBlock, METH_VARARGS, nullptr}, + {"getStructure", getStructure, METH_VARARGS, nullptr}, + {"setStructure", setStructure, METH_VARARGS, nullptr}, + {"explode", explode, METH_VARARGS, nullptr}, + {"spawnItem", spawnItem, METH_VARARGS, nullptr}, {nullptr} }; //妯″潡瀹氫箟 -static PyModuleDef PyAPI_Module{ +static PyModuleDef Module{ PyModuleDef_HEAD_INIT, MODULE_NAME, "API functions", -1, - PyAPI_Methods, + Methods, nullptr, nullptr, nullptr, @@ -1006,21 +1021,23 @@ static PyModuleDef PyAPI_Module{ }; //妯″潡鍒濆鍖 -static PyObject* PyAPI_init() { - PyObject* module = PyModule_Create(&PyAPI_Module); +static PyObject* init() { + PyObject* module = PyModule_Create(&Module); PyModule_AddObject(module, "Entity", reinterpret_cast(&PyEntity_Type)); return module; } + +} #pragma endregion -void init() { +void Init() { using namespace filesystem; cout << "[BDSpyrunner] " << VERSION_1 - << '.' << VERSION_2 << '.' << VERSION_3 << " loaded." << endl; + << '.' << VERSION_2 << '.' << VERSION_3 << "a loaded." << endl; //濡傛灉鐩綍涓嶅瓨鍦ㄥ垱寤虹洰褰 if (!exists(PLUGIN_PATH)) create_directories(PLUGIN_PATH); - if (getBDSVersion() != "1.17.11.01") { + if (GetBDSVersion() != "1.17.11.01") { cerr << "[BDSpyrunner] 鏈嶅姟绔増鏈潪鏈鏂扮増锛岀户缁娇鐢ㄥ彲鑳藉嚭鐜版湭鐭ラ棶棰" << endl; cerr << "[BDSpyrunner] The server version is not the latest version, unknown problems may occur if you continue to use it" << endl; } @@ -1036,7 +1053,7 @@ void init() { #pragma endregion #endif //澧炲姞涓涓ā鍧 - PyImport_AppendInittab(MODULE_NAME, PyAPI_init); + PyImport_AppendInittab(MODULE_NAME, mc::init); //鍒濆鍖栬В閲婂櫒 Py_Initialize(); if (PyType_Ready(&PyEntity_Type) < 0) diff --git a/BDSpyrunner/Source File/tool.cpp b/BDSpyrunner/Source File/tool.cpp index cf009ee4..b204464c 100644 --- a/BDSpyrunner/Source File/tool.cpp +++ b/BDSpyrunner/Source File/tool.cpp @@ -1,5 +1,4 @@ #include -#include #include using namespace std; @@ -27,7 +26,7 @@ uintptr_t createPacket(int type) { } //是否为玩家 bool isPlayer(void* ptr) { - for (auto& p : g_level->getAllPlayers()) { + for (auto& p : Global::data->getAllPlayers()) { if (ptr == p) return true; }