From 13608f340e507f8c6f3656f60dfbf6f372c0c62e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Boh=C3=A1=C4=8Dek?= Date: Thu, 23 Dec 2021 16:05:13 +0100 Subject: [PATCH] feat: add hook manager for walk function --- .../Hooks/NostaleHookManager.cs | 74 +++++++++++++++++++ .../Hooks/WalkEventArgs.cs | 34 +++++++++ 2 files changed, 108 insertions(+) create mode 100644 Local/NosSmooth.LocalClient/Hooks/NostaleHookManager.cs create mode 100644 Local/NosSmooth.LocalClient/Hooks/WalkEventArgs.cs diff --git a/Local/NosSmooth.LocalClient/Hooks/NostaleHookManager.cs b/Local/NosSmooth.LocalClient/Hooks/NostaleHookManager.cs new file mode 100644 index 0000000..d84d938 --- /dev/null +++ b/Local/NosSmooth.LocalClient/Hooks/NostaleHookManager.cs @@ -0,0 +1,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; + +/// +/// The manager for hooking functions. +/// +public class NostaleHookManager +{ + private readonly NosClient _nosClient; + + /// + /// Initializes a new instance of the class. + /// + /// The nostale client. + public NostaleHookManager(NosClient nosClient) + { + _nosClient = nosClient; + } + + /// + /// Event for the character walk function. + /// + public event Func? ClientWalked; + + /// + /// Hook the Character.Walk function. + /// + /// A result that may or may not have succeeded. + public Result HookCharacterWalk() + { + try + { + _nosClient.GetCharacter().SetWalkCallback(Walk); + } + catch (Exception e) + { + return e; + } + + return Result.FromSuccess(); + } + + /// + /// Reset the registered hooks. + /// + /// A result that may or may not have succeeded. + 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; + } +} \ No newline at end of file diff --git a/Local/NosSmooth.LocalClient/Hooks/WalkEventArgs.cs b/Local/NosSmooth.LocalClient/Hooks/WalkEventArgs.cs new file mode 100644 index 0000000..9563909 --- /dev/null +++ b/Local/NosSmooth.LocalClient/Hooks/WalkEventArgs.cs @@ -0,0 +1,34 @@ +// +// WalkEventArgs.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. + +namespace NosSmooth.LocalClient.Hooks; + +/// +/// The event args for event in . +/// +public class WalkEventArgs +{ + /// + /// Initializes a new instance of the class. + /// + /// The target x coordinate. + /// The target y coordinate. + public WalkEventArgs(int targetX, int targetY) + { + TargetX = targetX; + TargetY = targetY; + } + + /// + /// Gets the target x coordinate. + /// + public int TargetX { get; } + + /// + /// Gets the target y coordinate. + /// + public int TargetY { get; } +} \ No newline at end of file -- 2.49.0