// // NameChangeInterceptor.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.Logging; using NosCore.Packets.Enumerations; using NosCore.Packets.ServerPackets.Chats; using NosSmooth.Core.Client; using NosSmooth.LocalClient; namespace InterceptNameChanger { /// /// Intercepts the packets so name in c_info may be replaced. /// public class NameChangeInterceptor : IPacketInterceptor { private readonly INostaleClient _client; private readonly ILogger _logger; private string _name = "Intercept"; /// /// Initializes a new instance of the class. /// /// The nostale client. /// The logger. public NameChangeInterceptor(INostaleClient client, ILogger logger) { _client = client; _logger = logger; } /// public bool InterceptSend(ref string packet) { if (packet.StartsWith("say #")) { _name = packet.Substring(5).Replace(" ", "⠀"); // Mind the symbols! _logger.LogInformation("Name changed to {Name}", _name); _client.ReceivePacketAsync(new SayPacket() { Message = $"Name changed to {_name}, change map for it to take effect.", Type = SayColorType.Red }).GetAwaiter().GetResult(); return false; } return true; // Accept the packet } /// public bool InterceptReceive(ref string packet) { if (packet.StartsWith("c_info")) { var oldPart = packet.Substring(packet.IndexOf(' ', 7)); var result = _client.ReceivePacketAsync($"c_info {_name} " + oldPart) .GetAwaiter().GetResult(); // Change the name if (!result.IsSuccess) { _logger.LogError("Could not send the c_info packet: {Reason}", result.Error.Message); return true; // Accept the packet so client is not confused } return false; // Reject the packet } return true; // Accept the packet } } }