From e4b2ea9faa61be1369e4acb98706d7c33bf0a9f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Boh=C3=A1=C4=8Dek?= Date: Thu, 23 Dec 2021 16:04:56 +0100 Subject: [PATCH] feat: add sample for walk command --- Samples/WalkCommands/ChatPacketInterceptor.cs | 106 ++++++++++++++++++ .../WalkCommands/Commands/DetachCommand.cs | 53 +++++++++ Samples/WalkCommands/Commands/WalkCommands.cs | 77 +++++++++++++ Samples/WalkCommands/DllMain.cs | 40 +++++++ Samples/WalkCommands/FodyWeavers.xml | 3 + Samples/WalkCommands/Startup.cs | 51 +++++++++ Samples/WalkCommands/WalkCommands.csproj | 76 +++++++++++++ 7 files changed, 406 insertions(+) create mode 100644 Samples/WalkCommands/ChatPacketInterceptor.cs create mode 100644 Samples/WalkCommands/Commands/DetachCommand.cs create mode 100644 Samples/WalkCommands/Commands/WalkCommands.cs create mode 100644 Samples/WalkCommands/DllMain.cs create mode 100644 Samples/WalkCommands/FodyWeavers.xml create mode 100644 Samples/WalkCommands/Startup.cs create mode 100644 Samples/WalkCommands/WalkCommands.csproj diff --git a/Samples/WalkCommands/ChatPacketInterceptor.cs b/Samples/WalkCommands/ChatPacketInterceptor.cs new file mode 100644 index 0000000..9abcfc1 --- /dev/null +++ b/Samples/WalkCommands/ChatPacketInterceptor.cs @@ -0,0 +1,106 @@ +// +// ChatPacketInterceptor.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 Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using NosCore.Packets.Enumerations; +using NosCore.Packets.ServerPackets.Chats; +using NosSmooth.Core.Client; +using NosSmooth.Core.Commands; +using NosSmooth.Core.Extensions; +using NosSmooth.LocalClient; +using Remora.Results; +using WalkCommands.Commands; + +namespace WalkCommands; + +/// +/// Interceptor of chat commands. +/// +public class ChatPacketInterceptor : IPacketInterceptor +{ + private readonly IServiceProvider _provider; + private readonly ILogger _logger; + private readonly INostaleClient _client; + + /// + /// Initializes a new instance of the class. + /// + /// The service provider. + /// The logger. + /// The nostale client. + public ChatPacketInterceptor(IServiceProvider provider, ILogger logger, INostaleClient client) + { + _provider = provider; + _logger = logger; + _client = client; + } + + /// + public bool InterceptSend(ref string packet) + { + if (packet.StartsWith($"say #")) + { + var packetString = packet; + Task.Run(async () => + { + try + { + await ExecuteCommand(packetString.Substring(5)); + } + catch (Exception ex) + { + _logger.LogError(ex, "Could not execute command."); + } + }); + return false; + } + + return true; + } + + /// + public bool InterceptReceive(ref string packet) + { + return true; + } + + private async Task ExecuteCommand(string command) + { + await _client.ReceivePacketAsync(new SayPacket + { + Type = SayColorType.Green, Message = $"Handling a command {command}." + }); + + var splitted = command.Split(new[] { ' ' }); + using var scope = _provider.CreateScope(); + Result result; + switch (splitted[0]) + { + case "walk": + var walkCommand = scope.ServiceProvider.GetRequiredService(); + result = await walkCommand.HandleWalkToAsync(int.Parse(splitted[1]), int.Parse(splitted[2]), splitted.Length > 3 ? bool.Parse(splitted[3]) : true); + break; + case "detach": + var detachCommand = scope.ServiceProvider.GetRequiredService(); + result = await detachCommand.HandleDetach(); + break; + default: + await _client.ReceivePacketAsync(new SayPacket + { + Type = SayColorType.Red, Message = $"The command {splitted[0]} was not found." + }); + return; + } + + if (!result.IsSuccess) + { + _logger.LogError("Could not execute a command"); + _logger.LogResultError(result); + } + } +} \ No newline at end of file diff --git a/Samples/WalkCommands/Commands/DetachCommand.cs b/Samples/WalkCommands/Commands/DetachCommand.cs new file mode 100644 index 0000000..5e47ecc --- /dev/null +++ b/Samples/WalkCommands/Commands/DetachCommand.cs @@ -0,0 +1,53 @@ +// +// DetachCommand.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 NosCore.Packets.Enumerations; +using NosCore.Packets.ServerPackets.Chats; +using NosSmooth.Core.Client; +using Remora.Results; + +namespace WalkCommands.Commands; + +/// +/// Group for detaching command that detaches the dll. +/// +public class DetachCommand +{ + private readonly CancellationTokenSource _dllStop; + private readonly INostaleClient _client; + + /// + /// Initializes a new instance of the class. + /// + /// The cancellation token source to stop the client. + /// The nostale client. + public DetachCommand(CancellationTokenSource dllStop, INostaleClient client) + { + _dllStop = dllStop; + _client = client; + } + + /// + /// Detach the dll. + /// + /// A result that may or may not have succeeded. + public async Task HandleDetach() + { + var receiveResult = await _client.ReceivePacketAsync(new SayPacket + { + Message = "Going to detach!", + Type = SayColorType.Green + }); + + if (!receiveResult.IsSuccess) + { + return receiveResult; + } + + _dllStop.Cancel(); + return Result.FromSuccess(); + } +} \ No newline at end of file diff --git a/Samples/WalkCommands/Commands/WalkCommands.cs b/Samples/WalkCommands/Commands/WalkCommands.cs new file mode 100644 index 0000000..08b139b --- /dev/null +++ b/Samples/WalkCommands/Commands/WalkCommands.cs @@ -0,0 +1,77 @@ +// +// WalkCommands.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 NosCore.Packets.Enumerations; +using NosCore.Packets.ServerPackets.Chats; +using NosSmooth.Core.Client; +using NosSmooth.Core.Commands; +using Remora.Results; + +namespace WalkCommands.Commands; + +/// +/// Represents command group for walking. +/// +public class WalkCommands +{ + private readonly INostaleClient _client; + + /// + /// Initializes a new instance of the class. + /// + /// The nostale client. + public WalkCommands(INostaleClient client) + { + _client = client ?? throw new ArgumentNullException(nameof(client)); + } + + /// + /// Attempts to walk the character to the specified lcoation. + /// + /// The x coordinate. + /// The y coordinate. + /// Whether the user can cancel the operation. + /// The cancellation token for cancelling the operation. + /// A result that may or may not have succeeded. + public async Task HandleWalkToAsync + ( + int x, + int y, + bool isCancellable = true, + CancellationToken ct = default + ) + { + var receiveResult = await _client.ReceivePacketAsync + ( + new SayPacket + { + Type = SayColorType.Red, Message = $"Going to walk to {x} {y}" + }, + ct + ); + + if (!receiveResult.IsSuccess) + { + return receiveResult; + } + + var command = new WalkCommand(x, y, isCancellable); + var walkResult = await _client.SendCommandAsync(command, ct); + if (!walkResult.IsSuccess) + { + return walkResult; + } + + return await _client.ReceivePacketAsync + ( + new SayPacket + { + Type = SayColorType.Red, Message = "Walk has finished successfully." + }, + ct + ); + } +} \ No newline at end of file diff --git a/Samples/WalkCommands/DllMain.cs b/Samples/WalkCommands/DllMain.cs new file mode 100644 index 0000000..08460f8 --- /dev/null +++ b/Samples/WalkCommands/DllMain.cs @@ -0,0 +1,40 @@ +// +// DllMain.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.Runtime.InteropServices; + +namespace WalkCommands; + +/// +/// Represents the dll entrypoint class. +/// +public class DllMain +{ + [DllImport("kernel32")] +#pragma warning disable SA1600 + public static extern bool AllocConsole(); +#pragma warning restore SA1600 + + /// + /// Represents the dll entrypoint method. + /// + [DllExport] + public static void Main() + { + AllocConsole(); + new Thread(() => + { + try + { + new Startup().RunAsync().GetAwaiter().GetResult(); + } + catch (Exception e) + { + Console.WriteLine(e.ToString()); + } + }).Start(); + } +} \ No newline at end of file diff --git a/Samples/WalkCommands/FodyWeavers.xml b/Samples/WalkCommands/FodyWeavers.xml new file mode 100644 index 0000000..5029e70 --- /dev/null +++ b/Samples/WalkCommands/FodyWeavers.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/Samples/WalkCommands/Startup.cs b/Samples/WalkCommands/Startup.cs new file mode 100644 index 0000000..35ba79c --- /dev/null +++ b/Samples/WalkCommands/Startup.cs @@ -0,0 +1,51 @@ +// +// Startup.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 Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using NosSmooth.Core.Client; +using NosSmooth.LocalClient; +using NosSmooth.LocalClient.Extensions; +using WalkCommands.Commands; + +namespace WalkCommands; + +/// +/// Startup class of WalkCommands. +/// +public class Startup +{ + private IServiceProvider BuildServices() + { + return new ServiceCollection() + .AddLocalClient() + .AddScoped() + .AddScoped() + .AddSingleton() + .Configure(o => o.AllowIntercept = true) + .AddSingleton() + .AddLogging(b => + { + b.ClearProviders(); + b.AddConsole(); + b.SetMinimumLevel(LogLevel.Debug); + }) + .BuildServiceProvider(); + } + + /// + /// Run the MoveToMiniland. + /// + /// A task that may or may not have succeeded. + public async Task RunAsync() + { + var provider = BuildServices(); + var mainCancellation = provider.GetRequiredService(); + + var client = provider.GetRequiredService(); + await client.RunAsync(mainCancellation.Token); + } +} \ No newline at end of file diff --git a/Samples/WalkCommands/WalkCommands.csproj b/Samples/WalkCommands/WalkCommands.csproj new file mode 100644 index 0000000..cfcbb16 --- /dev/null +++ b/Samples/WalkCommands/WalkCommands.csproj @@ -0,0 +1,76 @@ + + + net48 + enable + enable + WalkCommands + WalkCommands + 10 + + + 9C088A1D-54DE-4A9B-9C1B-DBC5BD5F5299 + DllExport.dll + WalkCommands + true + x86 + 7 + false + false + false + false + 30000 + 2 + 0 + 0 + 0 + + + + 5.7.0 + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + 1.7.4 + false + 1 + + + 6.6.0 + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + 6.0.0 + + + 6.0.0 + + + 6.0.0 + + + + + + + + + + + + + + + + + $(SolutionDir)packages\DllExport.1.7.4\gcache\$(DllExportMetaXBase)\$(DllExportNamespace)\$(DllExportMetaLibName) + False + False + + + + + + + \ No newline at end of file -- 2.49.0