Skip to content

Commit fd63cc1

Browse files
committed
Improved Entity's initialization by pre-caching BaseEntity.pointer.
1 parent 4e265d4 commit fd63cc1

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

Diff for: src/core/modules/entities/entities_entity.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "modules/physics/physics.h"
3737
#include ENGINE_INCLUDE_PATH(entities_datamaps_wrap.h)
3838
#include "../engines/engines.h"
39+
#include "modules/core/core_cache.h"
3940

4041
// ============================================================================
4142
// >> External variables
@@ -59,6 +60,12 @@ boost::shared_ptr<CBaseEntityWrapper> CBaseEntityWrapper::wrap(CBaseEntity* pEnt
5960
);
6061
}
6162

63+
void CBaseEntityWrapper::Initialize(object self, unsigned int uiEntityIndex)
64+
{
65+
static CCachedProperty *pointer = extract<CCachedProperty *>(get_class_object<CBaseEntityWrapper>().attr("pointer"));
66+
pointer->set_cached_value(self, object(GetPointer()));
67+
}
68+
6269
CBaseEntity* CBaseEntityWrapper::create(const char* name)
6370
{
6471
#ifdef ENGINE_CSGO

Diff for: src/core/modules/entities/entities_entity.h

+1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ class CBaseEntityWrapper: public IServerEntity
9494

9595
public:
9696
static boost::shared_ptr<CBaseEntityWrapper> __init__(unsigned int uiEntityIndex);
97+
void Initialize(object self, unsigned int uiEntityIndex);
9798
static boost::shared_ptr<CBaseEntityWrapper> wrap(CBaseEntity* pEntity);
9899
static CBaseEntity* create(const char* name);
99100
static object create(object cls, const char* name);

Diff for: src/core/modules/entities/entities_entity_wrap.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,13 @@ void export_base_entity(scope _entity)
6161
BaseEntity.def("__init__",
6262
make_constructor(
6363
&CBaseEntityWrapper::__init__,
64-
default_call_policies(),
64+
post_constructor_policies<default_call_policies>(
65+
make_function(
66+
&CBaseEntityWrapper::Initialize,
67+
default_call_policies(),
68+
args("self", "entity_index")
69+
)
70+
),
6571
args("entity_index")
6672
)
6773
);

Diff for: src/core/utilities/sp_util.h

+13
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,19 @@ inline bool CheckClassname(object obj, char* name)
9393
return strcmp(extract<char *>(obj.attr("__class__").attr("__name__")), name) == 0;
9494
}
9595

96+
//-----------------------------------------------------------------------------
97+
// Returns the registered python class for T.
98+
//-----------------------------------------------------------------------------
99+
template<class T>
100+
inline object get_class_object()
101+
{
102+
const converter::registration *pRegistration = converter::registry::query(typeid(T));
103+
if (!pRegistration || !pRegistration->m_class_object)
104+
return object();
105+
106+
return object(handle<>(borrowed(upcast<PyObject>(pRegistration->m_class_object))));
107+
}
108+
96109
//-----------------------------------------------------------------------------
97110
// Helper template methods for __getitem__ and __setitem__
98111
//-----------------------------------------------------------------------------

0 commit comments

Comments
 (0)