Skip to content

Commit

Permalink
Fixed crashes which were inroduced in R19
Browse files Browse the repository at this point in the history
- Currently OnServerQueryInfo & GetNPCCommandLine are disabled for
testing purpose
  • Loading branch information
kurta999 committed Mar 8, 2017
1 parent e7e6b53 commit 1a2ada8
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 50 deletions.
70 changes: 41 additions & 29 deletions src/CPlayerData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,14 @@ CPlayerData::CPlayerData( WORD playerid )
{
pNetGame->pPlayerPool->pPlayer[playerid]->dwNickNameColor = dwPlayerColors[playerid % 100];
}

/*
// Store NPC Process ID if it's an NPC
if (pNetGame->pPlayerPool->bIsNPC[playerid])
{
if (CSAMPFunctions::GetPlayerIDFromIndex(playerid).binaryAddress == 0x0100007F)
iNPCProcessID = CServer::Get()->FindNPCProcessID(playerid);
}
*/
}

CPlayerData::~CPlayerData( void )
Expand Down Expand Up @@ -258,17 +259,25 @@ bool CPlayerData::DestroyObject(WORD objectid)
// Return a pointer from map if exists or add it if isn't - to save memory
CPlayerObjectAttachAddon* CPlayerData::GetObjectAddon(WORD objectid)
{
CPlayerObjectAttachAddon* pAddon;
auto it = m_PlayerObjectsAddon.find(static_cast<WORD>(objectid));
if (it == m_PlayerObjectsAddon.end())
CPlayerObjectAttachAddon* pAddon = NULL;

try
{
pAddon = new CPlayerObjectAttachAddon();
m_PlayerObjectsAddon.emplace(objectid, pAddon);
auto it = m_PlayerObjectsAddon.find(static_cast<WORD>(objectid));
if (it == m_PlayerObjectsAddon.end())
{
pAddon = new CPlayerObjectAttachAddon();
m_PlayerObjectsAddon.emplace(objectid, pAddon);
}
else
{
pAddon = it->second;
}
}
else
catch (...)
{
pAddon = it->second;
}
//logprintf("CPlayerData::GetObjectAddon(WORD objectid) catch");
}
return pAddon;
}

Expand All @@ -287,8 +296,8 @@ void CPlayerData::DeleteObjectAddon(WORD objectid)
if (it != m_PlayerObjectsAddon.end())
{
SAFE_DELETE(it->second);
m_PlayerObjectsAttachQueue.erase(it->first);
m_PlayerObjectsAddon.erase(it);
m_PlayerObjectsAttachQueue.erase(it->first);
}
}

