Skip to content

Commit

Permalink
Update redirectPlayer args handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Lpsd committed Nov 18, 2023
1 parent c96a8ff commit ea878ef
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 16 deletions.
46 changes: 41 additions & 5 deletions Client/mods/deathmatch/logic/lua/CLuaFunctionParseHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ IMPLEMENT_ENUM_BEGIN(ERenderFormat)
ADD_ENUM(RFORMAT_UNKNOWN, "unknown")
ADD_ENUM(RFORMAT_ARGB, "argb")
ADD_ENUM(RFORMAT_XRGB, "xrgb")
ADD_ENUM(RFORMAT_RGB, "rgb")
ADD_ENUM(RFORMAT_RGB, "rgb")
ADD_ENUM(RFORMAT_DXT1, "dxt1")
ADD_ENUM(RFORMAT_DXT2, "dxt2")
ADD_ENUM(RFORMAT_DXT3, "dxt3")
Expand Down Expand Up @@ -456,11 +456,11 @@ IMPLEMENT_ENUM_BEGIN(eFontQuality)
ADD_ENUM(FONT_QUALITY_DEFAULT, "default")
ADD_ENUM(FONT_QUALITY_DRAFT, "draft")
ADD_ENUM(FONT_QUALITY_PROOF, "proof")
#if( WINVER >= 0x0400 )
#if (WINVER >= 0x0400)
ADD_ENUM(FONT_QUALITY_NONANTIALIASED, "nonantialiased")
ADD_ENUM(FONT_QUALITY_ANTIALIASED, "antialiased")
#endif
#if( _WIN32_WINNT >= _WIN32_WINNT_WINXP )
#if (_WIN32_WINNT >= _WIN32_WINNT_WINXP)
ADD_ENUM(FONT_QUALITY_CLEARTYPE, "cleartype")
ADD_ENUM(FONT_QUALITY_CLEARTYPE_NATURAL, "cleartype_natural")
#endif
Expand Down Expand Up @@ -1082,10 +1082,10 @@ bool MinClientReqCheck(CScriptArgReader& argStream, const char* szVersionReq, co
{
if (pResource->GetMinClientReq() < szVersionReq)
{
#if MTASA_VERSION_TYPE == VERSION_TYPE_RELEASE
#if MTASA_VERSION_TYPE == VERSION_TYPE_RELEASE
if (szReason)
argStream.SetVersionWarning(szVersionReq, "client", szReason);
#endif
#endif
return false;
}
}
Expand Down Expand Up @@ -1187,3 +1187,39 @@ void CheckCanAccessOtherResourceFile(CScriptArgReader& argStream, CResource* pTh
{
// No operation on the client
}

std::unordered_map<std::string, std::string> ArgMapToStringMap(const std::unordered_map<CLuaArgument, CLuaArgument, CLuaArgument::Hash>& argMap)
{
std::unordered_map<std::string, std::string> stringMap;

for (auto& [k, value] : argMap)
{
CLuaArgument key = k; // make a copy due to GetAsString not working with const

SString keystr, valuestr;
if (!key.GetAsString(keystr) || !value.GetAsString(valuestr))
continue;

stringMap.emplace(keystr, valuestr);
}

return stringMap;
}

std::string StringMapToArgString(const std::unordered_map<std::string, std::string>& strMap)
{
std::string path;

for (auto& [k, v] : strMap)
{
std::string key = k;
std::string value = v;

key.erase(std::remove(key.begin(), key.end(), '/'), key.end());
value.erase(std::remove(value.begin(), value.end(), '/'), value.end());

path += (path.empty() ? std::string{} : "/") + key + "/" + value;
}

return path;
}
4 changes: 4 additions & 0 deletions Client/mods/deathmatch/logic/lua/CLuaFunctionParseHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -586,3 +586,7 @@ void CheckCanAccessOtherResourceFile(CScriptArgReader& argStream, CResource* pTh
//
bool IsWeaponPropertyFlag(eWeaponProperty weaponProperty);
uint GetWeaponPropertyFlagBit(eWeaponProperty weaponProperty);


std::unordered_map<std::string, std::string> ArgMapToStringMap(const std::unordered_map<CLuaArgument, CLuaArgument, CLuaArgument::Hash>& argMap);
std::string StringMapToArgString(const std::unordered_map<std::string, std::string>& strMap);
40 changes: 38 additions & 2 deletions Server/mods/deathmatch/logic/lua/CLuaFunctionParseHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -586,9 +586,9 @@ void MinServerReqCheck(CScriptArgReader& argStream, const char* szVersionReq, co
{
if (pResource->GetMinServerRequirement() < szVersionReq)
{
#if MTASA_VERSION_TYPE == VERSION_TYPE_RELEASE
#if MTASA_VERSION_TYPE == VERSION_TYPE_RELEASE
argStream.SetVersionWarning(szVersionReq, "server", szReason);
#endif
#endif
}
}
}
Expand Down Expand Up @@ -813,3 +813,39 @@ void CheckCanAccessOtherResourceFile(CScriptArgReader& argStream, CResource* pTh
SString("Database credentials protection denied resource %s to access %s", *pThisResource->GetName(), *pOtherResource->GetName()), "Access denied");
}
}

std::unordered_map<std::string, std::string> ArgMapToStringMap(const std::unordered_map<CLuaArgument, CLuaArgument, CLuaArgument::Hash>& argMap)
{
std::unordered_map<std::string, std::string> stringMap;

for (auto& [k, value] : argMap)
{
CLuaArgument key = k; // make a copy due to GetAsString not working with const

SString keystr, valuestr;
if (!key.GetAsString(keystr) || !value.GetAsString(valuestr))
continue;

stringMap.emplace(keystr, valuestr);
}

return stringMap;
}

std::string StringMapToArgString(const std::unordered_map<std::string, std::string>& strMap)
{
std::string path;

for (auto& [k, v] : strMap)
{
std::string key = k;
std::string value = v;

key.erase(std::remove(key.begin(), key.end(), '/'), key.end());
value.erase(std::remove(value.begin(), value.end(), '/'), value.end());

path += (path.empty() ? std::string{} : "/") + key + "/" + value;
}

return path;
}
3 changes: 3 additions & 0 deletions Server/mods/deathmatch/logic/lua/CLuaFunctionParseHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -412,3 +412,6 @@ void CheckCanAccessOtherResourceFile(CScriptArgReader& argStream, CResource* pTh
//
bool IsWeaponPropertyFlag(eWeaponProperty weaponProperty);
uint GetWeaponPropertyFlagBit(eWeaponProperty weaponProperty);

std::unordered_map<std::string, std::string> ArgMapToStringMap(const std::unordered_map<CLuaArgument, CLuaArgument, CLuaArgument::Hash>& argMap);
std::string StringMapToArgString(const std::unordered_map<std::string, std::string>& strMap);
12 changes: 4 additions & 8 deletions Server/mods/deathmatch/logic/luadefs/CLuaPlayerDefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "CScriptArgReader.h"
#include "CKeyBinds.h"
#include <numeric>
#include <algorithm>

void CLuaPlayerDefs::LoadFunctions()
{
Expand Down Expand Up @@ -1164,19 +1165,14 @@ int CLuaPlayerDefs::SetPlayerBlurLevel(lua_State* luaVM)
}

bool CLuaPlayerDefs::RedirectPlayer(CPlayer* pElement, std::string strHost, unsigned short usPort, std::optional<std::string> strPassword,
std::optional<std::unordered_map<std::string, std::string>> mArgs)
std::optional<std::unordered_map<CLuaArgument, CLuaArgument, CLuaArgument::Hash>> mArgs)
{
std::string strDetails;

if (mArgs.has_value())
{
auto args = mArgs.value();

// Convert table of strings ({["a"] = "1", ["b"] = "2"}) to string ("a/1/b/2")
const std::string delimiter = "/";
strDetails = std::accumulate(args.begin(), args.end(), std::string(),
[delimiter](const std::string& s, const std::pair<const std::string, std::string>& p)
{ return s + (s.empty() ? std::string() : delimiter) + p.first + delimiter + p.second; });
auto& strArgs = ArgMapToStringMap(mArgs.value());
strDetails = StringMapToArgString(strArgs);
}

if (strDetails.length() > MAX_REDIRECT_DETAILS_LENGTH)
Expand Down
2 changes: 1 addition & 1 deletion Server/mods/deathmatch/logic/luadefs/CLuaPlayerDefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,5 @@ class CLuaPlayerDefs : public CLuaDefs
LUA_DECLARE(BanPlayer);

static bool RedirectPlayer(CPlayer* pElement, std::string strHost, unsigned short usPort, std::optional<std::string> strPassword,
std::optional<std::unordered_map<std::string, std::string>> mArgs);
std::optional<std::unordered_map<CLuaArgument, CLuaArgument, CLuaArgument::Hash>> mArgs);
};

0 comments on commit ea878ef

Please sign in to comment.