NetworkDirectConnect.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /*
  2. ** Command & Conquer Generals Zero Hour(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. ///////////////////////////////////////////////////////////////////////////////////////
  24. // FILE: NetworkDirectConnect.cpp
  25. // Author: Bryan Cleveland, November 2001
  26. // Description: Lan Lobby Menu
  27. ///////////////////////////////////////////////////////////////////////////////////////
  28. // INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
  29. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  30. #include "GameSpy/peer/peer.h"
  31. #include "Common/QuotedPrintable.h"
  32. #include "Common/UserPreferences.h"
  33. #include "GameClient/AnimateWindowManager.h"
  34. #include "GameClient/WindowLayout.h"
  35. #include "GameClient/Gadget.h"
  36. #include "GameClient/GameText.h"
  37. #include "GameClient/GameWindowManager.h"
  38. #include "GameClient/GadgetComboBox.h"
  39. #include "GameClient/GadgetTextEntry.h"
  40. #include "GameClient/GadgetStaticText.h"
  41. #include "GameClient/Shell.h"
  42. #include "GameClient/GameWindowTransitions.h"
  43. #include "GameNetwork/IPEnumeration.h"
  44. #include "GameNetwork/LANAPI.h"
  45. #include "GameNetwork/LANAPICallbacks.h"
  46. #ifdef _INTERNAL
  47. // for occasional debugging...
  48. //#pragma optimize("", off)
  49. //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
  50. #endif
  51. // window ids ------------------------------------------------------------------------------
  52. // Window Pointers ------------------------------------------------------------------------
  53. // PRIVATE DATA ///////////////////////////////////////////////////////////////////////////////////
  54. extern Bool LANbuttonPushed;
  55. extern Bool LANisShuttingDown;
  56. static Bool isShuttingDown = false;
  57. static Bool buttonPushed = false;
  58. static NameKeyType buttonBackID = NAMEKEY_INVALID;
  59. static NameKeyType buttonHostID = NAMEKEY_INVALID;
  60. static NameKeyType buttonJoinID = NAMEKEY_INVALID;
  61. static NameKeyType editPlayerNameID = NAMEKEY_INVALID;
  62. static NameKeyType comboboxRemoteIPID = NAMEKEY_INVALID;
  63. static NameKeyType staticLocalIPID = NAMEKEY_INVALID;
  64. static GameWindow *buttonBack = NULL;
  65. static GameWindow *buttonHost = NULL;
  66. static GameWindow *buttonJoin = NULL;
  67. static GameWindow *editPlayerName = NULL;
  68. static GameWindow *comboboxRemoteIP = NULL;
  69. static GameWindow *staticLocalIP = NULL;
  70. void PopulateRemoteIPComboBox()
  71. {
  72. LANPreferences userprefs;
  73. GadgetComboBoxReset(comboboxRemoteIP);
  74. Int numRemoteIPs = userprefs.getNumRemoteIPs();
  75. Color white = GameMakeColor(255,255,255,255);
  76. for (Int i = 0; i < numRemoteIPs; ++i)
  77. {
  78. UnicodeString entry;
  79. entry = userprefs.getRemoteIPEntry(i);
  80. GadgetComboBoxAddEntry(comboboxRemoteIP, entry, white);
  81. }
  82. if (numRemoteIPs > 0)
  83. {
  84. GadgetComboBoxSetSelectedPos(comboboxRemoteIP, 0, TRUE);
  85. }
  86. userprefs.write();
  87. }
  88. void UpdateRemoteIPList()
  89. {
  90. Int n1[4], n2[4];
  91. LANPreferences prefs;
  92. Int numEntries = GadgetComboBoxGetLength(comboboxRemoteIP);
  93. Int currentSelection = -1;
  94. GadgetComboBoxGetSelectedPos(comboboxRemoteIP, &currentSelection);
  95. UnicodeString unisel = GadgetComboBoxGetText(comboboxRemoteIP);
  96. AsciiString sel;
  97. sel.translate(unisel);
  98. // UnicodeString newEntry = prefs.getRemoteIPEntry(0);
  99. UnicodeString newEntry = unisel;
  100. UnicodeString newIP;
  101. newEntry.nextToken(&newIP, UnicodeString(L":"));
  102. Int numFields = swscanf(newIP.str(), L"%d.%d.%d.%d", &(n1[0]), &(n1[1]), &(n1[2]), &(n1[3]));
  103. if (numFields != 4) {
  104. // this is not a properly formatted IP, don't change a thing.
  105. return;
  106. }
  107. prefs["RemoteIP0"] = sel;
  108. Int currentINIEntry = 1;
  109. for (Int i = 0; i < numEntries; ++i)
  110. {
  111. if (i != currentSelection)
  112. {
  113. GadgetComboBoxSetSelectedPos(comboboxRemoteIP, i, FALSE);
  114. UnicodeString uni;
  115. uni = GadgetComboBoxGetText(comboboxRemoteIP);
  116. AsciiString ascii;
  117. ascii.translate(uni);
  118. // prevent more than one copy of an IP address from being put in the list.
  119. if (currentSelection == -1)
  120. {
  121. UnicodeString oldEntry = uni;
  122. UnicodeString oldIP;
  123. oldEntry.nextToken(&oldIP, UnicodeString(L":"));
  124. swscanf(oldIP.str(), L"%d.%d.%d.%d", &(n2[0]), &(n2[1]), &(n2[2]), &(n2[3]));
  125. Bool isEqual = TRUE;
  126. for (Int i = 0; (i < 4) && (isEqual == TRUE); ++i) {
  127. if (n1[i] != n2[i]) {
  128. isEqual = FALSE;
  129. }
  130. }
  131. // check to see if this is a duplicate or if this is not a properly formatted IP address.
  132. if (isEqual == TRUE)
  133. {
  134. --numEntries;
  135. continue;
  136. }
  137. }
  138. AsciiString temp;
  139. temp.format("RemoteIP%d", currentINIEntry);
  140. ++currentINIEntry;
  141. prefs[temp.str()] = ascii;
  142. }
  143. }
  144. if (currentSelection == -1)
  145. {
  146. ++numEntries;
  147. }
  148. AsciiString numRemoteIPs;
  149. numRemoteIPs.format("%d", numEntries);
  150. prefs["NumRemoteIPs"] = numRemoteIPs;
  151. prefs.write();
  152. }
  153. void HostDirectConnectGame()
  154. {
  155. // Init LAN API Singleton
  156. DEBUG_ASSERTCRASH(TheLAN != NULL, ("TheLAN is NULL!"));
  157. if (!TheLAN)
  158. {
  159. TheLAN = NEW LANAPI();
  160. }
  161. UnsignedInt localIP = TheLAN->GetLocalIP();
  162. UnicodeString localIPString;
  163. localIPString.format(L"%d.%d.%d.%d", localIP >> 24, (localIP & 0xff0000) >> 16, (localIP & 0xff00) >> 8, localIP & 0xff);
  164. UnicodeString name;
  165. name = GadgetTextEntryGetText(editPlayerName);
  166. LANPreferences prefs;
  167. prefs["UserName"] = UnicodeStringToQuotedPrintable(name);
  168. prefs.write();
  169. while (name.getLength() > g_lanPlayerNameLength)
  170. name.removeLastChar();
  171. TheLAN->RequestSetName(name);
  172. TheLAN->RequestGameCreate(localIPString, TRUE);
  173. }
  174. void JoinDirectConnectGame()
  175. {
  176. // Init LAN API Singleton
  177. if (!TheLAN)
  178. {
  179. TheLAN = NEW LANAPI();
  180. }
  181. UnsignedInt ipaddress = 0;
  182. UnicodeString ipunistring = GadgetComboBoxGetText(comboboxRemoteIP);
  183. AsciiString asciientry;
  184. asciientry.translate(ipunistring);
  185. AsciiString ipstring;
  186. asciientry.nextToken(&ipstring, "(");
  187. char ipstr[16];
  188. strcpy(ipstr, ipstring.str());
  189. Int ip1, ip2, ip3, ip4;
  190. sscanf(ipstr, "%d.%d.%d.%d", &ip1, &ip2, &ip3, &ip4);
  191. DEBUG_LOG(("JoinDirectConnectGame - joining at %d.%d.%d.%d\n", ip1, ip2, ip3, ip4));
  192. ipaddress = (ip1 << 24) + (ip2 << 16) + (ip3 << 8) + ip4;
  193. // ipaddress = htonl(ipaddress);
  194. UnicodeString name;
  195. name = GadgetTextEntryGetText(editPlayerName);
  196. LANPreferences prefs;
  197. prefs["UserName"] = UnicodeStringToQuotedPrintable(name);
  198. prefs.write();
  199. UpdateRemoteIPList();
  200. PopulateRemoteIPComboBox();
  201. while (name.getLength() > g_lanPlayerNameLength)
  202. name.removeLastChar();
  203. TheLAN->RequestSetName(name);
  204. TheLAN->RequestGameJoinDirectConnect(ipaddress);
  205. }
  206. //-------------------------------------------------------------------------------------------------
  207. /** Initialize the WOL Welcome Menu */
  208. //-------------------------------------------------------------------------------------------------
  209. void NetworkDirectConnectInit( WindowLayout *layout, void *userData )
  210. {
  211. LANbuttonPushed = false;
  212. LANisShuttingDown = false;
  213. if (TheLAN == NULL)
  214. {
  215. TheLAN = NEW LANAPI();
  216. TheLAN->init();
  217. }
  218. TheLAN->reset();
  219. buttonPushed = false;
  220. isShuttingDown = false;
  221. TheShell->showShellMap(TRUE);
  222. buttonBackID = TheNameKeyGenerator->nameToKey( AsciiString( "NetworkDirectConnect.wnd:ButtonBack" ) );
  223. buttonHostID = TheNameKeyGenerator->nameToKey( AsciiString( "NetworkDirectConnect.wnd:ButtonHost" ) );
  224. buttonJoinID = TheNameKeyGenerator->nameToKey( AsciiString( "NetworkDirectConnect.wnd:ButtonJoin" ) );
  225. editPlayerNameID = TheNameKeyGenerator->nameToKey( AsciiString( "NetworkDirectConnect.wnd:EditPlayerName" ) );
  226. comboboxRemoteIPID = TheNameKeyGenerator->nameToKey( AsciiString( "NetworkDirectConnect.wnd:ComboboxRemoteIP" ) );
  227. staticLocalIPID = TheNameKeyGenerator->nameToKey( AsciiString( "NetworkDirectConnect.wnd:StaticLocalIP" ) );
  228. buttonBack = TheWindowManager->winGetWindowFromId( NULL, buttonBackID);
  229. buttonHost = TheWindowManager->winGetWindowFromId( NULL, buttonHostID);
  230. buttonJoin = TheWindowManager->winGetWindowFromId( NULL, buttonJoinID);
  231. editPlayerName = TheWindowManager->winGetWindowFromId( NULL, editPlayerNameID);
  232. comboboxRemoteIP = TheWindowManager->winGetWindowFromId( NULL, comboboxRemoteIPID);
  233. staticLocalIP = TheWindowManager->winGetWindowFromId( NULL, staticLocalIPID);
  234. // // animate controls
  235. // TheShell->registerWithAnimateManager(buttonBack, WIN_ANIMATION_SLIDE_LEFT, TRUE, 800);
  236. // TheShell->registerWithAnimateManager(buttonHost, WIN_ANIMATION_SLIDE_LEFT, TRUE, 600);
  237. // TheShell->registerWithAnimateManager(buttonJoin, WIN_ANIMATION_SLIDE_LEFT, TRUE, 200);
  238. //
  239. LANPreferences userprefs;
  240. UnicodeString name;
  241. name = userprefs.getUserName();
  242. if (name.getLength() == 0)
  243. {
  244. name = TheGameText->fetch("GUI:Player");
  245. }
  246. GadgetTextEntrySetText(editPlayerName, name);
  247. PopulateRemoteIPComboBox();
  248. UnicodeString ipstr;
  249. delete TheLAN;
  250. TheLAN = NULL;
  251. if (TheLAN == NULL) {
  252. // DEBUG_ASSERTCRASH(TheLAN != NULL, ("TheLAN is null initializing the direct connect screen."));
  253. TheLAN = NEW LANAPI();
  254. OptionPreferences prefs;
  255. UnsignedInt IP = prefs.getOnlineIPAddress();
  256. IPEnumeration IPs;
  257. // if (!IP)
  258. // {
  259. EnumeratedIP *IPlist = IPs.getAddresses();
  260. DEBUG_ASSERTCRASH(IPlist, ("No IP addresses found!"));
  261. if (!IPlist)
  262. {
  263. /// @todo: display error and exit lan lobby if no IPs are found
  264. }
  265. Bool foundIP = FALSE;
  266. EnumeratedIP *tempIP = IPlist;
  267. while ((tempIP != NULL) && (foundIP == FALSE)) {
  268. if (IP == tempIP->getIP()) {
  269. foundIP = TRUE;
  270. }
  271. tempIP = tempIP->getNext();
  272. }
  273. if (foundIP == FALSE) {
  274. // The IP that we had no longer exists, we need to pick a new one.
  275. IP = IPlist->getIP();
  276. }
  277. // IP = IPlist->getIP();
  278. // }
  279. TheLAN->init();
  280. TheLAN->SetLocalIP(IP);
  281. }
  282. UnsignedInt ip = TheLAN->GetLocalIP();
  283. ipstr.format(L"%d.%d.%d.%d", ip >> 24, (ip & 0xff0000) >> 16, (ip & 0xff00) >> 8, ip & 0xff);
  284. GadgetStaticTextSetText(staticLocalIP, ipstr);
  285. TheLAN->RequestLobbyLeave(true);
  286. layout->hide(FALSE);
  287. layout->bringForward();
  288. TheTransitionHandler->setGroup("NetworkDirectConnectFade");
  289. } // NetworkDirectConnectInit
  290. //-------------------------------------------------------------------------------------------------
  291. /** This is called when a shutdown is complete for this menu */
  292. //-------------------------------------------------------------------------------------------------
  293. static void shutdownComplete( WindowLayout *layout )
  294. {
  295. isShuttingDown = false;
  296. // hide the layout
  297. layout->hide( TRUE );
  298. // our shutdown is complete
  299. TheShell->shutdownComplete( layout );
  300. } // end if
  301. //-------------------------------------------------------------------------------------------------
  302. /** WOL Welcome Menu shutdown method */
  303. //-------------------------------------------------------------------------------------------------
  304. void NetworkDirectConnectShutdown( WindowLayout *layout, void *userData )
  305. {
  306. isShuttingDown = true;
  307. // if we are shutting down for an immediate pop, skip the animations
  308. Bool popImmediate = *(Bool *)userData;
  309. if( popImmediate )
  310. {
  311. shutdownComplete( layout );
  312. return;
  313. } //end if
  314. TheShell->reverseAnimatewindow();
  315. TheTransitionHandler->reverse("NetworkDirectConnectFade");
  316. } // NetworkDirectConnectShutdown
  317. //-------------------------------------------------------------------------------------------------
  318. /** WOL Welcome Menu update method */
  319. //-------------------------------------------------------------------------------------------------
  320. void NetworkDirectConnectUpdate( WindowLayout * layout, void *userData)
  321. {
  322. // We'll only be successful if we've requested to
  323. if(isShuttingDown && TheShell->isAnimFinished() && TheTransitionHandler->isFinished())
  324. shutdownComplete(layout);
  325. }// NetworkDirectConnectUpdate
  326. //-------------------------------------------------------------------------------------------------
  327. /** WOL Welcome Menu input callback */
  328. //-------------------------------------------------------------------------------------------------
  329. WindowMsgHandledType NetworkDirectConnectInput( GameWindow *window, UnsignedInt msg,
  330. WindowMsgData mData1, WindowMsgData mData2 )
  331. {
  332. switch( msg )
  333. {
  334. // --------------------------------------------------------------------------------------------
  335. case GWM_CHAR:
  336. {
  337. UnsignedByte key = mData1;
  338. UnsignedByte state = mData2;
  339. if (buttonPushed)
  340. break;
  341. switch( key )
  342. {
  343. // ----------------------------------------------------------------------------------------
  344. case KEY_ESC:
  345. {
  346. //
  347. // send a simulated selected event to the parent window of the
  348. // back/exit button
  349. //
  350. if( BitTest( state, KEY_STATE_UP ) )
  351. {
  352. TheWindowManager->winSendSystemMsg( window, GBM_SELECTED,
  353. (WindowMsgData)buttonBack, buttonBackID );
  354. } // end if
  355. // don't let key fall through anywhere else
  356. return MSG_HANDLED;
  357. } // end escape
  358. } // end switch( key )
  359. } // end char
  360. } // end switch( msg )
  361. return MSG_IGNORED;
  362. }// NetworkDirectConnectInput
  363. //-------------------------------------------------------------------------------------------------
  364. /** WOL Welcome Menu window system callback */
  365. //-------------------------------------------------------------------------------------------------
  366. WindowMsgHandledType NetworkDirectConnectSystem( GameWindow *window, UnsignedInt msg,
  367. WindowMsgData mData1, WindowMsgData mData2 )
  368. {
  369. UnicodeString txtInput;
  370. switch( msg )
  371. {
  372. case GWM_CREATE:
  373. {
  374. break;
  375. } // case GWM_DESTROY:
  376. case GWM_DESTROY:
  377. {
  378. break;
  379. } // case GWM_DESTROY:
  380. case GWM_INPUT_FOCUS:
  381. {
  382. // if we're givin the opportunity to take the keyboard focus we must say we want it
  383. if( mData1 == TRUE )
  384. *(Bool *)mData2 = TRUE;
  385. return MSG_HANDLED;
  386. }//case GWM_INPUT_FOCUS:
  387. case GBM_SELECTED:
  388. {
  389. if (buttonPushed)
  390. break;
  391. GameWindow *control = (GameWindow *)mData1;
  392. Int controlID = control->winGetWindowId();
  393. if ( controlID == buttonBackID )
  394. {
  395. UnicodeString name;
  396. name = GadgetTextEntryGetText(editPlayerName);
  397. LANPreferences prefs;
  398. prefs["UserName"] = UnicodeStringToQuotedPrintable(name);
  399. prefs.write();
  400. while (name.getLength() > g_lanPlayerNameLength)
  401. name.removeLastChar();
  402. TheLAN->RequestSetName(name);
  403. buttonPushed = true;
  404. LANbuttonPushed = true;
  405. TheShell->pop();
  406. } //if ( controlID == buttonBack )
  407. else if (controlID == buttonHostID)
  408. {
  409. HostDirectConnectGame();
  410. }
  411. else if (controlID == buttonJoinID)
  412. {
  413. JoinDirectConnectGame();
  414. }
  415. break;
  416. }// case GBM_SELECTED:
  417. case GEM_EDIT_DONE:
  418. {
  419. break;
  420. }
  421. default:
  422. return MSG_IGNORED;
  423. }//Switch
  424. return MSG_HANDLED;
  425. }// NetworkDirectConnectSystem