aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.World/SynchedEntityData.cpp
blob: 655fa0c3021a24e0b0b59082d002fe1a8680d874 (plain)
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
#include "stdafx.h"
#include "Class.h"
#include "BasicTypeContainers.h"
#include "InputOutputStream.h"
#include "net.minecraft.h"
#include "net.minecraft.network.packet.h"
#include "net.minecraft.world.item.h"
#include "SynchedEntityData.h"


SynchedEntityData::SynchedEntityData()
{
	m_isDirty = false;
	m_isEmpty = true;
}

void SynchedEntityData::define(int id, int value)
{
	MemSect(17);
	checkId(id);
	int type = TYPE_INT;
	shared_ptr<DataItem> dataItem = shared_ptr<DataItem>( new DataItem(type, id, value) );
	itemsById[id] = dataItem;
	MemSect(0);
	m_isEmpty = false;
}

void SynchedEntityData::define(int id, byte value)
{
	MemSect(17);
	checkId(id);
	int type = TYPE_BYTE;
	shared_ptr<DataItem> dataItem = shared_ptr<DataItem>( new DataItem(type, id, value) );
	itemsById[id] = dataItem;
	MemSect(0);
	m_isEmpty = false;
}

void SynchedEntityData::define(int id, short value)
{
	MemSect(17);
	checkId(id);
	int type = TYPE_SHORT;
	shared_ptr<DataItem> dataItem = shared_ptr<DataItem>( new DataItem(type, id, value) );
	itemsById[id] = dataItem;
	MemSect(0);
	m_isEmpty = false;
}

void SynchedEntityData::define(int id, float value)
{
	MemSect(17);
	checkId(id);
	int type = TYPE_FLOAT;
	shared_ptr<DataItem> dataItem = shared_ptr<DataItem>( new DataItem(type, id, value) );
	itemsById[id] = dataItem;
	MemSect(0);
	m_isEmpty = false;
}

void SynchedEntityData::define(int id, const wstring& value)
{
	MemSect(17);
	checkId(id);
	int type = TYPE_STRING;
	shared_ptr<DataItem> dataItem = shared_ptr<DataItem>( new DataItem(type, id, value) );
	itemsById[id] = dataItem;
	MemSect(0);
	m_isEmpty = false;
}

void SynchedEntityData::defineNULL(int id, void *pVal)
{
	MemSect(17);
	checkId(id);
	int type = TYPE_ITEMINSTANCE;
	shared_ptr<DataItem> dataItem = shared_ptr<DataItem>( new DataItem(type, id, shared_ptr<ItemInstance>()) );
	itemsById[id] = dataItem;
	MemSect(0);
	m_isEmpty = false;
}

void SynchedEntityData::checkId(int id)
{
#if 0
	if (id > MAX_ID_VALUE)
	{
		throw new IllegalArgumentException(L"Data value id is too big with " + std::to_wstring(id) + L"! (Max is " + std::to_wstring(MAX_ID_VALUE) + L")");
	}
	if (itemsById.find(id) != itemsById.end())
	{
		throw new IllegalArgumentException(L"Duplicate id value for " + std::to_wstring(id) + L"!");
	}
#endif
}

byte SynchedEntityData::getByte(int id)
{
	return itemsById[id]->getValue_byte();
}

short SynchedEntityData::getShort(int id)
{
	return itemsById[id]->getValue_short();
}

int SynchedEntityData::getInteger(int id)
{
	return itemsById[id]->getValue_int();
}

float SynchedEntityData::getFloat(int id)
{
	return itemsById[id]->getValue_float();
}

wstring SynchedEntityData::getString(int id)
{
	return itemsById[id]->getValue_wstring();
}

shared_ptr<ItemInstance> SynchedEntityData::getItemInstance(int id)
{
	//assert(false);	// 4J - not currently implemented
	return itemsById[id]->getValue_itemInstance();
}

Pos *SynchedEntityData::getPos(int id)
{
	assert(false);	// 4J - not currently implemented
	return NULL;
}

