Merge pull request #234 from mjkzy/master-server

Use master server, fallback to node if not reached
This commit is contained in:
Edo 2022-05-03 20:51:21 +01:00 committed by GitHub
commit 7393f6da7f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 53 additions and 27 deletions

View File

@ -374,7 +374,6 @@ namespace Components
} }
}); });
#ifdef USE_LEGACY_SERVER_LIST
// Heartbeats // Heartbeats
Scheduler::Once(Dedicated::Heartbeat); Scheduler::Once(Dedicated::Heartbeat);
Scheduler::OnFrame([]() Scheduler::OnFrame([]()
@ -387,7 +386,6 @@ namespace Components
Dedicated::Heartbeat(); Dedicated::Heartbeat();
} }
}); });
#endif
Dvar::OnInit([]() Dvar::OnInit([]()
{ {

View File

@ -167,6 +167,7 @@ namespace Components
void Node::RunFrame() void Node::RunFrame()
{ {
if (ServerList::useMasterServer) return;
if (Dedicated::IsEnabled() && Dvar::Var("sv_lanOnly").get<bool>()) return; if (Dedicated::IsEnabled() && Dvar::Var("sv_lanOnly").get<bool>()) return;
if (!Dedicated::IsEnabled() && *Game::clcState > 0) if (!Dedicated::IsEnabled() && *Game::clcState > 0)
@ -246,7 +247,7 @@ namespace Components
if (list.isnode() && (!list.port() || list.port() == address.getPort())) if (list.isnode() && (!list.port() || list.port() == address.getPort()))
{ {
if (!Dedicated::IsEnabled() && ServerList::IsOnlineList() && list.protocol() == PROTOCOL) if (!Dedicated::IsEnabled() && ServerList::IsOnlineList() && !ServerList::useMasterServer && list.protocol() == PROTOCOL)
{ {
NODE_LOG("Inserting %s into the serverlist\n", address.getCString()); NODE_LOG("Inserting %s into the serverlist\n", address.getCString());
ServerList::InsertRequest(address); ServerList::InsertRequest(address);

View File

@ -19,6 +19,8 @@ namespace Components
Dvar::Var ServerList::NETServerQueryLimit; Dvar::Var ServerList::NETServerQueryLimit;
Dvar::Var ServerList::NETServerFrames; Dvar::Var ServerList::NETServerFrames;
bool ServerList::useMasterServer = true;
std::vector<ServerList::ServerInfo>* ServerList::GetList() std::vector<ServerList::ServerInfo>* ServerList::GetList()
{ {
if (ServerList::IsOnlineList()) if (ServerList::IsOnlineList())
@ -205,13 +207,6 @@ namespace Components
auto list = ServerList::GetList(); auto list = ServerList::GetList();
if (!list) return; if (!list) return;
// Refresh entirely, if there is no entry in the list
if (list->empty())
{
ServerList::Refresh(UIScript::Token());
return;
}
bool ui_browserShowFull = Dvar::Var("ui_browserShowFull").get<bool>(); bool ui_browserShowFull = Dvar::Var("ui_browserShowFull").get<bool>();
bool ui_browserShowEmpty = Dvar::Var("ui_browserShowEmpty").get<bool>(); bool ui_browserShowEmpty = Dvar::Var("ui_browserShowEmpty").get<bool>();
int ui_browserShowHardcore = Dvar::Var("ui_browserKillcam").get<int>(); int ui_browserShowHardcore = Dvar::Var("ui_browserKillcam").get<int>();
@ -274,22 +269,28 @@ namespace Components
} }
else if (ServerList::IsOnlineList()) else if (ServerList::IsOnlineList())
{ {
#ifdef USE_LEGACY_SERVER_LIST const auto masterPort = Dvar::Var("masterPort").get<int>();
const auto masterServerName = Dvar::Var("masterServerName").get<const char*>();
// Check if our dvars can properly convert to a address
Game::netadr_t masterServerAddr;
if (!ServerList::GetMasterServer(masterServerName, masterPort, masterServerAddr))
{
Logger::Print("Could not resolve address for %s:%u", masterServerName, masterPort);
Toast::Show("cardicon_headshot", "^1Error", Utils::String::VA("Could not resolve address for %s:%u", masterServerName, masterPort), 5000);
return;
}
Toast::Show("cardicon_headshot", "Server Browser", "Fetching servers...", 3000);
useMasterServer = true;
ServerList::RefreshContainer.awatingList = true; ServerList::RefreshContainer.awatingList = true;
ServerList::RefreshContainer.awaitTime = Game::Sys_Milliseconds(); ServerList::RefreshContainer.awaitTime = Game::Sys_Milliseconds();
int masterPort = Dvar::Var("masterPort").get<int>();
const char* masterServerName = Dvar::Var("masterServerName").get<const char*>();
ServerList::RefreshContainer.host = Network::Address(Utils::String::VA("%s:%u", masterServerName, masterPort)); ServerList::RefreshContainer.host = Network::Address(Utils::String::VA("%s:%u", masterServerName, masterPort));
Logger::Print("Sending serverlist request to master: %s:%u\n", masterServerName, masterPort); Logger::Print("Sending serverlist request to master\n");
Network::SendCommand(ServerList::RefreshContainer.host, "getservers", Utils::String::VA("IW4 %i full empty", PROTOCOL)); Network::SendCommand(ServerList::RefreshContainer.host, "getservers", Utils::String::VA("IW4 %i full empty", PROTOCOL));
//Network::SendCommand(ServerList::RefreshContainer.Host, "getservers", "0 full empty");
#else
Node::Synchronize();
#endif
} }
else if (ServerList::IsFavouriteList()) else if (ServerList::IsFavouriteList())
{ {
@ -570,8 +571,7 @@ namespace Components
void ServerList::SortList() void ServerList::SortList()
{ {
// Only sort when the serverlist is open // Only sort when the serverlist is open
Game::menuDef_t* menu = Game::Menus_FindByName(Game::uiContext, "pc_join_unranked"); if (!ServerList::IsServerListOpen()) return;
if (!menu || !Game::Menu_IsVisible(Game::uiContext, menu)) return;
std::stable_sort(ServerList::VisibleList.begin(), ServerList::VisibleList.end(), [](const unsigned int &server1, const unsigned int &server2) -> bool std::stable_sort(ServerList::VisibleList.begin(), ServerList::VisibleList.end(), [](const unsigned int &server1, const unsigned int &server2) -> bool
{ {
@ -637,12 +637,22 @@ namespace Components
if (ServerList::RefreshContainer.awatingList) if (ServerList::RefreshContainer.awatingList)
{ {
// Check if we haven't got a response within 10 seconds // Stop counting if we are out of the server browser menu
if (!ServerList::IsServerListOpen())
{
ServerList::RefreshContainer.awatingList = false;
}
// Check if we haven't got a response within 5 seconds
if (Game::Sys_Milliseconds() - ServerList::RefreshContainer.awaitTime > 5000) if (Game::Sys_Milliseconds() - ServerList::RefreshContainer.awaitTime > 5000)
{ {
ServerList::RefreshContainer.awatingList = false; ServerList::RefreshContainer.awatingList = false;
Logger::Print("We haven't received a response from the master within %d seconds!\n", (Game::Sys_Milliseconds() - ServerList::RefreshContainer.awaitTime) / 1000); Logger::Print("We haven't received a response from the master within %d seconds!\n", (Game::Sys_Milliseconds() - ServerList::RefreshContainer.awaitTime) / 1000);
Toast::Show("cardicon_headshot", "^1Error", "Failed to reach master server, using node servers instead.", 5000);
useMasterServer = false;
Node::Synchronize();
} }
} }
@ -733,6 +743,20 @@ namespace Components
} }
} }
bool ServerList::GetMasterServer(const char* ip, int port, Game::netadr_t& address)
{
return Game::NET_StringToAdr(Utils::String::VA("%s:%u", ip, port), &address);
}
bool ServerList::IsServerListOpen()
{
auto* menu = Game::Menus_FindByName(Game::uiContext, "pc_join_unranked");
if (!menu)
return false;
return Game::Menu_IsVisible(Game::uiContext, menu);
}
ServerList::ServerList() ServerList::ServerList()
{ {
ServerList::OnlineList.clear(); ServerList::OnlineList.clear();
@ -792,11 +816,9 @@ namespace Components
}); });
// Set default masterServerName + port and save it // Set default masterServerName + port and save it
#ifdef USE_LEGACY_SERVER_LIST Utils::Hook::Set<const char*>(0x60AD92, "master.xlabs.dev");
Utils::Hook::Set<char*>(0x60AD92, "127.0.0.1");
Utils::Hook::Set<BYTE>(0x60AD90, Game::dvar_flag::DVAR_ARCHIVE); // masterServerName Utils::Hook::Set<BYTE>(0x60AD90, Game::dvar_flag::DVAR_ARCHIVE); // masterServerName
Utils::Hook::Set<BYTE>(0x60ADC6, Game::dvar_flag::DVAR_ARCHIVE); // masterPort Utils::Hook::Set<BYTE>(0x60ADC6, Game::dvar_flag::DVAR_ARCHIVE); // masterPort
#endif
// Add server list feeder // Add server list feeder
UIFeeder::Add(2.0f, ServerList::GetServerCount, ServerList::GetServerText, ServerList::SelectServer); UIFeeder::Add(2.0f, ServerList::GetServerCount, ServerList::GetServerText, ServerList::SelectServer);

View File

@ -50,6 +50,9 @@ namespace Components
static void UpdateVisibleInfo(); static void UpdateVisibleInfo();
static bool GetMasterServer(const char* ip, int port, Game::netadr_t& address);
static bool useMasterServer;
private: private:
enum Column enum Column
{ {
@ -143,5 +146,7 @@ namespace Components
static Dvar::Var UIServerSelectedMap; static Dvar::Var UIServerSelectedMap;
static Dvar::Var NETServerQueryLimit; static Dvar::Var NETServerQueryLimit;
static Dvar::Var NETServerFrames; static Dvar::Var NETServerFrames;
static bool IsServerListOpen();
}; };
} }