Kawe Mazidjatari 446f13680f Improve SQVM function registration and add SERVER and CLIENT logic
SERVER and CLIENT functions can now be registered as well.
For checking whether to register for CLIENT or UI, we now check its context instead of comparing pointers. For server, a dedicated function was build.
2022-03-28 19:34:51 +02:00

203 lines
14 KiB
C++

#pragma once
typedef int SQRESULT;
#define SQ_OK (1)
#define SQ_ERROR (-1)
#define SQ_FAILED(res) (res<0)
#define SQ_SUCCEEDED(res) (res>=0)
enum class SQCONTEXT : int
{
SERVER = 0,
CLIENT,
UI
};
const static std::string SQVM_TYPE_T[3] =
{
"SERVER",
"CLIENT",
"UI",
};
const static std::string SQVM_LOG_T[4] =
{
"Script(S):",
"Script(C):",
"Script(U):",
"Script(X):"
};
const static std::string SQVM_WARNING_LOG_T[4] =
{
"Script(S):Warning:",
"Script(C):Warning:",
"Script(U):Warning:",
"Script(X):Warning:"
};
const static std::string SQVM_ANSI_LOG_T[4] =
{
"\033[38;2;151;149;187mScript(S):",
"\033[38;2;151;149;163mScript(C):",
"\033[38;2;151;123;136mScript(U):",
"\033[38;2;151;149;163mScript(X):"
};
const static std::string SQVM_WARNING_ANSI_LOG_T[4] =
{
"\033[38;2;151;149;187mScript(S):\033[38;2;255;255;000mWarning:",
"\033[38;2;151;149;163mScript(C):\033[38;2;255;255;000mWarning:",
"\033[38;2;151;123;136mScript(U):\033[38;2;255;255;000mWarning:",
"\033[38;2;151;149;163mScript(X):\033[38;2;255;255;000mWarning:"
};
struct SQFuncRegistration
{
const char* m_szScriptName; // 00
const char* m_szNativeName; // 08
const char* m_szHelpString; // 10
const char* m_szRetValType; // 18
const char* m_szArgTypes; // 20
std::int16_t unk28; // 28
std::int16_t padding1; // 2A
std::int32_t unk2c; // 2C
std::int64_t unk30; // 30
std::int32_t unk38; // 38
std::int32_t padding2; // 3C
std::int64_t unk40; // 40
std::int64_t unk48; // 48
std::int64_t unk50; // 50
std::int32_t unk58; // 58
std::int32_t padding3; // 5C
void* m_pFunction; // 60
SQFuncRegistration()
{
memset(this, 0, sizeof(SQFuncRegistration));
this->padding2 = 6;
}
};
namespace
{
/* ==== SQUIRREL ======================================================================================================================================================== */
ADDRESS p_SQVM_PrintFunc = g_mGameDll.FindPatternSIMD((std::uint8_t*)"\x48\x8B\xC4\x48\x89\x50\x10\x4C\x89\x40\x18\x4C\x89\x48\x20\x53\x56\x57\x48\x81\xEC\x30\x08\x00\x00\x48\x8B\xDA\x48\x8D\x70\x18\x48\x8B\xF9\xE8\x00\x00\x00\xFF\x48\x89\x74\x24\x28\x48\x8D\x54\x24\x30\x33", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx???xxxxxxxxxxxx");
void* SQVM_PrintFunc = (void*)p_SQVM_PrintFunc.GetPtr(); /*48 8B C4 48 89 50 10 4C 89 40 18 4C 89 48 20 53 56 57 48 81 EC 30 08 00 00 48 8B DA 48 8D 70 18 48 8B F9 E8 ?? ?? ?? FF 48 89 74 24 28 48 8D 54 24 30 33*/
ADDRESS p_SQVM_WarningFunc = g_mGameDll.FindPatternSIMD((std::uint8_t*)"\x4C\x89\x4C\x24\x20\x44\x89\x44\x24\x18\x89\x54\x24\x10\x53\x55\x56\x57\x41\x54\x41\x55\x41\x56\x41\x57\x48\x83\xEC\x00\x48\x8B", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx?xx");
void* (*SQVM_WarningFunc)(void* sqvm, int a2, int a3, int* nStringSize, void** ppString) = (void* (*)(void*, int, int, int*, void**))p_SQVM_WarningFunc.GetPtr(); /*4C 89 4C 24 20 44 89 44 24 18 89 54 24 10 53 55 56 57 41 54 41 55 41 56 41 57 48 83 EC ?? 48 8B*/
ADDRESS p_SQVM_ErrorFunc = g_mGameDll.FindPatternSIMD((std::uint8_t*)"\x48\x89\x5C\x24\x00\x48\x89\x6C\x24\x00\x48\x89\x74\x24\x00\x48\x89\x7C\x24\x00\x41\x56\x48\x81\xEC\x00\x00\x00\x00\x48\x8B\xD9\x4C\x8B\xF2", "xxxx?xxxx?xxxx?xxxx?xxxxx????xxxxxx");
void* (*SQVM_ErrorFunc)(void* sqvm, const char* pszError, const char* pszFile, unsigned int nLine, int nColumn) = (void* (*)(void*, const char*, const char*, unsigned int, int))p_SQVM_ErrorFunc.GetPtr(); /*48 89 5C 24 ? 48 89 6C 24 ? 48 89 74 24 ? 48 89 7C 24 ? 41 56 48 81 EC ? ? ? ? 48 8B D9 4C 8B F2*/
#if defined (GAMEDLL_S0) || defined (GAMEDLL_S1)
ADDRESS p_SQVM_GetErrorLine = g_mGameDll.FindPatternSIMD((std::uint8_t*)"\x48\x89\x5C\x24\x00\x48\x89\x74\x24\x00\x48\x89\x7C\x24\x00\x55\x41\x54\x41\x55\x41\x56\x41\x57\x48\x8D\xAC\x24\x00\x00\x00\x00\x48\x81\xEC\x00\x00\x00\x00\x83\x65\x90\xFC", "xxxx?xxxx?xxxx?xxxxxxxxxxxxx????xxx????xxxx");
void* (*SQVM_GetErrorLine)(const char* pszFile, int nLine, char* pszContextBuf, int nBufLen) = (void* (*)(const char*, int, char*, int))p_SQVM_GetErrorLine.GetPtr(); /*48 89 5C 24 ? 48 89 74 24 ? 48 89 7C 24 ? 55 41 54 41 55 41 56 41 57 48 8D AC 24 ? ? ? ? 48 81 EC ? ? ? ? 83 65 90 FC*/
ADDRESS p_SQVM_LoadScript = g_mGameDll.FindPatternSIMD((std::uint8_t*)"\x48\x89\x5C\x24\x10\x48\x89\x74\x24\x18\x48\x89\x7C\x24\x20\x48\x89\x4C\x24\x08\x55\x41\x54\x41\x55\x41\x56\x41\x57\x48\x8D\x6C", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
bool (*SQVM_LoadScript)(void* sqvm, const char* szScriptPath, const char* szScriptName, int nFlag) = (bool (*)(void*, const char*, const char*, int))p_SQVM_LoadScript.GetPtr(); /*48 89 5C 24 10 48 89 74 24 18 48 89 7C 24 20 48 89 4C 24 08 55 41 54 41 55 41 56 41 57 48 8D 6C*/
#elif defined (GAMEDLL_S2) || defined (GAMEDLL_S3)
ADDRESS p_SQVM_GetErrorLine = g_mGameDll.FindPatternSIMD((std::uint8_t*)"\x48\x8B\xC4\x55\x56\x48\x8D\xA8\x00\x00\x00\x00\x48\x81\xEC\x00\x00\x00\x00\x83\x65\x90\xFC", "xxxxxxxx????xxx????xxxx");
void* (*SQVM_GetErrorLine)(const char* pszFile, int nLine, char* pszContextBuf, int nBufLen) = (void* (*)(const char*, int, char*, int))p_SQVM_GetErrorLine.GetPtr(); /*48 8B C4 55 56 48 8D A8 ? ? ? ? 48 81 EC ? ? ? ? 83 65 90 FC*/
ADDRESS p_SQVM_LoadScript = g_mGameDll.FindPatternSIMD((std::uint8_t*)"\x48\x8B\xC4\x48\x89\x48\x08\x55\x41\x56\x48\x8D\x68", "xxxxxxxxxxxxx"); /*48 8B C4 48 89 48 08 55 41 56 48 8D 68*/
bool (*SQVM_LoadScript)(void* sqvm, const char* szScriptPath, const char* szScriptName, int nFlag) = (bool (*)(void*, const char*, const char*, int))p_SQVM_LoadScript.GetPtr();
#endif
ADDRESS p_SQVM_LoadRson = g_mGameDll.FindPatternSIMD((std::uint8_t*)"\x4C\x8B\xDC\x49\x89\x5B\x08\x57\x48\x81\xEC\xA0\x00\x00\x00\x33", "xxxxxxxxxxxxxxxx");
void* (*SQVM_LoadRson)(const char* szRsonName) = (void* (*)(const char*))p_SQVM_LoadRson.GetPtr(); /*4C 8B DC 49 89 5B 08 57 48 81 EC A0 00 00 00 33*/
ADDRESS p_SQVM_WarningCmd = g_mGameDll.FindPatternSIMD((std::uint8_t*)"\x40\x53\x48\x83\xEC\x30\x33\xDB\x48\x8D\x44\x24\x00\x4C\x8D\x4C\x24\x00", "xxxxxxxxxxxx?xxxx?");
void* (*SQVM_WarningCmd)(int a1, int a2) = (void* (*)(int, int))p_SQVM_WarningCmd.GetPtr(); /*40 53 48 83 EC 30 33 DB 48 8D 44 24 ?? 4C 8D 4C 24 ??*/
ADDRESS p_SQVM_RegisterFunc = g_mGameDll.FindPatternSIMD((std::uint8_t*)"\x48\x83\xEC\x38\x45\x0F\xB6\xC8", "xxxxxxxx"); /*48 83 EC 38 45 0F B6 C8*/
void* (*SQVM_RegisterFunc)(void* sqvm, SQFuncRegistration* sqFunc, int a1) = (void* (*)(void*, SQFuncRegistration*, int))p_SQVM_RegisterFunc.GetPtr();
ADDRESS p_SQVM_CompileError = g_mGameDll.FindPatternSIMD((std::uint8_t*)"\x48\x89\x5C\x24\x00\x48\x89\x6C\x24\x00\x48\x89\x74\x24\x00\x48\x89\x7C\x24\x00\x41\x56\x48\x81\xEC\x00\x00\x00\x00\x48\x8B\xD9\x4C\x8B\xF2", "xxxx?xxxx?xxxx?xxxx?xxxxx????xxxxxx");
void (*SQVM_CompileError)(void* sqvm, std::int64_t a2, std::int64_t a3, std::uint32_t a4, int a5) = (void(*)(void*, std::int64_t, std::int64_t, std::uint32_t, int))p_SQVM_CompileError.GetPtr();/*48 89 5C 24 ? 48 89 6C 24 ? 48 89 74 24 ? 48 89 7C 24 ? 41 56 48 81 EC ? ? ? ? 48 8B D9 4C 8B F2*/
#if !defined (CLIENT_DLL)
ADDRESS p_SQVM_InitializeSVGlobalScriptStructs = g_mGameDll.FindPatternSIMD((std::uint8_t*)"\x48\x89\x74\x24\x00\x57\x48\x83\xEC\x30\x48\x8B\x3D\x00\x00\x00\x00\x48\x8B\xF1", "xxxx?xxxxxxxx????xxx");
void* (*SQVM_InitializeSVGlobalScriptStructs)(void* sqvm/**(+8)*/) = (void* (*)(void*))p_SQVM_InitializeSVGlobalScriptStructs.GetPtr(); /*48 89 74 24 ? 57 48 83 EC 30 48 8B 3D ? ? ? ? 48 8B F1*/
#endif // !CLIENT_DLL
#if !defined (DEDICATED)
ADDRESS p_SQVM_InitializeCLGlobalScriptStructs = g_mGameDll.FindPatternSIMD((std::uint8_t*)"\x48\x89\x74\x24\x00\x48\x89\x7C\x24\x00\x41\x56\x48\x83\xEC\x30\x48\x63\xC2\x48\x8D\x3D\x00\x00\x00\x00", "xxxx?xxxx?xxxxxxxxxxxx????");
int (*SQVM_InitializeCLGlobalScriptStructs)(void* sqvm/**(+8)*/, SQCONTEXT context) = (int (*)(void*, SQCONTEXT))p_SQVM_InitializeCLGlobalScriptStructs.GetPtr(); /*48 89 74 24 ? 48 89 7C 24 ? 41 56 48 83 EC 30 48 63 C2 48 8D 3D ? ? ? ?*/
#endif // !DEDICATED
#if !defined (CLIENT_DLL) && defined (GAMEDLL_S0) || defined (GAMEDLL_S1)
ADDRESS p_SQVM_CreateServerVM = g_mGameDll.FindPatternSIMD((std::uint8_t*)"\x40\x53\x48\x83\xEC\x50\x48\x8D\x0D\x00\x00\x00\x00", "xxxxxxxxx????");
bool (*SQVM_CreateServerVM)() = (bool(*)())p_SQVM_CreateServerVM.GetPtr(); /*40 53 48 83 EC 50 48 8D 0D ? ? ? ?*/
#elif !defined (CLIENT_DLL) && defined (GAMEDLL_S3) || defined (GAMEDLL_S2)
ADDRESS p_SQVM_CreateServerVM = g_mGameDll.FindPatternSIMD((std::uint8_t*)"\x40\x53\x56\x48\x83\xEC\x48\x48\x8D\x0D\x00\x00\x00\x00", "xxxxxxxxxx????");
bool (*SQVM_CreateServerVM)() = (bool(*)())p_SQVM_CreateServerVM.GetPtr(); /*40 53 56 48 83 EC 48 48 8D 0D ? ? ? ?*/
#endif
#if !defined (DEDICATED) && defined (GAMEDLL_S0) || defined (GAMEDLL_S1) || defined (GAMEDLL_S2)
ADDRESS p_SQVM_CreateClientVM = g_mGameDll.FindPatternSIMD((std::uint8_t*)"\x48\x83\xEC\x58\x48\x83\x3D\x00\x00\x00\x00\x00\x74\x05", "xxxxxxx?????xx");
bool (*SQVM_CreateClientVM)(void* chlclient) = (bool(*)(void*))p_SQVM_CreateClientVM.GetPtr(); /*48 83 EC 58 48 83 3D ? ? ? ? ? 74 05*/
#elif !defined (DEDICATED) && defined (GAMEDLL_S3)
ADDRESS p_SQVM_CreateClientVM = g_mGameDll.FindPatternSIMD((std::uint8_t*)"\x40\x53\x41\x57\x48\x83\xEC\x68\x48\x83\x3D\x00\x00\x00\x00\x00", "xxxxxxxxxxx?????");
bool (*SQVM_CreateClientVM)(void* chlclient) = (bool(*)(void*))p_SQVM_CreateClientVM.GetPtr(); /*40 53 41 57 48 83 EC 68 48 83 3D ? ? ? ? ?*/
#endif
#if !defined (DEDICATED)
ADDRESS p_SQVM_CreateUIVM = g_mGameDll.FindPatternSIMD((std::uint8_t*)"\x40\x53\x48\x83\xEC\x20\x48\x8B\x1D\x00\x00\x00\x00\xC6\x05\x00\x00\x00\x00\x00", "xxxxxxxxx????xx?????");
bool (*SQVM_CreateUIVM)() = (bool(*)())p_SQVM_CreateUIVM.GetPtr(); /*40 53 48 83 EC 20 48 8B 1D ? ? ? ? C6 05 ? ? ? ? ?*/
#endif // !DEDICATED
#if !defined (CLIENT_DLL)
ADDRESS g_pServerVM = p_SQVM_CreateServerVM.FindPatternSelf("48 89 1D", ADDRESS::Direction::DOWN, 150).ResolveRelativeAddressSelf(0x3, 0x7);
#endif // !CLIENT_DLL
#if !defined (DEDICATED)
ADDRESS g_pClientVM = p_SQVM_CreateClientVM.FindPatternSelf("48 83 3D", ADDRESS::Direction::DOWN, 150).ResolveRelativeAddressSelf(0x3, 0x8);
ADDRESS g_pUIVM = p_SQVM_CreateUIVM.FindPatternSelf("48 8B 1D", ADDRESS::Direction::DOWN, 150).ResolveRelativeAddressSelf(0x3, 0x7);
#endif // !DEDICATED
}
void* HSQVM_PrintFunc(void* sqvm, char* fmt, ...);
void* HSQVM_LoadRson(const char* szRsonName);
bool HSQVM_LoadScript(void* sqvm, const char* szScriptPath, const char* szScriptName, int nFlags);
void HSQVM_RegisterFunction(void* sqvm, const char* szName, const char* szHelpString, const char* szRetValType, const char* szArgTypes, void* pFunction);
void SQVM_Attach();
void SQVM_Detach();
///////////////////////////////////////////////////////////////////////////////
class HSQVM : public IDetour
{
virtual void debugp()
{
std::cout << "| FUN: SQVM_PrintFunc : 0x" << std::hex << std::uppercase << p_SQVM_PrintFunc.GetPtr() << std::setw(npad) << " |" << std::endl;
std::cout << "| FUN: SQVM_WarningFunc : 0x" << std::hex << std::uppercase << p_SQVM_WarningFunc.GetPtr() << std::setw(npad) << " |" << std::endl;
std::cout << "| FUN: SQVM_ErrorFunc : 0x" << std::hex << std::uppercase << p_SQVM_ErrorFunc.GetPtr() << std::setw(npad) << " |" << std::endl;
std::cout << "| FUN: SQVM_GetErrorLine : 0x" << std::hex << std::uppercase << p_SQVM_GetErrorLine.GetPtr() << std::setw(npad) << " |" << std::endl;
std::cout << "| FUN: SQVM_LoadScript : 0x" << std::hex << std::uppercase << p_SQVM_LoadScript.GetPtr() << std::setw(npad) << " |" << std::endl;
std::cout << "| FUN: SQVM_LoadRson : 0x" << std::hex << std::uppercase << p_SQVM_LoadRson.GetPtr() << std::setw(npad) << " |" << std::endl;
std::cout << "| FUN: SQVM_WarningCmd : 0x" << std::hex << std::uppercase << p_SQVM_WarningCmd.GetPtr() << std::setw(npad) << " |" << std::endl;
std::cout << "| FUN: SQVM_RegisterFunc : 0x" << std::hex << std::uppercase << p_SQVM_RegisterFunc.GetPtr() << std::setw(npad) << " |" << std::endl;
std::cout << "| FUN: SQVM_CompileError : 0x" << std::hex << std::uppercase << p_SQVM_CompileError.GetPtr() << std::setw(npad) << " |" << std::endl;
#ifndef CLIENT_DLL
std::cout << "| FUN: SQVM_InitializeSVGlobalScriptStructs : 0x" << std::hex << std::uppercase << p_SQVM_InitializeSVGlobalScriptStructs.GetPtr() << std::setw(npad) << " |" << std::endl;
#endif // !CLIENT_DLL
#ifndef DEDICATED
std::cout << "| FUN: SQVM_InitializeCLGlobalScriptStructs : 0x" << std::hex << std::uppercase << p_SQVM_InitializeCLGlobalScriptStructs.GetPtr() << std::setw(npad) << " |" << std::endl;
#endif // !DEDICATED
#ifndef CLIENT_DLL
std::cout << "| FUN: SQVM_CreateServerVM : 0x" << std::hex << std::uppercase << p_SQVM_CreateServerVM.GetPtr() << std::setw(npad) << " |" << std::endl;
#endif // !CLIENT_DLL
#ifndef DEDICATED
std::cout << "| FUN: SQVM_CreateClientVM : 0x" << std::hex << std::uppercase << p_SQVM_CreateClientVM.GetPtr() << std::setw(npad) << " |" << std::endl;
std::cout << "| FUN: SQVM_CreateUIVM : 0x" << std::hex << std::uppercase << p_SQVM_CreateUIVM.GetPtr() << std::setw(npad) << " |" << std::endl;
#endif // !DEDICATED
#ifndef CLIENT_DLL
std::cout << "| VAR: g_pServerVM : 0x" << std::hex << std::uppercase << g_pServerVM.GetPtr() << std::setw(npad) << " |" << std::endl;
#endif // !CLIENT_DLL
#ifndef DEDICATED
std::cout << "| VAR: g_pClientVM : 0x" << std::hex << std::uppercase << g_pClientVM.GetPtr() << std::setw(npad) << " |" << std::endl;
std::cout << "| VAR: g_pUIVM : 0x" << std::hex << std::uppercase << g_pUIVM.GetPtr() << std::setw(npad) << " |" << std::endl;
#endif // !DEDICATED
std::cout << "+----------------------------------------------------------------+" << std::endl;
}
};
///////////////////////////////////////////////////////////////////////////////
REGISTER(HSQVM);