LANAPIhandlers.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  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: LANAPIHandlers.cpp
  25. // Author: Matthew D. Campbell, October 2001
  26. // Description: LAN callback handlers
  27. ///////////////////////////////////////////////////////////////////////////////////////
  28. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  29. #include "Common/CRC.h"
  30. #include "Common/GameState.h"
  31. #include "Common/Registry.h"
  32. #include "Common/GlobalData.h"
  33. #include "Common/QuotedPrintable.h"
  34. #include "Common/UserPreferences.h"
  35. #include "GameNetwork/LANAPI.h"
  36. #include "GameNetwork/LANAPICallbacks.h"
  37. #include "GameClient/MapUtil.h"
  38. void LANAPI::handleRequestLocations( LANMessage *msg, UnsignedInt senderIP )
  39. {
  40. if (m_inLobby)
  41. {
  42. LANMessage reply;
  43. fillInLANMessage( &reply );
  44. reply.LANMessageType = LANMessage::MSG_LOBBY_ANNOUNCE;
  45. sendMessage(&reply);
  46. m_lastResendTime = timeGetTime();
  47. }
  48. else
  49. {
  50. // In game - are we a game host?
  51. if (m_currentGame)
  52. {
  53. if (m_currentGame->getIP(0) == m_localIP)
  54. {
  55. LANMessage reply;
  56. fillInLANMessage( &reply );
  57. reply.LANMessageType = LANMessage::MSG_GAME_ANNOUNCE;
  58. AsciiString gameOpts = GenerateGameOptionsString();
  59. strncpy(reply.GameInfo.options,gameOpts.str(),m_lanMaxOptionsLength);
  60. wcsncpy(reply.GameInfo.gameName, m_currentGame->getName().str(), g_lanGameNameLength);
  61. reply.GameInfo.gameName[g_lanGameNameLength] = 0;
  62. reply.GameInfo.inProgress = m_currentGame->isGameInProgress();
  63. sendMessage(&reply);
  64. }
  65. else
  66. {
  67. // We're a joiner
  68. }
  69. }
  70. }
  71. // Add the player to the lobby player list
  72. LANPlayer *player = LookupPlayer(senderIP);
  73. if (!player)
  74. {
  75. player = NEW LANPlayer;
  76. player->setIP(senderIP);
  77. }
  78. else
  79. {
  80. removePlayer(player);
  81. }
  82. player->setName(UnicodeString(msg->name));
  83. player->setHost(msg->hostName);
  84. player->setLogin(msg->userName);
  85. player->setLastHeard(timeGetTime());
  86. addPlayer(player);
  87. OnNameChange(player->getIP(), player->getName());
  88. }
  89. void LANAPI::handleGameAnnounce( LANMessage *msg, UnsignedInt senderIP )
  90. {
  91. if (senderIP == m_localIP)
  92. {
  93. return; // Don't try to update own info
  94. }
  95. else if (m_currentGame && m_currentGame->isGameInProgress())
  96. {
  97. return; // Don't care about games if we're playing
  98. }
  99. else if (senderIP == m_directConnectRemoteIP)
  100. {
  101. if (m_currentGame == NULL)
  102. {
  103. LANGameInfo *game = LookupGame(UnicodeString(msg->GameInfo.gameName));
  104. if (!game)
  105. {
  106. game = NEW LANGameInfo;
  107. game->setName(UnicodeString(msg->GameInfo.gameName));
  108. addGame(game);
  109. }
  110. Bool success = ParseGameOptionsString(game,AsciiString(msg->GameInfo.options));
  111. game->setGameInProgress(msg->GameInfo.inProgress);
  112. game->setIsDirectConnect(msg->GameInfo.isDirectConnect);
  113. game->setLastHeard(timeGetTime());
  114. if (!success)
  115. {
  116. // remove from list
  117. removeGame(game);
  118. delete game;
  119. game = NULL;
  120. return;
  121. }
  122. RequestGameJoin(game, m_directConnectRemoteIP);
  123. }
  124. }
  125. else
  126. {
  127. LANGameInfo *game = LookupGame(UnicodeString(msg->GameInfo.gameName));
  128. if (!game)
  129. {
  130. game = NEW LANGameInfo;
  131. game->setName(UnicodeString(msg->GameInfo.gameName));
  132. addGame(game);
  133. }
  134. Bool success = ParseGameOptionsString(game,AsciiString(msg->GameInfo.options));
  135. game->setGameInProgress(msg->GameInfo.inProgress);
  136. game->setIsDirectConnect(msg->GameInfo.isDirectConnect);
  137. game->setLastHeard(timeGetTime());
  138. if (!success)
  139. {
  140. // remove from list
  141. removeGame(game);
  142. delete game;
  143. game = NULL;
  144. }
  145. OnGameList( m_games );
  146. // if (game == m_currentGame && !m_inLobby)
  147. // OnSlotList(RET_OK, game);
  148. }
  149. }
  150. void LANAPI::handleLobbyAnnounce( LANMessage *msg, UnsignedInt senderIP )
  151. {
  152. LANPlayer *player = LookupPlayer(senderIP);
  153. if (!player)
  154. {
  155. player = NEW LANPlayer;
  156. player->setIP(senderIP);
  157. }
  158. else
  159. {
  160. removePlayer(player);
  161. }
  162. player->setName(UnicodeString(msg->name));
  163. player->setHost(msg->hostName);
  164. player->setLogin(msg->userName);
  165. player->setLastHeard(timeGetTime());
  166. addPlayer(player);
  167. OnNameChange(player->getIP(), player->getName());
  168. }
  169. void LANAPI::handleRequestGameInfo( LANMessage *msg, UnsignedInt senderIP )
  170. {
  171. // In game - are we a game host?
  172. if (m_currentGame)
  173. {
  174. if (m_currentGame->getIP(0) == m_localIP || (m_currentGame->isGameInProgress() && TheNetwork && TheNetwork->isPacketRouter())) // if we're in game we should reply if we're the packet router
  175. {
  176. LANMessage reply;
  177. fillInLANMessage( &reply );
  178. reply.LANMessageType = LANMessage::MSG_GAME_ANNOUNCE;
  179. AsciiString gameOpts = GameInfoToAsciiString(m_currentGame);
  180. strncpy(reply.GameInfo.options,gameOpts.str(),m_lanMaxOptionsLength);
  181. wcsncpy(reply.GameInfo.gameName, m_currentGame->getName().str(), g_lanGameNameLength);
  182. reply.GameInfo.gameName[g_lanGameNameLength] = 0;
  183. reply.GameInfo.inProgress = m_currentGame->isGameInProgress();
  184. reply.GameInfo.isDirectConnect = m_currentGame->getIsDirectConnect();
  185. sendMessage(&reply, senderIP);
  186. }
  187. }
  188. }
  189. void LANAPI::handleRequestJoin( LANMessage *msg, UnsignedInt senderIP )
  190. {
  191. UnsignedInt responseIP = senderIP; // need this cause the player may or may not be
  192. // in the player list at the sendMessage.
  193. if (msg->GameToJoin.gameIP != m_localIP)
  194. {
  195. return; // Not us. Ignore it.
  196. }
  197. LANMessage reply;
  198. fillInLANMessage( &reply );
  199. if (!m_inLobby && m_currentGame && m_currentGame->getIP(0) == m_localIP)
  200. {
  201. if (m_currentGame->isGameInProgress())
  202. {
  203. reply.LANMessageType = LANMessage::MSG_JOIN_DENY;
  204. reply.GameNotJoined.reason = LANAPIInterface::RET_GAME_STARTED;
  205. reply.GameNotJoined.gameIP = m_localIP;
  206. reply.GameNotJoined.playerIP = senderIP;
  207. DEBUG_LOG(("LANAPI::handleRequestJoin - join denied because game already started.\n"));
  208. }
  209. else
  210. {
  211. int player;
  212. Bool canJoin = true;
  213. // see if the CRCs match
  214. #if defined(_DEBUG) || defined(_INTERNAL)
  215. if (TheGlobalData->m_netMinPlayers > 0) {
  216. #endif
  217. /* if (msg->GameToJoin.iniCRC != TheGlobalData->m_iniCRC ||
  218. msg->GameToJoin.exeCRC != TheGlobalData->m_exeCRC)
  219. {
  220. DEBUG_LOG(("LANAPI::handleRequestJoin - join denied because of CRC mismatch. CRCs are them/us INI:%X/%X exe:%X/%X\n",
  221. msg->GameToJoin.iniCRC, TheGlobalData->m_iniCRC,
  222. msg->GameToJoin.exeCRC, TheGlobalData->m_exeCRC));
  223. reply.LANMessageType = LANMessage::MSG_JOIN_DENY;
  224. reply.GameNotJoined.reason = LANAPIInterface::RET_CRC_MISMATCH;
  225. reply.GameNotJoined.gameIP = m_localIP;
  226. reply.GameNotJoined.playerIP = senderIP;
  227. canJoin = false;
  228. }
  229. */
  230. #if defined(_DEBUG) || defined(_INTERNAL)
  231. }
  232. #endif
  233. // check for a duplicate serial
  234. AsciiString s;
  235. for (player = 0; canJoin && player<MAX_SLOTS; ++player)
  236. {
  237. LANGameSlot *slot = m_currentGame->getLANSlot(player);
  238. s.clear();
  239. if (player == 0)
  240. {
  241. GetStringFromRegistry("\\ergc", "", s);
  242. }
  243. else if (slot->isHuman())
  244. {
  245. s = slot->getSerial();
  246. if (s.isEmpty())
  247. s = "<Munkee>";
  248. }
  249. if (s.isNotEmpty())
  250. {
  251. DEBUG_LOG(("Checking serial '%s' in slot %d\n", s.str(), player));
  252. if (!strncmp(s.str(), msg->GameToJoin.serial, g_maxSerialLength))
  253. {
  254. // serials match! kick the punk!
  255. reply.LANMessageType = LANMessage::MSG_JOIN_DENY;
  256. reply.GameNotJoined.reason = LANAPIInterface::RET_SERIAL_DUPE;
  257. reply.GameNotJoined.gameIP = m_localIP;
  258. reply.GameNotJoined.playerIP = senderIP;
  259. canJoin = false;
  260. DEBUG_LOG(("LANAPI::handleRequestJoin - join denied because of duplicate serial # (%s).\n", s.str()));
  261. break;
  262. }
  263. }
  264. }
  265. // We're the host, so see if he has a duplicate name
  266. for (player = 0; canJoin && player<MAX_SLOTS; ++player)
  267. {
  268. LANGameSlot *slot = m_currentGame->getLANSlot(player);
  269. if (slot->isHuman() && slot->getName().compare(msg->name) == 0)
  270. {
  271. // just deny duplicates
  272. reply.LANMessageType = LANMessage::MSG_JOIN_DENY;
  273. reply.GameNotJoined.reason = LANAPIInterface::RET_DUPLICATE_NAME;
  274. reply.GameNotJoined.gameIP = m_localIP;
  275. reply.GameNotJoined.playerIP = senderIP;
  276. canJoin = false;
  277. DEBUG_LOG(("LANAPI::handleRequestJoin - join denied because of duplicate names.\n"));
  278. break;
  279. }
  280. }
  281. // See if there's room
  282. // First get the number of players currently in the room.
  283. Int numPlayers = 0;
  284. for (player = 0; player < MAX_SLOTS; ++player)
  285. {
  286. if (m_currentGame->getLANSlot(player)->isOccupied()
  287. && !(m_currentGame->getLANSlot(player)->getPlayerTemplate() == PLAYERTEMPLATE_OBSERVER))
  288. {
  289. ++numPlayers;
  290. }
  291. }
  292. // now get the number of starting spots on the map.
  293. Int numStartingSpots = MAX_SLOTS;
  294. const MapMetaData *md = TheMapCache->findMap(m_currentGame->getMap());
  295. if (md != NULL)
  296. {
  297. numStartingSpots = md->m_numPlayers;
  298. }
  299. if (numPlayers < numStartingSpots) {
  300. for (player = 0; canJoin && player<MAX_SLOTS; ++player)
  301. {
  302. if (m_currentGame->getLANSlot(player)->isOpen())
  303. {
  304. // OK, add him in.
  305. reply.LANMessageType = LANMessage::MSG_JOIN_ACCEPT;
  306. wcsncpy(reply.GameJoined.gameName, m_currentGame->getName().str(), g_lanGameNameLength);
  307. reply.GameJoined.gameName[g_lanGameNameLength] = 0;
  308. reply.GameJoined.slotPosition = player;
  309. reply.GameJoined.gameIP = m_localIP;
  310. reply.GameJoined.playerIP = senderIP;
  311. LANGameSlot newSlot;
  312. newSlot.setState(SLOT_PLAYER, UnicodeString(msg->name));
  313. newSlot.setIP(senderIP);
  314. newSlot.setPort(NETWORK_BASE_PORT_NUMBER);
  315. newSlot.setLastHeard(timeGetTime());
  316. newSlot.setSerial(msg->GameToJoin.serial);
  317. m_currentGame->setSlot(player,newSlot);
  318. DEBUG_LOG(("LANAPI::handleRequestJoin - added player %ls at ip 0x%08x to the game\n", msg->name, senderIP));
  319. OnPlayerJoin(player, UnicodeString(msg->name));
  320. responseIP = 0;
  321. break;
  322. }
  323. }
  324. }
  325. if (canJoin && player == MAX_SLOTS)
  326. {
  327. reply.LANMessageType = LANMessage::MSG_JOIN_DENY;
  328. wcsncpy(reply.GameNotJoined.gameName, m_currentGame->getName().str(), g_lanGameNameLength);
  329. reply.GameNotJoined.gameName[g_lanGameNameLength] = 0;
  330. reply.GameNotJoined.reason = LANAPIInterface::RET_GAME_FULL;
  331. reply.GameNotJoined.gameIP = m_localIP;
  332. reply.GameNotJoined.playerIP = senderIP;
  333. DEBUG_LOG(("LANAPI::handleRequestJoin - join denied because game is full.\n"));
  334. }
  335. }
  336. }
  337. else
  338. {
  339. reply.LANMessageType = LANMessage::MSG_JOIN_DENY;
  340. reply.GameNotJoined.reason = LANAPIInterface::RET_GAME_GONE;
  341. reply.GameNotJoined.gameIP = m_localIP;
  342. reply.GameNotJoined.playerIP = senderIP;
  343. }
  344. sendMessage(&reply, responseIP);
  345. RequestGameOptions(GenerateGameOptionsString(), true);
  346. }
  347. void LANAPI::handleJoinAccept( LANMessage *msg, UnsignedInt senderIP )
  348. {
  349. if (msg->GameJoined.playerIP == m_localIP) // Is it for us?
  350. {
  351. if (m_pendingAction == ACT_JOIN) // Are we trying to join?
  352. {
  353. m_currentGame = LookupGame(UnicodeString(msg->GameJoined.gameName));
  354. if (!m_currentGame)
  355. {
  356. DEBUG_ASSERTCRASH(false, ("Could not find game to join!"));
  357. OnGameJoin(RET_UNKNOWN, NULL);
  358. }
  359. else
  360. {
  361. m_inLobby = false;
  362. AsciiString options = GameInfoToAsciiString(m_currentGame);
  363. m_currentGame->enterGame();
  364. ParseAsciiStringToGameInfo(m_currentGame, options);
  365. Int pos = msg->GameJoined.slotPosition;
  366. LANGameSlot slot;
  367. slot.setState(SLOT_PLAYER, m_name);
  368. slot.setIP(m_localIP);
  369. slot.setPort(NETWORK_BASE_PORT_NUMBER);
  370. slot.setLastHeard(0);
  371. slot.setLogin(m_userName);
  372. slot.setHost(m_hostName);
  373. m_currentGame->setSlot(pos, slot);
  374. m_currentGame->getLANSlot(0)->setHost(msg->hostName);
  375. m_currentGame->getLANSlot(0)->setLogin(msg->userName);
  376. LANPreferences prefs;
  377. AsciiString entry;
  378. entry.format("%d.%d.%d.%d:%s", senderIP >> 24, (senderIP & 0xff0000) >> 16, (senderIP & 0xff00) >> 8, senderIP & 0xff, UnicodeStringToQuotedPrintable(m_currentGame->getSlot(0)->getName()).str());
  379. prefs["RemoteIP0"] = entry;
  380. prefs.write();
  381. OnGameJoin(RET_OK, m_currentGame);
  382. //DEBUG_ASSERTCRASH(false, ("setting host to %ls@%ls\n", m_currentGame->getLANSlot(0)->getUser()->getLogin().str(),
  383. // m_currentGame->getLANSlot(0)->getUser()->getHost().str()));
  384. }
  385. m_pendingAction = ACT_NONE;
  386. m_expiration = 0;
  387. }
  388. }
  389. }
  390. void LANAPI::handleJoinDeny( LANMessage *msg, UnsignedInt senderIP )
  391. {
  392. if (msg->GameJoined.playerIP == m_localIP) // Is it for us?
  393. {
  394. if (m_pendingAction == ACT_JOIN) // Are we trying to join?
  395. {
  396. OnGameJoin(msg->GameNotJoined.reason, LookupGame(UnicodeString(msg->GameNotJoined.gameName)));
  397. m_pendingAction = ACT_NONE;
  398. m_expiration = 0;
  399. }
  400. }
  401. }
  402. void LANAPI::handleRequestGameLeave( LANMessage *msg, UnsignedInt senderIP )
  403. {
  404. if (!m_inLobby && m_currentGame && !m_currentGame->isGameInProgress())
  405. {
  406. int player;
  407. for (player = 0; player < MAX_SLOTS; ++player)
  408. {
  409. if (m_currentGame->getIP(player) == senderIP)
  410. {
  411. if (player == 0)
  412. {
  413. OnHostLeave();
  414. removeGame(m_currentGame);
  415. delete m_currentGame;
  416. m_currentGame = NULL;
  417. /// @todo re-add myself to lobby? Or just keep me there all the time? If we send a LOBBY_ANNOUNCE things'll work out...
  418. LANPlayer *lanPlayer = LookupPlayer(m_localIP);
  419. if (!lanPlayer)
  420. {
  421. lanPlayer = NEW LANPlayer;
  422. lanPlayer->setIP(m_localIP);
  423. }
  424. else
  425. {
  426. removePlayer(lanPlayer);
  427. }
  428. lanPlayer->setName(UnicodeString(m_name));
  429. lanPlayer->setHost(m_hostName);
  430. lanPlayer->setLogin(m_userName);
  431. lanPlayer->setLastHeard(timeGetTime());
  432. addPlayer(lanPlayer);
  433. }
  434. else
  435. {
  436. if (AmIHost())
  437. {
  438. // remove the deadbeat
  439. LANGameSlot slot;
  440. slot.setState(SLOT_OPEN);
  441. m_currentGame->setSlot( player, slot );
  442. }
  443. OnPlayerLeave(UnicodeString(msg->name));
  444. m_currentGame->getLANSlot(player)->setState(SLOT_OPEN);
  445. m_currentGame->resetAccepted();
  446. RequestGameOptions(GenerateGameOptionsString(), false, senderIP);
  447. //m_currentGame->endGame();
  448. }
  449. break;
  450. }
  451. DEBUG_ASSERTCRASH(player < MAX_SLOTS, ("Didn't find player!"));
  452. }
  453. }
  454. else if (m_inLobby)
  455. {
  456. // Look for dissappearing games
  457. LANGameInfo *game = m_games;
  458. while (game)
  459. {
  460. if (game->getName().compare(msg->GameToLeave.gameName) == 0)
  461. {
  462. removeGame(game);
  463. delete game;
  464. OnGameList(m_games);
  465. break;
  466. }
  467. game = game->getNext();
  468. }
  469. }
  470. }
  471. void LANAPI::handleRequestLobbyLeave( LANMessage *msg, UnsignedInt senderIP )
  472. {
  473. if (m_inLobby)
  474. {
  475. LANPlayer *player = m_lobbyPlayers;
  476. while (player)
  477. {
  478. if (player->getIP() == senderIP)
  479. {
  480. removePlayer(player);
  481. OnPlayerList(m_lobbyPlayers);
  482. break;
  483. }
  484. player = player->getNext();
  485. }
  486. }
  487. }
  488. void LANAPI::handleSetAccept( LANMessage *msg, UnsignedInt senderIP )
  489. {
  490. if (!m_inLobby && m_currentGame && !m_currentGame->isGameInProgress())
  491. {
  492. int player;
  493. for (player = 0; player < MAX_SLOTS; ++player)
  494. {
  495. if (m_currentGame->getIP(player) == senderIP)
  496. {
  497. OnAccept(senderIP, msg->Accept.isAccepted);
  498. break;
  499. }
  500. }
  501. }
  502. }
  503. void LANAPI::handleHasMap( LANMessage *msg, UnsignedInt senderIP )
  504. {
  505. if (!m_inLobby && m_currentGame)
  506. {
  507. CRC mapNameCRC;
  508. // mapNameCRC.computeCRC(m_currentGame->getMap().str(), m_currentGame->getMap().getLength());
  509. AsciiString portableMapName = TheGameState->realMapPathToPortableMapPath(m_currentGame->getMap());
  510. mapNameCRC.computeCRC(portableMapName.str(), portableMapName.getLength());
  511. if (mapNameCRC.get() != msg->MapStatus.mapCRC)
  512. {
  513. return;
  514. }
  515. int player;
  516. for (player = 0; player < MAX_SLOTS; ++player)
  517. {
  518. if (m_currentGame->getIP(player) == senderIP)
  519. {
  520. OnHasMap(senderIP, msg->MapStatus.hasMap);
  521. break;
  522. }
  523. }
  524. }
  525. }
  526. void LANAPI::handleChat( LANMessage *msg, UnsignedInt senderIP )
  527. {
  528. if (m_inLobby)
  529. {
  530. LANPlayer *player;
  531. if((player=LookupPlayer(senderIP)) != 0)
  532. {
  533. OnChat(UnicodeString(player->getName()), player->getIP(), UnicodeString(msg->Chat.message), msg->Chat.chatType);
  534. player->setLastHeard(timeGetTime());
  535. }
  536. }
  537. else
  538. {
  539. if (LookupGame(UnicodeString(msg->Chat.gameName)) != m_currentGame)
  540. {
  541. DEBUG_LOG(("Game '%ls' is not my game\n", msg->Chat.gameName));
  542. if (m_currentGame)
  543. {
  544. DEBUG_LOG(("Current game is '%ls'\n", m_currentGame->getName().str()));
  545. }
  546. return;
  547. }
  548. int player;
  549. for (player = 0; player < MAX_SLOTS; ++player)
  550. {
  551. if (m_currentGame && m_currentGame->getIP(player) == senderIP)
  552. {
  553. OnChat(UnicodeString(msg->name), m_currentGame->getIP(player), UnicodeString(msg->Chat.message), msg->Chat.chatType);
  554. break;
  555. }
  556. }
  557. }
  558. }
  559. void LANAPI::handleGameStart( LANMessage *msg, UnsignedInt senderIP )
  560. {
  561. if (!m_inLobby && m_currentGame && m_currentGame->getIP(0) == senderIP && !m_currentGame->isGameInProgress())
  562. {
  563. OnGameStart();
  564. }
  565. }
  566. void LANAPI::handleGameStartTimer( LANMessage *msg, UnsignedInt senderIP )
  567. {
  568. if (!m_inLobby && m_currentGame && m_currentGame->getIP(0) == senderIP && !m_currentGame->isGameInProgress())
  569. {
  570. OnGameStartTimer(msg->StartTimer.seconds);
  571. }
  572. }
  573. void LANAPI::handleGameOptions( LANMessage *msg, UnsignedInt senderIP )
  574. {
  575. if (!m_inLobby && m_currentGame && !m_currentGame->isGameInProgress())
  576. {
  577. int player;
  578. for (player = 0; player < MAX_SLOTS; ++player)
  579. {
  580. if (m_currentGame->getIP(player) == senderIP)
  581. {
  582. OnGameOptions(senderIP, player, AsciiString(msg->GameOptions.options));
  583. break;
  584. }
  585. }
  586. }
  587. }
  588. void LANAPI::handleInActive(LANMessage *msg, UnsignedInt senderIP) {
  589. if (m_inLobby || (m_currentGame == NULL) || (m_currentGame->isGameInProgress())) {
  590. return;
  591. }
  592. // check to see if we are the host of this game.
  593. if (m_currentGame->amIHost() == FALSE) {
  594. return;
  595. }
  596. UnicodeString playerName;
  597. playerName = msg->name;
  598. Int slotNum = m_currentGame->getSlotNum(playerName);
  599. if (slotNum < 0)
  600. return;
  601. GameSlot *slot = m_currentGame->getSlot(slotNum);
  602. if (slot == NULL) {
  603. return;
  604. }
  605. if (senderIP != slot->getIP()) {
  606. return;
  607. }
  608. // don't want to unaccept the host, that's silly. They can't hit start alt-tabbed anyways.
  609. if (senderIP == TheLAN->GetLocalIP()) {
  610. return;
  611. }
  612. // only unaccept if the timer hasn't started yet.
  613. if (m_gameStartTime != 0) {
  614. return;
  615. }
  616. slot->unAccept();
  617. AsciiString options = GenerateGameOptionsString();
  618. RequestGameOptions(options, FALSE);
  619. lanUpdateSlotList();
  620. }