Skip to content

Commit b1e88dd

Browse files
committed
Wait for network to become ready on launch
1 parent 2d4c878 commit b1e88dd

File tree

9 files changed

+94
-29
lines changed

9 files changed

+94
-29
lines changed

Client/core/CCommandFuncs.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,12 @@ void CCommandFuncs::Unload(const char* szParameters)
186186

187187
void CCommandFuncs::Connect(const char* szParameters)
188188
{
189+
if (!CCore::GetSingleton().IsNetworkReady())
190+
{
191+
CCore::GetSingleton().GetConsole()->Print(_("connect: Network is not ready, please wait a moment"));
192+
return;
193+
}
194+
189195
// Parse the arguments (host port nick pass)
190196
char szBuffer[256] = "";
191197
if (szParameters)
@@ -273,6 +279,12 @@ void CCommandFuncs::ReloadNews(const char* szParameters)
273279

274280
void CCommandFuncs::Reconnect(const char* szParameters)
275281
{
282+
if (!CCore::GetSingleton().IsNetworkReady())
283+
{
284+
CCore::GetSingleton().GetConsole()->Print(_("reconnect: Network is not ready, please wait a moment"));
285+
return;
286+
}
287+
276288
CModManager::GetSingleton().Unload();
277289

278290
std::string strHost, strNick, strPassword;

Client/core/CConnectManager.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ bool CConnectManager::Connect(const char* szHost, unsigned short usPort, const c
5252
assert(szNick);
5353
assert(szPassword);
5454

55+
if (!CCore::GetSingleton().IsNetworkReady())
56+
{
57+
CCore::GetSingleton().GetLocalGUI()->GetMainMenu()->ShowNetworkNotReadyWindow();
58+
return false;
59+
}
60+
5561
m_bNotifyServerBrowser = bNotifyServerBrowser;
5662

5763
// For detecting startup problems
@@ -337,10 +343,13 @@ void CConnectManager::DoPulse()
337343
}
338344
else if (m_bReconnect)
339345
{
340-
std::string strNick;
341-
CVARS_GET("nick", strNick);
342-
Connect(m_strHost.c_str(), m_usPort, strNick.c_str(), m_strPassword.c_str(), false);
343-
m_bReconnect = false;
346+
if (CCore::GetSingleton().IsNetworkReady())
347+
{
348+
std::string strNick;
349+
CVARS_GET("nick", strNick);
350+
Connect(m_strHost.c_str(), m_usPort, strNick.c_str(), m_strPassword.c_str(), false);
351+
m_bReconnect = false;
352+
}
344353
}
345354
}
346355

Client/core/CCore.cpp

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,11 @@ CCore::CCore()
117117
m_pfnMessageProcessor = NULL;
118118
m_pMessageBox = NULL;
119119

120-
m_bFirstFrame = true;
121120
m_bIsOfflineMod = false;
122121
m_bQuitOnPulse = false;
123122
m_bDestroyMessageBox = false;
124123
m_bCursorToggleControls = false;
125124
m_bLastFocused = true;
126-
m_bWaitToSetNick = false;
127125
m_DiagnosticDebug = EDiagnosticDebug::NONE;
128126

129127
// Create our Direct3DData handler.
@@ -151,7 +149,6 @@ CCore::CCore()
151149
m_bDoneFrameRateLimit = false;
152150
m_uiFrameRateLimit = 0;
153151
m_uiServerFrameRateLimit = 0;
154-
m_uiNewNickWaitFrames = 0;
155152
m_iUnminimizeFrameCounter = 0;
156153
m_bDidRecreateRenderTargets = false;
157154
m_fMinStreamingMemory = 0;
@@ -1236,16 +1233,22 @@ void CCore::DoPostFramePulse()
12361233
// This is the first frame in the menu?
12371234
if (m_pGame->GetSystemState() == 7) // GS_FRONTEND
12381235
{
1239-
if (m_bFirstFrame)
1240-
{
1241-
m_bFirstFrame = false;
1236+
if (m_menuFrame < 255)
1237+
++m_menuFrame;
12421238

1239+
if (m_menuFrame == 1)
1240+
{
12431241
WatchDogCompletedSection("L2"); // gta_sa.set seems ok
12441242
WatchDogCompletedSection("L3"); // No hang on startup
12451243
HandleCrashDumpEncryption();
12461244

12471245
// Disable vsync while it's all dark
12481246
m_pGame->DisableVSync();
1247+
}
1248+
1249+
if (m_menuFrame >= 5 && !m_isNetworkReady && m_pNet->IsReady())
1250+
{
1251+
m_isNetworkReady = true;
12491252

12501253
// Parse the command line
12511254
// Does it begin with mtasa://?
@@ -1269,16 +1272,11 @@ void CCore::DoPostFramePulse()
12691272
}
12701273
}
12711274

1272-
if (m_bWaitToSetNick && GetLocalGUI()->GetMainMenu()->IsVisible() && !GetLocalGUI()->GetMainMenu()->IsFading())
1275+
if (m_menuFrame >= 75 && m_requestNewNickname && GetLocalGUI()->GetMainMenu()->IsVisible() && !GetLocalGUI()->GetMainMenu()->IsFading())
12731276
{
1274-
if (m_uiNewNickWaitFrames > 75)
1275-
{
1276-
// Request a new nickname if we're waiting for one
1277-
GetLocalGUI()->GetMainMenu()->GetSettingsWindow()->RequestNewNickname();
1278-
m_bWaitToSetNick = false;
1279-
}
1280-
else
1281-
m_uiNewNickWaitFrames++;
1277+
// Request a new nickname if we're waiting for one
1278+
GetLocalGUI()->GetMainMenu()->GetSettingsWindow()->RequestNewNickname();
1279+
m_requestNewNickname = false;
12821280
}
12831281
}
12841282

Client/core/CCore.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,8 @@ class CCore : public CCoreInterface, public CSingleton<CCore>
238238
std::map<std::string, std::string>& GetCommandLineOptions() { return m_CommandLineOptions; }
239239
const char* GetCommandLineOption(const char* szOption);
240240
const char* GetCommandLineArgs() { return m_szCommandLineArgs; }
241-
void RequestNewNickOnStart() { m_bWaitToSetNick = true; };
242-
bool WillRequestNewNickOnStart() { return m_bWaitToSetNick; };
241+
void RequestNewNickOnStart() { m_requestNewNickname = true; }
242+
bool WillRequestNewNickOnStart() { return m_requestNewNickname; }
243243
bool WasLaunchedWithConnectURI();
244244
void HandleCrashDumpEncryption();
245245

@@ -279,7 +279,9 @@ class CCore : public CCoreInterface, public CSingleton<CCore>
279279
void SetFakeLagCommandEnabled(bool bEnabled) { m_bFakeLagCommandEnabled = bEnabled; }
280280
bool IsFakeLagCommandEnabled() { return m_bFakeLagCommandEnabled; }
281281
SString GetBlueCopyrightString();
282-
bool IsFirstFrame() const noexcept { return m_bFirstFrame; }
282+
283+
bool IsNetworkReady() const noexcept { return m_isNetworkReady; }
284+
bool CanHandleKeyMessages() const noexcept { return m_menuFrame > 1; }
283285

284286
void SetCustomStreamingMemory(size_t szMB);
285287
bool IsUsingCustomStreamingMemorySize();
@@ -346,7 +348,8 @@ class CCore : public CCoreInterface, public CSingleton<CCore>
346348
CKeyBinds* m_pKeyBinds;
347349
CMouseControl* m_pMouseControl;
348350

349-
bool m_bFirstFrame;
351+
unsigned short m_menuFrame{};
352+
bool m_isNetworkReady{};
350353
bool m_bIsOfflineMod;
351354
bool m_bCursorToggleControls;
352355
pfnProcessMessage m_pfnMessageProcessor;
@@ -368,8 +371,7 @@ class CCore : public CCoreInterface, public CSingleton<CCore>
368371
CElapsedTimeHD m_FrameRateTimer;
369372
uint m_uiQueuedFrameRate;
370373
bool m_bQueuedFrameRateValid;
371-
bool m_bWaitToSetNick;
372-
uint m_uiNewNickWaitFrames;
374+
bool m_requestNewNickname{false};
373375
EDiagnosticDebugType m_DiagnosticDebug;
374376

375377
// Below 2 are used for the UI only

Client/core/CMainMenu.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,19 @@ bool CMainMenu::OnMenuClick(CGUIMouseEventArgs Args)
845845
break;
846846
}
847847
}
848+
else if (!g_pCore->IsNetworkReady())
849+
{
850+
switch (m_pHoveredItem->menuType)
851+
{
852+
// case MENU_ITEM_QUICK_CONNECT: // We only prevent it for left click in OnQuickConnectButtonClick.
853+
case MENU_ITEM_HOST_GAME:
854+
case MENU_ITEM_MAP_EDITOR:
855+
ShowNetworkNotReadyWindow();
856+
return true;
857+
default:
858+
break;
859+
}
860+
}
848861

