bool ItemAttributes::unserializeMap(PropStream& stream) { uint16_t n; if(!stream.getShort(n)) return true; createAttributes(); while(n--) { std::string key; if(!stream.getString(key)) return false; ItemAttribute attr; if(!attr.unserialize(stream)) return false; (*attributes)[key] = attr; } return true; }
bool IOMap::loadMap(Map* map, const std::string& identifier) { FileLoader f; if(!f.openFile(identifier.c_str(), false, true)) { std::stringstream ss; ss << "Could not open the file " << identifier << "."; setLastErrorString(ss.str()); return false; } uint32_t type = 0; NODE root = f.getChildNode((NODE)NULL, type); PropStream propStream; if(!f.getProps(root, propStream)) { setLastErrorString("Could not read root property."); return false; } OTBM_root_header* rootHeader; if(!propStream.getStruct(rootHeader)) { setLastErrorString("Could not read header."); return false; } uint32_t headerVersion = rootHeader->version; if(headerVersion <= 0) { //In otbm version 1 the count variable after splashes/fluidcontainers and stackables //are saved as attributes instead, this solves alot of problems with items //that is changed (stackable/charges/fluidcontainer/splash) during an update. setLastErrorString("This map needs to be upgraded by using the latest map editor version to be able to load correctly."); return false; } if(headerVersion > 3) { setLastErrorString("Unknown OTBM version detected."); return false; } uint32_t headerMajorItems = rootHeader->majorVersionItems; if(headerMajorItems < 3) { setLastErrorString("This map needs to be upgraded by using the latest map editor version to be able to load correctly."); return false; } if(headerMajorItems > (uint32_t)Items::dwMajorVersion) { setLastErrorString("The map was saved with a different items.otb version, an upgraded items.otb is required."); return false; } uint32_t headerMinorItems = rootHeader->minorVersionItems; if(headerMinorItems < CLIENT_VERSION_810) { setLastErrorString("This map needs an updated items.otb."); return false; } if(headerMinorItems > (uint32_t)Items::dwMinorVersion) setLastErrorString("This map needs an updated items.otb."); std::clog << "> Map size: " << rootHeader->width << "x" << rootHeader->height << "." << std::endl; map->mapWidth = rootHeader->width; map->mapHeight = rootHeader->height; NODE nodeMap = f.getChildNode(root, type); if(type != OTBM_MAP_DATA) { setLastErrorString("Could not read data node."); return false; } if(!f.getProps(nodeMap, propStream)) { setLastErrorString("Could not read map data attributes."); return false; } std::string tmp; uint8_t attribute; while(propStream.getByte(attribute)) { switch(attribute) { case OTBM_ATTR_DESCRIPTION: { if(!propStream.getString(tmp)) { setLastErrorString("Invalid description tag."); return false; } map->descriptions.push_back(tmp); break; } case OTBM_ATTR_EXT_SPAWN_FILE: { if(!propStream.getString(tmp)) { setLastErrorString("Invalid spawnfile tag."); return false; } map->spawnfile = identifier.substr(0, identifier.rfind('/') + 1); map->spawnfile += tmp; break; } case OTBM_ATTR_EXT_HOUSE_FILE: { if(!propStream.getString(tmp)) { setLastErrorString("Invalid housefile tag."); return false; } map->housefile = identifier.substr(0, identifier.rfind('/') + 1); map->housefile += tmp; break; } default: { setLastErrorString("Unknown header node."); return false; } } } std::clog << "> Map descriptions: " << std::endl; for(StringVec::iterator it = map->descriptions.begin(); it != map->descriptions.end(); ++it) std::clog << "\"" << (*it) << "\"" << std::endl; NODE nodeMapData = f.getChildNode(nodeMap, type); while(nodeMapData != NO_NODE) { if(f.getError() != ERROR_NONE) { setLastErrorString("Invalid map node."); return false; } if(type == OTBM_TILE_AREA) { if(!f.getProps(nodeMapData, propStream)) { setLastErrorString("Invalid map node."); return false; } OTBM_Destination_coords* area_coord; if(!propStream.getStruct(area_coord)) { setLastErrorString("Invalid map node."); return false; } int32_t base_x = area_coord->_x, base_y = area_coord->_y, base_z = area_coord->_z; NODE nodeTile = f.getChildNode(nodeMapData, type); while(nodeTile != NO_NODE) { if(f.getError() != ERROR_NONE) { setLastErrorString("Could not read node data."); return false; } if(type == OTBM_TILE || type == OTBM_HOUSETILE) { if(!f.getProps(nodeTile, propStream)) { setLastErrorString("Could not read node data."); return false; } OTBM_Tile_coords* tileCoord; if(!propStream.getStruct(tileCoord)) { setLastErrorString("Could not read tile position."); return false; } Tile* tile = NULL; Item* ground = NULL; uint32_t tileflags = 0; uint16_t px = base_x + tileCoord->_x, py = base_y + tileCoord->_y, pz = base_z; House* house = NULL; if(type == OTBM_HOUSETILE) { uint32_t _houseid; if(!propStream.getLong(_houseid)) { std::stringstream ss; ss << "[x:" << px << ", y:" << py << ", z:" << pz << "] Could not read house id."; setLastErrorString(ss.str()); return false; } house = Houses::getInstance()->getHouse(_houseid, true); if(!house) { std::stringstream ss; ss << "[x:" << px << ", y:" << py << ", z:" << pz << "] Could not create house id: " << _houseid; setLastErrorString(ss.str()); return false; } tile = new HouseTile(px, py, pz, house); house->addTile(static_cast<HouseTile*>(tile)); } //read tile attributes uint8_t attribute; while(propStream.getByte(attribute)) { switch(attribute) { case OTBM_ATTR_TILE_FLAGS: { uint32_t flags; if(!propStream.getLong(flags)) { std::stringstream ss; ss << "[x:" << px << ", y:" << py << ", z:" << pz << "] Failed to read tile flags."; setLastErrorString(ss.str()); return false; } if((flags & TILESTATE_PROTECTIONZONE) == TILESTATE_PROTECTIONZONE) tileflags |= TILESTATE_PROTECTIONZONE; else if((flags & TILESTATE_OPTIONALZONE) == TILESTATE_OPTIONALZONE) tileflags |= TILESTATE_OPTIONALZONE; else if((flags & TILESTATE_HARDCOREZONE) == TILESTATE_HARDCOREZONE) tileflags |= TILESTATE_HARDCOREZONE; if((flags & TILESTATE_NOLOGOUT) == TILESTATE_NOLOGOUT) tileflags |= TILESTATE_NOLOGOUT; if((flags & TILESTATE_REFRESH) == TILESTATE_REFRESH) { if(house) std::clog << "[x:" << px << ", y:" << py << ", z:" << pz << "] House tile flagged as refreshing!"; tileflags |= TILESTATE_REFRESH; } break; } case OTBM_ATTR_ITEM: { Item* item = Item::CreateItem(propStream); if(!item) { std::stringstream ss; ss << "[x:" << px << ", y:" << py << ", z:" << pz << "] Failed to create item."; setLastErrorString(ss.str()); return false; } if(house && item->isMoveable()) { std::clog << "[Warning - IOMap::loadMap] Movable item in house: " << house->getId(); std::clog << ", item type: " << item->getID() << ", at position " << px << "/" << py << "/"; std::clog << pz << std::endl; delete item; item = NULL; } else if(tile) { tile->__internalAddThing(item); item->__startDecaying(); item->setLoadedFromMap(true); } else if(item->isGroundTile()) { if(ground) delete ground; ground = item; } else { tile = createTile(ground, item, px, py, pz); tile->__internalAddThing(item); item->__startDecaying(); item->setLoadedFromMap(true); } break; } default: { std::stringstream ss; ss << "[x:" << px << ", y:" << py << ", z:" << pz << "] Unknown tile attribute."; setLastErrorString(ss.str()); return false; } } } NODE nodeItem = f.getChildNode(nodeTile, type); while(nodeItem) { if(type == OTBM_ITEM) { PropStream propStream; f.getProps(nodeItem, propStream); Item* item = Item::CreateItem(propStream); if(!item) { std::stringstream ss; ss << "[x:" << px << ", y:" << py << ", z:" << pz << "] Failed to create item."; setLastErrorString(ss.str()); return false; } if(item->unserializeItemNode(f, nodeItem, propStream)) { if(house && item->isMoveable()) { std::clog << "[Warning - IOMap::loadMap] Movable item in house: "; std::clog << house->getId() << ", item type: " << item->getID(); std::clog << ", pos " << px << "/" << py << "/" << pz << std::endl; delete item; item = NULL; } else if(tile) { tile->__internalAddThing(item); item->__startDecaying(); item->setLoadedFromMap(true); } else if(item->isGroundTile()) { if(ground) delete ground; ground = item; } else { tile = createTile(ground, item, px, py, pz); tile->__internalAddThing(item); item->__startDecaying(); item->setLoadedFromMap(true); } } else { std::stringstream ss; ss << "[x:" << px << ", y:" << py << ", z:" << pz << "] Failed to load item " << item->getID() << "."; setLastErrorString(ss.str()); delete item; item = NULL; return false; } } else { std::stringstream ss; ss << "[x:" << px << ", y:" << py << ", z:" << pz << "] Unknown node type."; setLastErrorString(ss.str()); } nodeItem = f.getNextNode(nodeItem, type); } if(!tile) tile = createTile(ground, NULL, px, py, pz); tile->setFlag((tileflags_t)tileflags); map->setTile(px, py, pz, tile); } else { setLastErrorString("Unknown tile node."); return false; } nodeTile = f.getNextNode(nodeTile, type); } } else if(type == OTBM_TOWNS) { NODE nodeTown = f.getChildNode(nodeMapData, type); while(nodeTown != NO_NODE) { if(type == OTBM_TOWN) { if(!f.getProps(nodeTown, propStream)) { setLastErrorString("Could not read town data."); return false; } uint32_t townId = 0; if(!propStream.getLong(townId)) { setLastErrorString("Could not read town id."); return false; } Town* town = Towns::getInstance()->getTown(townId); if(!town) { town = new Town(townId); Towns::getInstance()->addTown(townId, town); } std::string townName; if(!propStream.getString(townName)) { setLastErrorString("Could not read town name."); return false; } town->setName(townName); OTBM_Destination_coords *townCoords; if(!propStream.getStruct(townCoords)) { setLastErrorString("Could not read town coordinates."); return false; } town->setPosition(Position(townCoords->_x, townCoords->_y, townCoords->_z)); } else { setLastErrorString("Unknown town node."); return false; } nodeTown = f.getNextNode(nodeTown, type); } } else if(type == OTBM_WAYPOINTS && headerVersion > 1) { NODE nodeWaypoint = f.getChildNode(nodeMapData, type); while(nodeWaypoint != NO_NODE) { if(type == OTBM_WAYPOINT) { if(!f.getProps(nodeWaypoint, propStream)) { setLastErrorString("Could not read waypoint data."); return false; } std::string name; if(!propStream.getString(name)) { setLastErrorString("Could not read waypoint name."); return false; } OTBM_Destination_coords* waypoint_coords; if(!propStream.getStruct(waypoint_coords)) { setLastErrorString("Could not read waypoint coordinates."); return false; } map->waypoints.addWaypoint(WaypointPtr(new Waypoint(name, Position(waypoint_coords->_x, waypoint_coords->_y, waypoint_coords->_z)))); } else { setLastErrorString("Unknown waypoint node."); return false; } nodeWaypoint = f.getNextNode(nodeWaypoint, type); } } else { setLastErrorString("Unknown map node."); return false; } nodeMapData = f.getNextNode(nodeMapData, type); } return true; }
Attr_ReadValue Item::readAttr(AttrTypes_t attr, PropStream& propStream) { switch(attr) { case ATTR_COUNT: { uint8_t _count; if(!propStream.getByte(_count)) return ATTR_READ_ERROR; setSubType((uint16_t)_count); break; } case ATTR_ACTION_ID: { uint16_t aid; if(!propStream.getShort(aid)) return ATTR_READ_ERROR; setAttribute("aid", aid); break; } case ATTR_UNIQUE_ID: { uint16_t uid; if(!propStream.getShort(uid)) return ATTR_READ_ERROR; setUniqueId(uid); break; } case ATTR_NAME: { std::string name; if(!propStream.getString(name)) return ATTR_READ_ERROR; setAttribute("name", name); break; } case ATTR_PLURALNAME: { std::string name; if(!propStream.getString(name)) return ATTR_READ_ERROR; setAttribute("pluralname", name); break; } case ATTR_ARTICLE: { std::string article; if(!propStream.getString(article)) return ATTR_READ_ERROR; setAttribute("article", article); break; } case ATTR_ATTACK: { int32_t attack; if(!propStream.getLong((uint32_t&)attack)) return ATTR_READ_ERROR; setAttribute("attack", attack); break; } case ATTR_EXTRAATTACK: { int32_t attack; if(!propStream.getLong((uint32_t&)attack)) return ATTR_READ_ERROR; setAttribute("extraattack", attack); break; } case ATTR_DEFENSE: { int32_t defense; if(!propStream.getLong((uint32_t&)defense)) return ATTR_READ_ERROR; setAttribute("defense", defense); break; } case ATTR_EXTRADEFENSE: { int32_t defense; if(!propStream.getLong((uint32_t&)defense)) return ATTR_READ_ERROR; setAttribute("extradefense", defense); break; } case ATTR_ARMOR: { int32_t armor; if(!propStream.getLong((uint32_t&)armor)) return ATTR_READ_ERROR; setAttribute("armor", armor); break; } case ATTR_ATTACKSPEED: { int32_t attackSpeed; if(!propStream.getLong((uint32_t&)attackSpeed)) return ATTR_READ_ERROR; setAttribute("attackspeed", attackSpeed); break; } case ATTR_HITCHANCE: { int32_t hitChance; if(!propStream.getLong((uint32_t&)hitChance)) return ATTR_READ_ERROR; setAttribute("hitchance", hitChance); break; } case ATTR_SCRIPTPROTECTED: { uint8_t protection; if(!propStream.getByte(protection)) return ATTR_READ_ERROR; setAttribute("scriptprotected", protection != 0); break; } case ATTR_DUALWIELD: { uint8_t wield; if(!propStream.getByte(wield)) return ATTR_READ_ERROR; setAttribute("dualwield", wield != 0); break; } case ATTR_TEXT: { std::string text; if(!propStream.getString(text)) return ATTR_READ_ERROR; setAttribute("text", text); break; } case ATTR_WRITTENDATE: { int32_t date; if(!propStream.getLong((uint32_t&)date)) return ATTR_READ_ERROR; setAttribute("date", date); break; } case ATTR_WRITTENBY: { std::string writer; if(!propStream.getString(writer)) return ATTR_READ_ERROR; setAttribute("writer", writer); break; } case ATTR_DESC: { std::string text; if(!propStream.getString(text)) return ATTR_READ_ERROR; setAttribute("description", text); break; } case ATTR_RUNE_CHARGES: { uint8_t charges; if(!propStream.getByte(charges)) return ATTR_READ_ERROR; setSubType((uint16_t)charges); break; } case ATTR_CHARGES: { uint16_t charges; if(!propStream.getShort(charges)) return ATTR_READ_ERROR; setSubType(charges); break; } case ATTR_DURATION: { int32_t duration; if(!propStream.getLong((uint32_t&)duration)) return ATTR_READ_ERROR; setAttribute("duration", duration); break; } case ATTR_DECAYING_STATE: { uint8_t state; if(!propStream.getByte(state)) return ATTR_READ_ERROR; if((ItemDecayState_t)state != DECAYING_FALSE) setAttribute("decaying", (int32_t)DECAYING_PENDING); break; } //these should be handled through derived classes //if these are called then something has changed in the items.otb since the map was saved //just read the values //Depot class case ATTR_DEPOT_ID: { uint16_t depot; if(!propStream.getShort(depot)) return ATTR_READ_ERROR; break; } //Door class case ATTR_HOUSEDOORID: { uint8_t door; if(!propStream.getByte(door)) return ATTR_READ_ERROR; break; } //Teleport class case ATTR_TELE_DEST: { TeleportDest* dest; if(!propStream.getStruct(dest)) return ATTR_READ_ERROR; break; } //Bed class case ATTR_SLEEPERGUID: { uint32_t sleeper; if(!propStream.getLong(sleeper)) return ATTR_READ_ERROR; break; } case ATTR_SLEEPSTART: { uint32_t sleepStart; if(!propStream.getLong(sleepStart)) return ATTR_READ_ERROR; break; } //Container class case ATTR_CONTAINER_ITEMS: { uint32_t _count; propStream.getLong(_count); return ATTR_READ_ERROR; } //ItemAttributes class case ATTR_ATTRIBUTE_MAP: { bool unique = hasIntegerAttribute("uid"), ret = unserializeMap(propStream); if(!unique && hasIntegerAttribute("uid")) // unfortunately we have to do this ScriptEnviroment::addUniqueThing(this); // this attribute has a custom behavior as well if(getDecaying() != DECAYING_FALSE) setDecaying(DECAYING_PENDING); if(ret) break; } default: return ATTR_READ_ERROR; } return ATTR_READ_CONTINUE; }