~ruther/NosSmooth.Local

ref: c0c354f151614939fa28d2d770ff75efc0c1e5c9 NosSmooth.Local/src/Core/NosSmooth.LocalBinding/Hooks/Implementations/EntityFocusHook.cs -rw-r--r-- 2.6 KiB
c0c354f1 — Rutherther ci: remove --output from dotnet pack, put output directory to Directory.Build.props 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
76
77
78
79
//
//  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.Memory, browserManager.UnitManager),
            hook => hook.Detour,
            options
        );

        return hook;
    }

    private readonly Optional<UnitManager> _unitManager;
    private readonly IMemory _memory;

    private EntityFocusHook(IMemory memory, Optional<UnitManager> unitManager)
    {
        _memory = memory;
        _unitManager = unitManager;
    }

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

    /// <inheritdoc />
    public override Optional<IEntityFocusHook.EntityFocusWrapperDelegate> WrapperFunction
        => _unitManager.Map<IEntityFocusHook.EntityFocusWrapperDelegate>
            (unitManager => 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(_memory, entityPtr);
        var entityArgs = new EntityEventArgs(entity);
        return HandleCall(entityArgs);
    }
}
Do not follow this link