-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathExtensionMethods.cs
More file actions
122 lines (93 loc) · 2.84 KB
/
ExtensionMethods.cs
File metadata and controls
122 lines (93 loc) · 2.84 KB
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Numerics;
using Windows.Foundation;
using Windows.UI;
namespace Stuart
{
static class BinaryWriterExtensions
{
public static void WriteCollection<T>(this BinaryWriter writer, ICollection<T> collection, Action<T> writeItem)
{
writer.Write(collection.Count);
foreach (var item in collection)
{
writeItem(item);
}
}
public static void WriteByteArray(this BinaryWriter writer, byte[] array)
{
if (array == null)
{
writer.Write(0);
}
else
{
writer.Write(array.Length);
writer.Write(array);
}
}
public static void WriteColor(this BinaryWriter writer, Color color)
{
writer.Write(color.R);
writer.Write(color.G);
writer.Write(color.B);
writer.Write(color.A);
}
public static void WriteRect(this BinaryWriter writer, Rect rect)
{
writer.Write(rect.X);
writer.Write(rect.Y);
writer.Write(rect.Width);
writer.Write(rect.Height);
}
public static void WriteVector2(this BinaryWriter writer, Vector2 vector)
{
writer.Write(vector.X);
writer.Write(vector.Y);
}
}
static class BinaryReaderExtensions
{
public static void ReadCollection<T>(this BinaryReader reader, ICollection<T> collection, Func<T> readItem)
{
collection.Clear();
var count = reader.ReadInt32();
for (int i = 0; i < count; i++)
{
collection.Add(readItem());
}
}
public static byte[] ReadByteArray(this BinaryReader reader)
{
var count = reader.ReadInt32();
return (count == 0) ? null : reader.ReadBytes(count);
}
public static Color ReadColor(this BinaryReader reader)
{
Color color;
color.R = reader.ReadByte();
color.G = reader.ReadByte();
color.B = reader.ReadByte();
color.A = reader.ReadByte();
return color;
}
public static Rect ReadRect(this BinaryReader reader)
{
Rect rect;
rect.X = reader.ReadDouble();
rect.Y = reader.ReadDouble();
rect.Width = reader.ReadDouble();
rect.Height = reader.ReadDouble();
return rect;
}
public static Vector2 ReadVector2(this BinaryReader reader)
{
Vector2 vector;
vector.X = reader.ReadSingle();
vector.Y = reader.ReadSingle();
return vector;
}
}
}