Raised This Month: $ Target: $400
 0% 

[L4D2] Weapon Switch API


Post New Thread Reply   
 
Thread Tools Display Modes
Author
Paimon
Member
Join Date: Jul 2021
Location: Zootopia
Plugin ID:
8753
Plugin Version:
2024.08.17#37
Plugin Category:
Technical/Development
Plugin Game:
Left 4 Dead
Plugin Dependencies:
    Servers with this Plugin:
     
    Plugin Description:
    [L4D2] Weapon Switch API
    Old 08-17-2024 , 00:46   [L4D2] Weapon Switch API
    Reply With Quote #1

    Description:

    This plugin is a library that allows you to switch the players actived weapon to target slot by using this api:
    PHP Code:
    /**
     * Switch to weapon
     *
     * @param client    Client index
     * @param slot    Weapon/Item slot
     * @return          True if success, false otherwise.
     */
    native bool Player_SwitchToWeapon(int clientint slot); 
    and 2 forward
    PHP Code:
    /**
     * When a client tries to switch weapon
     *
     * @param client    Client index
     * @param weapon    Weapon entity index
     * @param param    Dont know
     * @return          Plugin_Handled or Plugin_Stop to block player switch weapon, Plugin_Continue otherwise.
     */
    forward Action Player_OnSwitchToWeapon(int clientint weaponint param);

    /**
     * When a client tries to switch weapon finished
     *
     * @param client    Client index
     * @param weapon    Weapon entity index
     * @param param    Dont know
     */
    forward void Player_OnSwitchToWeapon_Post(int clientint weaponint param); 
    The forwards have no difference with SDKHook_WeaponSwitch and SDKHook_WeaponSwitchPost, just you dont need to create a sdkhook?

    A test plugin has been upload as followed.
    Attached Files
    File Type: sp Get Plugin or Get Source (weapon_action_api.sp - 58 views - 3.9 KB)
    File Type: inc weapon_action_api.inc (1.2 KB, 27 views)
    File Type: sp Get Plugin or Get Source (wpnapi_test.sp - 28 views - 1.2 KB)
    File Type: txt weapon_action_api.txt (3.0 KB, 28 views)
    File Type: smx weapon_action_api.smx (4.8 KB, 17 views)
    __________________

    Last edited by Paimon; 09-11-2024 at 14:11.
    Paimon is offline
    Paimon
    Member
    Join Date: Jul 2021
    Location: Zootopia
    Old 08-17-2024 , 01:17   Re: [L4D2] Weapon Switch API
    Reply With Quote #2

    My whining:
    After a long time searching finally I realized there is no implemented API for "Switch player weapon".
    SDKHook only provide hooks for "SDKHook_WeaponSwitch", but no function call.
    I am so confused about that because I think this is very useful(maybe?), current for me.
    So I decided to find it.

    First, we need to find "CBaseCombatCharacter::Weapon_Equip",
    it's easy to get it because of unique string "%s equipped with %s, proficiency is %s".
    Then, compare it with the same function in "server_srv.so",
    we can locate a function "CBaseCombatCharacter::SetActiveWeapon" (believe you can do this),
    open it, and check its xrefs by press "X" at IDA,
    finally you could see 5 calls,
    one of them is "CBaseCombatCharacter::Weapon_Switch",
    one by one to analyze it though "server_srv.so" and "server.dll", you could figure it out.
    The last step is the hardest step so it needs patience.
    And using "Pseudocode" window in IDA(Press Tab) can do a lot of help.
    __________________

    Last edited by Paimon; 08-17-2024 at 01:31.
    Paimon is offline
    Paimon
    Member
    Join Date: Jul 2021
    Location: Zootopia
    Old 08-17-2024 , 01:46   Re: [L4D2] Weapon Switch API
    Reply With Quote #3

    Target usage:
    1. Make the inputs(slot1, slot2, ...) go through Menu/Panel. (My target)
    2. Prevent original switch action.
    3. balabala....
    __________________

    Last edited by Paimon; 09-11-2024 at 14:22.
    Paimon is offline
    little_froy
    Senior Member
    Join Date: May 2021
    Old 08-17-2024 , 06:35   Re: [L4D2] Weapon Switch API
    Reply With Quote #4

    I use this to change weapon: FakeClientCommand "use weapon_rifle" for example
    little_froy is offline
    Paimon
    Member
    Join Date: Jul 2021
    Location: Zootopia
    Old 08-17-2024 , 07:15   Re: [L4D2] Weapon Switch API
    Reply With Quote #5

    Quote:
    Originally Posted by little_froy View Post
    I use this to change weapon: FakeClientCommand "use weapon_rifle" for example
    Damned I even dont know this command...
    __________________
    Paimon is offline
    Reply


    Thread Tools
    Display Modes

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off

    Forum Jump


    All times are GMT -4. The time now is 18:43.


    Powered by vBulletin®
    Copyright ©2000 - 2024, vBulletin Solutions, Inc.
    Theme made by Freecode