File size: 4,558 Bytes
27123f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
namespace SilkroadBot.Core.Networking;

/// <summary>
/// Represents a Silkroad packet with opcode and payload.
/// </summary>
public class Packet
{
    public ushort Opcode { get; }
    public byte[] Payload { get; }
    public bool IsEncrypted { get; }
    public DateTime Timestamp { get; }
    public PacketDirection Direction { get; }

    public Packet(ushort opcode, byte[] payload, PacketDirection direction, bool isEncrypted = false)
    {
        Opcode = opcode;
        Payload = payload ?? Array.Empty<byte>();
        IsEncrypted = isEncrypted;
        Timestamp = DateTime.UtcNow;
        Direction = direction;
    }

    /// <summary>Create a client-to-server packet.</summary>
    public static Packet ToServer(ushort opcode, byte[] payload, bool encrypted = false)
        => new(opcode, payload, PacketDirection.ClientToServer, encrypted);

    /// <summary>Create a server-to-client packet.</summary>
    public static Packet FromServer(ushort opcode, byte[] payload, bool encrypted = false)
        => new(opcode, payload, PacketDirection.ServerToClient, encrypted);

    public override string ToString() => $"[{Direction}] 0x{Opcode:X4} ({Payload.Length} bytes)";
}

public enum PacketDirection
{
    ClientToServer,
    ServerToClient
}

/// <summary>
/// Packet reader utility for extracting data from packet payloads.
/// </summary>
public class PacketReader
{
    private readonly byte[] _data;
    private int _position;

    public PacketReader(byte[] data)
    {
        _data = data;
        _position = 0;
    }

    public int Remaining => _data.Length - _position;
    public int Position => _position;

    public byte ReadByte() => _data[_position++];
    
    public ushort ReadUInt16()
    {
        var value = BitConverter.ToUInt16(_data, _position);
        _position += 2;
        return value;
    }
    
    public uint ReadUInt32()
    {
        var value = BitConverter.ToUInt32(_data, _position);
        _position += 4;
        return value;
    }
    
    public ulong ReadUInt64()
    {
        var value = BitConverter.ToUInt64(_data, _position);
        _position += 8;
        return value;
    }
    
    public float ReadSingle()
    {
        var value = BitConverter.ToSingle(_data, _position);
        _position += 4;
        return value;
    }
    
    public string ReadAscii()
    {
        ushort length = ReadUInt16();
        var text = System.Text.Encoding.ASCII.GetString(_data, _position, length);
        _position += length;
        return text;
    }
    
    public string ReadUnicode()
    {
        ushort length = ReadUInt16();
        var text = System.Text.Encoding.Unicode.GetString(_data, _position, length * 2);
        _position += length * 2;
        return text;
    }
    
    public byte[] ReadBytes(int count)
    {
        var bytes = new byte[count];
        Array.Copy(_data, _position, bytes, 0, count);
        _position += count;
        return bytes;
    }
    
    public void Skip(int count) => _position += count;
}

/// <summary>
/// Packet writer utility for constructing packet payloads.
/// </summary>
public class PacketWriter
{
    private readonly MemoryStream _stream;

    public PacketWriter(int initialCapacity = 64)
    {
        _stream = new MemoryStream(initialCapacity);
    }

    public PacketWriter WriteByte(byte value) { _stream.WriteByte(value); return this; }
    
    public PacketWriter WriteUInt16(ushort value) 
    { 
        _stream.Write(BitConverter.GetBytes(value)); 
        return this; 
    }
    
    public PacketWriter WriteUInt32(uint value) 
    { 
        _stream.Write(BitConverter.GetBytes(value)); 
        return this; 
    }
    
    public PacketWriter WriteUInt64(ulong value) 
    { 
        _stream.Write(BitConverter.GetBytes(value)); 
        return this; 
    }
    
    public PacketWriter WriteSingle(float value) 
    { 
        _stream.Write(BitConverter.GetBytes(value)); 
        return this; 
    }
    
    public PacketWriter WriteAscii(string text)
    {
        var bytes = System.Text.Encoding.ASCII.GetBytes(text);
        WriteUInt16((ushort)bytes.Length);
        _stream.Write(bytes);
        return this;
    }
    
    public PacketWriter WriteUnicode(string text)
    {
        var bytes = System.Text.Encoding.Unicode.GetBytes(text);
        WriteUInt16((ushort)(bytes.Length / 2));
        _stream.Write(bytes);
        return this;
    }
    
    public PacketWriter WriteBytes(byte[] data) { _stream.Write(data); return this; }
    
    public byte[] ToArray() => _stream.ToArray();
}