Skip to content

Commit 24c8f65

Browse files
committed
Added classname filter options for BaseEntityGenerator
1 parent 289e1a1 commit 24c8f65

File tree

3 files changed

+77
-41
lines changed

3 files changed

+77
-41
lines changed

src/core/modules/entities/entities_generator.cpp

Lines changed: 68 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ extern IServerGameDLL* servergamedll;
4343

4444

4545
// ----------------------------------------------------------------------------
46-
// CEntityGenerator Constructor.
46+
// CEntityGenerator
4747
// ----------------------------------------------------------------------------
4848
CEntityGenerator::CEntityGenerator( PyObject* self ):
4949
IPythonGenerator<edict_t>(self),
@@ -54,9 +54,6 @@ CEntityGenerator::CEntityGenerator( PyObject* self ):
5454
{
5555
}
5656

57-
// ----------------------------------------------------------------------------
58-
// CEntityGenerator Copy-Constructor.
59-
// ----------------------------------------------------------------------------
6057
CEntityGenerator::CEntityGenerator( PyObject* self, const CEntityGenerator& rhs ):
6158
IPythonGenerator<edict_t>(self),
6259
m_pCurrentEntity(rhs.m_pCurrentEntity),
@@ -66,9 +63,6 @@ CEntityGenerator::CEntityGenerator( PyObject* self, const CEntityGenerator& rhs
6663
makeStringCopy(rhs.m_szClassName, m_uiClassNameLen);
6764
}
6865

69-
// ----------------------------------------------------------------------------
70-
// CEntityGenerator Constructor (takes a filter string).
71-
// ----------------------------------------------------------------------------
7266
CEntityGenerator::CEntityGenerator(PyObject* self, const char* szClassName):
7367
IPythonGenerator<edict_t>(self),
7468
m_pCurrentEntity((CBaseEntity *)servertools->FirstEntity()),
@@ -78,9 +72,6 @@ CEntityGenerator::CEntityGenerator(PyObject* self, const char* szClassName):
7872
makeStringCopy(szClassName, m_uiClassNameLen);
7973
}
8074

81-
// ----------------------------------------------------------------------------
82-
// CEntityGenerator Constructor (takes a filter string and a boolean flag).
83-
// ----------------------------------------------------------------------------
8475
CEntityGenerator::CEntityGenerator(PyObject* self, const char* szClassName, bool bExactMatch):
8576
IPythonGenerator<edict_t>(self),
8677
m_pCurrentEntity((CBaseEntity *)servertools->FirstEntity()),
@@ -90,17 +81,11 @@ CEntityGenerator::CEntityGenerator(PyObject* self, const char* szClassName, bool
9081
makeStringCopy(szClassName, m_uiClassNameLen);
9182
}
9283

93-
// ----------------------------------------------------------------------------
94-
// CEntityGenerator Destructor.
95-
// ----------------------------------------------------------------------------
9684
CEntityGenerator::~CEntityGenerator()
9785
{
9886
delete[] m_szClassName;
9987
}
10088

101-
// ----------------------------------------------------------------------------
102-
// Returns the next valid edict_t instance.
103-
// ----------------------------------------------------------------------------
10489
edict_t* CEntityGenerator::getNext()
10590
{
10691
while (m_pCurrentEntity)
@@ -126,9 +111,6 @@ edict_t* CEntityGenerator::getNext()
126111
return NULL;
127112
}
128113

