the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
at main 300 lines 6.2 kB view raw
1#pragma once 2#include "Tag.h" 3#include "ListTag.h" 4#include "ByteTag.h" 5#include "DoubleTag.h" 6#include "FloatTag.h" 7#include "IntTag.h" 8#include "LongTag.h" 9#include "ShortTag.h" 10#include "StringTag.h" 11#include "ByteArrayTag.h" 12#include "IntArrayTag.h" 13 14class CompoundTag : public Tag 15{ 16private: 17 unordered_map<wstring, Tag *> tags; 18 19public: 20 CompoundTag() : Tag(L"") {} 21 CompoundTag(const wstring &name) : Tag(name) {} 22 23 void write(DataOutput *dos) 24 { 25 AUTO_VAR(itEnd, tags.end()); 26 for( unordered_map<wstring, Tag *>::iterator it = tags.begin(); it != itEnd; it++ ) 27 { 28 Tag::writeNamedTag(it->second, dos); 29 } 30 dos->writeByte(Tag::TAG_End); 31 } 32 33 34 void load(DataInput *dis, int tagDepth) 35 { 36 if(tagDepth > MAX_DEPTH) 37 { 38#ifndef _CONTENT_PACKAGE 39 printf("Tried to read NBT tag with too high complexity, depth > %d" , MAX_DEPTH); 40 __debugbreak(); 41#endif 42 return; 43 } 44 tags.clear(); 45 Tag *tag; 46 while ((tag = Tag::readNamedTag(dis))->getId() != Tag::TAG_End) 47 { 48 tags[tag->getName()] = tag; 49 } 50 delete tag; 51 } 52 53 vector<Tag *> *getAllTags() // 4J - was collection 54 { 55 // 4J - was return tags.values(); 56 vector<Tag *> *ret = new vector<Tag *>; 57 58 AUTO_VAR(itEnd, tags.end()); 59 for( unordered_map<wstring, Tag *>::iterator it = tags.begin(); it != itEnd; it++ ) 60 { 61 ret->push_back(it->second); 62 } 63 return ret; 64 } 65 66 byte getId() 67 { 68 return TAG_Compound; 69 } 70 71 void put(const wstring &name, Tag *tag) 72 { 73 tags[name] = tag->setName(name); 74 } 75 76 void putByte(const wstring &name, byte value) 77 { 78 tags[name] = (new ByteTag(name,value)); 79 } 80 81 void putShort(const wstring &name, short value) 82 { 83 tags[name] = (new ShortTag(name,value)); 84 } 85 86 void putInt(const wstring &name, int value) 87 { 88 tags[name] = (new IntTag(name,value)); 89 } 90 91 void putLong(const wstring &name, __int64 value) 92 { 93 tags[name] = (new LongTag(name,value)); 94 } 95 96 void putFloat(const wstring &name, float value) 97 { 98 tags[name] = (new FloatTag(name,value)); 99 } 100 101 void putDouble(const wstring &name, double value) 102 { 103 tags[name] = (new DoubleTag(name,value)); 104 } 105 106 void putString(const wstring &name, const wstring& value) 107 { 108 tags[name] = (new StringTag(name,value)); 109 } 110 111 void putByteArray(const wstring &name, byteArray value) 112 { 113 tags[name] = (new ByteArrayTag(name,value)); 114 } 115 116 void putIntArray(const wstring &name, intArray value) 117 { 118 tags[name] = (new IntArrayTag(name, value)); 119 } 120 121 void putCompound(const wstring &name, CompoundTag *value) 122 { 123 tags[name] = value->setName( wstring( name ) ); 124 } 125 126 void putBoolean(const wstring &name, bool val) 127 { 128 putByte(name, val?(byte)1:0); 129 } 130 131 Tag *get(const wstring &name) 132 { 133 AUTO_VAR(it, tags.find(name)); 134 if(it != tags.end()) return it->second; 135 return NULL; 136 } 137 138 bool contains(const wstring &name) 139 { 140 return tags.find(name) != tags.end(); 141 } 142 143 byte getByte(const wstring &name) 144 { 145 if (tags.find(name) == tags.end()) return (byte)0; 146 return ((ByteTag *) tags[name])->data; 147 } 148 149 short getShort(const wstring &name) 150 { 151 if (tags.find(name) == tags.end()) return (short)0; 152 return ((ShortTag *) tags[name])->data; 153 } 154 155 int getInt(const wstring &name) 156 { 157 if (tags.find(name) == tags.end()) return (int)0; 158 return ((IntTag *) tags[name])->data; 159 } 160 161 __int64 getLong(const wstring &name) 162 { 163 if (tags.find(name) == tags.end()) return (__int64)0; 164 return ((LongTag *) tags[name])->data; 165 } 166 167 float getFloat(const wstring &name) 168 { 169 if (tags.find(name) == tags.end()) return (float)0; 170 return ((FloatTag *) tags[name])->data; 171 } 172 173 double getDouble(const wstring &name) 174 { 175 if (tags.find(name) == tags.end()) return (double)0; 176 return ((DoubleTag *) tags[name])->data; 177 } 178 179 wstring getString(const wstring &name) 180 { 181 if (tags.find(name) == tags.end()) return wstring( L"" ); 182 return ((StringTag *) tags[name])->data; 183 } 184 185 byteArray getByteArray(const wstring &name) 186 { 187 if (tags.find(name) == tags.end()) return byteArray(); 188 return ((ByteArrayTag *) tags[name])->data; 189 } 190 191 intArray getIntArray(const wstring &name) 192 { 193 if (tags.find(name) == tags.end()) return intArray(); 194 return ((IntArrayTag *) tags[name])->data; 195 } 196 197 CompoundTag *getCompound(const wstring &name) 198 { 199 if (tags.find(name) == tags.end()) return new CompoundTag(name); 200 return (CompoundTag *) tags[name]; 201 } 202 203 ListTag<Tag> *getList(const wstring &name) 204 { 205 if (tags.find(name) == tags.end()) return new ListTag<Tag>(name); 206 return (ListTag<Tag> *) tags[name]; 207 } 208 209 bool getBoolean(const wstring &string) 210 { 211 return getByte(string)!=0; 212 } 213 214 void remove(const wstring &name) 215 { 216 AUTO_VAR(it, tags.find(name)); 217 if(it != tags.end()) tags.erase(it); 218 //tags.remove(name); 219 } 220 221 wstring toString() 222 { 223 static const int bufSize = 32; 224 static wchar_t buf[bufSize]; 225 swprintf(buf,bufSize,L"%d entries",tags.size()); 226 return wstring( buf ); 227 } 228 229 void print(char *prefix, ostream out) 230 { 231 /* 232 Tag::print(prefix, out); 233 out << prefix << "{" << endl; 234 235 char *newPrefix = new char[ strlen(prefix) + 4 ]; 236 strcpy( newPrefix, prefix); 237 strcat( newPrefix, " "); 238 239 AUTO_VAR(itEnd, tags.end()); 240 for( unordered_map<string, Tag *>::iterator it = tags.begin(); it != itEnd; it++ ) 241 { 242 it->second->print(newPrefix, out); 243 } 244 delete[] newPrefix; 245 out << prefix << "}" << endl; 246 */ 247 } 248 249 bool isEmpty() 250 { 251 return tags.empty(); 252 } 253 254 virtual ~CompoundTag() 255 { 256 AUTO_VAR(itEnd, tags.end()); 257 for( AUTO_VAR(it, tags.begin()); it != itEnd; it++ ) 258 { 259 delete it->second; 260 } 261 } 262 263 Tag *copy() 264 { 265 CompoundTag *tag = new CompoundTag(getName()); 266 267 AUTO_VAR(itEnd, tags.end()); 268 for( AUTO_VAR(it, tags.begin()); it != itEnd; it++ ) 269 { 270 tag->put((wchar_t *)it->first.c_str(), it->second->copy()); 271 } 272 return tag; 273 } 274 275 bool equals(Tag *obj) 276 { 277 if (Tag::equals(obj)) 278 { 279 CompoundTag *o = (CompoundTag *) obj; 280 281 if(tags.size() == o->tags.size()) 282 { 283 bool equal = true; 284 AUTO_VAR(itEnd, tags.end()); 285 for( AUTO_VAR(it, tags.begin()); it != itEnd; it++ ) 286 { 287 AUTO_VAR(itFind, o->tags.find(it->first)); 288 if(itFind == o->tags.end() || !it->second->equals(itFind->second) ) 289 { 290 equal = false; 291 break; 292 } 293 } 294 return equal; 295 //return tags.entrySet().equals(o.tags.entrySet()); 296 } 297 } 298 return false; 299 } 300};