DownloadMenu.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. // FILE: DownloadMenu.cpp /////////////////////////////////////////////////////////
  24. //-----------------------------------------------------------------------------
  25. //
  26. // Electronic Arts Pacific.
  27. //
  28. // Confidential Information
  29. // Copyright (C) 2002 - All Rights Reserved
  30. //
  31. //-----------------------------------------------------------------------------
  32. //
  33. // Project: RTS3
  34. //
  35. // File name: DownloadMenu.cpp
  36. //
  37. // Created: Matthew D. Campbell, July 2002
  38. //
  39. // Desc: the Patch Download window control
  40. //
  41. //-----------------------------------------------------------------------------
  42. ///////////////////////////////////////////////////////////////////////////////
  43. // INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
  44. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  45. #include "Common/GameEngine.h"
  46. #include "Common/NameKeyGenerator.h"
  47. #include "GameClient/GUICallbacks.h"
  48. #include "GameClient/GameWindowManager.h"
  49. #include "GameClient/GadgetStaticText.h"
  50. #include "GameClient/GadgetProgressBar.h"
  51. #include "GameClient/GameText.h"
  52. #include "GameClient/MessageBox.h"
  53. #include "GameLogic/GameLogic.h"
  54. #include "GameNetwork/DownloadManager.h"
  55. #include "GameNetwork/GameSpy/MainMenuUtils.h"
  56. // PRIVATE DATA ///////////////////////////////////////////////////////////////////////////////////
  57. static NameKeyType buttonCancelID = NAMEKEY_INVALID;
  58. static NameKeyType staticTextSizeID = NAMEKEY_INVALID;
  59. static NameKeyType staticTextTimeID = NAMEKEY_INVALID;
  60. static NameKeyType staticTextFileID = NAMEKEY_INVALID;
  61. static NameKeyType staticTextStatusID = NAMEKEY_INVALID;
  62. static NameKeyType progressBarMunkeeID = NAMEKEY_INVALID;
  63. static GameWindow * staticTextSize = NULL;
  64. static GameWindow * staticTextTime = NULL;
  65. static GameWindow * staticTextFile = NULL;
  66. static GameWindow * staticTextStatus = NULL;
  67. static GameWindow * progressBarMunkee = NULL;
  68. static GameWindow *parent = NULL;
  69. static void closeDownloadWindow( void )
  70. {
  71. DEBUG_ASSERTCRASH(parent, ("No Parent"));
  72. if (!parent)
  73. return;
  74. WindowLayout *menuLayout = parent->winGetLayout();
  75. menuLayout->runShutdown();
  76. menuLayout->destroyWindows();
  77. menuLayout->deleteInstance();
  78. menuLayout = NULL;
  79. GameWindow *mainWin = TheWindowManager->winGetWindowFromId( NULL, NAMEKEY("MainMenu.wnd:MainMenuParent") );
  80. if (mainWin)
  81. TheWindowManager->winSetFocus( mainWin );
  82. }
  83. static void errorCallback( void )
  84. {
  85. HandleCanceledDownload();
  86. closeDownloadWindow();
  87. }
  88. static void successQuitCallback( void )
  89. {
  90. TheGameEngine->setQuitting( TRUE );
  91. closeDownloadWindow();
  92. // Clean up game data. No crashy-crash for you!
  93. if (TheGameLogic->isInGame())
  94. TheMessageStream->appendMessage( GameMessage::MSG_CLEAR_GAME_DATA );
  95. }
  96. static void successNoQuitCallback( void )
  97. {
  98. HandleCanceledDownload();
  99. closeDownloadWindow();
  100. }
  101. class DownloadManagerMunkee : public DownloadManager
  102. {
  103. public:
  104. DownloadManagerMunkee() {m_shouldQuitOnSuccess = true; m_shouldQuitOnSuccess = false;}
  105. virtual HRESULT OnError( Int error );
  106. virtual HRESULT OnEnd();
  107. virtual HRESULT OnProgressUpdate( Int bytesread, Int totalsize, Int timetaken, Int timeleft );
  108. virtual HRESULT OnStatusUpdate( Int status );
  109. virtual HRESULT downloadFile( AsciiString server, AsciiString username, AsciiString password, AsciiString file, AsciiString localfile, AsciiString regkey, Bool tryResume );
  110. private:
  111. Bool m_shouldQuitOnSuccess;
  112. };
  113. HRESULT DownloadManagerMunkee::downloadFile( AsciiString server, AsciiString username, AsciiString password, AsciiString file, AsciiString localfile, AsciiString regkey, Bool tryResume )
  114. {
  115. // see if we'll need to restart
  116. if (strstr(localfile.str(), "patches\\") != NULL)
  117. {
  118. m_shouldQuitOnSuccess = true;
  119. }
  120. if (staticTextFile)
  121. {
  122. AsciiString bob = file;
  123. // just get the filename, not the pathname
  124. const char *tmp = bob.reverseFind('/');
  125. if (tmp)
  126. bob = tmp+1;
  127. tmp = bob.reverseFind('\\');
  128. if (tmp)
  129. bob = tmp+1;
  130. UnicodeString fileString;
  131. fileString.translate(bob);
  132. GadgetStaticTextSetText(staticTextFile, fileString);
  133. }
  134. password.format("-%s", password.str());
  135. return DownloadManager::downloadFile( server, username, password, file, localfile, regkey, tryResume );
  136. }
  137. HRESULT DownloadManagerMunkee::OnError( Int error )
  138. {
  139. HRESULT ret = DownloadManager::OnError( error );
  140. MessageBoxOk(TheGameText->fetch("GUI:DownloadErrorTitle"), getErrorString(), errorCallback);
  141. return ret;
  142. }
  143. HRESULT DownloadManagerMunkee::OnEnd()
  144. {
  145. HRESULT ret = DownloadManager::OnEnd();
  146. if (isFileQueuedForDownload())
  147. {
  148. return downloadNextQueuedFile();
  149. }
  150. if (m_shouldQuitOnSuccess)
  151. MessageBoxOk(TheGameText->fetch("GUI:DownloadSuccessTitle"), TheGameText->fetch("GUI:DownloadSuccessMustQuit"), successQuitCallback);
  152. else
  153. MessageBoxOk(TheGameText->fetch("GUI:DownloadSuccessTitle"), TheGameText->fetch("GUI:DownloadSuccess"), successNoQuitCallback);
  154. return ret;
  155. }
  156. static time_t lastUpdate = 0;
  157. static Int timeLeft = 0;
  158. HRESULT DownloadManagerMunkee::OnProgressUpdate( Int bytesread, Int totalsize, Int timetaken, Int timeleft )
  159. {
  160. HRESULT ret = DownloadManager::OnProgressUpdate( bytesread, totalsize, timetaken, timeleft );
  161. if (progressBarMunkee)
  162. {
  163. Int percent = bytesread * 100 / totalsize;
  164. GadgetProgressBarSetProgress( progressBarMunkee, percent );
  165. }
  166. if (staticTextSize)
  167. {
  168. UnicodeString sizeString;
  169. sizeString.format(TheGameText->fetch("GUI:DownloadBytesRatio"), bytesread, totalsize);
  170. GadgetStaticTextSetText(staticTextSize, sizeString);
  171. }
  172. timeLeft = timeleft;
  173. if (staticTextTime && GadgetStaticTextGetText(staticTextTime).isEmpty()) // only update immediately the first time
  174. {
  175. lastUpdate = time(NULL);
  176. UnicodeString timeString;
  177. if (timeleft)
  178. {
  179. DEBUG_ASSERTCRASH(timeleft > 0, ("Time left is negative!"));
  180. timeleft = max(1, timeleft);
  181. Int takenHour, takenMin, takenSec;
  182. takenHour = timeleft / 60 / 60;
  183. takenMin = timeleft / 60;
  184. takenSec = timeleft % 60;
  185. timeString.format(TheGameText->fetch("GUI:DownloadTimeLeft"), takenHour, takenMin, takenSec);
  186. }
  187. else
  188. {
  189. timeString = TheGameText->fetch("GUI:DownloadUnknownTime");
  190. }
  191. GadgetStaticTextSetText(staticTextTime, timeString);
  192. }
  193. return ret;
  194. }
  195. HRESULT DownloadManagerMunkee::OnStatusUpdate( Int status )
  196. {
  197. HRESULT ret = DownloadManager::OnStatusUpdate( status );
  198. if (staticTextStatus)
  199. {
  200. GadgetStaticTextSetText(staticTextStatus, getStatusString());
  201. }
  202. return ret;
  203. }
  204. // PUBLIC FUNCTIONS ///////////////////////////////////////////////////////////////////////////////
  205. //-------------------------------------------------------------------------------------------------
  206. /** Initialize the menu */
  207. //-------------------------------------------------------------------------------------------------
  208. void DownloadMenuInit( WindowLayout *layout, void *userData )
  209. {
  210. //set keyboard focus to main parent and set modal
  211. NameKeyType parentID = TheNameKeyGenerator->nameToKey("DownloadMenu.wnd:ParentDownload");
  212. parent = TheWindowManager->winGetWindowFromId( NULL, parentID );
  213. // get ids for our children controls
  214. buttonCancelID = TheNameKeyGenerator->nameToKey( "DownloadMenu.wnd:ButtonCancel" );
  215. staticTextSizeID = TheNameKeyGenerator->nameToKey( "DownloadMenu.wnd:StaticTextSize" );
  216. staticTextTimeID = TheNameKeyGenerator->nameToKey( "DownloadMenu.wnd:StaticTextTime" );
  217. staticTextFileID = TheNameKeyGenerator->nameToKey( "DownloadMenu.wnd:StaticTextFile" );
  218. staticTextStatusID = TheNameKeyGenerator->nameToKey( "DownloadMenu.wnd:StaticTextStatus" );
  219. progressBarMunkeeID = TheNameKeyGenerator->nameToKey( "DownloadMenu.wnd:ProgressBarMunkee" );
  220. staticTextSize = TheWindowManager->winGetWindowFromId( parent, staticTextSizeID );
  221. staticTextTime = TheWindowManager->winGetWindowFromId( parent, staticTextTimeID );
  222. staticTextFile = TheWindowManager->winGetWindowFromId( parent, staticTextFileID );
  223. staticTextStatus = TheWindowManager->winGetWindowFromId( parent, staticTextStatusID );
  224. progressBarMunkee = TheWindowManager->winGetWindowFromId( parent, progressBarMunkeeID );
  225. DEBUG_ASSERTCRASH(!TheDownloadManager, ("Download manager already exists"));
  226. if (TheDownloadManager)
  227. {
  228. delete TheDownloadManager;
  229. }
  230. TheDownloadManager = NEW DownloadManagerMunkee;
  231. } // end DownloadMenuInit
  232. //-------------------------------------------------------------------------------------------------
  233. /** menu shutdown method */
  234. //-------------------------------------------------------------------------------------------------
  235. void DownloadMenuShutdown( WindowLayout *layout, void *userData )
  236. {
  237. DEBUG_ASSERTCRASH(TheDownloadManager, ("No download manager"));
  238. if (TheDownloadManager)
  239. {
  240. delete TheDownloadManager;
  241. TheDownloadManager = NULL;
  242. }
  243. staticTextSize = NULL;
  244. staticTextTime = NULL;
  245. staticTextFile = NULL;
  246. staticTextStatus = NULL;
  247. progressBarMunkee = NULL;
  248. parent = NULL;
  249. } // end DownloadMenuShutdown
  250. //-------------------------------------------------------------------------------------------------
  251. /** menu update method */
  252. //-------------------------------------------------------------------------------------------------
  253. void DownloadMenuUpdate( WindowLayout *layout, void *userData )
  254. {
  255. if (staticTextTime && !GadgetStaticTextGetText(staticTextTime).isEmpty())
  256. {
  257. time_t now = time(NULL);
  258. if (now <= lastUpdate)
  259. return;
  260. lastUpdate = now;
  261. UnicodeString timeString;
  262. if (timeLeft)
  263. {
  264. DEBUG_ASSERTCRASH(timeLeft > 0, ("Time left is negative!"));
  265. timeLeft = max(1, timeLeft);
  266. Int takenHour, takenMin, takenSec;
  267. takenHour = timeLeft / 60 / 60;
  268. takenMin = timeLeft / 60;
  269. takenSec = timeLeft % 60;
  270. timeString.format(TheGameText->fetch("GUI:DownloadTimeLeft"), takenHour, takenMin, takenSec);
  271. }
  272. else
  273. {
  274. timeString = TheGameText->fetch("GUI:DownloadUnknownTime");
  275. }
  276. GadgetStaticTextSetText(staticTextTime, timeString);
  277. }
  278. } // end DownloadMenuUpdate
  279. //-------------------------------------------------------------------------------------------------
  280. /** menu input callback */
  281. //-------------------------------------------------------------------------------------------------
  282. WindowMsgHandledType DownloadMenuInput( GameWindow *window, UnsignedInt msg,
  283. WindowMsgData mData1, WindowMsgData mData2 )
  284. {
  285. switch( msg )
  286. {
  287. // --------------------------------------------------------------------------------------------
  288. case GWM_CHAR:
  289. {
  290. UnsignedByte key = mData1;
  291. UnsignedByte state = mData2;
  292. switch( key )
  293. {
  294. // ----------------------------------------------------------------------------------------
  295. case KEY_ESC:
  296. {
  297. //
  298. // send a simulated selected event to the parent window of the
  299. // back/exit button
  300. //
  301. if( BitTest( state, KEY_STATE_UP ) )
  302. {
  303. AsciiString buttonName( "DownloadMenu.wnd:ButtonCancel" );
  304. NameKeyType buttonID = TheNameKeyGenerator->nameToKey( buttonName );
  305. GameWindow *button = TheWindowManager->winGetWindowFromId( window, buttonID );
  306. TheWindowManager->winSendSystemMsg( window, GBM_SELECTED,
  307. (WindowMsgData)button, buttonID );
  308. } // end if
  309. // don't let key fall through anywhere else
  310. return MSG_HANDLED;
  311. } // end escape
  312. } // end switch( key )
  313. } // end char
  314. } // end switch( msg )
  315. return MSG_IGNORED;
  316. } // end DownloadMenuInput
  317. //-------------------------------------------------------------------------------------------------
  318. /** menu window system callback */
  319. //-------------------------------------------------------------------------------------------------
  320. WindowMsgHandledType DownloadMenuSystem( GameWindow *window, UnsignedInt msg,
  321. WindowMsgData mData1, WindowMsgData mData2 )
  322. {
  323. switch( msg )
  324. {
  325. // --------------------------------------------------------------------------------------------
  326. case GWM_CREATE:
  327. {
  328. break;
  329. } // end create
  330. //---------------------------------------------------------------------------------------------
  331. case GWM_DESTROY:
  332. {
  333. break;
  334. } // end case
  335. //----------------------------------------------------------------------------------------------
  336. case GWM_INPUT_FOCUS:
  337. {
  338. // if we're givin the opportunity to take the keyboard focus we must say we want it
  339. if( mData1 == TRUE )
  340. *(Bool *)mData2 = TRUE;
  341. break;
  342. } // end input
  343. //---------------------------------------------------------------------------------------------
  344. case GBM_SELECTED:
  345. {
  346. GameWindow *control = (GameWindow *)mData1;
  347. Int controlID = control->winGetWindowId();
  348. if( controlID == buttonCancelID )
  349. {
  350. HandleCanceledDownload();
  351. closeDownloadWindow();
  352. } // end if
  353. break;
  354. } // end selected
  355. default:
  356. return MSG_IGNORED;
  357. } // end switch
  358. return MSG_HANDLED;
  359. } // end DownloadMenuSystem