~ruther/NosSmooth.Local

ref: 25299b8f7815d3f72b3486f636b67031a68a788c NosSmooth.Local/src/Core/NosSmooth.LocalBinding/Hooks/Implementations/PeriodicHook.cs -rw-r--r-- 2.2 KiB
25299b8f — Rutherther Merge pull request #8 from Rutherther/feat/shared-binding 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
//
//  PeriodicHook.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.Diagnostics;
using NosSmooth.LocalBinding.Extensions;
using Remora.Results;

namespace NosSmooth.LocalBinding.Hooks.Implementations;

/// <inheritdoc />
internal class PeriodicHook : IPeriodicHook
{
    /// <summary>
    /// Create the periodic 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<PeriodicHook> Create(NosBindingManager bindingManager, HookOptions<IPeriodicHook> options)
    {
        var periodicHook = new PeriodicHook();

        var hookResult = bindingManager.CreateCustomAsmHookFromPattern<IPeriodicHook.PeriodicDelegate>
            (periodicHook.Name, periodicHook.Detour, options, false);
        if (!hookResult.IsDefined(out var hook))
        {
            return Result<PeriodicHook>.FromError(hookResult);
        }

        periodicHook._hook = hook;
        return periodicHook;
    }

    private PeriodicHook()
    {
    }

    private NosAsmHook<IPeriodicHook.PeriodicDelegate> _hook = null!;

    /// <inheritdoc />
    public string Name => IHookManager.PeriodicName;

    /// <inheritdoc />
    public bool IsEnabled => _hook.Hook.IsEnabled;

    /// <inheritdoc />
    public IPeriodicHook.PeriodicDelegate WrapperFunction => OriginalFunction;

    /// <inheritdoc/>
    public IPeriodicHook.PeriodicDelegate OriginalFunction => throw new InvalidOperationException
        ("Calling NosTale periodic function from NosSmooth is not allowed.");

    /// <inheritdoc />
    public event EventHandler<System.EventArgs>? Called;

    /// <inheritdoc />
    public Result Enable()
    {
        _hook.Hook.EnableOrActivate();
        return Result.FromSuccess();
    }

    /// <inheritdoc />
    public Result Disable()
    {
        _hook.Hook.Disable();
        return Result.FromSuccess();
    }

    private void Detour()
    {
        Called?.Invoke(this, System.EventArgs.Empty);
    }
}
Do not follow this link