Skip to content

Commit eb9973a

Browse files
authored
Merge pull request #9 from Periapsises/bugfix/proper-message-seeking
Change how MessageReader assigns its type
2 parents 8f4a1d8 + 5987bae commit eb9973a

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

QuickLink/Messages/MessageReader.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ public class MessageReader
2222
/// <param name="buffer">The byte buffer to read from.</param>
2323
public MessageReader(byte[] buffer)
2424
{
25-
_buffer = buffer;
25+
_buffer = new byte[buffer.Length - 4];
26+
Array.Copy(buffer, 4, _buffer, 0, _buffer.Length);
2627
_offset = 0;
2728

28-
Type = MessageType.Get(ReadUInt32());
29+
Type = MessageType.Get(BitConverter.ToUInt32(buffer, 0));
2930
}
3031

3132
private void EnsureCanReadLength(int length)

QuickLink/Messages/MessageType.cs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,28 @@ public class MessageType
2121
/// </summary>
2222
public string Name { get; private set; }
2323

24+
private MessageType(uint id)
25+
{
26+
Id = id;
27+
Name = "UNKNOWN";
28+
}
29+
2430
/// <summary>
2531
/// Gets the message type with the specified identifier.
2632
/// </summary>
2733
/// <param name="id">The identifier of the message type.</param>
2834
/// <returns>The message type with the specified identifier.</returns>
2935
public static MessageType Get(uint id)
3036
{
31-
return _messageTypes[id];
32-
}
37+
lock (_lock)
38+
{
39+
if (!_messageTypes.ContainsKey(id))
40+
{
41+
_messageTypes.Add(id, new MessageType(id));
42+
}
3343

34-
private MessageType(uint id, string name)
35-
{
36-
Id = id;
37-
Name = name;
44+
return _messageTypes[id];
45+
}
3846
}
3947

4048
/// <summary>
@@ -48,12 +56,14 @@ public static MessageType Get(string name)
4856
lock (_lock)
4957
{
5058
uint id = CRC32.GenerateHash(name);
51-
if (!_messageTypes.ContainsKey(id))
59+
MessageType messageType = Get(id);
60+
61+
if (messageType.Name == "UNKNOWN")
5262
{
53-
_messageTypes.Add(id, new MessageType(id, name));
63+
messageType.Name = name;
5464
}
5565

56-
return _messageTypes[id];
66+
return messageType;
5767
}
5868
}
5969
}

0 commit comments

Comments
 (0)