GSConfig.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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: GSConfig.cpp
  25. // Author: Matthew D. Campbell, Sept 2002
  26. // Description: GameSpy online config
  27. ///////////////////////////////////////////////////////////////////////////////////////
  28. // INCLUDES ///////////////////////////////////////////////////////////////////////////
  29. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  30. #include "Common/GameState.h"
  31. #include "GameClient/MapUtil.h"
  32. #include "GameNetwork/GameSpy/GSConfig.h"
  33. #include "GameNetwork/RankPointValue.h"
  34. #ifdef _INTERNAL
  35. // for occasional debugging...
  36. //#pragma optimize("", off)
  37. //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
  38. #endif
  39. ///////////////////////////////////////////////////////////////////////////////////////
  40. GameSpyConfigInterface *TheGameSpyConfig = NULL;
  41. class GameSpyConfig : public GameSpyConfigInterface
  42. {
  43. public:
  44. GameSpyConfig( AsciiString config );
  45. ~GameSpyConfig() {}
  46. // Pings
  47. std::list<AsciiString> getPingServers(void) { return m_pingServers; }
  48. Int getNumPingRepetitions(void) { return m_pingReps; }
  49. Int getPingTimeoutInMs(void) { return m_pingTimeout; }
  50. virtual Int getPingCutoffGood( void ) { return m_pingCutoffGood; }
  51. virtual Int getPingCutoffBad( void ) { return m_pingCutoffBad; }
  52. // QM
  53. std::list<AsciiString> getQMMaps(void) { return m_qmMaps; }
  54. Int getQMBotID(void) { return m_qmBotID; }
  55. Int getQMChannel(void) { return m_qmChannel; }
  56. void setQMChannel(Int channel) { m_qmChannel = channel; }
  57. // Player Info
  58. Int getPointsForRank(Int rank);
  59. virtual Bool isPlayerVIP(Int id);
  60. virtual Bool getManglerLocation(Int index, AsciiString& host, UnsignedShort& port);
  61. // Ladder / Any other external parsing
  62. AsciiString getLeftoverConfig(void) { return m_leftoverConfig; }
  63. // NAT Timeouts
  64. virtual Int getTimeBetweenRetries() { return m_natRetryInterval; }
  65. virtual Int getMaxManglerRetries() { return m_natMaxManglerRetries; }
  66. virtual time_t getRetryInterval() { return m_natManglerRetryInterval; }
  67. virtual time_t getKeepaliveInterval() { return m_natKeepaliveInterval; }
  68. virtual time_t getPortTimeout() { return m_natPortTimeout; }
  69. virtual time_t getRoundTimeout() { return m_natRoundTimeout; }
  70. // Custom match
  71. virtual Bool restrictGamesToLobby() { return m_restrictGamesToLobby; }
  72. protected:
  73. std::list<AsciiString> m_pingServers;
  74. Int m_pingReps;
  75. Int m_pingTimeout;
  76. Int m_pingCutoffGood;
  77. Int m_pingCutoffBad;
  78. Int m_natRetryInterval;
  79. Int m_natMaxManglerRetries;
  80. time_t m_natManglerRetryInterval;
  81. time_t m_natKeepaliveInterval;
  82. time_t m_natPortTimeout;
  83. time_t m_natRoundTimeout;
  84. std::vector<AsciiString> m_manglerHosts;
  85. std::vector<UnsignedShort> m_manglerPorts;
  86. std::list<AsciiString> m_qmMaps;
  87. Int m_qmBotID;
  88. Int m_qmChannel;
  89. Bool m_restrictGamesToLobby;
  90. std::set<Int> m_vip; // VIP people
  91. Int m_rankPoints[MAX_RANKS];
  92. AsciiString m_leftoverConfig;
  93. };
  94. ///////////////////////////////////////////////////////////////////////////////////////
  95. GameSpyConfigInterface* GameSpyConfigInterface::create(AsciiString config)
  96. {
  97. return NEW GameSpyConfig(config);
  98. }
  99. ///////////////////////////////////////////////////////////////////////////////////////
  100. class SectionChecker
  101. {
  102. public:
  103. typedef std::list<const Bool *> SectionList;
  104. void addVar(const Bool *var) { m_bools.push_back(var); }
  105. Bool isInSection();
  106. protected:
  107. SectionList m_bools;
  108. };
  109. Bool SectionChecker::isInSection() {
  110. Bool ret = FALSE;
  111. for (SectionList::const_iterator it = m_bools.begin(); it != m_bools.end(); ++it)
  112. {
  113. ret = ret || **it;
  114. }
  115. return ret;
  116. }
  117. ///////////////////////////////////////////////////////////////////////////////////////
  118. GameSpyConfig::GameSpyConfig( AsciiString config ) :
  119. m_natRetryInterval(1000),
  120. m_natMaxManglerRetries(25),
  121. m_natManglerRetryInterval(300),
  122. m_natKeepaliveInterval(15000),
  123. m_natPortTimeout(10000),
  124. m_natRoundTimeout(10000),
  125. m_pingReps(1),
  126. m_pingTimeout(1000),
  127. m_pingCutoffGood(300),
  128. m_pingCutoffBad(600),
  129. m_restrictGamesToLobby(FALSE),
  130. m_qmBotID(0),
  131. m_qmChannel(0)
  132. {
  133. m_rankPoints[0] = 0;
  134. m_rankPoints[1] = 5;
  135. m_rankPoints[2] = 10;
  136. m_rankPoints[3] = 20;
  137. m_rankPoints[4] = 50;
  138. m_rankPoints[5] = 100;
  139. m_rankPoints[6] = 200;
  140. m_rankPoints[7] = 500;
  141. m_rankPoints[8] = 1000;
  142. m_rankPoints[9] = 2000;
  143. AsciiString line;
  144. Bool inPingServers = FALSE;
  145. Bool inPingDuration = FALSE;
  146. Bool inQMMaps = FALSE;
  147. Bool inQMBot = FALSE;
  148. Bool inManglers = FALSE;
  149. Bool inVIP = FALSE;
  150. Bool inNAT = FALSE;
  151. Bool inCustom = FALSE;
  152. SectionChecker sections;
  153. sections.addVar(&inPingServers);
  154. sections.addVar(&inPingDuration);
  155. sections.addVar(&inQMMaps);
  156. sections.addVar(&inQMBot);
  157. sections.addVar(&inManglers);
  158. sections.addVar(&inVIP);
  159. sections.addVar(&inNAT);
  160. sections.addVar(&inCustom);
  161. while (config.nextToken(&line, "\n"))
  162. {
  163. if (line.getCharAt(line.getLength()-1) == '\r')
  164. line.removeLastChar(); // there is a trailing '\r'
  165. line.trim();
  166. if (line.isEmpty())
  167. continue;
  168. if (!sections.isInSection() && line.compare("<PingServers>") == 0)
  169. {
  170. inPingServers = TRUE;
  171. }
  172. else if (inPingServers && line.compare("</PingServers>") == 0)
  173. {
  174. inPingServers = FALSE;
  175. }
  176. else if (!sections.isInSection() && line.compare("<PingDuration>") == 0)
  177. {
  178. inPingDuration = TRUE;
  179. }
  180. else if (inPingDuration && line.compare("</PingDuration>") == 0)
  181. {
  182. inPingDuration = FALSE;
  183. }
  184. else if (!sections.isInSection() && line.compare("<QMMaps>") == 0)
  185. {
  186. inQMMaps = TRUE;
  187. }
  188. else if (inQMMaps && line.compare("</QMMaps>") == 0)
  189. {
  190. inQMMaps = FALSE;
  191. }
  192. else if (!sections.isInSection() && line.compare("<Manglers>") == 0)
  193. {
  194. inManglers = TRUE;
  195. }
  196. else if (inManglers && line.compare("</Manglers>") == 0)
  197. {
  198. inManglers = FALSE;
  199. }
  200. else if (!sections.isInSection() && line.compare("<QMBot>") == 0)
  201. {
  202. inQMBot = TRUE;
  203. }
  204. else if (inQMBot && line.compare("</QMBot>") == 0)
  205. {
  206. inQMBot = FALSE;
  207. }
  208. else if (!sections.isInSection() && line.compare("<VIP>") == 0)
  209. {
  210. inVIP = TRUE;
  211. }
  212. else if (inVIP && line.compare("</VIP>") == 0)
  213. {
  214. inVIP = FALSE;
  215. }
  216. else if (!sections.isInSection() && line.compare("<NAT>") == 0)
  217. {
  218. inNAT = TRUE;
  219. }
  220. else if (inNAT && line.compare("</NAT>") == 0)
  221. {
  222. inNAT = FALSE;
  223. }
  224. else if (!sections.isInSection() && line.compare("<Custom>") == 0)
  225. {
  226. inCustom = TRUE;
  227. }
  228. else if (inCustom && line.compare("</Custom>") == 0)
  229. {
  230. inCustom = FALSE;
  231. }
  232. else if (inVIP)
  233. {
  234. line.toLower();
  235. if (line.getLength())
  236. {
  237. Int val = atoi(line.str());
  238. if (val > 0)
  239. m_vip.insert(val);
  240. }
  241. }
  242. else if (inPingServers)
  243. {
  244. line.toLower();
  245. m_pingServers.push_back(line);
  246. }
  247. else if (inPingDuration)
  248. {
  249. line.toLower();
  250. AsciiString key, val;
  251. if (line.nextToken(&key, " ="))
  252. {
  253. if (key == "reps")
  254. {
  255. if (line.nextToken(&val, " ="))
  256. {
  257. m_pingReps = atoi(val.str());
  258. }
  259. }
  260. else if (key == "timeout")
  261. {
  262. if (line.nextToken(&val, " ="))
  263. {
  264. m_pingTimeout = atoi(val.str());
  265. }
  266. }
  267. else if (key == "low")
  268. {
  269. if (line.nextToken(&val, " ="))
  270. {
  271. m_pingCutoffGood = atoi(val.str());
  272. }
  273. }
  274. else if (key == "med")
  275. {
  276. if (line.nextToken(&val, " ="))
  277. {
  278. m_pingCutoffBad = atoi(val.str());
  279. }
  280. }
  281. }
  282. }
  283. else if (inManglers)
  284. {
  285. line.trim();
  286. line.toLower();
  287. AsciiString hostStr;
  288. AsciiString portStr;
  289. line.nextToken(&hostStr, ":");
  290. line.nextToken(&portStr, ":\n\r");
  291. if (hostStr.isNotEmpty() && portStr.isNotEmpty())
  292. {
  293. m_manglerHosts.push_back(hostStr);
  294. m_manglerPorts.push_back(atoi(portStr.str()));
  295. }
  296. }
  297. else if (inQMMaps)
  298. {
  299. line.toLower();
  300. AsciiString mapName;
  301. mapName.format("%s\\%s\\%s.map", TheMapCache->getMapDir().str(), line.str(), line.str());
  302. mapName = TheGameState->portableMapPathToRealMapPath(TheGameState->realMapPathToPortableMapPath(mapName));
  303. mapName.toLower();
  304. // [SKB: Jul 01 2003 @ 6:43pm] :
  305. // German2 is missing some maps because of content. But, we need the m_qmMaps
  306. // to contain same number of strings as the Retail version so that the
  307. // QM Bot thinks that they have the same number of maps.
  308. #if 1
  309. m_qmMaps.push_back(mapName);
  310. #else
  311. const MapMetaData *md = TheMapCache->findMap(mapName);
  312. if (md)
  313. {
  314. m_qmMaps.push_back(mapName);
  315. }
  316. #endif
  317. }
  318. else if (inQMBot)
  319. {
  320. line.toLower();
  321. AsciiString key, val;
  322. if (line.nextToken(&key, " ="))
  323. {
  324. if (key == "id")
  325. {
  326. if (line.nextToken(&val, " ="))
  327. {
  328. m_qmBotID = atoi(val.str());
  329. }
  330. }
  331. }
  332. }
  333. else if (inNAT)
  334. {
  335. line.toLower();
  336. AsciiString key, val;
  337. if (line.nextToken(&key, " ="))
  338. {
  339. if (key == "retryinterval")
  340. {
  341. if (line.nextToken(&val, " ="))
  342. {
  343. m_natRetryInterval = atoi(val.str());
  344. }
  345. }
  346. else if (key == "manglerretries")
  347. {
  348. if (line.nextToken(&val, " ="))
  349. {
  350. m_natMaxManglerRetries = atoi(val.str());
  351. }
  352. }
  353. else if (key == "manglerinterval")
  354. {
  355. if (line.nextToken(&val, " ="))
  356. {
  357. m_natManglerRetryInterval = atoi(val.str());
  358. }
  359. }
  360. else if (key == "keepaliveinterval")
  361. {
  362. if (line.nextToken(&val, " ="))
  363. {
  364. m_natKeepaliveInterval = atoi(val.str());
  365. }
  366. }
  367. else if (key == "porttimeout")
  368. {
  369. if (line.nextToken(&val, " ="))
  370. {
  371. m_natPortTimeout = atoi(val.str());
  372. }
  373. }
  374. else if (key == "roundtimeout")
  375. {
  376. if (line.nextToken(&val, " ="))
  377. {
  378. m_natRoundTimeout = atoi(val.str());
  379. }
  380. }
  381. else
  382. {
  383. DEBUG_LOG(("Unknown key '%s' = '%s' in NAT block of GameSpy Config\n", key.str(), val.str()));
  384. }
  385. }
  386. else
  387. {
  388. DEBUG_LOG(("Key '%s' missing val in NAT block of GameSpy Config\n", key.str()));
  389. }
  390. }
  391. else if (inCustom)
  392. {
  393. line.toLower();
  394. AsciiString key, val;
  395. if (line.nextToken(&key, " =") && line.nextToken(&val, " ="))
  396. {
  397. if (key == "restricted")
  398. {
  399. m_restrictGamesToLobby = atoi(val.str());
  400. }
  401. else
  402. {
  403. DEBUG_LOG(("Unknown key '%s' = '%s' in Custom block of GameSpy Config\n", key.str(), val.str()));
  404. }
  405. }
  406. else
  407. {
  408. DEBUG_LOG(("Key '%s' missing val in Custom block of GameSpy Config\n", key.str()));
  409. }
  410. }
  411. else
  412. {
  413. m_leftoverConfig.concat(line);
  414. m_leftoverConfig.concat('\n');
  415. }
  416. }
  417. }
  418. ///////////////////////////////////////////////////////////////////////////////////////
  419. Int GameSpyConfig::getPointsForRank(Int rank)
  420. {
  421. if (rank >= MAX_RANKS) rank = MAX_RANKS-1;
  422. if (rank < 0) rank = 0;
  423. return m_rankPoints[rank];
  424. }
  425. ///////////////////////////////////////////////////////////////////////////////////////
  426. Bool GameSpyConfig::getManglerLocation(Int index, AsciiString& host, UnsignedShort& port)
  427. {
  428. if (index < 0 || index >= m_manglerHosts.size())
  429. {
  430. return FALSE;
  431. }
  432. host = m_manglerHosts[index];
  433. port = m_manglerPorts[index];
  434. return TRUE;
  435. }
  436. ///////////////////////////////////////////////////////////////////////////////////////
  437. Bool GameSpyConfig::isPlayerVIP(Int id)
  438. {
  439. std::set<Int>::const_iterator it = std::find(m_vip.begin(), m_vip.end(), id);
  440. return it != m_vip.end();
  441. }
  442. ///////////////////////////////////////////////////////////////////////////////////////