Page 1 of 1

Unable to convert index from pointer in hook.

Posted: Sun Dec 10, 2017 2:28 pm
by Doldol
This used to work in an older version of SP on Windows, migrated to a newer version of SP and linux, now I'm running into this issue, I checked the offsets but that're still good. I simplified everything to just the conversion to index and still have the issue.

This is when trying to +USE on an AK/M4

Code:

Syntax: Select all

class CBaseEntity(CustomType, metaclass=manager):
#CBaseEntity::Use(CBaseEntity*, CBaseEntity*, USE_TYPE, float)
Use = manager.virtual_function(
97 if PLATFORM == 'windows' else 98,
(DataType.POINTER, DataType.POINTER, DataType.POINTER, DataType.FLOAT,),
DataType.VOID
)

@property
def entity(self):
return Entity(index_from_pointer(self))

weapon_usp = Entity.create('weapon_ak47')
CBEntity = make_object(CBaseEntity, weapon_usp.pointer)

# @PreHook(weapon_usp.get_input("Use")) = same issue
# @PreHook(weapon_usp.use) = same issue
# @PreHook(CBEntity.Use) = server crashes
@PostHook(CBEntity.Use)
def preUse(args, ra):
w_index = index_from_pointer(args[0])
p_index = index_from_pointer(args[1])
raise Exception(str([w_index, p_index]))

CBEntity.entity.remove()


Exception:

Syntax: Select all

2017-12-10 09:20:38 - sp	-	EXCEPTION	
[SP] Caught an Exception:
Traceback (most recent call last):
File "../addons/source-python/plugins/eweppickup/eweppickup.py", line 83, in preUse
w_index = index_from_pointer(args[0])

ValueError: Conversion from "Pointer" (<_memory.Pointer object at 0xe7ef6f20>) to "Index" failed.


Version:

Syntax: Select all

15:21:39 sp info
15:21:39
Date : 2017-12-10 14:21:41.131080
OS : Linux-4.4.0-101-generic-x86_64-with-debian-stretch-sid
Game : css
SP version : 621



Hello BTW been a while :3

Re: Unable to convert index from pointer in hook.

Posted: Sun Dec 10, 2017 3:31 pm
by Ayuto
Hey,

are you sure this was working fine before the update (what was your previous version)?

In my tests the pre hook was working fine. Only the post hook is having some issues with the this-pointer, because it seems to get overwritten during the function call. So, args[0] doesn't seem to store the this-pointer anymore. Thus, index_from_pointer properly raises an exception (due to the security check we have added on the 8th March 2016.

Re: Unable to convert index from pointer in hook.

Posted: Sun Dec 10, 2017 3:32 pm
by Doldol
I should've clarified that I was using it as a pre-hook previously, I was using the posthook to test because the prehook was crashing.

Edit: test code as prehook works now, I narrowed down to the wrong thing I guess urh, I'll need to figure out what else is wrong.

I'll post if I need some more help, thanks.

Re: Unable to convert index from pointer in hook.

Posted: Sun Dec 10, 2017 3:47 pm
by Doldol

Syntax: Select all

class WeaponDict(dict):
def __init__(self):
data = {weapon.name:"primary" for weapon in WeaponClassIter('primary')}
data.update({weapon.name:"secondary" for weapon in WeaponClassIter('secondary')})
super().__init__(data)

def __missing__(self, key):
return False

weapon_dict = WeaponDict()

class CCSPlayer(CustomType, metaclass=manager):
#CCSPlayer::BumpWeapon(CBaseCombatWeapon*)
BumpWeapon = manager.virtual_function(
397 if PLATFORM == 'windows' else 398,
(DataType.POINTER,),
DataType.BOOL
)

Weapon_Drop = manager.virtual_function(
263 if PLATFORM == 'windows' else 264,
(DataType.POINTER, DataType.POINTER, DataType.POINTER,),
DataType.VOID
)

@property
def entity(self):
return Player(index_from_pointer(self))


class CBaseEntity(CustomType, metaclass=manager):
#CBaseEntity::Use(CBaseEntity*, CBaseEntity*, USE_TYPE, float)
Use = manager.virtual_function(
97 if PLATFORM == 'windows' else 98,
(DataType.POINTER, DataType.POINTER, DataType.POINTER, DataType.FLOAT,),
DataType.VOID
)

@property
def entity(self):
return Entity(index_from_pointer(self))

weapon_usp = Entity.create('weapon_ak47')
CBEntity = make_object(CBaseEntity, weapon_usp.pointer)

# @PreHook(weapon_usp.get_input("Use")) = same issue

# @PreHook(CBEntity.Use) = server crashes
# @PreHook(CBEntity.Use)
@PreHook(weapon_usp.use)
def preUse(args):
CWeapon = make_object(CBaseEntity, args[0])
CPlayer = make_object(CCSPlayer, args[1])
weapon_type = weapon_dict[CWeapon.entity.classname]
if weapon_type:
drop_this = CPlayer.entity.get_weapon(is_filters=weapon_type)
if drop_this:
CPlayer.Weapon_Drop(drop_this.pointer, None, None)
CPlayer.BumpWeapon(CWeapon)

CBEntity.entity.remove()


Exception:

Syntax: Select all

2017-12-10 10:43:33 - sp	-	EXCEPTION	
[SP] Caught an Exception:
Traceback (most recent call last):
File "../addons/source-python/plugins/eweppickup/eweppickup.py", line 88, in preUse
drop_this = CPlayer.entity.get_weapon(is_filters=weapon_type)
File "../addons/source-python/plugins/eweppickup/eweppickup.py", line 60, in entity
return Player(index_from_pointer(self))

ValueError: Conversion from "Pointer" (<_memory.Pointer object at 0xe7fc2200>) to "Index" failed.


This is basically the same issue again but on a prehook afaik.

This is the original code with some edits to match changed function names

Re: Unable to convert index from pointer in hook.

Posted: Sun Dec 10, 2017 4:54 pm
by Ayuto
Doldol wrote:This is the original code with some edits to match changed function names
And all the imports removed :frown: I like copy and pastable code, because it's quite annoying to always write down the imports for someone else' plugin.

The reason why the conversion fails is because you are now hooking "<weapon entity>.use". That's returning the input function which takes different arguments:

Syntax: Select all

from entities.datamaps import InputData

@PreHook(weapon_usp.use)
def preUse(args):
data = make_object(InputData, args[1])
print(data.activator.classname)

However, why don't you want to use OnEntityOutput?

Re: Unable to convert index from pointer in hook.

Posted: Sun Dec 10, 2017 6:03 pm
by Doldol
Ayuto wrote:
Doldol wrote:This is the original code with some edits to match changed function names
And all the imports removed :frown: I like copy and pastable code, because it's quite annoying to always write down the imports for someone else' plugin.

The reason why the conversion fails is because you are now hooking "<weapon entity>.use". That's returning the input function which takes different arguments:

Syntax: Select all

from entities.datamaps import InputData

@PreHook(weapon_usp.use)
def preUse(args):
data = make_object(InputData, args[1])
print(data.activator.classname)

However, why don't you want to use OnEntityOutput?


Sorry :/

It's really old code from before all those helpers were available, thanks for the pointers.