LadderDefs.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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. // FILE: LadderDefs.cpp //////////////////////////////////////////////////////
  24. // Generals ladder code
  25. // Author: Matthew D. Campbell, August 2002
  26. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  27. #include "GameNetwork/GameSpy/ThreadUtils.h"
  28. #include "GameNetwork/GameSpy/LadderDefs.h"
  29. #include "GameNetwork/GameSpy/PeerDefs.h"
  30. #include "GameNetwork/GameSpy/GSConfig.h"
  31. #include "Common/GameState.h"
  32. #include "Common/File.h"
  33. #include "Common/FileSystem.h"
  34. #include "Common/PlayerTemplate.h"
  35. #include "GameClient/GameText.h"
  36. #include "GameClient/MapUtil.h"
  37. #ifdef _INTERNAL
  38. // for occasional debugging...
  39. //#pragma optimize("", off)
  40. //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
  41. #endif
  42. LadderList *TheLadderList = NULL;
  43. LadderInfo::LadderInfo()
  44. {
  45. playersPerTeam = 1;
  46. minWins = 0;
  47. maxWins = 0;
  48. randomMaps = TRUE;
  49. randomFactions = TRUE;
  50. validQM = TRUE;
  51. validCustom = FALSE;
  52. port = 0;
  53. submitReplay = FALSE;
  54. index = -1;
  55. }
  56. static LadderInfo *parseLadder(AsciiString raw)
  57. {
  58. DEBUG_LOG(("Looking at ladder:\n%s\n", raw.str()));
  59. LadderInfo *lad = NULL;
  60. AsciiString line;
  61. while (raw.nextToken(&line, "\n"))
  62. {
  63. if (line.getCharAt(line.getLength()-1) == '\r')
  64. line.removeLastChar(); // there is a trailing '\r'
  65. line.trim();
  66. if (line.isEmpty())
  67. continue;
  68. // woohoo! got a line!
  69. line.trim();
  70. if ( !lad && line.startsWith("<Ladder ") )
  71. {
  72. // start of a ladder def
  73. lad = NEW LadderInfo;
  74. // fill in some info
  75. AsciiString tokenName, tokenAddr, tokenPort, tokenHomepage;
  76. line.removeLastChar(); // the '>'
  77. line = line.str() + 7; // the "<Ladder "
  78. line.nextToken(&tokenAddr, "\" ");
  79. line.nextToken(&tokenPort, " ");
  80. line.nextToken(&tokenHomepage, " ");
  81. lad->name = MultiByteToWideCharSingleLine(tokenName.str()).c_str();
  82. while (lad->name.getLength() > 20)
  83. lad->name.removeLastChar(); // Per Harvard's request, ladder names are limited to 20 chars
  84. lad->address = tokenAddr;
  85. lad->port = atoi(tokenPort.str());
  86. lad->homepageURL = tokenHomepage;
  87. }
  88. else if ( lad && line.startsWith("Name ") )
  89. {
  90. lad->name = MultiByteToWideCharSingleLine(line.str() + 5).c_str();
  91. }
  92. else if ( lad && line.startsWith("Desc ") )
  93. {
  94. lad->description = MultiByteToWideCharSingleLine(line.str() + 5).c_str();
  95. }
  96. else if ( lad && line.startsWith("Loc ") )
  97. {
  98. lad->location = MultiByteToWideCharSingleLine(line.str() + 4).c_str();
  99. }
  100. else if ( lad && line.startsWith("TeamSize ") )
  101. {
  102. lad->playersPerTeam = atoi(line.str() + 9);
  103. }
  104. else if ( lad && line.startsWith("RandomMaps ") )
  105. {
  106. lad->randomMaps = atoi(line.str() + 11);
  107. }
  108. else if ( lad && line.startsWith("RandomFactions ") )
  109. {
  110. lad->randomFactions = atoi(line.str() + 15);
  111. }
  112. else if ( lad && line.startsWith("Faction ") )
  113. {
  114. AsciiString faction = line.str() + 8;
  115. AsciiStringList outStringList;
  116. ThePlayerTemplateStore->getAllSideStrings(&outStringList);
  117. AsciiStringList::iterator aIt = std::find(outStringList.begin(), outStringList.end(), faction);
  118. if (aIt != outStringList.end())
  119. {
  120. // valid faction - now check for dupes
  121. aIt = std::find(lad->validFactions.begin(), lad->validFactions.end(), faction);
  122. if (aIt == lad->validFactions.end())
  123. {
  124. lad->validFactions.push_back(faction);
  125. }
  126. }
  127. }
  128. /*
  129. else if ( lad && line.startsWith("QM ") )
  130. {
  131. lad->validQM = atoi(line.str() + 3);
  132. }
  133. else if ( lad && line.startsWith("Custom ") )
  134. {
  135. lad->validCustom = atoi(line.str() + 7);
  136. }
  137. */
  138. else if ( lad && line.startsWith("MinWins ") )
  139. {
  140. lad->minWins = atoi(line.str() + 8);
  141. }
  142. else if ( lad && line.startsWith("MaxWins ") )
  143. {
  144. lad->maxWins = atoi(line.str() + 8);
  145. }
  146. else if ( lad && line.startsWith("CryptedPass ") )
  147. {
  148. lad->cryptedPassword = line.str() + 12;
  149. }
  150. else if ( lad && line.compare("</Ladder>") == 0 )
  151. {
  152. DEBUG_LOG(("Saw a ladder: name=%ls, addr=%s:%d, players=%dv%d, pass=%s, replay=%d, homepage=%s\n",
  153. lad->name.str(), lad->address.str(), lad->port, lad->playersPerTeam, lad->playersPerTeam, lad->cryptedPassword.str(),
  154. lad->submitReplay, lad->homepageURL.str()));
  155. // end of a ladder
  156. if (lad->playersPerTeam >= 1 && lad->playersPerTeam <= MAX_SLOTS/2)
  157. {
  158. if (lad->validFactions.size() == 0)
  159. {
  160. DEBUG_LOG(("No factions specified. Using all.\n"));
  161. lad->validFactions.clear();
  162. Int numTemplates = ThePlayerTemplateStore->getPlayerTemplateCount();
  163. for ( Int i = 0; i < numTemplates; ++i )
  164. {
  165. const PlayerTemplate *pt = ThePlayerTemplateStore->getNthPlayerTemplate(i);
  166. if (!pt)
  167. continue;
  168. if (pt->isPlayableSide() && pt->getSide().compare("Boss") != 0 )
  169. lad->validFactions.push_back(pt->getSide());
  170. }
  171. }
  172. else
  173. {
  174. AsciiStringList validFactions = lad->validFactions;
  175. for (AsciiStringListIterator it = validFactions.begin(); it != validFactions.end(); ++it)
  176. {
  177. AsciiString faction = *it;
  178. AsciiString marker;
  179. marker.format("INI:Faction%s", faction.str());
  180. DEBUG_LOG(("Faction %s has marker %s corresponding to str %ls\n", faction.str(), marker.str(), TheGameText->fetch(marker).str()));
  181. }
  182. }
  183. if (lad->validMaps.size() == 0)
  184. {
  185. DEBUG_LOG(("No maps specified. Using all.\n"));
  186. std::list<AsciiString> qmMaps = TheGameSpyConfig->getQMMaps();
  187. for (std::list<AsciiString>::const_iterator it = qmMaps.begin(); it != qmMaps.end(); ++it)
  188. {
  189. AsciiString mapName = *it;
  190. // check sizes on the maps before allowing them
  191. const MapMetaData *md = TheMapCache->findMap(mapName);
  192. if (md && md->m_numPlayers >= lad->playersPerTeam*2)
  193. {
  194. lad->validMaps.push_back(mapName);
  195. }
  196. }
  197. }
  198. return lad;
  199. }
  200. else
  201. {
  202. // no maps? don't play on it!
  203. delete lad;
  204. lad = NULL;
  205. return NULL;
  206. }
  207. }
  208. else if ( lad && line.startsWith("Map ") )
  209. {
  210. // valid map
  211. AsciiString mapName = line.str() + 4;
  212. mapName.trim();
  213. if (mapName.isNotEmpty())
  214. {
  215. mapName.format("%s\\%s\\%s.map", TheMapCache->getMapDir().str(), mapName.str(), mapName.str());
  216. mapName = TheGameState->portableMapPathToRealMapPath(TheGameState->realMapPathToPortableMapPath(mapName));
  217. mapName.toLower();
  218. std::list<AsciiString> qmMaps = TheGameSpyConfig->getQMMaps();
  219. if (std::find(qmMaps.begin(), qmMaps.end(), mapName) != qmMaps.end())
  220. {
  221. // check sizes on the maps before allowing them
  222. const MapMetaData *md = TheMapCache->findMap(mapName);
  223. if (md && md->m_numPlayers >= lad->playersPerTeam*2)
  224. lad->validMaps.push_back(mapName);
  225. }
  226. }
  227. }
  228. else
  229. {
  230. // bad ladder - kill it
  231. delete lad;
  232. lad = NULL;
  233. }
  234. }
  235. if (lad)
  236. {
  237. delete lad;
  238. lad = NULL;
  239. }
  240. return NULL;
  241. }
  242. LadderList::LadderList()
  243. {
  244. //Int profile = TheGameSpyInfo->getLocalProfileID();
  245. AsciiString rawMotd = TheGameSpyConfig->getLeftoverConfig();
  246. AsciiString line;
  247. Bool inLadders = FALSE;
  248. Bool inSpecialLadders = FALSE;
  249. Bool inLadder = FALSE;
  250. LadderInfo *lad = NULL;
  251. Int index = 1;
  252. AsciiString rawLadder;
  253. while (rawMotd.nextToken(&line, "\n"))
  254. {
  255. if (line.getCharAt(line.getLength()-1) == '\r')
  256. line.removeLastChar(); // there is a trailing '\r'
  257. line.trim();
  258. if (line.isEmpty())
  259. continue;
  260. if (!inLadders && line.compare("<Ladders>") == 0)
  261. {
  262. inLadders = TRUE;
  263. rawLadder.clear();
  264. }
  265. else if (inLadders && line.compare("</Ladders>") == 0)
  266. {
  267. inLadders = FALSE;
  268. }
  269. else if (!inSpecialLadders && line.compare("<SpecialLadders>") == 0)
  270. {
  271. inSpecialLadders = TRUE;
  272. rawLadder.clear();
  273. }
  274. else if (inSpecialLadders && line.compare("</SpecialLadders>") == 0)
  275. {
  276. inSpecialLadders = FALSE;
  277. }
  278. else if (inLadders || inSpecialLadders)
  279. {
  280. if (line.startsWith("<Ladder ") && !inLadder)
  281. {
  282. inLadder = TRUE;
  283. rawLadder.clear();
  284. rawLadder.concat(line);
  285. rawLadder.concat('\n');
  286. }
  287. else if (line.compare("</Ladder>") == 0 && inLadder)
  288. {
  289. inLadder = FALSE;
  290. rawLadder.concat(line);
  291. rawLadder.concat('\n');
  292. if ((lad = parseLadder(rawLadder)) != NULL)
  293. {
  294. lad->index = index++;
  295. if (inLadders)
  296. {
  297. DEBUG_LOG(("Adding to standard ladders\n"));
  298. m_standardLadders.push_back(lad);
  299. }
  300. else
  301. {
  302. DEBUG_LOG(("Adding to special ladders\n"));
  303. m_specialLadders.push_back(lad);
  304. }
  305. }
  306. rawLadder.clear();
  307. }
  308. else if (inLadder)
  309. {
  310. rawLadder.concat(line);
  311. rawLadder.concat('\n');
  312. }
  313. }
  314. }
  315. // look for local ladders
  316. loadLocalLadders();
  317. DEBUG_LOG(("After looking for ladders, we have %d local, %d special && %d normal\n", m_localLadders.size(), m_specialLadders.size(), m_standardLadders.size()));
  318. }
  319. LadderList::~LadderList()
  320. {
  321. LadderInfoList::iterator it;
  322. for (it = m_specialLadders.begin(); it != m_specialLadders.end(); it = m_specialLadders.begin())
  323. {
  324. delete *it;
  325. m_specialLadders.pop_front();
  326. }
  327. for (it = m_standardLadders.begin(); it != m_standardLadders.end(); it = m_standardLadders.begin())
  328. {
  329. delete *it;
  330. m_standardLadders.pop_front();
  331. }
  332. for (it = m_localLadders.begin(); it != m_localLadders.end(); it = m_localLadders.begin())
  333. {
  334. delete *it;
  335. m_localLadders.pop_front();
  336. }
  337. }
  338. const LadderInfo* LadderList::findLadder( const AsciiString& addr, UnsignedShort port )
  339. {
  340. LadderInfoList::const_iterator cit;
  341. for (cit = m_specialLadders.begin(); cit != m_specialLadders.end(); ++cit)
  342. {
  343. const LadderInfo *li = *cit;
  344. if (li->address == addr && li->port == port)
  345. {
  346. return li;
  347. }
  348. }
  349. for (cit = m_standardLadders.begin(); cit != m_standardLadders.end(); ++cit)
  350. {
  351. const LadderInfo *li = *cit;
  352. if (li->address == addr && li->port == port)
  353. {
  354. return li;
  355. }
  356. }
  357. for (cit = m_localLadders.begin(); cit != m_localLadders.end(); ++cit)
  358. {
  359. const LadderInfo *li = *cit;
  360. if (li->address == addr && li->port == port)
  361. {
  362. return li;
  363. }
  364. }
  365. return NULL;
  366. }
  367. const LadderInfo* LadderList::findLadderByIndex( Int index )
  368. {
  369. if (index == 0)
  370. return NULL;
  371. LadderInfoList::const_iterator cit;
  372. for (cit = m_specialLadders.begin(); cit != m_specialLadders.end(); ++cit)
  373. {
  374. const LadderInfo *li = *cit;
  375. if (li->index == index)
  376. {
  377. return li;
  378. }
  379. }
  380. for (cit = m_standardLadders.begin(); cit != m_standardLadders.end(); ++cit)
  381. {
  382. const LadderInfo *li = *cit;
  383. if (li->index == index)
  384. {
  385. return li;
  386. }
  387. }
  388. for (cit = m_localLadders.begin(); cit != m_localLadders.end(); ++cit)
  389. {
  390. const LadderInfo *li = *cit;
  391. if (li->index == index)
  392. {
  393. return li;
  394. }
  395. }
  396. return NULL;
  397. }
  398. const LadderInfoList* LadderList::getSpecialLadders( void )
  399. {
  400. return &m_specialLadders;
  401. }
  402. const LadderInfoList* LadderList::getStandardLadders( void )
  403. {
  404. return &m_standardLadders;
  405. }
  406. const LadderInfoList* LadderList::getLocalLadders( void )
  407. {
  408. return &m_localLadders;
  409. }
  410. void LadderList::loadLocalLadders( void )
  411. {
  412. AsciiString dirname;
  413. dirname.format("%sGeneralsOnline\\Ladders\\", TheGlobalData->getPath_UserData().str());
  414. FilenameList filenameList;
  415. TheFileSystem->getFileListInDirectory(dirname, AsciiString("*.ini"), filenameList, TRUE);
  416. Int index = -1;
  417. FilenameList::iterator it = filenameList.begin();
  418. while (it != filenameList.end())
  419. {
  420. AsciiString filename = *it;
  421. DEBUG_LOG(("Looking at possible ladder info file '%s'\n", filename.str()));
  422. filename.toLower();
  423. checkLadder( filename, index-- );
  424. ++it;
  425. }
  426. }
  427. void LadderList::checkLadder( AsciiString fname, Int index )
  428. {
  429. File *fp = TheFileSystem->openFile(fname.str(), File::READ | File::TEXT);
  430. char buf[1024];
  431. AsciiString rawData;
  432. if (fp)
  433. {
  434. Int len;
  435. while (!fp->eof())
  436. {
  437. len = fp->read(buf, 1023);
  438. buf[len] = 0;
  439. buf[1023] = 0;
  440. rawData.concat(buf);
  441. }
  442. fp->close();
  443. fp = NULL;
  444. }
  445. DEBUG_LOG(("Read %d bytes from '%s'\n", rawData.getLength(), fname.str()));
  446. if (rawData.isEmpty())
  447. return;
  448. LadderInfo *li = parseLadder(rawData);
  449. if (!li)
  450. {
  451. return;
  452. }
  453. // sanity check
  454. if (li->address.isEmpty())
  455. {
  456. DEBUG_LOG(("Bailing because of li->address.isEmpty()\n"));
  457. delete li;
  458. return;
  459. }
  460. if (!li->port)
  461. {
  462. DEBUG_LOG(("Bailing because of !li->port\n"));
  463. delete li;
  464. return;
  465. }
  466. if (li->validMaps.size() == 0)
  467. {
  468. DEBUG_LOG(("Bailing because of li->validMaps.size() == 0\n"));
  469. delete li;
  470. return;
  471. }
  472. li->index = index;
  473. // ladders are QM-only at this point, which kinda invalidates the whole concept of local ladders. Oh well.
  474. li->validQM = FALSE; // no local ladders in QM
  475. li->validCustom = FALSE;
  476. //for (Int i=0; i<4; ++i)
  477. // fname.removeLastChar(); // remove .lad
  478. //li->name = UnicodeString(MultiByteToWideCharSingleLine(fname.reverseFind('\\')+1).c_str());
  479. DEBUG_LOG(("Adding local ladder %ls\n", li->name.str()));
  480. m_localLadders.push_back(li);
  481. }