void SynchedEntityData::set(int id, int value)
{
	shared_ptr<DataItem> dataItem = itemsById[id];

	// update the value if it has changed
	if (value != dataItem->getValue_int())
	{
		dataItem->setValue(value);
		dataItem->setDirty(true);
		m_isDirty = true;
	}
}

void SynchedEntityData::set(int id, byte value)
{
	shared_ptr<DataItem> dataItem = itemsById[id];

	// update the value if it has changed
	if (value != dataItem->getValue_byte())
	{
		dataItem->setValue(value);
		dataItem->setDirty(true);
		m_isDirty = true;
	}
}

void SynchedEntityData::set(int id, short value)
{
	shared_ptr<DataItem> dataItem = itemsById[id];

	// update the value if it has changed
	if (value != dataItem->getValue_short())
	{
		dataItem->setValue(value);
		dataItem->setDirty(true);
		m_isDirty = true;
	}
}

void SynchedEntityData::set(int id, float value)
{
	shared_ptr<DataItem> dataItem = itemsById[id];

	// update the value if it has changed
	if (value != dataItem->getValue_float())
	{
		dataItem->setValue(value);
		dataItem->setDirty(true);
		m_isDirty = true;
	}
}

void SynchedEntityData::set(int id, const wstring& value)
{
	shared_ptr<DataItem> dataItem = itemsById[id];

	// update the value if it has changed
	if (value != dataItem->getValue_wstring())
	{
		dataItem->setValue(value);
		dataItem->setDirty(true);
		m_isDirty = true;
	}
}

void SynchedEntityData::set(int id, shared_ptr<ItemInstance> value)
{
	shared_ptr<DataItem> dataItem = itemsById[id];

	// update the value if it has changed
	if (value != dataItem->getValue_itemInstance())
	{
		dataItem->setValue(value);
		dataItem->setDirty(true);
		m_isDirty = true;
	}
}

void SynchedEntityData::markDirty(int id)
{
	itemsById[id]->dirty = true;
	m_isDirty = true;
}

bool SynchedEntityData::isDirty()
{
	return m_isDirty;
}

void SynchedEntityData::pack(vector<shared_ptr<DataItem> > *items, DataOutputStream *output) // TODO throws IOException
{

	if (items)
	{
		for (auto& dataItem : *items)
		{
			writeDataItem(output, dataItem);
		}
	}

	// add an eof
	output->writeByte(EOF_MARKER);
}

vector<shared_ptr<SynchedEntityData::DataItem> > *SynchedEntityData::packDirty()
{

	vector<shared_ptr<DataItem> > *result = NULL;

	if (m_isDirty)
	{
		for( int i = 0; i <= MAX_ID_VALUE; i++ )
		{
			shared_ptr<DataItem> dataItem = itemsById[i];
			if ((dataItem != NULL) && dataItem->isDirty())
			{
				dataItem->setDirty(false);

				if (result == NULL)
				{
					result = new vector<shared_ptr<DataItem> >();
				}
				result->push_back(dataItem);
			}
		}
	}
	m_isDirty = false;

	return result;
}

void SynchedEntityData::packAll(DataOutputStream *output) // throws IOException
{
	for( int i = 0; i <= MAX_ID_VALUE; i++ )
	{
		shared_ptr<DataItem> dataItem = itemsById[i];
		if(dataItem != NULL)
		{
			writeDataItem(output, dataItem);
		}
	}

	// add an eof
	output->writeByte(EOF_MARKER);
}

vector<shared_ptr<SynchedEntityData::DataItem> > *SynchedEntityData::getAll()
{
	vector<shared_ptr<DataItem> > *result = NULL;

	for( int i = 0; i <= MAX_ID_VALUE; i++ )
	{
		shared_ptr<DataItem> dataItem = itemsById[i];
		if(dataItem != NULL)
		{
			if (result == NULL)
			{
				result = new vector<shared_ptr<DataItem> >();
			}
			result->push_back(dataItem);
		}
	}

	return result;
}


