~ruther/NosSmooth.Local

ref: d5b3c3ffb40aa86f582a0f26fc0376caa245f220 NosSmooth.Local/src/Extensions/NosSmooth.Extensions.SharedBinding/Hooks/SharedHookManager.cs -rw-r--r-- 5.7 KiB
d5b3c3ff — Rutherther Merge pull request #18 from Rutherther/feat/optional-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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//
//  SharedHookManager.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.Extensions.SharedBinding.Hooks.Specific;
using NosSmooth.LocalBinding;
using NosSmooth.LocalBinding.Hooks;
using NosSmooth.LocalBinding.Options;
using Remora.Results;

namespace NosSmooth.Extensions.SharedBinding.Hooks;

/// <summary>
/// A hook manager managing <see cref="SingleHookManager"/>s of all of the instances.
/// </summary>
public class SharedHookManager
{
    private readonly IHookManager _underlyingManager;

    private bool _initialized;
    private Dictionary<string, int> _hookedCount;

    /// <summary>
    /// Initializes a new instance of the <see cref="SharedHookManager"/> class.
    /// </summary>
    /// <param name="underlyingManager">The underlying hook manager.</param>
    public SharedHookManager
    (
        IHookManager underlyingManager
    )
    {
        _hookedCount = new Dictionary<string, int>();
        _underlyingManager = underlyingManager;
    }

    /// <summary>
    /// Initialize a shared NosSmooth instance.
    /// </summary>
    /// <param name="bindingManager">The binding manager.</param>
    /// <param name="browserManager">The browser manager.</param>
    /// <param name="options">The initial options to be respected.</param>
    /// <returns>The dictionary containing all of the hooks.</returns>
    public (Dictionary<string, INostaleHook>, IResult) InitializeInstance
        (NosBindingManager bindingManager, NosBrowserManager browserManager, HookManagerOptions options)
    {
        IResult result = Result.FromSuccess();
        if (!_initialized)
        {
            result = _underlyingManager.Initialize(bindingManager, browserManager);
            _initialized = true;
        }

        var hooks = new Dictionary<string, INostaleHook>();

        // TODO: initialize using reflection
        HandleAdd
        (
            hooks,
            IHookManager.PeriodicName,
            InitializeSingleHook
            (
                _underlyingManager.Periodic,
                u => new PeriodicHook(u),
                options.PeriodicHook
            )
        );

        HandleAdd
        (
            hooks,
            IHookManager.EntityFocusName,
            InitializeSingleHook
            (
                _underlyingManager.EntityFocus,
                u => new EntityFocusHook(u),
                options.EntityFocusHook
            )
        );

        HandleAdd
        (
            hooks,
            IHookManager.EntityFollowName,
            InitializeSingleHook
            (
                _underlyingManager.EntityFollow,
                u => new EntityFollowHook(u),
                options.EntityFollowHook
            )
        );

        HandleAdd
        (
            hooks,
            IHookManager.EntityUnfollowName,
            InitializeSingleHook
            (
                _underlyingManager.EntityUnfollow,
                u => new EntityUnfollowHook(u),
                options.EntityUnfollowHook
            )
        );

        HandleAdd
        (
            hooks,
            IHookManager.PacketReceiveName,
            InitializeSingleHook
            (
                _underlyingManager.PacketReceive,
                u => new PacketReceiveHook(u),
                options.PacketReceiveHook
            )
        );

        HandleAdd
        (
            hooks,
            IHookManager.PacketSendName,
            InitializeSingleHook
            (
                _underlyingManager.PacketSend,
                u => new PacketSendHook(u),
                options.PacketSendHook
            )
        );

        HandleAdd
        (
            hooks,
            IHookManager.PetWalkName,
            InitializeSingleHook
            (
                _underlyingManager.PetWalk,
                u => new PetWalkHook(u),
                options.PetWalkHook
            )
        );

        HandleAdd
        (
            hooks,
            IHookManager.CharacterWalkName,
            InitializeSingleHook
            (
                _underlyingManager.PlayerWalk,
                u => new PlayerWalkHook(u),
                options.PlayerWalkHook
            )
        );

        return (hooks, result);
    }

    private INostaleHook<TFunction, TWrapperFunction, TEventArgs>? InitializeSingleHook<THook, TFunction,
        TWrapperFunction,
        TEventArgs>
    (
        Optional<THook> hookOptional,
        Func<THook, SingleHook<TFunction, TWrapperFunction, TEventArgs>> hookCreator,
        HookOptions options
    )
        where THook : notnull
        where TFunction : Delegate
        where TWrapperFunction : Delegate
        where TEventArgs : System.EventArgs
    {
        if (!hookOptional.TryGet(out var underlyingHook))
        {
            return null;
        }

        var hook = hookCreator(underlyingHook);
        hook.StateChanged += (_, state) =>
        {
            if (!_hookedCount.ContainsKey(hook.Name))
            {
                _hookedCount[hook.Name] = 0;
            }

            _hookedCount[hook.Name] += state.Enabled ? 1 : -1;

            if (state.Enabled)
            {
                _underlyingManager.Enable(new[] { hook.Name });
            }
            else if (_hookedCount[hook.Name] == 0)
            {
                _underlyingManager.Disable(new[] { hook.Name });
            }
        };

        if (options.Hook)
        {
            hook.Enable();
        }

        return hook;
    }

    private void HandleAdd(Dictionary<string, INostaleHook> hooks, string name, INostaleHook? hook)
    {
        if (hook is not null)
        {
            hooks.Add(name, hook);
        }
    }
}
Do not follow this link