CS:S Windows crash when trying to hook function with a class method

All other Source.Python topics and issues.
User avatar
iPlayer
Developer
Posts: 590
Joined: Sat Nov 14, 2015 8:37 am
Location: Moscow
Contact:

CS:S Windows crash when trying to hook function with a class method

Postby iPlayer » Fri Apr 22, 2016 2:41 am

Code: Select all

from core import echo_console
from filters.players import PlayerIter


class MyClass:
    @staticmethod
    def pre_bump_weapon(args):
        echo_console("Hooked. args: {}".format(args))

my_class = MyClass()

for player in PlayerIter():
    break
else:
    raise RuntimeError("Join the server")


def load():
    player.bump_weapon.add_pre_hook(my_class.pre_bump_weapon)


def unload():
    player.bump_weapon.remove_pre_hook(my_class.pre_bump_weapon)
- does not crash

Code: Select all

from core import echo_console
from filters.players import PlayerIter


class MyClass:
    def pre_bump_weapon(self, args):
        echo_console("Hooked. Self: {}, args: {}".format(self, args))

my_class = MyClass()

for player in PlayerIter():
    break
else:
    raise RuntimeError("Join the server")


def load():
    player.bump_weapon.add_pre_hook(my_class.pre_bump_weapon)


def unload():
    player.bump_weapon.remove_pre_hook(my_class.pre_bump_weapon)
- crashes when I touch any weapon


In my working case it was a bit different - the function was hooked properly and hook was called successfully, but upon removing the hook from the function, the game crashes the very next time the previously hooked function is called.

Is this me being dumb or does this deserve a GitHub issue?
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
D3CEPTION
Senior Member
Posts: 129
Joined: Tue Jan 26, 2016 1:24 pm
Location: Switzerland

Re: CS:S Windows crash when trying to hook function with a class method

Postby D3CEPTION » Fri Apr 22, 2016 6:41 am

im pretty sure pre/post hook have pure functions access and only carry the args array, so using a method as your function dummy forces the decorator to handle 2 parameters.so i guess your crash happens on c level and ofc on python level a check could be added for "self", but i doubt its general usefulness, so i would simply adjust your code to let the decorator work separately
User avatar
Ayuto
Project Leader
Posts: 2193
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: CS:S Windows crash when trying to hook function with a class method

Postby Ayuto » Sun Apr 24, 2016 2:01 pm

Thanks for reporting! I have fixed the issue:
https://github.com/Source-Python-Dev-Te ... c0856fc0e3

As you can see here we were using a little hack to get around that issue. :D
https://github.com/Source-Python-Dev-Te ... b252d5L108
User avatar
iPlayer
Developer
Posts: 590
Joined: Sat Nov 14, 2015 8:37 am
Location: Moscow
Contact:

Re: CS:S Windows crash when trying to hook function with a class method

Postby iPlayer » Sun Apr 24, 2016 3:32 pm

Thanks, Ayuto.
Funny, that hack is exactly what I was going to use in case this wouldn't be fixed. But I'm happy it's fixed now.
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

Return to “General Discussion”

Who is online

Users browsing this forum: No registered users and 45 guests