Page 1 of 1

Something like sdkhooks

Posted: Thu Aug 04, 2016 5:52 pm
by velocity
I've just switched from SourceMod to sourcepython recently, since python is my favorite language.

Does sourcepython have something similar to sdkhooks and the OnPlayerRunCmd?

Such as OnWeaponSwitch and if I wanted to force a player to crouch how would I do that?

Re: Something like sdkhooks

Posted: Thu Aug 04, 2016 6:19 pm
by satoon101
That is all directly built into Source.Python, so you won't need an extension. Some of the virtual/dynamic functions are already in our data:
https://github.com/Source-Python-Dev-Te ... n/entities

If you look at the files in that directory, and the engine/game directories inside that directory, you will find that some have function and/or virtual_function sections. You can use those function names to hook or call the function itself. To hook CBasePlayer::PlayerRunCommand, for instance, you should see this in the data (for CS:S, but exists for other engines/games, as well):
https://github.com/Source-Python-Dev-Te ... er.ini#L40

Then, your plugin might look something like:

Syntax: Select all

from entities.hooks import EntityCondition, EntityPreHook

@EntityPreHook(EntityCondition.is_player, 'run_command')
def pre_player_run_command(stack_data):
pass

We do not have a hook for OnSwitchWeapon, as of yet. Your last question is unrelated and needs to be in its own thread.

Re: Something like sdkhooks

Posted: Thu Aug 04, 2016 6:31 pm
by Ayuto
To force all players to crouch, you could use something like that:

Syntax: Select all

import memory

from entities.hooks import EntityPreHook
from entities.hooks import EntityCondition

from players import UserCmd
from players.constants import PlayerButtons

@EntityPreHook(EntityCondition.is_player, 'run_command')
def pre_run_command(stack_data):
ucmd = memory.make_object(UserCmd, stack_data[1])

# Force the player to crouch
ucmd.buttons |= PlayerButtons.DUCK

Re: Something like sdkhooks

Posted: Thu Aug 04, 2016 8:29 pm
by velocity
Yo guys, thats super cool thank u!

Re: Something like sdkhooks

Posted: Fri Aug 12, 2016 12:00 pm
by L'In20Cible
CBaseCombatCharacter::Weapon_Switch has been added to the data:

Syntax: Select all

from entities.hooks import EntityCondition
from entities.hooks import EntityPreHook
from memory import make_object
from players.entity import Player
from weapons.entity import Weapon

@EntityPreHook(EntityCondition.is_player, 'weapon_switch')
def pre_weapon_switch(stack_data):
player = make_object(Player, stack_data[0])
weapon = make_object(Weapon, stack_data[1])

print(player.name, 'switched to his', weapon.classname)

Re: Something like sdkhooks

Posted: Fri Aug 12, 2016 1:11 pm
by L'In20Cible
I looked more into that function and, in order to use it as a listener, it would ideally look like:

Syntax: Select all

from entities.hooks import EntityCondition
from entities.hooks import EntityPostHook
from memory import make_object
from players.entity import Player
from weapons.entity import Weapon

@EntityPostHook(EntityCondition.is_player, 'weapon_switch')
def post_weapon_switch(stack_data, return_value):
# Was the switch successful?
if not return_value:
return

player = make_object(Player, stack_data[0])
weapon = make_object(Weapon, stack_data[1])

print(player.name, 'switched to his', weapon.classname)


However, from my testings, the player instance is not retrievable from a post-hook. An alternative could be:

Syntax: Select all

from entities.hooks import EntityCondition
from entities.hooks import EntityPreHook
from memory import make_object
from players.entity import Player
from weapons.entity import Weapon

@EntityPreHook(EntityCondition.is_player, 'weapon_switch')
def pre_weapon_switch(stack_data):
# Was the switch successful?
if not pre_weapon_switch.hooked_function.skip_hooks(
*tuple(stack_data)[1:]):
return

player = make_object(Player, stack_data[0])
weapon = make_object(Weapon, stack_data[1])

print(player.name, 'switched to his', weapon.classname)


However this is not an elegant solution as it may breaks other plugins that would want to block the switch to actually happens. Storing the this pointer into a pre-hook and using it into a post hook is probably the best solution I can think of.

Re: Something like sdkhooks

Posted: Fri Aug 12, 2016 4:08 pm
by iPlayer
However, from my testings, the player instance is not retrievable from a post-hook.

Related? viewtopic.php?p=7542#p7542

Re: Something like sdkhooks

Posted: Fri Aug 12, 2016 8:26 pm
by L'In20Cible
Yup it is.