849862
switch (m_pHoveredItem->menuType)
850863
{
@@ -886,12 +899,21 @@ bool CMainMenu::OnQuickConnectButtonClick(CGUIElement* pElement, bool left)
886899
return false;
887900

888901
if (left)
902+
{
903+
if (!g_pCore->IsNetworkReady())
904+
{
905+
ShowNetworkNotReadyWindow();
906+
return true;
907+
}
908+
889909
g_pCore->GetCommands()->Execute("reconnect", "");
910+
}
890911
else
891912
{
892913
m_ServerBrowser.SetVisible(true);
893914
m_ServerBrowser.OnQuickConnectButtonClick();
894915
}
916+
895917
return true;
896918
}
897919

@@ -1171,6 +1193,26 @@ void CMainMenu::AskUserIfHeWantsToDisconnect(uchar menuType)
11711193
pQuestionBox->Show();
11721194
}
11731195

1196+
/////////////////////////////////////////////////////////////
1197+
//
1198+
// CMainMenu::ShowNetworkNotReadyWindow
1199+
//
1200+
// Shows a window with information that the network module is not ready.
1201+
//
1202+
/////////////////////////////////////////////////////////////
1203+
void CMainMenu::ShowNetworkNotReadyWindow()
1204+
{
1205+
static auto HideQuestionWindow = [](void* window, uint) { reinterpret_cast<CQuestionBox*>(window)->Hide(); };
1206+
1207+
CQuestionBox& window = m_QuestionBox;
1208+
window.Reset();
1209+
window.SetTitle(_("INFORMATION"));
1210+
window.SetMessage("\n\nThe network module is not ready.\nPlease wait a moment and try again.");
1211+
window.SetButton(0, _("Ok"));
1212+
window.SetCallback(HideQuestionWindow, &window);
1213+
window.Show();
1214+
}
1215+
11741216
/////////////////////////////////////////////////////////////
11751217
//
11761218
// CMainMenu::StaticWantsToDisconnectCallBack

