From 77a89a28517390d95e111cd86e0fb151410ce6fe Mon Sep 17 00:00:00 2001 From: Rutherther Date: Mon, 2 Jan 2023 00:50:00 +0100 Subject: [PATCH] feat: use synchronizer for NosTale calls --- .../Attack/AttackCommandHandler.cs | 13 ++++++-- .../NostaleLocalClient.cs | 24 ++++++++++++-- .../SimplePiiBot/Commands/EntityCommands.cs | 33 ++++++++++++++----- 3 files changed, 57 insertions(+), 13 deletions(-) diff --git a/src/Core/NosSmooth.LocalClient/CommandHandlers/Attack/AttackCommandHandler.cs b/src/Core/NosSmooth.LocalClient/CommandHandlers/Attack/AttackCommandHandler.cs index 44b92c2..322afb4 100644 --- a/src/Core/NosSmooth.LocalClient/CommandHandlers/Attack/AttackCommandHandler.cs +++ b/src/Core/NosSmooth.LocalClient/CommandHandlers/Attack/AttackCommandHandler.cs @@ -9,6 +9,7 @@ using NosSmooth.Core.Commands; using NosSmooth.Core.Commands.Attack; using NosSmooth.Core.Commands.Control; using NosSmooth.Core.Extensions; +using NosSmooth.LocalBinding; using NosSmooth.LocalBinding.Objects; using NosSmooth.LocalBinding.Structs; using Remora.Results; @@ -21,6 +22,7 @@ namespace NosSmooth.LocalClient.CommandHandlers.Attack; public class AttackCommandHandler : ICommandHandler { private readonly INostaleClient _nostaleClient; + private readonly NosThreadSynchronizer _synchronizer; private readonly UnitManagerBinding _unitManagerBinding; private readonly SceneManager _sceneManager; @@ -28,12 +30,19 @@ public class AttackCommandHandler : ICommandHandler /// Initializes a new instance of the class. /// /// The NosTale client. + /// The thread synchronizer. /// The unit manager binding. /// The scene manager. public AttackCommandHandler - (INostaleClient nostaleClient, UnitManagerBinding unitManagerBinding, SceneManager sceneManager) + ( + INostaleClient nostaleClient, + NosThreadSynchronizer synchronizer, + UnitManagerBinding unitManagerBinding, + SceneManager sceneManager + ) { _nostaleClient = nostaleClient; + _synchronizer = synchronizer; _unitManagerBinding = unitManagerBinding; _sceneManager = sceneManager; } @@ -46,7 +55,7 @@ public class AttackCommandHandler : ICommandHandler var entityResult = _sceneManager.FindEntity(command.TargetId.Value); if (entityResult.IsDefined(out var entity)) { - _unitManagerBinding.FocusEntity(entity); + _synchronizer.EnqueueOperation(() => _unitManagerBinding.FocusEntity(entity)); } } diff --git a/src/Core/NosSmooth.LocalClient/NostaleLocalClient.cs b/src/Core/NosSmooth.LocalClient/NostaleLocalClient.cs index 805abe1..d96633a 100644 --- a/src/Core/NosSmooth.LocalClient/NostaleLocalClient.cs +++ b/src/Core/NosSmooth.LocalClient/NostaleLocalClient.cs @@ -12,6 +12,7 @@ using NosSmooth.Core.Commands; using NosSmooth.Core.Commands.Control; using NosSmooth.Core.Extensions; using NosSmooth.Core.Packets; +using NosSmooth.LocalBinding; using NosSmooth.LocalBinding.Objects; using NosSmooth.LocalBinding.Structs; using NosSmooth.Packets; @@ -31,6 +32,7 @@ namespace NosSmooth.LocalClient; /// public class NostaleLocalClient : BaseNostaleClient { + private readonly NosThreadSynchronizer _synchronizer; private readonly NetworkBinding _networkBinding; private readonly PlayerManagerBinding _playerManagerBinding; private readonly PetManagerBinding _petManagerBinding; @@ -47,6 +49,7 @@ public class NostaleLocalClient : BaseNostaleClient /// /// Initializes a new instance of the class. /// + /// The thread synchronizer. /// The network binding. /// The player manager binding. /// The pet manager binding. @@ -60,6 +63,7 @@ public class NostaleLocalClient : BaseNostaleClient /// The dependency injection provider. public NostaleLocalClient ( + NosThreadSynchronizer synchronizer, NetworkBinding networkBinding, PlayerManagerBinding playerManagerBinding, PetManagerBinding petManagerBinding, @@ -75,6 +79,7 @@ public class NostaleLocalClient : BaseNostaleClient : base(commandProcessor, packetSerializer) { _options = options.Value; + _synchronizer = synchronizer; _networkBinding = networkBinding; _playerManagerBinding = playerManagerBinding; _petManagerBinding = petManagerBinding; @@ -91,6 +96,7 @@ public class NostaleLocalClient : BaseNostaleClient { _stopRequested = stopRequested; _logger.LogInformation("Starting local client"); + _synchronizer.StartSynchronizer(); _networkBinding.PacketSend += SendCallback; _networkBinding.PacketReceive += ReceiveCallback; @@ -170,13 +176,19 @@ public class NostaleLocalClient : BaseNostaleClient private void SendPacket(string packetString) { - _networkBinding.SendPacket(packetString); + _synchronizer.EnqueueOperation + ( + () => _networkBinding.SendPacket(packetString) + ); _logger.LogDebug($"Sending client packet {packetString}"); } private void ReceivePacket(string packetString) { - _networkBinding.ReceivePacket(packetString); + _synchronizer.EnqueueOperation + ( + () => _networkBinding.ReceivePacket(packetString) + ); _logger.LogDebug($"Receiving client packet {packetString}"); } @@ -205,7 +217,13 @@ public class NostaleLocalClient : BaseNostaleClient } Result result = await _packetHandler.HandlePacketAsync - (this, type, packet, packetString, _stopRequested ?? default); + ( + this, + type, + packet, + packetString, + _stopRequested ?? default + ); if (!result.IsSuccess) { diff --git a/src/Samples/HighLevel/SimplePiiBot/Commands/EntityCommands.cs b/src/Samples/HighLevel/SimplePiiBot/Commands/EntityCommands.cs index 47fa1d2..768267c 100644 --- a/src/Samples/HighLevel/SimplePiiBot/Commands/EntityCommands.cs +++ b/src/Samples/HighLevel/SimplePiiBot/Commands/EntityCommands.cs @@ -7,6 +7,7 @@ using NosSmooth.ChatCommands; using NosSmooth.Extensions.Combat.Errors; using NosSmooth.Game; +using NosSmooth.LocalBinding; using NosSmooth.LocalBinding.Objects; using NosSmooth.LocalBinding.Structs; using Remora.Commands.Attributes; @@ -21,6 +22,7 @@ namespace SimplePiiBot.Commands; public class EntityCommands : CommandGroup { private readonly Game _game; + private readonly NosThreadSynchronizer _synchronizer; private readonly UnitManagerBinding _unitManagerBinding; private readonly SceneManager _sceneManager; private readonly PlayerManagerBinding _playerManagerBinding; @@ -30,6 +32,7 @@ public class EntityCommands : CommandGroup /// Initializes a new instance of the class. /// /// The game. + /// The thread synchronizer. /// The scene manager binding. /// The scene manager. /// The character binding. @@ -37,6 +40,7 @@ public class EntityCommands : CommandGroup public EntityCommands ( Game game, + NosThreadSynchronizer synchronizer, UnitManagerBinding unitManagerBinding, SceneManager sceneManager, PlayerManagerBinding playerManagerBinding, @@ -44,6 +48,7 @@ public class EntityCommands : CommandGroup ) { _game = game; + _synchronizer = synchronizer; _unitManagerBinding = unitManagerBinding; _sceneManager = sceneManager; _playerManagerBinding = playerManagerBinding; @@ -100,15 +105,19 @@ public class EntityCommands : CommandGroup /// The entity id to focus. /// A task that may or may not have succeeded. [Command("focus")] - public Task HandleFocusAsync(int entityId) + public async Task HandleFocusAsync(int entityId) { var entityResult = _sceneManager.FindEntity(entityId); if (!entityResult.IsSuccess) { - return Task.FromResult(Result.FromError(entityResult)); + return Result.FromError(entityResult); } - return Task.FromResult(_unitManagerBinding.FocusEntity(entityResult.Entity)); + return await _synchronizer.SynchronizeAsync + ( + () => _unitManagerBinding.FocusEntity(entityResult.Entity), + CancellationToken + ); } /// @@ -117,15 +126,19 @@ public class EntityCommands : CommandGroup /// The entity id to follow. /// A task that may or may not have succeeded. [Command("follow")] - public Task HandleFollowAsync(int entityId) + public async Task HandleFollowAsync(int entityId) { var entityResult = _sceneManager.FindEntity(entityId); if (!entityResult.IsSuccess) { - return Task.FromResult(Result.FromError(entityResult)); + return Result.FromError(entityResult); } - return Task.FromResult(_playerManagerBinding.FollowEntity(entityResult.Entity)); + return await _synchronizer.SynchronizeAsync + ( + () => _playerManagerBinding.FollowEntity(entityResult.Entity), + CancellationToken + ); } /// @@ -133,8 +146,12 @@ public class EntityCommands : CommandGroup /// /// A task that may or may not have succeeded. [Command("unfollow")] - public Task HandleUnfollowAsync() + public async Task HandleUnfollowAsync() { - return Task.FromResult(_playerManagerBinding.UnfollowEntity()); + return await _synchronizer.SynchronizeAsync + ( + () => _playerManagerBinding.UnfollowEntity(), + CancellationToken + ); } } \ No newline at end of file -- 2.49.0