LANAPIhandlers.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  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. ///////////////////////////////////////////////////////////////////////////////////////
  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. #if defined(_DEBUG) || defined(_INTERNAL)
  230. }
  231. #endif
  232. // check for a duplicate serial
  233. AsciiString s;
  234. for (player = 0; canJoin && player<MAX_SLOTS; ++player)
  235. {
  236. LANGameSlot *slot = m_currentGame->getLANSlot(player);
  237. s.clear();
  238. if (player == 0)
  239. {
  240. GetStringFromRegistry("\\ergc", "", s);
  241. }
  242. else if (slot->isHuman())
  243. {
  244. s = slot->getSerial();
  245. if (s.isEmpty())
  246. s = "<Munkee>";
  247. }
  248. if (s.isNotEmpty())
  249. {
  250. DEBUG_LOG(("Checking serial '%s' in slot %d\n", s.str(), player));
  251. if (!strncmp(s.str(), msg->GameToJoin.serial, g_maxSerialLength))
  252. {
  253. // serials match! kick the punk!
  254. reply.LANMessageType = LANMessage::MSG_JOIN_DENY;
  255. reply.GameNotJoined.reason = LANAPIInterface::RET_SERIAL_DUPE;
  256. reply.GameNotJoined.gameIP = m_localIP;
  257. reply.GameNotJoined.playerIP = senderIP;
  258. canJoin = false;
  259. DEBUG_LOG(("LANAPI::handleRequestJoin - join denied because of duplicate serial # (%s).\n", s.str()));
  260. break;
  261. }
  262. }
  263. }
  264. // We're the host, so see if he has a duplicate name
  265. for (player = 0; canJoin && player<MAX_SLOTS; ++player)
  266. {
  267. LANGameSlot *slot = m_currentGame->getLANSlot(player);
  268. if (slot->isHuman() && slot->getName().compare(msg->name) == 0)
  269. {
  270. // just deny duplicates
  271. reply.LANMessageType = LANMessage::MSG_JOIN_DENY;
  272. reply.GameNotJoined.reason = LANAPIInterface::RET_DUPLICATE_NAME;
  273. reply.GameNotJoined.gameIP = m_localIP;
  274. reply.GameNotJoined.playerIP = senderIP;
  275. canJoin = false;
  276. DEBUG_LOG(("LANAPI::handleRequestJoin - join denied because of duplicate names.\n"));
  277. break;
  278. }
  279. }
  280. // See if there's room
  281. // First get the number of players currently in the room.
  282. Int numPlayers = 0;
  283. for (player = 0; player < MAX_SLOTS; ++player)
  284. {
  285. if (m_currentGame->getLANSlot(player)->isOccupied()
  286. && !(m_currentGame->getLANSlot(player)->getPlayerTemplate() == PLAYERTEMPLATE_OBSERVER))
  287. {
  288. ++numPlayers;
  289. }
  290. }
  291. // now get the number of starting spots on the map.
  292. Int numStartingSpots = MAX_SLOTS;
  293. const MapMetaData *md = TheMapCache->findMap(m_currentGame->getMap());
  294. if (md != NULL)
  295. {
  296. numStartingSpots = md->m_numPlayers;
  297. }
  298. if (numPlayers < numStartingSpots) {
  299. for (player = 0; canJoin && player<MAX_SLOTS; ++player)
  300. {
  301. if (m_currentGame->getLANSlot(player)->isOpen())
  302. {
  303. // OK, add him in.
  304. reply.LANMessageType = LANMessage::MSG_JOIN_ACCEPT;
  305. wcsncpy(reply.GameJoined.gameName, m_currentGame->getName().str(), g_lanGameNameLength);
  306. reply.GameJoined.gameName[g_lanGameNameLength] = 0;
  307. reply.GameJoined.slotPosition = player;
  308. reply.GameJoined.gameIP = m_localIP;
  309. reply.GameJoined.playerIP = senderIP;
  310. LANGameSlot newSlot;
  311. newSlot.setState(SLOT_PLAYER, UnicodeString(msg->name));
  312. newSlot.setIP(senderIP);
  313. newSlot.setPort(NETWORK_BASE_PORT_NUMBER);
  314. newSlot.setLastHeard(timeGetTime());
  315. newSlot.setSerial(msg->GameToJoin.serial);
  316. m_currentGame->setSlot(player,newSlot);
  317. DEBUG_LOG(("LANAPI::handleRequestJoin - added player %ls at ip 0x%08x to the game\n", msg->name, senderIP));
  318. OnPlayerJoin(player, UnicodeString(msg->name));
  319. responseIP = 0;
  320. break;
  321. }
  322. }
  323. }
  324. if (canJoin && player == MAX_SLOTS)
  325. {
  326. reply.LANMessageType = LANMessage::MSG_JOIN_DENY;
  327. wcsncpy(reply.GameNotJoined.gameName, m_currentGame->getName().str(), g_lanGameNameLength);
  328. reply.GameNotJoined.gameName[g_lanGameNameLength] = 0;
  329. reply.GameNotJoined.reason = LANAPIInterface::RET_GAME_FULL;
  330. reply.GameNotJoined.gameIP = m_localIP;
  331. reply.GameNotJoined.playerIP = senderIP;
  332. DEBUG_LOG(("LANAPI::handleRequestJoin - join denied because game is full.\n"));
  333. }
  334. }
  335. }
  336. else
  337. {
  338. reply.LANMessageType = LANMessage::MSG_JOIN_DENY;
  339. reply.GameNotJoined.reason = LANAPIInterface::RET_GAME_GONE;
  340. reply.GameNotJoined.gameIP = m_localIP;
  341. reply.GameNotJoined.playerIP = senderIP;
  342. }
  343. sendMessage(&reply, responseIP);
  344. RequestGameOptions(GenerateGameOptionsString(), true);
  345. }
  346. void LANAPI::handleJoinAccept( LANMessage *msg, UnsignedInt senderIP )
  347. {
  348. if (msg->GameJoined.playerIP == m_localIP) // Is it for us?
  349. {
  350. if (m_pendingAction == ACT_JOIN) // Are we trying to join?
  351. {
  352. m_currentGame = LookupGame(UnicodeString(msg->GameJoined.gameName));
  353. if (!m_currentGame)
  354. {
  355. DEBUG_ASSERTCRASH(false, ("Could not find game to join!"));
  356. OnGameJoin(RET_UNKNOWN, NULL);
  357. }
  358. else
  359. {
  360. m_inLobby = false;
  361. AsciiString options = GameInfoToAsciiString(m_currentGame);
  362. m_currentGame->enterGame();
  363. ParseAsciiStringToGameInfo(m_currentGame, options);
  364. Int pos = msg->GameJoined.slotPosition;
  365. LANGameSlot slot;
  366. slot.setState(SLOT_PLAYER, m_name);
  367. slot.setIP(m_localIP);
  368. slot.setPort(NETWORK_BASE_PORT_NUMBER);
  369. slot.setLastHeard(0);
  370. slot.setLogin(m_userName);
  371. slot.setHost(m_hostName);
  372. m_currentGame->setSlot(pos, slot);
  373. m_currentGame->getLANSlot(0)->setHost(msg->hostName);
  374. m_currentGame->getLANSlot(0)->setLogin(msg->userName);
  375. LANPreferences prefs;
  376. AsciiString entry;
  377. entry.format("%d.%d.%d.%d:%s", senderIP >> 24, (senderIP & 0xff0000) >> 16, (senderIP & 0xff00) >> 8, senderIP & 0xff, UnicodeStringToQuotedPrintable(m_currentGame->getSlot(0)->getName()).str());
  378. prefs["RemoteIP0"] = entry;
  379. prefs.write();
  380. OnGameJoin(RET_OK, m_currentGame);
  381. //DEBUG_ASSERTCRASH(false, ("setting host to %ls@%ls\n", m_currentGame->getLANSlot(0)->getUser()->getLogin().str(),
  382. // m_currentGame->getLANSlot(0)->getUser()->getHost().str()));
  383. }
  384. m_pendingAction = ACT_NONE;
  385. m_expiration = 0;
  386. }
  387. }
  388. }
  389. void LANAPI::handleJoinDeny( LANMessage *msg, UnsignedInt senderIP )
  390. {
  391. if (msg->GameJoined.playerIP == m_localIP) // Is it for us?
  392. {
  393. if (m_pendingAction == ACT_JOIN) // Are we trying to join?
  394. {
  395. OnGameJoin(msg->GameNotJoined.reason, LookupGame(UnicodeString(msg->GameNotJoined.gameName)));
  396. m_pendingAction = ACT_NONE;
  397. m_expiration = 0;
  398. }
  399. }
  400. }
  401. void LANAPI::handleRequestGameLeave( LANMessage *msg, UnsignedInt senderIP )
  402. {
  403. if (!m_inLobby && m_currentGame && !m_currentGame->isGameInProgress())
  404. {
  405. int player;
  406. for (player = 0; player < MAX_SLOTS; ++player)
  407. {
  408. if (m_currentGame->getIP(player) == senderIP)
  409. {
  410. if (player == 0)
  411. {
  412. OnHostLeave();
  413. removeGame(m_currentGame);
  414. delete m_currentGame;
  415. m_currentGame = NULL;
  416. /// @todo re-add myself to lobby? Or just keep me there all the time? If we send a LOBBY_ANNOUNCE things'll work out...
  417. LANPlayer *lanPlayer = LookupPlayer(m_localIP);
  418. if (!lanPlayer)
  419. {
  420. lanPlayer = NEW LANPlayer;
  421. lanPlayer->setIP(m_localIP);
  422. }
  423. else
  424. {
  425. removePlayer(lanPlayer);
  426. }
  427. lanPlayer->setName(UnicodeString(m_name));
  428. lanPlayer->setHost(m_hostName);
  429. lanPlayer->setLogin(m_userName);
  430. lanPlayer->setLastHeard(timeGetTime());
  431. addPlayer(lanPlayer);
  432. }
  433. else
  434. {
  435. if (AmIHost())
  436. {
  437. // remove the deadbeat
  438. LANGameSlot slot;
  439. slot.setState(SLOT_OPEN);
  440. m_currentGame->setSlot( player, slot );
  441. }
  442. OnPlayerLeave(UnicodeString(msg->name));
  443. m_currentGame->getLANSlot(player)->setState(SLOT_OPEN);
  444. m_currentGame->resetAccepted();
  445. RequestGameOptions(GenerateGameOptionsString(), false, senderIP);
  446. //m_currentGame->endGame();
  447. }
  448. break;
  449. }
  450. DEBUG_ASSERTCRASH(player < MAX_SLOTS, ("Didn't find player!"));
  451. }
  452. }
  453. else if (m_inLobby)
  454. {
  455. // Look for dissappearing games
  456. LANGameInfo *game = m_games;
  457. while (game)
  458. {
  459. if (game->getName().compare(msg->GameToLeave.gameName) == 0)
  460. {
  461. removeGame(game);
  462. delete game;
  463. OnGameList(m_games);
  464. break;
  465. }
  466. game = game->getNext();
  467. }
  468. }
  469. }
  470. void LANAPI::handleRequestLobbyLeave( LANMessage *msg, UnsignedInt senderIP )
  471. {
  472. if (m_inLobby)
  473. {
  474. LANPlayer *player = m_lobbyPlayers;
  475. while (player)
  476. {
  477. if (player->getIP() == senderIP)
  478. {
  479. removePlayer(player);
  480. OnPlayerList(m_lobbyPlayers);
  481. break;
  482. }
  483. player = player->getNext();
  484. }
  485. }
  486. }
  487. void LANAPI::handleSetAccept( LANMessage *msg, UnsignedInt senderIP )
  488. {
  489. if (!m_inLobby && m_currentGame && !m_currentGame->isGameInProgress())
  490. {
  491. int player;
  492. for (player = 0; player < MAX_SLOTS; ++player)
  493. {
  494. if (m_currentGame->getIP(player) == senderIP)
  495. {
  496. OnAccept(senderIP, msg->Accept.isAccepted);
  497. break;
  498. }
  499. }
  500. }
  501. }
  502. void LANAPI::handleHasMap( LANMessage *msg, UnsignedInt senderIP )
  503. {
  504. if (!m_inLobby && m_currentGame)
  505. {
  506. CRC mapNameCRC;
  507. // mapNameCRC.computeCRC(m_currentGame->getMap().str(), m_currentGame->getMap().getLength());
  508. AsciiString portableMapName = TheGameState->realMapPathToPortableMapPath(m_currentGame->getMap());
  509. mapNameCRC.computeCRC(portableMapName.str(), portableMapName.getLength());
  510. if (mapNameCRC.get() != msg->MapStatus.mapCRC)
  511. {
  512. return;
  513. }
  514. int player;
  515. for (player = 0; player < MAX_SLOTS; ++player)
  516. {
  517. if (m_currentGame->getIP(player) == senderIP)
  518. {
  519. OnHasMap(senderIP, msg->MapStatus.hasMap);
  520. break;
  521. }
  522. }
  523. }
  524. }
  525. void LANAPI::handleChat( LANMessage *msg, UnsignedInt senderIP )
  526. {
  527. if (m_inLobby)
  528. {
  529. LANPlayer *player;
  530. if((player=LookupPlayer(senderIP)) != 0)
  531. {
  532. OnChat(UnicodeString(player->getName()), player->getIP(), UnicodeString(msg->Chat.message), msg->Chat.chatType);
  533. player->setLastHeard(timeGetTime());
  534. }
  535. }
  536. else
  537. {
  538. if (LookupGame(UnicodeString(msg->Chat.gameName)) != m_currentGame)
  539. {
  540. DEBUG_LOG(("Game '%ls' is not my game\n", msg->Chat.gameName));
  541. if (m_currentGame)
  542. {
  543. DEBUG_LOG(("Current game is '%ls'\n", m_currentGame->getName().str()));
  544. }
  545. return;
  546. }
  547. int player;
  548. for (player = 0; player < MAX_SLOTS; ++player)
  549. {
  550. if (m_currentGame && m_currentGame->getIP(player) == senderIP)
  551. {
  552. OnChat(UnicodeString(msg->name), m_currentGame->getIP(player), UnicodeString(msg->Chat.message), msg->Chat.chatType);
  553. break;
  554. }
  555. }
  556. }
  557. }
  558. void LANAPI::handleGameStart( LANMessage *msg, UnsignedInt senderIP )
  559. {
  560. if (!m_inLobby && m_currentGame && m_currentGame->getIP(0) == senderIP && !m_currentGame->isGameInProgress())
  561. {
  562. OnGameStart();
  563. }
  564. }
  565. void LANAPI::handleGameStartTimer( LANMessage *msg, UnsignedInt senderIP )
  566. {
  567. if (!m_inLobby && m_currentGame && m_currentGame->getIP(0) == senderIP && !m_currentGame->isGameInProgress())
  568. {
  569. OnGameStartTimer(msg->StartTimer.seconds);
  570. }
  571. }
  572. void LANAPI::handleGameOptions( LANMessage *msg, UnsignedInt senderIP )
  573. {
  574. if (!m_inLobby && m_currentGame && !m_currentGame->isGameInProgress())
  575. {
  576. int player;
  577. for (player = 0; player < MAX_SLOTS; ++player)
  578. {
  579. if (m_currentGame->getIP(player) == senderIP)
  580. {
  581. OnGameOptions(senderIP, player, AsciiString(msg->GameOptions.options));
  582. break;
  583. }
  584. }
  585. }
  586. }
  587. void LANAPI::handleInActive(LANMessage *msg, UnsignedInt senderIP) {
  588. if (m_inLobby || (m_currentGame == NULL) || (m_currentGame->isGameInProgress())) {
  589. return;
  590. }
  591. // check to see if we are the host of this game.
  592. if (m_currentGame->amIHost() == FALSE) {
  593. return;
  594. }
  595. UnicodeString playerName;
  596. playerName = msg->name;
  597. Int slotNum = m_currentGame->getSlotNum(playerName);
  598. if (slotNum < 0)
  599. return;
  600. GameSlot *slot = m_currentGame->getSlot(slotNum);
  601. if (slot == NULL) {
  602. return;
  603. }
  604. if (senderIP != slot->getIP()) {
  605. return;
  606. }
  607. // don't want to unaccept the host, that's silly. They can't hit start alt-tabbed anyways.
  608. if (senderIP == TheLAN->GetLocalIP()) {
  609. return;
  610. }
  611. // only unaccept if the timer hasn't started yet.
  612. if (m_gameStartTime != 0) {
  613. return;
  614. }
  615. slot->unAccept();
  616. AsciiString options = GenerateGameOptionsString();
  617. RequestGameOptions(options, FALSE);
  618. lanUpdateSlotList();
  619. }