void SynchedEntityData::writeDataItem(DataOutputStream *output, shared_ptr<DataItem> dataItem) //throws IOException
{
	// pack type and id
	int header = ((dataItem->getType() << TYPE_SHIFT) | (dataItem->getId() & MAX_ID_VALUE)) & 0xff;
	output->writeByte(header);

	// write value
	switch (dataItem->getType())
	{
	case TYPE_BYTE:
		output->writeByte( dataItem->getValue_byte());
		break;
	case TYPE_INT:
		output->writeInt( dataItem->getValue_int());
		break;
	case TYPE_SHORT:
		output->writeShort( dataItem->getValue_short());
		break;
	case TYPE_FLOAT:
		output->writeFloat( dataItem->getValue_float());
		break;
	case TYPE_STRING:
		Packet::writeUtf(dataItem->getValue_wstring(), output);
		break;
	case TYPE_ITEMINSTANCE:
		{
			shared_ptr<ItemInstance> instance = (shared_ptr<ItemInstance> )dataItem->getValue_itemInstance();
			Packet::writeItem(instance, output);
		}
		break;

	default:
		assert(false);	// 4J - not implemented
		break;
	}
}

vector<shared_ptr<SynchedEntityData::DataItem> > *SynchedEntityData::unpack(DataInputStream *input) //throws IOException
{

	vector<shared_ptr<DataItem> > *result = NULL;

	int currentHeader = input->readByte();

	while (currentHeader != EOF_MARKER)
	{

		if (result == NULL)
		{
			result = new vector<shared_ptr<DataItem> >();
		}

		// split type and id
		int itemType = (currentHeader & TYPE_MASK) >> TYPE_SHIFT;
		int itemId = (currentHeader & MAX_ID_VALUE);

		shared_ptr<DataItem> item = shared_ptr<DataItem>();
		switch (itemType)
		{
		case TYPE_BYTE:
			{
				byte dataRead = input->readByte();
				item = shared_ptr<DataItem>( new DataItem(itemType, itemId, dataRead) );
			}
			break;
		case TYPE_SHORT:
			{
				short dataRead = input->readShort();
				item = shared_ptr<DataItem>( new DataItem(itemType, itemId, dataRead) );
			}
			break;
		case TYPE_INT:
			{
				int dataRead = input->readInt();
				item = shared_ptr<DataItem>( new DataItem(itemType, itemId, dataRead) );
			}
			break;
		case TYPE_FLOAT:
			{
				float dataRead = input->readFloat();
				item = shared_ptr<DataItem>( new DataItem(itemType, itemId, dataRead) );

			}
			break;
		case TYPE_STRING:
			item = shared_ptr<DataItem>( new DataItem(itemType, itemId, Packet::readUtf(input, MAX_STRING_DATA_LENGTH)) );
			break;
		case TYPE_ITEMINSTANCE:
			{
				item = shared_ptr<DataItem>(new DataItem(itemType, itemId, Packet::readItem(input)));
			}
			break;
		default:
			app.DebugPrintf(" ------ garbage data, or early end of stream due to an incomplete packet\n");
			delete result;
			return NULL;
			break;
		}
		result->push_back(item);

		currentHeader = input->readByte();
	}

	return result;
}

/**
* Assigns values from a list of data items.
*
* @param items
*/

void SynchedEntityData::assignValues(vector<shared_ptr<DataItem> > *items)
{
	for (auto& item : *items)
	{
		shared_ptr<DataItem> itemFromId = itemsById[item->getId()];
		if( itemFromId != NULL )
		{
			switch(item->getType())
			{
			case TYPE_BYTE:
				itemFromId->setValue(item->getValue_byte());
				break;
			case TYPE_SHORT:
				itemFromId->setValue(item->getValue_short());
				break;
			case TYPE_INT:
				itemFromId->setValue(item->getValue_int());
				break;
			case TYPE_FLOAT:
				itemFromId->setValue(item->getValue_float());
				break;
			case TYPE_STRING:
				itemFromId->setValue(item->getValue_wstring());
				break;
			case TYPE_ITEMINSTANCE:
				itemFromId->setValue(item->getValue_itemInstance());
				break;
			default:
				assert(false); // 4J - not implemented
				break;
			}
		}
	}

	// client-side dirty
	m_isDirty = true;
}

