Skip to content

Commit

Permalink
repair commandPacket hook
Browse files Browse the repository at this point in the history
  • Loading branch information
hhhxiao committed Dec 4, 2020
1 parent 3d15fe9 commit 72b949e
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 45 deletions.
44 changes: 17 additions & 27 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set(CMAKE_CXX_STANDARD 17)
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
add_compile_definitions(-O3 -Wall)
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
add_compile_options(/wd4819 /EHsc)
add_compile_options(/wd4828 /wd4819 /EHsc /utf-8)
endif ()
include_directories(.)

Expand Down
26 changes: 13 additions & 13 deletions commands/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ using namespace SymHook;
//注册命令
void initCommand() {

getCommandManager().registerCmd("tick", "chang level tick speed")
getCommandManager().registerCmd("tick", "改变世界运行状态")
->then(ARG("fz", "freeze the world", NONE, { tick::freezeTick(); }))

->then(ARG("slow", "slow the world run for [num] times", INT, {
Expand All @@ -34,7 +34,7 @@ void initCommand() {
}))


->then(ARG("acc", "accelerate the wold run for [num] times", INT, {
->then(ARG("acc", "加速世界运行[num]倍速", INT, {
auto wrapTime = holder->getInt();
if (wrapTime > 1 && wrapTime <= 10) {
tick::wrapTick(wrapTime);
Expand All @@ -43,9 +43,9 @@ void initCommand() {
}
}))

->then(ARG("r", "reset the world run to default", NONE, { tick::resetTick(); }))
->then(ARG("r", "重置世界运行", NONE, { tick::resetTick(); }))

->then(ARG("fw", "forward the world run for [num] ticks", INT, { tick::forwardTick(holder->getInt()); }));
->then(ARG("fw", "世界运行步进[num] gt", INT, { tick::forwardTick(holder->getInt()); }));

getCommandManager().registerCmd("prof", "ticking profiling")->EXE({ tick::profileWorld(player); });

Expand Down Expand Up @@ -86,14 +86,14 @@ void initCommand() {

});

getCommandManager().registerCmd("vil", "village relative functions", MEMBER)
getCommandManager().registerCmd("village", "村庄相关功能", MEMBER)
->then(ARG("list", "list all ticking villages", NONE, { village::listVillages(player); }))
->then(ARG("show", "show ticking villages bounds and center", BOOL, {
enableVillageShow = holder->getBool();
// info(player, "developing...");
}));

getCommandManager().registerCmd("cfg", "settings")
getCommandManager().registerCmd("cfg", "设置")
->then(ARG("pvd", "config particle view distance(default=128)", INT, {
particleViewDistance = holder->getInt();
info(player, "set particle view distance to %d", particleViewDistance);
Expand Down Expand Up @@ -152,7 +152,7 @@ THook(
MSSYM_B1QA6handleB1AE20ServerNetworkHandlerB2AAE26UEAAXAEBVNetworkIdentifierB2AAE24AEBVCommandRequestPacketB3AAAA1Z,
void *handler,
NetworkIdentifier *id,
void * packet
void * commandPacket
) {
//根据玩家的网络id值来寻找发送命令的玩家
Actor *source = nullptr;
Expand All @@ -164,20 +164,20 @@ THook(
});
if (!source) {
L_DEBUG("can't not find valid player");
original(handler, id, packet);
original(handler, id, commandPacket);
return;
}

//! 这是一处强制转换
std::string commandString(reinterpret_cast<char *>(packet) + 40);
L_DEBUG("player %s execute command %s", source->getNameTag().c_str(), commandString.c_str());
auto *commandString = reinterpret_cast<std::string *>((char *) commandPacket + 40);
L_DEBUG("player %s execute command %s", source->getNameTag().c_str(), commandString->c_str());
//截获命令数据包,获取命令字符串,如果是插件自定义的命令就直接处理,屏蔽原版,如果不是自定义命令就转发给原版去处理
if (getCommandManager().findCommand(commandString)) {
if (getCommandManager().findCommand(*commandString)) {
//解析自定义命令
getCommandManager().parse(source, commandString);
getCommandManager().parse(source, *commandString);
} else {
//转发给原版
original(handler, id, packet);
original(handler, id, commandPacket);
}

}
Expand Down
3 changes: 2 additions & 1 deletion dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
#define DBG_MACRO_NO_WARNING

void mod_init() {
initLogger("trapdoor.log", false);
system("chcp 65001");
initLogger("trapdoor.log", true);
HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD dwMode;
GetConsoleMode(hOutput, &dwMode);
Expand Down
11 changes: 8 additions & 3 deletions test/testMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@
using json = nlohmann::json;

int main() {
initLogger("QAQ", true);
ConfigManager manager;
manager.initialize("config.json");
char *str = new char[12];
for (int i = 0; i < 12; ++i) {
str[i] = 'a';
}
str[11] = 0;
std::string s(str);
delete [] str;
std::cout<<s;
return 0;
}
16 changes: 16 additions & 0 deletions tools/Message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,19 @@ void mcbe_sendMessage(std::string &s, Actor *player) {
}


std::string gbkToUtf8(const char *src_str) {
int len = MultiByteToWideChar(CP_ACP, 0, src_str, -1, nullptr, 0);
auto *wstr = new wchar_t[len + 1];
memset(wstr, 0, len + 1);
MultiByteToWideChar(CP_ACP, 0, src_str, -1, wstr, len);
len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, nullptr, 0, nullptr, nullptr);
char *str = new char[len + 1];
memset(str, 0, len + 1);
WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, nullptr, nullptr);
std::string strTemp(str);
delete[] wstr;
delete[] str;
return strTemp;
}


4 changes: 4 additions & 0 deletions tools/Message.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,8 @@ void broadcastMsg(const std::string &format, Args ... args) {
});
}

//from https://blog.csdn.net/u012234115/article/details/83186386
std::string gbkToUtf8(const char *src_str);


#endif //TRAPDOOR_MESSAGE_H

0 comments on commit 72b949e

Please sign in to comment.