roflmuffin / CounterStrikeSharp

CounterStrikeSharp allows you to write server plugins in C# for Counter-Strike 2/Source2/CS2
https://docs.cssharp.dev
Other
801 stars 126 forks source link

`hook.SetParam<string>(...)` won't change hook's parameter #352

Open ianlucas opened 8 months ago

ianlucas commented 8 months ago

Consider the sample code below. I expect an AUG to be given instead of an AK-47 whenever an AK-47 is given to the player, however the game still gives the player an AK-47 instead. I wasn't able to find a usage of SetParam passing string, so I'm not sure if this is an existing issue.

public override void Load(bool hotReload)
{
    VirtualFunctions.GiveNamedItemFunc.Hook(hook =>
    {
        var itemServices = hook.GetParam<CCSPlayer_ItemServices>(0);
        var className = hook.GetParam<string>(1);
        var pawn = itemServices.Pawn.Value;
        if (pawn.IsValid && pawn.Controller.IsValid && pawn.Controller.Value != null)
        {
            var player = new CCSPlayerController(pawn.Controller.Value.Handle);
            if (className == "weapon_ak47")
            {
                hook.SetParam<string>(1, "weapon_aug");
                return HookResult.Changed;
            }
        }
        return HookResult.Continue;
    }, HookMode.Pre);
}

Note I'm using HookMode.Pre there.

KillStr3aK commented 8 months ago

DynamicHooks currently have a problem with strings in general, thats why hooking print related functions turns into garbage in the end, even if you change nothing related to any string

ianlucas commented 6 months ago

DynamicHooks currently have a problem with strings in general, thats why hooking print related functions turns into garbage in the end, even if you change nothing related to any string

Just to be clear, is this an issue within DynamicHooks API (C# and/or C++) or DynoHook? I'm trying to understand the current implementation, and so far I don't see where it could be failing.

KillStr3aK commented 6 months ago

DynamicHooks currently have a problem with strings in general, thats why hooking print related functions turns into garbage in the end, even if you change nothing related to any string

Just to be clear, is this an issue within DynamicHooks API (C# and/or C++) or DynoHook? I'm trying to understand the current implementation, and so far I don't see where it could be failing.

DynoHook should be fine, if I'd have to guess its somewhere where the type is set maybe?

I don't really see anything wrong here in general, not sure if it causes any anomaly that we interpret strings and pointers differently

KillStr3aK commented 6 months ago

we would have to debug it more for information