-
Notifications
You must be signed in to change notification settings - Fork 0
/
ValuePacket.cs
69 lines (55 loc) · 1.96 KB
/
ValuePacket.cs
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
using System;
using System.Collections.Generic;
using System.IO;
namespace Zenvin.Settings.Framework.Serialization {
/// <summary>
/// Helper class to serialize key/value pairs as byte array.<br></br>
/// Contains an implicit conversion to <see cref="byte"/>[].
/// </summary>
public partial class ValuePacket {
private readonly Dictionary<string, byte[]> data = new Dictionary<string, byte[]> ();
/// <summary> Default constructor that initializes an empty <see cref="ValuePacket"/>. </summary>
public ValuePacket () { }
/// <summary> Initializes a new <see cref="ValuePacket"/> with the contents of a given byte array. </summary>
/// <param name="byteData"> Initial data to write into the packet. </param>
/// <remarks>
/// Assumes that <paramref name="byteData"/> is a value that was returned by <see cref="ValuePacket.ToArray"/>, or is in the same format.
/// </remarks>
public ValuePacket (byte[] byteData) {
FromArray (byteData);
}
private void FromArray (byte[] byteData) {
using MemoryStream stream = new MemoryStream (byteData);
using BinaryReader reader = new BinaryReader (stream);
int count = reader.ReadInt32 ();
for (int i = 0; i < count; i++) {
string key = reader.ReadString ();
byte[] value = reader.ReadArray ();
data[key] = value;
}
}
/// <summary>
/// Returns the contents of the packet as a byte array.
/// </summary>
public byte[] ToArray () {
using MemoryStream stream = new MemoryStream ();
using BinaryWriter writer = new BinaryWriter(stream);
writer.Write (data.Count);
foreach (var val in data) {
writer.Write (val.Key);
writer.WriteArray (val.Value);
}
return stream.ToArray ();
}
/// <summary>
/// <inheritdoc cref="ToArray"/><br></br>
/// The same as calling <see cref="ToArray"/>.
/// </summary>
public static implicit operator byte[] (ValuePacket packet) {
if (packet == null) {
return Array.Empty<byte> ();
}
return packet.ToArray ();
}
}
}