1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//=============================================================================
#include "cbase.h"
#include "econ_item_inventory.h"
#include "vgui/ILocalize.h"
#include "tier3/tier3.h"
#include "econ_item_system.h"
#include "econ_item.h"
#include "econ_gcmessages.h"
#include "shareddefs.h"
#include "filesystem.h"
#include "econ_item_description.h" // only for CSteamAccountIDAttributeCollector
#ifdef CLIENT_DLL
#include <igameevents.h>
#include "econ_game_account_client.h"
#include "ienginevgui.h"
#include "econ_ui.h"
#include "item_pickup_panel.h"
#include "econ/econ_item_preset.h"
#include "econ/confirm_dialog.h"
#include "tf_xp_source.h"
#include "tf_notification.h"
#else
#include "props_shared.h"
#include "basemultiplayerplayer.h"
#endif
#if defined(TF_CLIENT_DLL) || defined(TF_DLL)
#include "tf_gcmessages.h"
#include "tf_duel_summary.h"
#include "econ_contribution.h"
#include "tf_player_info.h"
#include "econ/econ_claimcode.h"
#include "tf_wardata.h"
#include "tf_ladder_data.h"
#include "tf_rating_data.h"
#endif
#if defined(TF_DLL) && defined(GAME_DLL)
#include "tf_gc_api.h"
#include "econ/econ_game_account_server.h"
#endif
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
using namespace GCSDK;
#ifdef _DEBUG
ConVar item_inventory_debug( "item_inventory_debug", "0", FCVAR_REPLICATED | FCVAR_CHEAT );
#endif
#ifdef USE_DYNAMIC_ASSET_LOADING
//extern ConVar item_dynamicload;
#endif
#define ITEM_CLIENTACK_FILE "item_clientacks.txt"
#ifdef _DEBUG
#ifdef CLIENT_DLL
ConVar item_debug_clientacks( "item_debug_clientacks", "0", FCVAR_CLIENTDLL | FCVAR_ARCHIVE );
#endif
#endif // _DEBUG
// Result codes strings for GC results.
const char* GCResultString[8] =
{
"k_EGCMsgResponseOK", // Request succeeded
"k_EGCMsgResponseDenied", // Request denied
"k_EGCMsgResponseServerError", // Request failed due to a temporary server error
"k_EGCMsgResponseTimeout", // Request timed out
"k_EGCMsgResponseInvalid", // Request was corrupt
"k_EGCMsgResponseNoMatch", // No item definition matched the request
"k_EGCMsgResponseUnknownError", // Request failed with an unknown error
"k_EGCMsgResponseNotLoggedOn", // Client not logged on to steam
};
CBasePlayer *GetPlayerBySteamID( const CSteamID &steamID )
{
CSteamID steamIDPlayer;
for ( int i = 1; i <= gpGlobals->maxClients; i++ )
{
CBasePlayer *pPlayer = UTIL_PlayerByIndex( i );
if ( pPlayer == NULL )
continue;
if ( pPlayer->GetSteamID( &steamIDPlayer ) == false )
continue;
if ( steamIDPlayer == steamID )
return pPlayer;
}
return NULL;
}
// Inventory Less function.
// Used to sort the inventory items into their positions.
bool CInventoryListLess::Less( const CEconItemView &src1, const CEconItemView &src2, void *pCtx )
{
int iPos1 = src1.GetInventoryPosition();
int iPos2 = src2.GetInventoryPosition();
// Context can be specified to point to a func that extracts the position from the backend position.
// Necessary if your inventory packs a bunch of info into the position instead of using it just as a position.
if ( pCtx )
{
CPlayerInventory *pInv = (CPlayerInventory*)pCtx;
iPos1 = pInv->ExtractInventorySortPosition( iPos1 );
iPos2 = pInv->ExtractInventorySortPosition( iPos2 );
}
if ( iPos1 < iPos2 )
return true;
return false;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CInventoryManager::CInventoryManager( void )
#ifdef CLIENT_DLL
: m_mapPersonaNamesCache( DefLessFunc( uint32 ) )
, m_sPersonaStateChangedCallback( this, &CInventoryManager::OnPersonaStateChanged )
, m_personaNameRequests( DefLessFunc( uint64 ) )
#endif
{
#ifdef CLIENT_DLL
m_pkvItemClientAckFile = NULL;
m_bClientAckDirty = false;
m_iPredictedDiscards = 0;
m_flNextLoadPresetChange = 0.0f;
#endif
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CInventoryManager::SteamRequestInventory( CPlayerInventory *pInventory, CSteamID pSteamID, IInventoryUpdateListener *pListener )
{
// SteamID must be valid
if ( !pSteamID.IsValid() || !pSteamID.BIndividualAccount() )
{
if ( !HushAsserts() )
{
Assert( pSteamID.IsValid() );
Assert( pSteamID.BIndividualAccount() );
}
return;
}
// If we haven't seen this inventory before, register it
bool bFound = false;
for ( int i = 0; i < m_pInventories.Count(); i++ )
{
if ( m_pInventories[i].pInventory == pInventory )
{
bFound = true;
break;
}
}
if ( !bFound )
{
int iIdx = m_pInventories.AddToTail();
m_pInventories[iIdx].pInventory = pInventory;
m_pInventories[iIdx].pListener = pListener;
}
// Add the request to our list of pending requests
int iIdx = m_hPendingInventoryRequests.AddToTail();
m_hPendingInventoryRequests[iIdx].pID = pSteamID;
m_hPendingInventoryRequests[iIdx].pInventory = pInventory;
pInventory->RequestInventory( pSteamID );
if( pListener )
{
pInventory->AddListener( pListener );
}
}
//-----------------------------------------------------------------------------
// Purpose: Called when a gameserver connects to steam.
//-----------------------------------------------------------------------------
void CInventoryManager::GameServerSteamAPIActivated()
{
#if defined(TF_DLL) && defined(GAME_DLL)
GameCoordinator_NotifyGameState();
#endif
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CPlayerInventory *CInventoryManager::GetInventoryForAccount( uint32 iAccountID )
{
FOR_EACH_VEC( m_pInventories, i )
{
if ( m_pInventories[i].pInventory->GetOwner().GetAccountID() == iAccountID )
return m_pInventories[i].pInventory;
}
return NULL;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CInventoryManager::DeregisterInventory( CPlayerInventory *pInventory )
{
int iCount = m_pInventories.Count();
for ( int i = iCount-1; i >= 0; i-- )
{
if ( m_pInventories[i].pInventory == pInventory )
{
m_pInventories.Remove(i);
}
}
}
#ifdef CLIENT_DLL
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
bool CInventoryManager::IsPresetIndexValid( equipped_preset_t unPreset )
{
const bool bResult = GetItemSchema()->IsValidPreset( unPreset );
AssertMsg( bResult, "Invalid preset index!" );
return bResult;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
bool CInventoryManager::LoadPreset( equipped_class_t unClass, equipped_preset_t unPreset )
{
if ( !IsValidPlayerClass( unClass ) )
return false;
if ( !IsPresetIndexValid( unPreset ) )
return false;
if ( !GetLocalInventory()->GetSOC() )
return false;
if ( m_flNextLoadPresetChange > gpGlobals->realtime )
{
Msg( "Loadout change denied. Changing presets too quickly.\n" );
return false;
}
m_flNextLoadPresetChange = gpGlobals->realtime + 0.5f;
GCSDK::CProtoBufMsg<CMsgSelectPresetForClass> msg( k_EMsgGCPresets_SelectPresetForClass );
msg.Body().set_class_id( unClass );
msg.Body().set_preset_id( unPreset );
GCClientSystem()->BSendMessage( msg );
return true;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CInventoryManager::UpdateLocalInventory( void )
{
if ( steamapicontext->SteamUser() && GetLocalInventory() )
{
CSteamID steamID = steamapicontext->SteamUser()->GetSteamID();
if ( steamID.IsValid() ) // make sure we're logged in and we know who we are
{
SteamRequestInventory( GetLocalInventory(), steamID );
}
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CInventoryManager::OnPersonaStateChanged( PersonaStateChange_t *info )
{
if ( ( info->m_nChangeFlags & k_EPersonaChangeName ) != 0 )
m_personaNameRequests.InsertOrReplace( info->m_ulSteamID, true );
}
#endif
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
bool CInventoryManager::Init( void )
{
return true;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CInventoryManager::PostInit( void )
{
// Initialize the item system.
ItemSystem()->Init();
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CInventoryManager::PreInitGC()
{
REG_SHARED_OBJECT_SUBCLASS( CEconItem );
#if defined (CLIENT_DLL)
REG_SHARED_OBJECT_SUBCLASS( CEconGameAccountClient );
REG_SHARED_OBJECT_SUBCLASS( CEconItemPerClassPresetData );
REG_SHARED_OBJECT_SUBCLASS( CSOTFMatchResultPlayerInfo );
REG_SHARED_OBJECT_SUBCLASS( CXPSource );
REG_SHARED_OBJECT_SUBCLASS( CTFNotification );
#endif
#if defined(TF_CLIENT_DLL) || defined(TF_DLL)
REG_SHARED_OBJECT_SUBCLASS( CWarData );
REG_SHARED_OBJECT_SUBCLASS( CTFDuelSummary );
REG_SHARED_OBJECT_SUBCLASS( CTFMapContribution );
REG_SHARED_OBJECT_SUBCLASS( CTFPlayerInfo );
REG_SHARED_OBJECT_SUBCLASS( CEconClaimCode );
REG_SHARED_OBJECT_SUBCLASS( CSOTFLadderData );
#endif
#ifdef TF_DLL
REG_SHARED_OBJECT_SUBCLASS( CEconGameAccountForGameServers );
#endif // TF_DLL
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CInventoryManager::PostInitGC()
{
#ifdef CLIENT_DLL
// The client immediately loads the local player's inventory
UpdateLocalInventory();
#endif
}
//-----------------------------------------------------------------------------
void CInventoryManager::Shutdown()
{
int nInventoryCount = m_pInventories.Count();
for ( int iInventory = 0; iInventory < nInventoryCount; ++iInventory )
{
CPlayerInventory *pInventory = m_pInventories[iInventory].pInventory;
if ( pInventory )
{
pInventory->Clear();
}
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CInventoryManager::LevelInitPreEntity( void )
{
// Throw out any testitem definitions
for ( int i = 0; i < TI_TYPE_COUNT; i++ )
{
int iNewDef = TESTITEM_DEFINITIONS_BEGIN_AT + i;
ItemSystem()->GetItemSchema()->ItemTesting_DiscardTestDefinition( iNewDef );
}
// Precache all item models we've got
#ifdef GAME_DLL
CUtlVector<const char *> vecPrecacheModelStrings;
#endif // GAME_DLL
const CEconItemSchema::ItemDefinitionMap_t& mapItemDefs = ItemSystem()->GetItemSchema()->GetItemDefinitionMap();
FOR_EACH_MAP_FAST( mapItemDefs, i )
{
CEconItemDefinition *pData = mapItemDefs[i];
pData->SetHasBeenLoaded( true );
#ifdef GAME_DLL
bool bDynamicLoad = false;
#ifdef USE_DYNAMIC_ASSET_LOADING
bDynamicLoad = true;//item_dynamicload.GetBool();
#endif // USE_DYNAMIC_ASSET_LOADING
pData->GeneratePrecacheModelStrings( bDynamicLoad, &vecPrecacheModelStrings );
// Precache the models and the gibs for everything the definition requested.
FOR_EACH_VEC( vecPrecacheModelStrings, i )
{
// Ignore any objects which requested an empty precache string for whatever reason.
if ( vecPrecacheModelStrings[i] && vecPrecacheModelStrings[i][0] )
{
int iModelIndex = CBaseEntity::PrecacheModel( vecPrecacheModelStrings[i] );
PrecacheGibsForModel( iModelIndex );
}
}
vecPrecacheModelStrings.RemoveAll();
pData->GeneratePrecacheSoundStrings( bDynamicLoad, &vecPrecacheModelStrings );
// Precache the sounds for everything
FOR_EACH_VEC( vecPrecacheModelStrings, i )
{
// Ignore any objects which requested an empty precache string for whatever reason.
if ( vecPrecacheModelStrings[i] && vecPrecacheModelStrings[i][0] )
{
CBaseEntity::PrecacheScriptSound( vecPrecacheModelStrings[i] );
}
}
vecPrecacheModelStrings.RemoveAll();
#endif
}
// We reset the cached attribute class strings, since it's invalidated by level changes
ItemSystem()->ResetAttribStringCache();
#ifdef GAME_DLL
ItemSystem()->ReloadWhitelist();
#endif
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CInventoryManager::LevelShutdownPostEntity( void )
{
// We reset the cached attribute class strings, since it's invalidated by level changes
ItemSystem()->ResetAttribStringCache();
}
//-----------------------------------------------------------------------------
// Purpose: Lets the client know that we're now connected to the GC
//-----------------------------------------------------------------------------
#ifdef CLIENT_DLL
void CInventoryManager::SendGCConnectedEvent( void )
{
IGameEvent *event = gameeventmanager->CreateEvent( "gc_connected" );
if ( event )
{
gameeventmanager->FireEventClientSide( event );
}
}
#endif
#if !defined(NO_STEAM)
//-----------------------------------------------------------------------------
// Purpose: GC Msg handler to receive the dev "new item" response
//-----------------------------------------------------------------------------
class CGCDev_NewItemRequestResponse : public GCSDK::CGCClientJob
{
public:
CGCDev_NewItemRequestResponse( GCSDK::CGCClient *pClient ) : GCSDK::CGCClientJob( pClient ) {}
virtual bool BYieldingRunGCJob( GCSDK::IMsgNetPacket *pNetPacket )
{
GCSDK::CGCMsg<MsgGCStandardResponse_t> msg( pNetPacket );
if ( msg.Body().m_eResponse == k_EGCMsgResponseOK )
{
Msg("Received new item acknowledgement: %s\n", GCResultString[msg.Body().m_eResponse] );
}
else
{
Warning("Failed to generate new item: %s\n", GCResultString[msg.Body().m_eResponse] );
}
return true;
}
};
GC_REG_JOB( GCSDK::CGCClient, CGCDev_NewItemRequestResponse, "CGCDev_NewItemRequestResponse", k_EMsgGCDev_NewItemRequestResponse, GCSDK::k_EServerTypeGCClient );
#endif // NO_STEAM
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CInventoryManager::RemovePendingRequest( CSteamID *pSteamID )
{
#ifdef CLIENT_DLL
// Only the client, all requests are for the local player. Clear them all.
m_hPendingInventoryRequests.Purge();
return;
#endif
// On the server, remove all requests for the specified steam id
int iCount = m_hPendingInventoryRequests.Count();
for ( int i = iCount-1; i >= 0; i-- )
{
if ( m_hPendingInventoryRequests[i].pID == *pSteamID )
{
m_hPendingInventoryRequests.Remove(i);
}
}
}
#ifdef CLIENT_DLL
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CInventoryManager::DropItem( itemid_t iItemID )
{
static CSchemaAttributeDefHandle pAttrDef_NoDelete( "cannot delete" );
// Double check that this item can be delete
CEconItemView *pItem = GetLocalInventory()->GetInventoryItemByItemID( iItemID );
if ( !pItem || !pAttrDef_NoDelete || pItem->FindAttribute( pAttrDef_NoDelete ) )
{
return;
}
GCSDK::CGCMsg<MsgGCDelete_t> msg( k_EMsgGCDelete );
msg.Body().m_unItemID = iItemID;
GCClientSystem()->BSendMessage( msg );
// Keep track of how many items we've discarded, but haven't received responses for.
m_iPredictedDiscards++;
}
//-----------------------------------------------------------------------------
// Purpose: Delete any items we can't find static data for. This can happen when we're testing
// internally, and then remove an item. Shouldn't ever happen in the wild.
//-----------------------------------------------------------------------------
int CInventoryManager::DeleteUnknowns( CPlayerInventory *pInventory )
{
// We need to manually walk the main inventory's SOC, because unknown items won't be in the inventory
GCSDK::CGCClientSharedObjectCache *pSOC = pInventory->GetSOC();
if ( pSOC )
{
int iBadItems = 0;
CGCClientSharedObjectTypeCache *pTypeCache = pSOC->FindTypeCache( CEconItem::k_nTypeID );
if( pTypeCache )
{
for( uint32 unItem = 0; unItem < pTypeCache->GetCount(); unItem++ )
{
CEconItem *pItem = (CEconItem *)pTypeCache->GetObject( unItem );
if ( pItem )
{
CEconItemDefinition *pData = ItemSystem()->GetStaticDataForItemByDefIndex( pItem->GetDefinitionIndex() );
if ( !pData )
{
DropItem( pItem->GetItemID() );
iBadItems++;
}
}
}
}
return iBadItems;
}
return 0;
}
//-----------------------------------------------------------------------------
// Purpose: Tries to move the specified item into the player's backpack.
// FAILS if the backpack is full. Returns false in that case.
//-----------------------------------------------------------------------------
bool CInventoryManager::SetItemBackpackPosition( CEconItemView *pItem, uint32 iPosition, bool bForceUnequip, bool bAllowOverflow )
{
CPlayerInventory *pInventory = GetLocalInventory();
if ( !pInventory )
return false;
const int iMaxItems = pInventory->GetMaxItemCount();
if ( !iPosition )
{
// Build a list of empty slots. We track extra slots beyond the backpack for overflow.
CUtlVector< bool > bFilledSlots;
bFilledSlots.SetSize( iMaxItems * 2 );
for ( int i = 0; i < bFilledSlots.Count(); ++i )
{
bFilledSlots[i] = false;
}
for ( int i = 0; i < pInventory->GetItemCount(); i++ )
{
CEconItemView *pTmpItem = pInventory->GetItem(i);
// Ignore the item we're moving.
if ( pTmpItem == pItem )
continue;
int iBackpackPos = GetBackpackPositionFromBackend( pTmpItem->GetInventoryPosition() );
if ( iBackpackPos >= 0 && iBackpackPos < bFilledSlots.Count() )
{
bFilledSlots[iBackpackPos] = true;
}
}
// Add predicted filled slots
for ( int i = 0; i < m_PredictedFilledSlots.Count(); i++ )
{
int iBackpackPos = m_PredictedFilledSlots[i];
if ( iBackpackPos >= 0 && iBackpackPos < bFilledSlots.Count() )
{
bFilledSlots[iBackpackPos] = true;
}
}
// Now find an empty slot
for ( int i = 1; i < bFilledSlots.Count(); i++ )
{
if ( !bFilledSlots[i] )
{
iPosition = i;
break;
}
}
if ( !iPosition )
return false;
}
if ( !bAllowOverflow && iPosition > (uint32)iMaxItems )
return false;
//Warning("Moved item %llu to backpack slot: %d\n", pItem->GetItemID(), iPosition );
uint32 iBackendPosition = bForceUnequip ? 0 : pItem->GetInventoryPosition();
SetBackpackPosition( &iBackendPosition, iPosition );
UpdateInventoryPosition( pInventory, pItem->GetItemID(), iBackendPosition );
m_PredictedFilledSlots.AddToTail( iPosition );
return true;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CInventoryManager::MoveItemToBackpackPosition( CEconItemView *pItem, int iBackpackPosition )
{
CEconItemView *pOldItem = GetItemByBackpackPosition( iBackpackPosition );
if ( pOldItem )
{
// Move the item in the new spot to our current spot
SetItemBackpackPosition( pOldItem, GetBackpackPositionFromBackend(pItem->GetInventoryPosition()) );
//Warning("Moved OLD item %llu to backpack slot: %d\n", pOldItem->GetItemID(), GetBackpackPositionFromBackend(iBackendPosition) );
}
// Move the item to the new spot
SetItemBackpackPosition( pItem, iBackpackPosition );
//Warning("Moved item %llu to backpack slot: %d\n", pItem->GetItemID(), iBackpackPosition );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
class CWaitForBackpackSortFinishDialog : public CGenericWaitingDialog
{
public:
CWaitForBackpackSortFinishDialog( vgui::Panel *pParent ) : CGenericWaitingDialog( pParent )
{
}
protected:
virtual void OnTimeout()
{
InventoryManager()->SortBackpackFinished();
}
};
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CInventoryManager::SortBackpackBy( uint32 iSortType )
{
GCSDK::CProtoBufMsg<CMsgSortItems> msg( k_EMsgGCSortItems );
msg.Body().set_sort_type( iSortType );
GCClientSystem()->BSendMessage( msg );
ShowWaitingDialog( new CWaitForBackpackSortFinishDialog( NULL ), "#BackpackSortExplanation_Title", true, false, 3.0f );
m_bInBackpackSort = true;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CInventoryManager::SortBackpackFinished( void )
{
m_bInBackpackSort = false;
GetLocalInventory()->SendInventoryUpdateEvent();
}
//-----------------------------------------------------------------------------
// Purpose: GC Msg handler to receive the sort finished message
//-----------------------------------------------------------------------------
class CGBackpackSortFinished : public GCSDK::CGCClientJob
{
public:
CGBackpackSortFinished( GCSDK::CGCClient *pClient ) : GCSDK::CGCClientJob( pClient ) {}
virtual bool BYieldingRunGCJob( GCSDK::IMsgNetPacket *pNetPacket )
{
CloseWaitingDialog();
InventoryManager()->SortBackpackFinished();
return true;
}
};
GC_REG_JOB( GCSDK::CGCClient, CGBackpackSortFinished, "CGBackpackSortFinished", k_EMsgGCBackpackSortFinished, GCSDK::k_EServerTypeGCClient );
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CInventoryManager::UpdateInventoryPosition( CPlayerInventory *pInventory, uint64 ulItemID, uint32 unNewInventoryPos )
{
if ( !pInventory->GetInventoryItemByItemID( ulItemID ) )
{
Warning("Attempt to update inventory position failure: %s.\n", "could not find matching item ID");
return;
}
if ( !pInventory->GetSOCDataForItem( ulItemID ) )
{
Warning("Attempt to update inventory position failure: %s\n", "could not find SOC data for item");
return;
}
// In the incredibly rare case where the GC crashed while sorting our backpack, we won't have gotten
// a k_EMsgGCBackpackSortFinished message. Assume that if we're requesting a manual move of an item, we're not sorting anymore.
m_bInBackpackSort = false;
// TF has multiple ways of using the inventory position bits. For all inventory positions moving forward, assume
// they're in the new format.
#if defined(TF_CLIENT_DLL) || defined(TF_DLL)
if ( unNewInventoryPos != 0 )
{
unNewInventoryPos |= kBackendPosition_NewFormat;
}
#endif // defined(TF_CLIENT_DLL) || defined(TF_DLL)
// Queue a message to be sent to the GC
CMsgSetItemPositions_ItemPosition *pMsg = m_msgPendingSetItemPositions.add_item_positions();
pMsg->set_item_id( ulItemID );
pMsg->set_position( unNewInventoryPos );
}
void CInventoryManager::Update( float frametime )
{
// Check if we have any pending item position changes that we need to flush out
if ( m_msgPendingSetItemPositions.item_positions_size() > 0 )
{
// !KLUDGE! It would be nice if we could just send this in one line instead of making a copy
CProtoBufMsg<CMsgSetItemPositions> msg( k_EMsgGCSetItemPositions );
msg.Body() = m_msgPendingSetItemPositions;
GCClientSystem()->BSendMessage( msg );
m_msgPendingSetItemPositions.Clear();
}
// Check if we have any pending account lookups to batch up
if ( m_msgPendingLookupAccountNames.accountids_size() > 0 )
{
// !KLUDGE! It would be nice if we could just send this in one line instead of making a copy
CProtoBufMsg< CMsgLookupMultipleAccountNames > msg( k_EMsgGCLookupMultipleAccountNames );
msg.Body() = m_msgPendingLookupAccountNames;
GCClientSystem()->BSendMessage( msg );
m_msgPendingLookupAccountNames.Clear();
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CInventoryManager::UpdateInventoryEquippedState( CPlayerInventory *pInventory, uint64 ulItemID, equipped_class_t unClass, equipped_slot_t unSlot )
{
// passing in INVALID_ITEM_ID means "unequip from this slot"
if ( ulItemID != INVALID_ITEM_ID )
{
if ( !pInventory->GetInventoryItemByItemID( ulItemID ) )
{
//Warning("Attempt to update equipped state failure: %s.\n", "could not find matching item ID");
return;
}
if ( !pInventory->GetSOCDataForItem( ulItemID ) )
{
//Warning("Attempt to update equipped state failure: %s\n", "could not find SOC data for item");
return;
}
}
CProtoBufMsg<CMsgAdjustItemEquippedState> msg( k_EMsgGCAdjustItemEquippedState );
msg.Body().set_item_id( ulItemID );
msg.Body().set_new_class( unClass );
msg.Body().set_new_slot( unSlot );
GCClientSystem()->BSendMessage( msg );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
bool CInventoryManager::ShowItemsPickedUp( bool bForce, bool bReturnToGame, bool bNoPanel )
{
CPlayerInventory *pLocalInv = GetLocalInventory();
if ( !pLocalInv )
return false;
// Don't bring it up if we're already browsing something in the gameUI
vgui::VPANEL gameuiPanel = enginevgui->GetPanel( PANEL_GAMEUIDLL );
if ( !bForce && vgui::ipanel()->IsVisible( gameuiPanel ) )
return false;
CUtlVector<CEconItemView*> aItemsFound;
// Go through the root inventory and find any items that are in the "found" position
int iCount = pLocalInv->GetItemCount();
for ( int i = 0; i < iCount; i++ )
{
CEconItemView *pTmp = pLocalInv->GetItem(i);
if ( !pTmp )
continue;
if ( pTmp->GetStaticData()->IsHidden() )
continue;
uint32 iPosition = pTmp->GetInventoryPosition();
if ( IsUnacknowledged(iPosition) == false )
continue;
if ( GetBackpackPositionFromBackend(iPosition) != 0 )
continue;
// Now make sure we haven't got a clientside saved ack for this item.
// This makes sure we don't show multiple pickups for items that we've found,
// but haven't been able to move out of unack'd position due to the GC being unavailable.
if ( HasBeenAckedByClient( pTmp ) )
continue;
aItemsFound.AddToTail( pTmp );
}
if ( !aItemsFound.Count() )
return CheckForRoomAndForceDiscard();
// We're not forcing the player to make room yet. Just show the pickup panel.
CItemPickupPanel *pItemPanel = bNoPanel ? NULL : EconUI()->OpenItemPickupPanel();
if ( pItemPanel )
{
pItemPanel->SetReturnToGame( bReturnToGame );
}
for ( int i = 0; i < aItemsFound.Count(); i++ )
{
if ( pItemPanel )
{
pItemPanel->AddItem( aItemsFound[i] );
}
else
{
AcknowledgeItem( aItemsFound[i] );
}
}
if ( pItemPanel )
{
pItemPanel->MoveToFront();
}
else
{
SaveAckFile();
}
aItemsFound.Purge();
return true;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
bool CInventoryManager::CheckForRoomAndForceDiscard( void )
{
CPlayerInventory *pLocalInv = GetLocalInventory();
if ( !pLocalInv )
return false;
// Go through the inventory and attempt to move any items outside the backpack into valid positions.
// Remember the first item that we failed to move, so we can force a discard later.
CEconItemView *pItem = NULL;
const int iMaxItems = pLocalInv->GetMaxItemCount();
int iCount = pLocalInv->GetItemCount();
for ( int i = 0; i < iCount; i++ )
{
CEconItemView *pTmp = pLocalInv->GetItem(i);
if ( !pTmp )
continue;
if ( pTmp->GetStaticData()->IsHidden() )
continue;
uint32 iPosition = pTmp->GetInventoryPosition();
if ( IsUnacknowledged(iPosition) || GetBackpackPositionFromBackend(iPosition) > iMaxItems )
{
if ( !SetItemBackpackPosition( pTmp, 0, false, false ) )
{
pItem = pTmp;
break;
}
}
}
// If we're not over the limit, we're done.
if ( ( iCount - m_iPredictedDiscards ) <= iMaxItems )
return false;
if ( !pItem )
return false;
// We're forcing the player to make room for items he's found. Bring up that panel with the first item over the limit.
CItemDiscardPanel *pDiscardPanel = EconUI()->OpenItemDiscardPanel();
pDiscardPanel->SetItem( pItem );
return true;
}
//-----------------------------------------------------------------------------
// Purpose: Client Acknowledges an item and moves it in to the backpack
//-----------------------------------------------------------------------------
void CInventoryManager::AcknowledgeItem ( CEconItemView *pItem, bool bMoveToBackpack /* = true */ )
{
SetAckedByClient( pItem );
int iMethod = GetUnacknowledgedReason( pItem->GetInventoryPosition() ) - 1;
if ( iMethod >= ARRAYSIZE( g_pszItemPickupMethodStringsUnloc ) || iMethod < 0 )
iMethod = 0;
EconUI()->Gamestats_ItemTransaction( IE_ITEM_RECEIVED, pItem, g_pszItemPickupMethodStringsUnloc[iMethod] );
// Then move it to the first empty backpack position
if ( bMoveToBackpack )
{
SetItemBackpackPosition( pItem, 0, false, true );
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CEconItemView *CInventoryManager::GetItemByBackpackPosition( int iBackpackPosition )
{
CPlayerInventory *pInventory = GetLocalInventory();
if ( !pInventory )
return NULL;
// Backpack positions start from 1
Assert( iBackpackPosition > 0 && iBackpackPosition <= pInventory->GetMaxItemCount() );
for ( int i = 0; i < pInventory->GetItemCount(); i++ )
{
CEconItemView *pItem = pInventory->GetItem(i);
if ( GetBackpackPositionFromBackend( pItem->GetInventoryPosition() ) == iBackpackPosition )
return pItem;
}
return NULL;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
bool CInventoryManager::HasBeenAckedByClient( CEconItemView *pItem )
{
return ( GetAckKeyForItem( pItem ) != NULL );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CInventoryManager::SetAckedByClient( CEconItemView *pItem )
{
VerifyAckFileLoaded();
static char szTmp[128];
Q_snprintf( szTmp, sizeof(szTmp), "%llu", pItem->GetItemID() );
m_pkvItemClientAckFile->SetInt( szTmp, 1 );
m_bClientAckDirty = true;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CInventoryManager::SetAckedByGC( CEconItemView *pItem, bool bSave )
{
KeyValues *pkvItem = GetAckKeyForItem( pItem );
if ( pkvItem )
{
m_pkvItemClientAckFile->RemoveSubKey( pkvItem );
pkvItem->deleteThis();
m_bClientAckDirty = true;
if ( bSave )
{
SaveAckFile();
}
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
KeyValues *CInventoryManager::GetAckKeyForItem( CEconItemView *pItem )
{
VerifyAckFileLoaded();
static char szTmp[128];
Q_snprintf( szTmp, sizeof(szTmp), "%llu", pItem->GetItemID() );
return m_pkvItemClientAckFile->FindKey( szTmp );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CInventoryManager::VerifyAckFileLoaded( void )
{
if ( m_pkvItemClientAckFile )
return;
m_pkvItemClientAckFile = new KeyValues( ITEM_CLIENTACK_FILE );
ISteamRemoteStorage *pRemoteStorage = SteamClient()?(ISteamRemoteStorage *)SteamClient()->GetISteamGenericInterface(
SteamAPI_GetHSteamUser(), SteamAPI_GetHSteamPipe(), STEAMREMOTESTORAGE_INTERFACE_VERSION ):NULL;
if ( pRemoteStorage )
{
if ( pRemoteStorage->FileExists(ITEM_CLIENTACK_FILE) )
{
int32 nFileSize = pRemoteStorage->GetFileSize( ITEM_CLIENTACK_FILE );
if ( nFileSize > 0 )
{
CUtlBuffer buf( 0, nFileSize );
if ( pRemoteStorage->FileRead( ITEM_CLIENTACK_FILE, buf.Base(), nFileSize ) == nFileSize )
{
buf.SeekPut( CUtlBuffer::SEEK_HEAD, nFileSize );
m_pkvItemClientAckFile->ReadAsBinary( buf );
#ifdef _DEBUG
if ( item_debug_clientacks.GetBool() )
{
m_pkvItemClientAckFile->SaveToFile( g_pFullFileSystem, "cfg/tmp_readack.txt", "MOD" );
}
#endif
}
}
}
}
}
//-----------------------------------------------------------------------------
// Purpose: Clean up any item references that we no longer have items for.
// This ensures that if we delete an item on the backend, we remove it from the ack file.
//-----------------------------------------------------------------------------
void CInventoryManager::CleanAckFile( void )
{
CPlayerInventory *pInventory = InventoryManager()->GetLocalInventory();
if ( !pInventory )
return;
if ( !pInventory->RetrievedInventoryFromSteam() )
return;
if ( m_pkvItemClientAckFile )
{
KeyValues *pKVItem = m_pkvItemClientAckFile->GetFirstSubKey();
while ( pKVItem != NULL )
{
itemid_t ulID = (itemid_t)Q_atoi64( pKVItem->GetName() );
if ( pInventory->GetInventoryItemByItemID(ulID) == NULL )
{
KeyValues *pTmp = pKVItem->GetNextKey();
m_pkvItemClientAckFile->RemoveSubKey( pKVItem );
pKVItem->deleteThis();
m_bClientAckDirty = true;
pKVItem = pTmp;
}
else
{
pKVItem = pKVItem->GetNextKey();
}
}
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CInventoryManager::SaveAckFile( void )
{
if ( !m_bClientAckDirty )
return;
m_bClientAckDirty = false;
ISteamRemoteStorage *pRemoteStorage = SteamClient()?(ISteamRemoteStorage *)SteamClient()->GetISteamGenericInterface(
SteamAPI_GetHSteamUser(), SteamAPI_GetHSteamPipe(), STEAMREMOTESTORAGE_INTERFACE_VERSION ):NULL;
if ( pRemoteStorage )
{
CUtlBuffer buf;
m_pkvItemClientAckFile->WriteAsBinary( buf );
pRemoteStorage->FileWrite( ITEM_CLIENTACK_FILE, buf.Base(), buf.TellPut() );
#ifdef _DEBUG
if ( item_debug_clientacks.GetBool() )
{
m_pkvItemClientAckFile->SaveToFile( g_pFullFileSystem, "cfg/tmp_saveack.txt", "MOD" );
}
#endif
}
}
//-----------------------------------------------------------------------------
// Purpose: GC sent name of account down
//-----------------------------------------------------------------------------
class CGCLookupAccountNameResponse : public GCSDK::CGCClientJob
{
public:
CGCLookupAccountNameResponse( GCSDK::CGCClient *pClient ) : GCSDK::CGCClientJob( pClient ) {}
virtual bool BYieldingRunGCJob( GCSDK::IMsgNetPacket *pNetPacket )
{
GCSDK::CGCMsg<MsgGCLookupAccountNameResponse_t> msg( pNetPacket );
CUtlString playerName;
if ( msg.BReadStr( &playerName ) )
{
InventoryManager()->PersonaName_Store( msg.Body().m_unAccountID, playerName.Get() );
}
return true;
}
};
GC_REG_JOB( GCSDK::CGCClient, CGCLookupAccountNameResponse, "CGCLookupAccountNameResponse", k_EMsgGCLookupAccountNameResponse, GCSDK::k_EServerTypeGCClient );
class CGCLookupMultipleAccountsNameResponse : public GCSDK::CGCClientJob
{
public:
CGCLookupMultipleAccountsNameResponse( GCSDK::CGCClient *pClient ) : GCSDK::CGCClientJob( pClient ) {}
virtual bool BYieldingRunGCJob( GCSDK::IMsgNetPacket *pNetPacket )
{
CProtoBufMsg<CMsgLookupMultipleAccountNamesResponse> msg( pNetPacket );
for ( int i = 0 ; i < msg.Body().accounts_size() ; ++i )
{
const CMsgLookupMultipleAccountNamesResponse_Account &account = msg.Body().accounts( i );
InventoryManager()->PersonaName_Store( account.accountid(), account.persona().c_str() );
}
return true;
}
};
GC_REG_JOB( GCSDK::CGCClient, CGCLookupMultipleAccountsNameResponse, "CGCLookupMultipleAccountsNameResponse", k_EMsgGCLookupMultipleAccountNamesResponse, GCSDK::k_EServerTypeGCClient );
void CInventoryManager::PersonaName_Precache( uint32 unAccountID )
{
const char *pszName = PersonaName_Get( unAccountID );
if ( pszName == NULL )
{
// Queue request name from GC
m_msgPendingLookupAccountNames.add_accountids( unAccountID );
// insert empty string so we don't ask again
m_mapPersonaNamesCache.Insert( unAccountID, "" );
}
}
const char *CInventoryManager::PersonaName_Get( uint32 unAccountID )
{
tmZone( TELEMETRY_LEVEL0, TMZF_NONE, "%s", __FUNCTION__ );
// First ask Steam if this is one of friends -- if so we can get an up-to-date persona name.
{
const char *pszName = NULL;
if ( steamapicontext && steamapicontext->SteamUser() && steamapicontext->SteamFriends() )
{
CSteamID steamID = steamapicontext->SteamUser()->GetSteamID();
steamID.SetAccountID( unAccountID );
uint64 u64AccountId = steamID.ConvertToUint64();
// We're covering three states here:
// 1. We've never asked before. We need to queue up a RequestUserInformation.
// 2. We've asked before, and we haven't heard back yet
// 3. We've asked before, we heard back. Don't re-request user information.
auto index = m_personaNameRequests.Find( u64AccountId );
if ( !m_personaNameRequests.IsValidIndex( index ) )
{
// This is case 1--we've never asked before.
// If RequestUserInformation returns false, the information is already available.
// Otherwise, it will arrive later and we need to rebuild the description at that time.
if ( !steamapicontext->SteamFriends()->RequestUserInformation( steamID, true ) )
{
pszName = steamapicontext->SteamFriends()->GetFriendPersonaName( steamID );
Assert( pszName ); // Guaranteed by the steam api
if ( Q_strncmp( pszName, "[unknown]", ARRAYSIZE( "[unknown]" ) ) != 0 )
{
m_mapPersonaNamesCache.InsertOrReplace( unAccountID, pszName );
return pszName;
}
}
else
{
// This is case 2, we've asked above.
m_personaNameRequests.Insert( u64AccountId, false );
}
}
else
{
if ( m_personaNameRequests[ index ] )
{
// This is case 3.
pszName = steamapicontext->SteamFriends()->GetFriendPersonaName( steamID );
Assert( pszName ); // Guaranteed by the steam api
if ( Q_strncmp( pszName, "[unknown]", ARRAYSIZE( "[unknown]" ) ) != 0 )
{
m_mapPersonaNamesCache.InsertOrReplace( unAccountID, pszName );
return pszName;
}
}
}
}
}
// If that didn't work, ask the server we're playing on if they know this account ID.
CBasePlayer *pPlayer = GetPlayerByAccountID( unAccountID );
if ( pPlayer )
{
const char *pszPlayerName = pPlayer->GetPlayerName();
if ( pszPlayerName )
{
m_mapPersonaNamesCache.InsertOrReplace( unAccountID, pszPlayerName );
return pszPlayerName;
}
}
// If *that* didn't work, look in our cache populated by the GC (or the above paths). This
// might be out of date but it's better than nothing.
int idx = m_mapPersonaNamesCache.Find( unAccountID );
if ( m_mapPersonaNamesCache.IsValidIndex( idx ) )
{
return m_mapPersonaNamesCache[idx].Get();
}
return "[unknown]";
}
void CInventoryManager::PersonaName_Store( uint32 unAccountID, const char *pPersonaName )
{
m_mapPersonaNamesCache.InsertOrReplace( unAccountID, pPersonaName );
}
#endif // CLIENT_DLL
//=======================================================================================================================
// PLAYER INVENTORY
//=======================================================================================================================
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CPlayerInventory::CPlayerInventory( void )
{
m_bGotItemsFromSteam = false;
m_iPendingRequests = 0;
m_aInventoryItems.Purge();
m_pSOCache = NULL;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CPlayerInventory::~CPlayerInventory()
{
FOR_EACH_VEC( m_vecItemHandles, i )
{
m_vecItemHandles[ i ]->InventoryIsBeingDeleted();
}
m_vecItemHandles.Purge();
if ( m_iPendingRequests )
{
InventoryManager()->RemovePendingRequest( &m_OwnerID );
m_iPendingRequests = 0;
}
SOClear();
InventoryManager()->DeregisterInventory( this );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CPlayerInventory::SOClear()
{
if ( m_OwnerID.IsValid() )
{
CGCClientSystem *pClientSystem = GCClientSystem();
Assert ( pClientSystem != NULL );
if ( pClientSystem != NULL )
{
CGCClient *pClient = pClientSystem->GetGCClient();
Assert ( pClient != NULL );
pClient->RemoveSOCacheListener( m_OwnerID, this );
}
}
// Somebody registered as a listener through us, but now our Steam ID
// is changing? This is bad news.
Assert( m_vecListeners.Count() == 0 );
while ( m_vecListeners.Count() > 0 )
{
RemoveListener( m_vecListeners[0] );
}
// If we were subscribed, we should have gotten our unsubscribe message,
// and that should have cleared the pointer
Assert( m_pSOCache == NULL);
m_pSOCache = NULL;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CPlayerInventory::AddItemHandle( CEconItemViewHandle* pHandle )
{
FOR_EACH_VEC( m_vecItemHandles, i )
{
if ( m_vecItemHandles[ i ] == pHandle )
{
Assert( !"Item handle already in list to track!" );
return;
}
}
m_vecItemHandles.AddToTail( pHandle );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CPlayerInventory::RemoveItemHandle( CEconItemViewHandle* pHandle )
{
FOR_EACH_VEC( m_vecItemHandles, i )
{
if ( m_vecItemHandles[ i ] == pHandle )
{
m_vecItemHandles.Remove( i );
return;
}
}
Assert( !"Could not find item handle to remove!" );
}
void CPlayerInventory::Clear()
{
SOClear();
m_OwnerID = CSteamID();
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CPlayerInventory::RequestInventory( CSteamID pSteamID )
{
// Make sure we don't already have somebody else's stuff
// on hand
if ( m_OwnerID != pSteamID )
SOClear();
// Remember whose inventory we're looking at
m_OwnerID = pSteamID;
// SteamID must be valid
if ( !m_OwnerID.IsValid() || !m_OwnerID.BIndividualAccount() )
{
Assert( m_OwnerID.IsValid() );
Assert( m_OwnerID.BIndividualAccount() );
return;
}
// If we don't already have an SO cache, then ask the GC for one,
// and start listening to it. We will receive our "subscribed" message
// when the data is valid
GCClientSystem()->GetGCClient()->AddSOCacheListener( m_OwnerID, this );
}
void CPlayerInventory::AddListener( GCSDK::ISharedObjectListener *pListener )
{
Assert( m_OwnerID.IsValid() );
if ( m_vecListeners.Find( pListener ) < 0 )
{
m_vecListeners.AddToTail( pListener );
GCClientSystem()->GetGCClient()->AddSOCacheListener( m_OwnerID, pListener );
}
}
void CPlayerInventory::RemoveListener( GCSDK::ISharedObjectListener *pListener )
{
if ( m_OwnerID.IsValid() )
{
m_vecListeners.FindAndFastRemove( pListener );
GCClientSystem()->GetGCClient()->RemoveSOCacheListener( m_OwnerID, pListener );
}
else
{
Assert( m_vecListeners.Count() == 0 );
}
}
//-----------------------------------------------------------------------------
// Purpose: Helper function to add a new item for a econ item
//-----------------------------------------------------------------------------
bool CPlayerInventory::AddEconItem( CEconItem * pItem, bool bUpdateAckFile, bool bWriteAckFile, bool bCheckForNewItems )
{
CEconItemView newItem;
if( !FilloutItemFromEconItem( &newItem, pItem ) )
{
return false;
}
int iIdx = m_aInventoryItems.Insert( newItem );
DirtyItemHandles();
ItemHasBeenUpdated( &m_aInventoryItems[iIdx], bUpdateAckFile, bWriteAckFile );
#ifdef CLIENT_DLL
if ( bCheckForNewItems && InventoryManager()->GetLocalInventory() == this )
{
bool bNotify = IsUnacknowledged( pItem->GetInventoryToken() );
// ignore Halloween drops
bNotify &= pItem->GetOrigin() != kEconItemOrigin_HalloweenDrop;
// only notify for specific reasons
unacknowledged_item_inventory_positions_t reason = GetUnacknowledgedReason( pItem->GetInventoryToken() );
switch ( reason )
{
case UNACK_ITEM_UNKNOWN:
case UNACK_ITEM_DROPPED:
case UNACK_ITEM_SUPPORT:
case UNACK_ITEM_EARNED:
case UNACK_ITEM_REFUNDED:
case UNACK_ITEM_COLLECTION_REWARD:
case UNACK_ITEM_TRADED:
case UNACK_ITEM_GIFTED:
case UNACK_ITEM_QUEST_LOANER:
case UNACK_ITEM_VIRAL_COMPETITIVE_BETA_PASS_SPREAD:
break;
default:
bNotify = false;
break;
}
if ( bNotify && !pItem->GetItemDefinition()->IsHidden() )
{
OnHasNewItems();
}
}
#endif
return true;
}
//-----------------------------------------------------------------------------
// Purpose: Creates a script item and associates it with this econ item
//-----------------------------------------------------------------------------
void CPlayerInventory::SOCreated( const CSteamID & steamIDOwner, const GCSDK::CSharedObject *pObject, GCSDK::ESOCacheEvent eEvent )
{
tmZone( TELEMETRY_LEVEL0, TMZF_NONE, "%s", __FUNCTION__ );
if( pObject->GetTypeID() != CEconItem::k_nTypeID )
return;
Assert( steamIDOwner == m_OwnerID );
if ( steamIDOwner != m_OwnerID )
return;
// We shouldn't get these notifications unless we're subscribed, right?
if ( m_pSOCache == NULL)
{
Assert( m_pSOCache );
return;
}
// Don't bother unless it's an incremental notification.
// For mass updates, we'll do everything more efficiently in one place
if ( eEvent != GCSDK::eSOCacheEvent_Incremental )
{
Assert( eEvent == GCSDK::eSOCacheEvent_Subscribed || eEvent == GCSDK::eSOCacheEvent_Resubscribed || eEvent == GCSDK::eSOCacheEvent_ListenerAdded );
return;
}
CEconItem *pItem = (CEconItem *)pObject;
AddEconItem( pItem, true, true, true );
SendInventoryUpdateEvent();
}
//-----------------------------------------------------------------------------
// Purpose: Updates the script item associated with this econ item
//-----------------------------------------------------------------------------
void CPlayerInventory::SOUpdated( const CSteamID & steamIDOwner, const GCSDK::CSharedObject *pObject, GCSDK::ESOCacheEvent eEvent )
{
if( pObject->GetTypeID() != CEconItem::k_nTypeID )
return;
Assert( steamIDOwner == m_OwnerID );
if ( steamIDOwner != m_OwnerID )
return;
// We shouldn't get these notifications unless we're subscribed, right?
if ( m_pSOCache == NULL)
{
Assert( m_pSOCache );
return;
}
// Don't bother unless it's an incremental notification.
// For mass updates, we'll do everything more efficiently in one place
if ( eEvent != GCSDK::eSOCacheEvent_Incremental )
{
Assert( eEvent == GCSDK::eSOCacheEvent_Subscribed || eEvent == GCSDK::eSOCacheEvent_Resubscribed );
return;
}
CEconItem *pEconItem = (CEconItem *)pObject;
bool bChanged = false;
CEconItemView *pScriptItem = GetInventoryItemByItemID( pEconItem->GetItemID() );
if ( pScriptItem )
{
if ( FilloutItemFromEconItem( pScriptItem, pEconItem ) )
{
ItemHasBeenUpdated( pScriptItem, false, false );
}
bChanged = true;
}
else
{
// The item isn't in this inventory right now. But it may need to be
// after the update, so try adding it and see if the inventory wants it.
bChanged = AddEconItem( pEconItem, false, false, false );
}
if ( bChanged )
{
ResortInventory();
DirtyItemHandles();
#ifdef CLIENT_DLL
// Client doesn't update inventory while items are moving in a backpack sort. Does it once at the sort end instead.
if ( !InventoryManager()->IsInBackpackSort() )
#endif
{
SendInventoryUpdateEvent();
}
#ifdef _DEBUG
if ( item_inventory_debug.GetBool() )
{
DumpInventoryToConsole( true );
}
#endif
}
}
//-----------------------------------------------------------------------------
// Purpose: Removes the script item associated with this econ item
//-----------------------------------------------------------------------------
void CPlayerInventory::SODestroyed( const CSteamID & steamIDOwner, const GCSDK::CSharedObject *pObject, GCSDK::ESOCacheEvent eEvent )
{
if( pObject->GetTypeID() != CEconItem::k_nTypeID )
return;
Assert( steamIDOwner == m_OwnerID );
if ( steamIDOwner != m_OwnerID )
return;
// We shouldn't get these notifications unless we're subscribed, right?
if ( m_pSOCache == NULL)
{
Assert( m_pSOCache );
return;
}
// Don't bother unless it's an incremental notification.
// For mass updates, we'll do everything more efficiently in one place
if ( eEvent != GCSDK::eSOCacheEvent_Incremental )
{
Assert( eEvent == GCSDK::eSOCacheEvent_Subscribed || eEvent == GCSDK::eSOCacheEvent_Resubscribed );
return;
}
CEconItem *pEconItem = (CEconItem *)pObject;
RemoveItem( pEconItem->GetItemID() );
#ifdef CLIENT_DLL
InventoryManager()->OnItemDeleted( this );
#endif
SendInventoryUpdateEvent();
}
//-----------------------------------------------------------------------------
// Purpose: This is our initial notification that this cache has been received
// from the server.
//-----------------------------------------------------------------------------
void CPlayerInventory::SOCacheSubscribed( const CSteamID & steamIDOwner, GCSDK::ESOCacheEvent eEvent )
{
// Make sure we expect notifications about this guy
Assert( steamIDOwner == m_OwnerID );
if ( steamIDOwner != m_OwnerID )
return;
#ifdef _DEBUG
Msg("CPlayerInventory::SOCacheSubscribed\n");
#endif
// Clear our old inventory
m_aInventoryItems.Purge();
DirtyItemHandles();
// Locate the cache that was just subscribed to
m_pSOCache = GCClientSystem()->GetSOCache( m_OwnerID );
if ( m_pSOCache == NULL )
{
Assert( m_pSOCache != NULL );
return;
}
// add all the items already in the inventory
CSharedObjectTypeCache *pTypeCache = m_pSOCache->FindTypeCache( CEconItem::k_nTypeID );
if( pTypeCache )
{
for( uint32 unItem = 0; unItem < pTypeCache->GetCount(); unItem++ )
{
CEconItem *pItem = (CEconItem *)pTypeCache->GetObject( unItem );
AddEconItem(pItem, true, false, true );
}
}
m_bGotItemsFromSteam = true;
#ifdef CLIENT_DLL
if ( InventoryManager()->GetLocalInventory() == this )
{
// Only validate the local player inventory
ValidateInventoryPositions();
// tell the entire client that we're 'connected' to the GC now
CInventoryManager::SendGCConnectedEvent();
}
#endif
ResortInventory();
#ifdef CLIENT_DLL
// Now that we've read all the items in, write out the ack file (only if we're the local inventory)
if ( InventoryManager()->GetLocalInventory() == this )
{
InventoryManager()->CleanAckFile();
InventoryManager()->SaveAckFile();
}
#endif
}
bool CInventoryManager::IsValidPlayerClass( equipped_class_t unClass )
{
const bool bResult = ItemSystem()->GetItemSchema()->IsValidClass( unClass );
AssertMsg( bResult, "Invalid player class!" );
return bResult;
}
//-----------------------------------------------------------------------------
// Purpose: Removes the script item associated with this econ item
//-----------------------------------------------------------------------------
void CPlayerInventory::ValidateInventoryPositions( void )
{
#ifdef TF2
if ( engine->GetAppID() == 520 )
{
TFInventoryManager()->DeleteUnknowns( this );
}
#endif
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CPlayerInventory::ItemHasBeenUpdated( CEconItemView *pItem, bool bUpdateAckFile, bool bWriteAckFile )
{
#ifdef CLIENT_DLL
// Handle the clientside ack file
if ( bUpdateAckFile && !IsUnacknowledged(pItem->GetInventoryPosition()) )
{
if ( InventoryManager()->GetLocalInventory() == this )
{
InventoryManager()->SetAckedByGC( pItem, bWriteAckFile );
}
}
#endif
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CPlayerInventory::SOCacheUnsubscribed( const CSteamID & steamIDOwner, GCSDK::ESOCacheEvent eEvent )
{
m_pSOCache = NULL;
m_bGotItemsFromSteam = false;
m_aInventoryItems.Purge();
DirtyItemHandles();
}
//-----------------------------------------------------------------------------
// Purpose: On the client this sends the "inventory_updated" event. On the server
// it does nothing.
//-----------------------------------------------------------------------------
void CPlayerInventory::SendInventoryUpdateEvent()
{
#ifdef CLIENT_DLL
if( InventoryManager()->GetLocalInventory() == this )
{
IGameEvent *event = gameeventmanager->CreateEvent( "inventory_updated" );
if ( event )
{
gameeventmanager->FireEventClientSide( event );
}
}
#endif
}
//-----------------------------------------------------------------------------
// Purpose: Fills out all the fields in the script item based on what's in the
// econ item
//-----------------------------------------------------------------------------
bool CPlayerInventory::FilloutItemFromEconItem( CEconItemView *pScriptItem, CEconItem *pEconItem )
{
// We need to detect the case where items have been updated & moved bags / positions.
uint32 iOldPos = pScriptItem->GetInventoryPosition();
bool bWasInThisBag = ItemShouldBeIncluded( iOldPos );
// Ignore items that this inventory doesn't care about
if ( !ItemShouldBeIncluded( pEconItem->GetInventoryToken() ) )
{
// The item has been moved out of this bag. Ensure our derived inventory classes know.
if ( bWasInThisBag )
{
// We need to update it before it's removed.
ItemHasBeenUpdated( pScriptItem, false, false );
RemoveItem( pEconItem->GetItemID() );
}
return false;
}
pScriptItem->Init( pEconItem->GetDefinitionIndex(), pEconItem->GetQuality(), pEconItem->GetItemLevel(), pEconItem->GetAccountID() );
if ( !pScriptItem->IsValid() )
return false;
pScriptItem->SetItemID( pEconItem->GetItemID() );
pScriptItem->SetInventoryPosition( pEconItem->GetInventoryToken() );
OnItemChangedPosition( pScriptItem, iOldPos );
#if BUILD_ITEM_NAME_AND_DESC
// Precache account names if we have any. We do this way in advance of any code that might
// use it (ie., description text building) so that by the time we try that we already have
// the data setup.
//
// We don't worry about yielding here because this inventory code only runs on game
// clients/servers, not the GC.
CSteamAccountIDAttributeCollector AccountIDCollector;
pEconItem->IterateAttributes( &AccountIDCollector );
FOR_EACH_VEC( AccountIDCollector.GetAccountIDs(), i )
{
InventoryManager()->PersonaName_Precache( (AccountIDCollector.GetAccountIDs())[i] );
}
#endif
return true;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CPlayerInventory::DumpInventoryToConsole( bool bRoot )
{
if ( bRoot )
{
#ifdef CLIENT_DLL
Msg("(CLIENT) Inventory:\n");
#else
Msg("(SERVER) Inventory for account (%d):\n", m_OwnerID.GetAccountID() );
#endif
Msg(" Version: %llu:\n", m_pSOCache ? m_pSOCache->GetVersion() : -1 );
}
int iCount = m_aInventoryItems.Count();
Msg(" Num items: %d\n", iCount );
for ( int i = 0; i < iCount; i++ )
{
Msg(" %s (ID %llu)\n", m_aInventoryItems[i].GetStaticData()->GetDefinitionName(), m_aInventoryItems[i].GetItemID() );
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CPlayerInventory::RemoveItem( itemid_t iItemID )
{
int iIndex;
CEconItemView *pItem = GetInventoryItemByItemID( iItemID, &iIndex );
if ( pItem )
{
ItemIsBeingRemoved( pItem );
FOR_EACH_VEC( m_vecItemHandles, i )
{
m_vecItemHandles[ i ]->MarkDirty();
m_vecItemHandles[ i ]->ItemIsBeingDeleted( pItem );
}
m_aInventoryItems.Remove(iIndex);
#ifdef _DEBUG
if ( item_inventory_debug.GetBool() )
{
DumpInventoryToConsole( true );
}
#endif
}
// Don't need to resort because items will still be in order
}
//-----------------------------------------------------------------------------
// Purpose: Finds the item in our inventory that matches the specified global index
//-----------------------------------------------------------------------------
CEconItemView *CPlayerInventory::GetInventoryItemByItemID( itemid_t iIndex, int *pIndex )
{
int iCount = m_aInventoryItems.Count();
for ( int i = 0; i < iCount; i++ )
{
if ( m_aInventoryItems[i].GetItemID() == iIndex )
{
if ( pIndex )
{
*pIndex = i;
}
return &m_aInventoryItems[i];
}
}
return NULL;
}
//-----------------------------------------------------------------------------
// Finds the item in our inventory that matches the specified global original id
//-----------------------------------------------------------------------------
CEconItemView *CPlayerInventory::GetInventoryItemByOriginalID( itemid_t iOriginalID, int *pIndex /*= NULL*/ )
{
int iCount = m_aInventoryItems.Count();
for ( int i = 0; i < iCount; i++ )
{
CEconItem *pItem = m_aInventoryItems[i].GetSOCData();
if ( pItem && pItem->GetOriginalID() == iOriginalID )
{
if ( pIndex )
{
*pIndex = i;
}
return &m_aInventoryItems[i];
}
}
return NULL;
}
//-----------------------------------------------------------------------------
// Purpose: Finds the item in our inventory in the specified position
//-----------------------------------------------------------------------------
CEconItemView *CPlayerInventory::GetItemByPosition( int iPosition, int *pIndex )
{
int iCount = m_aInventoryItems.Count();
for ( int i = 0; i < iCount; i++ )
{
if ( m_aInventoryItems[i].GetInventoryPosition() == (unsigned int)iPosition )
{
if ( pIndex )
{
*pIndex = i;
}
return &m_aInventoryItems[i];
}
}
return NULL;
}
// Finds the first item in our backpack with match itemdef
//-----------------------------------------------------------------------------
CEconItemView *CPlayerInventory::FindFirstItembyItemDef( item_definition_index_t iItemDef )
{
int iCount = m_aInventoryItems.Count();
for ( int i = 0; i < iCount; i++ )
{
//GetItemDefIndex()
if ( m_aInventoryItems[i].GetItemDefIndex() == iItemDef )
{
return &m_aInventoryItems[i];
}
}
return NULL;
}
//-----------------------------------------------------------------------------
// Purpose: Get the index for the item in our inventory utlvector
//-----------------------------------------------------------------------------
int CPlayerInventory::GetIndexForItem( CEconItemView *pItem )
{
int iCount = m_aInventoryItems.Count();
for ( int i = 0; i < iCount; i++ )
{
if ( m_aInventoryItems[i].GetItemID() == pItem->GetItemID() )
return i;
}
return -1;
}
//-----------------------------------------------------------------------------
// Purpose: Dirty all the item handles that are registered with us
//-----------------------------------------------------------------------------
void CPlayerInventory::DirtyItemHandles()
{
FOR_EACH_VEC( m_vecItemHandles, i )
{
m_vecItemHandles[ i ]->MarkDirty();
}
}
//-----------------------------------------------------------------------------
// Purpose: Get the item object cache data for the specified item
//-----------------------------------------------------------------------------
CEconItem *CPlayerInventory::GetSOCDataForItem( itemid_t iItemID )
{
if ( !m_pSOCache )
return NULL;
CEconItem soIndex;
soIndex.SetItemID( iItemID );
return (CEconItem *)m_pSOCache->FindSharedObject( soIndex );
}
#if defined (_DEBUG) && defined(CLIENT_DLL)
CON_COMMAND_F( item_deleteall, "WARNING: Removes all of the items in your inventory.", FCVAR_CHEAT )
{
CPlayerInventory *pInventory = InventoryManager()->GetLocalInventory();
if ( !pInventory )
return;
int iCount = pInventory->GetItemCount();
for ( int i = 0; i < iCount; i++ )
{
CEconItemView *pItem = pInventory->GetItem(i);
if ( pItem )
{
InventoryManager()->DropItem( pItem->GetItemID() );
}
}
InventoryManager()->UpdateLocalInventory();
}
#endif
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
int CPlayerInventory::GetRecipeCount() const
{
const CUtlMap<int, CEconCraftingRecipeDefinition *, int>& mapRecipes = ItemSystem()->GetItemSchema()->GetRecipeDefinitionMap();
return mapRecipes.Count();
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
const CEconCraftingRecipeDefinition *CPlayerInventory::GetRecipeDef( int iIndex )
{
if ( !m_pSOCache )
return NULL;
if ( iIndex < 0 || iIndex >= GetRecipeCount() )
return NULL;
const CEconItemSchema::RecipeDefinitionMap_t& mapRecipes = GetItemSchema()->GetRecipeDefinitionMap();
// Store off separate index for "number of items iterated over" in case something
// deletes from the recipes map out from under us.
int j = 0;
FOR_EACH_MAP_FAST( mapRecipes, i )
{
if ( j == iIndex )
return mapRecipes[i];
j++;
}
return NULL;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
const CEconCraftingRecipeDefinition *CPlayerInventory::GetRecipeDefByDefIndex( uint16 iDefIndex )
{
if ( !m_pSOCache )
return NULL;
// check always-known recipes
const CUtlMap<int, CEconCraftingRecipeDefinition *, int>& mapRecipes = ItemSystem()->GetItemSchema()->GetRecipeDefinitionMap();
int i = mapRecipes.Find( iDefIndex );
if ( i != mapRecipes.InvalidIndex() )
return mapRecipes[i];
// there are no more SO recipes
return NULL;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CEconItemViewHandle::SetItem( CEconItemView* pItem )
{
m_pItem = pItem;
if ( pItem )
{
// Cache the item_id for lookup when our pointer gets dirtied
m_nItemID = pItem->GetItemID();
auto* pInv = InventoryManager()->GetInventoryForAccount( pItem->GetAccountID() );
Assert( pInv );
if ( m_pInv != pInv )
{
// If this is a different inventory, unsubscribe. This can happen if the
// handle gets reused
if ( m_pInv )
{
m_pInv->RemoveItemHandle( this );
}
m_pInv = pInv;
// Subscribe to the new inventory
m_pInv->AddItemHandle( this );
}
}
}
//-----------------------------------------------------------------------------
// Purpose: Return a pointer to a CEconItemView
//-----------------------------------------------------------------------------
CEconItemView* CEconItemViewHandle::Get() const
{
// If our pointer is dirty, we need to go get a new pointer
if ( m_bPointerDirty )
{
if ( m_pInv )
{
m_pItem = m_pInv->GetInventoryItemByItemID( m_nItemID );
m_bPointerDirty = false;
}
}
return m_pItem;
}
//-----------------------------------------------------------------------------
// Purpose: Unsubscribe us from future updates
//-----------------------------------------------------------------------------
CEconItemHandle::~CEconItemHandle()
{
UnsubscribeFromSOEvents();
}
//-----------------------------------------------------------------------------
// Purpose: Save a pointer to the item and register us for SOCache events
//-----------------------------------------------------------------------------
void CEconItemHandle::SetItem( CEconItem* pItem )
{
UnsubscribeFromSOEvents();
m_pItem = NULL;
m_iItemID = INVALID_ITEM_ID;
if ( pItem )
{
auto* pInv = InventoryManager()->GetInventoryForAccount( pItem->GetAccountID() );
if ( pInv )
{
m_OwnerSteamID.SetFromUint64( pInv->GetOwner().ConvertToUint64() );
GCClientSystem()->GetGCClient()->AddSOCacheListener( m_OwnerSteamID, this );
}
m_pItem = pItem;
m_iItemID = pItem->GetID();
}
}
//-----------------------------------------------------------------------------
// Purpose: Check if out item got deleted. If it did, mark our pointer as NULL
// so future dereferences will get NULL instead of a stale pointer.
//-----------------------------------------------------------------------------
void CEconItemHandle::SODestroyed( const CSteamID & steamIDOwner, const GCSDK::CSharedObject *pObject, GCSDK::ESOCacheEvent eEvent )
{
if( pObject->GetTypeID() != CEconItem::k_nTypeID || m_pItem == NULL )
return;
const CEconItem *pItem = (CEconItem *)pObject;
if ( m_iItemID == pItem->GetID() )
{
UnsubscribeFromSOEvents();
m_pItem = NULL;
m_iItemID = INVALID_ITEM_ID;
}
}
void CEconItemHandle::SOCreated( const CSteamID & steamIDOwner, const GCSDK::CSharedObject *pObject, GCSDK::ESOCacheEvent eEvent )
{
if( pObject->GetTypeID() != CEconItem::k_nTypeID )
return;
CEconItem *pItem = (CEconItem *)pObject;
if ( m_iItemID == pItem->GetID() )
{
SetItem( pItem );
}
}
void CEconItemHandle::SOUpdated( const CSteamID & steamIDOwner, const GCSDK::CSharedObject *pObject, GCSDK::ESOCacheEvent eEvent )
{
if ( pObject->GetTypeID() != CEconItem::k_nTypeID )
return;
CEconItem *pItem = (CEconItem *)pObject;
if ( m_iItemID == pItem->GetID() )
{
SetItem( pItem );
}
}
void CEconItemHandle::SOCacheUnsubscribed( const CSteamID & steamIDOwner, GCSDK::ESOCacheEvent eEvent )
{
UnsubscribeFromSOEvents();
}
void CEconItemHandle::UnsubscribeFromSOEvents()
{
if ( m_OwnerSteamID.GetAccountID() != 0 )
{
GCClientSystem()->GetGCClient()->RemoveSOCacheListener( m_OwnerSteamID, this );
}
}
#if defined( STAGING_ONLY ) || defined( _DEBUG )
#if defined(CLIENT_DLL)
CON_COMMAND_F( item_dumpinv, "Dumps the contents of a specified client inventory.", FCVAR_CHEAT )
#else
CON_COMMAND_F( item_dumpinv_sv, "Dumps the contents of a specified server inventory.", FCVAR_CHEAT )
#endif
{
#if defined(CLIENT_DLL)
CPlayerInventory *pInventory = InventoryManager()->GetLocalInventory();
#else
CSteamID steamID;
CBaseMultiplayerPlayer *pPlayer = ToBaseMultiplayerPlayer( UTIL_GetCommandClient() );
pPlayer->GetSteamID( &steamID );
CPlayerInventory *pInventory = InventoryManager()->GetInventoryForAccount( steamID.GetAccountID() );
#endif
if ( !pInventory )
{
Msg("No inventory found.\n");
return;
}
pInventory->DumpInventoryToConsole( true );
}
#if defined (CLIENT_DLL)
CON_COMMAND_F( item_dumpschema, "Dump the expanded schema for items to a file in sorted order suitable for diffs. Format: item_dumpschema <filename>", FCVAR_CHEAT )
{
if ( args.ArgC() != 2 )
{
Msg("Usage: item_dumpschema <filename>\n");
return;
}
if ( GetItemSchema()->DumpItems(args[1]) )
Msg("Dump complete, saved in game/tf/%s\n", args[1]);
else
Msg("Dump failed (?)\n");
}
CON_COMMAND_F( item_giveitem, "Give an item to the local player. Format: item_giveitem <item definition name> or <item def index>", FCVAR_NONE )
{
if ( !steamapicontext || !steamapicontext->SteamUser() )
{
Msg("Not connected to Steam.\n");
return;
}
CSteamID steamIDForPlayer = steamapicontext->SteamUser()->GetSteamID();
if ( !steamIDForPlayer.IsValid() )
{
Msg("Failed to find a valid steamID for the local player.\n");
return;
}
int iItemCount = args.ArgC();
for ( int i = 1; i < iItemCount; ++i )
{
// Check to see if args[1] is a number (itemdefid) and if so, translate it to actual itemname
const char *pszItemname = NULL;
if ( V_isdigit( args[i][0] ) )
{
int iDef = V_atoi( args[i] );
CEconItemDefinition *pItemDef = GetItemSchema()->GetItemDefinition( iDef );
if ( pItemDef )
{
pszItemname = pItemDef->GetItemDefinitionName();
}
}
else
{
pszItemname = args[i];
}
Msg("Sending request to generate '%s' for Local Player (%llu)\n", pszItemname, steamIDForPlayer.ConvertToUint64() );
CItemSelectionCriteria criteria;
GCSDK::CProtoBufMsg<CMsgDevNewItemRequest> msg( k_EMsgGCDev_NewItemRequest );
msg.Body().set_receiver( steamIDForPlayer.ConvertToUint64() );
criteria.SetIgnoreEnabledFlag( true );
if ( !criteria.BAddCondition( "name", k_EOperator_String_EQ, pszItemname, true ) ||
!criteria.BSerializeToMsg( *msg.Body().mutable_criteria() ) )
{
Msg("Failed to add condition and/or serialize item grant request. This is probably caused by having a string that's too long.\n" );
return;
}
GCClientSystem()->BSendMessage( msg );
}
}
CON_COMMAND_F( item_rolllootlist, "Force a loot list rool for the local player. Format: item_rolllootlist <loot list definition name>", FCVAR_NONE )
{
if ( !steamapicontext || !steamapicontext->SteamUser() )
{
Msg("Not connected to Steam.\n");
return;
}
CSteamID steamIDForPlayer = steamapicontext->SteamUser()->GetSteamID();
if ( !steamIDForPlayer.IsValid() )
{
Msg("Failed to find a valid steamID for the local player.\n");
return;
}
Msg("Sending request to roll '%s' for Local Player (%llu)\n", args[1], steamIDForPlayer.ConvertToUint64() );
GCSDK::CProtoBufMsg<CMsgDevDebugRollLootRequest> msg( k_EMsgGCDev_DebugRollLootRequest );
msg.Body().set_receiver( steamIDForPlayer.ConvertToUint64() );
msg.Body().set_loot_list_name( args[1] );
GCClientSystem()->BSendMessage( msg );
}
#include "econ_item_description.h"
#include "localization_provider.h"
CON_COMMAND_F( item_generate_all_descriptions, "Generate full item descriptions for every item in your backpack. Meant as a code test.", FCVAR_CHEAT )
{
CPlayerInventory *pInventory = InventoryManager()->GetLocalInventory();
for ( int i = 0; i < pInventory->GetItemCount(); i++ )
{
CEconItemDescription desc;
IEconItemDescription::YieldingFillOutEconItemDescription( &desc, GLocalizationProvider(), pInventory->GetItem( i ) );
}
Msg("Done.\n");
}
#endif // CLIENT_DLL
#endif // STAGING_ONLY || _DEBUG
|