Skip to content

Fixed Player/Weapon method resolution order #290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions addons/source-python/packages/source-python/players/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
# =============================================================================
# >> CLASSES
# =============================================================================
class Player(Entity, PlayerMixin):
class Player(PlayerMixin, Entity):
"""Class used to interact directly with players."""

def __init__(self, index):
Expand All @@ -85,7 +85,8 @@ def __init__(self, index):
:raise ValueError:
Raised if the index is invalid.
"""
super().__init__(index)
PlayerMixin.__init__(self, index)
Entity.__init__(self, index)
object.__setattr__(self, '_playerinfo', None)

@classmethod
Expand Down
13 changes: 12 additions & 1 deletion addons/source-python/packages/source-python/weapons/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,20 @@
# =============================================================================
# >> CLASSES
# =============================================================================
class Weapon(Entity, WeaponMixin):
class Weapon(WeaponMixin, Entity):
"""Allows easy usage of the weapon's attributes."""

def __init__(self, index):
"""Initialize the object.

:param int index:
A valid weapon index.
:raise ValueError:
Raised if the index is invalid.
"""
WeaponMixin.__init__(self, index)
Entity.__init__(self, index)

def _validate_clip(self):
"""Test if the weapon has a clip."""
if (
Expand Down
5 changes: 5 additions & 0 deletions src/core/modules/players/players_entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ boost::shared_ptr<PlayerMixin> PlayerMixin::wrap(CBaseEntity* pEntity)
);
}

object PlayerMixin::_obj(object cls, CPointer *pPtr)
{
return cls(object(ExcIndexFromPointer(pPtr)));
}


// CBasePlayer
float PlayerMixin::GetSpeed()
Expand Down
3 changes: 2 additions & 1 deletion src/core/modules/players/players_entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ class PlayerMixin: public CBaseEntityWrapper
public:
static boost::shared_ptr<PlayerMixin> __init__(unsigned int uiEntityIndex);
static boost::shared_ptr<PlayerMixin> wrap(CBaseEntity* pEntity);

static object _obj(object cls, CPointer *pPtr);

// CBasePlayer
// TODO: Return for some of these the proper entity class instead of a handle/index
// E. g. BaseEntity, Entity, Weapon, Player, etc.
Expand Down
5 changes: 4 additions & 1 deletion src/core/modules/players/players_wrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -730,5 +730,8 @@ void export_player_wrapper(scope _players)
".. note:: Only available in TF2.\n\n"
":rtype: int");

_PlayerMixin ADD_MEM_TOOLS(PlayerMixin);
CLASSMETHOD(_PlayerMixin, GET_OBJ_NAME, &PlayerMixin::_obj)
ADD_PTR(PlayerMixin)
ADD_SIZE(PlayerMixin)
STORE_CLASS("PlayerMixin", "PlayerMixin")
}
5 changes: 5 additions & 0 deletions src/core/modules/weapons/weapons_entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ boost::shared_ptr<WeaponMixin> WeaponMixin::wrap(CBaseEntity* pEntity)
);
}

object WeaponMixin::_obj(object cls, CPointer *pPtr)
{
return cls(object(ExcIndexFromPointer(pPtr)));
}


// CBaseCombatWeapon
float WeaponMixin::GetNextAttack()
Expand Down
1 change: 1 addition & 0 deletions src/core/modules/weapons/weapons_entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class WeaponMixin: public CBaseEntityWrapper
public:
static boost::shared_ptr<WeaponMixin> __init__(unsigned int uiEntityIndex);
static boost::shared_ptr<WeaponMixin> wrap(CBaseEntity* pEntity);
static object _obj(object cls, CPointer *pPtr);


float GetNextAttack();
Expand Down
5 changes: 4 additions & 1 deletion src/core/modules/weapons/weapons_entity_wrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,5 +145,8 @@ void export_weapon_entity(scope _entity)
":rtype: int");


_WeaponMixin ADD_MEM_TOOLS(WeaponMixin);
CLASSMETHOD(_WeaponMixin, GET_OBJ_NAME, &WeaponMixin::_obj)
ADD_PTR(WeaponMixin)
ADD_SIZE(WeaponMixin)
STORE_CLASS("WeaponMixin", "WeaponMixin")
}
18 changes: 18 additions & 0 deletions src/core/utilities/wrap_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,24 @@ inline void* GetFuncPtr(Function func)
static_cast< return_type(*)( __VA_ARGS__ ) >(&function)


//---------------------------------------------------------------------------------
// Use these to declare classmethod wrappers.
//---------------------------------------------------------------------------------
#define CLASSMETHOD(cls, name, ...) \
classmethod(cls.def(name, __VA_ARGS__), name)

template<typename T>
T classmethod(T cls, const char *szName)
{
PyTypeObject *self = downcast<PyTypeObject>(cls.ptr());
PyDict_SetItemString(
self->tp_dict, szName,
PyClassMethod_New(PyDict_GetItemString(self->tp_dict, szName))
);
return cls;
};


//---------------------------------------------------------------------------------
// Use this template to create variadic class methods
//---------------------------------------------------------------------------------
Expand Down