From 4f6f182d26094b7b7512d656b5085f80e8af3dc9 Mon Sep 17 00:00:00 2001 From: Maya Date: Tue, 23 Aug 2022 03:54:54 +0200 Subject: Add ability to load Datatables from files (#238) * first version of kinda working custom datatables * Fix copy error * Finish custom datatables * Fix Merge * Fix line endings * Add fallback to rpak when ns_prefere_datatable_from_disk is true * fix typo * Bug fixess * Fix Function Registration hook * Set convar value * Fix Client and Ui VM * enable server auth with ms agian * Add Filters * FIx unused import * Merge remote-tracking branch 'upsteam/bobs-big-refactor-pr' into datatables Co-authored-by: RoyalBlue1 --- NorthstarDLL/squirrel.cpp | 258 +++++++++++++++++++++++++++++++--------------- 1 file changed, 174 insertions(+), 84 deletions(-) (limited to 'NorthstarDLL/squirrel.cpp') diff --git a/NorthstarDLL/squirrel.cpp b/NorthstarDLL/squirrel.cpp index 8beee760..7cf46f4f 100644 --- a/NorthstarDLL/squirrel.cpp +++ b/NorthstarDLL/squirrel.cpp @@ -17,6 +17,8 @@ const char* GetContextName(ScriptContext context) return "SERVER"; case ScriptContext::UI: return "UI"; + default: + return "UNKNOWN"; } } @@ -39,73 +41,20 @@ eSQReturnType SQReturnTypeFromString(const char* pReturnType) return eSQReturnType::Default; // previous default value } -const char* SQTypeNameFromID(int iTypeId) -{ - switch (iTypeId) - { - case OT_ASSET: - return "asset"; - case OT_INTEGER: - return "int"; - case OT_BOOL: - return "bool"; - case SQOBJECT_NUMERIC: - return "float or int"; - case OT_NULL: - return "null"; - case OT_VECTOR: - return "vector"; - case 0: - return "var"; - case OT_USERDATA: - return "userdata"; - case OT_FLOAT: - return "float"; - case OT_STRING: - return "string"; - case 0x8000040: - return "array"; - case 0x8000200: - return "function"; - case 0x8100000: - return "structdef"; - case OT_THREAD: - return "thread"; - case OT_FUNCPROTO: - return "function"; - case OT_CLAAS: - return "class"; - case OT_WEAKREF: - return "weakref"; - case 0x8080000: - return "unimplemented function"; - case 0x8200000: - return "struct instance"; - case 0xA000020: - return "table"; - case 0xA008000: - return "instance"; - case 0xA400000: - return "entity"; - default: - return ""; - } -} - // needed to define implementations for squirrelmanager outside of squirrel.h without compiler errors template class SquirrelManager; template class SquirrelManager; template class SquirrelManager; -template void SquirrelManager::VMCreated(void* newSqvm) +template void SquirrelManager::VMCreated(CSquirrelVM* newSqvm) { - sqvm = newSqvm; - sqvm2 = *((void**)((char*)sqvm + 8)); // honestly not 100% sure on what this is, but alot of functions take it + SquirrelVM = newSqvm; + sqvm = SquirrelVM->sqvm; // honestly not 100% sure on what this is, but alot of functions take it for (SQFuncRegistration* funcReg : m_funcRegistrations) { spdlog::info("Registering {} function {}", GetContextName(context), funcReg->squirrelFuncName); - RegisterSquirrelFunc(sqvm, funcReg, 1); + RegisterSquirrelFunc(SquirrelVM, funcReg, 1); } for (auto& pair : g_pModManager->m_DependencyConstants) @@ -123,18 +72,18 @@ template void SquirrelManager::VMCreated(void* } } - defconst(sqvm, pair.first.c_str(), bWasFound); + defconst(SquirrelVM, pair.first.c_str(), bWasFound); } } template void SquirrelManager::VMDestroyed() { - sqvm = nullptr; + SquirrelVM = nullptr; } template void SquirrelManager::ExecuteCode(const char* pCode) { - if (!sqvm) + if (!SquirrelVM) { spdlog::error("Cannot execute code, {} squirrel vm is not initialised", GetContextName(context)); return; @@ -150,8 +99,8 @@ template void SquirrelManager::ExecuteCode(cons if (compileResult != SQRESULT_ERROR) { - pushroottable(sqvm2); - SQRESULT callResult = call(sqvm2, 0); + pushroottable(sqvm); + SQRESULT callResult = call(sqvm, 0); spdlog::info("sq_call returned {}", PrintSQRESULT.at(callResult)); } } @@ -182,22 +131,26 @@ template void SquirrelManager::AddFuncRegistrat template SQRESULT SquirrelManager::setupfunc(const SQChar* funcname) { - pushroottable(sqvm2); - pushstring(sqvm2, funcname, -1); + pushroottable(sqvm); + pushstring(sqvm, funcname, -1); - SQRESULT result = get(sqvm2, -2); + SQRESULT result = get(sqvm, -2); if (result != SQRESULT_ERROR) - pushroottable(sqvm2); + pushroottable(sqvm); return result; } +template void SquirrelManager::AddFuncOverride(std::string name, SQFunction func) +{ + m_funcOverrides[name] = func; +} // hooks -template void* (*sq_compiler_create)(void* sqvm, void* a2, void* a3, SQBool bShouldThrowError); -template void* sq_compiler_createHook(void* sqvm, void* a2, void* a3, SQBool bShouldThrowError) +template void* (*sq_compiler_create)(HSquirrelVM* sqvm, void* a2, void* a3, SQBool bShouldThrowError); +template void* sq_compiler_createHook(HSquirrelVM* sqvm, void* a2, void* a3, SQBool bShouldThrowError) { // store whether errors generated from this compile should be fatal - if (context == ScriptContext::CLIENT && sqvm == g_pSquirrel->sqvm2) + if (context == ScriptContext::CLIENT && sqvm == g_pSquirrel->sqvm) g_pSquirrel->m_bCompilationErrorsFatal = bShouldThrowError; else g_pSquirrel->m_bCompilationErrorsFatal = bShouldThrowError; @@ -205,8 +158,8 @@ template void* sq_compiler_createHook(void* sqvm, void* return sq_compiler_create(sqvm, a2, a3, bShouldThrowError); } -template SQInteger (*SQPrint)(void* sqvm, const char* fmt); -template SQInteger SQPrintHook(void* sqvm, const char* fmt, ...) +template SQInteger (*SQPrint)(HSquirrelVM* sqvm, const char* fmt); +template SQInteger SQPrintHook(HSquirrelVM* sqvm, const char* fmt, ...) { va_list va; va_start(va, fmt); @@ -226,16 +179,16 @@ template SQInteger SQPrintHook(void* sqvm, const char* f return 0; } -template void* (*CreateNewVM)(void* a1, ScriptContext contextArg); -template void* CreateNewVMHook(void* a1, ScriptContext realContext) +template CSquirrelVM* (*CreateNewVM)(void* a1, ScriptContext contextArg); +template CSquirrelVM* CreateNewVMHook(void* a1, ScriptContext realContext) { - void* sqvm = CreateNewVM(a1, realContext); + CSquirrelVM* sqvm = CreateNewVM(a1, realContext); if (realContext == ScriptContext::UI) g_pSquirrel->VMCreated(sqvm); else g_pSquirrel->VMCreated(sqvm); - spdlog::info("CreateNewVM {} {}", GetContextName(realContext), sqvm); + spdlog::info("CreateNewVM {} {}", GetContextName(realContext), (void*)sqvm); return sqvm; } @@ -259,7 +212,7 @@ template void ScriptCompileErrorHook(void* sqvm, const c { bool bIsFatalError = g_pSquirrel->m_bCompilationErrorsFatal; ScriptContext realContext = context; // ui and client use the same function so we use this for prints - if (context == ScriptContext::CLIENT && sqvm == g_pSquirrel->sqvm2) + if (context == ScriptContext::CLIENT && sqvm == g_pSquirrel->sqvm) { realContext = ScriptContext::UI; bIsFatalError = g_pSquirrel->m_bCompilationErrorsFatal; @@ -273,7 +226,7 @@ template void ScriptCompileErrorHook(void* sqvm, const c if (bIsFatalError) { // kill dedicated server if we hit this - if (IsDedicatedServer()) + if (IsDedicatedServer()) abort(); else { @@ -294,9 +247,39 @@ template void ScriptCompileErrorHook(void* sqvm, const c // dont call the original function since it kills game lol } + + + +template int64_t(*RegisterSquirrelFunction)(CSquirrelVM* sqvm, SQFuncRegistration* funcReg, char unknown); +template int64_t RegisterSquirrelFunctionHook(CSquirrelVM* sqvm, SQFuncRegistration* funcReg, char unknown) +{ + + if (context == ScriptContext::CLIENT && sqvm == g_pSquirrel->SquirrelVM) + { + if (g_pSquirrel->m_funcOverrides.count(funcReg->squirrelFuncName)) + { + g_pSquirrel->m_funcOriginals[funcReg->squirrelFuncName] = funcReg->funcPtr; + funcReg->funcPtr = g_pSquirrel->m_funcOverrides[funcReg->squirrelFuncName]; + spdlog::info("Replacing {} in UI", std::string(funcReg->squirrelFuncName)); + } + + return g_pSquirrel->RegisterSquirrelFunc(sqvm, funcReg, unknown); + } + + if (g_pSquirrel->m_funcOverrides.count(funcReg->squirrelFuncName)) + { + g_pSquirrel->m_funcOriginals[funcReg->squirrelFuncName] = funcReg->funcPtr; + funcReg->funcPtr = g_pSquirrel->m_funcOverrides[funcReg->squirrelFuncName]; + spdlog::info("Replacing {} in Client", std::string(funcReg->squirrelFuncName)); + } + + return g_pSquirrel->RegisterSquirrelFunc(sqvm, funcReg, unknown); +} + template bool (*CallScriptInitCallback)(void* sqvm, const char* callback); template bool CallScriptInitCallbackHook(void* sqvm, const char* callback) { + ScriptContext realContext = context; bool bShouldCallCustomCallbacks = true; @@ -366,6 +349,12 @@ template void ConCommand_script(const CCommand& args) g_pSquirrel->ExecuteCode(args.ArgS()); } +SQRESULT SQ_Stub(HSquirrelVM* sqvm) +{ + spdlog::info("Stubbed Function called"); + return SQRESULT_NULL; +} + ON_DLL_LOAD_RELIESON("client.dll", ClientSquirrel, ConCommand, (CModule module)) { AUTOHOOK_DISPATCH_MODULE(client.dll) @@ -373,9 +362,9 @@ ON_DLL_LOAD_RELIESON("client.dll", ClientSquirrel, ConCommand, (CModule module)) g_pSquirrel = new SquirrelManager; g_pSquirrel = new SquirrelManager; - g_pSquirrel->RegisterSquirrelFunc = module.Offset(0x108E0).As(); + //g_pSquirrel->RegisterSquirrelFunc = module.Offset(0x108E0).As(); + //g_pSquirrel->RegisterSquirrelFunc = g_pSquirrel->RegisterSquirrelFunc; g_pSquirrel->__sq_defconst = module.Offset(0x12120).As(); - g_pSquirrel->RegisterSquirrelFunc = g_pSquirrel->RegisterSquirrelFunc; g_pSquirrel->__sq_defconst = g_pSquirrel->__sq_defconst; g_pSquirrel->__sq_compilebuffer = module.Offset(0x3110).As(); @@ -400,25 +389,39 @@ ON_DLL_LOAD_RELIESON("client.dll", ClientSquirrel, ConCommand, (CModule module)) g_pSquirrel->__sq_pushinteger = module.Offset(0x36A0).As(); g_pSquirrel->__sq_pushfloat = module.Offset(0x3800).As(); g_pSquirrel->__sq_pushbool = module.Offset(0x3710).As(); + g_pSquirrel->__sq_pushasset = module.Offset(0x3560).As(); + g_pSquirrel->__sq_pushvector = module.Offset(0x3780).As(); g_pSquirrel->__sq_raiseerror = module.Offset(0x8470).As(); - g_pSquirrel->__sq_pushasset = module.Offset(0x3560).As(); g_pSquirrel->__sq_pushstring = g_pSquirrel->__sq_pushstring; g_pSquirrel->__sq_pushinteger = g_pSquirrel->__sq_pushinteger; g_pSquirrel->__sq_pushfloat = g_pSquirrel->__sq_pushfloat; g_pSquirrel->__sq_pushbool = g_pSquirrel->__sq_pushbool; - g_pSquirrel->__sq_raiseerror = g_pSquirrel->__sq_raiseerror; + g_pSquirrel->__sq_pushvector = g_pSquirrel->__sq_pushvector; g_pSquirrel->__sq_pushasset = g_pSquirrel->__sq_pushasset; + g_pSquirrel->__sq_raiseerror = g_pSquirrel->__sq_raiseerror; + g_pSquirrel->__sq_getstring = module.Offset(0x60C0).As(); g_pSquirrel->__sq_getinteger = module.Offset(0x60E0).As(); g_pSquirrel->__sq_getfloat = module.Offset(0x6100).As(); g_pSquirrel->__sq_getbool = module.Offset(0x6130).As(); g_pSquirrel->__sq_get = module.Offset(0x7C30).As(); + g_pSquirrel->__sq_getasset = module.Offset(0x6010).As(); + g_pSquirrel->__sq_getuserdata = module.Offset(0x63D0).As(); + g_pSquirrel->__sq_getvector = module.Offset(0x6140).As(); g_pSquirrel->__sq_getstring = g_pSquirrel->__sq_getstring; g_pSquirrel->__sq_getinteger = g_pSquirrel->__sq_getinteger; g_pSquirrel->__sq_getfloat = g_pSquirrel->__sq_getfloat; g_pSquirrel->__sq_getbool = g_pSquirrel->__sq_getbool; g_pSquirrel->__sq_get = g_pSquirrel->__sq_get; + g_pSquirrel->__sq_getasset = g_pSquirrel->__sq_getasset; + g_pSquirrel->__sq_getuserdata = g_pSquirrel->__sq_getuserdata; + g_pSquirrel->__sq_getvector = g_pSquirrel->__sq_getvector; + + g_pSquirrel->__sq_createuserdata = module.Offset(0x38D0).As(); + g_pSquirrel->__sq_setuserdatatypeid = module.Offset(0x6490).As(); + g_pSquirrel->__sq_createuserdata = g_pSquirrel->__sq_createuserdata; + g_pSquirrel->__sq_setuserdatatypeid = g_pSquirrel->__sq_setuserdatatypeid; // uiscript_reset concommand: don't loop forever if compilation fails module.Offset(0x3C6E4C).NOP(6); @@ -442,8 +445,26 @@ ON_DLL_LOAD_RELIESON("client.dll", ClientSquirrel, ConCommand, (CModule module)) &CallScriptInitCallbackHook, &CallScriptInitCallback); + MAKEHOOK(module.Offset(0x108E0), &RegisterSquirrelFunctionHook, &g_pSquirrel->RegisterSquirrelFunc); + + g_pSquirrel->RegisterSquirrelFunc = g_pSquirrel->RegisterSquirrelFunc; + RegisterConCommand("script_client", ConCommand_script, "Executes script code on the client vm", FCVAR_CLIENTDLL); RegisterConCommand("script_ui", ConCommand_script, "Executes script code on the ui vm", FCVAR_CLIENTDLL); + + g_pSquirrel->AddFuncOverride("DevTextBufferWrite", SQ_Stub); + g_pSquirrel->AddFuncOverride("DevTextBufferClear", SQ_Stub); + g_pSquirrel->AddFuncOverride("DevTextBufferDumpToFile", SQ_Stub); + g_pSquirrel->AddFuncOverride("Dev_CommandLineAddParam", SQ_Stub); + g_pSquirrel->AddFuncOverride("DevP4Checkout", SQ_Stub); + g_pSquirrel->AddFuncOverride("DevP4Add", SQ_Stub); + + g_pSquirrel->AddFuncOverride("DevTextBufferWrite", SQ_Stub); + g_pSquirrel->AddFuncOverride("DevTextBufferClear", SQ_Stub); + g_pSquirrel->AddFuncOverride("DevTextBufferDumpToFile", SQ_Stub); + g_pSquirrel->AddFuncOverride("Dev_CommandLineAddParam", SQ_Stub); + g_pSquirrel->AddFuncOverride("DevP4Checkout", SQ_Stub); + g_pSquirrel->AddFuncOverride("DevP4Add", SQ_Stub); } ON_DLL_LOAD_RELIESON("server.dll", ServerSquirrel, ConCommand, (CModule module)) @@ -452,7 +473,7 @@ ON_DLL_LOAD_RELIESON("server.dll", ServerSquirrel, ConCommand, (CModule module)) g_pSquirrel = new SquirrelManager; - g_pSquirrel->RegisterSquirrelFunc = module.Offset(0x1DD10).As(); + //g_pSquirrel->RegisterSquirrelFunc = module.Offset(0x1DD10).As(); g_pSquirrel->__sq_defconst = module.Offset(0x1F550).As(); g_pSquirrel->__sq_compilebuffer = module.Offset(0x3110).As(); @@ -469,14 +490,22 @@ ON_DLL_LOAD_RELIESON("server.dll", ServerSquirrel, ConCommand, (CModule module)) g_pSquirrel->__sq_pushinteger = module.Offset(0x36A0).As(); g_pSquirrel->__sq_pushfloat = module.Offset(0x3800).As(); g_pSquirrel->__sq_pushbool = module.Offset(0x3710).As(); + g_pSquirrel->__sq_pushasset = module.Offset(0x3560).As(); + g_pSquirrel->__sq_pushvector = module.Offset(0x3780).As(); g_pSquirrel->__sq_raiseerror = module.Offset(0x8440).As(); g_pSquirrel->__sq_getstring = module.Offset(0x60A0).As(); g_pSquirrel->__sq_getinteger = module.Offset(0x60C0).As(); g_pSquirrel->__sq_getfloat = module.Offset(0x60E0).As(); g_pSquirrel->__sq_getbool = module.Offset(0x6110).As(); + g_pSquirrel->__sq_getasset = module.Offset(0x5FF0).As(); + g_pSquirrel->__sq_getuserdata = module.Offset(0x63B0).As(); + g_pSquirrel->__sq_getvector = module.Offset(0x6120).As(); g_pSquirrel->__sq_get = module.Offset(0x7C00).As(); + g_pSquirrel->__sq_createuserdata = module.Offset(0x38D0).As(); + g_pSquirrel->__sq_setuserdatatypeid = module.Offset(0x6470).As(); + MAKEHOOK(module.Offset(0x8AA0), &sq_compiler_createHook, &sq_compiler_create); @@ -493,11 +522,72 @@ ON_DLL_LOAD_RELIESON("server.dll", ServerSquirrel, ConCommand, (CModule module)) &CallScriptInitCallbackHook, &CallScriptInitCallback); - // FCVAR_CHEAT and FCVAR_GAMEDLL_FOR_REMOTE_CLIENTS allows clients to execute this, but since it's unsafe we only allow it when cheats are enabled - // for script_client and script_ui, we don't use cheats, so clients can execute them on themselves all they want + MAKEHOOK(module.Offset(0x1DD10), &RegisterSquirrelFunctionHook, &g_pSquirrel->RegisterSquirrelFunc); + + // FCVAR_CHEAT and FCVAR_GAMEDLL_FOR_REMOTE_CLIENTS allows clients to execute this, but since it's unsafe we only allow it when cheats + // are enabled for script_client and script_ui, we don't use cheats, so clients can execute them on themselves all they want RegisterConCommand( "script", ConCommand_script, "Executes script code on the server vm", FCVAR_GAMEDLL | FCVAR_GAMEDLL_FOR_REMOTE_CLIENTS | FCVAR_CHEAT); + + g_pSquirrel->AddFuncOverride("DevTextBufferWrite", SQ_Stub); + g_pSquirrel->AddFuncOverride("DevTextBufferClear", SQ_Stub); + g_pSquirrel->AddFuncOverride("DevTextBufferDumpToFile", SQ_Stub); + g_pSquirrel->AddFuncOverride("Dev_CommandLineAddParam", SQ_Stub); + g_pSquirrel->AddFuncOverride("DevP4Checkout", SQ_Stub); + g_pSquirrel->AddFuncOverride("DevP4Add", SQ_Stub); } + +const char* SQTypeNameFromID(int type) +{ + switch (type) + { + case OT_ASSET: + return "asset"; + case OT_INTEGER: + return "int"; + case OT_BOOL: + return "bool"; + case SQOBJECT_NUMERIC: + return "float or int"; + case OT_NULL: + return "null"; + case OT_VECTOR: + return "vector"; + case 0: + return "var"; + case OT_USERDATA: + return "userdata"; + case OT_FLOAT: + return "float"; + case OT_STRING: + return "string"; + case OT_ARRAY: + return "array"; + case 0x8000200: + return "function"; + case 0x8100000: + return "structdef"; + case OT_THREAD: + return "thread"; + case OT_FUNCPROTO: + return "function"; + case OT_CLAAS: + return "class"; + case OT_WEAKREF: + return "weakref"; + case 0x8080000: + return "unimplemented function"; + case 0x8200000: + return "struct instance"; + case OT_TABLE: + return "table"; + case 0xA008000: + return "instance"; + case OT_ENTITY: + return "entity"; + } + return ""; +} \ No newline at end of file -- cgit v1.2.3