From c6be7bd2a809904ae8a4259b0fa2512051b4d4e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Boh=C3=A1=C4=8Dek?= Date: Fri, 31 Dec 2021 23:49:28 +0100 Subject: [PATCH] chore: remove NosCore dependency --- .gitmodules | 6 - .../Client/BaseNostaleClient.cs | 149 +++---- Core/NosSmooth.Core/Client/INostaleClient.cs | 142 +++--- .../Extensions/ResultExtensions.cs | 1 - .../Extensions/ServiceCollectionExtensions.cs | 336 +++++++------- Core/NosSmooth.Core/NosSmooth.Core.csproj | 36 +- .../Converters/ISpecificPacketSerializer.cs | 133 +----- .../Packets/Converters/InPacketSerializer.cs | 92 +--- Core/NosSmooth.Core/Packets/IPacketHandler.cs | 72 +-- .../Packets/IPacketResponder.cs | 104 ++--- .../Packets/IPacketSerializer.cs | 44 -- .../NosSmooth.Core/Packets/PacketEventArgs.cs | 6 +- Core/NosSmooth.Core/Packets/PacketHandler.cs | 209 ++++----- .../Packets/PacketSerializer.cs | 88 ---- .../Packets/PacketSerializerProvider.cs | 83 ---- Core/NosSmooth.Core/Packets/PacketType.cs | 23 - .../Packets/ParsingFailedPacket.cs | 74 ++-- .../Packets/Server/Entities/InPacket.cs | 2 +- .../NostaleLocalClient.cs | 413 +++++++++--------- NosSmooth.Unix.sln | 60 --- NosSmooth.sln | 60 +-- .../Packets/InPacketSerializerTest.cs | 159 +------ libs/Directory.Build.props | 3 - libs/NosCore.Packets | 1 - libs/NosCore.Shared | 1 - 25 files changed, 783 insertions(+), 1514 deletions(-) delete mode 100644 .gitmodules delete mode 100644 Core/NosSmooth.Core/Packets/IPacketSerializer.cs delete mode 100644 Core/NosSmooth.Core/Packets/PacketSerializer.cs delete mode 100644 Core/NosSmooth.Core/Packets/PacketSerializerProvider.cs delete mode 100644 Core/NosSmooth.Core/Packets/PacketType.cs delete mode 100644 libs/Directory.Build.props delete mode 160000 libs/NosCore.Packets delete mode 160000 libs/NosCore.Shared diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index 560e288..0000000 --- a/.gitmodules +++ /dev/null @@ -1,6 +0,0 @@ -[submodule "libs/NosCore.Packets"] - path = libs/NosCore.Packets - url = https://github.com/Rutherther/NosCore.Packets.git -[submodule "libs/NosCore.Shared"] - path = libs/NosCore.Shared - url = https://github.com/Rutherther/NosCore.Shared.git diff --git a/Core/NosSmooth.Core/Client/BaseNostaleClient.cs b/Core/NosSmooth.Core/Client/BaseNostaleClient.cs index c146d7b..7a43eb6 100644 --- a/Core/NosSmooth.Core/Client/BaseNostaleClient.cs +++ b/Core/NosSmooth.Core/Client/BaseNostaleClient.cs @@ -1,74 +1,75 @@ -// -// BaseNostaleClient.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.Threading; -using System.Threading.Tasks; -using NosCore.Packets.Interfaces; -using NosSmooth.Core.Commands; -using NosSmooth.Core.Packets; -using Remora.Results; - -namespace NosSmooth.Core.Client; - -/// -/// Represents base class of . -/// -/// -/// This class serializes packets and processes commands. -/// -public abstract class BaseNostaleClient : INostaleClient -{ - private readonly CommandProcessor _commandProcessor; - private readonly IPacketSerializer _packetSerializer; - - /// - /// Initializes a new instance of the class. - /// - /// The command processor. - /// The packet serializer. - protected BaseNostaleClient - ( - CommandProcessor commandProcessor, - IPacketSerializer packetSerializer - ) - { - _commandProcessor = commandProcessor; - _packetSerializer = packetSerializer; - } - - /// - public abstract Task RunAsync(CancellationToken stopRequested = default); - - /// - public virtual Task SendPacketAsync(IPacket packet, CancellationToken ct = default) - { - var serialized = _packetSerializer.Serialize(packet); - - return serialized.IsSuccess - ? SendPacketAsync(serialized.Entity, ct) - : Task.FromResult(Result.FromError(serialized)); - } - - /// - public abstract Task SendPacketAsync(string packetString, CancellationToken ct = default); - - /// - public abstract Task ReceivePacketAsync(string packetString, CancellationToken ct = default); - - /// - public virtual Task ReceivePacketAsync(IPacket packet, CancellationToken ct = default) - { - var serialized = _packetSerializer.Serialize(packet); - - return serialized.IsSuccess - ? ReceivePacketAsync(serialized.Entity, ct) - : Task.FromResult(Result.FromError(serialized)); - } - - /// - public Task SendCommandAsync(ICommand command, CancellationToken ct = default) => - _commandProcessor.ProcessCommand(command, ct); -} \ No newline at end of file +// +// BaseNostaleClient.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.Threading; +using System.Threading.Tasks; +using NosSmooth.Core.Commands; +using NosSmooth.Core.Packets; +using NosSmooth.Packets; +using NosSmooth.Packets.Packets; +using Remora.Results; + +namespace NosSmooth.Core.Client; + +/// +/// Represents base class of . +/// +/// +/// This class serializes packets and processes commands. +/// +public abstract class BaseNostaleClient : INostaleClient +{ + private readonly CommandProcessor _commandProcessor; + private readonly IPacketSerializer _packetSerializer; + + /// + /// Initializes a new instance of the class. + /// + /// The command processor. + /// The packet serializer. + protected BaseNostaleClient + ( + CommandProcessor commandProcessor, + IPacketSerializer packetSerializer + ) + { + _commandProcessor = commandProcessor; + _packetSerializer = packetSerializer; + } + + /// + public abstract Task RunAsync(CancellationToken stopRequested = default); + + /// + public virtual Task SendPacketAsync(IPacket packet, CancellationToken ct = default) + { + var serialized = _packetSerializer.Serialize(packet); + + return serialized.IsSuccess + ? SendPacketAsync(serialized.Entity, ct) + : Task.FromResult(Result.FromError(serialized)); + } + + /// + public abstract Task SendPacketAsync(string packetString, CancellationToken ct = default); + + /// + public abstract Task ReceivePacketAsync(string packetString, CancellationToken ct = default); + + /// + public virtual Task ReceivePacketAsync(IPacket packet, CancellationToken ct = default) + { + var serialized = _packetSerializer.Serialize(packet); + + return serialized.IsSuccess + ? ReceivePacketAsync(serialized.Entity, ct) + : Task.FromResult(Result.FromError(serialized)); + } + + /// + public Task SendCommandAsync(ICommand command, CancellationToken ct = default) => + _commandProcessor.ProcessCommand(command, ct); +} diff --git a/Core/NosSmooth.Core/Client/INostaleClient.cs b/Core/NosSmooth.Core/Client/INostaleClient.cs index f223f4a..8952f1c 100644 --- a/Core/NosSmooth.Core/Client/INostaleClient.cs +++ b/Core/NosSmooth.Core/Client/INostaleClient.cs @@ -1,71 +1,71 @@ -// -// INostaleClient.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.Threading; -using System.Threading.Tasks; -using NosCore.Packets.Interfaces; -using NosSmooth.Core.Commands; -using Remora.Results; - -namespace NosSmooth.Core.Client; - -/// -/// Class representing nostale client that may send and receive packets as well as process commands. -/// -public interface INostaleClient -{ - /// - /// Starts the client. - /// - /// A cancellation token for stopping the client. - /// The result that may or may not have succeeded. - public Task RunAsync(CancellationToken stopRequested = default); - - /// - /// Sends the given packet to the server. - /// - /// The packet to send. - /// The cancellation token for cancelling the operation. - /// A result that may or may not have succeeded. - public Task SendPacketAsync(IPacket packet, CancellationToken ct = default); - - /// - /// Sends the given raw packet string. - /// - /// The packed string to send in plain text. - /// The cancellation token for cancelling the operation. - /// A result that may or may not have succeeded. - public Task SendPacketAsync(string packetString, CancellationToken ct = default); - - /// - /// Receives the given raw packet string. - /// - /// The packet to receive in plain text. - /// The cancellation token for cancelling the operation. - /// A result that may or may not have succeeded. - public Task ReceivePacketAsync(string packetString, CancellationToken ct = default); - - /// - /// Receives the given packet. - /// - /// The packet to receive. - /// The cancellation token for cancelling the operation. - /// A result that may or may not have succeeded. - public Task ReceivePacketAsync(IPacket packet, CancellationToken ct = default); - - /// - /// Sends the given command to the client. - /// - /// - /// Commands can be used for doing complex operations like walking that require sending multiple packets - /// and/or calling some functions of the local client. - /// This method will not return until the command is finished or it failed. - /// - /// The command to send. - /// The cancellation token for cancelling the operation. - /// A result that may or may not have succeeded. - public Task SendCommandAsync(ICommand command, CancellationToken ct = default); -} \ No newline at end of file +// +// INostaleClient.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.Threading; +using System.Threading.Tasks; +using NosSmooth.Core.Commands; +using NosSmooth.Packets.Packets; +using Remora.Results; + +namespace NosSmooth.Core.Client; + +/// +/// Class representing nostale client that may send and receive packets as well as process commands. +/// +public interface INostaleClient +{ + /// + /// Starts the client. + /// + /// A cancellation token for stopping the client. + /// The result that may or may not have succeeded. + public Task RunAsync(CancellationToken stopRequested = default); + + /// + /// Sends the given packet to the server. + /// + /// The packet to send. + /// The cancellation token for cancelling the operation. + /// A result that may or may not have succeeded. + public Task SendPacketAsync(IPacket packet, CancellationToken ct = default); + + /// + /// Sends the given raw packet string. + /// + /// The packed string to send in plain text. + /// The cancellation token for cancelling the operation. + /// A result that may or may not have succeeded. + public Task SendPacketAsync(string packetString, CancellationToken ct = default); + + /// + /// Receives the given raw packet string. + /// + /// The packet to receive in plain text. + /// The cancellation token for cancelling the operation. + /// A result that may or may not have succeeded. + public Task ReceivePacketAsync(string packetString, CancellationToken ct = default); + + /// + /// Receives the given packet. + /// + /// The packet to receive. + /// The cancellation token for cancelling the operation. + /// A result that may or may not have succeeded. + public Task ReceivePacketAsync(IPacket packet, CancellationToken ct = default); + + /// + /// Sends the given command to the client. + /// + /// + /// Commands can be used for doing complex operations like walking that require sending multiple packets + /// and/or calling some functions of the local client. + /// This method will not return until the command is finished or it failed. + /// + /// The command to send. + /// The cancellation token for cancelling the operation. + /// A result that may or may not have succeeded. + public Task SendCommandAsync(ICommand command, CancellationToken ct = default); +} diff --git a/Core/NosSmooth.Core/Extensions/ResultExtensions.cs b/Core/NosSmooth.Core/Extensions/ResultExtensions.cs index c3c1596..84bcf0f 100644 --- a/Core/NosSmooth.Core/Extensions/ResultExtensions.cs +++ b/Core/NosSmooth.Core/Extensions/ResultExtensions.cs @@ -7,7 +7,6 @@ using System; using System.CodeDom.Compiler; using System.IO; -using System.Text; using Microsoft.Extensions.Logging; using Remora.Results; diff --git a/Core/NosSmooth.Core/Extensions/ServiceCollectionExtensions.cs b/Core/NosSmooth.Core/Extensions/ServiceCollectionExtensions.cs index 6e26531..cb978d4 100644 --- a/Core/NosSmooth.Core/Extensions/ServiceCollectionExtensions.cs +++ b/Core/NosSmooth.Core/Extensions/ServiceCollectionExtensions.cs @@ -1,185 +1,151 @@ -// -// ServiceCollectionExtensions.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; -using System.Linq; -using System.Reflection; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.DependencyInjection.Extensions; -using NosCore.Packets; -using NosCore.Packets.Attributes; -using NosCore.Packets.Enumerations; -using NosCore.Packets.Interfaces; -using NosSmooth.Core.Client; -using NosSmooth.Core.Commands; -using NosSmooth.Core.Packets; -using NosSmooth.Core.Packets.Converters; - -namespace NosSmooth.Core.Extensions; - -/// -/// Contains extension methods for . -/// -public static class ServiceCollectionExtensions -{ - /// - /// Adds base packet and command handling for nostale client. - /// - /// The service collection to register the responder to. - /// Custom types of packets to serialize and deserialize. - /// The collection. - public static IServiceCollection AddNostaleCore - ( - this IServiceCollection serviceCollection, - params Type[] additionalPacketTypes - ) - { - serviceCollection - .TryAddSingleton(); - - var clientPacketTypes = typeof(IPacket).Assembly.GetTypes() - .Where(p => (p.Namespace?.Contains("Client") ?? false) && p.GetInterfaces().Contains(typeof(IPacket)) && p.IsClass && !p.IsAbstract).ToList(); - var serverPacketTypes = typeof(IPacket).Assembly.GetTypes() - .Where(p => (p.Namespace?.Contains("Server") ?? false) && p.GetInterfaces().Contains(typeof(IPacket)) && p.IsClass && !p.IsAbstract).ToList(); - - if (additionalPacketTypes.Length != 0) - { - clientPacketTypes.AddRange(additionalPacketTypes); - } - - serviceCollection.AddSingleton(p => - new PacketSerializerProvider(clientPacketTypes, serverPacketTypes, p)); - serviceCollection.AddSingleton(p => p.GetRequiredService().ServerSerializer); - - serviceCollection.AddSingleton(); - - serviceCollection.AddSpecificPacketConverter(); - - return serviceCollection; - } - - /// - /// Adds the specified packet responder that will be called upon receiving the given event. - /// - /// The service collection to register the responder to. - /// The type of the responder. - /// Thrown if the type of the responder is incorrect. - /// The collection. - public static IServiceCollection AddPacketResponder - ( - this IServiceCollection serviceCollection - ) - where TPacketResponder : class, IPacketResponder - { - return serviceCollection.AddPacketResponder(typeof(TPacketResponder)); - } - - /// - /// Adds the specified packet responder that will be called upon receiving the given event. - /// - /// The service collection to register the responder to. - /// The type of the responder. - /// The collection. - /// Thrown if the type of the responder is incorrect. - public static IServiceCollection AddPacketResponder - ( - this IServiceCollection serviceCollection, - Type responderType - ) - { - if (responderType.GetInterfaces().Any(i => i == typeof(IEveryPacketResponder))) - { - return serviceCollection.AddScoped(typeof(IEveryPacketResponder), responderType); - } - - if (!responderType.GetInterfaces().Any( - i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IPacketResponder<>) - )) - { - throw new ArgumentException( - $"{nameof(responderType)} should implement IPacketResponder.", - nameof(responderType)); - } - - var responderTypeInterfaces = responderType.GetInterfaces(); - var responderInterfaces = responderTypeInterfaces.Where - ( - r => r.IsGenericType && r.GetGenericTypeDefinition() == typeof(IPacketResponder<>) - ); - - foreach (var responderInterface in responderInterfaces) - { - serviceCollection.AddScoped(responderInterface, responderType); - } - - return serviceCollection; - } - - /// - /// Adds the specified command handler. - /// - /// The service collection to register the responder to. - /// The type of the command. - /// Thrown if the type of the responder is incorrect. - /// The collection. - public static IServiceCollection AddCommandHandler - ( - this IServiceCollection serviceCollection - ) - where TCommandHandler : class, ICommandHandler - { - return serviceCollection.AddCommandHandler(typeof(TCommandHandler)); - } - - /// - /// Adds the specified command handler. - /// - /// The service collection to register the responder to. - /// The type of the command handler. - /// The collection. - /// Thrown if the type of the responder is incorrect. - public static IServiceCollection AddCommandHandler - ( - this IServiceCollection serviceCollection, - Type commandHandlerType - ) - { - if (!commandHandlerType.GetInterfaces().Any( - i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICommandHandler<>) - )) - { - throw new ArgumentException( - $"{nameof(commandHandlerType)} should implement ICommandHandler.", - nameof(commandHandlerType)); - } - - var handlerTypeInterfaces = commandHandlerType.GetInterfaces(); - var handlerInterfaces = handlerTypeInterfaces.Where - ( - r => r.IsGenericType && r.GetGenericTypeDefinition() == typeof(ICommandHandler<>) - ); - - foreach (var handlerInterface in handlerInterfaces) - { - serviceCollection.AddScoped(handlerInterface, commandHandlerType); - } - - return serviceCollection; - } - - /// - /// Adds the specified packet converter. - /// - /// The service collection to register the responder to. - /// The type of the packet. - /// Thrown if the type of the responder is incorrect. - /// The collection. - public static IServiceCollection AddSpecificPacketConverter(this IServiceCollection serviceCollection) - where TPacketConverter : class, ISpecificPacketSerializer - { - return serviceCollection.AddSingleton(); - } -} \ No newline at end of file +// +// ServiceCollectionExtensions.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; +using System.Linq; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; +using NosSmooth.Core.Commands; +using NosSmooth.Core.Packets; +using NosSmooth.Packets.Extensions; + +namespace NosSmooth.Core.Extensions; + +/// +/// Contains extension methods for . +/// +public static class ServiceCollectionExtensions +{ + /// + /// Adds base packet and command handling for nostale client. + /// + /// The service collection to register the responder to. + /// Custom types of packets to serialize and deserialize. + /// The collection. + public static IServiceCollection AddNostaleCore + ( + this IServiceCollection serviceCollection, + params Type[] additionalPacketTypes + ) + { + serviceCollection + .TryAddSingleton(); + + serviceCollection.AddPacketSerialization(); + serviceCollection.AddSingleton(); + + return serviceCollection; + } + + /// + /// Adds the specified packet responder that will be called upon receiving the given event. + /// + /// The service collection to register the responder to. + /// The type of the responder. + /// Thrown if the type of the responder is incorrect. + /// The collection. + public static IServiceCollection AddPacketResponder + ( + this IServiceCollection serviceCollection + ) + where TPacketResponder : class, IPacketResponder + { + return serviceCollection.AddPacketResponder(typeof(TPacketResponder)); + } + + /// + /// Adds the specified packet responder that will be called upon receiving the given event. + /// + /// The service collection to register the responder to. + /// The type of the responder. + /// The collection. + /// Thrown if the type of the responder is incorrect. + public static IServiceCollection AddPacketResponder + ( + this IServiceCollection serviceCollection, + Type responderType + ) + { + if (responderType.GetInterfaces().Any(i => i == typeof(IEveryPacketResponder))) + { + return serviceCollection.AddScoped(typeof(IEveryPacketResponder), responderType); + } + + if (!responderType.GetInterfaces().Any( + i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IPacketResponder<>) + )) + { + throw new ArgumentException( + $"{nameof(responderType)} should implement IPacketResponder.", + nameof(responderType)); + } + + var responderTypeInterfaces = responderType.GetInterfaces(); + var responderInterfaces = responderTypeInterfaces.Where + ( + r => r.IsGenericType && r.GetGenericTypeDefinition() == typeof(IPacketResponder<>) + ); + + foreach (var responderInterface in responderInterfaces) + { + serviceCollection.AddScoped(responderInterface, responderType); + } + + return serviceCollection; + } + + /// + /// Adds the specified command handler. + /// + /// The service collection to register the responder to. + /// The type of the command. + /// Thrown if the type of the responder is incorrect. + /// The collection. + public static IServiceCollection AddCommandHandler + ( + this IServiceCollection serviceCollection + ) + where TCommandHandler : class, ICommandHandler + { + return serviceCollection.AddCommandHandler(typeof(TCommandHandler)); + } + + /// + /// Adds the specified command handler. + /// + /// The service collection to register the responder to. + /// The type of the command handler. + /// The collection. + /// Thrown if the type of the responder is incorrect. + public static IServiceCollection AddCommandHandler + ( + this IServiceCollection serviceCollection, + Type commandHandlerType + ) + { + if (!commandHandlerType.GetInterfaces().Any( + i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICommandHandler<>) + )) + { + throw new ArgumentException( + $"{nameof(commandHandlerType)} should implement ICommandHandler.", + nameof(commandHandlerType)); + } + + var handlerTypeInterfaces = commandHandlerType.GetInterfaces(); + var handlerInterfaces = handlerTypeInterfaces.Where + ( + r => r.IsGenericType && r.GetGenericTypeDefinition() == typeof(ICommandHandler<>) + ); + + foreach (var handlerInterface in handlerInterfaces) + { + serviceCollection.AddScoped(handlerInterface, commandHandlerType); + } + + return serviceCollection; + } +} diff --git a/Core/NosSmooth.Core/NosSmooth.Core.csproj b/Core/NosSmooth.Core/NosSmooth.Core.csproj index 329d713..a496afd 100644 --- a/Core/NosSmooth.Core/NosSmooth.Core.csproj +++ b/Core/NosSmooth.Core/NosSmooth.Core.csproj @@ -1,18 +1,18 @@ - - - - enable - 10 - net6.0;netstandard2.0 - - - - - - - - - - - - + + + + enable + 10 + net6.0;netstandard2.0 + + + + + + + + + + + + diff --git a/Core/NosSmooth.Core/Packets/Converters/ISpecificPacketSerializer.cs b/Core/NosSmooth.Core/Packets/Converters/ISpecificPacketSerializer.cs index b3bfb1e..5f28270 100644 --- a/Core/NosSmooth.Core/Packets/Converters/ISpecificPacketSerializer.cs +++ b/Core/NosSmooth.Core/Packets/Converters/ISpecificPacketSerializer.cs @@ -1,132 +1 @@ -// -// ISpecificPacketSerializer.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.Reflection; -using NosCore.Packets.Attributes; -using NosCore.Packets.Interfaces; -using Remora.Results; - -namespace NosSmooth.Core.Packets.Converters; - -/// -/// Converts packets that cannot be handled easily by the default serializer. -/// -public interface ISpecificPacketSerializer -{ - /// - /// Gets whether this is a serializer. - /// - public bool Serializer { get; } - - /// - /// Gets whether this is a deserializer. - /// - public bool Deserializer { get; } - - /// - /// Whether the current packet serializer should handle the packet. - /// - /// The string of the packet. - /// If this serializer should be used for handling. - public bool ShouldHandle(string packetString); - - /// - /// Whether the current packet serializer should handle the packet. - /// - /// The packet object. - /// If this serializer should be used for handling. - public bool ShouldHandle(IPacket packet); - - /// - /// Serialize the given packet into string. - /// - /// The string of the packet. - /// The serialized packet or an error. - public Result Serialize(IPacket packet); - - /// - /// Deserialize the given packet to its type. - /// - /// The string of the packet. - /// The deserialized packet or an error. - public Result Deserialize(string packetString); -} - -/// -/// Converts packets that cannot be handled easily by the default serializer. -/// -/// The packet. -public abstract class SpecificPacketSerializer : ISpecificPacketSerializer - where TPacket : IPacket -{ - private string? _packetHeader; - - /// - /// Gets the packet header identifier. - /// - public string PacketHeader - { - get - { - if (_packetHeader is null) - { - _packetHeader = typeof(TPacket).GetCustomAttribute()!.Identification + " "; - } - - return _packetHeader; - } - } - - /// - public abstract bool Serializer { get; } - - /// - public abstract bool Deserializer { get; } - - /// - public bool ShouldHandle(string packetString) - { - return packetString.StartsWith(PacketHeader); - } - - /// - public bool ShouldHandle(IPacket packet) - { - return typeof(TPacket) == packet.GetType(); - } - - /// - Result ISpecificPacketSerializer.Serialize(IPacket packet) - { - return Serialize((TPacket)packet); - } - - /// - Result ISpecificPacketSerializer.Deserialize(string packetString) - { - var result = Deserialize(packetString); - if (!result.IsSuccess) - { - return Result.FromError(result); - } - - return Result.FromSuccess(result.Entity); - } - - /// - /// Serialize the given packet into string. - /// - /// The string of the packet. - /// The serialized packet or an error. - public abstract Result Serialize(TPacket packet); - - /// - /// Deserialize the given packet to its type. - /// - /// The string of the packet. - /// The deserialized packet or an error. - public abstract Result Deserialize(string packetString); -} \ No newline at end of file + \ No newline at end of file diff --git a/Core/NosSmooth.Core/Packets/Converters/InPacketSerializer.cs b/Core/NosSmooth.Core/Packets/Converters/InPacketSerializer.cs index 2d352cf..5f28270 100644 --- a/Core/NosSmooth.Core/Packets/Converters/InPacketSerializer.cs +++ b/Core/NosSmooth.Core/Packets/Converters/InPacketSerializer.cs @@ -1,91 +1 @@ -// -// InPacketSerializer.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; -using System.Linq; -using NosCore.Packets.ServerPackets.Visibility; -using NosCore.Shared.Enumerations; -using Remora.Results; - -namespace NosSmooth.Core.Packets.Converters; - -/// -/// Deserializes InPacket correctly. -/// -public class InPacketSerializer : SpecificPacketSerializer -{ - private readonly PacketSerializerProvider _packetSerializerProvider; - - /// - /// Initializes a new instance of the class. - /// - /// The provider of packet serializer. - public InPacketSerializer(PacketSerializerProvider packetSerializerProvider) - { - _packetSerializerProvider = packetSerializerProvider; - } - - /// - public override bool Serializer => false; - - /// - public override bool Deserializer => true; - - /// - public override Result Serialize(InPacket packet) - { - throw new System.NotImplementedException(); - } - - /// - public override Result Deserialize(string packetString) - { - try - { - var deserializer = _packetSerializerProvider.ServerSerializer.Deserializer; - var splitted = packetString.Split(new char[] { ' ' }, 9).Skip(1).ToArray(); - - if (!Enum.TryParse(splitted[0], out VisualType type)) - { - return new ArgumentInvalidError(nameof(packetString), "The visual type is incorrect."); - } - - var startAddress = type == VisualType.Player ? 3 : 2; - var inPacket = new InPacket - { - VisualType = type, - VNum = type != VisualType.Player ? long.Parse(splitted[1]) : null, - Name = type == VisualType.Player ? splitted[1] : null, - VisualId = long.Parse(splitted[startAddress]), - PositionX = short.Parse(splitted[startAddress + 1]), - PositionY = short.Parse(splitted[startAddress + 2]), - Direction = byte.Parse(splitted[startAddress + 3]) - }; - - switch (inPacket.VisualType) - { - case VisualType.Player: - inPacket.InCharacterSubPacket = (InCharacterSubPacket?)deserializer - .DeserializeHeaderlessIPacket(typeof(InCharacterSubPacket), splitted[7]); - break; - case VisualType.Object: - inPacket.InItemSubPacket = (InItemSubPacket?)deserializer - .DeserializeHeaderlessIPacket(typeof(InItemSubPacket), splitted[6] + " " + splitted[7]); - break; - default: - inPacket.InNonPlayerSubPacket = (InNonPlayerSubPacket?)deserializer - .DeserializeHeaderlessIPacket(typeof(InNonPlayerSubPacket), splitted[6] + " " + splitted[7]); - break; - } - - return inPacket; - } - catch (Exception e) - { - return e; - } - } -} \ No newline at end of file + \ No newline at end of file diff --git a/Core/NosSmooth.Core/Packets/IPacketHandler.cs b/Core/NosSmooth.Core/Packets/IPacketHandler.cs index e5ef885..ce7bf15 100644 --- a/Core/NosSmooth.Core/Packets/IPacketHandler.cs +++ b/Core/NosSmooth.Core/Packets/IPacketHandler.cs @@ -1,36 +1,36 @@ -// -// IPacketHandler.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.Threading; -using System.Threading.Tasks; -using NosCore.Packets.Interfaces; -using Remora.Results; - -namespace NosSmooth.Core.Packets; - -/// -/// Calls registered responders for the packet that should be handled. -/// -public interface IPacketHandler -{ - /// - /// Calls a responder for the given packet. - /// - /// The packet. - /// The string of the packet. - /// The cancellation token for cancelling the operation. - /// A result that may or may not have succeeded. - public Task HandleReceivedPacketAsync(IPacket packet, string packetString, CancellationToken ct = default); - - /// - /// Calls a responder for the given packet. - /// - /// The packet. - /// The string of the packet. - /// The cancellation token for cancelling the operation. - /// A result that may or may not have succeeded. - public Task HandleSentPacketAsync(IPacket packet, string packetString, CancellationToken ct = default); -} \ No newline at end of file +// +// IPacketHandler.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.Threading; +using System.Threading.Tasks; +using NosSmooth.Packets.Packets; +using Remora.Results; + +namespace NosSmooth.Core.Packets; + +/// +/// Calls registered responders for the packet that should be handled. +/// +public interface IPacketHandler +{ + /// + /// Calls a responder for the given packet. + /// + /// The packet. + /// The string of the packet. + /// The cancellation token for cancelling the operation. + /// A result that may or may not have succeeded. + public Task HandleReceivedPacketAsync(IPacket packet, string packetString, CancellationToken ct = default); + + /// + /// Calls a responder for the given packet. + /// + /// The packet. + /// The string of the packet. + /// The cancellation token for cancelling the operation. + /// A result that may or may not have succeeded. + public Task HandleSentPacketAsync(IPacket packet, string packetString, CancellationToken ct = default); +} diff --git a/Core/NosSmooth.Core/Packets/IPacketResponder.cs b/Core/NosSmooth.Core/Packets/IPacketResponder.cs index 2c5f744..1dad828 100644 --- a/Core/NosSmooth.Core/Packets/IPacketResponder.cs +++ b/Core/NosSmooth.Core/Packets/IPacketResponder.cs @@ -1,52 +1,52 @@ -// -// IPacketResponder.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.Threading; -using System.Threading.Tasks; -using NosCore.Packets.Interfaces; -using Remora.Results; - -namespace NosSmooth.Core.Packets; - -/// -/// Represents interface for classes that respond to packets. -/// -public interface IPacketResponder -{ -} - -/// -/// Represents interface for classes that respond to packets. -/// Responds to . -/// -/// The packet type this responder responds to. -public interface IPacketResponder : IPacketResponder - where TPacket : IPacket -{ - /// - /// Respond to the given packet. - /// - /// The packet to respond to. - /// The cancellation token for cancelling the operation. - /// A result that may or may not have succeeded. - public Task Respond(PacketEventArgs packet, CancellationToken ct = default); -} - -/// -/// Represents interface for classes that respond to every type of packets. -/// -public interface IEveryPacketResponder : IPacketResponder -{ - /// - /// Respond to the given packet. - /// - /// The packet to respond to. - /// The cancellation token for cancelling the operation. - /// The type of the packet. - /// A result that may or may not have succeeded. - public Task Respond(PacketEventArgs packet, CancellationToken ct = default) - where TPacket : IPacket; -} \ No newline at end of file +// +// IPacketResponder.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.Threading; +using System.Threading.Tasks; +using NosSmooth.Packets.Packets; +using Remora.Results; + +namespace NosSmooth.Core.Packets; + +/// +/// Represents interface for classes that respond to packets. +/// +public interface IPacketResponder +{ +} + +/// +/// Represents interface for classes that respond to packets. +/// Responds to . +/// +/// The packet type this responder responds to. +public interface IPacketResponder : IPacketResponder + where TPacket : IPacket +{ + /// + /// Respond to the given packet. + /// + /// The packet to respond to. + /// The cancellation token for cancelling the operation. + /// A result that may or may not have succeeded. + public Task Respond(PacketEventArgs packet, CancellationToken ct = default); +} + +/// +/// Represents interface for classes that respond to every type of packets. +/// +public interface IEveryPacketResponder : IPacketResponder +{ + /// + /// Respond to the given packet. + /// + /// The packet to respond to. + /// The cancellation token for cancelling the operation. + /// The type of the packet. + /// A result that may or may not have succeeded. + public Task Respond(PacketEventArgs packet, CancellationToken ct = default) + where TPacket : IPacket; +} diff --git a/Core/NosSmooth.Core/Packets/IPacketSerializer.cs b/Core/NosSmooth.Core/Packets/IPacketSerializer.cs deleted file mode 100644 index 9dc050e..0000000 --- a/Core/NosSmooth.Core/Packets/IPacketSerializer.cs +++ /dev/null @@ -1,44 +0,0 @@ -// -// IPacketSerializer.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; -using NosCore.Packets; -using NosCore.Packets.Interfaces; -using Remora.Results; - -namespace NosSmooth.Core.Packets; - -/// -/// Represents serialiazer and deserializer of . -/// -public interface IPacketSerializer -{ - /// - /// Serializes the given into string. - /// - /// The packet to serialize. - /// The serialized packet. - public Result Serialize(IPacket packet); - - /// - /// Deserializes the given string into . - /// - /// The packet to deserialize. - /// The deserialized packet. - public Result Deserialize(string packetString); - - /// - /// Gets the inner serializer from NosCore. - /// - [Obsolete("May be removed anytime.")] - public Serializer Serializer { get; } - - /// - /// Gets the inner deserializer from NosCore. - /// - [Obsolete("May be removed anytime.")] - public Deserializer Deserializer { get; } -} \ No newline at end of file diff --git a/Core/NosSmooth.Core/Packets/PacketEventArgs.cs b/Core/NosSmooth.Core/Packets/PacketEventArgs.cs index 1d0b8cd..02f35f1 100644 --- a/Core/NosSmooth.Core/Packets/PacketEventArgs.cs +++ b/Core/NosSmooth.Core/Packets/PacketEventArgs.cs @@ -4,14 +4,14 @@ // 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.Interfaces; +using NosSmooth.Packets.Attributes; namespace NosSmooth.Core.Packets; /// /// Arguments for /// -/// The type of the packet. +/// The source of the packet. /// The deserialized packet. /// The packet string. -public record PacketEventArgs(PacketType Type, TPacket Packet, string PacketString); \ No newline at end of file +public record PacketEventArgs(PacketSource Source, TPacket Packet, string PacketString); \ No newline at end of file diff --git a/Core/NosSmooth.Core/Packets/PacketHandler.cs b/Core/NosSmooth.Core/Packets/PacketHandler.cs index 775c9a8..0a0b3f0 100644 --- a/Core/NosSmooth.Core/Packets/PacketHandler.cs +++ b/Core/NosSmooth.Core/Packets/PacketHandler.cs @@ -1,104 +1,105 @@ -// -// PacketHandler.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; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using System.Threading; -using System.Threading.Tasks; -using Microsoft.Extensions.DependencyInjection; -using NosCore.Packets.Interfaces; -using Remora.Results; - -namespace NosSmooth.Core.Packets; - -/// -public class PacketHandler : IPacketHandler -{ - private readonly IServiceProvider _provider; - - /// - /// Initializes a new instance of the class. - /// - /// The dependency injection provider. - public PacketHandler(IServiceProvider provider) - { - _provider = provider; - } - - /// - public Task HandleReceivedPacketAsync(IPacket packet, string packetString, CancellationToken ct) - => HandlePacketAsync(PacketType.Received, packet, packetString, ct); - - /// - public Task HandleSentPacketAsync(IPacket packet, string packetString, CancellationToken ct) - => HandlePacketAsync(PacketType.Sent, packet, packetString, ct); - - private Task HandlePacketAsync - ( - PacketType packetType, - IPacket packet, - string packetString, - CancellationToken ct = default - ) - { - var processMethod = GetType().GetMethod - ( - nameof(DispatchResponder), - BindingFlags.NonPublic | BindingFlags.Instance - ); - - if (processMethod is null) - { - throw new InvalidOperationException("Could not find process command generic method in command processor."); - } - - var boundProcessMethod = processMethod.MakeGenericMethod(packet.GetType()); - return (Task)boundProcessMethod.Invoke(this, new object[] - { - packetType, - packet, - packetString, - ct - })!; - } - - private async Task DispatchResponder( - PacketType packetType, - TPacket packet, - string packetString, - CancellationToken ct - ) - where TPacket : class, IPacket - { - using var scope = _provider.CreateScope(); - var packetResponders = scope.ServiceProvider.GetServices>(); - var genericPacketResponders = scope.ServiceProvider.GetServices(); - - var packetEventArgs = new PacketEventArgs(packetType, packet, packetString); - var tasks = packetResponders.Select(responder => responder.Respond(packetEventArgs, ct)).ToList(); - tasks.AddRange(genericPacketResponders.Select(responder => responder.Respond(packetEventArgs, ct))); - - var results = await Task.WhenAll(tasks); - - var errors = new List(); - foreach (var result in results) - { - if (!result.IsSuccess) - { - errors.Add(result); - } - } - - return errors.Count switch - { - 0 => Result.FromSuccess(), - 1 => errors[0], - _ => new AggregateError(errors.Cast().ToArray()) - }; - } -} \ No newline at end of file +// +// PacketHandler.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; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using NosSmooth.Packets.Attributes; +using NosSmooth.Packets.Packets; +using Remora.Results; + +namespace NosSmooth.Core.Packets; + +/// +public class PacketHandler : IPacketHandler +{ + private readonly IServiceProvider _provider; + + /// + /// Initializes a new instance of the class. + /// + /// The dependency injection provider. + public PacketHandler(IServiceProvider provider) + { + _provider = provider; + } + + /// + public Task HandleReceivedPacketAsync(IPacket packet, string packetString, CancellationToken ct) + => HandlePacketAsync(PacketSource.Server, packet, packetString, ct); + + /// + public Task HandleSentPacketAsync(IPacket packet, string packetString, CancellationToken ct) + => HandlePacketAsync(PacketSource.Client, packet, packetString, ct); + + private Task HandlePacketAsync + ( + PacketSource packetType, + IPacket packet, + string packetString, + CancellationToken ct = default + ) + { + var processMethod = GetType().GetMethod + ( + nameof(DispatchResponder), + BindingFlags.NonPublic | BindingFlags.Instance + ); + + if (processMethod is null) + { + throw new InvalidOperationException("Could not find process command generic method in command processor."); + } + + var boundProcessMethod = processMethod.MakeGenericMethod(packet.GetType()); + return (Task)boundProcessMethod.Invoke(this, new object[] + { + packetType, + packet, + packetString, + ct + })!; + } + + private async Task DispatchResponder( + PacketSource packetType, + TPacket packet, + string packetString, + CancellationToken ct + ) + where TPacket : class, IPacket + { + using var scope = _provider.CreateScope(); + var packetResponders = scope.ServiceProvider.GetServices>(); + var genericPacketResponders = scope.ServiceProvider.GetServices(); + + var packetEventArgs = new PacketEventArgs(packetType, packet, packetString); + var tasks = packetResponders.Select(responder => responder.Respond(packetEventArgs, ct)).ToList(); + tasks.AddRange(genericPacketResponders.Select(responder => responder.Respond(packetEventArgs, ct))); + + var results = await Task.WhenAll(tasks); + + var errors = new List(); + foreach (var result in results) + { + if (!result.IsSuccess) + { + errors.Add(result); + } + } + + return errors.Count switch + { + 0 => Result.FromSuccess(), + 1 => errors[0], + _ => new AggregateError(errors.Cast().ToArray()) + }; + } +} diff --git a/Core/NosSmooth.Core/Packets/PacketSerializer.cs b/Core/NosSmooth.Core/Packets/PacketSerializer.cs deleted file mode 100644 index 3e2f0cb..0000000 --- a/Core/NosSmooth.Core/Packets/PacketSerializer.cs +++ /dev/null @@ -1,88 +0,0 @@ -// -// PacketSerializer.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; -using System.Collections.Generic; -using NosCore.Packets; -using NosCore.Packets.Interfaces; -using NosSmooth.Core.Packets.Converters; -using Remora.Results; - -namespace NosSmooth.Core.Packets; - -/// -public class PacketSerializer : IPacketSerializer -{ - private readonly Serializer _serializer; - private readonly Deserializer _deserializer; - private readonly IEnumerable _specificPacketSerializers; - - /// - /// Initializes a new instance of the class. - /// - /// The NosCore serializer. - /// The NosCore deserializer. - /// The specific packet serializers. - public PacketSerializer - ( - Serializer serializer, - Deserializer deserializer, - IEnumerable specificPacketSerializers - ) - { - _serializer = serializer; - _deserializer = deserializer; - _specificPacketSerializers = specificPacketSerializers; - } - - /// - public Result Serialize(IPacket packet) - { - try - { - foreach (var specificPacketSerializer in _specificPacketSerializers) - { - if (specificPacketSerializer.Serializer && specificPacketSerializer.ShouldHandle(packet)) - { - return specificPacketSerializer.Serialize(packet); - } - } - - return _serializer.Serialize(packet); - } - catch (Exception e) - { - return e; - } - } - - /// - public Result Deserialize(string packetString) - { - try - { - foreach (var specificPacketSerializer in _specificPacketSerializers) - { - if (specificPacketSerializer.Deserializer && specificPacketSerializer.ShouldHandle(packetString)) - { - return specificPacketSerializer.Deserialize(packetString); - } - } - - return Result.FromSuccess(_deserializer.Deserialize(packetString)); - } - catch (Exception e) - { - return e; - } - } - - /// - public Serializer Serializer => _serializer; - - /// - public Deserializer Deserializer => _deserializer; -} \ No newline at end of file diff --git a/Core/NosSmooth.Core/Packets/PacketSerializerProvider.cs b/Core/NosSmooth.Core/Packets/PacketSerializerProvider.cs deleted file mode 100644 index 44d6645..0000000 --- a/Core/NosSmooth.Core/Packets/PacketSerializerProvider.cs +++ /dev/null @@ -1,83 +0,0 @@ -// -// PacketSerializerProvider.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; -using System.Collections.Generic; -using Microsoft.Extensions.DependencyInjection; -using NosCore.Packets; -using NosSmooth.Core.Packets.Converters; - -namespace NosSmooth.Core.Packets; - -/// -/// Provides serializer for the server or client packets. -/// -public class PacketSerializerProvider -{ - private readonly List _clientPacketTypes; - private readonly List _serverPacketTypes; - - private IPacketSerializer? _serverSerializer; - private IPacketSerializer? _clientSerializer; - private IServiceProvider _serviceProvider; - - /// - /// Initializes a new instance of the class. - /// - /// The types of client packets. - /// The types of server packets. - /// The service provider for dependency injection. - public PacketSerializerProvider(List clientPacketTypes, List serverPacketTypes, IServiceProvider serviceProvider) - { - _clientPacketTypes = clientPacketTypes; - _serverPacketTypes = serverPacketTypes; - _serviceProvider = serviceProvider; - } - - /// - /// Gets the server serializer. - /// - public IPacketSerializer ServerSerializer - { - get - { - if (_serverSerializer is null) - { - _serverSerializer = - new PacketSerializer - ( - new Serializer(_serverPacketTypes), - new Deserializer(_serverPacketTypes), - _serviceProvider.GetServices() - ); - } - - return _serverSerializer; - } - } - - /// - /// Gets the client serializer. - /// - public IPacketSerializer ClientSerializer - { - get - { - if (_clientSerializer is null) - { - _clientSerializer = - new PacketSerializer - ( - new Serializer(_clientPacketTypes), - new Deserializer(_clientPacketTypes), - _serviceProvider.GetServices() - ); - } - - return _clientSerializer; - } - } -} \ No newline at end of file diff --git a/Core/NosSmooth.Core/Packets/PacketType.cs b/Core/NosSmooth.Core/Packets/PacketType.cs deleted file mode 100644 index afb2538..0000000 --- a/Core/NosSmooth.Core/Packets/PacketType.cs +++ /dev/null @@ -1,23 +0,0 @@ -// -// PacketType.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. - -namespace NosSmooth.Core.Packets; - -/// -/// The type of the packet. -/// -public enum PacketType -{ - /// - /// The packet was sent to the server. - /// - Sent, - - /// - /// The packet was received from the server. - /// - Received, -} \ No newline at end of file diff --git a/Core/NosSmooth.Core/Packets/ParsingFailedPacket.cs b/Core/NosSmooth.Core/Packets/ParsingFailedPacket.cs index d7b9507..2e93b55 100644 --- a/Core/NosSmooth.Core/Packets/ParsingFailedPacket.cs +++ b/Core/NosSmooth.Core/Packets/ParsingFailedPacket.cs @@ -1,37 +1,37 @@ -// -// ParsingFailedPacket.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; -using Remora.Results; - -namespace NosSmooth.Core.Packets; - -/// -/// Represents packet that failed to parse correctly. -/// -public class ParsingFailedPacket : PacketBase -{ - /// - /// Initializes a new instance of the class. - /// - /// The result from the serializer. - /// The full text of the packet. - public ParsingFailedPacket(IResult serializerResult, string packet) - { - SerializerResult = serializerResult; - Packet = packet; - } - - /// - /// Gets the result from the serializer. - /// - public IResult SerializerResult { get; } - - /// - /// Gets he full packet string. - /// - public string Packet { get; } -} \ No newline at end of file +// +// ParsingFailedPacket.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 NosSmooth.Packets.Packets; +using Remora.Results; + +namespace NosSmooth.Core.Packets; + +/// +/// Represents packet that failed to parse correctly. +/// +public class ParsingFailedPacket : IPacket +{ + /// + /// Initializes a new instance of the class. + /// + /// The result from the serializer. + /// The full text of the packet. + public ParsingFailedPacket(IResult serializerResult, string packet) + { + SerializerResult = serializerResult; + Packet = packet; + } + + /// + /// Gets the result from the serializer. + /// + public IResult SerializerResult { get; } + + /// + /// Gets he full packet string. + /// + public string Packet { get; } +} diff --git a/Core/NosSmooth.Packets/Packets/Server/Entities/InPacket.cs b/Core/NosSmooth.Packets/Packets/Server/Entities/InPacket.cs index 276717c..ccbd5f1 100644 --- a/Core/NosSmooth.Packets/Packets/Server/Entities/InPacket.cs +++ b/Core/NosSmooth.Packets/Packets/Server/Entities/InPacket.cs @@ -43,7 +43,7 @@ public record InPacket [PacketIndex(6)] short PositionY, [PacketConditionalIndex(7, "EntityType", true, EntityType.Object)] - byte Direction, + byte? Direction, [PacketConditionalIndex(8, "EntityType", false, EntityType.Player, InnerSeparator = ' ')] InPlayerSubPacket? PlayerSubPacket, [PacketConditionalIndex(9, "EntityType", false, EntityType.Object, InnerSeparator = ' ')] diff --git a/Local/NosSmooth.LocalClient/NostaleLocalClient.cs b/Local/NosSmooth.LocalClient/NostaleLocalClient.cs index 565b4c5..270bd22 100644 --- a/Local/NosSmooth.LocalClient/NostaleLocalClient.cs +++ b/Local/NosSmooth.LocalClient/NostaleLocalClient.cs @@ -1,212 +1,201 @@ -// -// NostaleLocalClient.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; -using NosSmooth.Core.Client; -using NosSmooth.Core.Commands; -using NosSmooth.Core.Extensions; -using NosSmooth.Core.Packets; -using NosSmooth.LocalClient.Hooks; -using NosSmoothCore; -using Remora.Results; - -namespace NosSmooth.LocalClient; - -/// -/// The local nostale client. -/// -/// -/// Client used for living in the same process as NostaleClientX.exe. -/// It hooks the send and receive packet methods. -/// -public class NostaleLocalClient : BaseNostaleClient -{ - private readonly PacketSerializerProvider _packetSerializerProvider; - private readonly NostaleHookManager _hookManager; - private readonly IPacketHandler _packetHandler; - private readonly ILogger _logger; - private readonly IServiceProvider _provider; - private readonly NosClient _client; - private readonly LocalClientOptions _options; - private CancellationToken? _stopRequested; - private IPacketInterceptor? _interceptor; - - /// - /// Initializes a new instance of the class. - /// - /// The command processor. - /// The packet serializer. - /// The packet serializer provider. - /// The hooking manager. - /// The packet handler. - /// The logger. - /// The options for the client. - /// The dependency injection provider. - /// The nostale managed client. - public NostaleLocalClient - ( - CommandProcessor commandProcessor, - IPacketSerializer packetSerializer, - PacketSerializerProvider packetSerializerProvider, - NostaleHookManager hookManager, - IPacketHandler packetHandler, - ILogger logger, - IOptions options, - IServiceProvider provider, - NosClient client - ) - : base(commandProcessor, packetSerializer) - { - _options = options.Value; - _packetSerializerProvider = packetSerializerProvider; - _hookManager = hookManager; - _packetHandler = packetHandler; - _logger = logger; - _provider = provider; - _client = client; - } - - /// - public override async Task RunAsync(CancellationToken stopRequested = default) - { - _stopRequested = stopRequested; - _logger.LogInformation("Starting local client"); - NetworkCallback receiveCallback = ReceiveCallback; - NetworkCallback sendCallback = SendCallback; - - if (_options.HookPacketReceive) - { - _client.GetNetwork().SetReceiveCallback(receiveCallback); - } - - if (_options.HookPacketSend) - { - _client.GetNetwork().SetSendCallback(sendCallback); - } - - if (_options.HookCharacterWalk) - { - _hookManager.HookCharacterWalk(); - } - - _logger.LogInformation("Packet methods hooked successfully"); - - try - { - await Task.Delay(-1, stopRequested); - } - catch - { - // ignored - } - - _client.ResetHooks(); - - return Result.FromSuccess(); - } - - /// - public override Task ReceivePacketAsync(string packetString, CancellationToken ct = default) - { - ReceivePacket(packetString); - return Task.FromResult(Result.FromSuccess()); - } - - /// - public override Task SendPacketAsync(string packetString, CancellationToken ct = default) - { - SendPacket(packetString); - return Task.FromResult(Result.FromSuccess()); - } - - private bool ReceiveCallback(string packet) - { - bool accepted = true; - if (_options.AllowIntercept) - { - if (_interceptor is null) - { - _interceptor = _provider.GetRequiredService(); - } - - accepted = _interceptor.InterceptReceive(ref packet); - } - - Task.Run(async () => await ProcessPacketAsync(PacketType.Received, packet)); - - return accepted; - } - - private bool SendCallback(string packet) - { - bool accepted = true; - if (_options.AllowIntercept) - { - if (_interceptor is null) - { - _interceptor = _provider.GetRequiredService(); - } - - accepted = _interceptor.InterceptSend(ref packet); - } - - Task.Run(async () => await ProcessPacketAsync(PacketType.Sent, packet)); - - return accepted; - } - - private void SendPacket(string packetString) - { - _client.GetNetwork().SendPacket(packetString); - _logger.LogDebug($"Sending client packet {packetString}"); - } - - private void ReceivePacket(string packetString) - { - _client.GetNetwork().ReceivePacket(packetString); - _logger.LogDebug($"Receiving client packet {packetString}"); - } - - private async Task ProcessPacketAsync(PacketType type, string packetString) - { - IPacketSerializer serializer; - if (type == PacketType.Received) - { - serializer = _packetSerializerProvider.ServerSerializer; - } - else - { - serializer = _packetSerializerProvider.ClientSerializer; - } - - var packet = serializer.Deserialize(packetString); - if (!packet.IsSuccess) - { - _logger.LogWarning("Could not parse {Packet}. Reason:", packetString); - _logger.LogResultError(packet); - packet = new ParsingFailedPacket(packet, packetString); - } - - Result result; - if (type == PacketType.Received) - { - result = await _packetHandler.HandleReceivedPacketAsync(packet.Entity, packetString, _stopRequested ?? default); - } - else - { - result = await _packetHandler.HandleSentPacketAsync(packet.Entity, packetString, _stopRequested ?? default); - } - - if (!result.IsSuccess) - { - _logger.LogError("There was an error whilst handling packet"); - _logger.LogResultError(result); - } - } -} \ No newline at end of file +// +// NostaleLocalClient.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 NosSmooth.Core.Client; +using NosSmooth.Core.Commands; +using NosSmooth.Core.Extensions; +using NosSmooth.Core.Packets; +using NosSmooth.LocalClient.Hooks; +using NosSmooth.Packets; +using NosSmooth.Packets.Attributes; +using NosSmoothCore; +using Remora.Results; + +namespace NosSmooth.LocalClient; + +/// +/// The local nostale client. +/// +/// +/// Client used for living in the same process as NostaleClientX.exe. +/// It hooks the send and receive packet methods. +/// +public class NostaleLocalClient : BaseNostaleClient +{ + private readonly IPacketSerializer _packetSerializer; + private readonly NostaleHookManager _hookManager; + private readonly IPacketHandler _packetHandler; + private readonly ILogger _logger; + private readonly IServiceProvider _provider; + private readonly NosClient _client; + private readonly LocalClientOptions _options; + private CancellationToken? _stopRequested; + private IPacketInterceptor? _interceptor; + + /// + /// Initializes a new instance of the class. + /// + /// The command processor. + /// The packet serializer. + /// The hooking manager. + /// The packet handler. + /// The logger. + /// The options for the client. + /// The dependency injection provider. + /// The nostale managed client. + public NostaleLocalClient + ( + CommandProcessor commandProcessor, + IPacketSerializer packetSerializer, + NostaleHookManager hookManager, + IPacketHandler packetHandler, + ILogger logger, + IOptions options, + IServiceProvider provider, + NosClient client + ) + : base(commandProcessor, packetSerializer) + { + _options = options.Value; + _packetSerializer = packetSerializer; + _hookManager = hookManager; + _packetHandler = packetHandler; + _logger = logger; + _provider = provider; + _client = client; + } + + /// + public override async Task RunAsync(CancellationToken stopRequested = default) + { + _stopRequested = stopRequested; + _logger.LogInformation("Starting local client"); + NetworkCallback receiveCallback = ReceiveCallback; + NetworkCallback sendCallback = SendCallback; + + if (_options.HookPacketReceive) + { + _client.GetNetwork().SetReceiveCallback(receiveCallback); + } + + if (_options.HookPacketSend) + { + _client.GetNetwork().SetSendCallback(sendCallback); + } + + if (_options.HookCharacterWalk) + { + _hookManager.HookCharacterWalk(); + } + + _logger.LogInformation("Packet methods hooked successfully"); + + try + { + await Task.Delay(-1, stopRequested); + } + catch + { + // ignored + } + + _client.ResetHooks(); + + return Result.FromSuccess(); + } + + /// + public override Task ReceivePacketAsync(string packetString, CancellationToken ct = default) + { + ReceivePacket(packetString); + return Task.FromResult(Result.FromSuccess()); + } + + /// + public override Task SendPacketAsync(string packetString, CancellationToken ct = default) + { + SendPacket(packetString); + return Task.FromResult(Result.FromSuccess()); + } + + private bool ReceiveCallback(string packet) + { + bool accepted = true; + if (_options.AllowIntercept) + { + if (_interceptor is null) + { + _interceptor = _provider.GetRequiredService(); + } + + accepted = _interceptor.InterceptReceive(ref packet); + } + + Task.Run(async () => await ProcessPacketAsync(PacketSource.Server, packet)); + + return accepted; + } + + private bool SendCallback(string packet) + { + bool accepted = true; + if (_options.AllowIntercept) + { + if (_interceptor is null) + { + _interceptor = _provider.GetRequiredService(); + } + + accepted = _interceptor.InterceptSend(ref packet); + } + + Task.Run(async () => await ProcessPacketAsync(PacketSource.Client, packet)); + + return accepted; + } + + private void SendPacket(string packetString) + { + _client.GetNetwork().SendPacket(packetString); + _logger.LogDebug($"Sending client packet {packetString}"); + } + + private void ReceivePacket(string packetString) + { + _client.GetNetwork().ReceivePacket(packetString); + _logger.LogDebug($"Receiving client packet {packetString}"); + } + + private async Task ProcessPacketAsync(PacketSource type, string packetString) + { + var packet = _packetSerializer.Deserialize(packetString, type); + if (!packet.IsSuccess) + { + _logger.LogWarning("Could not parse {Packet}. Reason:", packetString); + _logger.LogResultError(packet); + packet = new ParsingFailedPacket(packet, packetString); + } + + Result result; + if (type == PacketSource.Server) + { + result = await _packetHandler.HandleReceivedPacketAsync(packet.Entity, packetString, _stopRequested ?? default); + } + else + { + result = await _packetHandler.HandleSentPacketAsync(packet.Entity, packetString, _stopRequested ?? default); + } + + if (!result.IsSuccess) + { + _logger.LogError("There was an error whilst handling packet"); + _logger.LogResultError(result); + } + } +} diff --git a/NosSmooth.Unix.sln b/NosSmooth.Unix.sln index 7044e43..e7ba39b 100644 --- a/NosSmooth.Unix.sln +++ b/NosSmooth.Unix.sln @@ -19,10 +19,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NosSmooth.Extensions", "Cor EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Core", "Core", "{01B5E872-271F-4D30-A1AA-AD48D81840C5}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NosCore.Packets", "libs\NosCore.Packets\src\NosCore.Packets\NosCore.Packets.csproj", "{27DF38DF-AC58-4039-A91C-824D829ECECD}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NosCore.Shared", "libs\NosCore.Shared\src\NosCore.Shared\NosCore.Shared.csproj", "{945E9248-C150-4617-AB0F-1450561859E3}" -EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".metadata", ".metadata", "{FA63BCED-9D81-4FF7-BA75-A6F3BA31ECDE}" ProjectSection(SolutionItems) = preProject Directory.Build.props = Directory.Build.props @@ -32,10 +28,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".metadata", ".metadata", "{ EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{C6A8760D-92CB-4307-88A7-36CCAEBA4AD1}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NosCore.Packets.Tests", "libs\NosCore.Packets\test\NosCore.Packets.Tests\NosCore.Packets.Tests.csproj", "{726188BA-F0EA-4ECA-ACF4-CCC066464FF0}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NosSmooth.Core.Tests", "Tests\NosSmooth.Core.Tests\NosSmooth.Core.Tests.csproj", "{1A10C624-48E5-425D-938E-31A4CC7AC687}" -EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NosSmooth.Packets", "Core\NosSmooth.Packets\NosSmooth.Packets.csproj", "{93C2E14D-2E3F-426C-9AEF-8D0E8CA21BEB}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NosSmooth.Packets.Tests", "Tests\NosSmooth.Packets.Tests\NosSmooth.Packets.Tests.csproj", "{CE7EE14A-BCC5-4A8B-8D9F-403BF09BD99A}" @@ -112,54 +104,6 @@ Global {B6C23EA5-55FE-436A-96EA-FE2974C5881D}.Release|x64.Build.0 = Release|Any CPU {B6C23EA5-55FE-436A-96EA-FE2974C5881D}.Release|x86.ActiveCfg = Release|Any CPU {B6C23EA5-55FE-436A-96EA-FE2974C5881D}.Release|x86.Build.0 = Release|Any CPU - {27DF38DF-AC58-4039-A91C-824D829ECECD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {27DF38DF-AC58-4039-A91C-824D829ECECD}.Debug|Any CPU.Build.0 = Debug|Any CPU - {27DF38DF-AC58-4039-A91C-824D829ECECD}.Debug|x64.ActiveCfg = Debug|Any CPU - {27DF38DF-AC58-4039-A91C-824D829ECECD}.Debug|x64.Build.0 = Debug|Any CPU - {27DF38DF-AC58-4039-A91C-824D829ECECD}.Debug|x86.ActiveCfg = Debug|Any CPU - {27DF38DF-AC58-4039-A91C-824D829ECECD}.Debug|x86.Build.0 = Debug|Any CPU - {27DF38DF-AC58-4039-A91C-824D829ECECD}.Release|Any CPU.ActiveCfg = Release|Any CPU - {27DF38DF-AC58-4039-A91C-824D829ECECD}.Release|Any CPU.Build.0 = Release|Any CPU - {27DF38DF-AC58-4039-A91C-824D829ECECD}.Release|x64.ActiveCfg = Release|Any CPU - {27DF38DF-AC58-4039-A91C-824D829ECECD}.Release|x64.Build.0 = Release|Any CPU - {27DF38DF-AC58-4039-A91C-824D829ECECD}.Release|x86.ActiveCfg = Release|Any CPU - {27DF38DF-AC58-4039-A91C-824D829ECECD}.Release|x86.Build.0 = Release|Any CPU - {945E9248-C150-4617-AB0F-1450561859E3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {945E9248-C150-4617-AB0F-1450561859E3}.Debug|Any CPU.Build.0 = Debug|Any CPU - {945E9248-C150-4617-AB0F-1450561859E3}.Debug|x64.ActiveCfg = Debug|Any CPU - {945E9248-C150-4617-AB0F-1450561859E3}.Debug|x64.Build.0 = Debug|Any CPU - {945E9248-C150-4617-AB0F-1450561859E3}.Debug|x86.ActiveCfg = Debug|Any CPU - {945E9248-C150-4617-AB0F-1450561859E3}.Debug|x86.Build.0 = Debug|Any CPU - {945E9248-C150-4617-AB0F-1450561859E3}.Release|Any CPU.ActiveCfg = Release|Any CPU - {945E9248-C150-4617-AB0F-1450561859E3}.Release|Any CPU.Build.0 = Release|Any CPU - {945E9248-C150-4617-AB0F-1450561859E3}.Release|x64.ActiveCfg = Release|Any CPU - {945E9248-C150-4617-AB0F-1450561859E3}.Release|x64.Build.0 = Release|Any CPU - {945E9248-C150-4617-AB0F-1450561859E3}.Release|x86.ActiveCfg = Release|Any CPU - {945E9248-C150-4617-AB0F-1450561859E3}.Release|x86.Build.0 = Release|Any CPU - {726188BA-F0EA-4ECA-ACF4-CCC066464FF0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {726188BA-F0EA-4ECA-ACF4-CCC066464FF0}.Debug|Any CPU.Build.0 = Debug|Any CPU - {726188BA-F0EA-4ECA-ACF4-CCC066464FF0}.Debug|x64.ActiveCfg = Debug|Any CPU - {726188BA-F0EA-4ECA-ACF4-CCC066464FF0}.Debug|x64.Build.0 = Debug|Any CPU - {726188BA-F0EA-4ECA-ACF4-CCC066464FF0}.Debug|x86.ActiveCfg = Debug|Any CPU - {726188BA-F0EA-4ECA-ACF4-CCC066464FF0}.Debug|x86.Build.0 = Debug|Any CPU - {726188BA-F0EA-4ECA-ACF4-CCC066464FF0}.Release|Any CPU.ActiveCfg = Release|Any CPU - {726188BA-F0EA-4ECA-ACF4-CCC066464FF0}.Release|Any CPU.Build.0 = Release|Any CPU - {726188BA-F0EA-4ECA-ACF4-CCC066464FF0}.Release|x64.ActiveCfg = Release|Any CPU - {726188BA-F0EA-4ECA-ACF4-CCC066464FF0}.Release|x64.Build.0 = Release|Any CPU - {726188BA-F0EA-4ECA-ACF4-CCC066464FF0}.Release|x86.ActiveCfg = Release|Any CPU - {726188BA-F0EA-4ECA-ACF4-CCC066464FF0}.Release|x86.Build.0 = Release|Any CPU - {1A10C624-48E5-425D-938E-31A4CC7AC687}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {1A10C624-48E5-425D-938E-31A4CC7AC687}.Debug|Any CPU.Build.0 = Debug|Any CPU - {1A10C624-48E5-425D-938E-31A4CC7AC687}.Debug|x64.ActiveCfg = Debug|Any CPU - {1A10C624-48E5-425D-938E-31A4CC7AC687}.Debug|x64.Build.0 = Debug|Any CPU - {1A10C624-48E5-425D-938E-31A4CC7AC687}.Debug|x86.ActiveCfg = Debug|Any CPU - {1A10C624-48E5-425D-938E-31A4CC7AC687}.Debug|x86.Build.0 = Debug|Any CPU - {1A10C624-48E5-425D-938E-31A4CC7AC687}.Release|Any CPU.ActiveCfg = Release|Any CPU - {1A10C624-48E5-425D-938E-31A4CC7AC687}.Release|Any CPU.Build.0 = Release|Any CPU - {1A10C624-48E5-425D-938E-31A4CC7AC687}.Release|x64.ActiveCfg = Release|Any CPU - {1A10C624-48E5-425D-938E-31A4CC7AC687}.Release|x64.Build.0 = Release|Any CPU - {1A10C624-48E5-425D-938E-31A4CC7AC687}.Release|x86.ActiveCfg = Release|Any CPU - {1A10C624-48E5-425D-938E-31A4CC7AC687}.Release|x86.Build.0 = Release|Any CPU {93C2E14D-2E3F-426C-9AEF-8D0E8CA21BEB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {93C2E14D-2E3F-426C-9AEF-8D0E8CA21BEB}.Debug|Any CPU.Build.0 = Debug|Any CPU {93C2E14D-2E3F-426C-9AEF-8D0E8CA21BEB}.Debug|x64.ActiveCfg = Debug|Any CPU @@ -205,10 +149,6 @@ Global {19666500-4636-4400-8855-496317F4A7F7} = {01B5E872-271F-4D30-A1AA-AD48D81840C5} {05E3039D-EDF6-4CDC-B062-CB67760ACB5F} = {F9EFA63C-0A88-45EB-B36F-C4A9D63BDFD1} {06F03B1F-F68A-4C5B-B7FE-128A7CE1A1D7} = {01B5E872-271F-4D30-A1AA-AD48D81840C5} - {27DF38DF-AC58-4039-A91C-824D829ECECD} = {01B5E872-271F-4D30-A1AA-AD48D81840C5} - {945E9248-C150-4617-AB0F-1450561859E3} = {01B5E872-271F-4D30-A1AA-AD48D81840C5} - {726188BA-F0EA-4ECA-ACF4-CCC066464FF0} = {C6A8760D-92CB-4307-88A7-36CCAEBA4AD1} - {1A10C624-48E5-425D-938E-31A4CC7AC687} = {C6A8760D-92CB-4307-88A7-36CCAEBA4AD1} {93C2E14D-2E3F-426C-9AEF-8D0E8CA21BEB} = {01B5E872-271F-4D30-A1AA-AD48D81840C5} {CE7EE14A-BCC5-4A8B-8D9F-403BF09BD99A} = {C6A8760D-92CB-4307-88A7-36CCAEBA4AD1} {854A9D1D-5A7D-494E-B1D9-021A1C085F58} = {01B5E872-271F-4D30-A1AA-AD48D81840C5} diff --git a/NosSmooth.sln b/NosSmooth.sln index 4ec92d1..d8acb3c 100644 --- a/NosSmooth.sln +++ b/NosSmooth.sln @@ -25,10 +25,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NosSmooth.Extensions", "Cor EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Core", "Core", "{01B5E872-271F-4D30-A1AA-AD48D81840C5}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NosCore.Packets", "libs\NosCore.Packets\src\NosCore.Packets\NosCore.Packets.csproj", "{27DF38DF-AC58-4039-A91C-824D829ECECD}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NosCore.Shared", "libs\NosCore.Shared\src\NosCore.Shared\NosCore.Shared.csproj", "{945E9248-C150-4617-AB0F-1450561859E3}" -EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".metadata", ".metadata", "{FA63BCED-9D81-4FF7-BA75-A6F3BA31ECDE}" ProjectSection(SolutionItems) = preProject Directory.build.props = Directory.build.props @@ -48,6 +44,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WalkCommands", "Samples\Wal EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NosSmooth.Core.Tests", "Tests\NosSmooth.Core.Tests\NosSmooth.Core.Tests.csproj", "{1A10C624-48E5-425D-938E-31A4CC7AC687}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NosSmooth.Packets", "Core\NosSmooth.Packets\NosSmooth.Packets.csproj", "{C4DAFD83-C6DC-4597-AA1F-BA2F3ABB612C}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NosSmooth.PacketSerializersGenerator", "Core\NosSmooth.PacketSerializersGenerator\NosSmooth.PacketSerializersGenerator.csproj", "{C61EBDB6-053C-48C3-B896-58642639C93F}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -140,30 +140,6 @@ Global {B6C23EA5-55FE-436A-96EA-FE2974C5881D}.Release|x64.Build.0 = Release|Any CPU {B6C23EA5-55FE-436A-96EA-FE2974C5881D}.Release|x86.ActiveCfg = Release|Any CPU {B6C23EA5-55FE-436A-96EA-FE2974C5881D}.Release|x86.Build.0 = Release|Any CPU - {27DF38DF-AC58-4039-A91C-824D829ECECD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {27DF38DF-AC58-4039-A91C-824D829ECECD}.Debug|Any CPU.Build.0 = Debug|Any CPU - {27DF38DF-AC58-4039-A91C-824D829ECECD}.Debug|x64.ActiveCfg = Debug|Any CPU - {27DF38DF-AC58-4039-A91C-824D829ECECD}.Debug|x64.Build.0 = Debug|Any CPU - {27DF38DF-AC58-4039-A91C-824D829ECECD}.Debug|x86.ActiveCfg = Debug|Any CPU - {27DF38DF-AC58-4039-A91C-824D829ECECD}.Debug|x86.Build.0 = Debug|Any CPU - {27DF38DF-AC58-4039-A91C-824D829ECECD}.Release|Any CPU.ActiveCfg = Release|Any CPU - {27DF38DF-AC58-4039-A91C-824D829ECECD}.Release|Any CPU.Build.0 = Release|Any CPU - {27DF38DF-AC58-4039-A91C-824D829ECECD}.Release|x64.ActiveCfg = Release|Any CPU - {27DF38DF-AC58-4039-A91C-824D829ECECD}.Release|x64.Build.0 = Release|Any CPU - {27DF38DF-AC58-4039-A91C-824D829ECECD}.Release|x86.ActiveCfg = Release|Any CPU - {27DF38DF-AC58-4039-A91C-824D829ECECD}.Release|x86.Build.0 = Release|Any CPU - {945E9248-C150-4617-AB0F-1450561859E3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {945E9248-C150-4617-AB0F-1450561859E3}.Debug|Any CPU.Build.0 = Debug|Any CPU - {945E9248-C150-4617-AB0F-1450561859E3}.Debug|x64.ActiveCfg = Debug|Any CPU - {945E9248-C150-4617-AB0F-1450561859E3}.Debug|x64.Build.0 = Debug|Any CPU - {945E9248-C150-4617-AB0F-1450561859E3}.Debug|x86.ActiveCfg = Debug|Any CPU - {945E9248-C150-4617-AB0F-1450561859E3}.Debug|x86.Build.0 = Debug|Any CPU - {945E9248-C150-4617-AB0F-1450561859E3}.Release|Any CPU.ActiveCfg = Release|Any CPU - {945E9248-C150-4617-AB0F-1450561859E3}.Release|Any CPU.Build.0 = Release|Any CPU - {945E9248-C150-4617-AB0F-1450561859E3}.Release|x64.ActiveCfg = Release|Any CPU - {945E9248-C150-4617-AB0F-1450561859E3}.Release|x64.Build.0 = Release|Any CPU - {945E9248-C150-4617-AB0F-1450561859E3}.Release|x86.ActiveCfg = Release|Any CPU - {945E9248-C150-4617-AB0F-1450561859E3}.Release|x86.Build.0 = Release|Any CPU {4017A4F4-5E59-48AA-A7D0-A8518148933A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {4017A4F4-5E59-48AA-A7D0-A8518148933A}.Debug|Any CPU.Build.0 = Debug|Any CPU {4017A4F4-5E59-48AA-A7D0-A8518148933A}.Debug|x64.ActiveCfg = Debug|Any CPU @@ -224,6 +200,30 @@ Global {1A10C624-48E5-425D-938E-31A4CC7AC687}.Release|x64.Build.0 = Release|Any CPU {1A10C624-48E5-425D-938E-31A4CC7AC687}.Release|x86.ActiveCfg = Release|Any CPU {1A10C624-48E5-425D-938E-31A4CC7AC687}.Release|x86.Build.0 = Release|Any CPU + {C4DAFD83-C6DC-4597-AA1F-BA2F3ABB612C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C4DAFD83-C6DC-4597-AA1F-BA2F3ABB612C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C4DAFD83-C6DC-4597-AA1F-BA2F3ABB612C}.Debug|x64.ActiveCfg = Debug|Any CPU + {C4DAFD83-C6DC-4597-AA1F-BA2F3ABB612C}.Debug|x64.Build.0 = Debug|Any CPU + {C4DAFD83-C6DC-4597-AA1F-BA2F3ABB612C}.Debug|x86.ActiveCfg = Debug|Any CPU + {C4DAFD83-C6DC-4597-AA1F-BA2F3ABB612C}.Debug|x86.Build.0 = Debug|Any CPU + {C4DAFD83-C6DC-4597-AA1F-BA2F3ABB612C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C4DAFD83-C6DC-4597-AA1F-BA2F3ABB612C}.Release|Any CPU.Build.0 = Release|Any CPU + {C4DAFD83-C6DC-4597-AA1F-BA2F3ABB612C}.Release|x64.ActiveCfg = Release|Any CPU + {C4DAFD83-C6DC-4597-AA1F-BA2F3ABB612C}.Release|x64.Build.0 = Release|Any CPU + {C4DAFD83-C6DC-4597-AA1F-BA2F3ABB612C}.Release|x86.ActiveCfg = Release|Any CPU + {C4DAFD83-C6DC-4597-AA1F-BA2F3ABB612C}.Release|x86.Build.0 = Release|Any CPU + {C61EBDB6-053C-48C3-B896-58642639C93F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C61EBDB6-053C-48C3-B896-58642639C93F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C61EBDB6-053C-48C3-B896-58642639C93F}.Debug|x64.ActiveCfg = Debug|Any CPU + {C61EBDB6-053C-48C3-B896-58642639C93F}.Debug|x64.Build.0 = Debug|Any CPU + {C61EBDB6-053C-48C3-B896-58642639C93F}.Debug|x86.ActiveCfg = Debug|Any CPU + {C61EBDB6-053C-48C3-B896-58642639C93F}.Debug|x86.Build.0 = Debug|Any CPU + {C61EBDB6-053C-48C3-B896-58642639C93F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C61EBDB6-053C-48C3-B896-58642639C93F}.Release|Any CPU.Build.0 = Release|Any CPU + {C61EBDB6-053C-48C3-B896-58642639C93F}.Release|x64.ActiveCfg = Release|Any CPU + {C61EBDB6-053C-48C3-B896-58642639C93F}.Release|x64.Build.0 = Release|Any CPU + {C61EBDB6-053C-48C3-B896-58642639C93F}.Release|x86.ActiveCfg = Release|Any CPU + {C61EBDB6-053C-48C3-B896-58642639C93F}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -235,13 +235,13 @@ Global {05E3039D-EDF6-4CDC-B062-CB67760ACB5F} = {F9EFA63C-0A88-45EB-B36F-C4A9D63BDFD1} {06F03B1F-F68A-4C5B-B7FE-128A7CE1A1D7} = {01B5E872-271F-4D30-A1AA-AD48D81840C5} {63E97FF3-7E40-44DE-9E91-F5DEE79AF95F} = {6078AE6E-7CD0-48E4-84E0-EB164D8881DA} - {27DF38DF-AC58-4039-A91C-824D829ECECD} = {01B5E872-271F-4D30-A1AA-AD48D81840C5} - {945E9248-C150-4617-AB0F-1450561859E3} = {01B5E872-271F-4D30-A1AA-AD48D81840C5} {726188BA-F0EA-4ECA-ACF4-CCC066464FF0} = {C6A8760D-92CB-4307-88A7-36CCAEBA4AD1} {9025731C-084E-4E82-8CD4-0F52D3AA1F54} = {F20FE754-FDEA-4F3A-93D4-0750CB9EBB33} {F96F3AA0-131E-4B6B-AB21-BBE2DEBCEF3A} = {9025731C-084E-4E82-8CD4-0F52D3AA1F54} {4017A4F4-5E59-48AA-A7D0-A8518148933A} = {9025731C-084E-4E82-8CD4-0F52D3AA1F54} {1A10C624-48E5-425D-938E-31A4CC7AC687} = {C6A8760D-92CB-4307-88A7-36CCAEBA4AD1} + {C4DAFD83-C6DC-4597-AA1F-BA2F3ABB612C} = {01B5E872-271F-4D30-A1AA-AD48D81840C5} + {C61EBDB6-053C-48C3-B896-58642639C93F} = {01B5E872-271F-4D30-A1AA-AD48D81840C5} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {C5F46653-4DEC-429B-8580-4ED18ED9B4CA} diff --git a/Tests/NosSmooth.Core.Tests/Packets/InPacketSerializerTest.cs b/Tests/NosSmooth.Core.Tests/Packets/InPacketSerializerTest.cs index d66baac..5f28270 100644 --- a/Tests/NosSmooth.Core.Tests/Packets/InPacketSerializerTest.cs +++ b/Tests/NosSmooth.Core.Tests/Packets/InPacketSerializerTest.cs @@ -1,158 +1 @@ -// -// InPacketSerializerTest.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; -using System.Collections.Generic; -using System.Threading.Tasks; -using Microsoft.Extensions.DependencyInjection; -using NosCore.Packets.ServerPackets.Inventory; -using NosCore.Packets.ServerPackets.Visibility; -using NosCore.Shared.Enumerations; -using NosSmooth.Core.Packets; -using NosSmooth.Core.Packets.Converters; -using Xunit; - -namespace NosSmooth.Core.Tests.Packets; - -/// -/// Test class for . -/// -public class InPacketSerializerTest -{ - private readonly InPacketSerializer _inPacketSerializer; - - /// - /// Initializes a new instance of the class. - /// - public InPacketSerializerTest() - { - var types = new List(new[] - { - typeof(InPacket), - typeof(InAliveSubPacket), - typeof(InNonPlayerSubPacket), - typeof(InCharacterSubPacket), - typeof(InItemSubPacket), - typeof(InEquipmentSubPacket), - typeof(UpgradeRareSubPacket), - typeof(FamilySubPacket), - }); - - _inPacketSerializer = new ServiceCollection() - .AddSingleton( - p => new PacketSerializerProvider(types, types, p) - ) - .AddSingleton() - .BuildServiceProvider() - .GetRequiredService(); - } - - /// - /// Tests whether the serializer accepts to handle in packet. - /// - [Fact] - public void AcceptsInPacket() - { - var shouldHandle = _inPacketSerializer.ShouldHandle( - "in 1 dfrfgh - 1 79 2 6 2 1 0 106 2 -1.4480.4452.4468.4840.4132.-1.-1.-1.-1 100 100 0 -1 4 4 0 43 0 0 108 108 -1 - 26 0 0 0 0 99 0 0|0|0 0 0 10 80 0"); - Assert.True(shouldHandle); - } - - /// - /// Tests whether the serializer doesnt accept to handle non in packet. - /// - [Fact] - public void DoesntAcceptNonInPacket() - { - var shouldHandle = _inPacketSerializer.ShouldHandle( - "sr 5"); - Assert.False(shouldHandle); - } - - /// - /// Tests whether the result is successful when serializing player in packet. - /// - [Fact] - public void SucceedsDeserializingPlayerIn() - { - var result = _inPacketSerializer.Deserialize( - "in 1 dfrfgh - 1 79 2 6 2 1 0 106 2 -1.4480.4452.4468.4840.4132.-1.-1.-1.-1 50 98 0 -1 4 4 0 43 0 0 108 108 -1 - 26 0 0 0 0 99 0 0|0|0 0 0 10 80 0" - ); - Assert.True(result.IsSuccess); - } - - /// - /// Tests whether the result is successful when serializing monster in packet. - /// - [Fact] - public void SucceedsDeserializingMonsterIn() - { - var result = _inPacketSerializer.Deserialize( - "in 2 334 1992 134 112 2 100 100 0 0 0 -1 1 0 -1 - 0 -1 0 0 0 0 0 0 0 0 0 0" - ); - Assert.True(result.IsSuccess); - } - - /// - /// Tests whether the result of deserializing player is correct. - /// - [Fact] - public void DeserializesPlayerInCorrectly() - { - var result = _inPacketSerializer.Deserialize( - "in 1 dfrfgh - 55 79 2 6 2 1 0 106 2 -1.4480.4452.4468.4840.4132.-1.-1.-1.-1 50 95 0 -1 4 4 0 43 0 0 108 108 -1 - 26 0 0 0 0 99 0 0|0|0 0 0 10 80 0" - ); // 55 is id, 50 hp, 95 mp - - Assert.True(result.IsSuccess); - var inPacket = result.Entity; - - Assert.Equal(VisualType.Player, inPacket.VisualType); - Assert.NotNull(inPacket.Name); - Assert.Matches("dfrfgh", inPacket.Name); - Assert.Equal(55, inPacket.VisualId); - Assert.Equal(79, inPacket.PositionX); - Assert.Equal(2, inPacket.PositionY); - Assert.NotNull(inPacket.Direction); - Assert.Equal(6, (byte)inPacket.Direction!); - Assert.NotNull(inPacket.InCharacterSubPacket); - var characterSubPacket = inPacket.InCharacterSubPacket!; - Assert.Equal(AuthorityType.GameMaster, characterSubPacket.Authority); - Assert.Equal(CharacterClassType.Archer, characterSubPacket.Class); - Assert.NotNull(characterSubPacket.InAliveSubPacket); - Assert.Equal(50, characterSubPacket.InAliveSubPacket!.Hp); - Assert.Equal(95, characterSubPacket.InAliveSubPacket!.Mp); - - // TODO: check other things - } - - /// - /// Tests whether the result of deserializing monster is correct. - /// - [Fact] - public void DeserializesMonsterInCorrectly() - { - var result = _inPacketSerializer.Deserialize( - "in 2 334 1992 134 112 2 100 80 0 0 0 -1 1 0 -1 - 0 -1 0 0 0 0 0 0 0 0 0 0" - ); - Assert.True(result.IsSuccess); - var inPacket = result.Entity; - Assert.Equal(VisualType.Npc, inPacket.VisualType); - Assert.NotNull(inPacket.VNum); - Assert.Equal(334, inPacket.VNum!.Value); - Assert.Equal(1992, inPacket.VisualId); - Assert.Equal(134, inPacket.PositionX); - Assert.Equal(112, inPacket.PositionY); - Assert.NotNull(inPacket.Direction); - Assert.Equal(2, (byte)inPacket.Direction!); - Assert.NotNull(inPacket.InNonPlayerSubPacket); - var nonPlayerSubPacket = inPacket.InNonPlayerSubPacket!; - Assert.NotNull(nonPlayerSubPacket.InAliveSubPacket); - Assert.Equal(100, nonPlayerSubPacket.InAliveSubPacket!.Hp); - Assert.Equal(80, nonPlayerSubPacket.InAliveSubPacket!.Mp); - - // TODO: check other things - } -} \ No newline at end of file + \ No newline at end of file diff --git a/libs/Directory.Build.props b/libs/Directory.Build.props deleted file mode 100644 index b9b7c8a..0000000 --- a/libs/Directory.Build.props +++ /dev/null @@ -1,3 +0,0 @@ - - - diff --git a/libs/NosCore.Packets b/libs/NosCore.Packets deleted file mode 160000 index ccb4322..0000000 --- a/libs/NosCore.Packets +++ /dev/null @@ -1 +0,0 @@ -Subproject commit ccb432224367fa76403b794d4e5988b241ef19b6 diff --git a/libs/NosCore.Shared b/libs/NosCore.Shared deleted file mode 160000 index 05c3880..0000000 --- a/libs/NosCore.Shared +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 05c3880af07154903c294a9cf715025b8649ad85 -- 2.48.1