~ruther/NosSmooth.Local

ref: be80295a2ec212f85bc4bae367fee2e45349bf6d NosSmooth.Local/src/Core/NosSmooth.LocalBinding/Hooks/Implementations/EntityFocusHook.cs -rw-r--r-- 2.4 KiB
be80295a — Rutherther feat(binding): split hooks to individual classes, make a hook manager 2 years ago
                                                                                
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
73
74
75
//
//  EntityFocusHook.cs
//
//  Copyright (c) František Boháček. All rights reserved.
//  Licensed under the MIT license. See LICENSE file in the project root for full license information.

using NosSmooth.LocalBinding.EventArgs;
using NosSmooth.LocalBinding.Structs;
using Reloaded.Memory.Sources;
using Remora.Results;

namespace NosSmooth.LocalBinding.Hooks.Implementations;

/// <summary>
/// A hook of NetworkManager.EntityFocus.
/// </summary>
internal class EntityFocusHook : CancelableNostaleHook<IEntityFocusHook.EntityFocusDelegate,
    IEntityFocusHook.EntityFocusWrapperDelegate, EntityEventArgs>, IEntityFocusHook
{
    /// <summary>
    /// Create the packet send hook.
    /// </summary>
    /// <param name="bindingManager">The binding manager.</param>
    /// <param name="browserManager">The browser manager.</param>
    /// <param name="options">The options for the binding.</param>
    /// <returns>A packet send hook or an error.</returns>
    public static Result<EntityFocusHook> Create
        (NosBindingManager bindingManager, NosBrowserManager browserManager, HookOptions<IEntityFocusHook> options)
    {
        var hook = CreateHook
        (
            bindingManager,
            () => new EntityFocusHook(browserManager.UnitManager),
            hook => hook.Detour,
            options
        );

        return hook;
    }

    private readonly UnitManager _unitManager;

    private EntityFocusHook(UnitManager unitManager)
    {
        _unitManager = unitManager;
    }

    /// <inheritdoc />
    public override string Name => IHookManager.EntityFocusName;

    /// <inheritdoc />
    public override IEntityFocusHook.EntityFocusWrapperDelegate WrapperFunction => (entity) => OriginalFunction
        (_unitManager.Address, entity?.Address ?? 0);

    /// <inheritdoc />
    protected override IEntityFocusHook.EntityFocusDelegate WrapWithCalling(IEntityFocusHook.EntityFocusDelegate function)
        => (unitManagerPtr, entityPtr) =>
            {
                CallingFromNosSmooth = true;
                var res = function(unitManagerPtr, entityPtr);
                CallingFromNosSmooth = false;
                return res;
            };

    private nuint Detour
    (
        nuint unitManagerPtr,
        nuint entityPtr
    )
    {
        var entity = new MapBaseObj(_unitManager.Memory, entityPtr);
        var entityArgs = new EntityEventArgs(entity);
        return HandleCall(entityArgs);
    }
}
Do not follow this link