This commit is contained in:
Ahmed Samy 2013-08-22 21:15:17 +02:00
parent aaad8ab8a0
commit d5298c1011
2 changed files with 71 additions and 63 deletions

View File

@ -40,7 +40,6 @@ enum SpawnAttr : uint8
{ {
SpawnAttrRadius = 0, SpawnAttrRadius = 0,
SpawnAttrCenter = 1, SpawnAttrCenter = 1,
SpawnAttrPos = 2
}; };
class Spawn : public LuaObject class Spawn : public LuaObject

View File

@ -134,88 +134,95 @@ bool ThingTypeManager::loadOtml(std::string file)
void ThingTypeManager::loadOtb(const 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(); uint signature = fin->getU32();
if(signature != 0) if(signature != 0)
stdext::throw_exception("invalid otb file"); stdext::throw_exception("invalid otb file");
BinaryTreePtr root = fin->getBinaryTree(); BinaryTreePtr root = fin->getBinaryTree();
signature = root->getU32(); signature = root->getU32();
if(signature != 0) if(signature != 0)
stdext::throw_exception("invalid otb file"); stdext::throw_exception("invalid otb file");
root->skip(4); root->skip(4);
m_otbMajorVersion = root->getU32(); m_otbMajorVersion = root->getU32();
m_otbMinorVersion = root->getU32(); m_otbMinorVersion = root->getU32();
root->skip(4); root->skip(4);
root->skip(128); // description root->skip(128); // description
m_reverseItemTypes.clear(); m_reverseItemTypes.clear();
m_itemTypes.resize(root->getChildren().size() + 1, m_nullItemType); m_itemTypes.resize(root->getChildren().size() + 1, m_nullItemType);
for(const BinaryTreePtr& node : root->getChildren()) { for(const BinaryTreePtr& node : root->getChildren()) {
ItemTypePtr itemType(new ItemType); ItemTypePtr itemType(new ItemType);
itemType->unserialize(node); itemType->unserialize(node);
addItemType(itemType); addItemType(itemType);
uint16 clientId = itemType->getClientId(); uint16 clientId = itemType->getClientId();
if(clientId >= m_reverseItemTypes.size()) if(clientId >= m_reverseItemTypes.size())
m_reverseItemTypes.resize(clientId+1); m_reverseItemTypes.resize(clientId+1);
m_reverseItemTypes[clientId] = itemType; 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) void ThingTypeManager::loadXml(const std::string& file)
{ {
if(!isOtbLoaded()) try {
stdext::throw_exception("OTB must be loaded before XML"); if(!isOtbLoaded())
stdext::throw_exception("OTB must be loaded before XML");
TiXmlDocument doc; TiXmlDocument doc;
doc.Parse(g_resources.readFileContents(file).c_str()); doc.Parse(g_resources.readFileContents(file).c_str());
if(doc.Error()) if(doc.Error())
stdext::throw_exception(stdext::format("failed to parse '%s': '%s'", file, doc.ErrorDesc())); stdext::throw_exception(stdext::format("failed to parse '%s': '%s'", file, doc.ErrorDesc()));
TiXmlElement* root = doc.FirstChildElement(); TiXmlElement* root = doc.FirstChildElement();
if(!root || root->ValueTStr() != "items") if(!root || root->ValueTStr() != "items")
stdext::throw_exception("invalid root tag name"); stdext::throw_exception("invalid root tag name");
for (TiXmlElement *element = root->FirstChildElement(); element; element = element->NextSiblingElement()) { for (TiXmlElement *element = root->FirstChildElement(); element; element = element->NextSiblingElement()) {
if(element->ValueTStr() != "item") if(element->ValueTStr() != "item")
continue; continue;
uint16 id = element->readType<uint16>("id"); uint16 id = element->readType<uint16>("id");
if(id != 0) { if(id != 0) {
std::vector<std::string> s_ids = stdext::split(element->Attribute("id"), ";"); std::vector<std::string> s_ids = stdext::split(element->Attribute("id"), ";");
for(const std::string& s : s_ids) { for(const std::string& s : s_ids) {
std::vector<int32> ids = stdext::split<int32>(s, "-"); std::vector<int32> ids = stdext::split<int32>(s, "-");
if(ids.size() > 1) { if(ids.size() > 1) {
int32 i = ids[0]; int32 i = ids[0];
while(i <= ids[1]) while(i <= ids[1])
parseItemType(i++, element); parseItemType(i++, element);
} else } else
parseItemType(atoi(s.c_str()), element); parseItemType(atoi(s.c_str()), element);
} }
} else { } else {
std::vector<int32> begin = stdext::split<int32>(element->Attribute("fromid"), ";"); std::vector<int32> begin = stdext::split<int32>(element->Attribute("fromid"), ";");
std::vector<int32> end = stdext::split<int32>(element->Attribute("toid"), ";"); std::vector<int32> end = stdext::split<int32>(element->Attribute("toid"), ";");
if(begin[0] && begin.size() == end.size()) { if(begin[0] && begin.size() == end.size()) {
size_t size = begin.size(); size_t size = begin.size();
for(size_t i = 0; i < size; ++i) { for(size_t i = 0; i < size; ++i)
while(begin[i] <= end[i]) while(begin[i] <= end[i])
parseItemType(begin[i]++, element); parseItemType(begin[i]++, element);
} }
} }
} }
}
doc.Clear(); doc.Clear();
m_xmlLoaded = true; m_xmlLoaded = true;
g_logger.debug("items.xml read successfully."); 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) 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) const ItemTypePtr& ThingTypeManager::getItemType(uint16 id)
{ {
if(id >= m_itemTypes.size() || m_itemTypes[id] == m_nullItemType) { 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_nullItemType;
} }
return m_itemTypes[id]; 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)); stdext::throw_exception(stdext::format("invalid thing type category %d", category));
return m_thingTypes[category]; return m_thingTypes[category];
} }
/* vim: set ts=4 sw=4 et: */