Skip to content

Commit f5f2ec1

Browse files
committed
Added getting/setting network properties
1 parent 8c4b83f commit f5f2ec1

File tree

3 files changed

+74
-131
lines changed

3 files changed

+74
-131
lines changed

src/core/modules/entities/entities_wrap.cpp

Lines changed: 29 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,20 @@ typedef boost::unordered_map<std::string, CSendPropHashTable*> SendPropMap;
5959
SendPropMap g_SendPropMap;
6060

6161

62+
//-----------------------------------------------------------------------------
63+
// edict_t extension
64+
//-----------------------------------------------------------------------------
65+
object CEdictExt::GetProp( edict_t* pEdict, const char* prop_name )
66+
{
67+
return CSendProp(pEdict, prop_name).Get();
68+
}
69+
70+
void CEdictExt::SetProp( edict_t* pEdict, const char* prop_name, object value)
71+
{
72+
CSendProp(pEdict, prop_name).Set(value);
73+
}
74+
75+
6276
//-----------------------------------------------------------------------------
6377
// CSendProp code.
6478
//-----------------------------------------------------------------------------
@@ -172,120 +186,27 @@ CSendProp::CSendProp( edict_t* edict, const char* prop_name )
172186
}
173187
}
174188

175-
void CSendProp::set_int( int iValue )
176-
{
177-
// Is the property of "integer" type?
178-
if( m_send_prop->GetType() == DPT_Int )
179-
{
180-
// Set the value.
181-
*(int *)((char *)m_base_entity + m_prop_offset) = iValue;
182-
183-
// Force a network update.
184-
m_edict->StateChanged();
185-
}
186-
else
187-
{
188-
BOOST_RAISE_EXCEPTION(PyExc_TypeError, "Property is not an integer.");
189-
}
190-
}
191-
192-
void CSendProp::set_float( float flValue )
189+
void CSendProp::Set( object value )
193190
{
194-
// Is the property of "float" type?
195-
if( m_send_prop->GetType() == DPT_Float )
196-
{
197-
// Set the value.
198-
*(float *)((char *)m_base_entity + m_prop_offset) = flValue;
199-
200-
// Force a network update.
201-
m_edict->StateChanged();
202-
}
203-
else
191+
switch(m_send_prop->GetType())
204192
{
205-
BOOST_RAISE_EXCEPTION(PyExc_TypeError, "Property is not a float.");
193+
case DPT_Int: Set<int>(extract<int>(value)); break;
194+
case DPT_Float: Set<float>(extract<float>(value)); break;
195+
case DPT_String: Set<const char *>(extract<const char *>(value)); break;
196+
case DPT_Vector: Set<Vector>(extract<Vector>(value)); break;
206197
}
198+
BOOST_RAISE_EXCEPTION(PyExc_TypeError, "Unknown property type.");
207199
}
208200

209-
void CSendProp::set_string( const char* szValue )
201+
object CSendProp::Get()
210202
{
211-
// Is the property of "string" type?
212-
if( m_send_prop->GetType() == DPT_String )
203+
switch(m_send_prop->GetType())
213204
{
214-
// Get the address of the string buffer.
215-
char* data_buffer = (char *)((char *)m_base_entity + m_prop_offset);
216-
217-
// Write the string to the buffer.
218-
V_strncpy(data_buffer, szValue, DT_MAX_STRING_BUFFERSIZE);
219-
220-
// Force a network update.
221-
m_edict->StateChanged();
222-
}
223-
else
224-
{
225-
BOOST_RAISE_EXCEPTION(PyExc_TypeError, "Property is not a string.");
205+
case DPT_Int: return object(Get<int>());
206+
case DPT_Float: return object(Get<float>());
207+
case DPT_String: return object(Get<const char *>());
208+
case DPT_Vector: return object(Get<Vector>());
226209
}
227-
}
228-
229-
void CSendProp::set_vector( CVector* vecValue )
230-
{
231-
// Is the property of "vector" type?
232-
if( m_send_prop->GetType() == DPT_Vector )
233-
{
234-
*(Vector *)((char *)m_base_entity + m_prop_offset) = *(Vector *) vecValue;
235-
236-
// Force a network update.
237-
m_edict->StateChanged();
238-
}
239-
else
240-
{
241-
BOOST_RAISE_EXCEPTION(PyExc_TypeError, "Property is not a vector.");
242-
}
243-
}
244-
245-
int CSendProp::get_int()
246-
{
247-
// Is the property of "integer" type?
248-
if( m_send_prop->GetType() == DPT_Int )
249-
{
250-
return *(int *)((char *)m_base_entity + m_prop_offset);
251-
}
252-
253-
BOOST_RAISE_EXCEPTION(PyExc_TypeError, "Property is not an integer.");
254-
return NULL;
255-
}
256-
257-
float CSendProp::get_float()
258-
{
259-
// Is the property of "float" type?
260-
if( m_send_prop->GetType() == DPT_Float )
261-
{
262-
return *(float *)((char *)m_base_entity + m_prop_offset);
263-
}
264-
265-
BOOST_RAISE_EXCEPTION(PyExc_TypeError, "Property is not a float.");
266-
return NULL;
267-
}
268-
269-
const char* CSendProp::get_string()
270-
{
271-
// Is the property of "string" type?
272-
if( m_send_prop->GetType() == DPT_String )
273-
{
274-
return (const char *)((char *)m_base_entity + m_prop_offset);
275-
}
276-
277-
BOOST_RAISE_EXCEPTION(PyExc_TypeError, "Property is not a string.");
278-
return NULL;
279-
}
280-
281-
CVector* CSendProp::get_vector()
282-
{
283-
// Is the property of "vector" type?
284-
if( m_send_prop->GetType() == DPT_Vector )
285-
{
286-
return new CVector(*(Vector *) ((char *)m_base_entity + m_prop_offset));
287-
}
288-
289-
BOOST_RAISE_EXCEPTION(PyExc_TypeError, "Property is not a vector.");
290-
return NULL;
210+
BOOST_RAISE_EXCEPTION(PyExc_TypeError, "Unknown property type.");
211+
return object(); // Just to disable a warning...
291212
}

