~ruther/NosSmooth

ref: a7292e30b2ae3dad46fff315cfbddfd8b042686c NosSmooth/Samples/InterceptNameChanger/NameChangeInterceptor.cs -rw-r--r-- 2.8 KiB
a7292e30 — Rutherther feat(samples): allow passing pet selectors to walk command 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//
//  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 NosSmooth.Core.Client;
using NosSmooth.LocalClient;
using NosSmooth.Packets.Enums;
using NosSmooth.Packets.Enums.Chat;
using NosSmooth.Packets.Server.Chat;

namespace InterceptNameChanger
{
    /// <summary>
    /// Intercepts the packets so name in c_info may be replaced.
    /// </summary>
    public class NameChangeInterceptor : IPacketInterceptor
    {
        private readonly INostaleClient _client;
        private readonly ILogger<NameChangeInterceptor> _logger;
        private string _name = "Intercept";

        /// <summary>
        /// Initializes a new instance of the <see cref="NameChangeInterceptor"/> class.
        /// </summary>
        /// <param name="client">The nostale client.</param>
        /// <param name="logger">The logger.</param>
        public NameChangeInterceptor(INostaleClient client, ILogger<NameChangeInterceptor> logger)
        {
            _client = client;
            _logger = logger;
        }

        /// <inheritdoc/>
        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
                        (
                            EntityType.Map,
                            1,
                            SayColor.Red,
                            $"Name changed to {_name}, change map for it to take effect."
                        )
                    )
                    .GetAwaiter()
                    .GetResult();
                return false;
            }

            return true; // Accept the packet
        }

        /// <inheritdoc/>
        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
        }
    }
}
Do not follow this link