Using EntityPreHook on drop_weapon

Please post any questions about developing your plugin here. Please use the search function before posting!
User avatar
BackRaw
Senior Member
Posts: 537
Joined: Sun Jul 15, 2012 1:46 am
Location: Germany
Contact:

Using EntityPreHook on drop_weapon

Postby BackRaw » Sun Aug 02, 2015 9:08 pm

Hi,

how would I go about using EntityPreHook on drop_weapon for a player? I'm confused.. currently I have

Syntax: Select all

from entities.helpers import index_from_pointer
from entities.hooks import EntityPreHook

@EntityPreHook('player', 'drop_weapon')
def pre_drop_weapon(ptr):

print(index_from_pointer(ptr))

return 2

But, it turns out the variable passed, ptr, is a StackData object. How can I get the player index/entity that dropped the weapon? Do I need to go in a completely different direction? :D
My Github repositories:

Source.Python: https://github.com/backraw
User avatar
Ayuto
Project Leader
Posts: 2195
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Postby Ayuto » Sun Aug 02, 2015 9:31 pm

EntityPreHook is actually the same like a PreHook. The passed argument is a StackData object (I usually call it "args"), which provides you high- and low-level access to the stack of the hooked function. After looking at the data files you know that the StackData object will provide you access to 4 arguments. You could also just use "print(args)". However, the first argument is the pointer of the player, the second argument is the pointer of the weapon. So, you could do something like this:

Syntax: Select all

import memory

from entities.hooks import EntityPreHook
from players.entity import PlayerEntity
from weapons.entity import WeaponEntity

@EntityPreHook('player', 'drop_weapon')
def pre_drop_weapon(args):
player = memory.make_object(PlayerEntity, args[0])
weapon = memory.make_object(WeaponEntity, args[1])
print('{0} wants to drop his weapon.'.format(player.name))


While checking the number of arguments in the data files, I also noticed that we defined INT as the return type. That is actually wrong and should be VOID. If you are using the latest release you also need to update your hooking line. We no longer use the entity class names like "player" or "cs_bot", but "CCSPlayer" and "CCSBot". However, in order to block dropping a weapon, you could also block the client command "dop".
Predz
Senior Member
Posts: 158
Joined: Wed Aug 08, 2012 9:05 pm
Location: Bristol, United Kingdom

Postby Predz » Sun Aug 02, 2015 9:31 pm

Lol you beat me to it Ayuto! :P
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Sun Aug 02, 2015 9:35 pm

StackData is basically an indexible object that holds all of the arguments. The arguments for drop_weapon are the player's pointer, the weapon's pointer, and 2 vector pointers. Our entity data for the dynamic and virtual functions doesn't include the entity's (player's) pointer, since it is already known. Here is the data for drop_weapon on CS:S:
https://github.com/Source-Python-Dev-Team/Source.Python/blob/master/addons/source-python/data/source-python/entities/orangebox/cstrike/CBasePlayer.ini#L10

*Edit: darn slow typing phone :)
Image
User avatar
BackRaw
Senior Member
Posts: 537
Joined: Sun Jul 15, 2012 1:46 am
Location: Germany
Contact:

Postby BackRaw » Mon Aug 03, 2015 12:23 am

Thanks all of you for this wonderful clarification. However:
Ayuto wrote:We no longer use the entity class names like "player" or "cs_bot", but "CCSPlayer" and "CCSBot".
Do I need to use this now?

Syntax: Select all

@EntityPreHook('CCSPlayer', 'drop_weapon')
My Github repositories:

Source.Python: https://github.com/backraw
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Mon Aug 03, 2015 12:33 am

Yes. The main reason behind that change is that if you were to hook a few specific virtual functions for players using a bot, the function you would hook would be the CCSBot member function and not the CCSPlayer one. This would cause the function hook to not be called for human players. I ran into this issue while testing bump_weapon when I loaded the test script 'after' a bot was already on the server.

The following list is all the CCSBot specific virtual functions for CS:S:

  • 0 _ZN6CCSBotD1Ev
  • 1 _ZN6CCSBotD0Ev
  • 100 _ZN6CCSBot5TouchEP11CBaseEntity
  • 12 _ZN6CCSBot14GetDataDescMapEv
  • 23 _ZN6CCSBot5SpawnEv
  • 296 _ZN6CCSBot13PushawayTouchEP11CBaseEntity
  • 312 _ZNK6CCSBot16GetLastKnownAreaEv
  • 343 _ZNK6CCSBot9IsRunningEv
  • 397 _ZN6CCSBot10BumpWeaponEP17CBaseCombatWeapon
  • 468 _ZN6CCSBot5BlindEfff
  • 469 _ZN6CCSBot10InitializeEPK10BotProfilei
  • 470 _ZN6CCSBot6UpkeepEv
  • 471 _ZN6CCSBot6UpdateEv
  • 473 _ZN6CCSBot4WalkEv
  • 480 _ZN6CCSBot4JumpEb
  • 483 _ZN6CCSBot13PrimaryAttackEv
  • 489 _ZN6CCSBot12BuildUserCmdER8CUserCmdRK6QAnglefffih
  • 492 _ZN6CCSBot12GetMoveSpeedEv
  • 63 _ZN6CCSBot12OnTakeDamageERK15CTakeDamageInfo
  • 67 _ZN6CCSBot12Event_KilledERK15CTakeDamageInfo
Image
User avatar
BackRaw
Senior Member
Posts: 537
Joined: Sun Jul 15, 2012 1:46 am
Location: Germany
Contact:

Postby BackRaw » Tue Aug 04, 2015 4:50 am

Thanks a bunch!
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Fri Aug 07, 2015 1:59 pm

Just a heads up, but we have unfortunately found a major issue with this new EntityPreHook/EntityPostHook implementation, as well. We have a new version in the works that should work in any/all situations. More on that when we get it committed.
Image

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 136 guests