src/core/modules/entities/entities_wrap.h

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,12 @@
3838
//---------------------------------------------------------------------------------
3939
// edict_t extension
4040
//---------------------------------------------------------------------------------
41-
/*
42-
class edict_tExt
41+
class CEdictExt
4342
{
4443
public:
45-
int GetPropInt( const char* prop_name );
46-
float GetPropFloat( const char* prop_name );
47-
const char* GetPropString( const char* prop_name );
48-
Vector* GetPropVector( const char* prop_name );
49-
object GetProp( const char* prop_name );
50-
51-
void SetPropInt( const char* prop_name, int iValue );
52-
void SetPropFloat( const char* prop_name, float flValue );
53-
void SetPropString( const char* prop_name, const char* szValue );
54-
void SetPropVector( const char* prop_name, Vector* vecValue );
55-
void SetProp( const char* prop_name, object value);
44+
static object GetProp( edict_t* pEdict, const char* prop_name );
45+
static void SetProp( edict_t* pEdict, const char* prop_name, object value);
5646
};
57-
*/
5847

5948
//---------------------------------------------------------------------------------
6049
// Custom SendProp wrapper.
@@ -64,17 +53,37 @@ class CSendProp
6453
public:
6554
CSendProp( edict_t* edict, const char* prop_name );
6655

67-
SendPropType get_type();
56+
SendPropType GetType();
57+
58+
template<class T>
59+
T Get()
60+
{ return *(T *) ((char *) m_base_entity + m_prop_offset); }
61+
62+
object Get();
63+
64+
template<class T>
65+
void Set(T value)
66+
{
67+
*(T *)((char *) m_base_entity + m_prop_offset) = value;
68+
69+
// Force a network update.
70+
m_edict->StateChanged();
71+
}
72+
73+
template<>
74+
void Set(const char* szValue)
75+
{
76+
// Get the address of the string buffer.
77+
char* data_buffer = (char *)((char *)m_base_entity + m_prop_offset);
6878

69-
void set_int( int value );
70-
void set_float( float value );
71-
void set_string( const char* value );
72-
void set_vector( CVector* pVec );
79+
// Write the string to the buffer.
80+
V_strncpy(data_buffer, szValue, DT_MAX_STRING_BUFFERSIZE);
81+
82+
// Force a network update.
83+
m_edict->StateChanged();
84+
}
7385

74-
int get_int();
75-
float get_float();
76-
const char* get_string();
77-
CVector* get_vector();
86+
void Set(object value);
7887

7988
private:
8089
// Offset from the beginning of the network table that

src/core/modules/entities/entities_wrap_python.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,19 @@ void export_edict()
369369
reference_existing_object_policy()
370370
)
371371

372+
.def("get_prop",
373+
&CEdictExt::GetProp,
374+
"Returns the value of a network property.",
375+
args("prop_name")
376+
)
377+
378+
.def("set_prop",
379+
&CEdictExt::SetProp,
380+
"Set the a network property to the given value.",
381+
args("prop_name", "value")
382+
)
383+
384+
// Class attributes
372385
.def_readwrite("free_time",
373386
&edict_t::freetime,
374387
"The server timestampe at which the edict was freed."

0 commit comments

Comments
 (0)