Skip to content

Implement raw entity handle accessors#1830

Merged
psychonic merged 4 commits intoalliedmodders:masterfrom
nosoop:enthandle-from-addr
Dec 29, 2022
Merged

Implement raw entity handle accessors#1830
psychonic merged 4 commits intoalliedmodders:masterfrom
nosoop:enthandle-from-addr

Conversation

@nosoop
Copy link
Contributor

@nosoop nosoop commented Aug 31, 2022

Closes #1805.

Implements the ability to read / write handles located at arbitrary locations.

The native names LoadEntityFromHandleAddress and StoreEntityToHandleAddress are wordy. Feel free to suggest alternatives.

Tested and confirmed working in TF2 with the following plugin (dumps the entity and item definition indices of each wearable on the player):

#include <sourcemod>

#pragma semicolon 1
#pragma newdecls required

public void OnPluginStart() {
    RegConsoleCmd("sm_wearables", ListWearables);
}

Action ListWearables(int client, int argc) {
    int offs_CTFPlayer_hMyWearables = FindSendPropInfo("CTFPlayer", "m_hMyWearables");
    
    // CTFPlayer::m_hMyWearables is a CUtlVector<CHandle>
    int count = GetEntData(client, offs_CTFPlayer_hMyWearables + 0x0C);
    Address pData = view_as<Address>(GetEntData(client, offs_CTFPlayer_hMyWearables));
    
    for (int i; i < count; i++) {
        // I don't think there's currently a way to write this in a 64-bit compatible way
        Address pHandle = pData + view_as<Address>(0x04 * i);
        int wearable = LoadEntityFromHandleAddress(pHandle);
        
        // testing - write and read data again to confirm that both operations work
        StoreEntityToHandleAddress(pHandle, wearable);
        wearable = LoadEntityFromHandleAddress(pHandle);
        
        if (!IsValidEntity(wearable)) {
            continue;
        }
        ReplyToCommand(client, "[%d] wearable %d (itemdef %d)", i, wearable,
                GetEntProp(wearable, Prop_Send, "m_iItemDefinitionIndex"));
    }
    return Plugin_Handled;
}

@psychonic psychonic merged commit 2130c60 into alliedmodders:master Dec 29, 2022
@nosoop nosoop deleted the enthandle-from-addr branch January 4, 2023 13:30
@sapphonie
Copy link
Contributor

Can we get this in 1.11?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement natives to get / set entity handle values in addresses

3 participants