From 45f8bc92fd853ae7008cf52c153aa71368f11f32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Boh=C3=A1=C4=8Dek?= Date: Sat, 15 Jan 2022 20:10:55 +0100 Subject: [PATCH] feat: use nostale bindings instead of nossmooth core --- Core/NosSmooth.Core/Commands/WalkCommand.cs | 2 +- .../Walk/WalkCommandHandler.cs | 19 ++--- .../Walk/WalkCommandHandlerOptions.cs | 6 -- .../CommandHandlers/Walk/WalkStatus.cs | 14 ++-- .../Extensions/ServiceCollectionExtensions.cs | 7 +- .../Hooks/NostaleHookManager.cs | 74 ------------------- .../Hooks/WalkEventArgs.cs | 34 --------- .../LocalClientOptions.cs | 26 ------- .../NosSmooth.LocalClient.csproj | 9 ++- .../NostaleLocalClient.cs | 45 +++-------- Samples/WalkCommands/Commands/WalkCommands.cs | 4 +- 11 files changed, 40 insertions(+), 200 deletions(-) delete mode 100644 Local/NosSmooth.LocalClient/Hooks/NostaleHookManager.cs delete mode 100644 Local/NosSmooth.LocalClient/Hooks/WalkEventArgs.cs diff --git a/Core/NosSmooth.Core/Commands/WalkCommand.cs b/Core/NosSmooth.Core/Commands/WalkCommand.cs index 7fa2499d3f09430c64c50238b7e78d465e5cb536..5c7f7cea1f9f335ec7a0f08748675b4818d42954 100644 --- a/Core/NosSmooth.Core/Commands/WalkCommand.cs +++ b/Core/NosSmooth.Core/Commands/WalkCommand.cs @@ -13,4 +13,4 @@ namespace NosSmooth.Core.Commands; /// The x coordinate of the target position to move to. /// The y coordinate of the target position to move to. /// Whether to cancel the walk when the user clicks to move somewhere. -public record WalkCommand(int TargetX, int TargetY, bool CancelOnUserMove = true) : ICommand; \ No newline at end of file +public record WalkCommand(ushort TargetX, ushort TargetY, bool CancelOnUserMove = true) : ICommand; \ No newline at end of file diff --git a/Local/NosSmooth.LocalClient/CommandHandlers/Walk/WalkCommandHandler.cs b/Local/NosSmooth.LocalClient/CommandHandlers/Walk/WalkCommandHandler.cs index a57c4eab998ae158d017fddb2f99635811861783..dcc6356f9601334183b9cabb934179680cca9e29 100644 --- a/Local/NosSmooth.LocalClient/CommandHandlers/Walk/WalkCommandHandler.cs +++ b/Local/NosSmooth.LocalClient/CommandHandlers/Walk/WalkCommandHandler.cs @@ -6,8 +6,8 @@ using Microsoft.Extensions.Options; using NosSmooth.Core.Commands; +using NosSmooth.LocalBinding.Objects; using NosSmooth.LocalClient.CommandHandlers.Walk.Errors; -using NosSmoothCore; using Remora.Results; namespace NosSmooth.LocalClient.CommandHandlers.Walk; @@ -17,20 +17,20 @@ namespace NosSmooth.LocalClient.CommandHandlers.Walk; /// public class WalkCommandHandler : ICommandHandler { - private readonly NosClient _nosClient; + private readonly CharacterBinding _characterBinding; private readonly WalkStatus _walkStatus; private readonly WalkCommandHandlerOptions _options; /// /// Initializes a new instance of the class. /// - /// The local client. + /// The character object binding. /// The walk status. /// The options. - public WalkCommandHandler(NosClient nosClient, WalkStatus walkStatus, IOptions options) + public WalkCommandHandler(CharacterBinding characterBinding, WalkStatus walkStatus, IOptions options) { _options = options.Value; - _nosClient = nosClient; + _characterBinding = characterBinding; _walkStatus = walkStatus; } @@ -46,11 +46,8 @@ public class WalkCommandHandler : ICommandHandler await _walkStatus.SetWalking(linked, command.TargetX, command.TargetY, command.CancelOnUserMove); while (!ct.IsCancellationRequested) { - try - { - _nosClient.GetCharacter().Walk(command.TargetX, command.TargetY); - } - catch (Exception e) + var walkResult = _characterBinding.Walk(command.TargetX, command.TargetY); + if (!walkResult.IsSuccess) { try { @@ -61,7 +58,7 @@ public class WalkCommandHandler : ICommandHandler // ignored, just for cancellation } - return e; + return walkResult; } try diff --git a/Local/NosSmooth.LocalClient/CommandHandlers/Walk/WalkCommandHandlerOptions.cs b/Local/NosSmooth.LocalClient/CommandHandlers/Walk/WalkCommandHandlerOptions.cs index 03070db11bb3ebef6973adbc19c5c93feb09f0ef..723f0499fa0517a0a7cd269d5b6a7a4c56e0e80e 100644 --- a/Local/NosSmooth.LocalClient/CommandHandlers/Walk/WalkCommandHandlerOptions.cs +++ b/Local/NosSmooth.LocalClient/CommandHandlers/Walk/WalkCommandHandlerOptions.cs @@ -4,12 +4,6 @@ // 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; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - namespace NosSmooth.LocalClient.CommandHandlers.Walk { /// diff --git a/Local/NosSmooth.LocalClient/CommandHandlers/Walk/WalkStatus.cs b/Local/NosSmooth.LocalClient/CommandHandlers/Walk/WalkStatus.cs index 9f24cd63dadda8b8c14c83ee83d4cda64a2b7f33..0d72ba0941a3d6cbe92dd6b888b4d7d3e258c045 100644 --- a/Local/NosSmooth.LocalClient/CommandHandlers/Walk/WalkStatus.cs +++ b/Local/NosSmooth.LocalClient/CommandHandlers/Walk/WalkStatus.cs @@ -4,7 +4,7 @@ // 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.LocalClient.Hooks; +using NosSmooth.LocalBinding.Objects; namespace NosSmooth.LocalClient.CommandHandlers.Walk; @@ -13,7 +13,7 @@ namespace NosSmooth.LocalClient.CommandHandlers.Walk; /// public class WalkStatus { - private readonly NostaleHookManager _hookManager; + private readonly CharacterBinding _characterBinding; private readonly SemaphoreSlim _semaphore; private CancellationTokenSource? _walkingCancellation; private bool _userCanCancel; @@ -22,10 +22,10 @@ public class WalkStatus /// /// Initializes a new instance of the class. /// - /// The hooking manager. - public WalkStatus(NostaleHookManager hookManager) + /// The character binding. + public WalkStatus(CharacterBinding characterBinding) { - _hookManager = hookManager; + _characterBinding = characterBinding; _semaphore = new SemaphoreSlim(1, 1); } @@ -110,7 +110,7 @@ public class WalkStatus if (!_walkHooked) { - _hookManager.ClientWalked += OnCharacterWalked; + _characterBinding.WalkCall += OnCharacterWalked; _walkHooked = true; } @@ -159,7 +159,7 @@ public class WalkStatus await CancelWalkingAsync(ct: ct); } - private bool OnCharacterWalked(WalkEventArgs walkEventArgs) + private bool OnCharacterWalked(ushort x, ushort y) { if (IsWalking) { diff --git a/Local/NosSmooth.LocalClient/Extensions/ServiceCollectionExtensions.cs b/Local/NosSmooth.LocalClient/Extensions/ServiceCollectionExtensions.cs index 6b644096ebd0929f13af1cab45921cedca86cabc..c8e1fbb16349fbd96fa77bf0e33588ffa119e6e4 100644 --- a/Local/NosSmooth.LocalClient/Extensions/ServiceCollectionExtensions.cs +++ b/Local/NosSmooth.LocalClient/Extensions/ServiceCollectionExtensions.cs @@ -8,10 +8,8 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using NosSmooth.Core.Client; using NosSmooth.Core.Extensions; -using NosSmooth.LocalClient.CommandHandlers; +using NosSmooth.LocalBinding.Extensions; using NosSmooth.LocalClient.CommandHandlers.Walk; -using NosSmooth.LocalClient.Hooks; -using NosSmoothCore; namespace NosSmooth.LocalClient.Extensions; @@ -28,13 +26,12 @@ public static class ServiceCollectionExtensions public static IServiceCollection AddLocalClient(this IServiceCollection serviceCollection) { serviceCollection.AddNostaleCore(); + serviceCollection.AddNostaleBindings(); serviceCollection .AddCommandHandler() .AddPacketResponder() .AddSingleton(); serviceCollection.TryAddSingleton(); - serviceCollection.TryAddSingleton(); - serviceCollection.TryAddSingleton(); serviceCollection.TryAddSingleton(p => p.GetRequiredService()); return serviceCollection; diff --git a/Local/NosSmooth.LocalClient/Hooks/NostaleHookManager.cs b/Local/NosSmooth.LocalClient/Hooks/NostaleHookManager.cs deleted file mode 100644 index bc717ef6ce447328ab76abdc591d7a69952942f0..0000000000000000000000000000000000000000 --- a/Local/NosSmooth.LocalClient/Hooks/NostaleHookManager.cs +++ /dev/null @@ -1,74 +0,0 @@ -// -// 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 deleted file mode 100644 index 938e385c2311bfb6b377e98a325fc73b69fa75f6..0000000000000000000000000000000000000000 --- a/Local/NosSmooth.LocalClient/Hooks/WalkEventArgs.cs +++ /dev/null @@ -1,34 +0,0 @@ -// -// 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 diff --git a/Local/NosSmooth.LocalClient/LocalClientOptions.cs b/Local/NosSmooth.LocalClient/LocalClientOptions.cs index 7da0f3efaa9157fca1ca967670edac3e9162cf7f..cacf7cbee3f7737d39b7a34c8d4ffe4e2d7a5b92 100644 --- a/Local/NosSmooth.LocalClient/LocalClientOptions.cs +++ b/Local/NosSmooth.LocalClient/LocalClientOptions.cs @@ -4,8 +4,6 @@ // 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.Core.Commands; - namespace NosSmooth.LocalClient; /// @@ -17,28 +15,4 @@ public class LocalClientOptions /// Gets or sets whether the interception of packets should be allowed. /// public bool AllowIntercept { get; set; } - - /// - /// Hook the packet sent method. - /// - /// - /// Packet handlers and interceptors won't be called for sent packets. - /// - public bool HookPacketSend { get; set; } = true; - - /// - /// Hook the packet received method. - /// - /// - /// Packet handlers and interceptors won't be called for received packets. - /// - public bool HookPacketReceive { get; set; } = true; - - /// - /// Whether to hook Character.Walk method. True by default. - /// - /// - /// If set to false, won't take any effect. - /// - public bool HookCharacterWalk { get; set; } = true; } \ No newline at end of file diff --git a/Local/NosSmooth.LocalClient/NosSmooth.LocalClient.csproj b/Local/NosSmooth.LocalClient/NosSmooth.LocalClient.csproj index a18ad0632d759c150f37ee0ce2e022ef5863e4d8..62e41be6f46596ea4fcb25058ce3808b1d4c36be 100644 --- a/Local/NosSmooth.LocalClient/NosSmooth.LocalClient.csproj +++ b/Local/NosSmooth.LocalClient/NosSmooth.LocalClient.csproj @@ -5,15 +5,22 @@ enable 10 net48 + true - + + + + + + + diff --git a/Local/NosSmooth.LocalClient/NostaleLocalClient.cs b/Local/NosSmooth.LocalClient/NostaleLocalClient.cs index 7b26c4f44dd656d232ef629b31c30bbbf2172294..93b7b3de79e217677cf6c08099fd4df8e665fdf0 100644 --- a/Local/NosSmooth.LocalClient/NostaleLocalClient.cs +++ b/Local/NosSmooth.LocalClient/NostaleLocalClient.cs @@ -11,11 +11,10 @@ using NosSmooth.Core.Client; using NosSmooth.Core.Commands; using NosSmooth.Core.Extensions; using NosSmooth.Core.Packets; -using NosSmooth.LocalClient.Hooks; +using NosSmooth.LocalBinding.Objects; using NosSmooth.Packets; using NosSmooth.Packets.Attributes; using NosSmooth.Packets.Errors; -using NosSmoothCore; using Remora.Results; namespace NosSmooth.LocalClient; @@ -29,12 +28,11 @@ namespace NosSmooth.LocalClient; /// public class NostaleLocalClient : BaseNostaleClient { + private readonly NetworkBinding _networkBinding; private readonly IPacketSerializer _packetSerializer; - private readonly NostaleHookManager _hookManager; private readonly IPacketHandler _packetHandler; private readonly ILogger _logger; private readonly IServiceProvider _provider; - private readonly NosClient _client; private readonly LocalClientOptions _options; private CancellationToken? _stopRequested; private IPacketInterceptor? _interceptor; @@ -42,34 +40,31 @@ public class NostaleLocalClient : BaseNostaleClient /// /// Initializes a new instance of the class. /// + /// The network binding. /// The command processor. /// The packet serializer. - /// The hooking manager. /// The packet handler. /// The logger. /// The options for the client. /// The dependency injection provider. - /// The nostale managed client. public NostaleLocalClient ( + NetworkBinding networkBinding, CommandProcessor commandProcessor, IPacketSerializer packetSerializer, - NostaleHookManager hookManager, IPacketHandler packetHandler, ILogger logger, IOptions options, - IServiceProvider provider, - NosClient client + IServiceProvider provider ) : base(commandProcessor, packetSerializer) { _options = options.Value; + _networkBinding = networkBinding; _packetSerializer = packetSerializer; - _hookManager = hookManager; _packetHandler = packetHandler; _logger = logger; _provider = provider; - _client = client; } /// @@ -77,25 +72,8 @@ public class NostaleLocalClient : BaseNostaleClient { _stopRequested = stopRequested; _logger.LogInformation("Starting local client"); - NetworkCallback receiveCallback = ReceiveCallback; - NetworkCallback sendCallback = SendCallback; - - if (_options.HookPacketReceive) - { - _client.GetNetwork().SetReceiveCallback(receiveCallback); - } - - if (_options.HookPacketSend) - { - _client.GetNetwork().SetSendCallback(sendCallback); - } - - if (_options.HookCharacterWalk) - { - _hookManager.HookCharacterWalk(); - } - - _logger.LogInformation("Packet methods hooked successfully"); + _networkBinding.PacketSend += SendCallback; + _networkBinding.PacketReceive += ReceiveCallback; try { @@ -106,7 +84,8 @@ public class NostaleLocalClient : BaseNostaleClient // ignored } - _client.ResetHooks(); + _networkBinding.PacketSend -= SendCallback; + _networkBinding.PacketReceive -= ReceiveCallback; return Result.FromSuccess(); } @@ -163,13 +142,13 @@ public class NostaleLocalClient : BaseNostaleClient private void SendPacket(string packetString) { - _client.GetNetwork().SendPacket(packetString); + _networkBinding.SendPacket(packetString); _logger.LogDebug($"Sending client packet {packetString}"); } private void ReceivePacket(string packetString) { - _client.GetNetwork().ReceivePacket(packetString); + _networkBinding.ReceivePacket(packetString); _logger.LogDebug($"Receiving client packet {packetString}"); } diff --git a/Samples/WalkCommands/Commands/WalkCommands.cs b/Samples/WalkCommands/Commands/WalkCommands.cs index b52ce395e65dfd34713b291ac32e35ada6e7be13..a0f88ee95236ff53135038a6a4aa287c159ae0c8 100644 --- a/Samples/WalkCommands/Commands/WalkCommands.cs +++ b/Samples/WalkCommands/Commands/WalkCommands.cs @@ -39,8 +39,8 @@ public class WalkCommands /// A result that may or may not have succeeded. public async Task HandleWalkToAsync ( - int x, - int y, + ushort x, + ushort y, bool isCancellable = true, CancellationToken ct = default )