forked from OpenRA/OpenRA
/
OrderIO.cs
71 lines (62 loc) · 1.53 KB
/
OrderIO.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
70
71
#region Copyright & License Information
/*
* Copyright 2007-2018 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System.Collections.Generic;
using System.IO;
namespace OpenRA.Network
{
public static class OrderIO
{
public static List<Order> ToOrderList(this byte[] bytes, World world)
{
var ms = new MemoryStream(bytes, 4, bytes.Length - 4);
var reader = new BinaryReader(ms);
var ret = new List<Order>();
while (ms.Position < ms.Length)
{
var o = Order.Deserialize(world, reader);
if (o != null)
ret.Add(o);
}
return ret;
}
public static byte[] SerializeSync(int sync)
{
var ms = new MemoryStream(1 + 4);
using (var writer = new BinaryWriter(ms))
{
writer.Write((byte)0x65);
writer.Write(sync);
}
return ms.GetBuffer();
}
public static int2 ReadInt2(this BinaryReader r)
{
var x = r.ReadInt32();
var y = r.ReadInt32();
return new int2(x, y);
}
public static void Write(this BinaryWriter w, int2 p)
{
w.Write(p.X);
w.Write(p.Y);
}
public static void Write(this BinaryWriter w, CPos cell)
{
w.Write(cell.Bits);
}
public static void Write(this BinaryWriter w, WPos pos)
{
w.Write(pos.X);
w.Write(pos.Y);
w.Write(pos.Z);
}
}
}