[Cs:s] How to hook weapon reload

Please post any questions about developing your plugin here. Please use the search function before posting!
cssbestrpg
Senior Member
Posts: 246
Joined: Sun May 17, 2020 7:56 am
Location: Finland
Contact:

[Cs:s] How to hook weapon reload

Postby cssbestrpg » Sat Nov 05, 2022 4:25 pm

Hi, how i can hook when player have finished weapon reload?
I can't use weapon_reload event since it doesn't get called when weapon starts reloading when automatically starts reloading

Edit:

i tried to hook CBaseCombatWeapon::Reload

Syntax: Select all

from core import PLATFORM
from memory import Convention, DataType
from entities.hooks import EntityPreHook, EntityCondition

if PLATFORM == 'linux':
offset = 271
else:
offset = 270

def prepare_hook(entity):
return entity.pointer.make_virtual_function(offset,
Convention.THISCALL,
[DataType.POINTER],
DataType.VOID)

@EntityPreHook(EntityCondition.is_player, prepare_hook)
def weapon_reloaded_hook(args):
print('Weapon reload was executed')


but the print never get executed
User avatar
L'In20Cible
Project Leader
Posts: 1496
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Re: [Cs:s] How to hook weapon reload

Postby L'In20Cible » Sun Nov 06, 2022 11:21 am

The first problem is that you are effectively hooking CBaseCombatCharacter::RemovePlayerItem because your hook targets players, not weapons. To target weapons, you would have to change your condition to:

Syntax: Select all

lambda entity: entity.is_weapon()


However, Reload is virtual and each weapon entity implements its own so you have to hook it for every single one of them. Try something like this:

Syntax: Select all

from core import PLATFORM
from memory import Convention, DataType, make_object
from memory.hooks import use_pre_registers
from entities.hooks import EntityPostHook, EntityCondition
from players.entity import Player
from weapons.entity import Weapon
from weapons.manager import weapon_manager

def post_weapon_reload(stack_data, return_value):
with use_pre_registers(stack_data):
try:
weapon = make_object(Weapon, stack_data[0])
except ValueError:
return

if not weapon.get_datamap_property_bool('m_bInReload'):
return

owner = weapon.owner
if owner is None or not owner.is_player():
return

player = Player(owner.index)
print(f'{player.name} is reloading his {weapon.classname}')

for classname, data in weapon_manager.items():
if data.ammoprop is None:
continue
post_weapon_reload = EntityPostHook(
EntityCondition.equals_entity_classname(classname),
lambda entity: entity.make_virtual_function(
271 if PLATFORM == 'linux' else 270,
Convention.THISCALL,
(DataType.POINTER,),
DataType.VOID
)
)(post_weapon_reload)
cssbestrpg
Senior Member
Posts: 246
Joined: Sun May 17, 2020 7:56 am
Location: Finland
Contact:

Re: [Cs:s] How to hook weapon reload

Postby cssbestrpg » Sun Nov 06, 2022 12:56 pm

That seem to work fully, when i tried to hook it myself i honestly had no idea what i was doing, i am not really experienced of hooking stuff

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 2 guests