From e9b41cd3c6daa440c015077b19514980606872f5 Mon Sep 17 00:00:00 2001 From: Ivan Savenko Date: Wed, 4 Jun 2014 14:15:58 +0300 Subject: [PATCH] Minor fixes, no more missing objects & templates --- lib/CArtHandler.cpp | 3 +++ lib/CCreatureHandler.cpp | 4 ++++ lib/CObjectClassesHandler.cpp | 23 +++++++++++++++++++++-- lib/CObjectClassesHandler.h | 1 + 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/lib/CArtHandler.cpp b/lib/CArtHandler.cpp index b5505e224..c10577279 100644 --- a/lib/CArtHandler.cpp +++ b/lib/CArtHandler.cpp @@ -660,6 +660,9 @@ void CArtHandler::afterLoadFinalization() // Necessary for objects added via mods that don't have any templates in H3 VLC->objtypeh->getHandlerFor(Obj::ARTIFACT, art->id)->addTemplate(templ); } + // object does not have any templates - this is not usable object (e.g. pseudo-art like lock) + if (VLC->objtypeh->getHandlerFor(Obj::ARTIFACT, art->id)->getTemplates().empty()) + VLC->objtypeh->eraseObject(Obj::ARTIFACT, art->id); } } diff --git a/lib/CCreatureHandler.cpp b/lib/CCreatureHandler.cpp index 610c78258..a3f7ae46d 100644 --- a/lib/CCreatureHandler.cpp +++ b/lib/CCreatureHandler.cpp @@ -1123,6 +1123,10 @@ void CCreatureHandler::afterLoadFinalization() templ["animation"].String() = crea->advMapDef; VLC->objtypeh->getHandlerFor(Obj::MONSTER, crea->idNumber)->addTemplate(templ); } + + // object does not have any templates - this is not usable object (e.g. pseudo-creature like Arrow Tower) + if (VLC->objtypeh->getHandlerFor(Obj::MONSTER, crea->idNumber.num)->getTemplates().empty()) + VLC->objtypeh->eraseObject(Obj::MONSTER, crea->idNumber.num); } } diff --git a/lib/CObjectClassesHandler.cpp b/lib/CObjectClassesHandler.cpp index 4126c8ceb..429463766 100644 --- a/lib/CObjectClassesHandler.cpp +++ b/lib/CObjectClassesHandler.cpp @@ -391,7 +391,7 @@ CObjectClassesHandler::CObjectClassesHandler() } template -void readTextFile(Container objects, std::string path) +void readTextFile(Container & objects, std::string path) { CLegacyConfigParser parser(path); size_t totalNumber = parser.readNumber(); // first line contains number of objects to read and nothing else @@ -449,10 +449,14 @@ void CObjectClassesHandler::loadObjectEntry(const JsonNode & entry, ObjectContai if (handler->getTemplates().empty()) { - auto range = legacyTemplates.equal_range(std::make_pair(obj->id, si32(entry["index"].Float()))); + auto range = legacyTemplates.equal_range(std::make_pair(obj->id, id)); for (auto & templ : boost::make_iterator_range(range.first, range.second)) + { handler->addTemplate(templ.second); + } + legacyTemplates.erase(range.first, range.second); } + obj->objects[id] = handler; } @@ -500,6 +504,13 @@ void CObjectClassesHandler::createObject(std::string name, JsonNode config, si32 loadObjectEntry(config, objects[ID]); } +void CObjectClassesHandler::eraseObject(si32 ID, si32 subID) +{ + assert(objects.count(ID)); + assert(objects.at(ID)->objects.count(subID)); + objects.at(ID)->objects.erase(subID); +} + std::vector CObjectClassesHandler::getDefaultAllowed() const { return std::vector(); //TODO? @@ -532,6 +543,14 @@ void CObjectClassesHandler::beforeValidate(JsonNode & object) void CObjectClassesHandler::afterLoadFinalization() { legacyTemplates.clear(); // whatever left there is no longer needed + for (auto entry : objects) + { + for (auto obj : entry.second->objects) + { + if (obj.second->getTemplates().empty()) + logGlobal->warnStream() << "No templates found for " << entry.first << ":" << obj.first; + } + } } std::string CObjectClassesHandler::getObjectName(si32 type) const diff --git a/lib/CObjectClassesHandler.h b/lib/CObjectClassesHandler.h index 57ecd07c7..b28f95e8a 100644 --- a/lib/CObjectClassesHandler.h +++ b/lib/CObjectClassesHandler.h @@ -214,6 +214,7 @@ public: void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override; void createObject(std::string name, JsonNode config, si32 ID, boost::optional subID = boost::optional()); + void eraseObject(si32 ID, si32 subID); void beforeValidate(JsonNode & object) override; void afterLoadFinalization() override;