/
ClientNetworkReceiver.java
367 lines (331 loc) · 13.8 KB
/
ClientNetworkReceiver.java
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package com.riiablo.engine.client;
import com.google.flatbuffers.ByteBufferUtil;
import com.google.flatbuffers.Table;
import com.artemis.ComponentMapper;
import com.artemis.annotations.All;
import com.artemis.annotations.Wire;
import com.artemis.systems.IntervalSystem;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.net.Socket;
import com.badlogic.gdx.physics.box2d.Body;
import com.riiablo.CharData;
import com.riiablo.Riiablo;
import com.riiablo.save.D2S;
import com.riiablo.codec.excel.MonStats;
import com.riiablo.codec.util.BitStream;
import com.riiablo.engine.Dirty;
import com.riiablo.engine.Engine;
import com.riiablo.engine.EntityFactory;
import com.riiablo.engine.server.CofManager;
import com.riiablo.engine.server.component.Angle;
import com.riiablo.engine.server.component.Box2DBody;
import com.riiablo.engine.server.component.Class;
import com.riiablo.engine.server.component.CofAlphas;
import com.riiablo.engine.server.component.CofComponents;
import com.riiablo.engine.server.component.CofReference;
import com.riiablo.engine.server.component.CofTransforms;
import com.riiablo.engine.server.component.MapWrapper;
import com.riiablo.engine.server.component.Player;
import com.riiablo.engine.server.component.Position;
import com.riiablo.engine.server.component.Velocity;
import com.riiablo.item.Item;
import com.riiablo.map.Map;
import com.riiablo.net.packet.d2gs.AngleP;
import com.riiablo.net.packet.d2gs.ClassP;
import com.riiablo.net.packet.d2gs.CofAlphasP;
import com.riiablo.net.packet.d2gs.CofComponentsP;
import com.riiablo.net.packet.d2gs.CofTransformsP;
import com.riiablo.net.packet.d2gs.ComponentP;
import com.riiablo.net.packet.d2gs.Connection;
import com.riiablo.net.packet.d2gs.D2GS;
import com.riiablo.net.packet.d2gs.D2GSData;
import com.riiablo.net.packet.d2gs.DS1ObjectWrapperP;
import com.riiablo.net.packet.d2gs.Disconnect;
import com.riiablo.net.packet.d2gs.EntitySync;
import com.riiablo.net.packet.d2gs.ItemP;
import com.riiablo.net.packet.d2gs.MonsterP;
import com.riiablo.net.packet.d2gs.PlayerP;
import com.riiablo.net.packet.d2gs.PositionP;
import com.riiablo.net.packet.d2gs.VelocityP;
import com.riiablo.net.packet.d2gs.WarpP;
import com.riiablo.util.ArrayUtils;
import com.riiablo.util.BufferUtils;
import com.riiablo.util.DebugUtils;
import com.riiablo.widget.TextArea;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.util.Arrays;
@All
public class ClientNetworkReceiver extends IntervalSystem {
private static final String TAG = "ClientNetworkReceiver";
private static final boolean DEBUG = true;
private static final boolean DEBUG_PACKET = DEBUG && !true;
private static final boolean DEBUG_SYNC = DEBUG && !true;
// protected ComponentMapper<Networked> mNetworked;
protected ComponentMapper<CofReference> mCofReference;
protected ComponentMapper<CofComponents> mCofComponents;
protected ComponentMapper<CofTransforms> mCofTransforms;
protected ComponentMapper<CofAlphas> mCofAlphas;
protected ComponentMapper<Position> mPosition;
protected ComponentMapper<Velocity> mVelocity;
protected ComponentMapper<Angle> mAngle;
protected ComponentMapper<Player> mPlayer;
protected ComponentMapper<Box2DBody> mBox2DBody;
protected ComponentMapper<MapWrapper> mMapWrapper;
protected CofManager cofs;
protected NetworkIdManager syncIds;
@Wire(name="client.socket")
protected Socket socket;
@Wire(name = "factory")
protected EntityFactory factory;
@Wire(name = "map")
protected Map map;
@Wire(name = "output")
protected TextArea output;
private final ByteBuffer buffer = ByteBuffer.allocate(1 << 20).order(ByteOrder.LITTLE_ENDIAN);
private final EntitySync sync = new EntitySync();
public ClientNetworkReceiver() {
super(null, 1 / 60f);
}
@Override
protected void processSystem() {
InputStream in = socket.getInputStream();
try {
if (in.available() > 0) {
ReadableByteChannel channel = Channels.newChannel(in);
buffer.clear();
int i = channel.read(buffer);
buffer.rewind().limit(i);
D2GS d2gs = new D2GS();
int p = 0;
while (buffer.hasRemaining()) {
int size = ByteBufferUtil.getSizePrefix(buffer);
D2GS.getRootAsD2GS(ByteBufferUtil.removeSizePrefix(buffer), d2gs);
if (DEBUG_PACKET) Gdx.app.debug(TAG, p++ + " packet type " + D2GSData.name(d2gs.dataType()) + ":" + size + "B");
process(d2gs);
// System.out.println(buffer.position() + "->" + (buffer.position() + size + 4));
buffer.position(buffer.position() + size + 4);
}
}
} catch (Throwable t) {
Gdx.app.error(TAG, t.getMessage(), t);
}
}
private void process(D2GS packet) {
switch (packet.dataType()) {
case D2GSData.Connection:
Connection(packet);
break;
case D2GSData.Disconnect:
Disconnect(packet);
break;
case D2GSData.EntitySync:
Synchronize(packet);
break;
default:
Gdx.app.error(TAG, "Unknown packet type: " + packet.dataType());
}
}
private void Connection(D2GS packet) {
Connection connection = (Connection) packet.data(new Connection());
String charName = connection.charName();
int charClass = connection.charClass();
output.appendText(Riiablo.string.format(3641, charName));
output.appendText("\n");
Vector2 origin = map.find(Map.ID.TOWN_ENTRY_1);
if (origin == null) origin = map.find(Map.ID.TOWN_ENTRY_2);
if (origin == null) origin = map.find(Map.ID.TP_LOCATION);
int entityId = factory.createPlayer(charName, charClass, origin.x, origin.y);
syncIds.put(connection.entityId(), entityId);
int[] component = mCofComponents.get(entityId).component;
// for (int i = 0; i < 16; i++) component[i] = connection.cofComponents(i);
float[] alpha = mCofAlphas.get(entityId).alpha;
// for (int i = 0; i < 16; i++) alpha[i] = connection.cofAlphas(i) / 255f;
byte[] transform = mCofTransforms.get(entityId).transform;
// for (int i = 0; i < 16; i++) transform[i] = (byte) connection.cofTransforms(i);
int alphaFlags = Dirty.NONE;
int transformFlags = Dirty.NONE;
for (int i = 0; i < 16; i++) {
cofs.setComponent(entityId, i, connection.cofComponents(i));
}
for (int i = 0; i < 16; i++) {
alphaFlags |= cofs.setAlpha(entityId, i, connection.cofAlphas(i) / 255f);
transformFlags |= cofs.setTransform(entityId, i, (byte) connection.cofTransforms(i));
}
cofs.updateAlpha(entityId, alphaFlags);
cofs.updateTransform(entityId, transformFlags);
cofs.setMode(entityId, Engine.Player.MODE_TN, true);
cofs.setWClass(entityId, Engine.WEAPON_1HS); // TODO...
System.out.println(" " + DebugUtils.toByteArray(ArrayUtils.toByteArray(component)));
System.out.println(" " + Arrays.toString(alpha));
System.out.println(" " + DebugUtils.toByteArray(transform));
}
private void Disconnect(D2GS packet) {
Disconnect disconnect = (Disconnect) packet.data(new Disconnect());
int serverEntityId = disconnect.entityId();
int entityId = syncIds.get(serverEntityId);
CharData data = mPlayer.get(entityId).data;
output.appendText(Riiablo.string.format(3642, data.getD2S().header.name));
output.appendText("\n");
world.delete(entityId);
Body body = mBox2DBody.get(entityId).body;
if (body != null) ;
}
@Deprecated
private int findType(EntitySync s) {
for (int i = 0, len = s.componentLength(); i < len; i++) {
if (s.componentType(i) == ComponentP.ClassP) {
return ((ClassP) s.component(new ClassP(), i)).type();
}
}
return -1;
}
private <T extends Table> T findTable(EntitySync s, byte dataType, T table) {
ByteBuffer dataTypes = s.componentTypeAsByteBuffer();
for (int i = 0; dataTypes.hasRemaining(); i++) {
if (dataTypes.get() == dataType) {
s.component(table, i);
return table;
}
}
return null;
}
private int createEntity(EntitySync sync) {
assert syncIds.get(sync.entityId()) == Engine.INVALID_ENTITY;
Class.Type type = Class.Type.valueOf(sync.type());
switch (type) {
case OBJ: {
DS1ObjectWrapperP ds1ObjectWrapper = findTable(sync, ComponentP.DS1ObjectWrapperP, new DS1ObjectWrapperP());
if (ds1ObjectWrapper != null) {
PositionP position = findTable(sync, ComponentP.PositionP, new PositionP());
return factory.createObject(ds1ObjectWrapper.act(), ds1ObjectWrapper.type(), ds1ObjectWrapper.id(), position.x(), position.y());
}
return Engine.INVALID_ENTITY;
}
case MON: {
DS1ObjectWrapperP ds1ObjectWrapper = findTable(sync, ComponentP.DS1ObjectWrapperP, new DS1ObjectWrapperP());
if (ds1ObjectWrapper != null) {
PositionP position = findTable(sync, ComponentP.PositionP, new PositionP());
String objectType = Riiablo.files.MonPreset.getPlace(ds1ObjectWrapper.act(), ds1ObjectWrapper.id());
MonStats.Entry monstats = Riiablo.files.monstats.get(objectType);
return factory.createMonster(monstats, position.x(), position.y());
} else {
PositionP position = findTable(sync, ComponentP.PositionP, new PositionP());
MonsterP monster = findTable(sync, ComponentP.MonsterP, new MonsterP());
return factory.createMonster(monster.monsterId(), position.x(), position.y());
}
}
case PLR: {
PlayerP player = findTable(sync, ComponentP.PlayerP, new PlayerP());
PositionP position = findTable(sync, ComponentP.PositionP, new PositionP());
int entityId = factory.createPlayer(player.charName(), player.charClass(), position.x(), position.y());
cofs.setMode(entityId, Engine.Player.MODE_TN);
cofs.setWClass(entityId, Engine.WEAPON_1HS); // TODO...
return entityId;
}
case ITM: {
ItemP item = findTable(sync, ComponentP.ItemP, new ItemP());
PositionP position = findTable(sync, ComponentP.PositionP, new PositionP());
byte[] bytes = BufferUtils.readRemaining(item.dataAsByteBuffer());
BitStream bitStream = new BitStream(bytes);
bitStream.skip(D2S.ItemData.SECTION_HEADER_BITS);
Item itemObj = com.riiablo.item.Item.loadFromStream(bitStream);
return factory.createItem(itemObj, position.x(), position.y());
}
case WRP: {
WarpP warp = findTable(sync, ComponentP.WarpP, new WarpP());
PositionP position = findTable(sync, ComponentP.PositionP, new PositionP());
int entityId = factory.createWarp(warp.index(), position.x(), position.y());
Map.Zone zone = mMapWrapper.get(entityId).zone;
zone.addWarp(entityId);
return entityId;
}
case MIS: {
return Engine.INVALID_ENTITY;
}
default:
return Engine.INVALID_ENTITY;
}
}
private void Synchronize(D2GS packet) {
packet.data(sync);
Synchronize(sync);
}
private void Synchronize(EntitySync entityData) {
int entityId = syncIds.get(entityData.entityId());
if (entityId == Engine.INVALID_ENTITY) {
syncIds.put(entityData.entityId(), entityId = createEntity(entityData));
}
int tFlags = Dirty.NONE;
int aFlags = Dirty.NONE;
if (DEBUG_SYNC) Gdx.app.debug(TAG, "syncing " + entityId);
for (int i = 0, len = entityData.componentLength(); i < len; i++) {
switch (entityData.componentType(i)) {
case ComponentP.ClassP:
case ComponentP.PlayerP:
case ComponentP.DS1ObjectWrapperP:
case ComponentP.WarpP:
case ComponentP.MonsterP:
case ComponentP.ItemP:
break;
case ComponentP.CofComponentsP: {
CofComponentsP data = (CofComponentsP) entityData.component(new CofComponentsP(), i);
for (int j = 0, s0 = data.componentLength(); j < s0; j++) {
cofs.setComponent(entityId, j, (byte) data.component(j));
}
break;
}
case ComponentP.CofTransformsP: {
CofTransformsP data = (CofTransformsP) entityData.component(new CofTransformsP(), i);
for (int j = 0, s0 = data.transformLength(); j < s0; j++) {
tFlags |= cofs.setTransform(entityId, j, (byte) data.transform(j));
}
break;
}
case ComponentP.CofAlphasP: {
CofAlphasP data = (CofAlphasP) entityData.component(new CofAlphasP(), i);
for (int j = 0, s0 = data.alphaLength(); j < s0; j++) {
aFlags |= cofs.setAlpha(entityId, j, data.alpha(j) / 255f);
}
break;
}
case ComponentP.PositionP: {
Vector2 position = mPosition.get(entityId).position;
PositionP data = (PositionP) entityData.component(new PositionP(), i);
position.x = data.x();
position.y = data.y();
if (mBox2DBody.has(entityId)) {
Body body = mBox2DBody.get(entityId).body;
if (body != null) body.setTransform(position, body.getAngle());
}
//Gdx.app.log(TAG, " " + position);
break;
}
case ComponentP.VelocityP: {
Vector2 velocity = mVelocity.get(entityId).velocity;
VelocityP data = (VelocityP) entityData.component(new VelocityP(), i);
velocity.x = data.x();
velocity.y = data.y();
//Gdx.app.log(TAG, " " + velocity);
break;
}
case ComponentP.AngleP: {
Vector2 angle = mAngle.get(entityId).target;
AngleP data = (AngleP) entityData.component(new AngleP(), i);
angle.x = data.x();
angle.y = data.y();
//Gdx.app.log(TAG, " " + angle);
break;
}
default:
Gdx.app.error(TAG, "Unknown packet type: " + ComponentP.name(entityData.componentType(i)));
}
}
cofs.updateTransform(entityId, tFlags);
cofs.updateAlpha(entityId, aFlags);
}
}