Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save server row index instead of finding it #1716

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 32 additions & 14 deletions Client/core/ServerBrowser/CServerBrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,8 @@ void CServerBrowser::UpdateServerList(ServerBrowserType Type, bool bClearServerL
// Get the appropriate server list
CServerList* pList = GetServerList(Type);

if (pList->GetRevision() != m_pServerListRevision[Type] || bClearServerList)
bool bGetListsCleared = pList->GetRevision() != m_pServerListRevision[Type] || bClearServerList;
if (bGetListsCleared)
{
m_pServerListRevision[Type] = pList->GetRevision();

Expand All @@ -832,18 +833,29 @@ void CServerBrowser::UpdateServerList(ServerBrowserType Type, bool bClearServerL
m_pServerPlayerList[Type]->Clear();
}

bool didUpdateRowIndices = false;

// Loop the server list
for (CServerListIterator it = pList->IteratorBegin(); it != pList->IteratorEnd(); it++)
{
CServerListItem* pServer = *it;

if (bGetListsCleared)
pServer->iRowIndex = -1;

// Find info from server cache for favourites and recent
if (Type == ServerBrowserType::FAVOURITES || Type == ServerBrowserType::RECENTLY_PLAYED)
GetServerCache()->GetServerCachedInfo(pServer);

// Add/update/remove the item to the list
if (pServer->revisionInList[Type] != pServer->uiRevision || bClearServerList)
{
if (!didUpdateRowIndices)
{
UpdateRowIndexMembers(Type);
didUpdateRowIndices = true;
}

pServer->revisionInList[Type] = pServer->uiRevision;
AddServerToList(pServer, Type);
}
Expand Down Expand Up @@ -950,7 +962,7 @@ void CServerBrowser::UpdateHistoryList()
}
}

void CServerBrowser::AddServerToList(const CServerListItem* pServer, const ServerBrowserType Type)
void CServerBrowser::AddServerToList(CServerListItem* pServer, const ServerBrowserType Type)
{
bool bIncludeEmpty = m_pIncludeEmpty[Type]->GetSelected();
bool bIncludeFull = m_pIncludeFull[Type]->GetSelected();
Expand Down Expand Up @@ -1023,11 +1035,12 @@ void CServerBrowser::AddServerToList(const CServerListItem* pServer, const Serve
//
// Remove server from list
//

int iIndex = FindRowFromServer(Type, pServer);
int iIndex = pServer->iRowIndex;
if (iIndex != -1)
{
m_pServerList[Type]->RemoveRow(iIndex);
pServer->iRowIndex = -1;
UpdateRowIndexMembers(Type);
}
}
else
Expand All @@ -1037,9 +1050,12 @@ void CServerBrowser::AddServerToList(const CServerListItem* pServer, const Serve
//

// Get existing row or create a new row if not found
int iIndex = FindRowFromServer(Type, pServer);
int iIndex = pServer->iRowIndex;
if (iIndex == -1)
{
iIndex = m_pServerList[Type]->AddRow(true);
pServer->iRowIndex = iIndex;
}

const SString strVersion = !bIncludeOtherVersions ? "" : pServer->strVersion;
const SString strVersionSortKey = pServer->strVersionSortKey + pServer->strTieBreakSortKey;
Expand Down Expand Up @@ -1087,6 +1103,10 @@ void CServerBrowser::AddServerToList(const CServerListItem* pServer, const Serve
m_pServerList[Type]->SetItemColor(iIndex, m_hPlayers[Type], color.R, color.G, color.B, color.A);
m_pServerList[Type]->SetItemColor(iIndex, m_hPing[Type], color.R, color.G, color.B, color.A);
m_pServerList[Type]->SetItemColor(iIndex, m_hGame[Type], color.R, color.G, color.B, color.A);

// If the index was modified from the original, then update all indexes because it means there was some sort
if (pServer->iRowIndex != iIndex)
UpdateRowIndexMembers(Type);
}
}

Expand Down Expand Up @@ -2073,23 +2093,21 @@ unsigned short CServerBrowser::FindServerHttpPort(const std::string& strHost, un

/////////////////////////////////////////////////////////////////
//
// CServerBrowser::FindRowFromServer
//
// CServerBrowser::UpdateRowIndexMembers
//
// Update row index property of each CServerListItem on the server list
//
/////////////////////////////////////////////////////////////////
int CServerBrowser::FindRowFromServer(ServerBrowserType Type, const CServerListItem* pServer)
void CServerBrowser::UpdateRowIndexMembers(ServerBrowserType Type)
{
CGUIGridList* pServerList = m_pServerList[Type];
int iRowCount = pServerList->GetRowCount();
for (int i = 0; i < iRowCount; i++)

for (int iRowIndex = 0; iRowIndex < iRowCount; iRowIndex++)
{
if (pServer == (CServerListItem*)pServerList->GetItemData(i, DATA_PSERVER))
{
return i;
}
CServerListItem* pServer = (CServerListItem*)pServerList->GetItemData(iRowIndex, DATA_PSERVER);
pServer->iRowIndex = iRowIndex;
}
return -1;
}

/////////////////////////////////////////////////////////////////
Expand Down
4 changes: 2 additions & 2 deletions Client/core/ServerBrowser/CServerBrowser.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class CServerBrowser : public CSingleton<CServerBrowser>
CServerListItem* FindServerFromRow(ServerBrowserType Type, int iRow);
CServerListItem* FindServer(const std::string& strHost, unsigned short usPort);
unsigned short FindServerHttpPort(const std::string& strHost, unsigned short usPort);
int FindRowFromServer(ServerBrowserType Type, const CServerListItem* pServer);
void UpdateRowIndexMembers(ServerBrowserType Type);
void UpdateSelectedServerPlayerList(ServerBrowserType Type);
void GetVisibleEndPointList(std::vector<SAddressPort>& outEndpointList);

Expand Down Expand Up @@ -207,7 +207,7 @@ class CServerBrowser : public CSingleton<CServerBrowser>
void UpdateServerList(ServerBrowserType Type, bool bClearServerList = false);
void UpdateHistoryList();
CServerList* GetServerList(ServerBrowserType Type);
void AddServerToList(const CServerListItem* pServer, const ServerBrowserType Type);
void AddServerToList(CServerListItem* pServer, const ServerBrowserType Type);
bool RemoveSelectedServerFromRecentlyPlayedList();

bool OnClick(CGUIElement* pElement);
Expand Down
3 changes: 3 additions & 0 deletions Client/core/ServerBrowser/CServerList.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ class CServerListItem
bSerials = false;
bPassworded = false;
bKeepFlag = false;
iRowIndex = -1;
ecastro98 marked this conversation as resolved.
Show resolved Hide resolved

nPlayers = 0;
nMaxPlayers = 0;
nPing = 9999;
Expand Down Expand Up @@ -192,6 +194,7 @@ class CServerListItem
uint uiQueryRetryCount;
uint uiRevision;
bool bKeepFlag;
int iRowIndex;

SString strGameName; // Game name. Always 'mta'
SString strVersion; // Game version
Expand Down