From 9f8190486e04c66483dc8cd0ca9fd92fc732789a Mon Sep 17 00:00:00 2001 From: Maya Date: Sat, 9 Jul 2022 00:17:05 +0200 Subject: Add StringToAsset function to squirrel (#216) * Add StringToAsset function to squirrel also added better values for return type enum because otherwise asset can't be returned without casting * Formatting * Switched to a template for the stringToAsset function also renamed files but have to do it in 2 commits because git ignores file case * Rename step 2 * change to lowercase in include * changed to lowercase in include inside dllmain --- .../NorthstarDedicatedTest.vcxproj | 2 ++ .../NorthstarDedicatedTest.vcxproj.filters | 6 +++++ NorthstarDedicatedTest/dllmain.cpp | 3 +++ NorthstarDedicatedTest/scriptutility.cpp | 29 ++++++++++++++++++++++ NorthstarDedicatedTest/scriptutility.h | 4 +++ NorthstarDedicatedTest/squirrel.cpp | 29 ++++++++++++++++++++++ NorthstarDedicatedTest/squirrel.h | 25 +++++++++++++++++-- 7 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 NorthstarDedicatedTest/scriptutility.cpp create mode 100644 NorthstarDedicatedTest/scriptutility.h diff --git a/NorthstarDedicatedTest/NorthstarDedicatedTest.vcxproj b/NorthstarDedicatedTest/NorthstarDedicatedTest.vcxproj index ea9ccbb3..ff6ca2ea 100644 --- a/NorthstarDedicatedTest/NorthstarDedicatedTest.vcxproj +++ b/NorthstarDedicatedTest/NorthstarDedicatedTest.vcxproj @@ -121,6 +121,7 @@ + @@ -616,6 +617,7 @@ + diff --git a/NorthstarDedicatedTest/NorthstarDedicatedTest.vcxproj.filters b/NorthstarDedicatedTest/NorthstarDedicatedTest.vcxproj.filters index 576b9004..0e48f4a0 100644 --- a/NorthstarDedicatedTest/NorthstarDedicatedTest.vcxproj.filters +++ b/NorthstarDedicatedTest/NorthstarDedicatedTest.vcxproj.filters @@ -1515,6 +1515,9 @@ Header Files\Shared\ExploitFixes + + Header Files\Shared + @@ -1682,6 +1685,9 @@ Source Files\Client + + Source Files\Shared + diff --git a/NorthstarDedicatedTest/dllmain.cpp b/NorthstarDedicatedTest/dllmain.cpp index 04cb883b..d64cc34f 100644 --- a/NorthstarDedicatedTest/dllmain.cpp +++ b/NorthstarDedicatedTest/dllmain.cpp @@ -45,6 +45,7 @@ #include #include "version.h" #include "pch.h" +#include "scriptutility.h" #include "rapidjson/document.h" #include "rapidjson/stringbuffer.h" @@ -255,6 +256,7 @@ bool InitialiseNorthstar() AddDllLoadCallbackForClient("client.dll", InitialiseClientVideoOverrides); AddDllLoadCallbackForClient("engine.dll", InitialiseEngineClientRUIHooks); AddDllLoadCallbackForClient("engine.dll", InitialiseDebugOverlay); + AddDllLoadCallbackForClient("client.dll", InitialiseClientSquirrelUtilityFunctions); // audio hooks AddDllLoadCallbackForClient("client.dll", InitialiseMilesAudioHooks); } @@ -267,6 +269,7 @@ bool InitialiseNorthstar() AddDllLoadCallback("server.dll", InitialiseMiscServerScriptCommand); AddDllLoadCallback("server.dll", InitialiseMiscServerFixes); AddDllLoadCallback("server.dll", InitialiseBuildAINFileHooks); + AddDllLoadCallback("server.dll", InitialiseServerSquirrelUtilityFunctions); AddDllLoadCallback("engine.dll", InitialisePlaylistHooks); diff --git a/NorthstarDedicatedTest/scriptutility.cpp b/NorthstarDedicatedTest/scriptutility.cpp new file mode 100644 index 00000000..98c6f3a3 --- /dev/null +++ b/NorthstarDedicatedTest/scriptutility.cpp @@ -0,0 +1,29 @@ +#include "pch.h" +#include "scriptutility.h" +#include "squirrel.h" + +template SQRESULT SQ_StringToAsset(void* sqvm) +{ + if (context == ScriptContext::SERVER) + { + const char* asset = ServerSq_getstring(sqvm, 1); + ServerSq_pushAsset(sqvm, asset, -1); + } + else + { + const char* asset = ClientSq_getstring(sqvm, 1); + ClientSq_pushAsset(sqvm, asset, -1); + } + return SQRESULT_NOTNULL; +} + +void InitialiseClientSquirrelUtilityFunctions(HMODULE baseAddress) +{ + g_ClientSquirrelManager->AddFuncRegistration("asset", "StringToAsset", "string assetName", "", SQ_StringToAsset); + g_UISquirrelManager->AddFuncRegistration("asset", "StringToAsset", "string assetName", "", SQ_StringToAsset); +} + +void InitialiseServerSquirrelUtilityFunctions(HMODULE baseAddress) +{ + g_ServerSquirrelManager->AddFuncRegistration("asset", "StringToAsset", "string assetName", "", SQ_StringToAsset); +} \ No newline at end of file diff --git a/NorthstarDedicatedTest/scriptutility.h b/NorthstarDedicatedTest/scriptutility.h new file mode 100644 index 00000000..2b58ebfb --- /dev/null +++ b/NorthstarDedicatedTest/scriptutility.h @@ -0,0 +1,4 @@ +#pragma once + +void InitialiseClientSquirrelUtilityFunctions(HMODULE baseAddress); +void InitialiseServerSquirrelUtilityFunctions(HMODULE baseAddress); \ No newline at end of file diff --git a/NorthstarDedicatedTest/squirrel.cpp b/NorthstarDedicatedTest/squirrel.cpp index b1c5b4a8..d0d54a3a 100644 --- a/NorthstarDedicatedTest/squirrel.cpp +++ b/NorthstarDedicatedTest/squirrel.cpp @@ -72,6 +72,9 @@ sq_pushboolType ServerSq_pushbool; sq_pusherrorType ClientSq_pusherror; sq_pusherrorType ServerSq_pusherror; +sq_pushAssetType ClientSq_pushAsset; +sq_pushAssetType ServerSq_pushAsset; + // sq stack get funcs sq_getstringType ClientSq_getstring; sq_getstringType ServerSq_getstring; @@ -136,6 +139,7 @@ void InitialiseClientSquirrel(HMODULE baseAddress) ClientSq_pushfloat = (sq_pushfloatType)((char*)baseAddress + 0x3800); ClientSq_pushbool = (sq_pushboolType)((char*)baseAddress + 0x3710); ClientSq_pusherror = (sq_pusherrorType)((char*)baseAddress + 0x8470); + ClientSq_pushAsset = (sq_pushAssetType)((char*)baseAddress + 0x3560); ClientSq_getstring = (sq_getstringType)((char*)baseAddress + 0x60C0); ClientSq_getinteger = (sq_getintegerType)((char*)baseAddress + 0x60E0); @@ -190,6 +194,7 @@ void InitialiseServerSquirrel(HMODULE baseAddress) ServerSq_pushfloat = (sq_pushfloatType)((char*)baseAddress + 0x3800); ServerSq_pushbool = (sq_pushboolType)((char*)baseAddress + 0x3710); ServerSq_pusherror = (sq_pusherrorType)((char*)baseAddress + 0x8440); + ServerSq_pushAsset = (sq_pushAssetType)((char*)baseAddress + 0x3560); ServerSq_getstring = (sq_getstringType)((char*)baseAddress + 0x60A0); ServerSq_getinteger = (sq_getintegerType)((char*)baseAddress + 0x60C0); @@ -485,4 +490,28 @@ template int64_t RegisterSquirrelFuncHook(void* sqvm, SQ return ServerRegisterSquirrelFunc(sqvm, funcReg, unknown); else return ClientRegisterSquirrelFunc(sqvm, funcReg, unknown); +} + +SQReturnTypeEnum GetReturnTypeEnumFromString(const char* returnTypeString) +{ + + static std::map sqEnumStrMap = { + {"bool", SqReturnBoolean}, + {"float", SqReturnFloat}, + {"vector", SqReturnVector}, + {"int", SqReturnInteger}, + {"entity", SqReturnEntity}, + {"string", SqReturnString}, + {"array", SqReturnArrays}, + {"asset", SqReturnAsset}, + {"table", SqReturnTable}}; + + if (sqEnumStrMap.count(returnTypeString)) + { + return sqEnumStrMap[returnTypeString]; + } + else + { + return SqReturnDefault; // previous default value + } } \ No newline at end of file diff --git a/NorthstarDedicatedTest/squirrel.h b/NorthstarDedicatedTest/squirrel.h index d6dfcc57..cbdf0c8c 100644 --- a/NorthstarDedicatedTest/squirrel.h +++ b/NorthstarDedicatedTest/squirrel.h @@ -18,6 +18,20 @@ const SQRESULT SQRESULT_NOTNULL = 1; typedef SQInteger (*SQFunction)(void* sqvm); +enum SQReturnTypeEnum +{ + SqReturnFloat = 0x1, + SqReturnVector = 0x3, + SqReturnInteger = 0x5, + SqReturnBoolean = 0x6, + SqReturnEntity = 0xD, + SqReturnString = 0x21, + SqReturnDefault = 0x20, + SqReturnArrays = 0x25, + SqReturnAsset = 0x28, + SqReturnTable = 0x26, +}; + struct CompileBufferState { const SQChar* buffer; @@ -43,7 +57,7 @@ struct SQFuncRegistration __int32 devLevel; const char* shortNameMaybe; __int32 unknown2; - __int32 returnTypeEnum; + SQReturnTypeEnum returnTypeEnum; __int32* externalBufferPointer; __int64 externalBufferSize; __int64 unknown3; @@ -53,10 +67,12 @@ struct SQFuncRegistration SQFuncRegistration() { memset(this, 0, sizeof(SQFuncRegistration)); - this->returnTypeEnum = 32; + this->returnTypeEnum = SqReturnDefault; } }; +SQReturnTypeEnum GetReturnTypeEnumFromString(const char* returnTypeString); + // core sqvm funcs typedef SQRESULT (*sq_compilebufferType)(void* sqvm, CompileBufferState* compileBuffer, const char* file, int a1, ScriptContext a2); extern sq_compilebufferType ClientSq_compilebuffer; @@ -105,6 +121,10 @@ typedef SQInteger (*sq_pusherrorType)(void* sqvm, const SQChar* error); extern sq_pusherrorType ClientSq_pusherror; extern sq_pusherrorType ServerSq_pusherror; +typedef SQRESULT (*sq_pushAssetType)(void* sqvm, const SQChar* assetName, SQInteger nameLength); +extern sq_pushAssetType ServerSq_pushAsset; +extern sq_pushAssetType ClientSq_pushAsset; + // sq stack get funcs typedef const SQChar* (*sq_getstringType)(void* sqvm, SQInteger stackpos); extern sq_getstringType ClientSq_getstring; @@ -277,6 +297,7 @@ template class SquirrelManager reg->returnTypeString = new char[returnType.size() + 1]; strcpy((char*)reg->returnTypeString, returnType.c_str()); + reg->returnTypeEnum = GetReturnTypeEnumFromString(returnType.c_str()); reg->argTypes = new char[argTypes.size() + 1]; strcpy((char*)reg->argTypes, argTypes.c_str()); -- cgit v1.2.3