Team Fortress 2 Source Code as on 22/4/2020
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.

534 lines
16 KiB

  1. //========= Copyright 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. const 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. #ifdef SUPPORTS_INT64
  125. m_Int64ToInt64 = RecvProxy_Int64ToInt64;
  126. #endif
  127. m_FloatToFloat = RecvProxy_FloatToFloat;
  128. m_VectorToVector = RecvProxy_VectorToVector;
  129. }
  130. CStandardRecvProxies g_StandardRecvProxies;
  131. // ---------------------------------------------------------------------- //
  132. // RecvProp.
  133. // ---------------------------------------------------------------------- //
  134. RecvProp::RecvProp()
  135. {
  136. m_pExtraData = NULL;
  137. m_pVarName = NULL;
  138. m_Offset = 0;
  139. m_RecvType = DPT_Int;
  140. m_Flags = 0;
  141. m_ProxyFn = NULL;
  142. m_DataTableProxyFn = NULL;
  143. m_pDataTable = NULL;
  144. m_nElements = 1;
  145. m_ElementStride = -1;
  146. m_pArrayProp = NULL;
  147. m_ArrayLengthProxy = NULL;
  148. m_bInsideArray = false;
  149. }
  150. // ---------------------------------------------------------------------- //
  151. // RecvTable.
  152. // ---------------------------------------------------------------------- //
  153. RecvTable::RecvTable()
  154. {
  155. Construct( NULL, 0, NULL );
  156. }
  157. RecvTable::RecvTable(RecvProp *pProps, int nProps, const char *pNetTableName)
  158. {
  159. Construct( pProps, nProps, pNetTableName );
  160. }
  161. RecvTable::~RecvTable()
  162. {
  163. }
  164. void RecvTable::Construct( RecvProp *pProps, int nProps, const char *pNetTableName )
  165. {
  166. m_pProps = pProps;
  167. m_nProps = nProps;
  168. m_pDecoder = NULL;
  169. m_pNetTableName = pNetTableName;
  170. m_bInitialized = false;
  171. m_bInMainList = false;
  172. }
  173. // ---------------------------------------------------------------------- //
  174. // Prop setup functions (for building tables).
  175. // ---------------------------------------------------------------------- //
  176. RecvProp RecvPropFloat(
  177. const char *pVarName,
  178. int offset,
  179. int sizeofVar,
  180. int flags,
  181. RecvVarProxyFn varProxy
  182. )
  183. {
  184. RecvProp ret;
  185. #ifdef _DEBUG
  186. if ( varProxy == RecvProxy_FloatToFloat )
  187. {
  188. Assert( sizeofVar == 0 || sizeofVar == 4 );
  189. }
  190. #endif
  191. ret.m_pVarName = pVarName;
  192. ret.SetOffset( offset );
  193. ret.m_RecvType = DPT_Float;
  194. ret.m_Flags = flags;
  195. ret.SetProxyFn( varProxy );
  196. return ret;
  197. }
  198. RecvProp RecvPropVector(
  199. const char *pVarName,
  200. int offset,
  201. int sizeofVar,
  202. int flags,
  203. RecvVarProxyFn varProxy
  204. )
  205. {
  206. RecvProp ret;
  207. #ifdef _DEBUG
  208. if ( varProxy == RecvProxy_VectorToVector )
  209. {
  210. Assert( sizeofVar == sizeof( Vector ) );
  211. }
  212. #endif
  213. ret.m_pVarName = pVarName;
  214. ret.SetOffset( offset );
  215. ret.m_RecvType = DPT_Vector;
  216. ret.m_Flags = flags;
  217. ret.SetProxyFn( varProxy );
  218. return ret;
  219. }
  220. RecvProp RecvPropVectorXY(
  221. const char *pVarName,
  222. int offset,
  223. int sizeofVar,
  224. int flags,
  225. RecvVarProxyFn varProxy
  226. )
  227. {
  228. RecvProp ret;
  229. #ifdef _DEBUG
  230. if ( varProxy == RecvProxy_VectorToVector )
  231. {
  232. Assert( sizeofVar == sizeof( Vector ) );
  233. }
  234. #endif
  235. ret.m_pVarName = pVarName;
  236. ret.SetOffset( offset );
  237. ret.m_RecvType = DPT_VectorXY;
  238. ret.m_Flags = flags;
  239. ret.SetProxyFn( varProxy );
  240. return ret;
  241. }
  242. #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!!!
  243. RecvProp RecvPropQuaternion(
  244. const char *pVarName,
  245. int offset,
  246. int sizeofVar, // Handled by RECVINFO macro, but set to SIZEOF_IGNORE if you don't want to bother.
  247. int flags,
  248. RecvVarProxyFn varProxy
  249. )
  250. {
  251. RecvProp ret;
  252. #ifdef _DEBUG
  253. if ( varProxy == RecvProxy_QuaternionToQuaternion )
  254. {
  255. Assert( sizeofVar == sizeof( Quaternion ) );
  256. }
  257. #endif
  258. ret.m_pVarName = pVarName;
  259. ret.SetOffset( offset );
  260. ret.m_RecvType = DPT_Quaternion;
  261. ret.m_Flags = flags;
  262. ret.SetProxyFn( varProxy );
  263. return ret;
  264. }
  265. #endif
  266. RecvProp RecvPropInt(
  267. const char *pVarName,
  268. int offset,
  269. int sizeofVar,
  270. int flags,
  271. RecvVarProxyFn varProxy
  272. )
  273. {
  274. RecvProp ret;
  275. // If they didn't specify a proxy, then figure out what type we're writing to.
  276. if (varProxy == NULL)
  277. {
  278. if (sizeofVar == 1)
  279. {
  280. varProxy = RecvProxy_Int32ToInt8;
  281. }
  282. else if (sizeofVar == 2)
  283. {
  284. varProxy = RecvProxy_Int32ToInt16;
  285. }
  286. else if (sizeofVar == 4)
  287. {
  288. varProxy = RecvProxy_Int32ToInt32;
  289. }
  290. #ifdef SUPPORTS_INT64
  291. else if (sizeofVar == 8)
  292. {
  293. varProxy = RecvProxy_Int64ToInt64;
  294. }
  295. #endif
  296. else
  297. {
  298. Assert(!"RecvPropInt var has invalid size");
  299. varProxy = RecvProxy_Int32ToInt8; // safest one...
  300. }
  301. }
  302. ret.m_pVarName = pVarName;
  303. ret.SetOffset( offset );
  304. #ifdef SUPPORTS_INT64
  305. ret.m_RecvType = (sizeofVar == 8) ? DPT_Int64 : DPT_Int;
  306. #else
  307. ret.m_RecvType = DPT_Int;
  308. #endif
  309. ret.m_Flags = flags;
  310. ret.SetProxyFn( varProxy );
  311. return ret;
  312. }
  313. RecvProp RecvPropString(
  314. const char *pVarName,
  315. int offset,
  316. int bufferSize,
  317. int flags,
  318. RecvVarProxyFn varProxy
  319. )
  320. {
  321. RecvProp ret;
  322. ret.m_pVarName = pVarName;
  323. ret.SetOffset( offset );
  324. ret.m_RecvType = DPT_String;
  325. ret.m_Flags = flags;
  326. ret.m_StringBufferSize = bufferSize;
  327. ret.SetProxyFn( varProxy );
  328. return ret;
  329. }
  330. RecvProp RecvPropDataTable(
  331. const char *pVarName,
  332. int offset,
  333. int flags,
  334. RecvTable *pTable,
  335. DataTableRecvVarProxyFn varProxy
  336. )
  337. {
  338. RecvProp ret;
  339. ret.m_pVarName = pVarName;
  340. ret.SetOffset( offset );
  341. ret.m_RecvType = DPT_DataTable;
  342. ret.m_Flags = flags;
  343. ret.SetDataTableProxyFn( varProxy );
  344. ret.SetDataTable( pTable );
  345. return ret;
  346. }
  347. RecvProp RecvPropArray3(
  348. const char *pVarName,
  349. int offset,
  350. int sizeofVar,
  351. int elements,
  352. RecvProp pArrayProp,
  353. DataTableRecvVarProxyFn varProxy
  354. )
  355. {
  356. RecvProp ret;
  357. Assert( elements <= MAX_ARRAY_ELEMENTS );
  358. ret.m_pVarName = pVarName;
  359. ret.SetOffset( offset );
  360. ret.m_RecvType = DPT_DataTable;
  361. ret.SetDataTableProxyFn( varProxy );
  362. RecvProp *pProps = new RecvProp[elements]; // TODO free that again
  363. const char *pParentArrayPropName = AllocateStringHelper( "%s", pVarName );
  364. for ( int i=0; i < elements; i++ )
  365. {
  366. pProps[i] = pArrayProp; // copy basic property settings
  367. pProps[i].SetOffset( i * sizeofVar ); // adjust offset
  368. pProps[i].m_pVarName = s_ClientElementNames[i]; // give unique name
  369. pProps[i].SetParentArrayPropName( pParentArrayPropName ); // For debugging...
  370. }
  371. RecvTable *pTable = new RecvTable( pProps, elements, pVarName ); // TODO free that again
  372. ret.SetDataTable( pTable );
  373. return ret;
  374. }
  375. RecvProp InternalRecvPropArray(
  376. const int elementCount,
  377. const int elementStride,
  378. const char *pName,
  379. ArrayLengthRecvProxyFn proxy
  380. )
  381. {
  382. RecvProp ret;
  383. ret.InitArray( elementCount, elementStride );
  384. ret.m_pVarName = pName;
  385. ret.SetArrayLengthProxy( proxy );
  386. return ret;
  387. }
  388. // ---------------------------------------------------------------------- //
  389. // Proxies.
  390. // ---------------------------------------------------------------------- //
  391. void RecvProxy_FloatToFloat( const CRecvProxyData *pData, void *pStruct, void *pOut )
  392. {
  393. Assert( IsFinite( pData->m_Value.m_Float ) );
  394. *((float*)pOut) = pData->m_Value.m_Float;
  395. }
  396. void RecvProxy_VectorToVector( 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] ) && IsFinite( v[2] ) );
  400. ((float*)pOut)[0] = v[0];
  401. ((float*)pOut)[1] = v[1];
  402. ((float*)pOut)[2] = v[2];
  403. }
  404. void RecvProxy_VectorXYToVectorXY( const CRecvProxyData *pData, void *pStruct, void *pOut )
  405. {
  406. const float *v = pData->m_Value.m_Vector;
  407. Assert( IsFinite( v[0] ) && IsFinite( v[1] ) );
  408. ((float*)pOut)[0] = v[0];
  409. ((float*)pOut)[1] = v[1];
  410. }
  411. void RecvProxy_QuaternionToQuaternion( const CRecvProxyData *pData, void *pStruct, void *pOut )
  412. {
  413. const float *v = pData->m_Value.m_Vector;
  414. Assert( IsFinite( v[0] ) && IsFinite( v[1] ) && IsFinite( v[2] ) && IsFinite( v[3] ) );
  415. ((float*)pOut)[0] = v[0];
  416. ((float*)pOut)[1] = v[1];
  417. ((float*)pOut)[2] = v[2];
  418. ((float*)pOut)[3] = v[3];
  419. }
  420. void RecvProxy_Int32ToInt8( const CRecvProxyData *pData, void *pStruct, void *pOut )
  421. {
  422. *((unsigned char*)pOut) = (unsigned char)pData->m_Value.m_Int;
  423. }
  424. void RecvProxy_Int32ToInt16( const CRecvProxyData *pData, void *pStruct, void *pOut )
  425. {
  426. *((unsigned short*)pOut) = (unsigned short)pData->m_Value.m_Int;
  427. }
  428. void RecvProxy_Int32ToInt32( const CRecvProxyData *pData, void *pStruct, void *pOut )
  429. {
  430. *((unsigned long*)pOut) = (unsigned long)pData->m_Value.m_Int;
  431. }
  432. #ifdef SUPPORTS_INT64
  433. void RecvProxy_Int64ToInt64( const CRecvProxyData *pData, void *pStruct, void *pOut )
  434. {
  435. *((int64*)pOut) = (int64)pData->m_Value.m_Int64;
  436. }
  437. #endif
  438. void RecvProxy_StringToString( const CRecvProxyData *pData, void *pStruct, void *pOut )
  439. {
  440. char *pStrOut = (char*)pOut;
  441. if ( pData->m_pRecvProp->m_StringBufferSize <= 0 )
  442. {
  443. return;
  444. }
  445. for ( int i=0; i < pData->m_pRecvProp->m_StringBufferSize; i++ )
  446. {
  447. pStrOut[i] = pData->m_Value.m_pString[i];
  448. if ( pStrOut[i] == 0 )
  449. break;
  450. }
  451. pStrOut[pData->m_pRecvProp->m_StringBufferSize-1] = 0;
  452. }
  453. void DataTableRecvProxy_StaticDataTable( const RecvProp *pProp, void **pOut, void *pData, int objectID )
  454. {
  455. *pOut = pData;
  456. }
  457. void DataTableRecvProxy_PointerDataTable( const RecvProp *pProp, void **pOut, void *pData, int objectID )
  458. {
  459. *pOut = *((void**)pData);
  460. }
  461. #endif