Skip to content

Commit

Permalink
Add similar functionality to redirectPlayer via argument
Browse files Browse the repository at this point in the history
  • Loading branch information
Lpsd committed Nov 16, 2023
1 parent 5b3849a commit ed7f1cb
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 41 deletions.
4 changes: 4 additions & 0 deletions Client/mods/deathmatch/logic/CClientGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,11 @@ bool CClientGame::StartGame(const char* szNick, const char* szPassword, eServerT

// Send the packet as joindata
if (g_pNet->CanServerBitStream(eBitStreamVersion::CPlayerJoinDataPacket_ProtocolConnectArgs))
{
pBitStream->Write(g_pCore->GetProtocolConnectArgs().c_str(), MAX_PROTOCOL_CONNECT_ARGS_LENGTH);
g_pCore->SetProtocolConnectArgs("");
}


g_pNet->SendPacket(PACKET_ID_PLAYER_JOINDATA, pBitStream, PACKET_PRIORITY_HIGH, PACKET_RELIABILITY_RELIABLE_ORDERED);
g_pNet->DeallocateNetBitStream(pBitStream);
Expand Down
16 changes: 15 additions & 1 deletion Client/mods/deathmatch/logic/rpc/CInputRPCs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ void CInputRPCs::SetControlState(NetBitStreamInterface& bitStream)

void CInputRPCs::ForceReconnect(NetBitStreamInterface& bitStream)
{
unsigned char ucHost, ucPassword;
unsigned char ucHost, ucPassword, ucArgs;
unsigned short usPort;

if (bitStream.Read(ucHost))
Expand All @@ -240,6 +240,20 @@ void CInputRPCs::ForceReconnect(NetBitStreamInterface& bitStream)

if (bitStream.Read(usPort))
{
char* szArgs = new char[MAX_PROTOCOL_CONNECT_ARGS_LENGTH + 1];

if (g_pNet->CanServerBitStream(eBitStreamVersion::CPlayerJoinDataPacket_ProtocolConnectArgs))
{
if (bitStream.Read(ucArgs))
{
szArgs[ucArgs] = NULL;
bitStream.Read(szArgs, ucArgs);

if (std::strlen(szArgs) > 0)
g_pCore->SetProtocolConnectArgs(szArgs);
}
}

if (bitStream.Read(ucPassword))
{
char* szPassword = new char[ucPassword + 1];
Expand Down
5 changes: 4 additions & 1 deletion Server/mods/deathmatch/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@
#define MAX_BAN_RESPONSIBLE_LENGTH 30

// Max args length when connecting via mtasa:// protocol
#define MAX_PROTOCOL_CONNECT_ARGS_LENGTH 1024
#define MAX_CONNECT_ARGS_LENGTH 1024

// Max length of details via redirectPlayer
#define MAX_REDIRECT_DETAILS_LENGTH 1024

// Couple of defines to ensure proper configuration
#if MAX_CHAT_LENGTH > 255
Expand Down
8 changes: 6 additions & 2 deletions Server/mods/deathmatch/logic/CGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1735,7 +1735,10 @@ void CGame::Packet_PlayerJoinData(CPlayerJoinDataPacket& Packet)
pPlayer->SetBitStreamVersion(Packet.GetBitStreamVersion());
g_pNetServer->SetClientBitStreamVersion(Packet.GetSourceSocket(), Packet.GetBitStreamVersion());

pPlayer->SetProtocolConnectArgs(std::move(Packet.GetProtocolConnectArgs()));
const char* connectArgs = Packet.GetConnectArgs();

if (std::strlen(connectArgs) > 0)
pPlayer->SetConnectArgs(std::move(Packet.GetConnectArgs()));

// Get the serial number from the packet source
NetServerPlayerID p = Packet.GetSourceSocket();
Expand Down Expand Up @@ -4252,7 +4255,7 @@ void CGame::PlayerCompleteConnect(CPlayer* pPlayer)
Arguments.PushNumber(pPlayer->GetMTAVersion());
Arguments.PushString(pPlayer->GetPlayerVersion());

auto vecArgs = SharedUtil::Explode(pPlayer->GetProtocolConnectArgs(), '/');
auto vecArgs = SharedUtil::Explode(pPlayer->GetConnectArgs(), '/');

if (vecArgs.size() > 1)
{
Expand All @@ -4264,6 +4267,7 @@ void CGame::PlayerCompleteConnect(CPlayer* pPlayer)
}

Arguments.PushTable(&ProtocolConnectArgs);
pPlayer->SetConnectArgs("");

if (!g_pGame->GetMapManager()->GetRootElement()->CallEvent("onPlayerConnect", Arguments))
{
Expand Down
8 changes: 4 additions & 4 deletions Server/mods/deathmatch/logic/CPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ enum eVoiceState
VOICESTATE_TRANSMITTING_IGNORED,
};

#define MOVEMENT_UPDATE_THRESH (5)
#define MOVEMENT_UPDATE_THRESH (5)
#define DISTANCE_FOR_NEAR_VIEWER (310)

struct SViewerInfo
Expand Down Expand Up @@ -335,8 +335,8 @@ class CPlayer final : public CPed, public CClient
const SString& GetQuitReasonForLog() { return m_strQuitReasonForLog; }
void SetQuitReasonForLog(const SString& strReason) { m_strQuitReasonForLog = strReason; }

void SetProtocolConnectArgs(const SString&& args) { m_strProtocolConnectArgs = args; }
const SString& GetProtocolConnectArgs() const { return m_strProtocolConnectArgs; }
void SetConnectArgs(const SString&& args) { m_strConnectArgs = args; }
const SString& GetConnectArgs() const { return m_strConnectArgs; }

CFastHashSet<CPlayer*> m_PureSyncSimSendList;
bool m_bPureSyncSimSendListDirty;
Expand Down Expand Up @@ -468,5 +468,5 @@ class CPlayer final : public CPed, public CClient

ushort m_usPrevDimension;
SString m_strQuitReasonForLog;
SString m_strProtocolConnectArgs;
SString m_strConnectArgs;
};
11 changes: 10 additions & 1 deletion Server/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3446,7 +3446,7 @@ bool CStaticFunctionDefinitions::SetPlayerBlurLevel(CElement* pElement, unsigned
return false;
}

bool CStaticFunctionDefinitions::RedirectPlayer(CElement* pElement, const char* szHost, unsigned short usPort, const char* szPassword)
bool CStaticFunctionDefinitions::RedirectPlayer(CElement* pElement, const char* szHost, unsigned short usPort, const char* szPassword, const char* szArgs)
{
if (IS_PLAYER(pElement))
{
Expand All @@ -3457,10 +3457,19 @@ bool CStaticFunctionDefinitions::RedirectPlayer(CElement* pElement, const char*

unsigned char ucHostLength = static_cast<unsigned char>(strlen(szHost));

if (strlen(szPassword) == 0)
szPassword = nullptr;

CBitStream BitStream;
BitStream.pBitStream->Write(ucHostLength);
BitStream.pBitStream->Write(szHost, ucHostLength);
BitStream.pBitStream->Write(usPort);
if (pPlayer->CanBitStream(eBitStreamVersion::CPlayerJoinDataPacket_ProtocolConnectArgs))
{
unsigned char ucArgsLength = static_cast<unsigned char>(strlen(szArgs));
BitStream.pBitStream->Write(ucArgsLength);
BitStream.pBitStream->Write(szArgs, ucArgsLength);
}
if (szPassword)
{
unsigned char ucPasswordLength = static_cast<unsigned char>(strlen(szPassword));
Expand Down
2 changes: 1 addition & 1 deletion Server/mods/deathmatch/logic/CStaticFunctionDefinitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class CStaticFunctionDefinitions
unsigned short usDimension, CTeam* pTeam = NULL);
static bool SetPlayerMuted(CElement* pElement, bool bMuted);
static bool SetPlayerBlurLevel(CElement* pElement, unsigned char ucLevel);
static bool RedirectPlayer(CElement* pElement, const char* szHost, unsigned short usPort, const char* szPassword);
static bool RedirectPlayer(CElement* pElement, const char* szHost, unsigned short usPort, const char* szPassword, const char* szArgs = "");
static bool SetPlayerName(CElement* pElement, const char* szName);
static bool DetonateSatchels(CElement* pElement);
static bool TakePlayerScreenShot(CElement* pElement, uint uiSizeX, uint uiSizeY, const SString& strTag, uint uiQuality, uint uiMaxBandwidth,
Expand Down
43 changes: 21 additions & 22 deletions Server/mods/deathmatch/logic/luadefs/CLuaPlayerDefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "CStaticFunctionDefinitions.h"
#include "CScriptArgReader.h"
#include "CKeyBinds.h"
#include <numeric>

void CLuaPlayerDefs::LoadFunctions()
{
Expand Down Expand Up @@ -65,7 +66,7 @@ void CLuaPlayerDefs::LoadFunctions()
{"setPlayerNametagShowing", SetPlayerNametagShowing},
{"setPlayerMuted", SetPlayerMuted},
{"setPlayerBlurLevel", SetPlayerBlurLevel},
{"redirectPlayer", RedirectPlayer},
{"redirectPlayer", ArgumentParserWarn<false, RedirectPlayer>},
{"setPlayerName", SetPlayerName},
{"detonateSatchels", DetonateSatchels},
{"takePlayerScreenShot", TakePlayerScreenShot},
Expand Down Expand Up @@ -1162,32 +1163,30 @@ int CLuaPlayerDefs::SetPlayerBlurLevel(lua_State* luaVM)
return 1;
}

int CLuaPlayerDefs::RedirectPlayer(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)
{
CPlayer* pElement;
SString strHost;
unsigned short usPort;
SString strPassword;

CScriptArgReader argStream(luaVM);
argStream.ReadUserData(pElement);
argStream.ReadString(strHost);
argStream.ReadNumber(usPort);
argStream.ReadString(strPassword, "");
std::string strDetails;

if (!argStream.HasErrors())
if (mArgs.has_value())
{
if (CStaticFunctionDefinitions::RedirectPlayer(pElement, strHost, usPort, strPassword.empty() ? nullptr : *strPassword))
{
lua_pushboolean(luaVM, true);
return 1;
}
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; });
}
else
m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage());

lua_pushboolean(luaVM, false);
return 1;
if (strDetails.length() > MAX_REDIRECT_DETAILS_LENGTH)
throw std::invalid_argument("Details must be " + std::to_string(MAX_REDIRECT_DETAILS_LENGTH) + " characters or less");

if (CStaticFunctionDefinitions::RedirectPlayer(pElement, strHost.c_str(), usPort, strPassword.has_value() ? strPassword.value().c_str() : nullptr,
strDetails.c_str()))
return true;

return false;
}

int CLuaPlayerDefs::TakePlayerMoney(lua_State* luaVM)
Expand Down
4 changes: 3 additions & 1 deletion Server/mods/deathmatch/logic/luadefs/CLuaPlayerDefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ class CLuaPlayerDefs : public CLuaDefs
LUA_DECLARE(SetPlayerNametagShowing);
LUA_DECLARE(SetPlayerMuted);
LUA_DECLARE(SetPlayerBlurLevel);
LUA_DECLARE(RedirectPlayer);
LUA_DECLARE(SetPlayerName);
LUA_DECLARE(DetonateSatchels);
LUA_DECLARE(TakePlayerScreenShot);
Expand Down Expand Up @@ -101,4 +100,7 @@ class CLuaPlayerDefs : public CLuaDefs
// Admin funcs
LUA_DECLARE(KickPlayer);
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);
};
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ bool CPlayerJoinDataPacket::Read(NetBitStreamInterface& BitStream)

if (BitStream.Read(m_ucGameVersion) && BitStream.ReadStringCharacters(m_strNick, MAX_PLAYER_NICK_LENGTH) &&
BitStream.Read(reinterpret_cast<char*>(&m_Password), 16) && BitStream.ReadStringCharacters(m_strSerialUser, MAX_SERIAL_LENGTH) &&
BitStream.ReadStringCharacters(m_strProtocolConnectArgs, MAX_PROTOCOL_CONNECT_ARGS_LENGTH))
BitStream.ReadStringCharacters(m_strConnectArgs, MAX_CONNECT_ARGS_LENGTH))
{
// Shrink string sizes to fit
m_strNick = *m_strNick;
m_strSerialUser = *m_strSerialUser;
m_strProtocolConnectArgs = *m_strProtocolConnectArgs;
m_strConnectArgs = *m_strConnectArgs;

return true;
}
Expand Down
9 changes: 3 additions & 6 deletions Server/mods/deathmatch/logic/packets/CPlayerJoinDataPacket.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,8 @@ class CPlayerJoinDataPacket final : public CPacket

bool IsOptionalUpdateInfoRequired() { return m_bOptionalUpdateInfoRequired; }

const char* GetProtocolConnectArgs() { return m_strProtocolConnectArgs; };
void SetProtocolConnectArgs(const char* szProtocolConnectArgs)
{
m_strProtocolConnectArgs.AssignLeft(szProtocolConnectArgs, MAX_PROTOCOL_CONNECT_ARGS_LENGTH);
};
const char* GetConnectArgs() { return m_strConnectArgs; };
void SetConnectArgs(const char* szConnectArgs) { m_strConnectArgs.AssignLeft(szConnectArgs, MAX_CONNECT_ARGS_LENGTH); };

private:
unsigned short m_usNetVersion;
Expand All @@ -60,5 +57,5 @@ class CPlayerJoinDataPacket final : public CPacket
MD5 m_Password;
SString m_strSerialUser;
CMtaVersion m_strPlayerVersion;
SString m_strProtocolConnectArgs;
SString m_strConnectArgs;
};

0 comments on commit ed7f1cb

Please sign in to comment.