// // 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; /// internal class PeriodicHook : IPeriodicHook { /// /// Create the periodic hook. /// /// The binding manager. /// The options for the binding. /// A packet send hook or an error. public static Result Create(NosBindingManager bindingManager, HookOptions options) { var periodicHook = new PeriodicHook(); var hookResult = bindingManager.CreateCustomAsmHookFromPattern (periodicHook.Name, periodicHook.Detour, options, false); if (!hookResult.IsDefined(out var hook)) { return Result.FromError(hookResult); } periodicHook._hook = hook; return periodicHook; } private PeriodicHook() { } private NosAsmHook _hook = null!; /// public string Name => IHookManager.PeriodicName; /// public bool IsEnabled => _hook.Hook.IsEnabled; /// public IPeriodicHook.PeriodicDelegate WrapperFunction => OriginalFunction; /// public IPeriodicHook.PeriodicDelegate OriginalFunction => throw new InvalidOperationException ("Calling NosTale periodic function from NosSmooth is not allowed."); /// public event EventHandler? Called; /// public Result Enable() { _hook.Hook.EnableOrActivate(); return Result.FromSuccess(); } /// public Result Disable() { _hook.Hook.Disable(); return Result.FromSuccess(); } private void Detour() { Called?.Invoke(this, System.EventArgs.Empty); } }