the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
at master 135 lines 3.9 kB view raw
1#pragma once 2using namespace std; 3 4class Pos; 5 6 7class SynchedEntityData 8{ 9public: 10 class DataItem 11 { 12 friend class SynchedEntityData; 13 private: 14 const int type; 15 const int id; 16 // 4J - there used to be one "value" type here of general type Object, just storing the different (used) varieties 17 // here separately for us 18 union { 19 byte value_byte; 20 int value_int; 21 short value_short; 22 float value_float; 23 }; 24 wstring value_wstring; 25 shared_ptr<ItemInstance> value_itemInstance; 26 bool dirty; 27 28 public: 29 // There was one type here that took a generic Object type, using overloading here instead 30 DataItem(int type, int id, byte value); 31 DataItem(int type, int id, int value); 32 DataItem(int type, int id, const wstring& value); 33 DataItem(int type, int id, shared_ptr<ItemInstance> itemInstance); 34 DataItem(int type, int id, short value); 35 DataItem(int type, int id, float value); 36 37 int getId(); 38 void setValue(byte value); 39 void setValue(int value); 40 void setValue(short value); 41 void setValue(float value); 42 void setValue(const wstring& value); 43 void setValue(shared_ptr<ItemInstance> value); 44 byte getValue_byte(); 45 int getValue_int(); 46 short getValue_short(); 47 float getValue_float(); 48 wstring getValue_wstring(); 49 shared_ptr<ItemInstance> getValue_itemInstance(); 50 int getType(); 51 bool isDirty(); 52 void setDirty(bool dirty); 53 }; 54 55public: 56 static const int MAX_STRING_DATA_LENGTH = 64; 57 static const int EOF_MARKER = 0x7f; 58 59 static const int TYPE_BYTE = 0; 60 static const int TYPE_SHORT = 1; 61 static const int TYPE_INT = 2; 62 static const int TYPE_FLOAT = 3; 63 static const int TYPE_STRING = 4; 64 // special types (max possible value is 7): 65 static const int TYPE_ITEMINSTANCE = 5; 66 static const int TYPE_POS = 6; 67 68private: 69 bool m_isEmpty; 70 71 // must have enough bits to fit the type 72private: 73 static const int TYPE_MASK = 0xe0; 74 static const int TYPE_SHIFT = 5; 75 76 // the id value must fit in the remaining bits 77 static const int MAX_ID_VALUE = ~TYPE_MASK & 0xff; 78 79 shared_ptr<DataItem> itemsById[MAX_ID_VALUE+1]; 80 bool m_isDirty; 81 82public: 83 SynchedEntityData(); 84 85 // 4J - this function used to be a template, but there's only 3 varieties of use I've found so just hard-coding now, as 86 // the original had some automatic Class to type sort of conversion that's a real pain for us to actually do 87 void define(int id, byte value); 88 void define(int id, const wstring& value); 89 void define(int id, int value); 90 void define(int id, short value); 91 void define(int id, float value); 92 void defineNULL(int id, void *pVal); 93 94 void checkId(int id); // 4J - added to contain common code from overloaded define functions above 95 byte getByte(int id); 96 short getShort(int id); 97 int getInteger(int id); 98 float getFloat(int id); 99 wstring getString(int id); 100 shared_ptr<ItemInstance> getItemInstance(int id); 101 Pos *getPos(int id); 102 // 4J - using overloads rather than template here 103 void set(int id, byte value); 104 void set(int id, int value); 105 void set(int id, short value); 106 void set(int id, float value); 107 void set(int id, const wstring& value); 108 void set(int id, shared_ptr<ItemInstance>); 109 void markDirty(int id); 110 bool isDirty(); 111 static void pack(vector<shared_ptr<DataItem> > *items, DataOutputStream *output); // TODO throws IOException 112 vector<shared_ptr<DataItem> > *packDirty(); 113 void packAll(DataOutputStream *output); // throws IOException 114 vector<shared_ptr<DataItem> > *getAll(); 115 116private: 117 static void writeDataItem(DataOutputStream *output, shared_ptr<DataItem> dataItem); //throws IOException 118 119 120public: 121 static vector<shared_ptr<DataItem> > *unpack(DataInputStream *input); // throws IOException 122 123 /** 124 * Assigns values from a list of data items. 125 * 126 * @param items 127 */ 128public: 129 void assignValues(vector<shared_ptr<DataItem> > *items); 130 bool isEmpty(); 131 void clearDirty(); 132 133 // 4J Added 134 int getSizeInBytes(); 135};