~ruther/NosSmooth.Local

922401c8259d81113ce50e2bd46490915865a396 — Rutherther 2 years ago cf76dfc
feat(client): implement support for optional modules and hooks
M src/Core/NosSmooth.LocalClient/CommandHandlers/Attack/AttackCommandHandler.cs => src/Core/NosSmooth.LocalClient/CommandHandlers/Attack/AttackCommandHandler.cs +4 -1
@@ 56,7 56,10 @@ public class AttackCommandHandler : ICommandHandler<AttackCommand>
            var entityResult = _sceneManager.FindEntity(command.TargetId.Value);
            if (entityResult.IsDefined(out var entity))
            {
                _synchronizer.EnqueueOperation(() => _entityFocusHook.WrapperFunction(entity));
                if (_entityFocusHook.WrapperFunction.IsPresent)
                {
                    _synchronizer.EnqueueOperation(() => _entityFocusHook.WrapperFunction.Get()(entity));
                }
            }
        }


M src/Core/NosSmooth.LocalClient/CommandHandlers/Walk/PetWalkCommandHandler.cs => src/Core/NosSmooth.LocalClient/CommandHandlers/Walk/PetWalkCommandHandler.cs +3 -2
@@ 78,8 78,9 @@ public class PetWalkCommandHandler : ICommandHandler<PetWalkCommand>
                (
                    () => _userActionDetector.NotUserAction<Result<bool>>
                    (
                        () => _petWalkHook.WrapperFunction(petManager, (ushort)x, (ushort)y)
                    )
                        () => _petWalkHook.WrapperFunction.Get()(petManager, (ushort)x, (ushort)y)
                    ),
                    ct
                ),
            petManager,
            _options

M src/Core/NosSmooth.LocalClient/NostaleLocalClient.cs => src/Core/NosSmooth.LocalClient/NostaleLocalClient.cs +72 -27
@@ 13,6 13,7 @@ using NosSmooth.Core.Commands.Control;
using NosSmooth.Core.Extensions;
using NosSmooth.Core.Packets;
using NosSmooth.LocalBinding;
using NosSmooth.LocalBinding.Errors;
using NosSmooth.LocalBinding.EventArgs;
using NosSmooth.LocalBinding.Hooks;
using NosSmooth.LocalBinding.Objects;


