More cleanups

This commit is contained in:
Ahmed Samy 2013-08-23 02:12:36 +02:00
parent b27352c321
commit 644d4daeea
2 changed files with 75 additions and 58 deletions

View File

@ -223,47 +223,55 @@ void CreatureManager::loadSpawns(const std::string& fileName)
return; return;
} }
TiXmlDocument doc; try {
doc.Parse(g_resources.readFileContents(fileName).c_str()); TiXmlDocument doc;
if(doc.Error()) doc.Parse(g_resources.readFileContents(fileName).c_str());
stdext::throw_exception(stdext::format("cannot load spawns xml file '%s: '%s'", fileName, doc.ErrorDesc())); if(doc.Error())
stdext::throw_exception(stdext::format("cannot load spawns xml file '%s: '%s'", fileName, doc.ErrorDesc()));
TiXmlElement* root = doc.FirstChildElement(); TiXmlElement* root = doc.FirstChildElement();
if(!root || root->ValueStr() != "spawns") if(!root || root->ValueStr() != "spawns")
stdext::throw_exception("malformed spawns file"); stdext::throw_exception("malformed spawns file");
for(TiXmlElement* node = root->FirstChildElement(); node; node = node->NextSiblingElement()) { for(TiXmlElement* node = root->FirstChildElement(); node; node = node->NextSiblingElement()) {
if(node->ValueTStr() != "spawn") if(node->ValueTStr() != "spawn")
stdext::throw_exception("invalid spawn node"); stdext::throw_exception("invalid spawn node");
SpawnPtr spawn(new Spawn); SpawnPtr spawn(new Spawn);
spawn->load(node); spawn->load(node);
m_spawns.insert(std::make_pair(spawn->getCenterPos(), spawn)); m_spawns.insert(std::make_pair(spawn->getCenterPos(), spawn));
}
doc.Clear();
m_spawnLoaded = true;
} catch(std::exception& e) {
g_logger.error(stdext::format("Failed to load '%s': %s", fileName, e.what()));
} }
doc.Clear();
m_spawnLoaded = true;
} }
void CreatureManager::saveSpawns(const std::string& fileName) void CreatureManager::saveSpawns(const std::string& fileName)
{ {
TiXmlDocument doc; try {
doc.SetTabSize(2); TiXmlDocument doc;
doc.SetTabSize(2);
TiXmlDeclaration* decl = new TiXmlDeclaration("1.0", "UTF-8", ""); TiXmlDeclaration* decl = new TiXmlDeclaration("1.0", "UTF-8", "");
doc.LinkEndChild(decl); doc.LinkEndChild(decl);
TiXmlElement* root = new TiXmlElement("spawns"); TiXmlElement* root = new TiXmlElement("spawns");
doc.LinkEndChild(root); doc.LinkEndChild(root);
for(auto pair : m_spawns) { for(auto pair : m_spawns) {
TiXmlElement* elem = new TiXmlElement("spawn"); TiXmlElement* elem = new TiXmlElement("spawn");
pair.second->save(elem); pair.second->save(elem);
root->LinkEndChild(elem); root->LinkEndChild(elem);
}
std::string savePath = g_resources.getRealPath(fileName);
if(!doc.SaveFile(savePath))
stdext::throw_exception(stdext::format("failed to save spawns XML %s: %s", savePath, doc.ErrorDesc()));
} catch(std::exception& e) {
g_logger.error(stdext::format("Failed to save '%s': %s", fileName, e.what()));
} }
std::string savePath = g_resources.getRealPath(fileName);
if(!doc.SaveFile(savePath))
stdext::throw_exception(stdext::format("failed to save spawns XML %s: %s", savePath, doc.ErrorDesc()));
} }
void CreatureManager::loadCreatureBuffer(const std::string& buffer) void CreatureManager::loadCreatureBuffer(const std::string& buffer)

View File

