From 79d4aca33ddc65fa85bdb048e9823eee09b8d022 Mon Sep 17 00:00:00 2001 From: Diavolo Date: Sun, 10 Apr 2022 14:00:55 +0200 Subject: [PATCH] Add some more utils funcs --- src/Components/Modules/Script.cpp | 49 +++++++++------------- src/Components/Modules/Script.hpp | 5 +-- src/Components/Modules/ScriptExtension.cpp | 29 ++++++++++++- src/Utils/String.cpp | 5 ++- src/Utils/String.hpp | 4 +- 5 files changed, 54 insertions(+), 38 deletions(-) diff --git a/src/Components/Modules/Script.cpp b/src/Components/Modules/Script.cpp index d7ddc546..292ca51d 100644 --- a/src/Components/Modules/Script.cpp +++ b/src/Components/Modules/Script.cpp @@ -292,29 +292,9 @@ namespace Components CustomScrMethods.insert_or_assign(Utils::String::ToLower(name), std::move(toAdd)); } - Game::xfunction_t Script::Scr_GetFunctionStub(const char** pName, int* type) - { - for (const auto& [key, value] : Script::CustomScrFunctions) - { - Game::Scr_RegisterFunction(reinterpret_cast(value.actionFunc), key.data()); - } - - return Utils::Hook::Call(0x44E700)(pName, type); // Scr_GetFunction - } - - Game::xmethod_t Script::Scr_GetMethodStub(const char** pName, int* type) - { - for (const auto& [key, value] : Script::CustomScrMethods) - { - Game::Scr_RegisterFunction(reinterpret_cast(value.actionFunc), key.data()); - } - - return Utils::Hook::Call(0x4EC870)(pName, type); // Scr_GetMethod - } - Game::xfunction_t Script::BuiltIn_GetFunctionStub(const char** pName, int* type) { - if (pName && *pName) + if (pName) { const auto got = Script::CustomScrFunctions.find(Utils::String::ToLower(*pName)); @@ -325,24 +305,39 @@ namespace Components return got->second.actionFunc; } } + else + { + for (const auto& [name, builtin] : Script::CustomScrFunctions) + { + Game::Scr_RegisterFunction(reinterpret_cast(builtin.actionFunc), name.data()); + } + } return Utils::Hook::Call(0x5FA2B0)(pName, type); // BuiltIn_GetFunction } - Game::xmethod_t Script::Player_GetMethodStub(const char** pName) + Game::xmethod_t Script::BuiltIn_GetMethod(const char** pName, int* type) { - if (pName && *pName) + if (pName) { const auto got = Script::CustomScrMethods.find(Utils::String::ToLower(*pName)); // If no method was found let's call game's function if (got != Script::CustomScrMethods.end()) { + *type = got->second.type; return got->second.actionFunc; } } + else + { + for (const auto& [name, builtin] : Script::CustomScrMethods) + { + Game::Scr_RegisterFunction(reinterpret_cast(builtin.actionFunc), name.data()); + } + } - return Utils::Hook::Call(0x4C07D0)(pName); // Player_GetMethod + return Utils::Hook::Call(0x5FA360)(pName, type); // Player_GetMethod } void Script::StoreScriptBaseProgramNum() @@ -721,13 +716,9 @@ namespace Components Utils::Hook(0x48EFFE, Script::LoadGameType, HOOK_CALL).install()->quick(); Utils::Hook(0x45D44A, Script::LoadGameTypeScript, HOOK_CALL).install()->quick(); - // Register custom functions - Utils::Hook(0x46577C, Script::Scr_GetFunctionStub, HOOK_CALL).install()->quick(); // Scr_BeginLoadScripts - Utils::Hook(0x465787, Script::Scr_GetMethodStub, HOOK_CALL).install()->quick(); // Scr_BeginLoadScripts - // Fetch custom functions Utils::Hook(0x44E72E, Script::BuiltIn_GetFunctionStub, HOOK_CALL).install()->quick(); // Scr_GetFunction - Utils::Hook(0x4EC88E, Script::Player_GetMethodStub, HOOK_CALL).install()->quick(); // Scr_GetMethod + Utils::Hook(0x4EC8DD, Script::BuiltIn_GetMethod, HOOK_CALL).install()->quick(); // Scr_GetMethod Utils::Hook(0x5F41A3, Script::SetExpFogStub, HOOK_CALL).install()->quick(); diff --git a/src/Components/Modules/Script.hpp b/src/Components/Modules/Script.hpp index 457879e5..09a63c22 100644 --- a/src/Components/Modules/Script.hpp +++ b/src/Components/Modules/Script.hpp @@ -49,11 +49,8 @@ namespace Components static void LoadGameType(); static void LoadGameTypeScript(); - static Game::xfunction_t Scr_GetFunctionStub(const char** pName, int* type); - static Game::xmethod_t Scr_GetMethodStub(const char** pName, int* type); - static Game::xfunction_t BuiltIn_GetFunctionStub(const char** pName, int* type); - static Game::xmethod_t Player_GetMethodStub(const char** pName); + static Game::xmethod_t BuiltIn_GetMethod(const char** pName, int* type); static void ScrShutdownSystemStub(unsigned char sys); static void StoreScriptBaseProgramNumStub(); diff --git a/src/Components/Modules/ScriptExtension.cpp b/src/Components/Modules/ScriptExtension.cpp index 7c0ed851..e156a5d0 100644 --- a/src/Components/Modules/ScriptExtension.cpp +++ b/src/Components/Modules/ScriptExtension.cpp @@ -126,7 +126,7 @@ namespace Components }); // Misc functions - Script::AddFunction("ToUpper", []() + Script::AddFunction("ToUpper", []() // gsc: ToUpper() { const auto scriptValue = Game::Scr_GetConstString(0); const auto* string = Game::SL_ConvertToString(scriptValue); @@ -169,6 +169,33 @@ namespace Components Game::SL_RemoveRefToString(scriptValue); } }); + + // Func present on IW5 + Script::AddFunction("StrICmp", []() // gsc: StrICmp(, ) + { + const auto value1 = Game::Scr_GetConstString(0); + const auto value2 = Game::Scr_GetConstString(1); + + const auto result = std::strcmp(Game::SL_ConvertToString(value1), + Game::SL_ConvertToString(value2)); + + Game::Scr_AddInt(result); + }); + + // Func present on IW5 + Script::AddFunction("IsEndStr", []() // gsc: IsEndStr(, ) + { + const auto* s1 = Game::Scr_GetString(0); + const auto* s2 = Game::Scr_GetString(0); + + if (s1 == nullptr || s2 == nullptr) + { + Game::Scr_Error("^1IsEndStr: Illegal parameters!\n"); + return; + } + + Game::Scr_AddBool(Utils::String::EndsWith(s1, s2)); + }); } void ScriptExtension::AddMethods() diff --git a/src/Utils/String.cpp b/src/Utils/String.cpp index 422a9158..3927e48e 100644 --- a/src/Utils/String.cpp +++ b/src/Utils/String.cpp @@ -89,12 +89,13 @@ namespace Utils bool StartsWith(const std::string& haystack, const std::string& needle) { - return (haystack.size() >= needle.size() && haystack.substr(0, needle.size()) == needle); + return haystack.find(needle) == 0; // If the pos of the first found char is 0, string starts with 'needle' } bool EndsWith(const std::string& haystack, const std::string& needle) { - return (haystack.size() >= needle.size() && haystack.substr(haystack.size() - needle.size()) == needle); + if (needle.size() > haystack.size()) return false; + return std::equal(needle.rbegin(), needle.rend(), haystack.rbegin()); } int IsSpace(int c) diff --git a/src/Utils/String.hpp b/src/Utils/String.hpp index bd99e73a..8b2b12aa 100644 --- a/src/Utils/String.hpp +++ b/src/Utils/String.hpp @@ -77,10 +77,10 @@ namespace Utils int IsSpace(int c); std::string ToLower(std::string input); std::string ToUpper(std::string input); - bool EndsWith(const std::string& haystack, const std::string& needle); - std::vector Split(const std::string& str, const char delim); void Replace(std::string& string, const std::string& find, const std::string& replace); + bool EndsWith(const std::string& haystack, const std::string& needle); bool StartsWith(const std::string& haystack, const std::string& needle); + std::vector Split(const std::string& str, const char delim); std::string& LTrim(std::string& str); std::string& RTrim(std::string& str);