Skip to content

Commit d093f73

Browse files
author
L'In20Cible
committed
Patched #124
1 parent f61a23b commit d093f73

File tree

4 files changed

+47
-32
lines changed

4 files changed

+47
-32
lines changed

addons/source-python/packages/source-python/effects/base.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,6 @@
4545
from _effects._base import BaseTempEntity
4646

4747

48-
# =============================================================================
49-
# >> GLOBAL VARIABLES
50-
# =============================================================================
51-
# Use a global RecipientFilter to prevent memory leaking...
52-
# See also: https://github.com/Source-Python-Dev-Team/Source.Python/issues/124
53-
_recipient_filter = RecipientFilter()
54-
55-
5648
# =============================================================================
5749
# >> ALL DECLARATION
5850
# =============================================================================
@@ -568,8 +560,8 @@ def create(self, *recipients, delay=0.0, **aliases):
568560
:param dict aliases: Any aliases to set before creating the temp entity
569561
effect.
570562
"""
571-
# Update the global recipient filter...
572-
_recipient_filter.update(*recipients)
563+
# Get a recipient filter matching the given players...
564+
recipients = RecipientFilter(*recipients)
573565

574566
# Loop trhough all given aliases...
575567
for alias, value in aliases.items():
@@ -578,7 +570,7 @@ def create(self, *recipients, delay=0.0, **aliases):
578570
setattr(self, alias, value)
579571

580572
# Create the temp entity effect...
581-
super().create(_recipient_filter, delay)
573+
super().create(recipients, delay)
582574

583575
@property
584576
def template(self):

addons/source-python/packages/source-python/messages/base.py

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,6 @@
3131
from _messages import FadeFlags
3232

3333

34-
# =============================================================================
35-
# >> GLOBAL VARIABLES
36-
# =============================================================================
37-
# Use a global RecipientFilter to prevent memory leaking...
38-
# See also: https://github.com/Source-Python-Dev-Team/Source.Python/issues/124
39-
_recipient_filter = RecipientFilter()
40-
41-
4234
# =============================================================================
4335
# >> CLASSES
4436
# =============================================================================
@@ -73,9 +65,9 @@ def __setattr__(self, attr, value):
7365

7466
def send(self, *player_indexes, **tokens):
7567
"""Send the user message."""
76-
_recipient_filter.update(*player_indexes)
68+
player_indexes = RecipientFilter(*player_indexes)
7769
for language, indexes in self._categorize_players_by_language(
78-
_recipient_filter).items():
70+
player_indexes).items():
7971
translated_kwargs = AttrDict(self)
8072
translated_kwargs.update(
8173
self._get_translated_kwargs(language, tokens))
@@ -88,9 +80,8 @@ def _send(self, player_indexes, translated_kwargs):
8880
setting.
8981
:param AttrDict translated_kwargs: The translated arguments.
9082
"""
91-
_recipient_filter.update(*player_indexes)
92-
user_message = UserMessage(
93-
_recipient_filter, self.message_name)
83+
recipients = RecipientFilter(*player_indexes)
84+
user_message = UserMessage(recipients, self.message_name)
9485

9586
if user_message.is_protobuf():
9687
self.protobuf(user_message.buffer, translated_kwargs)
@@ -211,9 +202,8 @@ def send(self, *player_indexes):
211202
# differently, because the maximum size is 255. If the message exceeds
212203
# this length, we need to sent it in several parts.
213204
if UserMessage.is_protobuf():
214-
_recipient_filter.update(*player_indexes)
215-
user_message = UserMessage(
216-
_recipient_filter, self.message_name)
205+
recipients = RecipientFilter(*player_indexes)
206+
user_message = UserMessage(recipients, self.message_name)
217207
self.protobuf(user_message.buffer, self)
218208
user_message.send()
219209
else:
@@ -229,10 +219,9 @@ def bitbuf(self, player_indexes, kwargs):
229219
"""Send the ShowMenu with bitbuf."""
230220
menu_string = kwargs.menu_string
231221
length = len(menu_string)
232-
_recipient_filter.update(*player_indexes)
222+
recipients = RecipientFilter(*player_indexes)
233223
while True:
234-
user_message = UserMessage(
235-
_recipient_filter, self.message_name)
224+
user_message = UserMessage(recipients, self.message_name)
236225

237226
buffer = user_message.buffer
238227
buffer.write_word(kwargs.valid_slots)

src/core/modules/filters/filters_recipients.h

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,40 @@
3636
#include "irecipientfilter.h"
3737
#include "bitvec.h"
3838
#include "tier1/utlvector.h"
39+
#include "modules/memory/memory_alloc.h"
40+
41+
42+
//---------------------------------------------------------------------------------
43+
// Patch for issue #124.
44+
//---------------------------------------------------------------------------------
45+
template<class T, class U = int>
46+
class CRecipientFilterAllocator : public CUtlMemory<T, U>
47+
{
48+
public:
49+
CRecipientFilterAllocator(int nGrowSize = 0, int nInitSize = 0)
50+
{
51+
::CUtlMemory<T, U>(nGrowSize, nInitSize);
52+
}
53+
54+
~CRecipientFilterAllocator() { m_nAllocationCount = 0; }
55+
56+
void *DetachAndReturn()
57+
{
58+
void *pMemory = m_pMemory;
59+
m_pMemory = NULL;
60+
return pMemory;
61+
}
62+
};
63+
64+
template<class T, class U = CRecipientFilterAllocator<T>>
65+
class CVecRecipients : public CUtlVector<T, U>
66+
{
67+
public:
68+
~CVecRecipients()
69+
{
70+
UTIL_Dealloc(m_Memory.DetachAndReturn());
71+
}
72+
};
3973

4074

4175
//---------------------------------------------------------------------------------
@@ -61,7 +95,7 @@ class MRecipientFilter : public IRecipientFilter
6195
private:
6296
bool m_bReliable;
6397
bool m_bInitMessage;
64-
CUtlVector< int > m_Recipients;
98+
CVecRecipients<int> m_Recipients;
6599

66100
// If using prediction rules, the filter itself suppresses local player
67101
bool m_bUsingPredictionRules;

src/core/modules/filters/filters_recipients_wrap.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ void export_mrecipientfilter(scope _recipients)
8181
ADD_MEM_TOOLS(IRecipientFilter)
8282
;
8383

84-
class_<MRecipientFilter, MRecipientFilter*, bases<IRecipientFilter>, boost::noncopyable >("_RecipientFilter")
84+
class_<MRecipientFilter, boost::shared_ptr<MRecipientFilter>, bases<IRecipientFilter>, boost::noncopyable >("_RecipientFilter")
8585
.def("add_all_players",
8686
&MRecipientFilter::AddAllPlayers,
8787
"Adds all the players on the server to the filter"

0 commit comments

Comments
 (0)