/
BufferedBlockSender.cs
240 lines (212 loc) · 9.58 KB
/
BufferedBlockSender.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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
Copyright 2015 MCGalaxy
Dual-licensed under the Educational Community License, Version 2.0 and
the GNU General Public License, Version 3 (the "Licenses"); you may
not use this file except in compliance with the Licenses. You may
obtain a copy of the Licenses at
http://www.opensource.org/licenses/ecl2.php
http://www.gnu.org/licenses/gpl-3.0.html
Unless required by applicable law or agreed to in writing,
software distributed under the Licenses are distributed on an "AS IS"
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the Licenses for the specific language governing
permissions and limitations under the Licenses.
*/
using System;
using BlockID = System.UInt16;
using BlockRaw = System.Byte;
namespace MCGalaxy.Network
{
/// <summary> Helper class for efficiently sending many block changes. </summary>
/// <remarks> Sends block changes as either a single CPE BulkBlockUpdate packet,
/// or 256 SetBlock packets combined as a single byte array to reduce overhead. </remarks>
public sealed class BufferedBlockSender
{
int[] indices = new int[256];
BlockID[] blocks = new BlockID[256];
int count = 0;
public Level level;
public Player player;
public BufferedBlockSender() { }
/// <summary> Constructs a bulk sender that will send block changes to all players on that level </summary>
public BufferedBlockSender(Level level) {
this.level = level;
}
/// <summary> Constructs a bulk sender that will only send block changes to that player </summary>
public BufferedBlockSender(Player player) {
this.player = player;
this.level = player.level;
}
/// <summary> Adds a block change to list of buffered changes </summary>
/// <remarks> This method automatically calls Flush() when buffer limit is reached </remarks>
public void Add(int index, BlockID block) {
indices[count] = index;
if (Block.IsPhysicsType(block)) {
blocks[count] = Block.Convert(block);
} else {
blocks[count] = block;
}
count++;
if (count == 256) Flush();
}
/// <summary> Sends buffered block changes to target player(s) </summary>
public void Flush() {
if (count == 0) return;
if (player != null) SendPlayer();
else SendLevel();
count = 0;
}
void SendLevel() {
byte[] bulk = null, normal = null, classic = null, ext = null, extBulk = null;
Player[] players = PlayerInfo.Online.Items;
foreach (Player p in players)
{
if (p.level != level) continue;
byte[] packet = MakePacket(p, ref bulk, ref normal,
ref classic, ref ext, ref extBulk);
p.Socket.Send(packet, SendFlags.LowPriority);
}
}
void SendPlayer() {
byte[] bulk = null, normal = null,classic = null, ext = null, extBulk = null;
byte[] packet = MakePacket(player, ref bulk, ref normal,
ref classic, ref ext, ref extBulk);
player.Socket.Send(packet, SendFlags.LowPriority);
}
#region Packet construction
byte[] MakePacket(Player p, ref byte[] bulk, ref byte[] normal,
ref byte[] classic, ref byte[] ext, ref byte[] extBulk) {
#if TEN_BIT_BLOCKS
if (p.hasExtBlocks) {
if (p.hasBulkBlockUpdate && count >= 150) {
if (extBulk == null) extBulk = MakeBulkExt();
return extBulk;
} else {
if (ext == null) ext = MakeExt();
return ext;
}
}
#endif
// Different clients support varying types of blocks
if (p.hasBulkBlockUpdate && p.hasBlockDefs && count >= 160) {
if (bulk == null) bulk = MakeBulk();
return bulk;
} else if (p.hasBlockDefs) {
// supports all 255 blocks (classicube enhanced client)
if (normal == null) normal = MakeNormal();
return normal;
} else if (!p.hasCustomBlocks && p.ProtocolVersion == Server.VERSION_0030) {
// support original 45 blocks (classic client)
if (classic == null) classic = MakeLimited(p.fallback);
return classic;
} else {
// other support combination (CPE only, preclassic, etc)
// don't bother trying to optimise for this case
return MakeLimited(p.fallback);
}
}
#if TEN_BIT_BLOCKS
byte[] MakeBulkExt() {
byte[] data = new byte[2 + 256 * 5 + (256 / 4)];
data[0] = Opcode.CpeBulkBlockUpdate;
data[1] = (byte)(count - 1);
for (int i = 0, j = 2; i < count; i++)
{
int index = indices[i];
data[j++] = (byte)(index >> 24); data[j++] = (byte)(index >> 16);
data[j++] = (byte)(index >> 8); data[j++] = (byte)index;
}
for (int i = 0, j = 2 + 256 * sizeof(int); i < count; i++)
{
data[j++] = (BlockRaw)blocks[i];
}
for (int i = 0, j = 2 + 256 * (sizeof(int) + 1); i < count; i += 4) {
int flags = 0;
flags |= (Block.ToRaw(blocks[i ]) >> 8) & 0x03;
flags |= (Block.ToRaw(blocks[i + 1]) >> 6) & 0x0C;
flags |= (Block.ToRaw(blocks[i + 2]) >> 4) & 0x30;
flags |= (Block.ToRaw(blocks[i + 3]) >> 2) & 0xC0;
data[j++] = (byte)flags;
}
return data;
}
byte[] MakeExt() {
byte[] data = new byte[count * 9];
for (int i = 0, j = 0; i < count; i++)
{
int index = indices[i];
int x = (index % level.Width);
int y = (index / level.Width) / level.Length;
int z = (index / level.Width) % level.Length;
data[j++] = Opcode.SetBlock;
data[j++] = (byte)(x >> 8); data[j++] = (byte)x;
data[j++] = (byte)(y >> 8); data[j++] = (byte)y;
data[j++] = (byte)(z >> 8); data[j++] = (byte)z;
BlockID raw = Block.ToRaw(blocks[i]);
data[j++] = (byte)(raw >> 8);
data[j++] = (byte)raw;
}
return data;
}
#endif
byte[] MakeBulk() {
byte[] data = new byte[2 + 256 * 5];
data[0] = Opcode.CpeBulkBlockUpdate;
data[1] = (byte)(count - 1);
for (int i = 0, j = 2; i < count; i++)
{
int index = indices[i];
data[j++] = (byte)(index >> 24); data[j++] = (byte)(index >> 16);
data[j++] = (byte)(index >> 8); data[j++] = (byte)index;
}
for (int i = 0, j = 2 + 256 * sizeof(int); i < count; i++)
{
#if TEN_BIT_BLOCKS
BlockID block = blocks[i];
data[j++] = block <= 511 ? (BlockRaw)block : level.GetFallback(block);
#else
data[j++] = (BlockRaw)blocks[i];
#endif
}
return data;
}
byte[] MakeNormal() {
byte[] data = new byte[count * 8];
for (int i = 0, j = 0; i < count; i++)
{
int index = indices[i];
int x = (index % level.Width);
int y = (index / level.Width) / level.Length;
int z = (index / level.Width) % level.Length;
data[j++] = Opcode.SetBlock;
data[j++] = (byte)(x >> 8); data[j++] = (byte)x;
data[j++] = (byte)(y >> 8); data[j++] = (byte)y;
data[j++] = (byte)(z >> 8); data[j++] = (byte)z;
#if TEN_BIT_BLOCKS
BlockID block = blocks[i];
data[j++] = block <= 511 ? (BlockRaw)block : level.GetFallback(block);
#else
data[j++] = (BlockRaw)blocks[i];
#endif
}
return data;
}
byte[] MakeLimited(byte[] fallback) {
byte[] data = new byte[count * 8];
for (int i = 0, j = 0; i < count; i++)
{
int index = indices[i];
int x = (index % level.Width);
int y = (index / level.Width) / level.Length;
int z = (index / level.Width) % level.Length;
data[j++] = Opcode.SetBlock;
data[j++] = (byte)(x >> 8); data[j++] = (byte)x;
data[j++] = (byte)(y >> 8); data[j++] = (byte)y;
data[j++] = (byte)(z >> 8); data[j++] = (byte)z;
data[j++] = fallback[level.GetFallback(blocks[i])];
}
return data;
}
#endregion
}
}