1+
2+ #include " extdll.h"
3+ #include " util.h"
4+ #include " cbase.h"
5+ #include " player.h"
6+
7+ #include " com_weapons.h"
8+
9+ #include " parsemsg.h"
10+
11+ #include " bte_weapons.h"
12+ #include " weapons_msg.h"
13+
14+ #include " wpn_shared.h"
15+
16+ #include < string>
17+ #include < map>
18+
19+ /*
20+ Weapon Registers
21+ */
22+ template <class T >
23+ CBasePlayerWeapon *WeaponEntityPlaceHolderFactory () // Static
24+ {
25+ static T w;
26+ static entvars_t ev;
27+
28+ CBasePlayerWeapon *pEntity = &w;
29+ pEntity->pev = &ev;
30+ pEntity->Precache ();
31+ pEntity->Spawn ();
32+
33+ return pEntity;
34+ }
35+
36+ const std::map<std::string, CBasePlayerWeapon *(*)()> g_WeaponEntityFindList = {
37+ { " weapon_ak47l" , WeaponEntityPlaceHolderFactory<CAK47_Long> },
38+ { " weapon_deagled" , WeaponEntityPlaceHolderFactory<CDeagleD> }
39+ };
40+
41+ /*
42+ Entity Pools
43+ */
44+ extern CBasePlayerWeapon *g_pWpns[MAX_WEAPONS]; // cs_weapons.cpp
45+
46+ /*
47+ CBTEClientWeapons impls
48+ */
49+
50+ void CBTEClientWeapons::PrepEntity (CBasePlayer *pWeaponOwner)
51+ {
52+ for (auto &kv : g_WeaponEntityFindList)
53+ {
54+ CBasePlayerWeapon *pEntity = kv.second ();
55+
56+ if (pWeaponOwner)
57+ {
58+ ((CBasePlayerWeapon *)pEntity)->m_pPlayer = pWeaponOwner;
59+ }
60+ }
61+
62+ }
63+
64+ void CBTEClientWeapons::ActiveWeapon (const char *name)
65+ {
66+ m_pActiveWeapon = nullptr ;
67+
68+ auto iter = g_WeaponEntityFindList.find ({ name });
69+ if (iter != g_WeaponEntityFindList.end ())
70+ {
71+ m_pActiveWeapon = iter->second ();
72+
73+ ItemInfo info;
74+ memset (&info, 0 , sizeof (ItemInfo));
75+ m_pActiveWeapon->GetItemInfo (&info);
76+ g_pWpns[info.iId ] = m_pActiveWeapon;
77+ }
78+
79+ }
80+
81+ /*
82+ Message Handlers
83+ */
84+
85+ typedef void MsgDispatchFunction_t (BufferReader &reader);
86+
87+ void MsgDispatch_Active (BufferReader &reader)
88+ {
89+ const char *name = reader.ReadString ();
90+ BTEClientWeapons ().ActiveWeapon (name);
91+ }
92+
93+ MsgDispatchFunction_t *gMsgDispatchFunctions [BTE_Weapon_MaxMsgs] = {
94+ MsgDispatch_Active
95+ };
96+
97+ int __MsgFunc_BTEWeapon (const char *pszName, int iSize, void *pbuf)
98+ {
99+ BufferReader reader (pszName, pbuf, iSize);
100+
101+ BTEWeaponMsgType type = static_cast <BTEWeaponMsgType>(reader.ReadByte ());
102+ gMsgDispatchFunctions [type](reader);
103+ return 1 ;
104+ }
105+
106+
107+ void CBTEClientWeapons::Init ()
108+ {
109+ gEngfuncs .pfnHookUserMsg (" BTEWeapon" , __MsgFunc_BTEWeapon);
110+ }
111+
112+ CBTEClientWeapons::CBTEClientWeapons () : m_pActiveWeapon(nullptr )
113+ {
114+
115+ }
116+
117+ CBTEClientWeapons &BTEClientWeapons ()
118+ {
119+ static CBTEClientWeapons x;
120+ return x;
121+ }
0 commit comments