Expand Down Expand Up @@ -320,27 +329,30 @@ void CPlayerData::Process(void)
for (std::set<WORD>::iterator o = m_PlayerObjectsAttachQueue.begin(); o != m_PlayerObjectsAttachQueue.end(); )
{
auto it = m_PlayerObjectsAddon.find(*(o));
if (it != m_PlayerObjectsAddon.end() && it->second->bCreated)
if (it != m_PlayerObjectsAddon.end() )
{
default_clock::duration passed_time = default_clock::now() - it->second->creation_timepoint;
//logprintf("time passed: %d", std::chrono::duration_cast<std::chrono::milliseconds>(passed_time).count());
if (std::chrono::duration_cast<std::chrono::milliseconds>(passed_time).count() > CServer::Get()->m_iAttachObjectDelay)
{
RakNet::BitStream bs;
bs.Write((WORD)it->first); // wObjectID
bs.Write((WORD)it->second->wAttachPlayerID); // wAttachPlayerID
bs.Write((char*)&it->second->vecOffset, sizeof(CVector));
bs.Write((char*)&it->second->vecRot, sizeof(CVector));
CSAMPFunctions::RPC(&RPC_AttachObject, &bs, LOW_PRIORITY, RELIABLE_ORDERED, 0, CSAMPFunctions::GetPlayerIDFromIndex(wPlayerID), 0, 0);

it->second->bAttached = true;
o = m_PlayerObjectsAttachQueue.erase(o);

//logprintf("attached and removed: %d", it->first);
}
else
if(it->second && it->second->bCreated)
{
++o;
default_clock::duration passed_time = default_clock::now() - it->second->creation_timepoint;
//logprintf("time passed: %d", std::chrono::duration_cast<std::chrono::milliseconds>(passed_time).count());
if (std::chrono::duration_cast<std::chrono::milliseconds>(passed_time).count() > CServer::Get()->m_iAttachObjectDelay)
{
RakNet::BitStream bs;
bs.Write((WORD)it->first); // wObjectID
bs.Write((WORD)it->second->wAttachPlayerID); // wAttachPlayerID
bs.Write((char*)&it->second->vecOffset, sizeof(CVector));
bs.Write((char*)&it->second->vecRot, sizeof(CVector));
CSAMPFunctions::RPC(&RPC_AttachObject, &bs, LOW_PRIORITY, RELIABLE_ORDERED, 0, CSAMPFunctions::GetPlayerIDFromIndex(wPlayerID), 0, 0);

it->second->bAttached = true;
o = m_PlayerObjectsAttachQueue.erase(o);

//logprintf("attached and removed: %d", it->first);
}
else
{
++o;
}
}
}
else
Expand Down
12 changes: 7 additions & 5 deletions src/CServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void CServer::Initialize(SAMPVersion version)
m_iAttachObjectDelay = Utility::CFGLoad("AttachObjectDelay");
m_bStorePlayerObjectsMaterial = static_cast<int>(Utility::CFGLoad("StorePlayerObjectsMaterial") != 0);

logprintf("%d, %d, %d, %d", m_bPickupProtection, m_bDeathProtection, m_iRakNetInternalSleepTime, m_iAttachObjectDelay);
//logprintf("%d, %d, %d, %d", m_bPickupProtection, m_bDeathProtection, m_iRakNetInternalSleepTime, m_iAttachObjectDelay);
#ifndef _WIN32
LoadTickCount();
#endif
Expand Down Expand Up @@ -102,7 +102,7 @@ void CServer::Initialize(SAMPVersion version)
m_RCONCommands.push_back(std::string(cmds->szName));
cmds++;
} while (cmds->szName[0] && !cmds->dwFlags);
logprintf("cussess");
//logprintf("cussess");
}

