From 3fef05595453b3cf3058582e146990509b1ec5ce Mon Sep 17 00:00:00 2001 From: Rutherther Date: Tue, 14 Feb 2023 10:29:51 +0100 Subject: [PATCH] feat(samples): implement support for optional modules and hooks --- .../External/ExternalBrowser/Program.cs | 6 ++-- .../SimplePiiBot/Commands/EntityCommands.cs | 29 +++++++++---------- .../HighLevel/SimplePiiBot/HostedService.cs | 10 +++++++ .../InterceptNameChanger/NameChanger.cs | 11 +++++++ src/Samples/LowLevel/SimpleChat/SimpleChat.cs | 11 +++++++ src/Samples/LowLevel/WalkCommands/Startup.cs | 11 +++++++ 6 files changed, 59 insertions(+), 19 deletions(-) diff --git a/src/Samples/External/ExternalBrowser/Program.cs b/src/Samples/External/ExternalBrowser/Program.cs index bd7c5f10604dd9e482d336b1f5b8fd57bc405cd9..665ed337e10966a74f2297ac2063c6bceef05627 100644 --- a/src/Samples/External/ExternalBrowser/Program.cs +++ b/src/Samples/External/ExternalBrowser/Program.cs @@ -55,17 +55,17 @@ public class Program Console.Error.WriteLine(initializationResult.ToFullString()); } - var length = externalBrowser.PetManagerList.Length; + var length = externalBrowser.PetManagerList.Get().Length; Console.WriteLine(length); - if (!externalBrowser.IsInGame) + if (!externalBrowser.IsInGame.Get()) { Console.Error.WriteLine("The player is not in game, cannot get the name of the player."); continue; } Console.WriteLine - ($"Player in process {process.Id} is named {externalBrowser.PlayerManager.Player.Name}"); + ($"Player in process {process.Id} is named {externalBrowser.PlayerManager.Get().Player.Name}"); } } } diff --git a/src/Samples/HighLevel/SimplePiiBot/Commands/EntityCommands.cs b/src/Samples/HighLevel/SimplePiiBot/Commands/EntityCommands.cs index 0d8295f6204627d78c149d7620628da21704941d..538091605e9aa4b9b75a3f21148bb1df14b53184 100644 --- a/src/Samples/HighLevel/SimplePiiBot/Commands/EntityCommands.cs +++ b/src/Samples/HighLevel/SimplePiiBot/Commands/EntityCommands.cs @@ -107,18 +107,17 @@ public class EntityCommands : CommandGroup public async Task HandleFocusAsync(int entityId) { var entityResult = _sceneManager.FindEntity(entityId); - if (!entityResult.IsSuccess) + if (!entityResult.IsDefined(out var entity)) { return Result.FromError(entityResult); } return await _synchronizer.SynchronizeAsync ( - () => - { - _hookManager.EntityFocus.WrapperFunction(entityResult.Entity); - return Result.FromSuccess(); - }, + () => _hookManager.EntityFocus.MapResult + ( + focus => focus.WrapperFunction.MapResult(wrapper => wrapper(entity)) + ), CancellationToken ); } @@ -139,11 +138,10 @@ public class EntityCommands : CommandGroup return await _synchronizer.SynchronizeAsync ( - () => - { - _hookManager.EntityFollow.WrapperFunction(entity); - return Result.FromSuccess(); - }, + () => _hookManager.EntityFollow.MapResult + ( + follow => follow.WrapperFunction.MapResult(wrapper => wrapper(entity)) + ), CancellationToken ); } @@ -157,11 +155,10 @@ public class EntityCommands : CommandGroup { return await _synchronizer.SynchronizeAsync ( - () => - { - _hookManager.EntityUnfollow.WrapperFunction(); - return Result.FromSuccess(); - }, + () => _hookManager.EntityUnfollow.MapResult + ( + unfollow => unfollow.WrapperFunction.MapResult(wrapper => wrapper()) + ), CancellationToken ); } diff --git a/src/Samples/HighLevel/SimplePiiBot/HostedService.cs b/src/Samples/HighLevel/SimplePiiBot/HostedService.cs index 0ff9e39ceac06dade1193dbb2e4370b9c3941717..7254808ede870fb0c393001cab5bb8667a11730c 100644 --- a/src/Samples/HighLevel/SimplePiiBot/HostedService.cs +++ b/src/Samples/HighLevel/SimplePiiBot/HostedService.cs @@ -11,6 +11,7 @@ using NosSmooth.Core.Client; using NosSmooth.Core.Extensions; using NosSmooth.Data.NOSFiles; using NosSmooth.LocalBinding; +using NosSmooth.LocalBinding.Hooks; using NosSmooth.PacketSerializer.Extensions; using NosSmooth.PacketSerializer.Packets; using OneOf.Types; @@ -78,6 +79,15 @@ public class HostedService : BackgroundService if (!bindingResult.IsSuccess) { _logger.LogResultError(bindingResult); + } + + if (!_bindingManager.IsModulePresent() || !_bindingManager.IsModulePresent() + || !_bindingManager.IsModulePresent()) + { + _logger.LogError + ( + "At least one of: periodic, packet receive, packet send has not been loaded correctly, the bot may not be used at all. Aborting" + ); return; } diff --git a/src/Samples/LowLevel/InterceptNameChanger/NameChanger.cs b/src/Samples/LowLevel/InterceptNameChanger/NameChanger.cs index b5d197604d68bf1af446c9cc7be4237baf740f0a..1384b37641b5110692b290d23ba25cb75fa17d51 100644 --- a/src/Samples/LowLevel/InterceptNameChanger/NameChanger.cs +++ b/src/Samples/LowLevel/InterceptNameChanger/NameChanger.cs @@ -11,6 +11,7 @@ using NosSmooth.Core.Client; using NosSmooth.Core.Extensions; using NosSmooth.Extensions.SharedBinding.Extensions; using NosSmooth.LocalBinding; +using NosSmooth.LocalBinding.Hooks; using NosSmooth.LocalClient; using NosSmooth.LocalClient.Extensions; using NosSmooth.Packets.Enums; @@ -63,6 +64,16 @@ namespace InterceptNameChanger logger.LogResultError(initializeResult); } + if (!bindingManager.IsModulePresent() || !bindingManager.IsModulePresent() + || !bindingManager.IsModulePresent()) + { + logger.LogError + ( + "At least one of: periodic, packet receive, packet send has not been loaded correctly, the bot may not be used at all. Aborting" + ); + return; + } + var packetTypesRepository = provider.GetRequiredService(); var packetAddResult = packetTypesRepository.AddDefaultPackets(); if (!packetAddResult.IsSuccess) diff --git a/src/Samples/LowLevel/SimpleChat/SimpleChat.cs b/src/Samples/LowLevel/SimpleChat/SimpleChat.cs index f6c1b671cc90ab97da5b953c65397e0bbb65c037..37ae32f0b67862ebcc246dc0ac8538d67ad99068 100644 --- a/src/Samples/LowLevel/SimpleChat/SimpleChat.cs +++ b/src/Samples/LowLevel/SimpleChat/SimpleChat.cs @@ -10,6 +10,7 @@ using NosSmooth.Core.Client; using NosSmooth.Core.Extensions; using NosSmooth.Extensions.SharedBinding.Extensions; using NosSmooth.LocalBinding; +using NosSmooth.LocalBinding.Hooks; using NosSmooth.LocalClient.Extensions; using NosSmooth.Packets.Enums; using NosSmooth.Packets.Enums.Chat; @@ -57,6 +58,16 @@ public class SimpleChat logger.LogError($"Could not initialize NosBindingManager."); logger.LogResultError(initializeResult); } + + if (!bindingManager.IsModulePresent() || !bindingManager.IsModulePresent() + || !bindingManager.IsModulePresent()) + { + logger.LogError + ( + "At least one of: periodic, packet receive, packet send has not been loaded correctly, the bot may not be used at all. Aborting" + ); + return; + } var packetTypesRepository = provider.GetRequiredService(); var packetAddResult = packetTypesRepository.AddDefaultPackets(); diff --git a/src/Samples/LowLevel/WalkCommands/Startup.cs b/src/Samples/LowLevel/WalkCommands/Startup.cs index a48c433495be878f42cc508ae14188b77de65531..3a94f203b9d17b9f817c5c67b813489918b4c492 100644 --- a/src/Samples/LowLevel/WalkCommands/Startup.cs +++ b/src/Samples/LowLevel/WalkCommands/Startup.cs @@ -16,6 +16,7 @@ using NosSmooth.Extensions.Pathfinding.Extensions; using NosSmooth.Extensions.SharedBinding.Extensions; using NosSmooth.LocalBinding; using NosSmooth.LocalBinding.Extensions; +using NosSmooth.LocalBinding.Hooks; using NosSmooth.LocalClient; using NosSmooth.LocalClient.Extensions; using NosSmooth.PacketSerializer.Extensions; @@ -82,6 +83,16 @@ public class Startup logger.LogError($"Could not initialize NosBindingManager."); logger.LogResultError(initializeResult); } + + if (!bindingManager.IsModulePresent() || !bindingManager.IsModulePresent() + || !bindingManager.IsModulePresent()) + { + logger.LogError + ( + "At least one of: periodic, packet receive, packet send has not been loaded correctly, the bot may not be used at all. Aborting" + ); + return; + } var packetTypesRepository = provider.GetRequiredService(); var packetAddResult = packetTypesRepository.AddDefaultPackets();