~ruther/NosSmooth.Local

ref: be80295a2ec212f85bc4bae367fee2e45349bf6d NosSmooth.Local/src/Core/NosSmooth.LocalBinding/Hooks/Implementations/PetWalkHook.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
76
77
78
79
80
81
82
83
//
//  PetWalkHook.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.PetWalk.
/// </summary>
internal class PetWalkHook : CancelableNostaleHook<IPetWalkHook.PetWalkDelegate,
    IPetWalkHook.PetWalkWrapperDelegate, PetWalkEventArgs>, IPetWalkHook
{
    /// <summary>
    /// Create the packet send hook.
    /// </summary>
    /// <param name="bindingManager">The binding manager.</param>
    /// <param name="options">The options for the binding.</param>
    /// <returns>A packet send hook or an error.</returns>
    public static Result<PetWalkHook> Create
        (NosBindingManager bindingManager, HookOptions<IPetWalkHook> options)
    {
        var hook = CreateHook
        (
            bindingManager,
            () => new PetWalkHook(bindingManager.Memory),
            hook => hook.Detour,
            options
        );

        return hook;
    }

    private IMemory _memory;

    private PetWalkHook(IMemory memory)
    {
        _memory = memory;
    }

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

    /// <inheritdoc />
    public override IPetWalkHook.PetWalkWrapperDelegate WrapperFunction
        => (p, x, y) => OriginalFunction(p.Address, ((int)x << 16) | (int)y) == 1;

    /// <inheritdoc />
    protected override IPetWalkHook.PetWalkDelegate WrapWithCalling(IPetWalkHook.PetWalkDelegate function)
        =>
            (
                petManagerPtr,
                position,
                un0,
                un1
            ) =>
            {
                CallingFromNosSmooth = true;
                var res = function(petManagerPtr, position, un0, un1);
                CallingFromNosSmooth = false;
                return res;
            };

    private nuint Detour
    (
        nuint petManagerPtr,
        int position,
        short un0,
        int un1
    )
    {
        var petManager = new PetManager(_memory, petManagerPtr);
        var walkArgs = new PetWalkEventArgs
            (petManager, (ushort)((position >> 16) & 0xFFFF), (ushort)(position & 0xFFFF));
        return HandleCall(walkArgs);
    }
}
Do not follow this link