LadderDefs.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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: 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.push_back("America");
  162. lad->validFactions.push_back("China");
  163. lad->validFactions.push_back("GLA");
  164. }
  165. else
  166. {
  167. AsciiStringList validFactions = lad->validFactions;
  168. for (AsciiStringListIterator it = validFactions.begin(); it != validFactions.end(); ++it)
  169. {
  170. AsciiString faction = *it;
  171. AsciiString marker;
  172. marker.format("INI:Faction%s", faction.str());
  173. DEBUG_LOG(("Faction %s has marker %s corresponding to str %ls\n", faction.str(), marker.str(), TheGameText->fetch(marker).str()));
  174. }
  175. }
  176. if (lad->validMaps.size() == 0)
  177. {
  178. DEBUG_LOG(("No maps specified. Using all.\n"));
  179. std::list<AsciiString> qmMaps = TheGameSpyConfig->getQMMaps();
  180. for (std::list<AsciiString>::const_iterator it = qmMaps.begin(); it != qmMaps.end(); ++it)
  181. {
  182. AsciiString mapName = *it;
  183. // check sizes on the maps before allowing them
  184. const MapMetaData *md = TheMapCache->findMap(mapName);
  185. if (md && md->m_numPlayers >= lad->playersPerTeam*2)
  186. {
  187. lad->validMaps.push_back(mapName);
  188. }
  189. }
  190. }
  191. return lad;
  192. }
  193. else
  194. {
  195. // no maps? don't play on it!
  196. delete lad;
  197. lad = NULL;
  198. return NULL;
  199. }
  200. }
  201. else if ( lad && line.startsWith("Map ") )
  202. {
  203. // valid map
  204. AsciiString mapName = line.str() + 4;
  205. mapName.trim();
  206. if (mapName.isNotEmpty())
  207. {
  208. mapName.format("%s\\%s\\%s.map", TheMapCache->getMapDir().str(), mapName.str(), mapName.str());
  209. mapName = TheGameState->portableMapPathToRealMapPath(TheGameState->realMapPathToPortableMapPath(mapName));
  210. mapName.toLower();
  211. std::list<AsciiString> qmMaps = TheGameSpyConfig->getQMMaps();
  212. if (std::find(qmMaps.begin(), qmMaps.end(), mapName) != qmMaps.end())
  213. {
  214. // check sizes on the maps before allowing them
  215. const MapMetaData *md = TheMapCache->findMap(mapName);
  216. if (md && md->m_numPlayers >= lad->playersPerTeam*2)
  217. lad->validMaps.push_back(mapName);
  218. }
  219. }
  220. }
  221. else
  222. {
  223. // bad ladder - kill it
  224. delete lad;
  225. lad = NULL;
  226. }
  227. }
  228. if (lad)
  229. {
  230. delete lad;
  231. lad = NULL;
  232. }
  233. return NULL;
  234. }
  235. LadderList::LadderList()
  236. {
  237. //Int profile = TheGameSpyInfo->getLocalProfileID();
  238. AsciiString rawMotd = TheGameSpyConfig->getLeftoverConfig();
  239. AsciiString line;
  240. Bool inLadders = FALSE;
  241. Bool inSpecialLadders = FALSE;
  242. Bool inLadder = FALSE;
  243. LadderInfo *lad = NULL;
  244. Int index = 1;
  245. AsciiString rawLadder;
  246. while (rawMotd.nextToken(&line, "\n"))
  247. {
  248. if (line.getCharAt(line.getLength()-1) == '\r')
  249. line.removeLastChar(); // there is a trailing '\r'
  250. line.trim();
  251. if (line.isEmpty())
  252. continue;
  253. if (!inLadders && line.compare("<Ladders>") == 0)
  254. {
  255. inLadders = TRUE;
  256. rawLadder.clear();
  257. }
  258. else if (inLadders && line.compare("</Ladders>") == 0)
  259. {
  260. inLadders = FALSE;
  261. }
  262. else if (!inSpecialLadders && line.compare("<SpecialLadders>") == 0)
  263. {
  264. inSpecialLadders = TRUE;
  265. rawLadder.clear();
  266. }
  267. else if (inSpecialLadders && line.compare("</SpecialLadders>") == 0)
  268. {
  269. inSpecialLadders = FALSE;
  270. }
  271. else if (inLadders || inSpecialLadders)
  272. {
  273. if (line.startsWith("<Ladder ") && !inLadder)
  274. {
  275. inLadder = TRUE;
  276. rawLadder.clear();
  277. rawLadder.concat(line);
  278. rawLadder.concat('\n');
  279. }
  280. else if (line.compare("</Ladder>") == 0 && inLadder)
  281. {
  282. inLadder = FALSE;
  283. rawLadder.concat(line);
  284. rawLadder.concat('\n');
  285. if ((lad = parseLadder(rawLadder)) != NULL)
  286. {
  287. lad->index = index++;
  288. if (inLadders)
  289. {
  290. DEBUG_LOG(("Adding to standard ladders\n"));
  291. m_standardLadders.push_back(lad);
  292. }
  293. else
  294. {
  295. DEBUG_LOG(("Adding to special ladders\n"));
  296. m_specialLadders.push_back(lad);
  297. }
  298. }
  299. rawLadder.clear();
  300. }
  301. else if (inLadder)
  302. {
  303. rawLadder.concat(line);
  304. rawLadder.concat('\n');
  305. }
  306. }
  307. }
  308. // look for local ladders
  309. loadLocalLadders();
  310. DEBUG_LOG(("After looking for ladders, we have %d local, %d special && %d normal\n", m_localLadders.size(), m_specialLadders.size(), m_standardLadders.size()));
  311. }
  312. LadderList::~LadderList()
  313. {
  314. LadderInfoList::iterator it;
  315. for (it = m_specialLadders.begin(); it != m_specialLadders.end(); it = m_specialLadders.begin())
  316. {
  317. delete *it;
  318. m_specialLadders.pop_front();
  319. }
  320. for (it = m_standardLadders.begin(); it != m_standardLadders.end(); it = m_standardLadders.begin())
  321. {
  322. delete *it;
  323. m_standardLadders.pop_front();
  324. }
  325. for (it = m_localLadders.begin(); it != m_localLadders.end(); it = m_localLadders.begin())
  326. {
  327. delete *it;
  328. m_localLadders.pop_front();
  329. }
  330. }
  331. const LadderInfo* LadderList::findLadder( const AsciiString& addr, UnsignedShort port )
  332. {
  333. LadderInfoList::const_iterator cit;
  334. for (cit = m_specialLadders.begin(); cit != m_specialLadders.end(); ++cit)
  335. {
  336. const LadderInfo *li = *cit;
  337. if (li->address == addr && li->port == port)
  338. {
  339. return li;
  340. }
  341. }
  342. for (cit = m_standardLadders.begin(); cit != m_standardLadders.end(); ++cit)
  343. {
  344. const LadderInfo *li = *cit;
  345. if (li->address == addr && li->port == port)
  346. {
  347. return li;
  348. }
  349. }
  350. for (cit = m_localLadders.begin(); cit != m_localLadders.end(); ++cit)
  351. {
  352. const LadderInfo *li = *cit;
  353. if (li->address == addr && li->port == port)
  354. {
  355. return li;
  356. }
  357. }
  358. return NULL;
  359. }
  360. const LadderInfo* LadderList::findLadderByIndex( Int index )
  361. {
  362. if (index == 0)
  363. return NULL;
  364. LadderInfoList::const_iterator cit;
  365. for (cit = m_specialLadders.begin(); cit != m_specialLadders.end(); ++cit)
  366. {
  367. const LadderInfo *li = *cit;
  368. if (li->index == index)
  369. {
  370. return li;
  371. }
  372. }
  373. for (cit = m_standardLadders.begin(); cit != m_standardLadders.end(); ++cit)
  374. {
  375. const LadderInfo *li = *cit;
  376. if (li->index == index)
  377. {
  378. return li;
  379. }
  380. }
  381. for (cit = m_localLadders.begin(); cit != m_localLadders.end(); ++cit)
  382. {
  383. const LadderInfo *li = *cit;
  384. if (li->index == index)
  385. {
  386. return li;
  387. }
  388. }
  389. return NULL;
  390. }
  391. const LadderInfoList* LadderList::getSpecialLadders( void )
  392. {
  393. return &m_specialLadders;
  394. }
  395. const LadderInfoList* LadderList::getStandardLadders( void )
  396. {
  397. return &m_standardLadders;
  398. }
  399. const LadderInfoList* LadderList::getLocalLadders( void )
  400. {
  401. return &m_localLadders;
  402. }
  403. void LadderList::loadLocalLadders( void )
  404. {
  405. AsciiString dirname;
  406. dirname.format("%sGeneralsOnline\\Ladders\\", TheGlobalData->getPath_UserData().str());
  407. FilenameList filenameList;
  408. TheFileSystem->getFileListInDirectory(dirname, AsciiString("*.ini"), filenameList, TRUE);
  409. Int index = -1;
  410. FilenameList::iterator it = filenameList.begin();
  411. while (it != filenameList.end())
  412. {
  413. AsciiString filename = *it;
  414. DEBUG_LOG(("Looking at possible ladder info file '%s'\n", filename.str()));
  415. filename.toLower();
  416. checkLadder( filename, index-- );
  417. ++it;
  418. }
  419. }
  420. void LadderList::checkLadder( AsciiString fname, Int index )
  421. {
  422. File *fp = TheFileSystem->openFile(fname.str(), File::READ | File::TEXT);
  423. char buf[1024];
  424. AsciiString rawData;
  425. if (fp)
  426. {
  427. Int len;
  428. while (!fp->eof())
  429. {
  430. len = fp->read(buf, 1023);
  431. buf[len] = 0;
  432. buf[1023] = 0;
  433. rawData.concat(buf);
  434. }
  435. fp->close();
  436. fp = NULL;
  437. }
  438. DEBUG_LOG(("Read %d bytes from '%s'\n", rawData.getLength(), fname.str()));
  439. if (rawData.isEmpty())
  440. return;
  441. LadderInfo *li = parseLadder(rawData);
  442. if (!li)
  443. {
  444. return;
  445. }
  446. // sanity check
  447. if (li->address.isEmpty())
  448. {
  449. DEBUG_LOG(("Bailing because of li->address.isEmpty()\n"));
  450. delete li;
  451. return;
  452. }
  453. if (!li->port)
  454. {
  455. DEBUG_LOG(("Bailing because of !li->port\n"));
  456. delete li;
  457. return;
  458. }
  459. if (li->validMaps.size() == 0)
  460. {
  461. DEBUG_LOG(("Bailing because of li->validMaps.size() == 0\n"));
  462. delete li;
  463. return;
  464. }
  465. li->index = index;
  466. // ladders are QM-only at this point, which kinda invalidates the whole concept of local ladders. Oh well.
  467. li->validQM = FALSE; // no local ladders in QM
  468. li->validCustom = FALSE;
  469. //for (Int i=0; i<4; ++i)
  470. // fname.removeLastChar(); // remove .lad
  471. //li->name = UnicodeString(MultiByteToWideCharSingleLine(fname.reverseFind('\\')+1).c_str());
  472. DEBUG_LOG(("Adding local ladder %ls\n", li->name.str()));
  473. m_localLadders.push_back(li);
  474. }