~ruther/NosSmooth

13608f340e507f8c6f3656f60dfbf6f372c0c62e — František Boháček 3 years ago e4b2ea9
feat: add hook manager for walk function
A Local/NosSmooth.LocalClient/Hooks/NostaleHookManager.cs => Local/NosSmooth.LocalClient/Hooks/NostaleHookManager.cs +74 -0
@@ 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;

/// <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;
    }
}
\ No newline at end of file

A Local/NosSmooth.LocalClient/Hooks/WalkEventArgs.cs => Local/NosSmooth.LocalClient/Hooks/WalkEventArgs.cs +34 -0
@@ 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;

/// <summary>
/// The event args for event in <see cref="NostaleHookManager"/>.
/// </summary>
public class WalkEventArgs
{
    /// <summary>
    /// Initializes a new instance of the <see cref="WalkEventArgs"/> class.
    /// </summary>
    /// <param name="targetX">The target x coordinate.</param>
    /// <param name="targetY">The target y coordinate.</param>
    public WalkEventArgs(int targetX, int targetY)
    {
        TargetX = targetX;
        TargetY = targetY;
    }

    /// <summary>
    /// Gets the target x coordinate.
    /// </summary>
    public int TargetX { get; }

    /// <summary>
    /// Gets the target y coordinate.
    /// </summary>
    public int TargetY { get; }
}
\ No newline at end of file

Do not follow this link