GameResPacket.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /*
  2. ** Command & Conquer Renegade(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. * FILE
  21. * $Archive: /Commando/Code/WWOnline/GameResPacket.cpp $
  22. *
  23. * DESCRIPTION
  24. *
  25. * PROGRAMMER
  26. * $Author: Denzil_l $
  27. *
  28. * VERSION INFO
  29. * $Revision: 4 $
  30. * $Modtime: 12/11/01 6:23p $
  31. *
  32. ******************************************************************************/
  33. #include "GameResPacket.h"
  34. #include <assert.h>
  35. #include <string.h>
  36. #include <winsock.h>
  37. namespace WWOnline {
  38. /**************************************************************************
  39. * GameResPacket::~GameResPacket -- destroys a packet class be freeing list *
  40. * *
  41. * INPUT: none *
  42. * *
  43. * OUTPUT: none *
  44. * *
  45. * HISTORY: *
  46. * 04/24/1996 PWG : Created. *
  47. *========================================================================*/
  48. GameResPacket::~GameResPacket(void)
  49. {
  50. GameResField* current;
  51. GameResField* next;
  52. // Loop through the entire field list and delete each entry.
  53. for (current = mHead; current; current = next)
  54. {
  55. next = current->mNext;
  56. delete current;
  57. }
  58. }
  59. /**************************************************************************
  60. * GameResPacket::ADD_FIELD -- Adds a GameResField entry to head of packet li *
  61. * *
  62. * INPUT: GameResField * - a properly constructed field class entry. *
  63. * *
  64. * OUTPUT: none *
  65. * *
  66. * HISTORY: *
  67. * 04/24/1996 PWG : Created. *
  68. *========================================================================*/
  69. void GameResPacket::Add_Field(GameResField *field)
  70. {
  71. field->mNext = mHead;
  72. mHead = field;
  73. }
  74. /**************************************************************************
  75. * GameResPacket::GameResPacket -- Creates a Packet object from a COMMS packe *
  76. * *
  77. * INPUT: *
  78. * *
  79. * OUTPUT: *
  80. * *
  81. * WARNINGS: *
  82. * *
  83. * HISTORY: *
  84. * 04/22/1996 PWG : Created. *
  85. *========================================================================*/
  86. GameResPacket::GameResPacket(unsigned char* curbuf)
  87. {
  88. // Pull the size and packet ID out of the linear packet stream.
  89. mSize = ntohl(*(unsigned long*)curbuf);
  90. curbuf += sizeof(mSize);
  91. mID = ntohs(*(unsigned short*)curbuf);
  92. curbuf += sizeof(mID);
  93. mReserved = ntohs(*(unsigned short*)curbuf);
  94. curbuf += sizeof(mReserved);
  95. mHead = NULL;
  96. // Calculate the remaining size so that we can loop through the
  97. // packets and extract them.
  98. unsigned long remaining_size = (mSize - (sizeof(mSize) + sizeof(mID) + sizeof(mReserved)));
  99. // Loop through the linear packet until we run out of room and
  100. // create a field for each.
  101. while (remaining_size > 0)
  102. {
  103. GameResField* field = new GameResField;
  104. // Copy the adjusted header into the buffer and then advance the buffer
  105. memcpy(field, curbuf, GAMERESFIELD_HEADER_SIZE);
  106. curbuf += GAMERESFIELD_HEADER_SIZE;
  107. remaining_size -= GAMERESFIELD_HEADER_SIZE;
  108. // Copy the data into the buffer
  109. unsigned short size = ntohs(field->mSize);
  110. field->mData = new unsigned char[size];
  111. memcpy(field->mData, curbuf, size);
  112. curbuf += size;
  113. remaining_size -= size;
  114. // Make sure we allow for the pad bytes.
  115. int pad = (4 - (ntohs(field->mSize) & 3)) & 3;
  116. curbuf += pad;
  117. remaining_size -= pad;
  118. // Convert the field back to the host format
  119. field->Net_To_Host();
  120. // Finally add the field to the field list in the packet
  121. // structure.
  122. Add_Field(field);
  123. }
  124. }
  125. /**************************************************************************
  126. * CREATE_COMMS_PACKET -- Walks field list creating a packet *
  127. * *
  128. * INPUT: short - the id of the packet so the server can identify it *
  129. * unsigned short & - the size of the packet returned here *
  130. * *
  131. * OUTPUT: void * pointer to the linear packet data *
  132. * *
  133. * WARNINGS: This routine allocates memory that the user is responsible*
  134. * for freeing. *
  135. * *
  136. * HISTORY: *
  137. * 04/22/1996 PWG : Created. *
  138. *========================================================================*/
  139. unsigned char* GameResPacket::Create_Comms_Packet(unsigned long& size, char* sig_name, unsigned long& sig_offset)
  140. {
  141. GameResField* current;
  142. sig_offset = 0;
  143. // Size starts at 8 because that is the size of the packet header. (size[4] + id[2] + reserved[2])
  144. size = (sizeof(mSize) + sizeof(mID) + sizeof(mReserved));
  145. // Take a quick spin through and calculate the size of the packet we
  146. // are building.
  147. for (current = mHead; current; current = current->mNext)
  148. {
  149. size += (unsigned long)GAMERESFIELD_HEADER_SIZE; // add in packet header size
  150. size += current->mSize; // add in data size
  151. size += (4 - (current->mSize & 3)) & 3; // add in pad value to dword align next packet
  152. }
  153. // Now that we know the size allocate a buffer big enough to hold the
  154. // packet.
  155. unsigned char* bufferStart = new unsigned char[size];
  156. unsigned char* curbuf = bufferStart;
  157. // write the size into the packet header
  158. *(unsigned long*)curbuf = htonl(size);
  159. curbuf += sizeof(unsigned long);
  160. *(unsigned short*)curbuf = htons(mID);
  161. curbuf += sizeof(unsigned short);
  162. *(unsigned short*)curbuf = htons(mReserved);
  163. curbuf += sizeof(unsigned short);
  164. // Ok now that the actual header information has been written we need to write out
  165. // field information.
  166. for (current = mHead; current; current = current->mNext)
  167. {
  168. #ifdef _DEBUG
  169. current->DebugDump();
  170. #endif
  171. unsigned short fieldSize = current->mSize;
  172. // Temporarily convert the packet to net format (this saves alot of
  173. // effort, and seems safe...)
  174. current->Host_To_Net();
  175. // Copy the adjusted header into the buffer and then advance the buffer
  176. memcpy(curbuf, current, GAMERESFIELD_HEADER_SIZE);
  177. curbuf += GAMERESFIELD_HEADER_SIZE;
  178. // If this is the sig data then make a note of the offset into the packet.
  179. if (sig_name && strncmp(sig_name, current->mID, 4) == 0)
  180. {
  181. // If sig_offset isn't zero then that would indicate that there were multiple sig entries in the packet.
  182. assert(sig_offset == 0);
  183. sig_offset = (curbuf - bufferStart);
  184. }
  185. // Copy the data into the buffer and then advance the buffer
  186. memcpy(curbuf, current->mData, fieldSize);
  187. curbuf += fieldSize;
  188. // Finally take care of any pad bytes by setting them to 0
  189. int pad = ((4 - (fieldSize & 3)) & 3);
  190. // If there is any pad left over, make sure you memset it
  191. // to zeros, so it looks like a pad.
  192. if (pad)
  193. {
  194. memset(curbuf, 0, pad);
  195. curbuf += pad;
  196. }
  197. current->Net_To_Host();
  198. }
  199. return bufferStart;
  200. }
  201. /**************************************************************************
  202. * GameResPacket::FIND_FIELD -- Finds a field if it exists in the packets *
  203. * *
  204. * INPUT: char * - the id of the field we are looking for. *
  205. * *
  206. * OUTPUT: GameResField * pointer to the field class *
  207. * *
  208. * HISTORY: *
  209. * 04/23/1996 PWG : Created. *
  210. *========================================================================*/
  211. GameResField *GameResPacket::Find_Field(char *id)
  212. {
  213. for (GameResField *current = mHead; current; current = current->mNext)
  214. {
  215. if (strncmp(id, current->mID, 4) == 0)
  216. return current;
  217. }
  218. return NULL;
  219. }
  220. /**************************************************************************
  221. * GET_FIELD -- Find specified name and returns data *
  222. * *
  223. * INPUT: char * - the id of the field that holds the data. *
  224. * char & - the reference to store the data into *
  225. * *
  226. * OUTPUT: true if the field was found, false if it was not. *
  227. * *
  228. * WARNINGS: The data reference is not changed if the field is not *
  229. * found. *
  230. * *
  231. * HISTORY: *
  232. * 04/23/1996 PWG : Created. *
  233. *========================================================================*/
  234. bool GameResPacket::Get_Field(char *id, char &data)
  235. {
  236. GameResField *field = Find_Field(id);
  237. if (field)
  238. {
  239. data = *((char*)field->mData);
  240. }
  241. return((field) ? true : false);
  242. }
  243. /**************************************************************************
  244. * GET_FIELD -- Find specified name and returns data *
  245. * *
  246. * INPUT: char * - the id of the field that holds the data. *
  247. * unsigned char & - the reference to store the data into *
  248. * *
  249. * OUTPUT: true if the field was found, false if it was not. *
  250. * *
  251. * WARNINGS: The data reference is not changed if the field is not *
  252. * found. *
  253. * *
  254. * HISTORY: *
  255. * 04/23/1996 PWG : Created. *
  256. *========================================================================*/
  257. bool GameResPacket::Get_Field(char *id, unsigned char &data)
  258. {
  259. GameResField *field = Find_Field(id);
  260. if (field)
  261. {
  262. data = *((unsigned char *)field->mData);
  263. }
  264. return((field) ? true : false);
  265. }
  266. /**************************************************************************
  267. * GET_FIELD -- Find specified name and returns data *
  268. * *
  269. * INPUT: char * - the id of the field that holds the data. *
  270. * short & - the reference to store the data into *
  271. * *
  272. * OUTPUT: true if the field was found, false if it was not. *
  273. * *
  274. * WARNINGS: The data reference is not changed if the field is not *
  275. * found. *
  276. * *
  277. * HISTORY: *
  278. * 04/23/1996 PWG : Created. *
  279. *========================================================================*/
  280. bool GameResPacket::Get_Field(char *id, short &data)
  281. {
  282. GameResField *field = Find_Field(id);
  283. if (field)
  284. {
  285. data = *((short *)field->mData);
  286. }
  287. return((field) ? true : false);
  288. }
  289. /**************************************************************************
  290. * GET_FIELD -- Find specified name and returns data *
  291. * *
  292. * INPUT: char * - the id of the field that holds the data. *
  293. * unsigned short & - the reference to store the data into*
  294. * *
  295. * OUTPUT: true if the field was found, false if it was not. *
  296. * *
  297. * WARNINGS: The data reference is not changed if the field is not *
  298. * found. *
  299. * *
  300. * HISTORY: *
  301. * 04/23/1996 PWG : Created. *
  302. *========================================================================*/
  303. bool GameResPacket::Get_Field(char *id, unsigned short &data)
  304. {
  305. GameResField *field = Find_Field(id);
  306. if (field)
  307. {
  308. data = *((unsigned short *)field->mData);
  309. }
  310. return((field) ? true : false);
  311. }
  312. /**************************************************************************
  313. * GET_FIELD -- Find specified name and returns data *
  314. * *
  315. * INPUT: char * - the id of the field that holds the data. *
  316. * long & - the reference to store the data into *
  317. * *
  318. * OUTPUT: true if the field was found, false if it was not. *
  319. * *
  320. * WARNINGS: The data reference is not changed if the field is not *
  321. * found. *
  322. * *
  323. * HISTORY: *
  324. * 04/23/1996 PWG : Created. *
  325. *========================================================================*/
  326. bool GameResPacket::Get_Field(char *id, long &data)
  327. {
  328. GameResField *field = Find_Field(id);
  329. if (field)
  330. {
  331. data = *((long *)field->mData);
  332. }
  333. return((field) ? true : false);
  334. }
  335. /**************************************************************************
  336. * GET_FIELD -- Find specified name and returns data as a string *
  337. * *
  338. * INPUT: char * - the id of the field that holds the data. *
  339. * char * - the string to store the data into *
  340. * *
  341. * OUTPUT: true if the field was found, false if it was not. *
  342. * *
  343. * WARNINGS: The string is not changed if the field is not found. It *
  344. * is assumed that the string variabled specified by the *
  345. * pointer is large enough to hold the data. *
  346. * *
  347. * HISTORY: *
  348. * 04/23/1996 PWG : Created. *
  349. *========================================================================*/
  350. bool GameResPacket::Get_Field(char *id, char *data)
  351. {
  352. GameResField *field = Find_Field(id);
  353. if (field)
  354. {
  355. strcpy(data, (char *)field->mData);
  356. }
  357. return((field) ? true : false);
  358. }
  359. /**************************************************************************
  360. * GET_FIELD -- Find specified name and returns data *
  361. * *
  362. * INPUT: char * - the id of the field that holds the data *
  363. * unsigned long & - the reference to store the data into *
  364. * *
  365. * OUTPUT: true if the field was found, false if it was not. *
  366. * *
  367. * WARNINGS: The data reference is not changed if the field is not *
  368. * found. *
  369. * *
  370. * HISTORY: *
  371. * 04/23/1996 PWG : Created. *
  372. *========================================================================*/
  373. bool GameResPacket::Get_Field(char *id, unsigned long &data)
  374. {
  375. GameResField *field = Find_Field(id);
  376. if (field)
  377. {
  378. data = *((unsigned long *)field->mData);
  379. }
  380. return((field) ? true : false);
  381. }
  382. /**************************************************************************
  383. * GET_FIELD -- Find specified name and returns data *
  384. * *
  385. * INPUT: char * - the id of the field that holds the data. *
  386. * void * - the reference to store the data into *
  387. * int - the length of the buffer passed in *
  388. * *
  389. * OUTPUT: true if the field was found, false if it was not. *
  390. * *
  391. * WARNINGS: The data reference is not changed if the field is not *
  392. * found. *
  393. * *
  394. * HISTORY: *
  395. * 6/4/96 4:46PM ST : Created *
  396. *========================================================================*/
  397. bool GameResPacket::Get_Field(char *id, void *data, int &length)
  398. {
  399. GameResField *field = Find_Field(id);
  400. if (field)
  401. {
  402. memcpy(data, field->mData, min((int)field->mSize, length));
  403. length = (int) field->mSize;
  404. }
  405. return((field) ? true : false);
  406. }
  407. } // namespace WWOnline