2022-06-04 04:56:14 -04:00
|
|
|
#include <STDInclude.hpp>
|
2023-01-03 07:16:44 -05:00
|
|
|
#include <Utils/InfoString.hpp>
|
|
|
|
|
2022-12-26 07:07:24 -05:00
|
|
|
#include "UserInfo.hpp"
|
|
|
|
|
2022-07-06 11:48:40 -04:00
|
|
|
#include "GSC/Script.hpp"
|
2022-06-04 04:56:14 -04:00
|
|
|
|
|
|
|
namespace Components
|
|
|
|
{
|
|
|
|
std::unordered_map<int, UserInfo::userInfoMap> UserInfo::UserInfoOverrides;
|
|
|
|
|
|
|
|
void UserInfo::SV_GetUserInfo_Stub(int index, char* buffer, int bufferSize)
|
|
|
|
{
|
|
|
|
Utils::Hook::Call<void(int, char*, int)>(0x49A160)(index, buffer, bufferSize);
|
|
|
|
|
|
|
|
Utils::InfoString map(buffer);
|
|
|
|
|
|
|
|
if (!UserInfoOverrides.contains(index))
|
|
|
|
{
|
|
|
|
UserInfoOverrides[index] = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto& [key, val] : UserInfoOverrides[index])
|
|
|
|
{
|
|
|
|
if (val.empty())
|
|
|
|
{
|
|
|
|
map.remove(key);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
map.set(key, val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto userInfo = map.build();
|
|
|
|
strncpy_s(buffer, bufferSize, userInfo.data(), _TRUNCATE);
|
|
|
|
}
|
|
|
|
|
2022-06-13 14:16:57 -04:00
|
|
|
void UserInfo::ClearClientOverrides(const int clientNum)
|
2022-06-04 04:56:14 -04:00
|
|
|
{
|
2022-06-13 14:16:57 -04:00
|
|
|
UserInfoOverrides[clientNum].clear();
|
2022-06-04 04:56:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void UserInfo::ClearAllOverrides()
|
|
|
|
{
|
|
|
|
UserInfoOverrides.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UserInfo::AddScriptMethods()
|
|
|
|
{
|
2022-07-23 17:22:58 -04:00
|
|
|
Script::AddMethod("SetName", [](Game::scr_entref_t entref) // gsc: self SetName(<string>)
|
2022-06-04 04:56:14 -04:00
|
|
|
{
|
|
|
|
const auto* ent = Game::GetPlayerEntity(entref);
|
|
|
|
const auto* name = Game::Scr_GetString(0);
|
|
|
|
|
2022-06-12 17:07:53 -04:00
|
|
|
if (name == nullptr)
|
|
|
|
{
|
|
|
|
Game::Scr_ParamError(0, "^1SetName: Illegal parameter!\n");
|
2022-07-21 12:56:16 -04:00
|
|
|
return;
|
2022-06-12 17:07:53 -04:00
|
|
|
}
|
|
|
|
|
2022-06-22 04:58:51 -04:00
|
|
|
Logger::Debug("Setting name of {} to {}", ent->s.number, name);
|
2022-06-04 04:56:14 -04:00
|
|
|
UserInfoOverrides[ent->s.number]["name"] = name;
|
|
|
|
Game::ClientUserinfoChanged(ent->s.number);
|
|
|
|
});
|
|
|
|
|
2022-07-23 17:22:58 -04:00
|
|
|
Script::AddMethod("ResetName", [](Game::scr_entref_t entref) // gsc: self ResetName()
|
2022-06-04 04:56:14 -04:00
|
|
|
{
|
|
|
|
const auto* ent = Game::GetPlayerEntity(entref);
|
|
|
|
|
2022-07-16 17:24:26 -04:00
|
|
|
Logger::Debug("Resetting name of {}", ent->s.number);
|
2022-06-04 04:56:14 -04:00
|
|
|
UserInfoOverrides[ent->s.number].erase("name");
|
|
|
|
Game::ClientUserinfoChanged(ent->s.number);
|
|
|
|
});
|
2022-07-16 17:24:26 -04:00
|
|
|
|
2022-07-23 17:22:58 -04:00
|
|
|
Script::AddMethod("SetClanTag", [](Game::scr_entref_t entref) // gsc: self SetClanTag(<string>)
|
2022-07-16 17:24:26 -04:00
|
|
|
{
|
|
|
|
const auto* ent = Game::GetPlayerEntity(entref);
|
|
|
|
const auto* clanName = Game::Scr_GetString(0);
|
|
|
|
|
|
|
|
if (clanName == nullptr)
|
|
|
|
{
|
|
|
|
Game::Scr_ParamError(0, "^1SetClanTag: Illegal parameter!\n");
|
2022-07-21 12:56:16 -04:00
|
|
|
return;
|
2022-07-16 17:24:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Logger::Debug("Setting clanName of {} to {}", ent->s.number, clanName);
|
|
|
|
UserInfoOverrides[ent->s.number]["clanAbbrev"] = clanName;
|
|
|
|
Game::ClientUserinfoChanged(ent->s.number);
|
|
|
|
});
|
|
|
|
|
2022-07-23 17:22:58 -04:00
|
|
|
Script::AddMethod("ResetClanTag", [](Game::scr_entref_t entref) // gsc: self ResetClanTag()
|
2022-07-16 17:24:26 -04:00
|
|
|
{
|
|
|
|
const auto* ent = Game::GetPlayerEntity(entref);
|
|
|
|
|
|
|
|
Logger::Debug("Resetting clanName of {}", ent->s.number);
|
|
|
|
UserInfoOverrides[ent->s.number].erase("clanAbbrev");
|
|
|
|
Game::ClientUserinfoChanged(ent->s.number);
|
|
|
|
});
|
2022-06-04 04:56:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
UserInfo::UserInfo()
|
|
|
|
{
|
|
|
|
Utils::Hook(0x445268, SV_GetUserInfo_Stub, HOOK_CALL).install()->quick();
|
|
|
|
Utils::Hook(0x478B04, SV_GetUserInfo_Stub, HOOK_CALL).install()->quick();
|
|
|
|
|
|
|
|
AddScriptMethods();
|
|
|
|
|
2022-06-13 14:16:57 -04:00
|
|
|
Events::OnVMShutdown([]
|
2022-06-04 04:56:14 -04:00
|
|
|
{
|
|
|
|
ClearAllOverrides();
|
|
|
|
});
|
2022-06-13 14:16:57 -04:00
|
|
|
|
|
|
|
Events::OnClientDisconnect([](const int clientNum)
|
|
|
|
{
|
|
|
|
// Clear the overrides for UserInfo
|
|
|
|
ClearClientOverrides(clientNum);
|
|
|
|
});
|
2022-06-04 04:56:14 -04:00
|
|
|
}
|
|
|
|
}
|