@ -134,48 +134,56 @@ HousePtr HouseManager::getHouseByName(std::string name)
void HouseManager::load(const std::string& fileName) void HouseManager::load(const std::string& fileName)
{ {
TiXmlDocument doc; try {
doc.Parse(g_resources.readFileContents(fileName).c_str()); TiXmlDocument doc;
if(doc.Error()) doc.Parse(g_resources.readFileContents(fileName).c_str());
stdext::throw_exception(stdext::format("failed to load '%s': %s (House XML)", fileName, doc.ErrorDesc())); if(doc.Error())
stdext::throw_exception(stdext::format("failed to load '%s': %s (House XML)", fileName, doc.ErrorDesc()));
TiXmlElement *root = doc.FirstChildElement(); TiXmlElement *root = doc.FirstChildElement();
if(!root || root->ValueTStr() != "houses") if(!root || root->ValueTStr() != "houses")
stdext::throw_exception("invalid root tag name"); stdext::throw_exception("invalid root tag name");
for(TiXmlElement *elem = root->FirstChildElement(); elem; elem = elem->NextSiblingElement()) { for(TiXmlElement *elem = root->FirstChildElement(); elem; elem = elem->NextSiblingElement()) {
if(elem->ValueTStr() != "house") if(elem->ValueTStr() != "house")
stdext::throw_exception("invalid house tag."); stdext::throw_exception("invalid house tag.");
uint32 houseId = elem->readType<uint32>("houseid"); uint32 houseId = elem->readType<uint32>("houseid");
HousePtr house = getHouse(houseId); HousePtr house = getHouse(houseId);
if(!house) if(!house)
house = HousePtr(new House(houseId)), addHouse(house); house = HousePtr(new House(houseId)), addHouse(house);
house->load(elem); house->load(elem);
}
} catch(std::exception& e) {
g_logger.error(stdext::format("Failed to load '%s': %s", fileName, e.what()));
} }
} }
void HouseManager::save(const std::string& fileName) void HouseManager::save(const std::string& fileName)
{ {
TiXmlDocument doc; try {
doc.SetTabSize(2); TiXmlDocument doc;
doc.SetTabSize(2);
TiXmlDeclaration* decl = new TiXmlDeclaration("1.0", "UTF-8", ""); TiXmlDeclaration* decl = new TiXmlDeclaration("1.0", "UTF-8", "");
doc.LinkEndChild(decl); doc.LinkEndChild(decl);
TiXmlElement* root = new TiXmlElement("houses"); TiXmlElement* root = new TiXmlElement("houses");
doc.LinkEndChild(root); doc.LinkEndChild(root);
for(auto house : m_houses) { for(auto house : m_houses) {
TiXmlElement *elem = new TiXmlElement("house"); TiXmlElement *elem = new TiXmlElement("house");
house->save(elem); house->save(elem);
root->LinkEndChild(elem); root->LinkEndChild(elem);
}
std::string savePath = g_resources.getRealPath(fileName);
if(!doc.SaveFile(savePath))
stdext::throw_exception(stdext::format("failed to save houses XML %s: %s", savePath, doc.ErrorDesc()));
} catch(std::exception& e) {
g_logger.error(stdext::format("Failed to save '%s': %s", fileName, e.what()));
} }
std::string savePath = g_resources.getRealPath(fileName);
if(!doc.SaveFile(savePath))
stdext::throw_exception(stdext::format("failed to save houses XML %s: %s", savePath, doc.ErrorDesc()));
} }
HouseList HouseManager::filterHouses(uint32 townId) HouseList HouseManager::filterHouses(uint32 townId)
@ -193,3 +201,4 @@ HouseList::iterator HouseManager::findHouse(uint32 houseId)
[=] (const HousePtr& house) -> bool { return house->getId() == houseId; }); [=] (const HousePtr& house) -> bool { return house->getId() == houseId; });
} }
/* vim: set ts=4 sw=4 et: */