Client/core/CMainMenu.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ class CMainMenu
7575
void WantsToDisconnectCallBack(void* pData, uint uiButton);
7676
void AskUserIfHeWantsToDisconnect(uchar menuType);
7777

78+
void ShowNetworkNotReadyWindow();
79+
7880
private:
7981
sMenuItem* CreateItem(unsigned char menuType, const char* szFilename, CVector2D vecRelPosition);
8082
bool SetItemHoverProgress(sMenuItem* pItem, float fProgress, bool bHovering);

Client/core/CMessageLoopHook.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,7 @@ LRESULT CALLBACK CMessageLoopHook::ProcessMessage(HWND hwnd, UINT uMsg, WPARAM w
200200
if (pCDS->dwData == URI_CONNECT)
201201
{
202202
// We can receive this message before we are ready to process it (e.g. trying to show a CEGUI message window before CEGUI is initialized).
203-
// Ignore these messages until we are in the main menu (when CCore sets m_bFirstFrame to false)
204-
if (!g_pCore->IsFirstFrame())
203+
if (g_pCore->IsNetworkReady())
205204
{
206205
LPSTR szConnectInfo = (LPSTR)pCDS->lpData;
207206
CCommandFuncs::Connect(szConnectInfo);
@@ -456,7 +455,7 @@ LRESULT CALLBACK CMessageLoopHook::ProcessMessage(HWND hwnd, UINT uMsg, WPARAM w
456455
}
457456

458457
// Lead the message through the keybinds message processor
459-
if (!g_pCore->IsFirstFrame())
458+
if (g_pCore->CanHandleKeyMessages())
460459
{
461460
g_pCore->GetKeyBinds()->ProcessMessage(hwnd, uMsg, wParam, lParam);
462461
}

Client/sdk/net/CNet.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class CNet
7474

7575
virtual void SetFakeLag(unsigned short usPacketLoss, unsigned short usMinExtraPing, unsigned short usExtraPingVariance, int iKBPSLimit) = 0;
7676

77+
virtual bool IsReady() = 0;
7778
virtual bool IsConnected() = 0;
7879

7980
virtual void DoPulse() = 0;

Shared/sdk/version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@
108108

109109
#define _ASE_VERSION QUOTE_DEFINE(MTASA_VERSION_MAJOR) "." QUOTE_DEFINE(MTASA_VERSION_MINOR)
110110
#define _NETCODE_VERSION_BRANCH_ID 0x4 // Use 0x1 - 0xF to indicate an incompatible branch is being used (0x0 is reserved, 0x4 is trunk)
111-
#define _CLIENT_NET_MODULE_VERSION 0x0B1 // (0x000 - 0xfff) Lvl9 wizards only
111+
#define _CLIENT_NET_MODULE_VERSION 0x0B2 // (0x000 - 0xfff) Lvl9 wizards only
112112
#define _SERVER_NET_MODULE_VERSION 0x0AB // (0x000 - 0xfff) Lvl9 wizards only
113113
#define _NETCODE_VERSION 0x1DA // (0x000 - 0xfff) Increment when net messages change (pre-release)
114114

0 commit comments

Comments
 (0)