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 bd7c5f1..665ed33 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 0d8295f..5380916 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 0ff9e39..7254808 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 b5d1976..1384b37 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 f6c1b67..37ae32f 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 a48c433..3a94f20 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(); -- 2.48.1