Page 1 of 1

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

Posted: Fri Apr 22, 2016 2:41 am
by iPlayer

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?

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

Posted: Fri Apr 22, 2016 6:41 am
by D3CEPTION
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

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

Posted: Sun Apr 24, 2016 2:01 pm
by Ayuto
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

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

Posted: Sun Apr 24, 2016 3:32 pm
by iPlayer
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.