~ruther/NosSmooth

ref: 8dcf58cbe2cee392418cb457c78eddff0bf612b5 NosSmooth/Local/NosSmooth.LocalClient/Hooks/NostaleHookManager.cs -rw-r--r-- 1.8 KiB
8dcf58cb — František Boháček chore: add DI dependency to Tests 3 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
//
//  NostaleHookManager.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 System.ComponentModel;
using NosSmoothCore;
using Remora.Results;

namespace NosSmooth.LocalClient.Hooks;

/// <summary>
/// The manager for hooking functions.
/// </summary>
public class NostaleHookManager
{
    private readonly NosClient _nosClient;

    /// <summary>
    /// Initializes a new instance of the <see cref="NostaleHookManager"/> class.
    /// </summary>
    /// <param name="nosClient">The nostale client.</param>
    public NostaleHookManager(NosClient nosClient)
    {
        _nosClient = nosClient;
    }

    /// <summary>
    /// Event for the character walk function.
    /// </summary>
    public event Func<WalkEventArgs, bool>? ClientWalked;

    /// <summary>
    /// Hook the Character.Walk function.
    /// </summary>
    /// <returns>A result that may or may not have succeeded.</returns>
    public Result HookCharacterWalk()
    {
        try
        {
            _nosClient.GetCharacter().SetWalkCallback(Walk);
        }
        catch (Exception e)
        {
            return e;
        }

        return Result.FromSuccess();
    }

    /// <summary>
    /// Reset the registered hooks.
    /// </summary>
    /// <returns>A result that may or may not have succeeded.</returns>
    public Result ResetHooks()
    {
        try
        {
            _nosClient.ResetHooks();
        }
        catch (Exception e)
        {
            return e;
        }

        return Result.FromSuccess();
    }

    private bool Walk(int position)
    {
        return ClientWalked?.Invoke(new WalkEventArgs(position & 0xFFFF, (int)((position & 0xFFFF0000) >> 16))) ?? true;
    }
}
Do not follow this link