bool SynchedEntityData::isEmpty()
{
	return m_isEmpty;
}

void SynchedEntityData::clearDirty()
{
	m_isDirty = false;
}

int SynchedEntityData::getSizeInBytes()
{
	int size = 1;

	for( int i = 0; i <= MAX_ID_VALUE; i++ )
	{
		shared_ptr<DataItem> dataItem = itemsById[i];
		if(dataItem != NULL)
		{
			size += 1;

			// write value
			switch (dataItem->getType())
			{
			case TYPE_BYTE:
				size += 1;
				break;
			case TYPE_SHORT:
				size += 2;
				break;
			case TYPE_INT:
				size += 4;
				break;
			case TYPE_FLOAT:
				size += 4;
				break;
			case TYPE_STRING:
				size += (int)dataItem->getValue_wstring().length() + 2; // Estimate, assuming all ascii chars
				break;
			case TYPE_ITEMINSTANCE:
				// short + byte + short
				size += 2 + 1 + 2; // Estimate, assuming all ascii chars
				break;
			default:
				break;
			}
		}
	}
	return size;
}


//////////////////
// DataItem class
/////////////////

SynchedEntityData::DataItem::DataItem(int type, int id, int value) : type( type ), id( id )
{
	this->value_int = value;
	this->dirty = true;
}

SynchedEntityData::DataItem::DataItem(int type, int id, byte value) : type( type ), id( id )
{
	this->value_byte = value;
	this->dirty = true;
}

SynchedEntityData::DataItem::DataItem(int type, int id, short value) : type( type ), id( id )
{
	this->value_short = value;
	this->dirty = true;
}

SynchedEntityData::DataItem::DataItem(int type, int id, float value) : type( type ), id( id )
{
	this->value_float = value;
	this->dirty = true;
}

SynchedEntityData::DataItem::DataItem(int type, int id, const wstring& value) : type( type ), id( id )
{
	this->value_wstring = value;
	this->dirty = true;
}

SynchedEntityData::DataItem::DataItem(int type, int id, shared_ptr<ItemInstance> itemInstance) : type( type ), id( id )
{
	this->value_itemInstance = itemInstance;
	this->dirty = true;
}

int SynchedEntityData::DataItem::getId()
{
	return id;
}

void SynchedEntityData::DataItem::setValue(int value)
{
	this->value_int = value;
}

void SynchedEntityData::DataItem::setValue(byte value)
{
	this->value_byte = value;
}

void SynchedEntityData::DataItem::setValue(short value)
{
	this->value_short = value;
}

void SynchedEntityData::DataItem::setValue(float value)
{
	this->value_float = value;
}

void SynchedEntityData::DataItem::setValue(const wstring& value)
{
	this->value_wstring = value;
}

void SynchedEntityData::DataItem::setValue(shared_ptr<ItemInstance> itemInstance)
{
	this->value_itemInstance = itemInstance;
}

int SynchedEntityData::DataItem::getValue_int()
{
	return value_int;
}

short SynchedEntityData::DataItem::getValue_short()
{
	return value_short;
}

float SynchedEntityData::DataItem::getValue_float()
{
	return value_float;
}

byte SynchedEntityData::DataItem::getValue_byte()
{
	return value_byte;
}

wstring SynchedEntityData::DataItem::getValue_wstring()
{
	return value_wstring;
}

shared_ptr<ItemInstance> SynchedEntityData::DataItem::getValue_itemInstance()
{
	return value_itemInstance;
}

int SynchedEntityData::DataItem::getType()
{
	return type;
}

bool SynchedEntityData::DataItem::isDirty()
{
	return dirty;
}

void SynchedEntityData::DataItem::setDirty(bool dirty)
{
	this->dirty = dirty;
}