129-
//---------------------------------------------------------------------------------
130-
// Private function, creates a copy of the class name string.
131-
//---------------------------------------------------------------------------------
132114
void CEntityGenerator::makeStringCopy(const char* szClassName, unsigned int uiClassNameLen)
133115
{
134116
if (uiClassNameLen > 0)
@@ -146,55 +128,101 @@ void CEntityGenerator::makeStringCopy(const char* szClassName, unsigned int uiCl
146128

147129

148130
// ----------------------------------------------------------------------------
149-
// CBaseEntityGenerator Constructor.
131+
// CBaseEntityGenerator
150132
// ----------------------------------------------------------------------------
151133
CBaseEntityGenerator::CBaseEntityGenerator( PyObject* self ):
152-
IPythonGenerator<CBaseEntityWrapper>(self)
134+
IPythonGenerator<CBaseEntityWrapper>(self),
135+
m_pCurrentEntity((CBaseEntity *)servertools->FirstEntity()),
136+
m_szClassName(NULL),
137+
m_uiClassNameLen(0),
138+
m_bExactMatch(false)
153139
{
154-
m_pCurrentEntity = (CBaseEntity *) servertools->FirstEntity();
155140
}
156141

157-
// ----------------------------------------------------------------------------
158-
// CBaseEntityGenerator Copy-Constructor.
159-
// ----------------------------------------------------------------------------
160142
CBaseEntityGenerator::CBaseEntityGenerator( PyObject* self, const CBaseEntityGenerator& rhs ):
161-
IPythonGenerator<CBaseEntityWrapper>(self)
143+
IPythonGenerator<CBaseEntityWrapper>(self),
144+
m_pCurrentEntity(rhs.m_pCurrentEntity),
145+
m_uiClassNameLen(rhs.m_uiClassNameLen),
146+
m_bExactMatch(rhs.m_bExactMatch)
162147
{
163-
m_pCurrentEntity = rhs.m_pCurrentEntity;
148+
makeStringCopy(rhs.m_szClassName, m_uiClassNameLen);
149+
}
150+
151+
CBaseEntityGenerator::CBaseEntityGenerator(PyObject* self, const char* szClassName):
152+
IPythonGenerator<CBaseEntityWrapper>(self),
153+
m_pCurrentEntity((CBaseEntity *)servertools->FirstEntity()),
154+
m_uiClassNameLen(strlen(szClassName)),
155+
m_bExactMatch(false)
156+
{
157+
makeStringCopy(szClassName, m_uiClassNameLen);
158+
}
159+
160+
CBaseEntityGenerator::CBaseEntityGenerator(PyObject* self, const char* szClassName, bool bExactMatch):
161+
IPythonGenerator<CBaseEntityWrapper>(self),
162+
m_pCurrentEntity((CBaseEntity *)servertools->FirstEntity()),
163+
m_uiClassNameLen(strlen(szClassName)),
164+
m_bExactMatch(bExactMatch)
165+
{
166+
makeStringCopy(szClassName, m_uiClassNameLen);
167+
}
168+
169+
void CBaseEntityGenerator::makeStringCopy(const char* szClassName, unsigned int uiClassNameLen)
170+
{
171+
if (uiClassNameLen > 0)
172+
{
173+
char* szClassNameCopy = new char[uiClassNameLen + 1];
174+
memcpy(szClassNameCopy, szClassName, uiClassNameLen);
175+
szClassNameCopy[uiClassNameLen] = 0;
176+
m_szClassName = szClassNameCopy;
177+
}
178+
else
179+
{
180+
m_szClassName = NULL;
181+
}
182+
}
183+
184+
CBaseEntityGenerator::~CBaseEntityGenerator()
185+
{
186+
delete[] m_szClassName;
164187
}
165188

166-
// ----------------------------------------------------------------------------
167-
// Returns the next valid CBaseEntity instance.
168-
// ----------------------------------------------------------------------------
169189
CBaseEntityWrapper* CBaseEntityGenerator::getNext()
170190
{
171-
CBaseEntity* result = m_pCurrentEntity;
172-
m_pCurrentEntity = (CBaseEntity *) servertools->NextEntity(m_pCurrentEntity);
173-
return (CBaseEntityWrapper *) result;
191+
CBaseEntity* result = NULL;
192+
while (m_pCurrentEntity)
193+
{
194+
result = m_pCurrentEntity;
195+
m_pCurrentEntity = (CBaseEntity *)servertools->NextEntity(m_pCurrentEntity);
196+
if (result && m_uiClassNameLen && m_szClassName)
197+
{
198+
const char* szClassname = IServerUnknownExt::GetClassname(result);
199+
if (!m_bExactMatch && strncmp(szClassname, m_szClassName, m_uiClassNameLen) != 0)
200+
continue;
201+
202+
else if (m_bExactMatch && strcmp(szClassname, m_szClassName) != 0)
203+
continue;
204+
}
205+
return (CBaseEntityWrapper*) result;
206+
}
207+
return NULL;
174208
}
175209

176210

177211
// ----------------------------------------------------------------------------
178-
// CServerClassGenerator Constructor.
212+
// CServerClassGenerator
179213
// ----------------------------------------------------------------------------
180214
CServerClassGenerator::CServerClassGenerator( PyObject* self ):
181215
IPythonGenerator<ServerClass>(self)
182216
{
183217
m_pCurrentServerClass = servergamedll->GetAllServerClasses();
184218
}
185219

186-
// ----------------------------------------------------------------------------
187-
// CServerClassGenerator Copy-Constructor.
188-
// ----------------------------------------------------------------------------
189220
CServerClassGenerator::CServerClassGenerator( PyObject* self, const CServerClassGenerator& rhs ):
190221
IPythonGenerator<ServerClass>(self)
191222
{
192223
m_pCurrentServerClass = rhs.m_pCurrentServerClass;
193224
}
194225

195-
// ----------------------------------------------------------------------------
196-
// Returns the next valid CBaseEntity instance.
197-
// ----------------------------------------------------------------------------
198226
ServerClass* CServerClassGenerator::getNext()
199227
{
200228
if (!m_pCurrentServerClass)

src/core/modules/entities/entities_generator.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,19 @@ class CBaseEntityGenerator: public IPythonGenerator<CBaseEntityWrapper>
7575
public:
7676
CBaseEntityGenerator(PyObject* self);
7777
CBaseEntityGenerator(PyObject* self, const CBaseEntityGenerator& rhs);
78-
virtual ~CBaseEntityGenerator() {}
78+
CBaseEntityGenerator(PyObject* self, const char* szClassName);
79+
CBaseEntityGenerator(PyObject* self, const char* szClassName, bool exactMatch);
80+
virtual ~CBaseEntityGenerator();
7981

8082
protected:
8183
virtual CBaseEntityWrapper* getNext();
8284

8385
private:
8486
CBaseEntity* m_pCurrentEntity;
87+
void makeStringCopy(const char* szClassName, unsigned int uiClassNameLen);
88+
const char* m_szClassName;
89+
unsigned int m_uiClassNameLen;
90+
bool m_bExactMatch;
8591
};
8692

8793
BOOST_SPECIALIZE_HAS_BACK_REFERENCE(CBaseEntityGenerator)

src/core/modules/entities/entities_wrap.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,8 @@ void export_check_transmit_info(scope _entities)
541541
void export_baseentity_generator(scope _entities)
542542
{
543543
class_<CBaseEntityGenerator>("BaseEntityGenerator")
544+
.def(init<const char*>())
545+
.def(init<const char*, bool>())
544546
.def("__iter__", &CBaseEntityGenerator::iter)
545547
.def("__next__", &CBaseEntityGenerator::next, reference_existing_object_policy())
546548
;

0 commit comments

Comments
 (0)