Something like sdkhooks

Please post any questions about developing your plugin here. Please use the search function before posting!
User avatar
velocity
Senior Member
Posts: 220
Joined: Sat May 10, 2014 6:17 pm

Something like sdkhooks

Postby velocity » Thu Aug 04, 2016 5:52 pm

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?
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Re: Something like sdkhooks

Postby satoon101 » Thu Aug 04, 2016 6:19 pm

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.
Image
User avatar
Ayuto
Project Leader
Posts: 2195
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: Something like sdkhooks

Postby Ayuto » Thu Aug 04, 2016 6:31 pm

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
User avatar
velocity
Senior Member
Posts: 220
Joined: Sat May 10, 2014 6:17 pm

Re: Something like sdkhooks

Postby velocity » Thu Aug 04, 2016 8:29 pm

Yo guys, thats super cool thank u!
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Re: Something like sdkhooks

Postby L'In20Cible » Fri Aug 12, 2016 12:00 pm

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)
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Re: Something like sdkhooks

Postby L'In20Cible » Fri Aug 12, 2016 1:11 pm

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.
User avatar
iPlayer
Developer
Posts: 590
Joined: Sat Nov 14, 2015 8:37 am
Location: Moscow
Contact:

Re: Something like sdkhooks

Postby iPlayer » Fri Aug 12, 2016 4:08 pm

However, from my testings, the player instance is not retrievable from a post-hook.

Related? viewtopic.php?p=7542#p7542
Image /id/its_iPlayer
My plugins: Map Cycle • Killstreaker • DeadChat • Infinite Jumping • TripMines • AdPurge • Bot Damage • PLRBots • Entity AntiSpam

Hail, Companion. [...] Hands to yourself, sneak thief. Image
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Re: Something like sdkhooks

Postby L'In20Cible » Fri Aug 12, 2016 8:26 pm

Yup it is.

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 48 guests