From 787eff9bce86997b2fb3cb9b982c6ec722e2d37d Mon Sep 17 00:00:00 2001 From: yangzhi <@4F!xZpJwly&KbWq> Date: Sun, 13 Oct 2019 13:30:52 +0800 Subject: [PATCH] Add HandleInventoryCheck --- CubeModLoader/DLL.h | 1 + .../callbacks/CheckInventoryFullHandler.h | 60 +++++++++++++++++++ CubeModLoader/main.cpp | 3 + 3 files changed, 64 insertions(+) create mode 100644 CubeModLoader/callbacks/CheckInventoryFullHandler.h diff --git a/CubeModLoader/DLL.h b/CubeModLoader/DLL.h index df71319..14917db 100644 --- a/CubeModLoader/DLL.h +++ b/CubeModLoader/DLL.h @@ -17,6 +17,7 @@ class DLL // Callbacks FARPROC HandleChat; FARPROC HandleP2PRequest; + FARPROC HandleInventoryCheck; DLL(std::string fileName); HMODULE Load(); diff --git a/CubeModLoader/callbacks/CheckInventoryFullHandler.h b/CubeModLoader/callbacks/CheckInventoryFullHandler.h new file mode 100644 index 0000000..0abe6ec --- /dev/null +++ b/CubeModLoader/callbacks/CheckInventoryFullHandler.h @@ -0,0 +1,60 @@ +int CheckInventoryFullHandler(void* player, void* item) { + for (DLL* dll: modDLLs) { + if (dll->HandleInventoryCheck) { + if ( int result = ((int(*)(void*, void*))dll->HandleInventoryCheck)(player, item) ){ + return result; + } + } + } + return 0; +} +void* CheckInventoryFullHandler_ptr = (void*)&CheckInventoryFullHandler; + +void* ASMCheckInventoryFullHandler_jmpback; +void* ASMCheckInventoryFullHandler_retn; +void no_optimize ASMCheckInventoryFullHandler() { + asm(PUSH_ALL + + PREPARE_STACK + "sub rsp, 0x0F \n" + "call [CheckInventoryFullHandler_ptr] \n" + "add rsp, 0x0F \n" + + RESTORE_STACK + + // Did the handler return 1? true + "cmp eax, 1 \n" + "je 1f \n" + + // Did the handler return 2? false + "cmp eax, 2 \n" + "je 2f \n" + + // Otherwise? Do nothing + POP_ALL + + // original code + "mov [rsp+0x20], rbp \n" + "push r12 \n" + "push r14 \n" + "push r15 \n" + "sub rsp, 0x20 \n" + "jmp [ASMCheckInventoryFullHandler_jmpback] \n" + + + "1: \n" //full + POP_ALL + "xor al,al \n" + "jmp [ASMCheckInventoryFullHandler_retn] \n" + + "2: \n" //not + POP_ALL + "mov al,1 \n" + "jmp [ASMCheckInventoryFullHandler_retn] \n" + ); +} +void SetupCheckInventoryFullHandler() { + WriteFarJMP(base+0x50670, (void*)&ASMCheckInventoryFullHandler); + ASMCheckInventoryFullHandler_jmpback = (void*)base+0x5067F; + ASMCheckInventoryFullHandler_retn = (void*)base+0x507A0; +} diff --git a/CubeModLoader/main.cpp b/CubeModLoader/main.cpp index 498428b..2198a03 100644 --- a/CubeModLoader/main.cpp +++ b/CubeModLoader/main.cpp @@ -51,10 +51,12 @@ void WriteFarJMP(void* source, void* destination) { #include "callbacks/ChatHandler.h" #include "callbacks/P2PRequestHandler.h" +#include "callbacks/CheckInventoryFullHandler.h" void SetupHandlers() { SetupChatHandler(); SetupP2PRequestHandler(); + SetupCheckInventoryFullHandler(); } void Popup(const char* title, char* msg ){ @@ -93,6 +95,7 @@ extern "C" __declspec(dllexport) BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD IMPORT(dll, ModInitialize); IMPORT(dll, HandleChat); IMPORT(dll, HandleP2PRequest); + IMPORT(dll, HandleInventoryCheck); } // Ensure version compatibility