From d5298c1011abe2dd4ca30f5d308eeb1c62cc8124 Mon Sep 17 00:00:00 2001 From: Ahmed Samy Date: Thu, 22 Aug 2013 21:15:17 +0200 Subject: [PATCH] Cleanups --- src/client/creatures.h | 1 - src/client/thingtypemanager.cpp | 133 +++++++++++++++++--------------- 2 files changed, 71 insertions(+), 63 deletions(-) diff --git a/src/client/creatures.h b/src/client/creatures.h index ab019bbc..94d6504c 100644 --- a/src/client/creatures.h +++ b/src/client/creatures.h @@ -40,7 +40,6 @@ enum SpawnAttr : uint8 { SpawnAttrRadius = 0, SpawnAttrCenter = 1, - SpawnAttrPos = 2 }; class Spawn : public LuaObject diff --git a/src/client/thingtypemanager.cpp b/src/client/thingtypemanager.cpp index d9d5142a..9fb6b87d 100644 --- a/src/client/thingtypemanager.cpp +++ b/src/client/thingtypemanager.cpp @@ -134,88 +134,95 @@ bool ThingTypeManager::loadOtml(std::string file) void ThingTypeManager::loadOtb(const std::string& file) { - FileStreamPtr fin = g_resources.openFile(file); + try { + FileStreamPtr fin = g_resources.openFile(file); - uint signature = fin->getU32(); - if(signature != 0) - stdext::throw_exception("invalid otb file"); + uint signature = fin->getU32(); + if(signature != 0) + stdext::throw_exception("invalid otb file"); - BinaryTreePtr root = fin->getBinaryTree(); + BinaryTreePtr root = fin->getBinaryTree(); - signature = root->getU32(); - if(signature != 0) - stdext::throw_exception("invalid otb file"); + signature = root->getU32(); + if(signature != 0) + stdext::throw_exception("invalid otb file"); - root->skip(4); + root->skip(4); - m_otbMajorVersion = root->getU32(); - m_otbMinorVersion = root->getU32(); - root->skip(4); - root->skip(128); // description + m_otbMajorVersion = root->getU32(); + m_otbMinorVersion = root->getU32(); + root->skip(4); + root->skip(128); // description - m_reverseItemTypes.clear(); - m_itemTypes.resize(root->getChildren().size() + 1, m_nullItemType); + m_reverseItemTypes.clear(); + m_itemTypes.resize(root->getChildren().size() + 1, m_nullItemType); - for(const BinaryTreePtr& node : root->getChildren()) { - ItemTypePtr itemType(new ItemType); - itemType->unserialize(node); - addItemType(itemType); + for(const BinaryTreePtr& node : root->getChildren()) { + ItemTypePtr itemType(new ItemType); + itemType->unserialize(node); + addItemType(itemType); - uint16 clientId = itemType->getClientId(); - if(clientId >= m_reverseItemTypes.size()) - m_reverseItemTypes.resize(clientId+1); - m_reverseItemTypes[clientId] = itemType; + uint16 clientId = itemType->getClientId(); + if(clientId >= m_reverseItemTypes.size()) + m_reverseItemTypes.resize(clientId+1); + m_reverseItemTypes[clientId] = itemType; + } + + m_otbLoaded = true; + } catch(std::exception& e) { + g_logger.error(stdext::format("Failed to load '%s' (OTB file): %s", file, e.what());) } - - m_otbLoaded = true; } void ThingTypeManager::loadXml(const std::string& file) { - if(!isOtbLoaded()) - stdext::throw_exception("OTB must be loaded before XML"); + try { + if(!isOtbLoaded()) + stdext::throw_exception("OTB must be loaded before XML"); - TiXmlDocument doc; - doc.Parse(g_resources.readFileContents(file).c_str()); - if(doc.Error()) - stdext::throw_exception(stdext::format("failed to parse '%s': '%s'", file, doc.ErrorDesc())); + TiXmlDocument doc; + doc.Parse(g_resources.readFileContents(file).c_str()); + if(doc.Error()) + stdext::throw_exception(stdext::format("failed to parse '%s': '%s'", file, doc.ErrorDesc())); - TiXmlElement* root = doc.FirstChildElement(); - if(!root || root->ValueTStr() != "items") - stdext::throw_exception("invalid root tag name"); + TiXmlElement* root = doc.FirstChildElement(); + if(!root || root->ValueTStr() != "items") + stdext::throw_exception("invalid root tag name"); - for (TiXmlElement *element = root->FirstChildElement(); element; element = element->NextSiblingElement()) { - if(element->ValueTStr() != "item") - continue; + for (TiXmlElement *element = root->FirstChildElement(); element; element = element->NextSiblingElement()) { + if(element->ValueTStr() != "item") + continue; - uint16 id = element->readType("id"); - if(id != 0) { - std::vector s_ids = stdext::split(element->Attribute("id"), ";"); - for(const std::string& s : s_ids) { - std::vector ids = stdext::split(s, "-"); - if(ids.size() > 1) { - int32 i = ids[0]; - while(i <= ids[1]) - parseItemType(i++, element); - } else - parseItemType(atoi(s.c_str()), element); - } - } else { - std::vector begin = stdext::split(element->Attribute("fromid"), ";"); - std::vector end = stdext::split(element->Attribute("toid"), ";"); - if(begin[0] && begin.size() == end.size()) { - size_t size = begin.size(); - for(size_t i = 0; i < size; ++i) { - while(begin[i] <= end[i]) - parseItemType(begin[i]++, element); + uint16 id = element->readType("id"); + if(id != 0) { + std::vector s_ids = stdext::split(element->Attribute("id"), ";"); + for(const std::string& s : s_ids) { + std::vector ids = stdext::split(s, "-"); + if(ids.size() > 1) { + int32 i = ids[0]; + while(i <= ids[1]) + parseItemType(i++, element); + } else + parseItemType(atoi(s.c_str()), element); + } + } else { + std::vector begin = stdext::split(element->Attribute("fromid"), ";"); + std::vector end = stdext::split(element->Attribute("toid"), ";"); + if(begin[0] && begin.size() == end.size()) { + size_t size = begin.size(); + for(size_t i = 0; i < size; ++i) + while(begin[i] <= end[i]) + parseItemType(begin[i]++, element); } } } - } - doc.Clear(); - m_xmlLoaded = true; - g_logger.debug("items.xml read successfully."); + doc.Clear(); + m_xmlLoaded = true; + g_logger.debug("items.xml read successfully."); + } catch(std::exception& e) { + g_logger.error(stdext::format("Failed to load '%s' (XML file): %s", file, e.what())); + } } void ThingTypeManager::parseItemType(uint16 id, TiXmlElement* elem) @@ -303,7 +310,7 @@ const ThingTypePtr& ThingTypeManager::getThingType(uint16 id, ThingCategory cate const ItemTypePtr& ThingTypeManager::getItemType(uint16 id) { if(id >= m_itemTypes.size() || m_itemTypes[id] == m_nullItemType) { - g_logger.error(stdext::format("invalid thing type server id %d", id)); + g_logger.error(stdext::format("invalid thing type, server id: %d", id)); return m_nullItemType; } return m_itemTypes[id]; @@ -334,3 +341,5 @@ const ThingTypeList& ThingTypeManager::getThingTypes(ThingCategory category) stdext::throw_exception(stdext::format("invalid thing type category %d", category)); return m_thingTypes[category]; } + +/* vim: set ts=4 sw=4 et: */