-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeaponGuard.cs
More file actions
72 lines (62 loc) · 1.92 KB
/
Copy pathWeaponGuard.cs
File metadata and controls
72 lines (62 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Memory;
using CounterStrikeSharp.API.Modules.Memory.DynamicFunctions;
namespace FunRandomRounds;
internal sealed class WeaponGuard
{
private readonly FunRandomRoundsPlugin _plugin;
private readonly Func<DynamicHook, HookResult> _onCanAcquire;
public WeaponGuard(FunRandomRoundsPlugin plugin)
{
_plugin = plugin;
_onCanAcquire = OnCanAcquire;
}
public void Hook()
{
VirtualFunctions.CCSPlayer_ItemServices_CanAcquireFunc.Hook(_onCanAcquire, HookMode.Pre);
}
public void Unhook()
{
try
{
VirtualFunctions.CCSPlayer_ItemServices_CanAcquireFunc.Unhook(_onCanAcquire, HookMode.Pre);
}
catch
{
// ignored
}
}
private HookResult OnCanAcquire(DynamicHook hook)
{
if (!_plugin.BlocksRestrictedWeapons || _plugin.IsInternalGive)
return HookResult.Continue;
try
{
var item = hook.GetParam<CEconItemView>(1);
var name = DesignerNameFromItem(item);
if (string.IsNullOrEmpty(name) || _plugin.AllowsRestrictedWeapon(name))
return HookResult.Continue;
var method = hook.GetParam<int>(2);
hook.SetReturn(method == 1 ? 9 : 8);
return HookResult.Handled;
}
catch
{
return HookResult.Continue;
}
}
private static string DesignerNameFromItem(CEconItemView item)
{
try
{
var data = VirtualFunctions.GetCSWeaponDataFromKeyFunc.Invoke(-1, item.ItemDefinitionIndex.ToString());
if (data == null)
return string.Empty;
return data.Name ?? string.Empty;
}
catch
{
return string.Empty;
}
}
}