Add some more utils funcs

This commit is contained in:
Diavolo 2022-04-10 14:00:55 +02:00
parent 25a84bdb0a
commit 79d4aca33d
No known key found for this signature in database
GPG Key ID: FA77F074E98D98A5
5 changed files with 54 additions and 38 deletions

View File

@ -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<int>(value.actionFunc), key.data());
}
return Utils::Hook::Call<Game::xfunction_t(const char**, int*)>(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<int>(value.actionFunc), key.data());
}
return Utils::Hook::Call<Game::xmethod_t(const char**, int*)>(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<int>(builtin.actionFunc), name.data());
}
}
return Utils::Hook::Call<Game::xfunction_t(const char**, int*)>(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<int>(builtin.actionFunc), name.data());
}
}
return Utils::Hook::Call<Game::xmethod_t(const char**)>(0x4C07D0)(pName); // Player_GetMethod
return Utils::Hook::Call<Game::xmethod_t(const char**, int*)>(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();

View File

@ -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();

View File

@ -126,7 +126,7 @@ namespace Components
});
// Misc functions
Script::AddFunction("ToUpper", []()
Script::AddFunction("ToUpper", []() // gsc: ToUpper(<string>)
{
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(<string>, <string>)
{
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(<string>, <string>)
{
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()

View File

@ -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)

View File

@ -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<std::string> 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<std::string> Split(const std::string& str, const char delim);
std::string& LTrim(std::string& str);
std::string& RTrim(std::string& str);