| @@ -0,0 +1,1088 @@ | |||
| 1 | + | ////////////////////////////////////////////////////////////////////// | |
| 2 | + | // This file is part of Remere's Map Editor | |
| 3 | + | ////////////////////////////////////////////////////////////////////// | |
| 4 | + | // Remere's Map Editor is free software: you can redistribute it and/or modify | |
| 5 | + | // it under the terms of the GNU General Public License as published by | |
| 6 | + | // the Free Software Foundation, either version 3 of the License, or | |
| 7 | + | // (at your option) any later version. | |
| 8 | + | // | |
| 9 | + | // Remere's Map Editor is distributed in the hope that it will be useful, | |
| 10 | + | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11 | + | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 12 | + | // GNU General Public License for more details. | |
| 13 | + | // | |
| 14 | + | // You should have received a copy of the GNU General Public License | |
| 15 | + | // along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 16 | + | ////////////////////////////////////////////////////////////////////// | |
| 17 | + | ||
| 18 | + | #include "main.h" | |
| 19 | + | ||
| 20 | + | #include "materials.h" | |
| 21 | + | #include "gui.h" | |
| 22 | + | #include <string.h> // memcpy | |
| 23 | + | ||
| 24 | + | #include "items.h" | |
| 25 | + | #include "item.h" | |
| 26 | + | ||
| 27 | + | ItemDatabase g_items; | |
| 28 | + | ||
| 29 | + | ItemType::ItemType() : | |
| 30 | + | sprite(nullptr), | |
| 31 | + | id(0), | |
| 32 | + | clientID(0), | |
| 33 | + | brush(nullptr), | |
| 34 | + | doodad_brush(nullptr), | |
| 35 | + | collection_brush(nullptr), | |
| 36 | + | raw_brush(nullptr), | |
| 37 | + | is_metaitem(false), | |
| 38 | + | has_raw(false), | |
| 39 | + | in_other_tileset(false), | |
| 40 | + | group(ITEM_GROUP_NONE), | |
| 41 | + | type(ITEM_TYPE_NONE), | |
| 42 | + | volume(0), | |
| 43 | + | maxTextLen(0), | |
| 44 | + | // writeOnceItemID(0), | |
| 45 | + | ground_equivalent(0), | |
| 46 | + | border_group(0), | |
| 47 | + | has_equivalent(false), | |
| 48 | + | wall_hate_me(false), | |
| 49 | + | name(""), | |
| 50 | + | description(""), | |
| 51 | + | weight(0.0f), | |
| 52 | + | attack(0), | |
| 53 | + | defense(0), | |
| 54 | + | armor(0), | |
| 55 | + | charges(0), | |
| 56 | + | slot_position(SLOTP_HAND), | |
| 57 | + | weapon_type(WEAPON_NONE), | |
| 58 | + | client_chargeable(false), | |
| 59 | + | extra_chargeable(false), | |
| 60 | + | ignoreLook(false), | |
| 61 | + | ||
| 62 | + | isHangable(false), | |
| 63 | + | hookEast(false), | |
| 64 | + | hookSouth(false), | |
| 65 | + | canReadText(false), | |
| 66 | + | canWriteText(false), | |
| 67 | + | replaceable(true), | |
| 68 | + | decays(false), | |
| 69 | + | stackable(false), | |
| 70 | + | moveable(true), | |
| 71 | + | alwaysOnBottom(false), | |
| 72 | + | pickupable(false), | |
| 73 | + | rotable(false), | |
| 74 | + | isBorder(false), | |
| 75 | + | isOptionalBorder(false), | |
| 76 | + | isWall(false), | |
| 77 | + | isBrushDoor(false), | |
| 78 | + | isOpen(false), | |
| 79 | + | isTable(false), | |
| 80 | + | isCarpet(false), | |
| 81 | + | ||
| 82 | + | floorChangeDown(false), | |
| 83 | + | floorChangeNorth(false), | |
| 84 | + | floorChangeSouth(false), | |
| 85 | + | floorChangeEast(false), | |
| 86 | + | floorChangeWest(false), | |
| 87 | + | floorChange(false), | |
| 88 | + | ||
| 89 | + | unpassable(false), | |
| 90 | + | blockPickupable(false), | |
| 91 | + | blockMissiles(false), | |
| 92 | + | blockPathfinder(false), | |
| 93 | + | hasElevation(false), | |
| 94 | + | ||
| 95 | + | alwaysOnTopOrder(0), | |
| 96 | + | rotateTo(0), | |
| 97 | + | border_alignment(BORDER_NONE) { | |
| 98 | + | //// | |
| 99 | + | } | |
| 100 | + | ||
| 101 | + | ItemType::~ItemType() { | |
| 102 | + | //// | |
| 103 | + | } | |
| 104 | + | ||
| 105 | + | bool ItemType::isFloorChange() const { | |
| 106 | + | return floorChange || floorChangeDown || floorChangeNorth || floorChangeSouth || floorChangeEast || floorChangeWest; | |
| 107 | + | } | |
| 108 | + | ||
| 109 | + | ItemDatabase::ItemDatabase() : | |
| 110 | + | // Version information | |
| 111 | + | MajorVersion(0), | |
| 112 | + | MinorVersion(0), | |
| 113 | + | BuildNumber(0), | |
| 114 | + | ||
| 115 | + | // Count of GameSprite types | |
| 116 | + | item_count(0), | |
| 117 | + | effect_count(0), | |
| 118 | + | monster_count(0), | |
| 119 | + | distance_count(0), | |
| 120 | + | ||
| 121 | + | minclientID(0), | |
| 122 | + | maxclientID(0), | |
| 123 | + | ||
| 124 | + | max_item_id(0) { | |
| 125 | + | //// | |
| 126 | + | } | |
| 127 | + | ||
| 128 | + | ItemDatabase::~ItemDatabase() { | |
| 129 | + | clear(); | |
| 130 | + | } | |
| 131 | + | ||
| 132 | + | void ItemDatabase::clear() { | |
| 133 | + | for (uint32_t i = 0; i < items.size(); i++) { | |
| 134 | + | delete items[i]; | |
| 135 | + | items.set(i, nullptr); | |
| 136 | + | } | |
| 137 | + | } | |
| 138 | + | ||
| 139 | + | bool ItemDatabase::loadFromOtbVer1(BinaryNode* itemNode, wxString& error, wxArrayString& warnings) { | |
| 140 | + | uint8_t u8; | |
| 141 | + | ||
| 142 | + | for (; itemNode != nullptr; itemNode = itemNode->advance()) { | |
| 143 | + | if (!itemNode->getU8(u8)) { | |
| 144 | + | // Invalid! | |
| 145 | + | warnings.push_back("Invalid item type encountered..."); | |
| 146 | + | continue; | |
| 147 | + | } | |
| 148 | + | ||
| 149 | + | if (u8 == ITEM_GROUP_DEPRECATED) { | |
| 150 | + | continue; | |
| 151 | + | } | |
| 152 | + | ||
| 153 | + | ItemType* t = newd ItemType(); | |
| 154 | + | t->group = ItemGroup_t(u8); | |
| 155 | + | ||
| 156 | + | switch (t->group) { | |
| 157 | + | case ITEM_GROUP_NONE: | |
| 158 | + | case ITEM_GROUP_GROUND: | |
| 159 | + | case ITEM_GROUP_SPLASH: | |
| 160 | + | case ITEM_GROUP_FLUID: | |
| 161 | + | case ITEM_GROUP_WEAPON: | |
| 162 | + | case ITEM_GROUP_AMMUNITION: | |
| 163 | + | case ITEM_GROUP_ARMOR: | |
| 164 | + | case ITEM_GROUP_WRITEABLE: | |
| 165 | + | case ITEM_GROUP_KEY: | |
| 166 | + | break; | |
| 167 | + | case ITEM_GROUP_DOOR: | |
| 168 | + | t->type = ITEM_TYPE_DOOR; | |
| 169 | + | break; | |
| 170 | + | case ITEM_GROUP_CONTAINER: | |
| 171 | + | t->type = ITEM_TYPE_CONTAINER; | |
| 172 | + | break; | |
| 173 | + | case ITEM_GROUP_RUNE: | |
| 174 | + | t->client_chargeable = true; | |
| 175 | + | break; | |
| 176 | + | case ITEM_GROUP_TELEPORT: | |
| 177 | + | t->type = ITEM_TYPE_TELEPORT; | |
| 178 | + | break; | |
| 179 | + | case ITEM_GROUP_MAGICFIELD: | |
| 180 | + | t->type = ITEM_TYPE_MAGICFIELD; | |
| 181 | + | break; | |
| 182 | + | default: | |
| 183 | + | warnings.push_back("Unknown item group declaration"); | |
| 184 | + | } | |
| 185 | + | ||
| 186 | + | uint32_t flags; | |
| 187 | + | if (itemNode->getU32(flags)) { | |
| 188 | + | t->unpassable = ((flags & FLAG_UNPASSABLE) == FLAG_UNPASSABLE); | |
| 189 | + | t->blockMissiles = ((flags & FLAG_BLOCK_MISSILES) == FLAG_BLOCK_MISSILES); | |
| 190 | + | t->blockPathfinder = ((flags & FLAG_BLOCK_PATHFINDER) == FLAG_BLOCK_PATHFINDER); | |
| 191 | + | t->hasElevation = ((flags & FLAG_HAS_ELEVATION) == FLAG_HAS_ELEVATION); | |
| 192 | + | // t->useable = ((flags & FLAG_USEABLE) == FLAG_USEABLE); | |
| 193 | + | t->pickupable = ((flags & FLAG_PICKUPABLE) == FLAG_PICKUPABLE); | |
| 194 | + | t->moveable = ((flags & FLAG_MOVEABLE) == FLAG_MOVEABLE); | |
| 195 | + | t->stackable = ((flags & FLAG_STACKABLE) == FLAG_STACKABLE); | |
| 196 | + | t->floorChangeDown = ((flags & FLAG_FLOORCHANGEDOWN) == FLAG_FLOORCHANGEDOWN); | |
| 197 | + | t->floorChangeNorth = ((flags & FLAG_FLOORCHANGENORTH) == FLAG_FLOORCHANGENORTH); | |
| 198 | + | t->floorChangeEast = ((flags & FLAG_FLOORCHANGEEAST) == FLAG_FLOORCHANGEEAST); | |
| 199 | + | t->floorChangeSouth = ((flags & FLAG_FLOORCHANGESOUTH) == FLAG_FLOORCHANGESOUTH); | |
| 200 | + | t->floorChangeWest = ((flags & FLAG_FLOORCHANGEWEST) == FLAG_FLOORCHANGEWEST); | |
| 201 | + | t->floorChange = t->floorChangeDown || t->floorChangeNorth || t->floorChangeEast || t->floorChangeSouth || t->floorChangeWest; | |
| 202 | + | // Now this is confusing, just accept that the ALWAYSONTOP flag means it's always on bottom, got it?! | |
| 203 | + | t->alwaysOnBottom = ((flags & FLAG_ALWAYSONTOP) == FLAG_ALWAYSONTOP); | |
| 204 | + | t->isHangable = ((flags & FLAG_HANGABLE) == FLAG_HANGABLE); | |
| 205 | + | t->hookEast = ((flags & FLAG_HOOK_EAST) == FLAG_HOOK_EAST); | |
| 206 | + | t->hookSouth = ((flags & FLAG_HOOK_SOUTH) == FLAG_HOOK_SOUTH); | |
| 207 | + | t->allowDistRead = ((flags & FLAG_ALLOWDISTREAD) == FLAG_ALLOWDISTREAD); | |
| 208 | + | t->rotable = ((flags & FLAG_ROTABLE) == FLAG_ROTABLE); | |
| 209 | + | t->canReadText = ((flags & FLAG_READABLE) == FLAG_READABLE); | |
| 210 | + | } | |
| 211 | + | ||
| 212 | + | uint8_t attribute; | |
| 213 | + | while (itemNode->getU8(attribute)) { | |
| 214 | + | uint16_t datalen; | |
| 215 | + | if (!itemNode->getU16(datalen)) { | |
| 216 | + | warnings.push_back("Invalid item type property"); | |
| 217 | + | break; | |
| 218 | + | } | |
| 219 | + | ||
| 220 | + | switch (attribute) { | |
| 221 | + | case ITEM_ATTR_SERVERID: { | |
| 222 | + | if (datalen != sizeof(uint16_t)) { | |
| 223 | + | error = "items.otb: Unexpected data length of server id block (Should be 2 bytes)"; | |
| 224 | + | return false; | |
| 225 | + | } | |
| 226 | + | if (!itemNode->getU16(t->id)) { | |
| 227 | + | warnings.push_back("Invalid item type property (2)"); | |
| 228 | + | } | |
| 229 | + | ||
| 230 | + | if (max_item_id < t->id) { | |
| 231 | + | max_item_id = t->id; | |
| 232 | + | } | |
| 233 | + | break; | |
| 234 | + | } | |
| 235 | + | ||
| 236 | + | case ITEM_ATTR_CLIENTID: { | |
| 237 | + | if (datalen != sizeof(uint16_t)) { | |
| 238 | + | error = "items.otb: Unexpected data length of client id block (Should be 2 bytes)"; | |
| 239 | + | return false; | |
| 240 | + | } | |
| 241 | + | ||
| 242 | + | if (!itemNode->getU16(t->clientID)) { | |
| 243 | + | warnings.push_back("Invalid item type property (2)"); | |
| 244 | + | } | |
| 245 | + | ||
| 246 | + | t->sprite = static_cast<GameSprite*>(g_gui.gfx.getSprite(t->clientID)); | |
| 247 | + | break; | |
| 248 | + | } | |
| 249 | + | ||
| 250 | + | case ITEM_ATTR_SPEED: { | |
| 251 | + | if (datalen != sizeof(uint16_t)) { | |
| 252 | + | error = "items.otb: Unexpected data length of speed block (Should be 2 bytes)"; | |
| 253 | + | return false; | |
| 254 | + | } | |
| 255 | + | ||
| 256 | + | // t->speed = itemNode->getU16(); | |
| 257 | + | if (!itemNode->skip(2)) { // Just skip two bytes, we don't need speed | |
| 258 | + | warnings.push_back("Invalid item type property (3)"); | |
| 259 | + | } | |
| 260 | + | break; | |
| 261 | + | } | |
| 262 | + | ||
| 263 | + | case ITEM_ATTR_LIGHT2: { | |
| 264 | + | if (datalen != sizeof(lightBlock2)) { | |
| 265 | + | warnings.push_back("items.otb: Unexpected data length of item light (2) block (Should be " + i2ws(sizeof(lightBlock2)) + " bytes)"); | |
| 266 | + | break; | |
| 267 | + | } | |
| 268 | + | ||
| 269 | + | if (!itemNode->skip(4)) { // Just skip two bytes, we don't need light | |
| 270 | + | warnings.push_back("Invalid item type property (4)"); | |
| 271 | + | } | |
| 272 | + | ||
| 273 | + | // t->lightLevel = itemNode->getU16(); | |
| 274 | + | // t->lightColor = itemNode->getU16(); | |
| 275 | + | break; | |
| 276 | + | } | |
| 277 | + | ||
| 278 | + | case ITEM_ATTR_TOPORDER: { | |
| 279 | + | if (datalen != sizeof(uint8_t)) { | |
| 280 | + | warnings.push_back("items.otb: Unexpected data length of item toporder block (Should be 1 byte)"); | |
| 281 | + | break; | |
| 282 | + | } | |
| 283 | + | ||
| 284 | + | uint8_t u8 = 0; | |
| 285 | + | if (!itemNode->getU8(u8)) { | |
| 286 | + | warnings.push_back("Invalid item type property (5)"); | |
| 287 | + | } | |
| 288 | + | ||
| 289 | + | t->alwaysOnTopOrder = u8; | |
| 290 | + | break; | |
| 291 | + | } | |
| 292 | + | ||
| 293 | + | case ITEM_ATTR_NAME: { | |
| 294 | + | if (datalen >= 128) { | |
| 295 | + | warnings.push_back("items.otb: Unexpected data length of item name block (Should be 128 bytes)"); | |
| 296 | + | break; | |
| 297 | + | } | |
| 298 | + | ||
| 299 | + | uint8_t name[128]; | |
| 300 | + | memset(&name, 0, 128); | |
| 301 | + | ||
| 302 | + | if (!itemNode->getRAW(name, datalen)) { | |
| 303 | + | warnings.push_back("Invalid item type property (6)"); | |
| 304 | + | break; | |
| 305 | + | } | |
| 306 | + | t->name = (char*)name; | |
| 307 | + | break; | |
| 308 | + | } | |
| 309 | + | ||
| 310 | + | case ITEM_ATTR_DESCR: { | |
| 311 | + | if (datalen >= 128) { | |
| 312 | + | warnings.push_back("items.otb: Unexpected data length of item descr block (Should be 128 bytes)"); | |
| 313 | + | break; | |
| 314 | + | } | |
| 315 | + | ||
| 316 | + | uint8_t description[128]; | |
| 317 | + | memset(&description, 0, 128); | |
| 318 | + | ||
| 319 | + | if (!itemNode->getRAW(description, datalen)) { | |
| 320 | + | warnings.push_back("Invalid item type property (7)"); | |
| 321 | + | break; | |
| 322 | + | } | |
| 323 | + | ||
| 324 | + | t->description = (char*)description; | |
| 325 | + | break; | |
| 326 | + | } | |
| 327 | + | ||
| 328 | + | case ITEM_ATTR_MAXITEMS: { | |
| 329 | + | if (datalen != sizeof(unsigned short)) { | |
| 330 | + | warnings.push_back("items.otb: Unexpected data length of item volume block (Should be 2 bytes)"); | |
| 331 | + | break; | |
| 332 | + | } | |
| 333 | + | ||
| 334 | + | if (!itemNode->getU16(t->volume)) { | |
| 335 | + | warnings.push_back("Invalid item type property (8)"); | |
| 336 | + | } | |
| 337 | + | break; | |
| 338 | + | } | |
| 339 | + | ||
| 340 | + | case ITEM_ATTR_WEIGHT: { | |
| 341 | + | if (datalen != sizeof(double)) { | |
| 342 | + | warnings.push_back("items.otb: Unexpected data length of item weight block (Should be 8 bytes)"); | |
| 343 | + | break; | |
| 344 | + | } | |
| 345 | + | uint8_t w[sizeof(double)]; | |
| 346 | + | if (!itemNode->getRAW(w, sizeof(double))) { | |
| 347 | + | warnings.push_back("Invalid item type property (7)"); | |
| 348 | + | break; | |
| 349 | + | } | |
| 350 | + | ||
| 351 | + | double wi = *reinterpret_cast<double*>(&w); | |
| 352 | + | t->weight = wi; | |
| 353 | + | break; | |
| 354 | + | } | |
| 355 | + | ||
| 356 | + | case ITEM_ATTR_ROTATETO: { | |
| 357 | + | if (datalen != sizeof(unsigned short)) { | |
| 358 | + | warnings.push_back("items.otb: Unexpected data length of item rotateTo block (Should be 2 bytes)"); | |
| 359 | + | break; | |
| 360 | + | } | |
| 361 | + | ||
| 362 | + | uint16_t rotate; | |
| 363 | + | if (!itemNode->getU16(rotate)) { | |
| 364 | + | warnings.push_back("Invalid item type property (8)"); | |
| 365 | + | break; | |
| 366 | + | } | |
| 367 | + | ||
| 368 | + | t->rotateTo = rotate; | |
| 369 | + | break; | |
| 370 | + | } | |
| 371 | + | ||
| 372 | + | case ITEM_ATTR_WRITEABLE3: { | |
| 373 | + | if (datalen != sizeof(writeableBlock3)) { | |
| 374 | + | warnings.push_back("items.otb: Unexpected data length of item toporder block (Should be 1 byte)"); | |
| 375 | + | break; | |
| 376 | + | } | |
| 377 | + | ||
| 378 | + | uint16_t readOnlyID; | |
| 379 | + | uint16_t maxTextLen; | |
| 380 | + | ||
| 381 | + | if (!itemNode->getU16(readOnlyID)) { | |
| 382 | + | warnings.push_back("Invalid item type property (9)"); | |
| 383 | + | break; | |
| 384 | + | } | |
| 385 | + | ||
| 386 | + | if (!itemNode->getU16(maxTextLen)) { | |
| 387 | + | warnings.push_back("Invalid item type property (10)"); | |
| 388 | + | break; | |
| 389 | + | } | |
| 390 | + | ||
| 391 | + | // t->readOnlyId = wb3->readOnlyId; | |
| 392 | + | t->maxTextLen = maxTextLen; | |
| 393 | + | break; | |
| 394 | + | } | |
| 395 | + | ||
| 396 | + | default: { | |
| 397 | + | // skip unknown attributes | |
| 398 | + | itemNode->skip(datalen); | |
| 399 | + | // warnings.push_back("items.otb: Skipped unknown attribute"); | |
| 400 | + | break; | |
| 401 | + | } | |
| 402 | + | } | |
| 403 | + | } | |
| 404 | + | ||
| 405 | + | if (t) { | |
| 406 | + | if (items[t->id]) { | |
| 407 | + | warnings.push_back("items.otb: Duplicate items"); | |
| 408 | + | delete items[t->id]; | |
| 409 | + | } | |
| 410 | + | items.set(t->id, t); | |
| 411 | + | } | |
| 412 | + | } | |
| 413 | + | return true; | |
| 414 | + | } | |
| 415 | + | ||
| 416 | + | bool ItemDatabase::loadFromOtbVer2(BinaryNode* itemNode, wxString& error, wxArrayString& warnings) { | |
| 417 | + | uint8_t u8; | |
| 418 | + | for (; itemNode != nullptr; itemNode = itemNode->advance()) { | |
| 419 | + | if (!itemNode->getU8(u8)) { | |
| 420 | + | // Invalid! | |
| 421 | + | warnings.push_back("Invalid item type encountered..."); | |
| 422 | + | continue; | |
| 423 | + | } | |
| 424 | + | ||
| 425 | + | if (ItemGroup_t(u8) == ITEM_GROUP_DEPRECATED) { | |
| 426 | + | continue; | |
| 427 | + | } | |
| 428 | + | ||
| 429 | + | ItemType* t = newd ItemType(); | |
| 430 | + | t->group = ItemGroup_t(u8); | |
| 431 | + | ||
| 432 | + | switch (t->group) { | |
| 433 | + | case ITEM_GROUP_NONE: | |
| 434 | + | case ITEM_GROUP_GROUND: | |
| 435 | + | case ITEM_GROUP_SPLASH: | |
| 436 | + | case ITEM_GROUP_FLUID: | |
| 437 | + | break; | |
| 438 | + | case ITEM_GROUP_DOOR: | |
| 439 | + | t->type = ITEM_TYPE_DOOR; | |
| 440 | + | break; | |
| 441 | + | case ITEM_GROUP_CONTAINER: | |
| 442 | + | t->type = ITEM_TYPE_CONTAINER; | |
| 443 | + | break; | |
| 444 | + | case ITEM_GROUP_RUNE: | |
| 445 | + | t->client_chargeable = true; | |
| 446 | + | break; | |
| 447 | + | case ITEM_GROUP_TELEPORT: | |
| 448 | + | t->type = ITEM_TYPE_TELEPORT; | |
| 449 | + | break; | |
| 450 | + | case ITEM_GROUP_MAGICFIELD: | |
| 451 | + | t->type = ITEM_TYPE_MAGICFIELD; | |
| 452 | + | break; | |
| 453 | + | default: | |
| 454 | + | warnings.push_back("Unknown item group declaration"); | |
| 455 | + | } | |
| 456 | + | ||
| 457 | + | uint32_t flags; | |
| 458 | + | if (itemNode->getU32(flags)) { | |
| 459 | + | t->unpassable = ((flags & FLAG_UNPASSABLE) == FLAG_UNPASSABLE); | |
| 460 | + | t->blockMissiles = ((flags & FLAG_BLOCK_MISSILES) == FLAG_BLOCK_MISSILES); | |
| 461 | + | t->blockPathfinder = ((flags & FLAG_BLOCK_PATHFINDER) == FLAG_BLOCK_PATHFINDER); | |
| 462 | + | t->hasElevation = ((flags & FLAG_HAS_ELEVATION) == FLAG_HAS_ELEVATION); | |
| 463 | + | t->pickupable = ((flags & FLAG_PICKUPABLE) == FLAG_PICKUPABLE); | |
| 464 | + | t->moveable = ((flags & FLAG_MOVEABLE) == FLAG_MOVEABLE); | |
| 465 | + | t->stackable = ((flags & FLAG_STACKABLE) == FLAG_STACKABLE); | |
| 466 | + | t->floorChangeDown = ((flags & FLAG_FLOORCHANGEDOWN) == FLAG_FLOORCHANGEDOWN); | |
| 467 | + | t->floorChangeNorth = ((flags & FLAG_FLOORCHANGENORTH) == FLAG_FLOORCHANGENORTH); | |
| 468 | + | t->floorChangeEast = ((flags & FLAG_FLOORCHANGEEAST) == FLAG_FLOORCHANGEEAST); | |
| 469 | + | t->floorChangeSouth = ((flags & FLAG_FLOORCHANGESOUTH) == FLAG_FLOORCHANGESOUTH); | |
| 470 | + | t->floorChangeWest = ((flags & FLAG_FLOORCHANGEWEST) == FLAG_FLOORCHANGEWEST); | |
| 471 | + | t->floorChange = t->floorChangeDown || t->floorChangeNorth || t->floorChangeEast || t->floorChangeSouth || t->floorChangeWest; | |
| 472 | + | // Now this is confusing, just accept that the ALWAYSONTOP flag means it's always on bottom, got it?! | |
| 473 | + | t->alwaysOnBottom = ((flags & FLAG_ALWAYSONTOP) == FLAG_ALWAYSONTOP); | |
| 474 | + | t->isHangable = ((flags & FLAG_HANGABLE) == FLAG_HANGABLE); | |
| 475 | + | t->hookEast = ((flags & FLAG_HOOK_EAST) == FLAG_HOOK_EAST); | |
| 476 | + | t->hookSouth = ((flags & FLAG_HOOK_SOUTH) == FLAG_HOOK_SOUTH); | |
| 477 | + | t->allowDistRead = ((flags & FLAG_ALLOWDISTREAD) == FLAG_ALLOWDISTREAD); | |
| 478 | + | t->rotable = ((flags & FLAG_ROTABLE) == FLAG_ROTABLE); | |
| 479 | + | t->canReadText = ((flags & FLAG_READABLE) == FLAG_READABLE); | |
| 480 | + | } | |
| 481 | + | ||
| 482 | + | uint8_t attribute; | |
| 483 | + | while (itemNode->getU8(attribute)) { | |
| 484 | + | uint16_t datalen; | |
| 485 | + | if (!itemNode->getU16(datalen)) { | |
| 486 | + | warnings.push_back("Invalid item type property"); | |
| 487 | + | break; | |
| 488 | + | } | |
| 489 | + | ||
| 490 | + | switch (attribute) { | |
| 491 | + | case ITEM_ATTR_SERVERID: { | |
| 492 | + | if (datalen != sizeof(uint16_t)) { | |
| 493 | + | error = "items.otb: Unexpected data length of server id block (Should be 2 bytes)"; | |
| 494 | + | return false; | |
| 495 | + | } | |
| 496 | + | ||
| 497 | + | if (!itemNode->getU16(t->id)) { | |
| 498 | + | warnings.push_back("Invalid item type property (2)"); | |
| 499 | + | } | |
| 500 | + | ||
| 501 | + | if (max_item_id < t->id) { | |
| 502 | + | max_item_id = t->id; | |
| 503 | + | } | |
| 504 | + | break; | |
| 505 | + | } | |
| 506 | + | ||
| 507 | + | case ITEM_ATTR_CLIENTID: { | |
| 508 | + | if (datalen != sizeof(uint16_t)) { | |
| 509 | + | error = "items.otb: Unexpected data length of client id block (Should be 2 bytes)"; | |
| 510 | + | return false; | |
| 511 | + | } | |
| 512 | + | ||
| 513 | + | if (!itemNode->getU16(t->clientID)) { | |
| 514 | + | warnings.push_back("Invalid item type property (2)"); | |
| 515 | + | } | |
| 516 | + | ||
| 517 | + | t->sprite = static_cast<GameSprite*>(g_gui.gfx.getSprite(t->clientID)); | |
| 518 | + | break; | |
| 519 | + | } | |
| 520 | + | ||
| 521 | + | case ITEM_ATTR_SPEED: { | |
| 522 | + | if (datalen != sizeof(uint16_t)) { | |
| 523 | + | error = "items.otb: Unexpected data length of speed block (Should be 2 bytes)"; | |
| 524 | + | return false; | |
| 525 | + | } | |
| 526 | + | ||
| 527 | + | // t->speed = itemNode->getU16(); | |
| 528 | + | if (!itemNode->skip(2)) { // Just skip two bytes, we don't need speed | |
| 529 | + | warnings.push_back("Invalid item type property (3)"); | |
| 530 | + | } | |
| 531 | + | break; | |
| 532 | + | } | |
| 533 | + | ||
| 534 | + | case ITEM_ATTR_LIGHT2: { | |
| 535 | + | if (datalen != sizeof(lightBlock2)) { | |
| 536 | + | warnings.push_back("items.otb: Unexpected data length of item light (2) block (Should be " + i2ws(sizeof(lightBlock2)) + " bytes)"); | |
| 537 | + | break; | |
| 538 | + | } | |
| 539 | + | ||
| 540 | + | if (!itemNode->skip(4)) { // Just skip two bytes, we don't need light | |
| 541 | + | warnings.push_back("Invalid item type property (4)"); | |
| 542 | + | } | |
| 543 | + | ||
| 544 | + | // t->lightLevel = itemNode->getU16(); | |
| 545 | + | // t->lightColor = itemNode->getU16(); | |
| 546 | + | break; | |
| 547 | + | } | |
| 548 | + | ||
| 549 | + | case ITEM_ATTR_TOPORDER: { | |
| 550 | + | if (datalen != sizeof(uint8_t)) { | |
| 551 | + | warnings.push_back("items.otb: Unexpected data length of item toporder block (Should be 1 byte)"); | |
| 552 | + | break; | |
| 553 | + | } | |
| 554 | + | ||
| 555 | + | uint8_t u8 = 0; | |
| 556 | + | if (!itemNode->getU8(u8)) { | |
| 557 | + | warnings.push_back("Invalid item type property (5)"); | |
| 558 | + | } | |
| 559 | + | t->alwaysOnTopOrder = u8; | |
| 560 | + | break; | |
| 561 | + | } | |
| 562 | + | ||
| 563 | + | default: { | |
| 564 | + | // skip unknown attributes | |
| 565 | + | itemNode->skip(datalen); | |
| 566 | + | // warnings.push_back("items.otb: Skipped unknown attribute"); | |
| 567 | + | break; | |
| 568 | + | } | |
| 569 | + | } | |
| 570 | + | } | |
| 571 | + | ||
| 572 | + | if (t) { | |
| 573 | + | if (items[t->id]) { | |
| 574 | + | warnings.push_back("items.otb: Duplicate items"); | |
| 575 | + | delete items[t->id]; | |
| 576 | + | } | |
| 577 | + | items.set(t->id, t); | |
| 578 | + | } | |
| 579 | + | } | |
| 580 | + | return true; | |
| 581 | + | } | |
| 582 | + | ||
| 583 | + | bool ItemDatabase::loadFromOtbVer3(BinaryNode* itemNode, wxString& error, wxArrayString& warnings) { | |
| 584 | + | uint8_t u8; | |
| 585 | + | for (; itemNode != nullptr; itemNode = itemNode->advance()) { | |
| 586 | + | if (!itemNode->getU8(u8)) { | |
| 587 | + | // Invalid! | |
| 588 | + | warnings.push_back("Invalid item type encountered..."); | |
| 589 | + | continue; | |
| 590 | + | } | |
| 591 | + | ||
| 592 | + | if (ItemGroup_t(u8) == ITEM_GROUP_DEPRECATED) { | |
| 593 | + | continue; | |
| 594 | + | } | |
| 595 | + | ||
| 596 | + | ItemType* t = newd ItemType(); | |
| 597 | + | t->group = ItemGroup_t(u8); | |
| 598 | + | ||
| 599 | + | switch (t->group) { | |
| 600 | + | case ITEM_GROUP_NONE: | |
| 601 | + | case ITEM_GROUP_GROUND: | |
| 602 | + | case ITEM_GROUP_SPLASH: | |
| 603 | + | case ITEM_GROUP_FLUID: | |
| 604 | + | break; | |
| 605 | + | case ITEM_GROUP_CONTAINER: | |
| 606 | + | t->type = ITEM_TYPE_CONTAINER; | |
| 607 | + | break; | |
| 608 | + | case ITEM_GROUP_PODIUM: | |
| 609 | + | t->type = ITEM_TYPE_PODIUM; | |
| 610 | + | break; | |
| 611 | + | default: | |
| 612 | + | warnings.push_back("Unknown item group declaration"); | |
| 613 | + | } | |
| 614 | + | ||
| 615 | + | uint32_t flags; | |
| 616 | + | if (itemNode->getU32(flags)) { | |
| 617 | + | t->unpassable = ((flags & FLAG_UNPASSABLE) == FLAG_UNPASSABLE); | |
| 618 | + | t->blockMissiles = ((flags & FLAG_BLOCK_MISSILES) == FLAG_BLOCK_MISSILES); | |
| 619 | + | t->blockPathfinder = ((flags & FLAG_BLOCK_PATHFINDER) == FLAG_BLOCK_PATHFINDER); | |
| 620 | + | t->hasElevation = ((flags & FLAG_HAS_ELEVATION) == FLAG_HAS_ELEVATION); | |
| 621 | + | t->pickupable = ((flags & FLAG_PICKUPABLE) == FLAG_PICKUPABLE); | |
| 622 | + | t->moveable = ((flags & FLAG_MOVEABLE) == FLAG_MOVEABLE); | |
| 623 | + | t->stackable = ((flags & FLAG_STACKABLE) == FLAG_STACKABLE); | |
| 624 | + | t->floorChangeDown = ((flags & FLAG_FLOORCHANGEDOWN) == FLAG_FLOORCHANGEDOWN); | |
| 625 | + | t->floorChangeNorth = ((flags & FLAG_FLOORCHANGENORTH) == FLAG_FLOORCHANGENORTH); | |
| 626 | + | t->floorChangeEast = ((flags & FLAG_FLOORCHANGEEAST) == FLAG_FLOORCHANGEEAST); | |
| 627 | + | t->floorChangeSouth = ((flags & FLAG_FLOORCHANGESOUTH) == FLAG_FLOORCHANGESOUTH); | |
| 628 | + | t->floorChangeWest = ((flags & FLAG_FLOORCHANGEWEST) == FLAG_FLOORCHANGEWEST); | |
| 629 | + | t->floorChange = t->floorChangeDown || t->floorChangeNorth || t->floorChangeEast || t->floorChangeSouth || t->floorChangeWest; | |
| 630 | + | // Now this is confusing, just accept that the ALWAYSONTOP flag means it's always on bottom, got it?! | |
| 631 | + | t->alwaysOnBottom = ((flags & FLAG_ALWAYSONTOP) == FLAG_ALWAYSONTOP); | |
| 632 | + | t->isHangable = ((flags & FLAG_HANGABLE) == FLAG_HANGABLE); | |
| 633 | + | t->hookEast = ((flags & FLAG_HOOK_EAST) == FLAG_HOOK_EAST); | |
| 634 | + | t->hookSouth = ((flags & FLAG_HOOK_SOUTH) == FLAG_HOOK_SOUTH); | |
| 635 | + | t->allowDistRead = ((flags & FLAG_ALLOWDISTREAD) == FLAG_ALLOWDISTREAD); | |
| 636 | + | t->rotable = ((flags & FLAG_ROTABLE) == FLAG_ROTABLE); | |
| 637 | + | t->canReadText = ((flags & FLAG_READABLE) == FLAG_READABLE); | |
| 638 | + | t->client_chargeable = ((flags & FLAG_CLIENTCHARGES) == FLAG_CLIENTCHARGES); | |
| 639 | + | t->ignoreLook = ((flags & FLAG_IGNORE_LOOK) == FLAG_IGNORE_LOOK); | |
| 640 | + | } | |
| 641 | + | ||
| 642 | + | uint8_t attribute; | |
| 643 | + | while (itemNode->getU8(attribute)) { | |
| 644 | + | uint16_t datalen; | |
| 645 | + | if (!itemNode->getU16(datalen)) { | |
| 646 | + | warnings.push_back("Invalid item type property"); | |
| 647 | + | break; | |
| 648 | + | } | |
| 649 | + | ||
| 650 | + | switch (attribute) { | |
| 651 | + | case ITEM_ATTR_SERVERID: { | |
| 652 | + | if (datalen != sizeof(uint16_t)) { | |
| 653 | + | error = "items.otb: Unexpected data length of server id block (Should be 2 bytes)"; | |
| 654 | + | return false; | |
| 655 | + | } | |
| 656 | + | ||
| 657 | + | if (!itemNode->getU16(t->id)) { | |
| 658 | + | warnings.push_back("Invalid item type property (2)"); | |
| 659 | + | } | |
| 660 | + | ||
| 661 | + | if (max_item_id < t->id) { | |
| 662 | + | max_item_id = t->id; | |
| 663 | + | } | |
| 664 | + | break; | |
| 665 | + | } | |
| 666 | + | ||
| 667 | + | case ITEM_ATTR_CLIENTID: { | |
| 668 | + | if (datalen != sizeof(uint16_t)) { | |
| 669 | + | error = "items.otb: Unexpected data length of client id block (Should be 2 bytes)"; | |
| 670 | + | return false; | |
| 671 | + | } | |
| 672 | + | ||
| 673 | + | if (!itemNode->getU16(t->clientID)) { | |
| 674 | + | warnings.push_back("Invalid item type property (2)"); | |
| 675 | + | } | |
| 676 | + | ||
| 677 | + | t->sprite = static_cast<GameSprite*>(g_gui.gfx.getSprite(t->clientID)); | |
| 678 | + | break; | |
| 679 | + | } | |
| 680 | + | ||
| 681 | + | case ITEM_ATTR_SPEED: { | |
| 682 | + | if (datalen != sizeof(uint16_t)) { | |
| 683 | + | error = "items.otb: Unexpected data length of speed block (Should be 2 bytes)"; | |
| 684 | + | return false; | |
| 685 | + | } | |
| 686 | + | ||
| 687 | + | // t->speed = itemNode->getU16(); | |
| 688 | + | if (!itemNode->skip(2)) { // Just skip two bytes, we don't need speed | |
| 689 | + | warnings.push_back("Invalid item type property (3)"); | |
| 690 | + | } | |
| 691 | + | break; | |
| 692 | + | } | |
| 693 | + | ||
| 694 | + | case ITEM_ATTR_LIGHT2: { | |
| 695 | + | if (datalen != sizeof(lightBlock2)) { | |
| 696 | + | warnings.push_back("items.otb: Unexpected data length of item light (2) block (Should be " + i2ws(sizeof(lightBlock2)) + " bytes)"); | |
| 697 | + | break; | |
| 698 | + | } | |
| 699 | + | if (!itemNode->skip(4)) { // Just skip two bytes, we don't need light | |
| 700 | + | warnings.push_back("Invalid item type property (4)"); | |
| 701 | + | } | |
| 702 | + | ||
| 703 | + | // t->lightLevel = itemNode->getU16(); | |
| 704 | + | // t->lightColor = itemNode->getU16(); | |
| 705 | + | break; | |
| 706 | + | } | |
| 707 | + | ||
| 708 | + | case ITEM_ATTR_TOPORDER: { | |
| 709 | + | if (datalen != sizeof(uint8_t)) { | |
| 710 | + | warnings.push_back("items.otb: Unexpected data length of item toporder block (Should be 1 byte)"); | |
| 711 | + | break; | |
| 712 | + | } | |
| 713 | + | ||
| 714 | + | if (!itemNode->getU8(u8)) { | |
| 715 | + | warnings.push_back("Invalid item type property (5)"); | |
| 716 | + | } | |
| 717 | + | ||
| 718 | + | t->alwaysOnTopOrder = u8; | |
| 719 | + | break; | |
| 720 | + | } | |
| 721 | + | ||
| 722 | + | case ITEM_ATTR_CLASSIFICATION: { | |
| 723 | + | if (datalen != sizeof(uint8_t)) { | |
| 724 | + | warnings.push_back("items.otb: Unexpected data length of item classification block (Should be 1 byte)"); | |
| 725 | + | break; | |
| 726 | + | } | |
| 727 | + | ||
| 728 | + | if (!itemNode->getU8(u8)) { | |
| 729 | + | warnings.push_back("Invalid item type property (5)"); | |
| 730 | + | } | |
| 731 | + | ||
| 732 | + | t->classification = u8; | |
| 733 | + | break; | |
| 734 | + | } | |
| 735 | + | ||
| 736 | + | default: { | |
| 737 | + | // skip unknown attributes | |
| 738 | + | itemNode->skip(datalen); | |
| 739 | + | // warnings.push_back("items.otb: Skipped unknown attribute"); | |
| 740 | + | break; | |
| 741 | + | } | |
| 742 | + | } | |
| 743 | + | } | |
| 744 | + | ||
| 745 | + | if (t) { | |
| 746 | + | if (items[t->id]) { | |
| 747 | + | warnings.push_back("items.otb: Duplicate items"); | |
| 748 | + | delete items[t->id]; | |
| 749 | + | } | |
| 750 | + | items.set(t->id, t); | |
| 751 | + | } | |
| 752 | + | } | |
| 753 | + | return true; | |
| 754 | + | } | |
| 755 | + | ||
| 756 | + | bool ItemDatabase::loadFromOtb(const FileName& datafile, wxString& error, wxArrayString& warnings) { | |
| 757 | + | std::string filename = nstr((datafile.GetPath(wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR) + datafile.GetFullName())); | |
| 758 | + | DiskNodeFileReadHandle f(filename, StringVector(1, "OTBI")); | |
| 759 | + | ||
| 760 | + | if (!f.isOk()) { | |
| 761 | + | error = "Couldn't open file \"" + wxstr(filename) + "\":" + wxstr(f.getErrorMessage()); | |
| 762 | + | return false; | |
| 763 | + | } | |
| 764 | + | ||
| 765 | + | BinaryNode* root = f.getRootNode(); | |
| 766 | + | ||
| 767 | + | #define safe_get(node, func, ...) \ | |
| 768 | + | do { \ | |
| 769 | + | if (!node->get##func(__VA_ARGS__)) { \ | |
| 770 | + | error = wxstr(f.getErrorMessage()); \ | |
| 771 | + | return false; \ | |
| 772 | + | } \ | |
| 773 | + | } while (false) | |
| 774 | + | ||
| 775 | + | // Read root flags | |
| 776 | + | root->skip(1); // Type info | |
| 777 | + | // uint32_t flags = | |
| 778 | + | ||
| 779 | + | root->skip(4); // Unused? | |
| 780 | + | ||
| 781 | + | uint8_t attr; | |
| 782 | + | safe_get(root, U8, attr); | |
| 783 | + | if (attr == ROOT_ATTR_VERSION) { | |
| 784 | + | uint16_t datalen; | |
| 785 | + | if (!root->getU16(datalen) || datalen != 4 + 4 + 4 + 1 * 128) { | |
| 786 | + | error = "items.otb: Size of version header is invalid, updated .otb version?"; | |
| 787 | + | return false; | |
| 788 | + | } | |
| 789 | + | safe_get(root, U32, MajorVersion); // items otb format file version | |
| 790 | + | safe_get(root, U32, MinorVersion); // client version | |
| 791 | + | safe_get(root, U32, BuildNumber); // revision | |
| 792 | + | std::string csd; | |
| 793 | + | csd.resize(128); | |
| 794 | + | ||
| 795 | + | if (!root->getRAW((uint8_t*)csd.data(), 128)) { // CSDVersion ?? | |
| 796 | + | error = wxstr(f.getErrorMessage()); | |
| 797 | + | return false; | |
| 798 | + | } | |
| 799 | + | } else { | |
| 800 | + | error = "Expected ROOT_ATTR_VERSION as first node of items.otb!"; | |
| 801 | + | } | |
| 802 | + | ||
| 803 | + | if (g_settings.getInteger(Config::CHECK_SIGNATURES)) { | |
| 804 | + | if (g_gui.GetCurrentVersion().getOTBVersion().format_version != MajorVersion) { | |
| 805 | + | error = "Unsupported items.otb version (version " + i2ws(MajorVersion) + ")"; | |
| 806 | + | return false; | |
| 807 | + | } | |
| 808 | + | } | |
| 809 | + | ||
| 810 | + | BinaryNode* itemNode = root->getChild(); | |
| 811 | + | switch (MajorVersion) { | |
| 812 | + | case 1: | |
| 813 | + | return loadFromOtbVer1(itemNode, error, warnings); | |
| 814 | + | case 2: | |
| 815 | + | return loadFromOtbVer2(itemNode, error, warnings); | |
| 816 | + | case 3: | |
| 817 | + | return loadFromOtbVer3(itemNode, error, warnings); | |
| 818 | + | } | |
| 819 | + | return true; | |
| 820 | + | } | |
| 821 | + | ||
| 822 | + | bool ItemDatabase::loadItemFromGameXml(pugi::xml_node itemNode, int id) { | |
| 823 | + | ClientVersionID clientVersion = g_gui.GetCurrentVersionID(); | |
| 824 | + | if (clientVersion < CLIENT_VERSION_980 && id > 20000 && id < 20100) { | |
| 825 | + | itemNode = itemNode.next_sibling(); | |
| 826 | + | return true; | |
| 827 | + | } else if (id > 30000 && id < 30100) { | |
| 828 | + | itemNode = itemNode.next_sibling(); | |
| 829 | + | return true; | |
| 830 | + | } | |
| 831 | + | ||
| 832 | + | ItemType& it = getItemType(id); | |
| 833 | + | ||
| 834 | + | it.name = itemNode.attribute("name").as_string(); | |
| 835 | + | it.editorsuffix = itemNode.attribute("editorsuffix").as_string(); | |
| 836 | + | ||
| 837 | + | pugi::xml_attribute attribute; | |
| 838 | + | for (pugi::xml_node itemAttributesNode = itemNode.first_child(); itemAttributesNode; itemAttributesNode = itemAttributesNode.next_sibling()) { | |
| 839 | + | if (!(attribute = itemAttributesNode.attribute("key"))) { | |
| 840 | + | continue; | |
| 841 | + | } | |
| 842 | + | ||
| 843 | + | std::string key = attribute.as_string(); | |
| 844 | + | to_lower_str(key); | |
| 845 | + | if (key == "type") { | |
| 846 | + | if (!(attribute = itemAttributesNode.attribute("value"))) { | |
| 847 | + | continue; | |
| 848 | + | } | |
| 849 | + | ||
| 850 | + | std::string typeValue = attribute.as_string(); | |
| 851 | + | to_lower_str(key); | |
| 852 | + | if (typeValue == "depot") { | |
| 853 | + | it.type = ITEM_TYPE_DEPOT; | |
| 854 | + | } else if (typeValue == "mailbox") { | |
| 855 | + | it.type = ITEM_TYPE_MAILBOX; | |
| 856 | + | } else if (typeValue == "trashholder") { | |
| 857 | + | it.type = ITEM_TYPE_TRASHHOLDER; | |
| 858 | + | } else if (typeValue == "container") { | |
| 859 | + | it.type = ITEM_TYPE_CONTAINER; | |
| 860 | + | } else if (typeValue == "door") { | |
| 861 | + | it.type = ITEM_TYPE_DOOR; | |
| 862 | + | } else if (typeValue == "magicfield") { | |
| 863 | + | it.group = ITEM_GROUP_MAGICFIELD; | |
| 864 | + | it.type = ITEM_TYPE_MAGICFIELD; | |
| 865 | + | } else if (typeValue == "teleport") { | |
| 866 | + | it.type = ITEM_TYPE_TELEPORT; | |
| 867 | + | } else if (typeValue == "bed") { | |
| 868 | + | it.type = ITEM_TYPE_BED; | |
| 869 | + | } else if (typeValue == "key") { | |
| 870 | + | it.type = ITEM_TYPE_KEY; | |
| 871 | + | } else if (typeValue == "podium") { | |
| 872 | + | it.type = ITEM_TYPE_PODIUM; | |
| 873 | + | } | |
| 874 | + | } else if (key == "name") { | |
| 875 | + | if ((attribute = itemAttributesNode.attribute("value"))) { | |
| 876 | + | it.name = attribute.as_string(); | |
| 877 | + | } | |
| 878 | + | } else if (key == "description") { | |
| 879 | + | if ((attribute = itemAttributesNode.attribute("value"))) { | |
| 880 | + | it.description = attribute.as_string(); | |
| 881 | + | } | |
| 882 | + | } else if (key == "runespellName") { | |
| 883 | + | /*if((attribute = itemAttributesNode.attribute("value"))) { | |
| 884 | + | it.runeSpellName = attribute.as_string(); | |
| 885 | + | }*/ | |
| 886 | + | } else if (key == "weight") { | |
| 887 | + | if ((attribute = itemAttributesNode.attribute("value"))) { | |
| 888 | + | it.weight = attribute.as_int() / 100.f; | |
| 889 | + | } | |
| 890 | + | } else if (key == "armor") { | |
| 891 | + | if ((attribute = itemAttributesNode.attribute("value"))) { | |
| 892 | + | it.armor = attribute.as_int(); | |
| 893 | + | } | |
| 894 | + | } else if (key == "defense") { | |
| 895 | + | if ((attribute = itemAttributesNode.attribute("value"))) { | |
| 896 | + | it.defense = attribute.as_int(); | |
| 897 | + | } | |
| 898 | + | } else if (key == "slottype") { | |
| 899 | + | if ((attribute = itemAttributesNode.attribute("value"))) { | |
| 900 | + | std::string typeValue = attribute.as_string(); | |
| 901 | + | if (typeValue == "head") { | |
| 902 | + | it.slot_position |= SLOTP_HEAD; | |
| 903 | + | } else if (typeValue == "body") { | |
| 904 | + | it.slot_position |= SLOTP_ARMOR; | |
| 905 | + | } else if (typeValue == "legs") { | |
| 906 | + | it.slot_position |= SLOTP_LEGS; | |
| 907 | + | } else if (typeValue == "feet") { | |
| 908 | + | it.slot_position |= SLOTP_FEET; | |
| 909 | + | } else if (typeValue == "backpack") { | |
| 910 | + | it.slot_position |= SLOTP_BACKPACK; | |
| 911 | + | } else if (typeValue == "two-handed") { | |
| 912 | + | it.slot_position |= SLOTP_TWO_HAND; | |
| 913 | + | } else if (typeValue == "right-hand") { | |
| 914 | + | it.slot_position &= ~SLOTP_LEFT; | |
| 915 | + | } else if (typeValue == "left-hand") { | |
| 916 | + | it.slot_position &= ~SLOTP_RIGHT; | |
| 917 | + | } else if (typeValue == "necklace") { | |
| 918 | + | it.slot_position |= SLOTP_NECKLACE; | |
| 919 | + | } else if (typeValue == "ring") { | |
| 920 | + | it.slot_position |= SLOTP_RING; | |
| 921 | + | } else if (typeValue == "ammo") { | |
| 922 | + | it.slot_position |= SLOTP_AMMO; | |
| 923 | + | } else if (typeValue == "hand") { | |
| 924 | + | it.slot_position |= SLOTP_HAND; | |
| 925 | + | } | |
| 926 | + | } | |
| 927 | + | } else if (key == "weapontype") { | |
| 928 | + | if ((attribute = itemAttributesNode.attribute("value"))) { | |
| 929 | + | std::string typeValue = attribute.as_string(); | |
| 930 | + | if (typeValue == "sword") { | |
| 931 | + | it.weapon_type = WEAPON_SWORD; | |
| 932 | + | } else if (typeValue == "club") { | |
| 933 | + | it.weapon_type = WEAPON_CLUB; | |
| 934 | + | } else if (typeValue == "axe") { | |
| 935 | + | it.weapon_type = WEAPON_AXE; | |
| 936 | + | } else if (typeValue == "shield") { | |
| 937 | + | it.weapon_type = WEAPON_SHIELD; | |
| 938 | + | } else if (typeValue == "distance") { | |
| 939 | + | it.weapon_type = WEAPON_DISTANCE; | |
| 940 | + | } else if (typeValue == "wand") { | |
| 941 | + | it.weapon_type = WEAPON_WAND; | |
| 942 | + | } else if (typeValue == "ammunition") { | |
| 943 | + | it.weapon_type = WEAPON_AMMO; | |
| 944 | + | } | |
| 945 | + | } | |
| 946 | + | } else if (key == "rotateto") { | |
| 947 | + | if ((attribute = itemAttributesNode.attribute("value"))) { | |
| 948 | + | it.rotateTo = attribute.as_ushort(); | |
| 949 | + | } | |
| 950 | + | } else if (key == "containersize") { | |
| 951 | + | if ((attribute = itemAttributesNode.attribute("value"))) { | |
| 952 | + | it.volume = attribute.as_ushort(); | |
| 953 | + | } | |
| 954 | + | } else if (key == "readable") { | |
| 955 | + | if ((attribute = itemAttributesNode.attribute("value"))) { | |
| 956 | + | it.canReadText = attribute.as_bool(); | |
| 957 | + | } | |
| 958 | + | } else if (key == "writeable") { | |
| 959 | + | if ((attribute = itemAttributesNode.attribute("value"))) { | |
| 960 | + | it.canWriteText = it.canReadText = attribute.as_bool(); | |
| 961 | + | } | |
| 962 | + | } else if (key == "decayto") { | |
| 963 | + | it.decays = true; | |
| 964 | + | } else if (key == "maxtextlen" || key == "maxtextlength") { | |
| 965 | + | if ((attribute = itemAttributesNode.attribute("value"))) { | |
| 966 | + | it.maxTextLen = attribute.as_ushort(); | |
| 967 | + | it.canReadText = it.maxTextLen > 0; | |
| 968 | + | } | |
| 969 | + | } else if (key == "writeonceitemid") { | |
| 970 | + | /*if((attribute = itemAttributesNode.attribute("value"))) { | |
| 971 | + | it.writeOnceItemId = pugi::cast<int32_t>(attribute.value()); | |
| 972 | + | }*/ | |
| 973 | + | } else if (key == "allowdistread") { | |
| 974 | + | if ((attribute = itemAttributesNode.attribute("value"))) { | |
| 975 | + | it.allowDistRead = attribute.as_bool(); | |
| 976 | + | } | |
| 977 | + | } else if (key == "charges") { | |
| 978 | + | if ((attribute = itemAttributesNode.attribute("value"))) { | |
| 979 | + | it.charges = attribute.as_uint(); | |
| 980 | + | it.extra_chargeable = true; | |
| 981 | + | } | |
| 982 | + | } else if (key == "floorchange") { | |
| 983 | + | if ((attribute = itemAttributesNode.attribute("value"))) { | |
| 984 | + | std::string value = attribute.as_string(); | |
| 985 | + | if (value == "down") { | |
| 986 | + | it.floorChangeDown = true; | |
| 987 | + | it.floorChange = true; | |
| 988 | + | } else if (value == "north") { | |
| 989 | + | it.floorChangeNorth = true; | |
| 990 | + | it.floorChange = true; | |
| 991 | + | } else if (value == "south") { | |
| 992 | + | it.floorChangeSouth = true; | |
| 993 | + | it.floorChange = true; | |
| 994 | + | } else if (value == "west") { | |
| 995 | + | it.floorChangeWest = true; | |
| 996 | + | it.floorChange = true; | |
| 997 | + | } else if (value == "east") { | |
| 998 | + | it.floorChangeEast = true; | |
| 999 | + | it.floorChange = true; | |
| 1000 | + | } else if (value == "northex") { | |
| 1001 | + | it.floorChange = true; | |
| 1002 | + | } else if (value == "southex") { | |
| 1003 | + | it.floorChange = true; | |
| 1004 | + | } else if (value == "westex") { | |
| 1005 | + | it.floorChange = true; | |
| 1006 | + | } else if (value == "eastex") { | |
| 1007 | + | it.floorChange = true; | |
| 1008 | + | } else if (value == "southalt") { | |
| 1009 | + | it.floorChange = true; | |
| 1010 | + | } else if (value == "eastalt") { | |
| 1011 | + | it.floorChange = true; | |
| 1012 | + | } | |
| 1013 | + | } | |
| 1014 | + | } | |
| 1015 | + | } | |
| 1016 | + | return true; | |
| 1017 | + | } | |
| 1018 | + | ||
| 1019 | + | bool ItemDatabase::loadFromGameXml(const FileName& identifier, wxString& error, wxArrayString& warnings) { | |
| 1020 | + | pugi::xml_document doc; | |
| 1021 | + | pugi::xml_parse_result result = doc.load_file(identifier.GetFullPath().mb_str()); | |
| 1022 | + | if (!result) { | |
| 1023 | + | error = "Could not load items.xml (Syntax error?)"; | |
| 1024 | + | return false; | |
| 1025 | + | } | |
| 1026 | + | ||
| 1027 | + | pugi::xml_node node = doc.child("items"); | |
| 1028 | + | if (!node) { | |
| 1029 | + | error = "items.xml, invalid root node."; | |
| 1030 | + | return false; | |
| 1031 | + | } | |
| 1032 | + | ||
| 1033 | + | for (pugi::xml_node itemNode = node.first_child(); itemNode; itemNode = itemNode.next_sibling()) { | |
| 1034 | + | if (as_lower_str(itemNode.name()) != "item") { | |
| 1035 | + | continue; | |
| 1036 | + | } | |
| 1037 | + | ||
| 1038 | + | uint16_t fromId = 0; | |
| 1039 | + | uint16_t toId = 0; | |
| 1040 | + | if (const pugi::xml_attribute attribute = itemNode.attribute("id")) { | |
| 1041 | + | fromId = toId = attribute.as_ushort(); | |
| 1042 | + | } else { | |
| 1043 | + | fromId = itemNode.attribute("fromid").as_ushort(); | |
| 1044 | + | toId = itemNode.attribute("toid").as_ushort(); | |
| 1045 | + | } | |
| 1046 | + | ||
| 1047 | + | if (fromId == 0 || toId == 0) { | |
| 1048 | + | error = "Could not read item id from item node."; | |
| 1049 | + | return false; | |
| 1050 | + | } | |
| 1051 | + | ||
| 1052 | + | for (uint16_t id = fromId; id <= toId; ++id) { | |
| 1053 | + | if (!loadItemFromGameXml(itemNode, id)) { | |
| 1054 | + | return false; | |
| 1055 | + | } | |
| 1056 | + | } | |
| 1057 | + | } | |
| 1058 | + | return true; | |
| 1059 | + | } | |
| 1060 | + | ||
| 1061 | + | bool ItemDatabase::loadMetaItem(pugi::xml_node node) { | |
| 1062 | + | if (const pugi::xml_attribute attribute = node.attribute("id")) { | |
| 1063 | + | const uint16_t id = attribute.as_ushort(); | |
| 1064 | + | if (id == 0 || items[id]) { | |
| 1065 | + | return false; | |
| 1066 | + | } | |
| 1067 | + | items.set(id, newd ItemType()); | |
| 1068 | + | items[id]->is_metaitem = true; | |
| 1069 | + | items[id]->id = id; | |
| 1070 | + | return true; | |
| 1071 | + | } | |
| 1072 | + | return false; | |
| 1073 | + | } | |
| 1074 | + | ||
| 1075 | + | ItemType& ItemDatabase::getItemType(int id) { | |
| 1076 | + | ItemType* it = items[id]; | |
| 1077 | + | if (it) { | |
| 1078 | + | return *it; | |
| 1079 | + | } else { | |
| 1080 | + | static ItemType dummyItemType; // use this for invalid ids | |
| 1081 | + | return dummyItemType; | |
| 1082 | + | } | |
| 1083 | + | } | |
| 1084 | + | ||
| 1085 | + | bool ItemDatabase::typeExists(int id) const { | |
| 1086 | + | ItemType* it = items[id]; | |
| 1087 | + | return it != nullptr; | |
| 1088 | + | } |
| @@ -0,0 +1,475 @@ | |||
| 1 | + | ////////////////////////////////////////////////////////////////////// | |
| 2 | + | // This file is part of Remere's Map Editor | |
| 3 | + | ////////////////////////////////////////////////////////////////////// | |
| 4 | + | // Remere's Map Editor is free software: you can redistribute it and/or modify | |
| 5 | + | // it under the terms of the GNU General Public License as published by | |
| 6 | + | // the Free Software Foundation, either version 3 of the License, or | |
| 7 | + | // (at your option) any later version. | |
| 8 | + | // | |
| 9 | + | // Remere's Map Editor is distributed in the hope that it will be useful, | |
| 10 | + | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11 | + | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 12 | + | // GNU General Public License for more details. | |
| 13 | + | // | |
| 14 | + | // You should have received a copy of the GNU General Public License | |
| 15 | + | // along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 16 | + | ////////////////////////////////////////////////////////////////////// | |
| 17 | + | ||
| 18 | + | #ifndef RME_ITEMS_H_ | |
| 19 | + | #define RME_ITEMS_H_ | |
| 20 | + | ||
| 21 | + | #include "filehandle.h" | |
| 22 | + | #include "brush_enums.h" | |
| 23 | + | ||
| 24 | + | class Brush; | |
| 25 | + | class GroundBrush; | |
| 26 | + | class WallBrush; | |
| 27 | + | class CarpetBrush; | |
| 28 | + | class TableBrush; | |
| 29 | + | class HouseBrush; | |
| 30 | + | class HouseExitBrush; | |
| 31 | + | class OptionalBorderBrush; | |
| 32 | + | class EraserBrush; | |
| 33 | + | class SpawnBrush; | |
| 34 | + | class DoorBrush; | |
| 35 | + | class FlagBrush; | |
| 36 | + | class RAWBrush; | |
| 37 | + | ||
| 38 | + | class ItemType; | |
| 39 | + | class GameSprite; | |
| 40 | + | class GameSprite; | |
| 41 | + | class ItemDatabase; | |
| 42 | + | ||
| 43 | + | extern ItemDatabase g_items; | |
| 44 | + | ||
| 45 | + | typedef uint8_t attribute_t; | |
| 46 | + | typedef uint32_t flags_t; | |
| 47 | + | typedef uint16_t datasize_t; | |
| 48 | + | ||
| 49 | + | enum ItemGroup_t { | |
| 50 | + | ITEM_GROUP_NONE = 0, | |
| 51 | + | ITEM_GROUP_GROUND, | |
| 52 | + | ITEM_GROUP_CONTAINER, | |
| 53 | + | ITEM_GROUP_WEAPON, | |
| 54 | + | ITEM_GROUP_AMMUNITION, | |
| 55 | + | ITEM_GROUP_ARMOR, | |
| 56 | + | ITEM_GROUP_RUNE, | |
| 57 | + | ITEM_GROUP_TELEPORT, | |
| 58 | + | ITEM_GROUP_MAGICFIELD, | |
| 59 | + | ITEM_GROUP_WRITEABLE, | |
| 60 | + | ITEM_GROUP_KEY, | |
| 61 | + | ITEM_GROUP_SPLASH, | |
| 62 | + | ITEM_GROUP_FLUID, | |
| 63 | + | ITEM_GROUP_DOOR, | |
| 64 | + | ITEM_GROUP_DEPRECATED, | |
| 65 | + | ITEM_GROUP_PODIUM, | |
| 66 | + | ITEM_GROUP_LAST | |
| 67 | + | }; | |
| 68 | + | ||
| 69 | + | enum ItemTypes_t { | |
| 70 | + | ITEM_TYPE_NONE = 0, | |
| 71 | + | ITEM_TYPE_DEPOT, | |
| 72 | + | ITEM_TYPE_MAILBOX, | |
| 73 | + | ITEM_TYPE_TRASHHOLDER, | |
| 74 | + | ITEM_TYPE_CONTAINER, | |
| 75 | + | ITEM_TYPE_DOOR, | |
| 76 | + | ITEM_TYPE_MAGICFIELD, | |
| 77 | + | ITEM_TYPE_TELEPORT, | |
| 78 | + | ITEM_TYPE_BED, | |
| 79 | + | ITEM_TYPE_KEY, | |
| 80 | + | ITEM_TYPE_PODIUM, | |
| 81 | + | ITEM_TYPE_LAST | |
| 82 | + | }; | |
| 83 | + | ||
| 84 | + | enum SlotPositionBits : uint32_t { | |
| 85 | + | SLOTP_WHEREEVER = 0xFFFFFFFF, | |
| 86 | + | SLOTP_HEAD = 1 << 0, | |
| 87 | + | SLOTP_NECKLACE = 1 << 1, | |
| 88 | + | SLOTP_BACKPACK = 1 << 2, | |
| 89 | + | SLOTP_ARMOR = 1 << 3, | |
| 90 | + | SLOTP_RIGHT = 1 << 4, | |
| 91 | + | SLOTP_LEFT = 1 << 5, | |
| 92 | + | SLOTP_LEGS = 1 << 6, | |
| 93 | + | SLOTP_FEET = 1 << 7, | |
| 94 | + | SLOTP_RING = 1 << 8, | |
| 95 | + | SLOTP_AMMO = 1 << 9, | |
| 96 | + | SLOTP_DEPOT = 1 << 10, | |
| 97 | + | SLOTP_TWO_HAND = 1 << 11, | |
| 98 | + | SLOTP_HAND = (SLOTP_LEFT | SLOTP_RIGHT) | |
| 99 | + | }; | |
| 100 | + | ||
| 101 | + | enum WeaponType_t : uint8_t { | |
| 102 | + | WEAPON_NONE, | |
| 103 | + | WEAPON_SWORD, | |
| 104 | + | WEAPON_CLUB, | |
| 105 | + | WEAPON_AXE, | |
| 106 | + | WEAPON_SHIELD, | |
| 107 | + | WEAPON_DISTANCE, | |
| 108 | + | WEAPON_WAND, | |
| 109 | + | WEAPON_AMMO, | |
| 110 | + | }; | |
| 111 | + | ||
| 112 | + | /////////OTB specific////////////// | |
| 113 | + | ||
| 114 | + | enum rootattrib_t { | |
| 115 | + | ROOT_ATTR_VERSION = 0x01 | |
| 116 | + | }; | |
| 117 | + | ||
| 118 | + | enum itemattrib_t { | |
| 119 | + | ITEM_ATTR_FIRST = 0x10, | |
| 120 | + | ITEM_ATTR_SERVERID = ITEM_ATTR_FIRST, | |
| 121 | + | ITEM_ATTR_CLIENTID, | |
| 122 | + | ITEM_ATTR_NAME, | |
| 123 | + | ITEM_ATTR_DESCR, | |
| 124 | + | ITEM_ATTR_SPEED, | |
| 125 | + | ITEM_ATTR_SLOT, | |
| 126 | + | ITEM_ATTR_MAXITEMS, | |
| 127 | + | ITEM_ATTR_WEIGHT, | |
| 128 | + | ITEM_ATTR_WEAPON, | |
| 129 | + | ITEM_ATTR_AMU, | |
| 130 | + | ITEM_ATTR_ARMOR, | |
| 131 | + | ITEM_ATTR_MAGLEVEL, | |
| 132 | + | ITEM_ATTR_MAGFIELDTYPE, | |
| 133 | + | ITEM_ATTR_WRITEABLE, | |
| 134 | + | ITEM_ATTR_ROTATETO, | |
| 135 | + | ITEM_ATTR_DECAY, | |
| 136 | + | ITEM_ATTR_SPRITEHASH, | |
| 137 | + | ITEM_ATTR_MINIMAPCOLOR, | |
| 138 | + | ITEM_ATTR_07, | |
| 139 | + | ITEM_ATTR_08, | |
| 140 | + | ITEM_ATTR_LIGHT, | |
| 141 | + | ||
| 142 | + | // 1-byte aligned | |
| 143 | + | ITEM_ATTR_DECAY2, | |
| 144 | + | ITEM_ATTR_WEAPON2, | |
| 145 | + | ITEM_ATTR_AMU2, | |
| 146 | + | ITEM_ATTR_ARMOR2, | |
| 147 | + | ITEM_ATTR_WRITEABLE2, | |
| 148 | + | ITEM_ATTR_LIGHT2, | |
| 149 | + | ITEM_ATTR_TOPORDER, | |
| 150 | + | ITEM_ATTR_WRITEABLE3, | |
| 151 | + | ||
| 152 | + | ITEM_ATTR_WAREID, | |
| 153 | + | ITEM_ATTR_CLASSIFICATION, | |
| 154 | + | ||
| 155 | + | ITEM_ATTR_LAST | |
| 156 | + | }; | |
| 157 | + | ||
| 158 | + | enum itemflags_t { | |
| 159 | + | FLAG_UNPASSABLE = 1 << 0, | |
| 160 | + | FLAG_BLOCK_MISSILES = 1 << 1, | |
| 161 | + | FLAG_BLOCK_PATHFINDER = 1 << 2, | |
| 162 | + | FLAG_HAS_ELEVATION = 1 << 3, | |
| 163 | + | FLAG_USEABLE = 1 << 4, | |
| 164 | + | FLAG_PICKUPABLE = 1 << 5, | |
| 165 | + | FLAG_MOVEABLE = 1 << 6, | |
| 166 | + | FLAG_STACKABLE = 1 << 7, | |
| 167 | + | FLAG_FLOORCHANGEDOWN = 1 << 8, | |
| 168 | + | FLAG_FLOORCHANGENORTH = 1 << 9, | |
| 169 | + | FLAG_FLOORCHANGEEAST = 1 << 10, | |
| 170 | + | FLAG_FLOORCHANGESOUTH = 1 << 11, | |
| 171 | + | FLAG_FLOORCHANGEWEST = 1 << 12, | |
| 172 | + | FLAG_ALWAYSONTOP = 1 << 13, | |
| 173 | + | FLAG_READABLE = 1 << 14, | |
| 174 | + | FLAG_ROTABLE = 1 << 15, | |
| 175 | + | FLAG_HANGABLE = 1 << 16, | |
| 176 | + | FLAG_HOOK_EAST = 1 << 17, | |
| 177 | + | FLAG_HOOK_SOUTH = 1 << 18, | |
| 178 | + | FLAG_CANNOTDECAY = 1 << 19, | |
| 179 | + | FLAG_ALLOWDISTREAD = 1 << 20, | |
| 180 | + | FLAG_UNUSED = 1 << 21, | |
| 181 | + | FLAG_CLIENTCHARGES = 1 << 22, | |
| 182 | + | FLAG_IGNORE_LOOK = 1 << 23 | |
| 183 | + | }; | |
| 184 | + | ||
| 185 | + | enum slotsOTB_t { | |
| 186 | + | OTB_SLOT_DEFAULT, | |
| 187 | + | OTB_SLOT_HEAD, | |
| 188 | + | OTB_SLOT_BODY, | |
| 189 | + | OTB_SLOT_LEGS, | |
| 190 | + | OTB_SLOT_BACKPACK, | |
| 191 | + | OTB_SLOT_WEAPON, | |
| 192 | + | OTB_SLOT_2HAND, | |
| 193 | + | OTB_SLOT_FEET, | |
| 194 | + | OTB_SLOT_AMULET, | |
| 195 | + | OTB_SLOT_RING, | |
| 196 | + | OTB_SLOT_HAND, | |
| 197 | + | }; | |
| 198 | + | ||
| 199 | + | enum ShootTypeOtb_t { | |
| 200 | + | OTB_SHOOT_NONE = 0, | |
| 201 | + | OTB_SHOOT_BOLT = 1, | |
| 202 | + | OTB_SHOOT_ARROW = 2, | |
| 203 | + | OTB_SHOOT_FIRE = 3, | |
| 204 | + | OTB_SHOOT_ENERGY = 4, | |
| 205 | + | OTB_SHOOT_POISONARROW = 5, | |
| 206 | + | OTB_SHOOT_BURSTARROW = 6, | |
| 207 | + | OTB_SHOOT_THROWINGSTAR = 7, | |
| 208 | + | OTB_SHOOT_THROWINGKNIFE = 8, | |
| 209 | + | OTB_SHOOT_SMALLSTONE = 9, | |
| 210 | + | OTB_SHOOT_SUDDENDEATH = 10, | |
| 211 | + | OTB_SHOOT_LARGEROCK = 11, | |
| 212 | + | OTB_SHOOT_SNOWBALL = 12, | |
| 213 | + | OTB_SHOOT_POWERBOLT = 13, | |
| 214 | + | OTB_SHOOT_SPEAR = 14, | |
| 215 | + | OTB_SHOOT_POISONFIELD = 15, | |
| 216 | + | OTB_SHOOT_INFERNALBOLT = 16 | |
| 217 | + | }; | |
| 218 | + | ||
| 219 | + | // 1-byte aligned structs | |
| 220 | + | #pragma pack(1) | |
| 221 | + | ||
| 222 | + | struct VERSIONINFO { | |
| 223 | + | uint32_t dwMajorVersion; | |
| 224 | + | uint32_t dwMinorVersion; | |
| 225 | + | uint32_t dwBuildNumber; | |
| 226 | + | uint8_t CSDVersion[128]; | |
| 227 | + | }; | |
| 228 | + | ||
| 229 | + | struct decayBlock2 { | |
| 230 | + | uint16_t decayTo; | |
| 231 | + | uint16_t decayTime; | |
| 232 | + | }; | |
| 233 | + | ||
| 234 | + | struct weaponBlock2 { | |
| 235 | + | uint8_t weaponType; | |
| 236 | + | uint8_t amuType; | |
| 237 | + | uint8_t shootType; | |
| 238 | + | uint8_t attack; | |
| 239 | + | uint8_t defence; | |
| 240 | + | }; | |
| 241 | + | ||
| 242 | + | struct amuBlock2 { | |
| 243 | + | uint8_t amuType; | |
| 244 | + | uint8_t shootType; | |
| 245 | + | uint8_t attack; | |
| 246 | + | }; | |
| 247 | + | ||
| 248 | + | struct armorBlock2 { | |
| 249 | + | uint16_t armor; | |
| 250 | + | double weight; | |
| 251 | + | uint16_t slot_position; | |
| 252 | + | }; | |
| 253 | + | ||
| 254 | + | struct writeableBlock2 { | |
| 255 | + | uint16_t readOnlyId; | |
| 256 | + | }; | |
| 257 | + | ||
| 258 | + | struct lightBlock2 { | |
| 259 | + | uint16_t lightLevel; | |
| 260 | + | uint16_t lightColor; | |
| 261 | + | }; | |
| 262 | + | ||
| 263 | + | struct writeableBlock3 { | |
| 264 | + | uint16_t readOnlyId; | |
| 265 | + | uint16_t maxTextLen; | |
| 266 | + | }; | |
| 267 | + | ||
| 268 | + | #pragma pack() | |
| 269 | + | ||
| 270 | + | class ItemType { | |
| 271 | + | private: | |
| 272 | + | ItemType(const ItemType&) { } | |
| 273 | + | ||
| 274 | + | public: | |
| 275 | + | ItemType(); | |
| 276 | + | ~ItemType(); | |
| 277 | + | ||
| 278 | + | bool isGroundTile() const { | |
| 279 | + | return (group == ITEM_GROUP_GROUND); | |
| 280 | + | } | |
| 281 | + | bool isSplash() const { | |
| 282 | + | return (group == ITEM_GROUP_SPLASH); | |
| 283 | + | } | |
| 284 | + | bool isFluidContainer() const { | |
| 285 | + | return (group == ITEM_GROUP_FLUID); | |
| 286 | + | } | |
| 287 | + | ||
| 288 | + | bool isClientCharged() const { | |
| 289 | + | return client_chargeable; | |
| 290 | + | } | |
| 291 | + | bool isExtraCharged() const { | |
| 292 | + | return !client_chargeable && extra_chargeable; | |
| 293 | + | } | |
| 294 | + | ||
| 295 | + | bool isDepot() const { | |
| 296 | + | return (type == ITEM_TYPE_DEPOT); | |
| 297 | + | } | |
| 298 | + | bool isMailbox() const { | |
| 299 | + | return (type == ITEM_TYPE_MAILBOX); | |
| 300 | + | } | |
| 301 | + | bool isTrashHolder() const { | |
| 302 | + | return (type == ITEM_TYPE_TRASHHOLDER); | |
| 303 | + | } | |
| 304 | + | bool isContainer() const { | |
| 305 | + | return (type == ITEM_TYPE_CONTAINER); | |
| 306 | + | } | |
| 307 | + | bool isDoor() const { | |
| 308 | + | return (type == ITEM_TYPE_DOOR); | |
| 309 | + | } | |
| 310 | + | bool isMagicField() const { | |
| 311 | + | return (type == ITEM_TYPE_MAGICFIELD); | |
| 312 | + | } | |
| 313 | + | bool isTeleport() const { | |
| 314 | + | return (type == ITEM_TYPE_TELEPORT); | |
| 315 | + | } | |
| 316 | + | bool isBed() const { | |
| 317 | + | return (type == ITEM_TYPE_BED); | |
| 318 | + | } | |
| 319 | + | bool isKey() const { | |
| 320 | + | return (type == ITEM_TYPE_KEY); | |
| 321 | + | } | |
| 322 | + | bool isPodium() const { | |
| 323 | + | return (type == ITEM_TYPE_PODIUM); | |
| 324 | + | } | |
| 325 | + | ||
| 326 | + | bool isStackable() const { | |
| 327 | + | return stackable; | |
| 328 | + | } | |
| 329 | + | bool isMetaItem() const { | |
| 330 | + | return is_metaitem; | |
| 331 | + | } | |
| 332 | + | ||
| 333 | + | bool isFloorChange() const; | |
| 334 | + | ||
| 335 | + | GameSprite* sprite; | |
| 336 | + | uint16_t id; | |
| 337 | + | uint16_t clientID; | |
| 338 | + | Brush* brush; | |
| 339 | + | Brush* doodad_brush; | |
| 340 | + | Brush* collection_brush; | |
| 341 | + | RAWBrush* raw_brush; | |
| 342 | + | bool is_metaitem; | |
| 343 | + | // This is needed as a consequence of the item palette & the raw palette | |
| 344 | + | // using the same brushes ("others" category consists of items with this | |
| 345 | + | // flag set to false) | |
| 346 | + | bool has_raw; | |
| 347 | + | bool in_other_tileset; | |
| 348 | + | ||
| 349 | + | ItemGroup_t group; | |
| 350 | + | ItemTypes_t type; | |
| 351 | + | ||
| 352 | + | uint16_t volume; | |
| 353 | + | uint16_t maxTextLen; | |
| 354 | + | // uint16_t writeOnceItemId; | |
| 355 | + | uint16_t slot_position; | |
| 356 | + | uint8_t weapon_type; | |
| 357 | + | uint8_t classification = 0; // 12.81 | |
| 358 | + | uint16_t ground_equivalent; | |
| 359 | + | uint32_t border_group; | |
| 360 | + | bool has_equivalent; // True if any item has this as ground_equivalent | |
| 361 | + | bool wall_hate_me; // (For wallbrushes, regard this as not part of the wall) | |
| 362 | + | ||
| 363 | + | std::string name; | |
| 364 | + | std::string editorsuffix; | |
| 365 | + | std::string description; | |
| 366 | + | ||
| 367 | + | float weight; | |
| 368 | + | // It might be useful to be able to extrapolate this information in the future | |
| 369 | + | int attack; | |
| 370 | + | int defense; | |
| 371 | + | int armor; | |
| 372 | + | uint32_t charges; | |
| 373 | + | bool client_chargeable; | |
| 374 | + | bool extra_chargeable; | |
| 375 | + | bool ignoreLook; | |
| 376 | + | ||
| 377 | + | bool isHangable; | |
| 378 | + | bool hookEast; | |
| 379 | + | bool hookSouth; | |
| 380 | + | bool canReadText; | |
| 381 | + | bool canWriteText; | |
| 382 | + | bool allowDistRead; | |
| 383 | + | bool replaceable; | |
| 384 | + | bool decays; | |
| 385 | + | ||
| 386 | + | bool stackable; | |
| 387 | + | bool moveable; | |
| 388 | + | bool alwaysOnBottom; | |
| 389 | + | bool pickupable; | |
| 390 | + | bool rotable; | |
| 391 | + | bool isBorder; | |
| 392 | + | bool isOptionalBorder; | |
| 393 | + | bool isWall; | |
| 394 | + | bool isBrushDoor; | |
| 395 | + | bool isOpen; // door or window physically open | |
| 396 | + | bool isLocked; // door key locked | |
| 397 | + | bool isTable; | |
| 398 | + | bool isCarpet; | |
| 399 | + | ||
| 400 | + | bool floorChangeDown; | |
| 401 | + | bool floorChangeNorth; | |
| 402 | + | bool floorChangeSouth; | |
| 403 | + | bool floorChangeEast; | |
| 404 | + | bool floorChangeWest; | |
| 405 | + | bool floorChange; | |
| 406 | + | ||
| 407 | + | bool unpassable; | |
| 408 | + | bool blockPickupable; | |
| 409 | + | bool blockMissiles; | |
| 410 | + | bool blockPathfinder; | |
| 411 | + | bool hasElevation; | |
| 412 | + | ||
| 413 | + | int alwaysOnTopOrder; | |
| 414 | + | uint16_t rotateTo; | |
| 415 | + | BorderType border_alignment; | |
| 416 | + | ||
| 417 | + | bool hasLight; | |
| 418 | + | ||
| 419 | + | ||
| 420 | + | }; | |
| 421 | + | ||
| 422 | + | class ItemDatabase { | |
| 423 | + | public: | |
| 424 | + | ItemDatabase(); | |
| 425 | + | ~ItemDatabase(); | |
| 426 | + | ||
| 427 | + | void clear(); | |
| 428 | + | ||
| 429 | + | ItemType& operator[](size_t id) { | |
| 430 | + | return getItemType(id); | |
| 431 | + | } | |
| 432 | + | uint16_t getMaxID() const { | |
| 433 | + | return max_item_id; | |
| 434 | + | } | |
| 435 | + | ||
| 436 | + | bool typeExists(int id) const; | |
| 437 | + | ItemType& getItemType(int id); | |
| 438 | + | ItemType& getItemIdByClientID(int spriteId); | |
| 439 | + | ||
| 440 | + | bool loadFromOtb(const FileName& datafile, wxString& error, wxArrayString& warnings); | |
| 441 | + | bool loadFromGameXml(const FileName& datafile, wxString& error, wxArrayString& warnings); | |
| 442 | + | bool loadItemFromGameXml(pugi::xml_node itemNode, int id); | |
| 443 | + | bool loadMetaItem(pugi::xml_node node); | |
| 444 | + | ||
| 445 | + | // typedef std::map<int32_t, ItemType*> ItemMap; | |
| 446 | + | typedef contigous_vector<ItemType*> ItemMap; | |
| 447 | + | typedef std::map<std::string, ItemType*> ItemNameMap; | |
| 448 | + | ItemMap items; | |
| 449 | + | ||
| 450 | + | // Version information | |
| 451 | + | uint32_t MajorVersion; | |
| 452 | + | uint32_t MinorVersion; | |
| 453 | + | uint32_t BuildNumber; | |
| 454 | + | ||
| 455 | + | protected: | |
| 456 | + | bool loadFromOtbVer1(BinaryNode* itemNode, wxString& error, wxArrayString& warnings); | |
| 457 | + | bool loadFromOtbVer2(BinaryNode* itemNode, wxString& error, wxArrayString& warnings); | |
| 458 | + | bool loadFromOtbVer3(BinaryNode* itemNode, wxString& error, wxArrayString& warnings); | |
| 459 | + | ||
| 460 | + | protected: | |
| 461 | + | // Count of GameSprite types | |
| 462 | + | uint16_t item_count; | |
| 463 | + | uint16_t effect_count; | |
| 464 | + | uint16_t monster_count; | |
| 465 | + | uint16_t distance_count; | |
| 466 | + | ||
| 467 | + | uint16_t minclientID; | |
| 468 | + | uint16_t maxclientID; | |
| 469 | + | uint16_t max_item_id; | |
| 470 | + | ||
| 471 | + | friend class GameSprite; | |
| 472 | + | friend class Item; | |
| 473 | + | }; | |
| 474 | + | ||
| 475 | + | #endif |
| @@ -0,0 +1,391 @@ | |||
| 1 | + | ////////////////////////////////////////////////////////////////////// | |
| 2 | + | // This file is part of Remere's Map Editor | |
| 3 | + | ////////////////////////////////////////////////////////////////////// | |
| 4 | + | // Remere's Map Editor is free software: you can redistribute it and/or modify | |
| 5 | + | // it under the terms of the GNU General Public License as published by | |
| 6 | + | // the Free Software Foundation, either version 3 of the License, or | |
| 7 | + | // (at your option) any later version. | |
| 8 | + | // | |
| 9 | + | // Remere's Map Editor is distributed in the hope that it will be useful, | |
| 10 | + | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11 | + | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 12 | + | // GNU General Public License for more details. | |
| 13 | + | // | |
| 14 | + | // You should have received a copy of the GNU General Public License | |
| 15 | + | // along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 16 | + | ////////////////////////////////////////////////////////////////////// | |
| 17 | + | ||
| 18 | + | #include "main.h" | |
| 19 | + | ||
| 20 | + | #include "item_attributes.h" | |
| 21 | + | #include "filehandle.h" | |
| 22 | + | ||
| 23 | + | ItemAttributes::ItemAttributes() : | |
| 24 | + | attributes(nullptr) { | |
| 25 | + | //// | |
| 26 | + | } | |
| 27 | + | ||
| 28 | + | ItemAttributes::ItemAttributes(const ItemAttributes& o) { | |
| 29 | + | if (o.attributes) { | |
| 30 | + | attributes = newd ItemAttributeMap(*o.attributes); | |
| 31 | + | } | |
| 32 | + | } | |
| 33 | + | ||
| 34 | + | ItemAttributes::~ItemAttributes() { | |
| 35 | + | clearAllAttributes(); | |
| 36 | + | } | |
| 37 | + | ||
| 38 | + | void ItemAttributes::createAttributes() { | |
| 39 | + | if (!attributes) { | |
| 40 | + | attributes = newd ItemAttributeMap; | |
| 41 | + | } | |
| 42 | + | } | |
| 43 | + | ||
| 44 | + | void ItemAttributes::clearAllAttributes() { | |
| 45 | + | if (attributes) { | |
| 46 | + | delete attributes; | |
| 47 | + | } | |
| 48 | + | attributes = nullptr; | |
| 49 | + | } | |
| 50 | + | ||
| 51 | + | ItemAttributeMap ItemAttributes::getAttributes() const { | |
| 52 | + | if (attributes) { | |
| 53 | + | return *attributes; | |
| 54 | + | } | |
| 55 | + | return ItemAttributeMap(); | |
| 56 | + | } | |
| 57 | + | ||
| 58 | + | void ItemAttributes::setAttribute(const std::string& key, const ItemAttribute& value) { | |
| 59 | + | createAttributes(); | |
| 60 | + | (*attributes)[key] = value; | |
| 61 | + | } | |
| 62 | + | ||
| 63 | + | void ItemAttributes::setAttribute(const std::string& key, const std::string& value) { | |
| 64 | + | createAttributes(); | |
| 65 | + | (*attributes)[key].set(value); | |
| 66 | + | } | |
| 67 | + | ||
| 68 | + | void ItemAttributes::setAttribute(const std::string& key, int32_t value) { | |
| 69 | + | createAttributes(); | |
| 70 | + | (*attributes)[key].set(value); | |
| 71 | + | } | |
| 72 | + | ||
| 73 | + | void ItemAttributes::setAttribute(const std::string& key, double value) { | |
| 74 | + | createAttributes(); | |
| 75 | + | (*attributes)[key].set(value); | |
| 76 | + | } | |
| 77 | + | ||
| 78 | + | void ItemAttributes::setAttribute(const std::string& key, bool value) { | |
| 79 | + | createAttributes(); | |
| 80 | + | (*attributes)[key].set(value); | |
| 81 | + | } | |
| 82 | + | ||
| 83 | + | void ItemAttributes::eraseAttribute(const std::string& key) { | |
| 84 | + | if (!attributes) { | |
| 85 | + | return; | |
| 86 | + | } | |
| 87 | + | ||
| 88 | + | ItemAttributeMap::iterator iter = attributes->find(key); | |
| 89 | + | ||
| 90 | + | if (iter != attributes->end()) { | |
| 91 | + | attributes->erase(iter); | |
| 92 | + | } | |
| 93 | + | } | |
| 94 | + | ||
| 95 | + | const std::string* ItemAttributes::getStringAttribute(const std::string& key) const { | |
| 96 | + | if (!attributes) { | |
| 97 | + | return nullptr; | |
| 98 | + | } | |
| 99 | + | ||
| 100 | + | ItemAttributeMap::iterator iter = attributes->find(key); | |
| 101 | + | if (iter != attributes->end()) { | |
| 102 | + | return iter->second.getString(); | |
| 103 | + | } | |
| 104 | + | return nullptr; | |
| 105 | + | } | |
| 106 | + | ||
| 107 | + | const int32_t* ItemAttributes::getIntegerAttribute(const std::string& key) const { | |
| 108 | + | if (!attributes) { | |
| 109 | + | return nullptr; | |
| 110 | + | } | |
| 111 | + | ||
| 112 | + | ItemAttributeMap::iterator iter = attributes->find(key); | |
| 113 | + | if (iter != attributes->end()) { | |
| 114 | + | return iter->second.getInteger(); | |
| 115 | + | } | |
| 116 | + | return nullptr; | |
| 117 | + | } | |
| 118 | + | ||
| 119 | + | const double* ItemAttributes::getFloatAttribute(const std::string& key) const { | |
| 120 | + | if (!attributes) { | |
| 121 | + | return nullptr; | |
| 122 | + | } | |
| 123 | + | ||
| 124 | + | ItemAttributeMap::iterator iter = attributes->find(key); | |
| 125 | + | if (iter != attributes->end()) { | |
| 126 | + | return iter->second.getFloat(); | |
| 127 | + | } | |
| 128 | + | return nullptr; | |
| 129 | + | } | |
| 130 | + | ||
| 131 | + | const bool* ItemAttributes::getBooleanAttribute(const std::string& key) const { | |
| 132 | + | if (!attributes) { | |
| 133 | + | return nullptr; | |
| 134 | + | } | |
| 135 | + | ||
| 136 | + | ItemAttributeMap::iterator iter = attributes->find(key); | |
| 137 | + | if (iter != attributes->end()) { | |
| 138 | + | return iter->second.getBoolean(); | |
| 139 | + | } | |
| 140 | + | return nullptr; | |
| 141 | + | } | |
| 142 | + | ||
| 143 | + | bool ItemAttributes::hasStringAttribute(const std::string& key) const { | |
| 144 | + | return getStringAttribute(key) != nullptr; | |
| 145 | + | } | |
| 146 | + | ||
| 147 | + | bool ItemAttributes::hasIntegerAttribute(const std::string& key) const { | |
| 148 | + | return getIntegerAttribute(key) != nullptr; | |
| 149 | + | } | |
| 150 | + | ||
| 151 | + | bool ItemAttributes::hasFloatAttribute(const std::string& key) const { | |
| 152 | + | return getFloatAttribute(key) != nullptr; | |
| 153 | + | } | |
| 154 | + | ||
| 155 | + | bool ItemAttributes::hasBooleanAttribute(const std::string& key) const { | |
| 156 | + | return getBooleanAttribute(key) != nullptr; | |
| 157 | + | } | |
| 158 | + | ||
| 159 | + | // Attribute type | |
| 160 | + | // Can hold either int, bool or std::string | |
| 161 | + | // Without using newd to allocate them | |
| 162 | + | ||
| 163 | + | ItemAttribute::ItemAttribute() : | |
| 164 | + | type(ItemAttribute::NONE) { | |
| 165 | + | //// | |
| 166 | + | } | |
| 167 | + | ||
| 168 | + | ItemAttribute::ItemAttribute(const std::string& str) : | |
| 169 | + | type(ItemAttribute::STRING) { | |
| 170 | + | new (data) std::string(str); | |
| 171 | + | } | |
| 172 | + | ||
| 173 | + | ItemAttribute::ItemAttribute(int32_t i) : | |
| 174 | + | type(ItemAttribute::INTEGER) { | |
| 175 | + | *reinterpret_cast<int*>(data) = i; | |
| 176 | + | } | |
| 177 | + | ||
| 178 | + | ItemAttribute::ItemAttribute(double f) : | |
| 179 | + | type(ItemAttribute::DOUBLE) { | |
| 180 | + | *reinterpret_cast<double*>(data) = f; | |
| 181 | + | } | |
| 182 | + | ||
| 183 | + | ItemAttribute::ItemAttribute(bool b) { | |
| 184 | + | *reinterpret_cast<bool*>(data) = b; | |
| 185 | + | } | |
| 186 | + | ||
| 187 | + | ItemAttribute::ItemAttribute(const ItemAttribute& o) : | |
| 188 | + | type(ItemAttribute::NONE) { | |
| 189 | + | *this = o; | |
| 190 | + | } | |
| 191 | + | ||
| 192 | + | ItemAttribute& ItemAttribute::operator=(const ItemAttribute& o) { | |
| 193 | + | if (&o == this) { | |
| 194 | + | return *this; | |
| 195 | + | } | |
| 196 | + | ||
| 197 | + | clear(); | |
| 198 | + | type = o.type; | |
| 199 | + | if (type == STRING) { | |
| 200 | + | new (data) std::string(*reinterpret_cast<const std::string*>(&o.data)); | |
| 201 | + | } else if (type == INTEGER) { | |
| 202 | + | *reinterpret_cast<int32_t*>(data) = *reinterpret_cast<const int32_t*>(&o.data); | |
| 203 | + | } else if (type == FLOAT) { | |
| 204 | + | *reinterpret_cast<float*>(data) = *reinterpret_cast<const float*>(&o.data); | |
| 205 | + | } else if (type == DOUBLE) { | |
| 206 | + | *reinterpret_cast<double*>(data) = *reinterpret_cast<const double*>(&o.data); | |
| 207 | + | } else if (type == BOOLEAN) { | |
| 208 | + | *reinterpret_cast<bool*>(data) = *reinterpret_cast<const bool*>(&o.data); | |
| 209 | + | } else { | |
| 210 | + | type = NONE; | |
| 211 | + | } | |
| 212 | + | ||
| 213 | + | return *this; | |
| 214 | + | } | |
| 215 | + | ||
| 216 | + | ItemAttribute::~ItemAttribute() { | |
| 217 | + | clear(); | |
| 218 | + | } | |
| 219 | + | ||
| 220 | + | void ItemAttribute::clear() { | |
| 221 | + | if (type == STRING) { | |
| 222 | + | (reinterpret_cast<std::string*>(&data))->~basic_string(); | |
| 223 | + | type = NONE; | |
| 224 | + | } | |
| 225 | + | } | |
| 226 | + | ||
| 227 | + | void ItemAttribute::set(const std::string& str) { | |
| 228 | + | clear(); | |
| 229 | + | type = STRING; | |
| 230 | + | new (data) std::string(str); | |
| 231 | + | } | |
| 232 | + | ||
| 233 | + | void ItemAttribute::set(int32_t i) { | |
| 234 | + | clear(); | |
| 235 | + | type = INTEGER; | |
| 236 | + | *reinterpret_cast<int32_t*>(&data) = i; | |
| 237 | + | } | |
| 238 | + | ||
| 239 | + | void ItemAttribute::set(double y) { | |
| 240 | + | clear(); | |
| 241 | + | type = DOUBLE; | |
| 242 | + | *reinterpret_cast<double*>(&data) = y; | |
| 243 | + | } | |
| 244 | + | ||
| 245 | + | void ItemAttribute::set(bool b) { | |
| 246 | + | clear(); | |
| 247 | + | type = BOOLEAN; | |
| 248 | + | *reinterpret_cast<bool*>(&data) = b; | |
| 249 | + | } | |
| 250 | + | ||
| 251 | + | const std::string* ItemAttribute::getString() const { | |
| 252 | + | if (type == STRING) { | |
| 253 | + | return reinterpret_cast<const std::string*>(&data); | |
| 254 | + | } | |
| 255 | + | return nullptr; | |
| 256 | + | } | |
| 257 | + | ||
| 258 | + | const int32_t* ItemAttribute::getInteger() const { | |
| 259 | + | if (type == INTEGER) { | |
| 260 | + | return reinterpret_cast<const int32_t*>(&data); | |
| 261 | + | } | |
| 262 | + | return nullptr; | |
| 263 | + | } | |
| 264 | + | ||
| 265 | + | const double* ItemAttribute::getFloat() const { | |
| 266 | + | if (type == DOUBLE) { | |
| 267 | + | return reinterpret_cast<const double*>(&data); | |
| 268 | + | } | |
| 269 | + | return nullptr; | |
| 270 | + | } | |
| 271 | + | ||
| 272 | + | const bool* ItemAttribute::getBoolean() const { | |
| 273 | + | if (type == BOOLEAN) { | |
| 274 | + | return reinterpret_cast<const bool*>(&data); | |
| 275 | + | } | |
| 276 | + | return nullptr; | |
| 277 | + | } | |
| 278 | + | ||
| 279 | + | bool ItemAttributes::unserializeAttributeMap(const IOMap& maphandle, BinaryNode* stream) { | |
| 280 | + | uint16_t n; | |
| 281 | + | if (stream->getU16(n)) { | |
| 282 | + | createAttributes(); | |
| 283 | + | ||
| 284 | + | std::string key; | |
| 285 | + | ItemAttribute attrib; | |
| 286 | + | ||
| 287 | + | while (n--) { | |
| 288 | + | if (!stream->getString(key)) { | |
| 289 | + | return false; | |
| 290 | + | } | |
| 291 | + | if (!attrib.unserialize(maphandle, stream)) { | |
| 292 | + | return false; | |
| 293 | + | } | |
| 294 | + | (*attributes)[key] = attrib; | |
| 295 | + | } | |
| 296 | + | } | |
| 297 | + | return true; | |
| 298 | + | } | |
| 299 | + | ||
| 300 | + | void ItemAttributes::serializeAttributeMap(const IOMap& maphandle, NodeFileWriteHandle& f) const { | |
| 301 | + | // Maximum of 65535 attributes per item | |
| 302 | + | f.addU16(std::min((size_t)0xFFFF, attributes->size())); | |
| 303 | + | ||
| 304 | + | ItemAttributeMap::const_iterator attribute = attributes->begin(); | |
| 305 | + | int i = 0; | |
| 306 | + | while (attribute != attributes->end() && i <= 0xFFFF) { | |
| 307 | + | const std::string& key = attribute->first; | |
| 308 | + | if (key.size() > 0xFFFF) { | |
| 309 | + | f.addString(key.substr(0, 65535)); | |
| 310 | + | } else { | |
| 311 | + | f.addString(key); | |
| 312 | + | } | |
| 313 | + | ||
| 314 | + | attribute->second.serialize(maphandle, f); | |
| 315 | + | ++attribute, ++i; | |
| 316 | + | } | |
| 317 | + | } | |
| 318 | + | ||
| 319 | + | bool ItemAttribute::unserialize(const IOMap& maphandle, BinaryNode* stream) { | |
| 320 | + | // Read type | |
| 321 | + | uint8_t rtype; | |
| 322 | + | stream->getU8(rtype); | |
| 323 | + | ||
| 324 | + | // Read contents | |
| 325 | + | switch (rtype) { | |
| 326 | + | case STRING: { | |
| 327 | + | std::string str; | |
| 328 | + | if (!stream->getLongString(str)) { | |
| 329 | + | return false; | |
| 330 | + | } | |
| 331 | + | set(str); | |
| 332 | + | break; | |
| 333 | + | } | |
| 334 | + | case INTEGER: { | |
| 335 | + | uint32_t u32; | |
| 336 | + | if (!stream->getU32(u32)) { | |
| 337 | + | return false; | |
| 338 | + | } | |
| 339 | + | set(*reinterpret_cast<int32_t*>(&u32)); | |
| 340 | + | break; | |
| 341 | + | } | |
| 342 | + | case FLOAT: { | |
| 343 | + | uint32_t u32; | |
| 344 | + | if (!stream->getU32(u32)) { | |
| 345 | + | return false; | |
| 346 | + | } | |
| 347 | + | set((double)*reinterpret_cast<float*>(&u32)); | |
| 348 | + | break; | |
| 349 | + | } | |
| 350 | + | case DOUBLE: { | |
| 351 | + | uint64_t u64; | |
| 352 | + | if (!stream->getU64(u64)) { | |
| 353 | + | return false; | |
| 354 | + | } | |
| 355 | + | set(*reinterpret_cast<double*>(&u64)); | |
| 356 | + | break; | |
| 357 | + | } | |
| 358 | + | case BOOLEAN: { | |
| 359 | + | uint8_t b; | |
| 360 | + | if (!stream->getU8(b)) { | |
| 361 | + | return false; | |
| 362 | + | } | |
| 363 | + | set(b != 0); | |
| 364 | + | } | |
| 365 | + | default: | |
| 366 | + | break; | |
| 367 | + | } | |
| 368 | + | return true; | |
| 369 | + | } | |
| 370 | + | ||
| 371 | + | void ItemAttribute::serialize(const IOMap& maphandle, NodeFileWriteHandle& f) const { | |
| 372 | + | // Write type | |
| 373 | + | f.addU8((uint8_t)(type)); | |
| 374 | + | ||
| 375 | + | // Write contents | |
| 376 | + | switch (type) { | |
| 377 | + | case STRING: | |
| 378 | + | f.addLongString(*getString()); | |
| 379 | + | break; | |
| 380 | + | case INTEGER: | |
| 381 | + | f.addU32(*(uint32_t*)getInteger()); | |
| 382 | + | break; | |
| 383 | + | case DOUBLE: | |
| 384 | + | f.addU64(*(uint64_t*)getFloat()); | |
| 385 | + | break; | |
| 386 | + | case BOOLEAN: | |
| 387 | + | f.addU8(*(uint8_t*)getBoolean()); | |
| 388 | + | default: | |
| 389 | + | break; | |
| 390 | + | } | |
| 391 | + | } |
| @@ -0,0 +1,115 @@ | |||
| 1 | + | ////////////////////////////////////////////////////////////////////// | |
| 2 | + | // This file is part of Remere's Map Editor | |
| 3 | + | ////////////////////////////////////////////////////////////////////// | |
| 4 | + | // Remere's Map Editor is free software: you can redistribute it and/or modify | |
| 5 | + | // it under the terms of the GNU General Public License as published by | |
| 6 | + | // the Free Software Foundation, either version 3 of the License, or | |
| 7 | + | // (at your option) any later version. | |
| 8 | + | // | |
| 9 | + | // Remere's Map Editor is distributed in the hope that it will be useful, | |
| 10 | + | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11 | + | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 12 | + | // GNU General Public License for more details. | |
| 13 | + | // | |
| 14 | + | // You should have received a copy of the GNU General Public License | |
| 15 | + | // along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 16 | + | ////////////////////////////////////////////////////////////////////// | |
| 17 | + | ||
| 18 | + | #ifndef RME_ITEM_ATTRIBUTES_H_ | |
| 19 | + | #define RME_ITEM_ATTRIBUTES_H_ | |
| 20 | + | ||
| 21 | + | #include <string> | |
| 22 | + | #include <map> | |
| 23 | + | ||
| 24 | + | #include "filehandle.h" | |
| 25 | + | ||
| 26 | + | #include <boost/static_assert.hpp> | |
| 27 | + | ||
| 28 | + | class IOMap; | |
| 29 | + | class ItemAttribute; | |
| 30 | + | ||
| 31 | + | class PropWriteStream; | |
| 32 | + | class PropStream; | |
| 33 | + | ||
| 34 | + | class ItemAttribute { | |
| 35 | + | public: | |
| 36 | + | ItemAttribute(); | |
| 37 | + | ItemAttribute(const std::string& str); | |
| 38 | + | ItemAttribute(int32_t i); | |
| 39 | + | ItemAttribute(double f); | |
| 40 | + | ItemAttribute(bool b); | |
| 41 | + | ItemAttribute(const ItemAttribute& o); | |
| 42 | + | ItemAttribute& operator=(const ItemAttribute& o); | |
| 43 | + | ~ItemAttribute(); | |
| 44 | + | ||
| 45 | + | enum Type { | |
| 46 | + | STRING = 1, | |
| 47 | + | INTEGER = 2, | |
| 48 | + | FLOAT = 3, | |
| 49 | + | BOOLEAN = 4, | |
| 50 | + | DOUBLE = 5, | |
| 51 | + | NONE = 0 | |
| 52 | + | } type; | |
| 53 | + | ||
| 54 | + | void serialize(const IOMap& maphandle, NodeFileWriteHandle& f) const; | |
| 55 | + | bool unserialize(const IOMap& maphandle, BinaryNode* f); | |
| 56 | + | ||
| 57 | + | void clear(); | |
| 58 | + | ||
| 59 | + | void set(const std::string& str); | |
| 60 | + | void set(int32_t i); | |
| 61 | + | void set(double f); | |
| 62 | + | void set(bool b); | |
| 63 | + | ||
| 64 | + | const std::string* getString() const; | |
| 65 | + | const int32_t* getInteger() const; | |
| 66 | + | const double* getFloat() const; | |
| 67 | + | const bool* getBoolean() const; | |
| 68 | + | ||
| 69 | + | private: | |
| 70 | + | char data[sizeof(std::string) > sizeof(double) ? sizeof(std::string) : sizeof(double)]; | |
| 71 | + | }; | |
| 72 | + | ||
| 73 | + | typedef std::map<std::string, ItemAttribute> ItemAttributeMap; | |
| 74 | + | ||
| 75 | + | class ItemAttributes { | |
| 76 | + | public: | |
| 77 | + | ItemAttributes(); | |
| 78 | + | ItemAttributes(const ItemAttributes& i); | |
| 79 | + | virtual ~ItemAttributes(); | |
| 80 | + | ||
| 81 | + | // Save / load | |
| 82 | + | void serializeAttributeMap(const IOMap& maphandle, NodeFileWriteHandle& f) const; | |
| 83 | + | bool unserializeAttributeMap(const IOMap& maphandle, BinaryNode* node); | |
| 84 | + | ||
| 85 | + | public: | |
| 86 | + | void setAttribute(const std::string& key, const ItemAttribute& attr); | |
| 87 | + | void setAttribute(const std::string& key, const std::string& value); | |
| 88 | + | void setAttribute(const std::string& key, int32_t value); | |
| 89 | + | void setAttribute(const std::string& key, double value); | |
| 90 | + | void setAttribute(const std::string& key, bool set); | |
| 91 | + | ||
| 92 | + | // returns nullptr if the attribute is not set | |
| 93 | + | const std::string* getStringAttribute(const std::string& key) const; | |
| 94 | + | const int32_t* getIntegerAttribute(const std::string& key) const; | |
| 95 | + | const double* getFloatAttribute(const std::string& key) const; | |
| 96 | + | const bool* getBooleanAttribute(const std::string& key) const; | |
| 97 | + | ||
| 98 | + | // Returns true if the attribute (of that type) exists | |
| 99 | + | bool hasStringAttribute(const std::string& key) const; | |
| 100 | + | bool hasIntegerAttribute(const std::string& key) const; | |
| 101 | + | bool hasFloatAttribute(const std::string& key) const; | |
| 102 | + | bool hasBooleanAttribute(const std::string& key) const; | |
| 103 | + | ||
| 104 | + | void eraseAttribute(const std::string& key); | |
| 105 | + | ||
| 106 | + | void clearAllAttributes(); | |
| 107 | + | ItemAttributeMap getAttributes() const; | |
| 108 | + | ||
| 109 | + | protected: | |
| 110 | + | ItemAttributeMap* attributes; | |
| 111 | + | ||
| 112 | + | void createAttributes(); | |
| 113 | + | }; | |
| 114 | + | ||
| 115 | + | #endif |
| @@ -0,0 +1,27 @@ | |||
| 1 | + | ////////////////////////////////////////////////////////////////////// | |
| 2 | + | // This file is part of Remere's Map Editor | |
| 3 | + | ////////////////////////////////////////////////////////////////////// | |
| 4 | + | // Remere's Map Editor is free software: you can redistribute it and/or modify | |
| 5 | + | // it under the terms of the GNU General Public License as published by | |
| 6 | + | // the Free Software Foundation, either version 3 of the License, or | |
| 7 | + | // (at your option) any later version. | |
| 8 | + | // | |
| 9 | + | // Remere's Map Editor is distributed in the hope that it will be useful, | |
| 10 | + | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11 | + | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 12 | + | // GNU General Public License for more details. | |
| 13 | + | // | |
| 14 | + | // You should have received a copy of the GNU General Public License | |
| 15 | + | // along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 16 | + | ////////////////////////////////////////////////////////////////////// | |
| 17 | + | ||
| 18 | + | #ifndef RME_JSON_H_ | |
| 19 | + | #define RME_JSON_H_ | |
| 20 | + | ||
| 21 | + | #include "json/json_spirit.h" | |
| 22 | + | ||
| 23 | + | namespace json { | |
| 24 | + | using namespace json_spirit; | |
| 25 | + | } | |
| 26 | + | ||
| 27 | + | #endif |
| @@ -0,0 +1,18 @@ | |||
| 1 | + | #ifndef JSON_SPIRIT | |
| 2 | + | #define JSON_SPIRIT | |
| 3 | + | ||
| 4 | + | // Copyright John W. Wilkinson 2007 - 2009. | |
| 5 | + | // Distributed under the MIT License, see accompanying file LICENSE.txt | |
| 6 | + | ||
| 7 | + | // json spirit version 4.03 | |
| 8 | + | ||
| 9 | + | #if defined(_MSC_VER) && (_MSC_VER >= 1020) | |
| 10 | + | # pragma once | |
| 11 | + | #endif | |
| 12 | + | ||
| 13 | + | #include "json_spirit_value.h" | |
| 14 | + | #include "json_spirit_reader.h" | |
| 15 | + | #include "json_spirit_writer.h" | |
| 16 | + | #include "json_spirit_utils.h" | |
| 17 | + | ||
| 18 | + | #endif |
| @@ -0,0 +1,56 @@ | |||
| 1 | + | #ifndef JSON_SPIRIT_ERROR_POSITION | |
| 2 | + | #define JSON_SPIRIT_ERROR_POSITION | |
| 3 | + | ||
| 4 | + | // Copyright John W. Wilkinson 2007 - 2009. | |
| 5 | + | // Distributed under the MIT License, see accompanying file LICENSE.txt | |
| 6 | + | ||
| 7 | + | // json spirit version 4.03 | |
| 8 | + | ||
| 9 | + | #if defined(_MSC_VER) && (_MSC_VER >= 1020) | |
| 10 | + | # pragma once | |
| 11 | + | #endif | |
| 12 | + | ||
| 13 | + | #include <string> | |
| 14 | + | ||
| 15 | + | namespace json_spirit | |
| 16 | + | { | |
| 17 | + | // An Error_position exception is thrown by the "read_or_throw" functions below on finding an error. | |
| 18 | + | // Note the "read_or_throw" functions are around 3 times slower than the standard functions "read" | |
| 19 | + | // functions that return a bool. | |
| 20 | + | // | |
| 21 | + | struct Error_position : std::runtime_error | |
| 22 | + | { | |
| 23 | + | Error_position(); | |
| 24 | + | Error_position( unsigned int line, unsigned int column, const std::string& reason ); | |
| 25 | + | bool operator==( const Error_position& lhs ) const; | |
| 26 | + | unsigned int line_; | |
| 27 | + | unsigned int column_; | |
| 28 | + | std::string reason_; | |
| 29 | + | }; | |
| 30 | + | ||
| 31 | + | inline Error_position::Error_position() | |
| 32 | + | : std::runtime_error("Error_position") | |
| 33 | + | , line_( 0 ) | |
| 34 | + | , column_( 0 ) | |
| 35 | + | { | |
| 36 | + | } | |
| 37 | + | ||
| 38 | + | inline Error_position::Error_position( unsigned int line, unsigned int column, const std::string& reason ) | |
| 39 | + | : std::runtime_error("Error_position") | |
| 40 | + | , line_( line ) | |
| 41 | + | , column_( column ) | |
| 42 | + | , reason_( reason ) | |
| 43 | + | { | |
| 44 | + | } | |
| 45 | + | ||
| 46 | + | inline bool Error_position::operator==( const Error_position& lhs ) const | |
| 47 | + | { | |
| 48 | + | if( this == &lhs ) return true; | |
| 49 | + | ||
| 50 | + | return ( reason_ == lhs.reason_ ) && | |
| 51 | + | ( line_ == lhs.line_ ) && | |
| 52 | + | ( column_ == lhs.column_ ); | |
| 53 | + | } | |
| 54 | + | } | |
| 55 | + | ||
| 56 | + | #endif |
| @@ -0,0 +1,137 @@ | |||
| 1 | + | // Copyright John W. Wilkinson 2007 - 2009. | |
| 2 | + | // Distributed under the MIT License, see accompanying file LICENSE.txt | |
| 3 | + | ||
| 4 | + | // json spirit version 4.03 | |
| 5 | + | ||
| 6 | + | #include "json_spirit_reader.h" | |
| 7 | + | #include "json_spirit_reader_template.h" | |
| 8 | + | ||
| 9 | + | using namespace json_spirit; | |
| 10 | + | ||
| 11 | + | bool json_spirit::read( const std::string& s, Value& value ) | |
| 12 | + | { | |
| 13 | + | return read_string( s, value ); | |
| 14 | + | } | |
| 15 | + | ||
| 16 | + | void json_spirit::read_or_throw( const std::string& s, Value& value ) | |
| 17 | + | { | |
| 18 | + | read_string_or_throw( s, value ); | |
| 19 | + | } | |
| 20 | + | ||
| 21 | + | bool json_spirit::read( std::istream& is, Value& value ) | |
| 22 | + | { | |
| 23 | + | return read_stream( is, value ); | |
| 24 | + | } | |
| 25 | + | ||
| 26 | + | void json_spirit::read_or_throw( std::istream& is, Value& value ) | |
| 27 | + | { | |
| 28 | + | read_stream_or_throw( is, value ); | |
| 29 | + | } | |
| 30 | + | ||
| 31 | + | bool json_spirit::read( std::string::const_iterator& begin, std::string::const_iterator end, Value& value ) | |
| 32 | + | { | |
| 33 | + | return read_range( begin, end, value ); | |
| 34 | + | } | |
| 35 | + | ||
| 36 | + | void json_spirit::read_or_throw( std::string::const_iterator& begin, std::string::const_iterator end, Value& value ) | |
| 37 | + | { | |
| 38 | + | begin = read_range_or_throw( begin, end, value ); | |
| 39 | + | } | |
| 40 | + | ||
| 41 | + | #ifndef BOOST_NO_STD_WSTRING | |
| 42 | + | ||
| 43 | + | bool json_spirit::read( const std::wstring& s, wValue& value ) | |
| 44 | + | { | |
| 45 | + | return read_string( s, value ); | |
| 46 | + | } | |
| 47 | + | ||
| 48 | + | void json_spirit::read_or_throw( const std::wstring& s, wValue& value ) | |
| 49 | + | { | |
| 50 | + | read_string_or_throw( s, value ); | |
| 51 | + | } | |
| 52 | + | ||
| 53 | + | bool json_spirit::read( std::wistream& is, wValue& value ) | |
| 54 | + | { | |
| 55 | + | return read_stream( is, value ); | |
| 56 | + | } | |
| 57 | + | ||
| 58 | + | void json_spirit::read_or_throw( std::wistream& is, wValue& value ) | |
| 59 | + | { | |
| 60 | + | read_stream_or_throw( is, value ); | |
| 61 | + | } | |
| 62 | + | ||
| 63 | + | bool json_spirit::read( std::wstring::const_iterator& begin, std::wstring::const_iterator end, wValue& value ) | |
| 64 | + | { | |
| 65 | + | return read_range( begin, end, value ); | |
| 66 | + | } | |
| 67 | + | ||
| 68 | + | void json_spirit::read_or_throw( std::wstring::const_iterator& begin, std::wstring::const_iterator end, wValue& value ) | |
| 69 | + | { | |
| 70 | + | begin = read_range_or_throw( begin, end, value ); | |
| 71 | + | } | |
| 72 | + | ||
| 73 | + | #endif | |
| 74 | + | ||
| 75 | + | bool json_spirit::read( const std::string& s, mValue& value ) | |
| 76 | + | { | |
| 77 | + | return read_string( s, value ); | |
| 78 | + | } | |
| 79 | + | ||
| 80 | + | void json_spirit::read_or_throw( const std::string& s, mValue& value ) | |
| 81 | + | { | |
| 82 | + | read_string_or_throw( s, value ); | |
| 83 | + | } | |
| 84 | + | ||
| 85 | + | bool json_spirit::read( std::istream& is, mValue& value ) | |
| 86 | + | { | |
| 87 | + | return read_stream( is, value ); | |
| 88 | + | } | |
| 89 | + | ||
| 90 | + | void json_spirit::read_or_throw( std::istream& is, mValue& value ) | |
| 91 | + | { | |
| 92 | + | read_stream_or_throw( is, value ); | |
| 93 | + | } | |
| 94 | + | ||
| 95 | + | bool json_spirit::read( std::string::const_iterator& begin, std::string::const_iterator end, mValue& value ) | |
| 96 | + | { | |
| 97 | + | return read_range( begin, end, value ); | |
| 98 | + | } | |
| 99 | + | ||
| 100 | + | void json_spirit::read_or_throw( std::string::const_iterator& begin, std::string::const_iterator end, mValue& value ) | |
| 101 | + | { | |
| 102 | + | begin = read_range_or_throw( begin, end, value ); | |
| 103 | + | } | |
| 104 | + | ||
| 105 | + | #ifndef BOOST_NO_STD_WSTRING | |
| 106 | + | ||
| 107 | + | bool json_spirit::read( const std::wstring& s, wmValue& value ) | |
| 108 | + | { | |
| 109 | + | return read_string( s, value ); | |
| 110 | + | } | |
| 111 | + | ||
| 112 | + | void json_spirit::read_or_throw( const std::wstring& s, wmValue& value ) | |
| 113 | + | { | |
| 114 | + | read_string_or_throw( s, value ); | |
| 115 | + | } | |
| 116 | + | ||
| 117 | + | bool json_spirit::read( std::wistream& is, wmValue& value ) | |
| 118 | + | { | |
| 119 | + | return read_stream( is, value ); | |
| 120 | + | } | |
| 121 | + | ||
| 122 | + | void json_spirit::read_or_throw( std::wistream& is, wmValue& value ) | |
| 123 | + | { | |
| 124 | + | read_stream_or_throw( is, value ); | |
| 125 | + | } | |
| 126 | + | ||
| 127 | + | bool json_spirit::read( std::wstring::const_iterator& begin, std::wstring::const_iterator end, wmValue& value ) | |
| 128 | + | { | |
| 129 | + | return read_range( begin, end, value ); | |
| 130 | + | } | |
| 131 | + | ||
| 132 | + | void json_spirit::read_or_throw( std::wstring::const_iterator& begin, std::wstring::const_iterator end, wmValue& value ) | |
| 133 | + | { | |
| 134 | + | begin = read_range_or_throw( begin, end, value ); | |
| 135 | + | } | |
| 136 | + | ||
| 137 | + | #endif |
| @@ -0,0 +1,62 @@ | |||
| 1 | + | #ifndef JSON_SPIRIT_READER | |
| 2 | + | #define JSON_SPIRIT_READER | |
| 3 | + | ||
| 4 | + | // Copyright John W. Wilkinson 2007 - 2009. | |
| 5 | + | // Distributed under the MIT License, see accompanying file LICENSE.txt | |
| 6 | + | ||
| 7 | + | // json spirit version 4.03 | |
| 8 | + | ||
| 9 | + | #if defined(_MSC_VER) && (_MSC_VER >= 1020) | |
| 10 | + | # pragma once | |
| 11 | + | #endif | |
| 12 | + | ||
| 13 | + | #include "json_spirit_value.h" | |
| 14 | + | #include "json_spirit_error_position.h" | |
| 15 | + | #include <iostream> | |
| 16 | + | ||
| 17 | + | namespace json_spirit | |
| 18 | + | { | |
| 19 | + | // functions to reads a JSON values | |
| 20 | + | ||
| 21 | + | bool read( const std::string& s, Value& value ); | |
| 22 | + | bool read( std::istream& is, Value& value ); | |
| 23 | + | bool read( std::string::const_iterator& begin, std::string::const_iterator end, Value& value ); | |
| 24 | + | ||
| 25 | + | void read_or_throw( const std::string& s, Value& value ); | |
| 26 | + | void read_or_throw( std::istream& is, Value& value ); | |
| 27 | + | void read_or_throw( std::string::const_iterator& begin, std::string::const_iterator end, Value& value ); | |
| 28 | + | ||
| 29 | + | #ifndef BOOST_NO_STD_WSTRING | |
| 30 | + | ||
| 31 | + | bool read( const std::wstring& s, wValue& value ); | |
| 32 | + | bool read( std::wistream& is, wValue& value ); | |
| 33 | + | bool read( std::wstring::const_iterator& begin, std::wstring::const_iterator end, wValue& value ); | |
| 34 | + | ||
| 35 | + | void read_or_throw( const std::wstring& s, wValue& value ); | |
| 36 | + | void read_or_throw( std::wistream& is, wValue& value ); | |
| 37 | + | void read_or_throw( std::wstring::const_iterator& begin, std::wstring::const_iterator end, wValue& value ); | |
| 38 | + | ||
| 39 | + | #endif | |
| 40 | + | ||
| 41 | + | bool read( const std::string& s, mValue& value ); | |
| 42 | + | bool read( std::istream& is, mValue& value ); | |
| 43 | + | bool read( std::string::const_iterator& begin, std::string::const_iterator end, mValue& value ); | |
| 44 | + | ||
| 45 | + | void read_or_throw( const std::string& s, mValue& value ); | |
| 46 | + | void read_or_throw( std::istream& is, mValue& value ); | |
| 47 | + | void read_or_throw( std::string::const_iterator& begin, std::string::const_iterator end, mValue& value ); | |
| 48 | + | ||
| 49 | + | #ifndef BOOST_NO_STD_WSTRING | |
| 50 | + | ||
| 51 | + | bool read( const std::wstring& s, wmValue& value ); | |
| 52 | + | bool read( std::wistream& is, wmValue& value ); | |
| 53 | + | bool read( std::wstring::const_iterator& begin, std::wstring::const_iterator end, wmValue& value ); | |
| 54 | + | ||
| 55 | + | void read_or_throw( const std::wstring& s, wmValue& value ); | |
| 56 | + | void read_or_throw( std::wistream& is, wmValue& value ); | |
| 57 | + | void read_or_throw( std::wstring::const_iterator& begin, std::wstring::const_iterator end, wmValue& value ); | |
| 58 | + | ||
| 59 | + | #endif | |
| 60 | + | } | |
| 61 | + | ||
| 62 | + | #endif |
| @@ -0,0 +1,613 @@ | |||
| 1 | + | #ifndef JSON_SPIRIT_READER_TEMPLATE | |
| 2 | + | #define JSON_SPIRIT_READER_TEMPLATE | |
| 3 | + | ||
| 4 | + | // Copyright John W. Wilkinson 2007 - 2009. | |
| 5 | + | // Distributed under the MIT License, see accompanying file LICENSE.txt | |
| 6 | + | ||
| 7 | + | // json spirit version 4.03 | |
| 8 | + | ||
| 9 | + | #include "json_spirit_value.h" | |
| 10 | + | #include "json_spirit_error_position.h" | |
| 11 | + | ||
| 12 | + | #define BOOST_SPIRIT_THREADSAFE // uncomment for multithreaded use, requires linking to boost.thread | |
| 13 | + | ||
| 14 | + | #include <boost/bind.hpp> | |
| 15 | + | #include <boost/function.hpp> | |
| 16 | + | #include <boost/version.hpp> | |
| 17 | + | ||
| 18 | + | #if BOOST_VERSION >= 103800 | |
| 19 | + | #include <boost/spirit/include/classic_core.hpp> | |
| 20 | + | #include <boost/spirit/include/classic_confix.hpp> | |
| 21 | + | #include <boost/spirit/include/classic_escape_char.hpp> | |
| 22 | + | #include <boost/spirit/include/classic_multi_pass.hpp> | |
| 23 | + | #include <boost/spirit/include/classic_position_iterator.hpp> | |
| 24 | + | #define spirit_namespace boost::spirit::classic | |
| 25 | + | #else | |
| 26 | + | #include <boost/spirit/core.hpp> | |
| 27 | + | #include <boost/spirit/utility/confix.hpp> | |
| 28 | + | #include <boost/spirit/utility/escape_char.hpp> | |
| 29 | + | #include <boost/spirit/iterator/multi_pass.hpp> | |
| 30 | + | #include <boost/spirit/iterator/position_iterator.hpp> | |
| 31 | + | #define spirit_namespace boost::spirit | |
| 32 | + | #endif | |
| 33 | + | ||
| 34 | + | namespace json_spirit | |
| 35 | + | { | |
| 36 | + | const spirit_namespace::int_parser < boost::int64_t > int64_p = spirit_namespace::int_parser < boost::int64_t >(); | |
| 37 | + | const spirit_namespace::uint_parser< boost::uint64_t > uint64_p = spirit_namespace::uint_parser< boost::uint64_t >(); | |
| 38 | + | ||
| 39 | + | template< class Iter_type > | |
| 40 | + | bool is_eq( Iter_type first, Iter_type last, const char* c_str ) | |
| 41 | + | { | |
| 42 | + | for( Iter_type i = first; i != last; ++i, ++c_str ) | |
| 43 | + | { | |
| 44 | + | if( *c_str == 0 ) return false; | |
| 45 | + | ||
| 46 | + | if( *i != *c_str ) return false; | |
| 47 | + | } | |
| 48 | + | ||
| 49 | + | return true; | |
| 50 | + | } | |
| 51 | + | ||
| 52 | + | template< class Char_type > | |
| 53 | + | Char_type hex_to_num( const Char_type c ) | |
| 54 | + | { | |
| 55 | + | if( ( c >= '0' ) && ( c <= '9' ) ) return c - '0'; | |
| 56 | + | if( ( c >= 'a' ) && ( c <= 'f' ) ) return c - 'a' + 10; | |
| 57 | + | if( ( c >= 'A' ) && ( c <= 'F' ) ) return c - 'A' + 10; | |
| 58 | + | return 0; | |
| 59 | + | } | |
| 60 | + | ||
| 61 | + | template< class Char_type, class Iter_type > | |
| 62 | + | Char_type hex_str_to_char( Iter_type& begin ) | |
| 63 | + | { | |
| 64 | + | const Char_type c1( *( ++begin ) ); | |
| 65 | + | const Char_type c2( *( ++begin ) ); | |
| 66 | + | ||
| 67 | + | return ( hex_to_num( c1 ) << 4 ) + hex_to_num( c2 ); | |
| 68 | + | } | |
| 69 | + | ||
| 70 | + | template< class Char_type, class Iter_type > | |
| 71 | + | Char_type unicode_str_to_char( Iter_type& begin ) | |
| 72 | + | { | |
| 73 | + | const Char_type c1( *( ++begin ) ); | |
| 74 | + | const Char_type c2( *( ++begin ) ); | |
| 75 | + | const Char_type c3( *( ++begin ) ); | |
| 76 | + | const Char_type c4( *( ++begin ) ); | |
| 77 | + | ||
| 78 | + | return ( hex_to_num( c1 ) << 12 ) + | |
| 79 | + | ( hex_to_num( c2 ) << 8 ) + | |
| 80 | + | ( hex_to_num( c3 ) << 4 ) + | |
| 81 | + | hex_to_num( c4 ); | |
| 82 | + | } | |
| 83 | + | ||
| 84 | + | template< class String_type > | |
| 85 | + | void append_esc_char_and_incr_iter( String_type& s, | |
| 86 | + | typename String_type::const_iterator& begin, | |
| 87 | + | typename String_type::const_iterator end ) | |
| 88 | + | { | |
| 89 | + | typedef typename String_type::value_type Char_type; | |
| 90 | + | ||
| 91 | + | const Char_type c2( *begin ); | |
| 92 | + | ||
| 93 | + | switch( c2 ) | |
| 94 | + | { | |
| 95 | + | case 't': s += '\t'; break; | |
| 96 | + | case 'b': s += '\b'; break; | |
| 97 | + | case 'f': s += '\f'; break; | |
| 98 | + | case 'n': s += '\n'; break; | |
| 99 | + | case 'r': s += '\r'; break; | |
| 100 | + | case '\\': s += '\\'; break; | |
| 101 | + | case '/': s += '/'; break; | |
| 102 | + | case '"': s += '"'; break; | |
| 103 | + | case 'x': | |
| 104 | + | { | |
| 105 | + | if( end - begin >= 3 ) // expecting "xHH..." | |
| 106 | + | { | |
| 107 | + | s += hex_str_to_char< Char_type >( begin ); | |
| 108 | + | } | |
| 109 | + | break; | |
| 110 | + | } | |
| 111 | + | case 'u': | |
| 112 | + | { | |
| 113 | + | if( end - begin >= 5 ) // expecting "uHHHH..." | |
| 114 | + | { | |
| 115 | + | s += unicode_str_to_char< Char_type >( begin ); | |
| 116 | + | } | |
| 117 | + | break; | |
| 118 | + | } | |
| 119 | + | } | |
| 120 | + | } | |
| 121 | + | ||
| 122 | + | template< class String_type > | |
| 123 | + | String_type substitute_esc_chars( typename String_type::const_iterator begin, | |
| 124 | + | typename String_type::const_iterator end ) | |
| 125 | + | { | |
| 126 | + | typedef typename String_type::const_iterator Iter_type; | |
| 127 | + | ||
| 128 | + | if( end - begin < 2 ) return String_type( begin, end ); | |
| 129 | + | ||
| 130 | + | String_type result; | |
| 131 | + | ||
| 132 | + | result.reserve( end - begin ); | |
| 133 | + | ||
| 134 | + | const Iter_type end_minus_1( end - 1 ); | |
| 135 | + | ||
| 136 | + | Iter_type substr_start = begin; | |
| 137 | + | Iter_type i = begin; | |
| 138 | + | ||
| 139 | + | for( ; i < end_minus_1; ++i ) | |
| 140 | + | { | |
| 141 | + | if( *i == '\\' ) | |
| 142 | + | { | |
| 143 | + | result.append( substr_start, i ); | |
| 144 | + | ||
| 145 | + | ++i; // skip the '\' | |
| 146 | + | ||
| 147 | + | append_esc_char_and_incr_iter( result, i, end ); | |
| 148 | + | ||
| 149 | + | substr_start = i + 1; | |
| 150 | + | } | |
| 151 | + | } | |
| 152 | + | ||
| 153 | + | result.append( substr_start, end ); | |
| 154 | + | ||
| 155 | + | return result; | |
| 156 | + | } | |
| 157 | + | ||
| 158 | + | template< class String_type > | |
| 159 | + | String_type get_str_( typename String_type::const_iterator begin, | |
| 160 | + | typename String_type::const_iterator end ) | |
| 161 | + | { | |
| 162 | + | assert( end - begin >= 2 ); | |
| 163 | + | ||
| 164 | + | typedef typename String_type::const_iterator Iter_type; | |
| 165 | + | ||
| 166 | + | Iter_type str_without_quotes( ++begin ); | |
| 167 | + | Iter_type end_without_quotes( --end ); | |
| 168 | + | ||
| 169 | + | return substitute_esc_chars< String_type >( str_without_quotes, end_without_quotes ); | |
| 170 | + | } | |
| 171 | + | ||
| 172 | + | inline std::string get_str( std::string::const_iterator begin, std::string::const_iterator end ) | |
| 173 | + | { | |
| 174 | + | return get_str_< std::string >( begin, end ); | |
| 175 | + | } | |
| 176 | + | ||
| 177 | + | inline std::wstring get_str( std::wstring::const_iterator begin, std::wstring::const_iterator end ) | |
| 178 | + | { | |
| 179 | + | return get_str_< std::wstring >( begin, end ); | |
| 180 | + | } | |
| 181 | + | ||
| 182 | + | template< class String_type, class Iter_type > | |
| 183 | + | String_type get_str( Iter_type begin, Iter_type end ) | |
| 184 | + | { | |
| 185 | + | const String_type tmp( begin, end ); // convert multipass iterators to string iterators | |
| 186 | + | ||
| 187 | + | return get_str( tmp.begin(), tmp.end() ); | |
| 188 | + | } | |
| 189 | + | ||
| 190 | + | // this class's methods get called by the spirit parse resulting | |
| 191 | + | // in the creation of a JSON object or array | |
| 192 | + | // | |
| 193 | + | // NB Iter_type could be a std::string iterator, wstring iterator, a position iterator or a multipass iterator | |
| 194 | + | // | |
| 195 | + | template< class Value_type, class Iter_type > | |
| 196 | + | class Semantic_actions | |
| 197 | + | { | |
| 198 | + | public: | |
| 199 | + | ||
| 200 | + | typedef typename Value_type::Config_type Config_type; | |
| 201 | + | typedef typename Config_type::String_type String_type; | |
| 202 | + | typedef typename Config_type::Object_type Object_type; | |
| 203 | + | typedef typename Config_type::Array_type Array_type; | |
| 204 | + | typedef typename String_type::value_type Char_type; | |
| 205 | + | ||
| 206 | + | Semantic_actions( Value_type& value ) | |
| 207 | + | : value_( value ) | |
| 208 | + | , current_p_( 0 ) | |
| 209 | + | { | |
| 210 | + | } | |
| 211 | + | ||
| 212 | + | void begin_obj( Char_type c ) | |
| 213 | + | { | |
| 214 | + | assert( c == '{' ); | |
| 215 | + | ||
| 216 | + | begin_compound< Object_type >(); | |
| 217 | + | } | |
| 218 | + | ||
| 219 | + | void end_obj( Char_type c ) | |
| 220 | + | { | |
| 221 | + | assert( c == '}' ); | |
| 222 | + | ||
| 223 | + | end_compound(); | |
| 224 | + | } | |
| 225 | + | ||
| 226 | + | void begin_array( Char_type c ) | |
| 227 | + | { | |
| 228 | + | assert( c == '[' ); | |
| 229 | + | ||
| 230 | + | begin_compound< Array_type >(); | |
| 231 | + | } | |
| 232 | + | ||
| 233 | + | void end_array( Char_type c ) | |
| 234 | + | { | |
| 235 | + | assert( c == ']' ); | |
| 236 | + | ||
| 237 | + | end_compound(); | |
| 238 | + | } | |
| 239 | + | ||
| 240 | + | void new_name( Iter_type begin, Iter_type end ) | |
| 241 | + | { | |
| 242 | + | assert( current_p_->type() == obj_type ); | |
| 243 | + | ||
| 244 | + | name_ = get_str< String_type >( begin, end ); | |
| 245 | + | } | |
| 246 | + | ||
| 247 | + | void new_str( Iter_type begin, Iter_type end ) | |
| 248 | + | { | |
| 249 | + | add_to_current( get_str< String_type >( begin, end ) ); | |
| 250 | + | } | |
| 251 | + | ||
| 252 | + | void new_true( Iter_type begin, Iter_type end ) | |
| 253 | + | { | |
| 254 | + | assert( is_eq( begin, end, "true" ) ); | |
| 255 | + | ||
| 256 | + | add_to_current( true ); | |
| 257 | + | } | |
| 258 | + | ||
| 259 | + | void new_false( Iter_type begin, Iter_type end ) | |
| 260 | + | { | |
| 261 | + | assert( is_eq( begin, end, "false" ) ); | |
| 262 | + | ||
| 263 | + | add_to_current( false ); | |
| 264 | + | } | |
| 265 | + | ||
| 266 | + | void new_null( Iter_type begin, Iter_type end ) | |
| 267 | + | { | |
| 268 | + | assert( is_eq( begin, end, "null" ) ); | |
| 269 | + | ||
| 270 | + | add_to_current( Value_type() ); | |
| 271 | + | } | |
| 272 | + | ||
| 273 | + | void new_int( boost::int64_t i ) | |
| 274 | + | { | |
| 275 | + | add_to_current( i ); | |
| 276 | + | } | |
| 277 | + | ||
| 278 | + | void new_uint64( boost::uint64_t ui ) | |
| 279 | + | { | |
| 280 | + | add_to_current( ui ); | |
| 281 | + | } | |
| 282 | + | ||
| 283 | + | void new_real( double d ) | |
| 284 | + | { | |
| 285 | + | add_to_current( d ); | |
| 286 | + | } | |
| 287 | + | ||
| 288 | + | private: | |
| 289 | + | ||
| 290 | + | Semantic_actions& operator=( const Semantic_actions& ); | |
| 291 | + | // to prevent "assignment operator could not be generated" warning | |
| 292 | + | ||
| 293 | + | Value_type* add_first( const Value_type& value ) | |
| 294 | + | { | |
| 295 | + | assert( current_p_ == 0 ); | |
| 296 | + | ||
| 297 | + | value_ = value; | |
| 298 | + | current_p_ = &value_; | |
| 299 | + | return current_p_; | |
| 300 | + | } | |
| 301 | + | ||
| 302 | + | template< class Array_or_obj > | |
| 303 | + | void begin_compound() | |
| 304 | + | { | |
| 305 | + | if( current_p_ == 0 ) | |
| 306 | + | { | |
| 307 | + | add_first( Array_or_obj() ); | |
| 308 | + | } | |
| 309 | + | else | |
| 310 | + | { | |
| 311 | + | stack_.push_back( current_p_ ); | |
| 312 | + | ||
| 313 | + | Array_or_obj new_array_or_obj; // avoid copy by building newd array or object in place | |
| 314 | + | ||
| 315 | + | current_p_ = add_to_current( new_array_or_obj ); | |
| 316 | + | } | |
| 317 | + | } | |
| 318 | + | ||
| 319 | + | void end_compound() | |
| 320 | + | { | |
| 321 | + | if( current_p_ != &value_ ) | |
| 322 | + | { | |
| 323 | + | current_p_ = stack_.back(); | |
| 324 | + | ||
| 325 | + | stack_.pop_back(); | |
| 326 | + | } | |
| 327 | + | } | |
| 328 | + | ||
| 329 | + | Value_type* add_to_current( const Value_type& value ) | |
| 330 | + | { | |
| 331 | + | if( current_p_ == 0 ) | |
| 332 | + | { | |
| 333 | + | return add_first( value ); | |
| 334 | + | } | |
| 335 | + | else if( current_p_->type() == array_type ) | |
| 336 | + | { | |
| 337 | + | current_p_->get_array().push_back( value ); | |
| 338 | + | ||
| 339 | + | return ¤t_p_->get_array().back(); | |
| 340 | + | } | |
| 341 | + | ||
| 342 | + | assert( current_p_->type() == obj_type ); | |
| 343 | + | ||
| 344 | + | return &Config_type::add( current_p_->get_obj(), name_, value ); | |
| 345 | + | } | |
| 346 | + | ||
| 347 | + | Value_type& value_; // this is the object or array that is being created | |
| 348 | + | Value_type* current_p_; // the child object or array that is currently being constructed | |
| 349 | + | ||
| 350 | + | std::vector< Value_type* > stack_; // previous child objects and arrays | |
| 351 | + | ||
| 352 | + | String_type name_; // of current name/value pair | |
| 353 | + | }; | |
| 354 | + | ||
| 355 | + | template< typename Iter_type > | |
| 356 | + | void throw_error( spirit_namespace::position_iterator< Iter_type > i, const std::string& reason ) | |
| 357 | + | { | |
| 358 | + | throw Error_position( i.get_position().line, i.get_position().column, reason ); | |
| 359 | + | } | |
| 360 | + | ||
| 361 | + | template< typename Iter_type > | |
| 362 | + | void throw_error( Iter_type i, const std::string& reason ) | |
| 363 | + | { | |
| 364 | + | throw reason; | |
| 365 | + | } | |
| 366 | + | ||
| 367 | + | // the spirit grammer | |
| 368 | + | // | |
| 369 | + | template< class Value_type, class Iter_type > | |
| 370 | + | class Json_grammer : public spirit_namespace::grammar< Json_grammer< Value_type, Iter_type > > | |
| 371 | + | { | |
| 372 | + | public: | |
| 373 | + | ||
| 374 | + | typedef Semantic_actions< Value_type, Iter_type > Semantic_actions_t; | |
| 375 | + | ||
| 376 | + | Json_grammer( Semantic_actions_t& semantic_actions ) | |
| 377 | + | : actions_( semantic_actions ) | |
| 378 | + | { | |
| 379 | + | } | |
| 380 | + | ||
| 381 | + | static void throw_not_value( Iter_type begin, Iter_type end ) | |
| 382 | + | { | |
| 383 | + | throw_error( begin, "not a value" ); | |
| 384 | + | } | |
| 385 | + | ||
| 386 | + | static void throw_not_array( Iter_type begin, Iter_type end ) | |
| 387 | + | { | |
| 388 | + | throw_error( begin, "not an array" ); | |
| 389 | + | } | |
| 390 | + | ||
| 391 | + | static void throw_not_object( Iter_type begin, Iter_type end ) | |
| 392 | + | { | |
| 393 | + | throw_error( begin, "not an object" ); | |
| 394 | + | } | |
| 395 | + | ||
| 396 | + | static void throw_not_pair( Iter_type begin, Iter_type end ) | |
| 397 | + | { | |
| 398 | + | throw_error( begin, "not a pair" ); | |
| 399 | + | } | |
| 400 | + | ||
| 401 | + | static void throw_not_colon( Iter_type begin, Iter_type end ) | |
| 402 | + | { | |
| 403 | + | throw_error( begin, "no colon in pair" ); | |
| 404 | + | } | |
| 405 | + | ||
| 406 | + | static void throw_not_string( Iter_type begin, Iter_type end ) | |
| 407 | + | { | |
| 408 | + | throw_error( begin, "not a string" ); | |
| 409 | + | } | |
| 410 | + | ||
| 411 | + | template< typename ScannerT > | |
| 412 | + | class definition | |
| 413 | + | { | |
| 414 | + | public: | |
| 415 | + | ||
| 416 | + | definition( const Json_grammer& self ) | |
| 417 | + | { | |
| 418 | + | using namespace spirit_namespace; | |
| 419 | + | ||
| 420 | + | typedef typename Value_type::String_type::value_type Char_type; | |
| 421 | + | ||
| 422 | + | // first we convert the semantic action class methods to functors with the | |
| 423 | + | // parameter signature expected by spirit | |
| 424 | + | ||
| 425 | + | typedef boost::function< void( Char_type ) > Char_action; | |
| 426 | + | typedef boost::function< void( Iter_type, Iter_type ) > Str_action; | |
| 427 | + | typedef boost::function< void( double ) > Real_action; | |
| 428 | + | typedef boost::function< void( boost::int64_t ) > Int_action; | |
| 429 | + | typedef boost::function< void( boost::uint64_t ) > Uint64_action; | |
| 430 | + | ||
| 431 | + | Char_action begin_obj ( boost::bind( &Semantic_actions_t::begin_obj, &self.actions_, _1 ) ); | |
| 432 | + | Char_action end_obj ( boost::bind( &Semantic_actions_t::end_obj, &self.actions_, _1 ) ); | |
| 433 | + | Char_action begin_array( boost::bind( &Semantic_actions_t::begin_array, &self.actions_, _1 ) ); | |
| 434 | + | Char_action end_array ( boost::bind( &Semantic_actions_t::end_array, &self.actions_, _1 ) ); | |
| 435 | + | Str_action new_name ( boost::bind( &Semantic_actions_t::new_name, &self.actions_, _1, _2 ) ); | |
| 436 | + | Str_action new_str ( boost::bind( &Semantic_actions_t::new_str, &self.actions_, _1, _2 ) ); | |
| 437 | + | Str_action new_true ( boost::bind( &Semantic_actions_t::new_true, &self.actions_, _1, _2 ) ); | |
| 438 | + | Str_action new_false ( boost::bind( &Semantic_actions_t::new_false, &self.actions_, _1, _2 ) ); | |
| 439 | + | Str_action new_null ( boost::bind( &Semantic_actions_t::new_null, &self.actions_, _1, _2 ) ); | |
| 440 | + | Real_action new_real ( boost::bind( &Semantic_actions_t::new_real, &self.actions_, _1 ) ); | |
| 441 | + | Int_action new_int ( boost::bind( &Semantic_actions_t::new_int, &self.actions_, _1 ) ); | |
| 442 | + | Uint64_action new_uint64 ( boost::bind( &Semantic_actions_t::new_uint64, &self.actions_, _1 ) ); | |
| 443 | + | ||
| 444 | + | // actual grammer | |
| 445 | + | ||
| 446 | + | json_ | |
| 447 | + | = value_ | eps_p[ &throw_not_value ] | |
| 448 | + | ; | |
| 449 | + | ||
| 450 | + | value_ | |
| 451 | + | = string_[ new_str ] | |
| 452 | + | | number_ | |
| 453 | + | | object_ | |
| 454 | + | | array_ | |
| 455 | + | | str_p( "true" ) [ new_true ] | |
| 456 | + | | str_p( "false" )[ new_false ] | |
| 457 | + | | str_p( "null" ) [ new_null ] | |
| 458 | + | ; | |
| 459 | + | ||
| 460 | + | object_ | |
| 461 | + | = ch_p('{')[ begin_obj ] | |
| 462 | + | >> !members_ | |
| 463 | + | >> ( ch_p('}')[ end_obj ] | eps_p[ &throw_not_object ] ) | |
| 464 | + | ; | |
| 465 | + | ||
| 466 | + | members_ | |
| 467 | + | = pair_ >> *( ',' >> pair_ ) | |
| 468 | + | ; | |
| 469 | + | ||
| 470 | + | pair_ | |
| 471 | + | = string_[ new_name ] | |
| 472 | + | >> ( ':' | eps_p[ &throw_not_colon ] ) | |
| 473 | + | >> ( value_ | eps_p[ &throw_not_value ] ) | |
| 474 | + | ; | |
| 475 | + | ||
| 476 | + | array_ | |
| 477 | + | = ch_p('[')[ begin_array ] | |
| 478 | + | >> !elements_ | |
| 479 | + | >> ( ch_p(']')[ end_array ] | eps_p[ &throw_not_array ] ) | |
| 480 | + | ; | |
| 481 | + | ||
| 482 | + | elements_ | |
| 483 | + | = value_ >> *( ',' >> value_ ) | |
| 484 | + | ; | |
| 485 | + | ||
| 486 | + | string_ | |
| 487 | + | = lexeme_d // this causes white space inside a string to be retained | |
| 488 | + | [ | |
| 489 | + | confix_p | |
| 490 | + | ( | |
| 491 | + | '"', | |
| 492 | + | *lex_escape_ch_p, | |
| 493 | + | '"' | |
| 494 | + | ) | |
| 495 | + | ] | |
| 496 | + | ; | |
| 497 | + | ||
| 498 | + | number_ | |
| 499 | + | = strict_real_p[ new_real ] | |
| 500 | + | | int64_p [ new_int ] | |
| 501 | + | | uint64_p [ new_uint64 ] | |
| 502 | + | ; | |
| 503 | + | } | |
| 504 | + | ||
| 505 | + | spirit_namespace::rule< ScannerT > json_, object_, members_, pair_, array_, elements_, value_, string_, number_; | |
| 506 | + | ||
| 507 | + | const spirit_namespace::rule< ScannerT >& start() const { return json_; } | |
| 508 | + | }; | |
| 509 | + | ||
| 510 | + | private: | |
| 511 | + | ||
| 512 | + | Json_grammer& operator=( const Json_grammer& ); // to prevent "assignment operator could not be generated" warning | |
| 513 | + | ||
| 514 | + | Semantic_actions_t& actions_; | |
| 515 | + | }; | |
| 516 | + | ||
| 517 | + | template< class Iter_type, class Value_type > | |
| 518 | + | Iter_type read_range_or_throw( Iter_type begin, Iter_type end, Value_type& value ) | |
| 519 | + | { | |
| 520 | + | Semantic_actions< Value_type, Iter_type > semantic_actions( value ); | |
| 521 | + | ||
| 522 | + | const spirit_namespace::parse_info< Iter_type > info = | |
| 523 | + | spirit_namespace::parse( begin, end, | |
| 524 | + | Json_grammer< Value_type, Iter_type >( semantic_actions ), | |
| 525 | + | spirit_namespace::space_p ); | |
| 526 | + | ||
| 527 | + | if( !info.hit ) | |
| 528 | + | { | |
| 529 | + | assert( false ); // in theory exception should already have been thrown | |
| 530 | + | throw_error( info.stop, "error" ); | |
| 531 | + | } | |
| 532 | + | ||
| 533 | + | return info.stop; | |
| 534 | + | } | |
| 535 | + | ||
| 536 | + | template< class Iter_type, class Value_type > | |
| 537 | + | void add_posn_iter_and_read_range_or_throw( Iter_type begin, Iter_type end, Value_type& value ) | |
| 538 | + | { | |
| 539 | + | typedef spirit_namespace::position_iterator< Iter_type > Posn_iter_t; | |
| 540 | + | ||
| 541 | + | const Posn_iter_t posn_begin( begin, end ); | |
| 542 | + | const Posn_iter_t posn_end( end, end ); | |
| 543 | + | ||
| 544 | + | read_range_or_throw( posn_begin, posn_end, value ); | |
| 545 | + | } | |
| 546 | + | ||
| 547 | + | template< class Iter_type, class Value_type > | |
| 548 | + | bool read_range( Iter_type& begin, Iter_type end, Value_type& value ) | |
| 549 | + | { | |
| 550 | + | try | |
| 551 | + | { | |
| 552 | + | begin = read_range_or_throw( begin, end, value ); | |
| 553 | + | ||
| 554 | + | return true; | |
| 555 | + | } | |
| 556 | + | catch( ... ) | |
| 557 | + | { | |
| 558 | + | return false; | |
| 559 | + | } | |
| 560 | + | } | |
| 561 | + | ||
| 562 | + | template< class String_type, class Value_type > | |
| 563 | + | void read_string_or_throw( const String_type& s, Value_type& value ) | |
| 564 | + | { | |
| 565 | + | add_posn_iter_and_read_range_or_throw( s.begin(), s.end(), value ); | |
| 566 | + | } | |
| 567 | + | ||
| 568 | + | template< class String_type, class Value_type > | |
| 569 | + | bool read_string( const String_type& s, Value_type& value ) | |
| 570 | + | { | |
| 571 | + | typename String_type::const_iterator begin = s.begin(); | |
| 572 | + | ||
| 573 | + | return read_range( begin, s.end(), value ); | |
| 574 | + | } | |
| 575 | + | ||
| 576 | + | template< class Istream_type > | |
| 577 | + | struct Multi_pass_iters | |
| 578 | + | { | |
| 579 | + | typedef typename Istream_type::char_type Char_type; | |
| 580 | + | typedef std::istream_iterator< Char_type, Char_type > istream_iter; | |
| 581 | + | typedef spirit_namespace::multi_pass< istream_iter > Mp_iter; | |
| 582 | + | ||
| 583 | + | Multi_pass_iters( Istream_type& is ) | |
| 584 | + | { | |
| 585 | + | is.unsetf( std::ios::skipws ); | |
| 586 | + | ||
| 587 | + | begin_ = spirit_namespace::make_multi_pass( istream_iter( is ) ); | |
| 588 | + | end_ = spirit_namespace::make_multi_pass( istream_iter() ); | |
| 589 | + | } | |
| 590 | + | ||
| 591 | + | Mp_iter begin_; | |
| 592 | + | Mp_iter end_; | |
| 593 | + | }; | |
| 594 | + | ||
| 595 | + | template< class Istream_type, class Value_type > | |
| 596 | + | bool read_stream( Istream_type& is, Value_type& value ) | |
| 597 | + | { | |
| 598 | + | Multi_pass_iters< Istream_type > mp_iters( is ); | |
| 599 | + | ||
| 600 | + | return read_range( mp_iters.begin_, mp_iters.end_, value ); | |
| 601 | + | } | |
| 602 | + | ||
| 603 | + | template< class Istream_type, class Value_type > | |
| 604 | + | void read_stream_or_throw( Istream_type& is, Value_type& value ) | |
| 605 | + | { | |
| 606 | + | const Multi_pass_iters< Istream_type > mp_iters( is ); | |
| 607 | + | ||
| 608 | + | add_posn_iter_and_read_range_or_throw( mp_iters.begin_, mp_iters.end_, value ); | |
| 609 | + | } | |
| 610 | + | } | |
| 611 | + | ||
| 612 | + | #endif | |
| 613 | + |