Prefix custom GSC functions with "iw4x_" (#362)

* Prefix custom GSC functions with a "iw4x_" client prefix to preserve older mods & for better backward & forward compatibility

* Prefix bot GSC functions too

* Updated comments

* Undo formatting

* Affect functions aswell

* Add deprecation warnings & auto upgrade of function names for botwarfare needs

* add iw4x_prefix

* Addressed review

* ADdress review²

* Remove choice to add clientprefix or not

* Fixes & follow some of laupetin's suggestions

* Fixe copy paste

* use constexpr const char*

* Update message

Co-authored-by: Louvenarde <louve@louve.systems>
Co-authored-by: Diavolo <edoardo.sanguineti222@gmail.com>
Co-authored-by: rackover <roxanne@thegamebakers.com>
This commit is contained in:
Louve 2022-07-18 13:01:29 +02:00 committed by GitHub
parent 269f484011
commit 617d9ad654
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 131 additions and 57 deletions

View File

@ -119,10 +119,10 @@ namespace Components
void Bots::AddMethods()
{
Script::AddMethod("IsBot", Bots::GScr_isTestClient); // Usage: self IsBot();
Script::AddMethod("IsTestClient", Bots::GScr_isTestClient); // Usage: self IsTestClient();
Script::AddMethod("IsBot", Bots::GScr_isTestClient); // Usage: self iw4x_IsBot();
Script::AddMethod("IsTestClient", Bots::GScr_isTestClient); // Usage: self iw4x_IsTestClient();
Script::AddMethod("BotStop", [](Game::scr_entref_t entref) // Usage: <bot> BotStop();
Script::AddMethod("BotStop", [](Game::scr_entref_t entref) // Usage: <bot> iw4x_BotStop();
{
const auto* ent = Game::GetPlayerEntity(entref);
@ -137,7 +137,7 @@ namespace Components
g_botai[entref.entnum].active = true;
});
Script::AddMethod("BotWeapon", [](Game::scr_entref_t entref) // Usage: <bot> BotWeapon(<str>);
Script::AddMethod("BotWeapon", [](Game::scr_entref_t entref) // Usage: <bot> iw4x_BotWeapon(<str>);
{
const auto* ent = Game::GetPlayerEntity(entref);
@ -160,7 +160,7 @@ namespace Components
g_botai[entref.entnum].active = true;
});
Script::AddMethod("BotAction", [](Game::scr_entref_t entref) // Usage: <bot> BotAction(<str action>);
Script::AddMethod("BotAction", [](Game::scr_entref_t entref) // Usage: <bot> iw4x_BotAction(<str action>);
{
const auto* ent = Game::GetPlayerEntity(entref);
@ -201,7 +201,7 @@ namespace Components
Game::Scr_ParamError(0, "^1BotAction: Unknown action.\n");
});
Script::AddMethod("BotMovement", [](Game::scr_entref_t entref) // Usage: <bot> BotMovement(<int>, <int>);
Script::AddMethod("BotMovement", [](Game::scr_entref_t entref) // Usage: <bot> iw4x_BotMovement(<int>, <int>);
{
const auto* ent = Game::GetPlayerEntity(entref);

View File

@ -389,7 +389,7 @@ namespace Components
void Chat::AddScriptFunctions()
{
Script::AddFunction("OnPlayerSay", [] // gsc: OnPlayerSay(<function>)
Script::AddFunction("OnPlayerSay", [] // gsc: iw4x_OnPlayerSay(<function>)
{
if (Game::Scr_GetNumParam() != 1)
{

View File

@ -351,7 +351,7 @@ namespace Components
void ClientCommand::AddScriptFunctions()
{
Script::AddMethod("Noclip", [](Game::scr_entref_t entref) // gsc: Noclip(<optional int toggle>);
Script::AddMethod("Noclip", [](Game::scr_entref_t entref) // gsc: iw4x_Noclip(<optional int toggle>);
{
const auto* ent = Game::GetPlayerEntity(entref);
@ -372,7 +372,7 @@ namespace Components
}
});
Script::AddMethod("Ufo", [](Game::scr_entref_t entref) // gsc: Ufo(<optional int toggle>);
Script::AddMethod("Ufo", [](Game::scr_entref_t entref) // gsc: iw4x_Ufo(<optional int toggle>);
{
const auto* ent = Game::GetPlayerEntity(entref);
@ -393,7 +393,7 @@ namespace Components
}
});
Script::AddMethod("God", [](Game::scr_entref_t entref) // gsc: God(<optional int toggle>);
Script::AddMethod("God", [](Game::scr_entref_t entref) // gsc: iw4x_God(<optional int toggle>);
{
auto* ent = Game::GetEntity(entref);
@ -414,7 +414,7 @@ namespace Components
}
});
Script::AddMethod("Demigod", [](Game::scr_entref_t entref) // gsc: Demigod(<optional int toggle>);
Script::AddMethod("Demigod", [](Game::scr_entref_t entref) // gsc: iw4x_Demigod(<optional int toggle>);
{
auto* ent = Game::GetEntity(entref);
@ -435,7 +435,7 @@ namespace Components
}
});
Script::AddMethod("Notarget", [](Game::scr_entref_t entref) // gsc: Notarget(<optional int toggle>);
Script::AddMethod("Notarget", [](Game::scr_entref_t entref) // gsc: iw4x_Notarget(<optional int toggle>);
{
auto* ent = Game::GetEntity(entref);
@ -456,7 +456,7 @@ namespace Components
}
});
Script::AddFunction("DropAllBots", [] // gsc: DropAllBots();
Script::AddFunction("DropAllBots", [] // gsc: iw4x_DropAllBots();
{
Game::SV_DropAllBots();
});

View File

@ -8,7 +8,7 @@ namespace Components
void IO::AddScriptFunctions()
{
Script::AddFunction("FileWrite", [] // gsc: FileWrite(<filepath>, <string>, <mode>)
Script::AddFunction("FileWrite", [] // gsc: iw4x_FileWrite(<filepath>, <string>, <mode>)
{
const auto* path = Game::Scr_GetString(0);
auto* text = Game::Scr_GetString(1);
@ -53,7 +53,7 @@ namespace Components
}
});
Script::AddFunction("FileRead", [] // gsc: FileRead(<filepath>)
Script::AddFunction("FileRead", [] // gsc: iw4x_FileRead(<filepath>)
{
const auto* path = Game::Scr_GetString(0);
@ -83,7 +83,7 @@ namespace Components
Game::Scr_AddString(FileSystem::FileReader(scriptData).getBuffer().data());
});
Script::AddFunction("FileExists", [] // gsc: FileExists(<filepath>)
Script::AddFunction("FileExists", [] // gsc: iw4x_FileExists(<filepath>)
{
const auto* path = Game::Scr_GetString(0);

View File

@ -3,9 +3,37 @@
namespace Components
{
std::unordered_map<std::string, Script::ScriptFunction> Script::CustomScrFunctions;
std::unordered_map<std::string, Script::ScriptMethod> Script::CustomScrMethods;
// This was added on the 17th of July 2022 to help transition current mods to
// the new prefixed functions. Look at the clock! If it's more than three months
// later than this date... remove this!
std::unordered_set<std::string_view> Script::DeprecatedFunctionsAndMethods =
{
"isbot",
"istestclient",
"botstop",
"botweapon",
"botmovement",
"botaction",
"onplayersay",
"fileread",
"filewrite",
"fileexists",
"getsystemmilliseconds",
"exec",
"printconsole",
"arecontrolsfrozen",
"setping",
"setname",
"getname",
"dropallbots",
"httpget",
"httpcancel"
};
std::string Script::ScriptName;
std::unordered_map<std::string, Game::BuiltinFunctionDef> Script::CustomScrFunctions;
std::unordered_map<std::string, Game::BuiltinMethodDef> Script::CustomScrMethods;
std::vector<std::string> Script::ScriptNameStack;
unsigned short Script::FunctionName;
std::unordered_map<int, std::string> Script::ScriptBaseProgramNum;
@ -273,32 +301,50 @@ namespace Components
Game::GScr_LoadGameTypeScript();
}
void Script::AddFunction(const char* name, Game::BuiltinFunction func, int type)
void Script::AddFunction(const std::string& name, Game::BuiltinFunction func, bool type)
{
Game::BuiltinFunctionDef toAdd;
toAdd.actionString = name;
const auto functionName = Script::ClientPrefix + name;
Script::ScriptFunction toAdd;
toAdd.actionFunc = func;
toAdd.type = type;
CustomScrFunctions.insert_or_assign(Utils::String::ToLower(name), toAdd);
CustomScrFunctions.insert_or_assign(Utils::String::ToLower(functionName), toAdd);
}
void Script::AddMethod(const char* name, Game::BuiltinMethod func, int type)
void Script::AddMethod(const std::string& name, Game::BuiltinMethod func, bool type)
{
Game::BuiltinMethodDef toAdd;
toAdd.actionString = name;
const auto functionName = Script::ClientPrefix + name;
Script::ScriptMethod toAdd;
toAdd.actionFunc = func;
toAdd.type = type;
CustomScrMethods.insert_or_assign(Utils::String::ToLower(name), toAdd);
CustomScrMethods.insert_or_assign(Utils::String::ToLower(functionName), toAdd);
}
bool Script::IsDeprecated(const std::string& name)
{
return Script::DeprecatedFunctionsAndMethods.contains(name);
}
Game::BuiltinFunction Script::BuiltIn_GetFunctionStub(const char** pName, int* type)
{
if (pName != nullptr)
{
const auto got = Script::CustomScrFunctions.find(Utils::String::ToLower(*pName));
auto name = Utils::String::ToLower(*pName);
if (IsDeprecated(name))
{
Toast::Show("cardicon_gumby", "WARNING!", std::format("{} uses the deprecated function {}", Script::ScriptName, name), 2048);
Logger::Print(Game::CON_CHANNEL_PARSERSCRIPT, "*** DEPRECATION WARNING ***\n");
Logger::PrintError(Game::CON_CHANNEL_ERROR, "Attempted to execute deprecated builtin {} from {}! This method or function should be prefixed with '{}'. Please update your mod!\n", name, Script::ScriptName, Script::ClientPrefix);
Logger::Print(Game::CON_CHANNEL_PARSERSCRIPT, "***************************\n");
name = Script::ClientPrefix + name; // Fixes it automatically
}
const auto got = Script::CustomScrFunctions.find(name);
// If no function was found let's call game's function
if (got != Script::CustomScrFunctions.end())
{
@ -317,12 +363,24 @@ namespace Components
return Utils::Hook::Call<Game::BuiltinFunction(const char**, int*)>(0x5FA2B0)(pName, type); // BuiltIn_GetFunction
}
Game::BuiltinMethod Script::BuiltIn_GetMethod(const char** pName, int* type)
Game::BuiltinMethod Script::BuiltIn_GetMethodStub(const char** pName, int* type)
{
if (pName != nullptr)
{
const auto got = Script::CustomScrMethods.find(Utils::String::ToLower(*pName));
auto name = Utils::String::ToLower(*pName);
if (IsDeprecated(name))
{
Toast::Show("cardicon_gumby", "WARNING!", std::format("{} uses the deprecated method {}", Script::ScriptName, name), 2048);
Logger::Print(Game::CON_CHANNEL_PARSERSCRIPT, "*** DEPRECATION WARNING ***\n");
Logger::PrintError(Game::CON_CHANNEL_ERROR, "Attempted to execute deprecated builtin {} from {}! This function or method should be prefixed with '{}'. Please update your mod!\n", name, Script::ScriptName, Script::ClientPrefix);
Logger::Print(Game::CON_CHANNEL_PARSERSCRIPT, "***************************\n");
name = Script::ClientPrefix + name; // Fixes it automatically
}
const auto got = Script::CustomScrMethods.find(name);
// If no method was found let's call game's function
if (got != Script::CustomScrMethods.end())
{
@ -538,7 +596,7 @@ namespace Components
void Script::AddFunctions()
{
Script::AddFunction("ReplaceFunc", [] // gsc: ReplaceFunc(<function>, <function>)
Script::AddFunction("ReplaceFunc", [] // gsc: iw4x_ReplaceFunc(<function>, <function>)
{
if (Game::Scr_GetNumParam() != 2)
{
@ -553,7 +611,7 @@ namespace Components
});
// System time
Script::AddFunction("GetSystemMilliseconds", [] // gsc: GetSystemMilliseconds()
Script::AddFunction("GetSystemMilliseconds", [] // gsc: iw4x_GetSystemMilliseconds()
{
SYSTEMTIME time;
GetSystemTime(&time);
@ -562,7 +620,7 @@ namespace Components
});
// Executes command to the console
Script::AddFunction("Exec", [] // gsc: Exec(<string>)
Script::AddFunction("Exec", [] // gsc: iw4x_Exec(<string>)
{
const auto str = Game::Scr_GetString(0);
@ -576,7 +634,7 @@ namespace Components
});
// Allow printing to the console even when developer is 0
Script::AddFunction("PrintConsole", [] // gsc: PrintConsole(<string>)
Script::AddFunction("PrintConsole", [] // gsc: iw4x_PrintConsole(<string>)
{
for (std::size_t i = 0; i < Game::Scr_GetNumParam(); ++i)
{
@ -593,7 +651,7 @@ namespace Components
});
// PlayerCmd_AreControlsFrozen GSC function from Black Ops 2
Script::AddMethod("AreControlsFrozen", [](Game::scr_entref_t entref) // Usage: self AreControlsFrozen();
Script::AddMethod("AreControlsFrozen", [](Game::scr_entref_t entref) // Usage: self iw4x_AreControlsFrozen();
{
const auto* ent = Game::GetPlayerEntity(entref);
@ -626,7 +684,7 @@ namespace Components
// Fetch custom functions
Utils::Hook(0x44E72E, Script::BuiltIn_GetFunctionStub, HOOK_CALL).install()->quick(); // Scr_GetFunction
Utils::Hook(0x4EC8DD, Script::BuiltIn_GetMethod, HOOK_CALL).install()->quick(); // Scr_GetMethod
Utils::Hook(0x4EC8DD, Script::BuiltIn_GetMethodStub, HOOK_CALL).install()->quick(); // Scr_GetMethod
Utils::Hook(0x5F41A3, Script::SetExpFogStub, HOOK_CALL).install()->quick();

View File

@ -7,17 +7,32 @@ namespace Components
public:
Script();
static void AddFunction(const char* name, Game::BuiltinFunction func, int type = 0);
static void AddMethod(const char* name, Game::BuiltinMethod func, int type = 0);
static void AddFunction(const std::string& name, Game::BuiltinFunction func, bool type = false);
static void AddMethod(const std::string& name, Game::BuiltinMethod func, bool type = false);
static Game::client_t* GetClient(const Game::gentity_t* gentity);
static const char* GetCodePosForParam(int index);
private:
struct ScriptFunction
{
Game::BuiltinFunction actionFunc;
bool type;
};
struct ScriptMethod
{
Game::BuiltinMethod actionFunc;
bool type;
};
static constexpr auto* ClientPrefix = "iw4x_";
static std::unordered_map<std::string, ScriptFunction> CustomScrFunctions;
static std::unordered_map<std::string, ScriptMethod> CustomScrMethods;
static std::unordered_set<std::string_view> DeprecatedFunctionsAndMethods;
static std::string ScriptName;
static std::unordered_map<std::string, Game::BuiltinFunctionDef> CustomScrFunctions;
static std::unordered_map<std::string, Game::BuiltinMethodDef> CustomScrMethods;
static std::vector<std::string> ScriptNameStack;
static unsigned short FunctionName;
static std::unordered_map<int, std::string> ScriptBaseProgramNum;
@ -46,8 +61,9 @@ namespace Components
static void Scr_StartupGameType_Stub();
static void GScr_LoadGameTypeScript_Stub();
static bool IsDeprecated(const std::string& name);
static Game::BuiltinFunction BuiltIn_GetFunctionStub(const char** pName, int* type);
static Game::BuiltinMethod BuiltIn_GetMethod(const char** pName, int* type);
static Game::BuiltinMethod BuiltIn_GetMethodStub(const char** pName, int* type);
static void StoreScriptBaseProgramNumStub();
static void StoreScriptBaseProgramNum();

View File

@ -116,7 +116,7 @@ namespace Components
void ScriptExtension::AddFunctions()
{
// Misc functions
Script::AddFunction("ToUpper", [] // gsc: ToUpper(<string>)
Script::AddFunction("ToUpper", [] // gsc: iw4x_ToUpper(<string>)
{
const auto scriptValue = Game::Scr_GetConstString(0);
const auto* string = Game::SL_ConvertToString(scriptValue);
@ -161,7 +161,7 @@ namespace Components
});
// Func present on IW5
Script::AddFunction("StrICmp", [] // gsc: StrICmp(<string>, <string>)
Script::AddFunction("StrICmp", [] // gsc: iw4x_StrICmp(<string>, <string>)
{
const auto value1 = Game::Scr_GetConstString(0);
const auto value2 = Game::Scr_GetConstString(1);
@ -173,7 +173,7 @@ namespace Components
});
// Func present on IW5
Script::AddFunction("IsEndStr", [] // gsc: IsEndStr(<string>, <string>)
Script::AddFunction("IsEndStr", [] // gsc: iw4x_IsEndStr(<string>, <string>)
{
const auto* s1 = Game::Scr_GetString(0);
const auto* s2 = Game::Scr_GetString(1);
@ -187,7 +187,7 @@ namespace Components
Game::Scr_AddBool(Utils::String::EndsWith(s1, s2));
});
Script::AddFunction("IsArray", [] // gsc: IsArray(<object>)
Script::AddFunction("IsArray", [] // gsc: iw4x_IsArray(<object>)
{
const auto type = Game::Scr_GetType(0);
@ -211,7 +211,7 @@ namespace Components
void ScriptExtension::AddMethods()
{
// ScriptExtension methods
Script::AddMethod("GetIp", [](Game::scr_entref_t entref) // gsc: self GetIp()
Script::AddMethod("GetIp", [](Game::scr_entref_t entref) // gsc: self iw4x_GetIp()
{
const auto* ent = Game::GetPlayerEntity(entref);
const auto* client = Script::GetClient(ent);
@ -224,7 +224,7 @@ namespace Components
Game::Scr_AddString(ip.data());
});
Script::AddMethod("GetPing", [](Game::scr_entref_t entref) // gsc: self GetPing()
Script::AddMethod("GetPing", [](Game::scr_entref_t entref) // gsc: self iw4x_GetPing()
{
const auto* ent = Game::GetPlayerEntity(entref);
const auto* client = Script::GetClient(ent);
@ -232,7 +232,7 @@ namespace Components
Game::Scr_AddInt(client->ping);
});
Script::AddMethod("SetPing", [](Game::scr_entref_t entref) // gsc: self SetPing(<int>)
Script::AddMethod("SetPing", [](Game::scr_entref_t entref) // gsc: self iw4x_SetPing(<int>)
{
auto ping = Game::Scr_GetInt(0);

View File

@ -8,7 +8,7 @@ namespace Components
void ScriptStorage::AddScriptFunctions()
{
Script::AddFunction("StorageSet", [] // gsc: StorageSet(<str key>, <str data>);
Script::AddFunction("StorageSet", [] // gsc: iw4x_StorageSet(<str key>, <str data>);
{
const auto* key = Game::Scr_GetString(0);
const auto* value = Game::Scr_GetString(1);
@ -22,7 +22,7 @@ namespace Components
Data.insert_or_assign(key, value);
});
Script::AddFunction("StorageRemove", [] // gsc: StorageRemove(<str key>);
Script::AddFunction("StorageRemove", [] // gsc: iw4x_StorageRemove(<str key>);
{
const auto* key = Game::Scr_GetString(0);
@ -41,7 +41,7 @@ namespace Components
Data.erase(key);
});
Script::AddFunction("StorageGet", [] // gsc: StorageGet(<str key>);
Script::AddFunction("StorageGet", [] // gsc: iw4x_StorageGet(<str key>);
{
const auto* key = Game::Scr_GetString(0);
@ -61,7 +61,7 @@ namespace Components
Game::Scr_AddString(data.data());
});
Script::AddFunction("StorageHas", [] // gsc: StorageHas(<str key>);
Script::AddFunction("StorageHas", [] // gsc: iw4x_StorageHas(<str key>);
{
const auto* key = Game::Scr_GetString(0);
@ -74,7 +74,7 @@ namespace Components
Game::Scr_AddBool(Data.contains(key));
});
Script::AddFunction("StorageDump", [] // gsc: StorageDump();
Script::AddFunction("StorageDump", [] // gsc: iw4x_StorageDump();
{
if (Data.empty())
{
@ -87,7 +87,7 @@ namespace Components
FileSystem::FileWriter("scriptdata/scriptstorage.json").write(json.dump());
});
Script::AddFunction("StorageClear", [] // gsc: StorageClear();
Script::AddFunction("StorageClear", [] // gsc: iw4x_StorageClear();
{
Data.clear();
});

View File

@ -44,7 +44,7 @@ namespace Components
void UserInfo::AddScriptMethods()
{
Script::AddMethod("SetName", [](Game::scr_entref_t entref) // gsc: self SetName(<string>)
Script::AddMethod("SetName", [](Game::scr_entref_t entref) // gsc: self iw4x_SetName(<string>)
{
const auto* ent = Game::GetPlayerEntity(entref);
const auto* name = Game::Scr_GetString(0);
@ -59,7 +59,7 @@ namespace Components
Game::ClientUserinfoChanged(ent->s.number);
});
Script::AddMethod("ResetName", [](Game::scr_entref_t entref) // gsc: self ResetName()
Script::AddMethod("ResetName", [](Game::scr_entref_t entref) // gsc: self iw4x_ResetName()
{
const auto* ent = Game::GetPlayerEntity(entref);
@ -68,7 +68,7 @@ namespace Components
Game::ClientUserinfoChanged(ent->s.number);
});
Script::AddMethod("SetClanTag", [](Game::scr_entref_t entref) // gsc: self setClanTag(<string>)
Script::AddMethod("SetClanTag", [](Game::scr_entref_t entref) // gsc: self iw4x_setClanTag(<string>)
{
const auto* ent = Game::GetPlayerEntity(entref);
const auto* clanName = Game::Scr_GetString(0);
@ -83,7 +83,7 @@ namespace Components
Game::ClientUserinfoChanged(ent->s.number);
});
Script::AddMethod("ResetClanTag", [](Game::scr_entref_t entref) // gsc: self ResetClanTag()
Script::AddMethod("ResetClanTag", [](Game::scr_entref_t entref) // gsc: self iw4x_ResetClanTag()
{
const auto* ent = Game::GetPlayerEntity(entref);