@@ 85,15 86,26 @@ public class NostaleLocalClient : BaseNostaleClient
    /// <inheritdoc />
    public override async Task<Result> RunAsync(CancellationToken stopRequested = default)
    {
        if (!_hookManager.IsHookLoaded<IPacketSendHook>() || !_hookManager.IsHookLoaded<IPacketReceiveHook>())
        {
            return new NeededModulesNotInitializedError
                ("Client cannot run", IHookManager.PacketSendName, IHookManager.PacketReceiveName);
        }

        _stopRequested = stopRequested;
        _logger.LogInformation("Starting local client");
        _synchronizer.StartSynchronizer();
        _hookManager.PacketSend.Called += SendCallCallback;
        _hookManager.PacketReceive.Called += ReceiveCallCallback;
        var synchronizerResult = _synchronizer.StartSynchronizer();
        if (!synchronizerResult.IsSuccess)
        {
            return synchronizerResult;
        }

        _hookManager.EntityFollow.Called += FollowEntity;
        _hookManager.PlayerWalk.Called += Walk;
        _hookManager.PetWalk.Called += PetWalk;
        _hookManager.PacketSend.Get().Called += SendCallCallback;
        _hookManager.PacketReceive.Get().Called += ReceiveCallCallback;

        _hookManager.EntityFollow.TryDo(follow => follow.Called += FollowEntity);
        _hookManager.PlayerWalk.TryDo(walk => walk.Called += Walk);
        _hookManager.PetWalk.TryDo(walk => walk.Called += PetWalk);

        try
        {


@@ 104,12 116,12 @@ public class NostaleLocalClient : BaseNostaleClient
            // ignored
        }

        _hookManager.PacketSend.Called -= SendCallCallback;
        _hookManager.PacketReceive.Called -= ReceiveCallCallback;
        _hookManager.PacketSend.Get().Called -= SendCallCallback;
        _hookManager.PacketReceive.Get().Called -= ReceiveCallCallback;

        _hookManager.EntityFollow.Called -= FollowEntity;
        _hookManager.PlayerWalk.Called -= Walk;
        _hookManager.PetWalk.Called -= PetWalk;
        _hookManager.EntityFollow.TryDo(follow => follow.Called -= FollowEntity);
        _hookManager.PlayerWalk.TryDo(walk => walk.Called -= Walk);
        _hookManager.PetWalk.TryDo(walk => walk.Called -= PetWalk);

        // the hooks are not needed anymore.
        _hookManager.DisableAll();


@@ 120,17 132,59 @@ public class NostaleLocalClient : BaseNostaleClient
    /// <inheritdoc />
    public override async Task<Result> ReceivePacketAsync(string packetString, CancellationToken ct = default)
    {
        ReceivePacket(packetString);
        await ProcessPacketAsync(PacketSource.Server, packetString);
        return Result.FromSuccess();
        var result = _hookManager.PacketReceive.MapResult
        (
            receive => receive.WrapperFunction.MapResult
            (
                wrapperFunction =>
                {
                    _synchronizer.EnqueueOperation(() => wrapperFunction(packetString));
                    return Result.FromSuccess();
                }
            )
        );

        if (result.IsSuccess)
        {
            _logger.LogDebug($"Receiving client packet {packetString}");
            await ProcessPacketAsync(PacketSource.Server, packetString);
        }
        else
        {
            _logger.LogError("Could not receive packet");
            _logger.LogResultError(result);
        }

        return result;
    }

    /// <inheritdoc />
    public override async Task<Result> SendPacketAsync(string packetString, CancellationToken ct = default)
    {
        SendPacket(packetString);
        await ProcessPacketAsync(PacketSource.Client, packetString);
        return Result.FromSuccess();
        var result = _hookManager.PacketSend.MapResult
        (
            send => send.WrapperFunction.MapResult
            (
                wrapperFunction =>
                {
                    _synchronizer.EnqueueOperation(() => wrapperFunction(packetString));
                    return Result.FromSuccess();
                }
            )
        );

        if (result.IsSuccess)
        {
            _logger.LogDebug($"Sending client packet {packetString}");
            await ProcessPacketAsync(PacketSource.Server, packetString);
        }
        else
        {
            _logger.LogError("Could not send packet");
            _logger.LogResultError(result);
        }

        return result;
    }

    private void ReceiveCallCallback(object? owner, PacketEventArgs packetArgs)


@@ 181,20 235,11 @@ public class NostaleLocalClient : BaseNostaleClient
    {
        _synchronizer.EnqueueOperation
        (
            () => _hookManager.PacketSend.WrapperFunction(packetString)
            () => _hookManager.PacketSend.Get().WrapperFunction.Get()(packetString)
        );
        _logger.LogDebug($"Sending client packet {packetString}");
    }

    private void ReceivePacket(string packetString)
    {
        _synchronizer.EnqueueOperation
        (
            () => _hookManager.PacketReceive.WrapperFunction(packetString)
        );
        _logger.LogDebug($"Receiving client packet {packetString}");
    }

    private async Task ProcessPacketAsync(PacketSource type, string packetString)
    {
        try

M src/Core/NosSmooth.LocalClient/UserActionDetector.cs => src/Core/NosSmooth.LocalClient/UserActionDetector.cs +2 -2
@@ 106,7 106,7 @@ public class UserActionDetector
            () =>
            {
                _lastWalkPosition = ((ushort)x, (ushort)y);
                return walkHook.WrapperFunction((ushort)x, (ushort)y);
                return walkHook.WrapperFunction.MapResult(func => func((ushort)x, (ushort)y));
            }
        );



@@ 124,7 124,7 @@ public class UserActionDetector
            () =>
            {
                _lastWalkPosition = ((ushort)x, (ushort)y);
                return walkHook.WrapperFunction((ushort)x, (ushort)y);
                return walkHook.WrapperFunction.MapResult(func => func((ushort)x, (ushort)y));
            },
            ct
        );

Do not follow this link