CServer::~CServer()
Expand Down Expand Up @@ -745,15 +745,17 @@ bool CServer::RebuildRPCData(BYTE uniqueID, RakNet::BitStream *bsSync, WORD play
{
case RPC_ScmEvent:
{
bsSync->ResetReadPointer();
const int read_offset = bsSync->GetReadOffset();

WORD issuerid;
bsSync->Read<WORD>(issuerid);
int data[4];
bsSync->Read<WORD>(issuerid);
bsSync->Read<int>(data[0]);
bsSync->Read<int>(data[1]);
bsSync->Read<int>(data[2]);
bsSync->Read<int>(data[3]);

bsSync->SetReadOffset(read_offset);

if (!CCallbackManager::OnOutcomeScmEvent(playerid, issuerid, data[0], data[1], data[2], data[3]))
return 0;

Expand Down
7 changes: 3 additions & 4 deletions src/Hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ int AMXAPI HOOK_amx_Register(AMX *amx, AMX_NATIVE_INFO *nativelist, int number)
if (!pSetPlayerWeather && !strcmp(nativelist[i].name, "SetPlayerWeather"))
pSetPlayerWeather = nativelist[i].func;

if(!pDestroyPlayerObject && !strcmp(nativelist[i].name, "DestroyObject"))
if(!pDestroyObject && !strcmp(nativelist[i].name, "DestroyObject"))
pDestroyObject = nativelist[i].func;

if(!pDestroyPlayerObject && !strcmp(nativelist[i].name, "DestroyPlayerObject"))
Expand Down Expand Up @@ -541,15 +541,15 @@ int HOOK_ProcessQueryPacket(unsigned int binaryAddress, unsigned short port, cha

memcpy(szMapName, temp, dwMapNameLen);
szMapName[dwMapNameLen] = 0;

/*
bool stringsChanged = CCallbackManager::OnServerQueryInfo(binaryAddress, szHostname, szGameMode, szMapName);
if (stringsChanged)
{
dwHostnameLen = strlen(szHostname);
dwGameModeLen = strlen(szGameMode);
dwMapNameLen = strlen(szMapName);
}

*/
WORD wPlayerCount = CServer::Get()->GetPlayerCount();
WORD wMaxPlayers = CServer::Get()->GetMaxPlayers();
BYTE byteIsPassworded = CSAMPFunctions::GetStringVariable("password")[0] != 0;
Expand Down Expand Up @@ -767,7 +767,6 @@ int HOOK_ProcessQueryPacket(unsigned int binaryAddress, unsigned short port, cha

if (pConsole)
{
//logprintf("onremoterconpacket %d, %d, %s - %s", binaryAddress, port, szPassword, szCommand);
if (CCallbackManager::OnRemoteRCONPacket(binaryAddress, port, szPassword, true, szCommand))
{
bRconSocketReply = true;
Expand Down
17 changes: 10 additions & 7 deletions src/Natives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1483,18 +1483,21 @@ AMX_DECLARE_NATIVE(Natives::YSF_DestroyPlayerObject)

if(objectid < 0 || objectid > MAX_OBJECTS) return 0;
if(!pNetGame->pObjectPool->bPlayerObjectSlotState[playerid][objectid]) return 0;

CObject *pObject = pNetGame->pObjectPool->pPlayerObjects[playerid][objectid];

auto it = pPlayerData[playerid]->m_PlayerObjectMaterialText.find(objectid);
if (it != pPlayerData[playerid]->m_PlayerObjectMaterialText.end())
if (IsPlayerConnected(playerid))
{
pPlayerData[playerid]->m_PlayerObjectMaterialText.erase(it);
auto it = pPlayerData[playerid]->m_PlayerObjectMaterialText.find(objectid);
if (it != pPlayerData[playerid]->m_PlayerObjectMaterialText.end())
{
pPlayerData[playerid]->m_PlayerObjectMaterialText.erase(it);
}

pPlayerData[playerid]->DeleteObjectAddon(static_cast<WORD>(objectid));
}

if(pDestroyPlayerObject(amx, params) && IsPlayerConnected(playerid))

if(pDestroyPlayerObject(amx, params))
{
pPlayerData[playerid]->DeleteObjectAddon(static_cast<WORD>(objectid));
return 1;
}
return 0;
Expand Down
12 changes: 7 additions & 5 deletions src/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,14 @@ bool IsPlayerConnected(int playerid)
return pPlayerData[playerid] != NULL && pNetGame->pPlayerPool->pPlayer != NULL;
}

std::vector<std::string> &Utility::split(const std::string &s, char delim, std::vector<std::string> &elems) {
std::vector<std::string> &Utility::split(const std::string &s, char delim, std::vector<std::string> &elems)
{
std::stringstream ss(s);
std::string item;
while (std::getline(ss, item, delim)) {
if (item.length() > 0) {
while (std::getline(ss, item, delim))
{
if (item.length() > 0)
{
elems.push_back(item);
}
}
Expand Down Expand Up @@ -283,7 +286,6 @@ BYTE Utility::GetWeaponSlot(BYTE weaponid)
// Load an entry from server.cfg - Y_Less
int Utility::CFGLoad(char const * const name, char * const dest, size_t dlen)
{
//logprintf("1");
FILE * fileConfig = fopen("plugins/YSF.cfg", "r");
//logprintf("2");

Expand Down Expand Up @@ -311,7 +313,7 @@ int Utility::CFGLoad(char const * const name, char * const dest, size_t dlen)
// first character fails.
if (!strncmp(line, name, len) && line[len] <= ' ')
{
logprintf("7");
//logprintf("7");

while (line[++len] <= ' ')
{
Expand Down

0 comments on commit 1a2ada8

Please sign in to comment.