From 00d1846f1e860c5e78e29db0384a1fc58e60b23c Mon Sep 17 00:00:00 2001 From: Rutherther Date: Tue, 14 Feb 2023 21:54:26 +0100 Subject: [PATCH] chore: update dependencies, add support for optional binding and hooks --- .../NosSmooth.Comms.Abstractions.csproj | 4 +- .../NosSmooth.Comms.Core.csproj | 2 +- .../MessageResponders/FocusResponder.cs | 28 ++++++++++---- .../MessageResponders/FollowResponder.cs | 37 +++++++++++++++---- .../MessageResponders/HandshakeResponder.cs | 8 ++-- .../NosSmooth.Comms.Inject.csproj | 10 ++--- .../NosSmooth.Comms.Local.csproj | 4 +- src/Samples/ConsolePacketLogger/Program.cs | 4 +- 8 files changed, 67 insertions(+), 30 deletions(-) diff --git a/src/Core/NosSmooth.Comms.Abstractions/NosSmooth.Comms.Abstractions.csproj b/src/Core/NosSmooth.Comms.Abstractions/NosSmooth.Comms.Abstractions.csproj index e5f4cd6..f344a7a 100644 --- a/src/Core/NosSmooth.Comms.Abstractions/NosSmooth.Comms.Abstractions.csproj +++ b/src/Core/NosSmooth.Comms.Abstractions/NosSmooth.Comms.Abstractions.csproj @@ -11,8 +11,8 @@ - - + + diff --git a/src/Core/NosSmooth.Comms.Core/NosSmooth.Comms.Core.csproj b/src/Core/NosSmooth.Comms.Core/NosSmooth.Comms.Core.csproj index 874a9a2..703d1e7 100644 --- a/src/Core/NosSmooth.Comms.Core/NosSmooth.Comms.Core.csproj +++ b/src/Core/NosSmooth.Comms.Core/NosSmooth.Comms.Core.csproj @@ -10,7 +10,7 @@ - + diff --git a/src/Local/NosSmooth.Comms.Inject/MessageResponders/FocusResponder.cs b/src/Local/NosSmooth.Comms.Inject/MessageResponders/FocusResponder.cs index 020ae20..bc14896 100644 --- a/src/Local/NosSmooth.Comms.Inject/MessageResponders/FocusResponder.cs +++ b/src/Local/NosSmooth.Comms.Inject/MessageResponders/FocusResponder.cs @@ -7,6 +7,7 @@ using NosSmooth.Comms.Data.Responders; using NosSmooth.Comms.Inject.Messages; using NosSmooth.LocalBinding; +using NosSmooth.LocalBinding.Errors; using NosSmooth.LocalBinding.Hooks; using NosSmooth.LocalBinding.Structs; using Remora.Results; @@ -20,7 +21,7 @@ public class FocusResponder : IMessageResponder { private readonly NosBrowserManager _browserManager; private readonly NosThreadSynchronizer _synchronizer; - private readonly IEntityFocusHook _focusHook; + private readonly Optional _focusHook; /// /// Initializes a new instance of the class. @@ -29,7 +30,7 @@ public class FocusResponder : IMessageResponder /// The synchronizer. /// The focus hook. public FocusResponder - (NosBrowserManager browserManager, NosThreadSynchronizer synchronizer, IEntityFocusHook focusHook) + (NosBrowserManager browserManager, NosThreadSynchronizer synchronizer, Optional focusHook) { _browserManager = browserManager; _synchronizer = synchronizer; @@ -40,9 +41,14 @@ public class FocusResponder : IMessageResponder public async Task Respond(FocusMessage message, CancellationToken ct = default) { MapBaseObj? entity = null; + if (!_browserManager.SceneManager.TryGet(out var sceneManager)) + { + return new OptionalNotPresentError(nameof(SceneManager)); + } + if (message.EntityId is not null) { - var entityResult = _browserManager.SceneManager.FindEntity(message.EntityId.Value); + var entityResult = sceneManager.FindEntity(message.EntityId.Value); if (!entityResult.IsDefined(out entity)) { @@ -52,11 +58,17 @@ public class FocusResponder : IMessageResponder return await _synchronizer.SynchronizeAsync ( - () => - { - _focusHook.WrapperFunction(entity); - return Result.FromSuccess(); - }, + () => _focusHook.MapResult + ( + hook => hook.WrapperFunction.MapResult + ( + wrapper => + { + wrapper(entity); + return Result.FromSuccess(); + } + ) + ), ct ); } diff --git a/src/Local/NosSmooth.Comms.Inject/MessageResponders/FollowResponder.cs b/src/Local/NosSmooth.Comms.Inject/MessageResponders/FollowResponder.cs index 1bacec4..5d9eea8 100644 --- a/src/Local/NosSmooth.Comms.Inject/MessageResponders/FollowResponder.cs +++ b/src/Local/NosSmooth.Comms.Inject/MessageResponders/FollowResponder.cs @@ -7,6 +7,7 @@ using NosSmooth.Comms.Data.Responders; using NosSmooth.Comms.Inject.Messages; using NosSmooth.LocalBinding; +using NosSmooth.LocalBinding.Errors; using NosSmooth.LocalBinding.Hooks; using NosSmooth.LocalBinding.Structs; using Remora.Results; @@ -40,9 +41,14 @@ public class FollowResponder : IMessageResponder public async Task Respond(FollowMessage message, CancellationToken ct = default) { MapBaseObj? entity = null; + if (!_browserManager.SceneManager.TryGet(out var sceneManager)) + { + return new OptionalNotPresentError(nameof(SceneManager)); + } + if (message.EntityId is not null) { - var entityResult = _browserManager.SceneManager.FindEntity(message.EntityId.Value); + var entityResult = sceneManager.FindEntity(message.EntityId.Value); if (!entityResult.IsDefined(out entity)) { @@ -56,13 +62,30 @@ public class FollowResponder : IMessageResponder { if (entity is null) { - _hookManager.EntityUnfollow.WrapperFunction(); + return _hookManager.EntityUnfollow.MapResult + ( + unfollow => unfollow.WrapperFunction.MapResult + ( + wrapper => + { + wrapper(); + return Result.FromSuccess(); + } + ) + ); } - else - { - _hookManager.EntityFollow.WrapperFunction(entity); - } - return Result.FromSuccess(); + + return _hookManager.EntityFollow.MapResult + ( + unfollow => unfollow.WrapperFunction.MapResult + ( + wrapper => + { + wrapper(entity); + return Result.FromSuccess(); + } + ) + ); }, ct ); diff --git a/src/Local/NosSmooth.Comms.Inject/MessageResponders/HandshakeResponder.cs b/src/Local/NosSmooth.Comms.Inject/MessageResponders/HandshakeResponder.cs index 1eafcab..dc4f075 100644 --- a/src/Local/NosSmooth.Comms.Inject/MessageResponders/HandshakeResponder.cs +++ b/src/Local/NosSmooth.Comms.Inject/MessageResponders/HandshakeResponder.cs @@ -57,10 +57,12 @@ public class HandshakeResponder : IMessageResponder string? playerName = null; long? playerId = null; - if (_browserManager.IsInGame) + if (_browserManager.PlayerManager.TryGet(out var playerManager) && + _browserManager.IsInGame.TryGet(out var isInGame) && + isInGame) { - playerName = _browserManager.PlayerManager.Player.Name; - playerId = _browserManager.PlayerManager.PlayerId; + playerName = playerManager.Player.Name; + playerId = playerManager.PlayerId; } var result = await _connectionHandler.SendMessageAsync diff --git a/src/Local/NosSmooth.Comms.Inject/NosSmooth.Comms.Inject.csproj b/src/Local/NosSmooth.Comms.Inject/NosSmooth.Comms.Inject.csproj index df85215..1763023 100644 --- a/src/Local/NosSmooth.Comms.Inject/NosSmooth.Comms.Inject.csproj +++ b/src/Local/NosSmooth.Comms.Inject/NosSmooth.Comms.Inject.csproj @@ -14,12 +14,12 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - + - - - - + + + + diff --git a/src/Local/NosSmooth.Comms.Local/NosSmooth.Comms.Local.csproj b/src/Local/NosSmooth.Comms.Local/NosSmooth.Comms.Local.csproj index 9300e1e..f5d7d7e 100644 --- a/src/Local/NosSmooth.Comms.Local/NosSmooth.Comms.Local.csproj +++ b/src/Local/NosSmooth.Comms.Local/NosSmooth.Comms.Local.csproj @@ -17,12 +17,12 @@ - + All None - + diff --git a/src/Samples/ConsolePacketLogger/Program.cs b/src/Samples/ConsolePacketLogger/Program.cs index 906f6b4..eb0bc16 100644 --- a/src/Samples/ConsolePacketLogger/Program.cs +++ b/src/Samples/ConsolePacketLogger/Program.cs @@ -50,8 +50,8 @@ public static class Program .Title("Choose NosTale process to log packets from.") .UseConverter ( - x => x.IsInGame - ? $"{x.PlayerManager.Player.Name} ({x.Process.ProcessName} - {x.Process.Id})" + x => x.IsInGame.Get() + ? $"{x.PlayerManager.Get().Player.Name} ({x.Process.ProcessName} - {x.Process.Id})" : $"Not in game ({x.Process.ProcessName} - {x.Process.Id})" ) .AddChoices(nostaleProcesses) -- 2.48.1