Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

530 lines
16 KiB

  1. //========= Copyright (c) 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=====================================================================================//
  6. #include "dt_recv.h"
  7. #include "mathlib/vector.h"
  8. #include "tier1/strtools.h"
  9. #include "dt_utlvector_common.h"
  10. // memdbgon must be the last include file in a .cpp file!!!
  11. #include "tier0/memdbgon.h"
  12. #if !defined(_STATIC_LINKED) || defined(CLIENT_DLL)
  13. char *s_ClientElementNames[MAX_ARRAY_ELEMENTS] =
  14. {
  15. "000", "001", "002", "003", "004", "005", "006", "007", "008", "009",
  16. "010", "011", "012", "013", "014", "015", "016", "017", "018", "019",
  17. "020", "021", "022", "023", "024", "025", "026", "027", "028", "029",
  18. "030", "031", "032", "033", "034", "035", "036", "037", "038", "039",
  19. "040", "041", "042", "043", "044", "045", "046", "047", "048", "049",
  20. "050", "051", "052", "053", "054", "055", "056", "057", "058", "059",
  21. "060", "061", "062", "063", "064", "065", "066", "067", "068", "069",
  22. "070", "071", "072", "073", "074", "075", "076", "077", "078", "079",
  23. "080", "081", "082", "083", "084", "085", "086", "087", "088", "089",
  24. "090", "091", "092", "093", "094", "095", "096", "097", "098", "099",
  25. "100", "101", "102", "103", "104", "105", "106", "107", "108", "109",
  26. "110", "111", "112", "113", "114", "115", "116", "117", "118", "119",
  27. "120", "121", "122", "123", "124", "125", "126", "127", "128", "129",
  28. "130", "131", "132", "133", "134", "135", "136", "137", "138", "139",
  29. "140", "141", "142", "143", "144", "145", "146", "147", "148", "149",
  30. "150", "151", "152", "153", "154", "155", "156", "157", "158", "159",
  31. "160", "161", "162", "163", "164", "165", "166", "167", "168", "169",
  32. "170", "171", "172", "173", "174", "175", "176", "177", "178", "179",
  33. "180", "181", "182", "183", "184", "185", "186", "187", "188", "189",
  34. "190", "191", "192", "193", "194", "195", "196", "197", "198", "199",
  35. "200", "201", "202", "203", "204", "205", "206", "207", "208", "209",
  36. "210", "211", "212", "213", "214", "215", "216", "217", "218", "219",
  37. "220", "221", "222", "223", "224", "225", "226", "227", "228", "229",
  38. "230", "231", "232", "233", "234", "235", "236", "237", "238", "239",
  39. "240", "241", "242", "243", "244", "245", "246", "247", "248", "249",
  40. "250", "251", "252", "253", "254", "255", "256", "257", "258", "259",
  41. "260", "261", "262", "263", "264", "265", "266", "267", "268", "269",
  42. "270", "271", "272", "273", "274", "275", "276", "277", "278", "279",
  43. "280", "281", "282", "283", "284", "285", "286", "287", "288", "289",
  44. "290", "291", "292", "293", "294", "295", "296", "297", "298", "299",
  45. "300", "301", "302", "303", "304", "305", "306", "307", "308", "309",
  46. "310", "311", "312", "313", "314", "315", "316", "317", "318", "319",
  47. "320", "321", "322", "323", "324", "325", "326", "327", "328", "329",
  48. "330", "331", "332", "333", "334", "335", "336", "337", "338", "339",
  49. "340", "341", "342", "343", "344", "345", "346", "347", "348", "349",
  50. "350", "351", "352", "353", "354", "355", "356", "357", "358", "359",
  51. "360", "361", "362", "363", "364", "365", "366", "367", "368", "369",
  52. "370", "371", "372", "373", "374", "375", "376", "377", "378", "379",
  53. "380", "381", "382", "383", "384", "385", "386", "387", "388", "389",
  54. "390", "391", "392", "393", "394", "395", "396", "397", "398", "399",
  55. "400", "401", "402", "403", "404", "405", "406", "407", "408", "409",
  56. "410", "411", "412", "413", "414", "415", "416", "417", "418", "419",
  57. "420", "421", "422", "423", "424", "425", "426", "427", "428", "429",
  58. "430", "431", "432", "433", "434", "435", "436", "437", "438", "439",
  59. "440", "441", "442", "443", "444", "445", "446", "447", "448", "449",
  60. "450", "451", "452", "453", "454", "455", "456", "457", "458", "459",
  61. "460", "461", "462", "463", "464", "465", "466", "467", "468", "469",
  62. "470", "471", "472", "473", "474", "475", "476", "477", "478", "479",
  63. "480", "481", "482", "483", "484", "485", "486", "487", "488", "489",
  64. "490", "491", "492", "493", "494", "495", "496", "497", "498", "499",
  65. "500", "501", "502", "503", "504", "505", "506", "507", "508", "509",
  66. "510", "511", "512", "513", "514", "515", "516", "517", "518", "519",
  67. "520", "521", "522", "523", "524", "525", "526", "527", "528", "529",
  68. "530", "531", "532", "533", "534", "535", "536", "537", "538", "539",
  69. "540", "541", "542", "543", "544", "545", "546", "547", "548", "549",
  70. "550", "551", "552", "553", "554", "555", "556", "557", "558", "559",
  71. "560", "561", "562", "563", "564", "565", "566", "567", "568", "569",
  72. "570", "571", "572", "573", "574", "575", "576", "577", "578", "579",
  73. "580", "581", "582", "583", "584", "585", "586", "587", "588", "589",
  74. "590", "591", "592", "593", "594", "595", "596", "597", "598", "599",
  75. "600", "601", "602", "603", "604", "605", "606", "607", "608", "609",
  76. "610", "611", "612", "613", "614", "615", "616", "617", "618", "619",
  77. "620", "621", "622", "623", "624", "625", "626", "627", "628", "629",
  78. "630", "631", "632", "633", "634", "635", "636", "637", "638", "639",
  79. "640", "641", "642", "643", "644", "645", "646", "647", "648", "649",
  80. "650", "651", "652", "653", "654", "655", "656", "657", "658", "659",
  81. "660", "661", "662", "663", "664", "665", "666", "667", "668", "669",
  82. "670", "671", "672", "673", "674", "675", "676", "677", "678", "679",
  83. "680", "681", "682", "683", "684", "685", "686", "687", "688", "689",
  84. "690", "691", "692", "693", "694", "695", "696", "697", "698", "699",
  85. "700", "701", "702", "703", "704", "705", "706", "707", "708", "709",
  86. "710", "711", "712", "713", "714", "715", "716", "717", "718", "719",
  87. "720", "721", "722", "723", "724", "725", "726", "727", "728", "729",
  88. "730", "731", "732", "733", "734", "735", "736", "737", "738", "739",
  89. "740", "741", "742", "743", "744", "745", "746", "747", "748", "749",
  90. "750", "751", "752", "753", "754", "755", "756", "757", "758", "759",
  91. "760", "761", "762", "763", "764", "765", "766", "767", "768", "769",
  92. "770", "771", "772", "773", "774", "775", "776", "777", "778", "779",
  93. "780", "781", "782", "783", "784", "785", "786", "787", "788", "789",
  94. "790", "791", "792", "793", "794", "795", "796", "797", "798", "799",
  95. "800", "801", "802", "803", "804", "805", "806", "807", "808", "809",
  96. "810", "811", "812", "813", "814", "815", "816", "817", "818", "819",
  97. "820", "821", "822", "823", "824", "825", "826", "827", "828", "829",
  98. "830", "831", "832", "833", "834", "835", "836", "837", "838", "839",
  99. "840", "841", "842", "843", "844", "845", "846", "847", "848", "849",
  100. "850", "851", "852", "853", "854", "855", "856", "857", "858", "859",
  101. "860", "861", "862", "863", "864", "865", "866", "867", "868", "869",
  102. "870", "871", "872", "873", "874", "875", "876", "877", "878", "879",
  103. "880", "881", "882", "883", "884", "885", "886", "887", "888", "889",
  104. "890", "891", "892", "893", "894", "895", "896", "897", "898", "899",
  105. "900", "901", "902", "903", "904", "905", "906", "907", "908", "909",
  106. "910", "911", "912", "913", "914", "915", "916", "917", "918", "919",
  107. "920", "921", "922", "923", "924", "925", "926", "927", "928", "929",
  108. "930", "931", "932", "933", "934", "935", "936", "937", "938", "939",
  109. "940", "941", "942", "943", "944", "945", "946", "947", "948", "949",
  110. "950", "951", "952", "953", "954", "955", "956", "957", "958", "959",
  111. "960", "961", "962", "963", "964", "965", "966", "967", "968", "969",
  112. "970", "971", "972", "973", "974", "975", "976", "977", "978", "979",
  113. "980", "981", "982", "983", "984", "985", "986", "987", "988", "989",
  114. "990", "991", "992", "993", "994", "995", "996", "997", "998", "999",
  115. "1000", "1001", "1002", "1003", "1004", "1005", "1006", "1007", "1008", "1009",
  116. "1010", "1011", "1012", "1013", "1014", "1015", "1016", "1017", "1018", "1019",
  117. "1020", "1021", "1022", "1023"
  118. };
  119. CStandardRecvProxies::CStandardRecvProxies()
  120. {
  121. m_Int32ToInt8 = RecvProxy_Int32ToInt8;
  122. m_Int32ToInt16 = RecvProxy_Int32ToInt16;
  123. m_Int32ToInt32 = RecvProxy_Int32ToInt32;
  124. m_Int64ToInt64 = RecvProxy_Int64ToInt64;
  125. m_FloatToFloat = RecvProxy_FloatToFloat;
  126. m_VectorToVector = RecvProxy_VectorToVector;
  127. }
  128. CStandardRecvProxies g_StandardRecvProxies;
  129. // ---------------------------------------------------------------------- //
  130. // RecvProp.
  131. // ---------------------------------------------------------------------- //
  132. RecvProp::RecvProp()
  133. {
  134. m_pExtraData = NULL;
  135. m_pVarName = NULL;
  136. m_Offset = 0;
  137. m_RecvType = DPT_Int;
  138. m_Flags = 0;
  139. m_ProxyFn = NULL;
  140. m_DataTableProxyFn = NULL;
  141. m_pDataTable = NULL;
  142. m_nElements = 1;
  143. m_ElementStride = -1;
  144. m_pArrayProp = NULL;
  145. m_ArrayLengthProxy = NULL;
  146. m_bInsideArray = false;
  147. }
  148. // ---------------------------------------------------------------------- //
  149. // RecvTable.
  150. // ---------------------------------------------------------------------- //
  151. RecvTable::RecvTable()
  152. {
  153. Construct( NULL, 0, NULL );
  154. }
  155. RecvTable::RecvTable(RecvProp *pProps, int nProps, char *pNetTableName)
  156. {
  157. Construct( pProps, nProps, pNetTableName );
  158. }
  159. RecvTable::~RecvTable()
  160. {
  161. }
  162. void RecvTable::Construct( RecvProp *pProps, int nProps, char *pNetTableName )
  163. {
  164. m_pProps = pProps;
  165. m_nProps = nProps;
  166. m_pDecoder = NULL;
  167. m_pNetTableName = pNetTableName;
  168. m_bInitialized = false;
  169. m_bInMainList = false;
  170. }
  171. // ---------------------------------------------------------------------- //
  172. // Prop setup functions (for building tables).
  173. // ---------------------------------------------------------------------- //
  174. RecvProp RecvPropFloat(
  175. char *pVarName,
  176. int offset,
  177. int sizeofVar,
  178. int flags,
  179. RecvVarProxyFn varProxy
  180. )
  181. {
  182. RecvProp ret;
  183. #ifdef _DEBUG
  184. if ( varProxy == RecvProxy_FloatToFloat )
  185. {
  186. Assert( sizeofVar == 0 || sizeofVar == 4 );
  187. }
  188. #endif
  189. ret.m_pVarName = pVarName;
  190. ret.SetOffset( offset );
  191. ret.m_RecvType = DPT_Float;
  192. ret.m_Flags = flags;
  193. ret.SetProxyFn( varProxy );
  194. return ret;
  195. }
  196. RecvProp RecvPropVector(
  197. char *pVarName,
  198. int offset,
  199. int sizeofVar,
  200. int flags,
  201. RecvVarProxyFn varProxy
  202. )
  203. {
  204. RecvProp ret;
  205. #ifdef _DEBUG
  206. if ( varProxy == RecvProxy_VectorToVector )
  207. {
  208. Assert( sizeofVar == sizeof( Vector ) );
  209. }
  210. #endif
  211. ret.m_pVarName = pVarName;
  212. ret.SetOffset( offset );
  213. ret.m_RecvType = DPT_Vector;
  214. ret.m_Flags = flags;
  215. ret.SetProxyFn( varProxy );
  216. return ret;
  217. }
  218. RecvProp RecvPropVectorXY(
  219. char *pVarName,
  220. int offset,
  221. int sizeofVar,
  222. int flags,
  223. RecvVarProxyFn varProxy
  224. )
  225. {
  226. RecvProp ret;
  227. #ifdef _DEBUG
  228. if ( varProxy == RecvProxy_VectorToVector )
  229. {
  230. Assert( sizeofVar == sizeof( Vector ) );
  231. }
  232. #endif
  233. ret.m_pVarName = pVarName;
  234. ret.SetOffset( offset );
  235. ret.m_RecvType = DPT_VectorXY;
  236. ret.m_Flags = flags;
  237. ret.SetProxyFn( varProxy );
  238. return ret;
  239. }
  240. #if 0 // We can't ship this since it changes the size of DTVariant to be 20 bytes instead of 16 and that breaks MODs!!!
  241. RecvProp RecvPropQuaternion(
  242. char *pVarName,
  243. int offset,
  244. int sizeofVar, // Handled by RECVINFO macro, but set to SIZEOF_IGNORE if you don't want to bother.
  245. int flags,
  246. RecvVarProxyFn varProxy
  247. )
  248. {
  249. RecvProp ret;
  250. #ifdef _DEBUG
  251. if ( varProxy == RecvProxy_QuaternionToQuaternion )
  252. {
  253. Assert( sizeofVar == sizeof( Quaternion ) );
  254. }
  255. #endif
  256. ret.m_pVarName = pVarName;
  257. ret.SetOffset( offset );
  258. ret.m_RecvType = DPT_Quaternion;
  259. ret.m_Flags = flags;
  260. ret.SetProxyFn( varProxy );
  261. return ret;
  262. }
  263. #endif
  264. RecvProp RecvPropInt(
  265. char *pVarName,
  266. int offset,
  267. int sizeofVar,
  268. int flags,
  269. RecvVarProxyFn varProxy
  270. )
  271. {
  272. RecvProp ret;
  273. // If they didn't specify a proxy, then figure out what type we're writing to.
  274. if (varProxy == NULL)
  275. {
  276. if (sizeofVar == 1)
  277. {
  278. varProxy = RecvProxy_Int32ToInt8;
  279. }
  280. else if (sizeofVar == 2)
  281. {
  282. varProxy = RecvProxy_Int32ToInt16;
  283. }
  284. else if (sizeofVar == 4)
  285. {
  286. varProxy = RecvProxy_Int32ToInt32;
  287. }
  288. else if (sizeofVar == 8)
  289. {
  290. varProxy = RecvProxy_Int64ToInt64;
  291. }
  292. else
  293. {
  294. Assert(!"RecvPropInt var has invalid size");
  295. varProxy = RecvProxy_Int32ToInt8; // safest one...
  296. }
  297. }
  298. ret.m_pVarName = pVarName;
  299. ret.SetOffset( offset );
  300. ret.m_RecvType = (sizeofVar == 8) ? DPT_Int64 : DPT_Int;
  301. ret.m_Flags = flags;
  302. ret.SetProxyFn( varProxy );
  303. return ret;
  304. }
  305. RecvProp RecvPropString(
  306. char *pVarName,
  307. int offset,
  308. int bufferSize,
  309. int flags,
  310. RecvVarProxyFn varProxy
  311. )
  312. {
  313. RecvProp ret;
  314. ret.m_pVarName = pVarName;
  315. ret.SetOffset( offset );
  316. ret.m_RecvType = DPT_String;
  317. ret.m_Flags = flags;
  318. ret.m_StringBufferSize = bufferSize;
  319. ret.SetProxyFn( varProxy );
  320. return ret;
  321. }
  322. RecvProp RecvPropDataTable(
  323. char *pVarName,
  324. int offset,
  325. int flags,
  326. RecvTable *pTable,
  327. DataTableRecvVarProxyFn varProxy
  328. )
  329. {
  330. RecvProp ret;
  331. ret.m_pVarName = pVarName;
  332. ret.SetOffset( offset );
  333. ret.m_RecvType = DPT_DataTable;
  334. ret.m_Flags = flags;
  335. ret.SetDataTableProxyFn( varProxy );
  336. ret.SetDataTable( pTable );
  337. return ret;
  338. }
  339. RecvProp RecvPropArray3(
  340. char *pVarName,
  341. int offset,
  342. int sizeofVar,
  343. int elements,
  344. RecvProp pArrayProp,
  345. DataTableRecvVarProxyFn varProxy
  346. )
  347. {
  348. RecvProp ret;
  349. Assert( elements <= MAX_ARRAY_ELEMENTS );
  350. ret.m_pVarName = pVarName;
  351. ret.SetOffset( offset );
  352. ret.m_RecvType = DPT_DataTable;
  353. ret.SetDataTableProxyFn( varProxy );
  354. RecvProp *pProps = new RecvProp[elements]; // TODO free that again
  355. const char *pParentArrayPropName = AllocateStringHelper( "%s", pVarName );
  356. for ( int i=0; i < elements; i++ )
  357. {
  358. pProps[i] = pArrayProp; // copy basic property settings
  359. pProps[i].SetOffset( i * sizeofVar ); // adjust offset
  360. pProps[i].m_pVarName = s_ClientElementNames[i]; // give unique name
  361. pProps[i].SetParentArrayPropName( pParentArrayPropName ); // For debugging...
  362. }
  363. RecvTable *pTable = new RecvTable( pProps, elements, pVarName ); // TODO free that again
  364. ret.SetDataTable( pTable );
  365. return ret;
  366. }
  367. RecvProp InternalRecvPropArray(
  368. const int elementCount,
  369. const int elementStride,
  370. char *pName,
  371. ArrayLengthRecvProxyFn proxy
  372. )
  373. {
  374. RecvProp ret;
  375. ret.InitArray( elementCount, elementStride );
  376. ret.m_pVarName = pName;
  377. ret.SetArrayLengthProxy( proxy );
  378. return ret;
  379. }
  380. // ---------------------------------------------------------------------- //
  381. // Proxies.
  382. // ---------------------------------------------------------------------- //
  383. void RecvProxy_FloatToFloat( const CRecvProxyData *pData, void *pStruct, void *pOut )
  384. {
  385. Assert( IsFinite( pData->m_Value.m_Float ) );
  386. *((float*)pOut) = pData->m_Value.m_Float;
  387. }
  388. void RecvProxy_VectorToVector( const CRecvProxyData *pData, void *pStruct, void *pOut )
  389. {
  390. const float *v = pData->m_Value.m_Vector;
  391. Assert( IsFinite( v[0] ) && IsFinite( v[1] ) && IsFinite( v[2] ) );
  392. ((float*)pOut)[0] = v[0];
  393. ((float*)pOut)[1] = v[1];
  394. ((float*)pOut)[2] = v[2];
  395. }
  396. void RecvProxy_VectorXYToVectorXY( const CRecvProxyData *pData, void *pStruct, void *pOut )
  397. {
  398. const float *v = pData->m_Value.m_Vector;
  399. Assert( IsFinite( v[0] ) && IsFinite( v[1] ) );
  400. ((float*)pOut)[0] = v[0];
  401. ((float*)pOut)[1] = v[1];
  402. }
  403. void RecvProxy_QuaternionToQuaternion( const CRecvProxyData *pData, void *pStruct, void *pOut )
  404. {
  405. const float *v = pData->m_Value.m_Vector;
  406. Assert( IsFinite( v[0] ) && IsFinite( v[1] ) && IsFinite( v[2] ) && IsFinite( v[3] ) );
  407. ((float*)pOut)[0] = v[0];
  408. ((float*)pOut)[1] = v[1];
  409. ((float*)pOut)[2] = v[2];
  410. ((float*)pOut)[3] = v[3];
  411. }
  412. void RecvProxy_Int32ToInt8( const CRecvProxyData *pData, void *pStruct, void *pOut )
  413. {
  414. *((unsigned char*)pOut) = (unsigned char)pData->m_Value.m_Int;
  415. }
  416. void RecvProxy_Int32ToInt16( const CRecvProxyData *pData, void *pStruct, void *pOut )
  417. {
  418. *((unsigned short*)pOut) = (unsigned short)pData->m_Value.m_Int;
  419. }
  420. void RecvProxy_Int32ToInt32( const CRecvProxyData *pData, void *pStruct, void *pOut )
  421. {
  422. *((uint32*)pOut) = (uint32)pData->m_Value.m_Int;
  423. }
  424. void RecvProxy_Int32ToColor32( const CRecvProxyData *pData, void *pStruct, void *pOut )
  425. {
  426. //Always send/receive as little endian to preserve byte order across network byte swaps
  427. *((uint32*)pOut) = LittleDWord((uint32)pData->m_Value.m_Int);
  428. }
  429. void RecvProxy_Int64ToInt64( const CRecvProxyData *pData, void *pStruct, void *pOut )
  430. {
  431. *((int64*)pOut) = (int64)pData->m_Value.m_Int64;
  432. }
  433. void RecvProxy_StringToString( const CRecvProxyData *pData, void *pStruct, void *pOut )
  434. {
  435. char *pStrOut = (char*)pOut;
  436. if ( pData->m_pRecvProp->m_StringBufferSize <= 0 )
  437. {
  438. return;
  439. }
  440. for ( int i=0; i < pData->m_pRecvProp->m_StringBufferSize; i++ )
  441. {
  442. pStrOut[i] = pData->m_Value.m_pString[i];
  443. if ( pStrOut[i] == 0 )
  444. break;
  445. }
  446. pStrOut[pData->m_pRecvProp->m_StringBufferSize-1] = 0;
  447. }
  448. void DataTableRecvProxy_StaticDataTable( const RecvProp *pProp, void **pOut, void *pData, int objectID )
  449. {
  450. *pOut = pData;
  451. }
  452. void DataTableRecvProxy_PointerDataTable( const RecvProp *pProp, void **pOut, void *pData, int objectID )
  453. {
  454. *pOut = *((void**)pData);
  455. }
  456. #endif