forked from Source-Python-Dev-Team/Source.Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentities_transmit.h
More file actions
339 lines (262 loc) · 9.75 KB
/
entities_transmit.h
File metadata and controls
339 lines (262 loc) · 9.75 KB
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
/**
* =============================================================================
* Source Python
* Copyright (C) 2012-2021 Source Python Development Team. All rights reserved.
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, the Source Python Team gives you permission
* to link the code of this program (as well as its derivative works) to
* "Half-Life 2," the "Source Engine," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, the Source.Python
* Development Team grants this exception to all derivative works.
*/
#ifndef _ENTITIES_TRANSMIT_H
#define _ENTITIES_TRANSMIT_H
//-----------------------------------------------------------------------------
// Includes.
//-----------------------------------------------------------------------------
// Source.Python
#include "modules/entities/entities_entity.h"
#include "modules/memory/memory_hooks.h"
#include "modules/listeners/listeners_manager.h"
// Boost
#include "boost/unordered_map.hpp"
#include "boost/unordered_set.hpp"
//-----------------------------------------------------------------------------
// Forward declarations.
//-----------------------------------------------------------------------------
class CTransmitSet;
class CTransmitCache;
//-----------------------------------------------------------------------------
// Typedefs.
//-----------------------------------------------------------------------------
typedef boost::unordered_set<unsigned int> TransmitSet_t;
typedef boost::unordered_map<unsigned int, boost::shared_ptr<CTransmitSet> > TransmitMap_t;
typedef CBitVec<MAX_EDICTS> TransmitCache_t;
typedef boost::unordered_map<unsigned int, CTransmitCache *> TransmitCacheMap_t;
typedef std::pair<unsigned int, unsigned int> TransmitPair_t;
typedef CBitVec<MAX_EDICTS> TransmitStates_t;
//-----------------------------------------------------------------------------
// CTransmitStates class.
//-----------------------------------------------------------------------------
class CTransmitStates : public TransmitStates_t
{
public:
bool __getitem__(int iIndex);
void __setitem__(int iIndex, bool bValue);
};
//-----------------------------------------------------------------------------
// ETransmitMode enumeration.
//-----------------------------------------------------------------------------
enum ETransmitMode
{
TRANSMIT_MODE_ALLOW,
TRANSMIT_MODE_PREVENT
};
//-----------------------------------------------------------------------------
// ITransmitRules class.
//-----------------------------------------------------------------------------
class ITransmitRules
{
public:
virtual ~ITransmitRules();
virtual void OnEntityDeleted(CBaseEntityWrapper *pEntity) = 0;
virtual bool ShouldTransmit(unsigned int uiPlayer, unsigned int uiEntity) = 0;
virtual bool ShouldTransmit(CBaseEntityWrapper *pPlayer, CBaseEntityWrapper *pEntity);
virtual void Clear() = 0;
virtual void UnloadInstance();
ETransmitMode GetMode();
void SetMode(ETransmitMode eMode);
private:
ETransmitMode m_eMode;
};
//-----------------------------------------------------------------------------
// CTransmitCache class.
//-----------------------------------------------------------------------------
class CTransmitCache : private TransmitCache_t
{
public:
bool HasResult(unsigned int uiIndex);
bool GetResult(unsigned int uiIndex);
void SetResult(unsigned int uiIndex, bool bResult);
void DelResult(unsigned int uiIndex);
private:
TransmitCache_t m_vecCache;
};
//-----------------------------------------------------------------------------
// CTransmitManager class.
//-----------------------------------------------------------------------------
class CTransmitManager
{
public:
friend CTransmitManager *GetTransmitManager();
friend class CTransmitListenerManager;
private:
CTransmitManager();
~CTransmitManager();
public:
void Initialize();
void Finalize();
void RegisterRules(ITransmitRules *pRules);
void UnregisterRules(ITransmitRules *pRules);
void OnNetworkedEntityDeleted(CBaseEntityWrapper *pEntity, unsigned int uiEntity);
void OnLevelShutdown();
void RegisterTransmitHook(object oCallback);
void UnregisterTransmitHook(object oCallback);
private:
static bool CheckTransmit(HookType_t eHookType, CHook *pHook);
CTransmitCache *GetCache(unsigned int uiIndex);
protected:
void IncRef();
void DecRef();
private:
bool m_bInitialized;
unsigned int m_uiRefCount;
CUtlVector<ITransmitRules *> m_vecRules;
CHook *m_pHook;
TransmitCacheMap_t m_mapCache;
CListenerManager *m_pTransmitHooks;
};
// Singleton accessor.
inline CTransmitManager *GetTransmitManager()
{
static CTransmitManager *s_pEntityTransmitManager = new CTransmitManager;
return s_pEntityTransmitManager;
}
//-----------------------------------------------------------------------------
// TransmitPairs_t definition and specializations.
//-----------------------------------------------------------------------------
struct TransmitPairHash_t
{
inline std::size_t operator()(TransmitPair_t const &p) const {
std::size_t s = 0;
boost::hash_combine(s, p.first < p.second ? p.first : p.second);
boost::hash_combine(s, p.first < p.second ? p.second : p.first);
return s;
}
};
struct TransmitPairEquals_t
{
inline bool operator()(const TransmitPair_t &a, const TransmitPair_t &b) const {
return (a == b) || (a.first == b.second && a.second == b.first);
}
};
typedef boost::unordered_set<TransmitPair_t, TransmitPairHash_t, TransmitPairEquals_t> TransmitPairs_t;
//-----------------------------------------------------------------------------
// CTransmitHash class.
//-----------------------------------------------------------------------------
class CTransmitHash : public ITransmitRules
{
public:
void OnEntityDeleted(CBaseEntityWrapper *pEntity);
bool ShouldTransmit(unsigned int uiPlayer, unsigned int uiEntity);
void AddPair(CBaseEntityWrapper *pEntity, CBaseEntityWrapper *pOther);
void RemovePair(CBaseEntityWrapper *pEntity, CBaseEntityWrapper *pOther);
void RemovePairs(CBaseEntityWrapper *pEntity);
void Clear();
bool Contains(CBaseEntityWrapper *pEntity);
bool HasPair(unsigned int uiEntity, unsigned int uiOther);
bool HasPair(CBaseEntityWrapper *pEntity, CBaseEntityWrapper *pOther);
unsigned int GetCount(CBaseEntityWrapper *pEntity);
list GetPairs(CBaseEntityWrapper *pEntity);
unsigned int GetSize();
bool HasElements();
object Iterate();
private:
TransmitPairs_t m_setPairs;
};
//-----------------------------------------------------------------------------
// CTransmitSet class.
//-----------------------------------------------------------------------------
class CTransmitSet : public ITransmitRules
{
public:
void Add(CBaseEntityWrapper *pEntity);
void Remove(CBaseEntityWrapper *pEntity);
void Clear();
bool Contains(unsigned int uiEntity);
bool Contains(CBaseEntityWrapper *pEntity);
unsigned int GetSize();
bool HasElements();
object Iterate();
void OnEntityDeleted(CBaseEntityWrapper *pEntity);
bool ShouldTransmit(unsigned int uiPlayer, unsigned int uiEntity);
private:
TransmitSet_t m_pSet;
};
//-----------------------------------------------------------------------------
// CTransmitMap class.
//-----------------------------------------------------------------------------
class CTransmitMap: public ITransmitRules
{
public:
void OnEntityDeleted(CBaseEntityWrapper *pEntity);
bool ShouldTransmit(unsigned int uiPlayer, unsigned int uiEntity);
boost::shared_ptr<CTransmitSet> Find(unsigned int uiEntity);
boost::shared_ptr<CTransmitSet> Find(CBaseEntityWrapper *pEntity);
void Remove(CBaseEntityWrapper *pEntity);
void Clear();
bool Contains(unsigned int uiEntity);
bool Contains(CBaseEntityWrapper *pEntity);
unsigned int GetSize();
bool HasElements();
object Iterate();
private:
TransmitMap_t m_mapSets;
};
//-----------------------------------------------------------------------------
// ITransmitRules extension class.
//-----------------------------------------------------------------------------
class ITransmitRulesExt
{
public:
template<class T>
static boost::shared_ptr<T> Construct(ETransmitMode eMode = TRANSMIT_MODE_PREVENT)
{
return boost::shared_ptr<T>(new T, &Finalize<T>);
}
template<class T>
static void Initialize(T *pSelf, object self, ETransmitMode eMode = TRANSMIT_MODE_PREVENT)
{
pSelf->SetMode(eMode);
static CTransmitManager *pManager = GetTransmitManager();
pManager->RegisterRules(pSelf);
}
template<class T>
static void Finalize(T *pSelf)
{
static CTransmitManager *pManager = GetTransmitManager();
pManager->UnregisterRules(pSelf);
delete pSelf;
}
};
//-----------------------------------------------------------------------------
// CTransmitListenerManager class.
//-----------------------------------------------------------------------------
class CTransmitListenerManager : public CListenerManager
{
public:
CTransmitListenerManager();
void Initialize();
void Finalize();
bool CallCallbacks(object oPlayer, object oEntity);
private:
bool m_bInitialized;
};
// Singleton accessors.
CTransmitListenerManager *GetOnEntityTransmitListenerManager();
CTransmitListenerManager *GetOnPlayerTransmitListenerManager();
#endif // _ENTITIES_TRANSMIT_H