From e6def84135ca75f14e8c894abfb6a9472b244787 Mon Sep 17 00:00:00 2001 From: Laserlicht <13953785+Laserlicht@users.noreply.github.com> Date: Sun, 28 Sep 2025 20:34:48 +0200 Subject: [PATCH] revert handlers --- lib/ResourceSet.cpp | 2 +- lib/entities/ResourceTypeHandler.cpp | 3 +- lib/modding/ContentTypeHandler.cpp | 51 ++++++++++++---------------- lib/modding/ContentTypeHandler.h | 2 +- 4 files changed, 25 insertions(+), 33 deletions(-) diff --git a/lib/ResourceSet.cpp b/lib/ResourceSet.cpp index 99fecc29a..e60f3faef 100644 --- a/lib/ResourceSet.cpp +++ b/lib/ResourceSet.cpp @@ -31,7 +31,7 @@ ResourceSet::ResourceSet(const ResourceSet& rhs) void ResourceSet::resizeContainer() { - container.resize(LIBRARY->resourceTypeHandler->getAllObjects().size()); + container.resize(std::max(static_cast(LIBRARY->resourceTypeHandler->getAllObjects().size()), GameConstants::RESOURCE_QUANTITY)); } void ResourceSet::resolveFromJson(const JsonNode & node) diff --git a/lib/entities/ResourceTypeHandler.cpp b/lib/entities/ResourceTypeHandler.cpp index a22654c89..ffea20fee 100644 --- a/lib/entities/ResourceTypeHandler.cpp +++ b/lib/entities/ResourceTypeHandler.cpp @@ -74,7 +74,8 @@ std::vector ResourceTypeHandler::getAllObjects() const std::vector result; for (const auto & resource : objects) - result.push_back(resource->getId()); + if(resource) + result.push_back(resource->getId()); return result; } diff --git a/lib/modding/ContentTypeHandler.cpp b/lib/modding/ContentTypeHandler.cpp index 9bdc6d5a4..6d7e1c220 100644 --- a/lib/modding/ContentTypeHandler.cpp +++ b/lib/modding/ContentTypeHandler.cpp @@ -243,29 +243,28 @@ void ContentTypeHandler::afterLoadFinalization() void CContentHandler::init() { - handlers.push_back(std::make_pair("resources", ContentTypeHandler(LIBRARY->resourceTypeHandler.get(), "resources"))); // needs to loaded first because needed by other handlers - - handlers.push_back(std::make_pair("heroClasses", ContentTypeHandler(LIBRARY->heroclassesh.get(), "heroClass"))); - handlers.push_back(std::make_pair("artifacts", ContentTypeHandler(LIBRARY->arth.get(), "artifact"))); - handlers.push_back(std::make_pair("bonuses", ContentTypeHandler(LIBRARY->bth.get(), "bonus"))); - handlers.push_back(std::make_pair("creatures", ContentTypeHandler(LIBRARY->creh.get(), "creature"))); - handlers.push_back(std::make_pair("campaignRegions", ContentTypeHandler(LIBRARY->campaignRegions.get(), "campaignRegion"))); - handlers.push_back(std::make_pair("factions", ContentTypeHandler(LIBRARY->townh.get(), "faction"))); - handlers.push_back(std::make_pair("objects", ContentTypeHandler(LIBRARY->objtypeh.get(), "object"))); - handlers.push_back(std::make_pair("heroes", ContentTypeHandler(LIBRARY->heroh.get(), "hero"))); - handlers.push_back(std::make_pair("spells", ContentTypeHandler(LIBRARY->spellh.get(), "spell"))); - handlers.push_back(std::make_pair("spellSchools", ContentTypeHandler(LIBRARY->spellSchoolHandler.get(), "spellSchool"))); - handlers.push_back(std::make_pair("skills", ContentTypeHandler(LIBRARY->skillh.get(), "skill"))); - handlers.push_back(std::make_pair("templates", ContentTypeHandler(LIBRARY->tplh.get(), "template"))); + handlers.insert(std::make_pair("heroClasses", ContentTypeHandler(LIBRARY->heroclassesh.get(), "heroClass"))); + handlers.insert(std::make_pair("artifacts", ContentTypeHandler(LIBRARY->arth.get(), "artifact"))); + handlers.insert(std::make_pair("bonuses", ContentTypeHandler(LIBRARY->bth.get(), "bonus"))); + handlers.insert(std::make_pair("creatures", ContentTypeHandler(LIBRARY->creh.get(), "creature"))); + handlers.insert(std::make_pair("campaignRegions", ContentTypeHandler(LIBRARY->campaignRegions.get(), "campaignRegion"))); + handlers.insert(std::make_pair("factions", ContentTypeHandler(LIBRARY->townh.get(), "faction"))); + handlers.insert(std::make_pair("objects", ContentTypeHandler(LIBRARY->objtypeh.get(), "object"))); + handlers.insert(std::make_pair("heroes", ContentTypeHandler(LIBRARY->heroh.get(), "hero"))); + handlers.insert(std::make_pair("spells", ContentTypeHandler(LIBRARY->spellh.get(), "spell"))); + handlers.insert(std::make_pair("spellSchools", ContentTypeHandler(LIBRARY->spellSchoolHandler.get(), "spellSchool"))); + handlers.insert(std::make_pair("skills", ContentTypeHandler(LIBRARY->skillh.get(), "skill"))); + handlers.insert(std::make_pair("templates", ContentTypeHandler(LIBRARY->tplh.get(), "template"))); #if SCRIPTING_ENABLED - handlers.push_back(std::make_pair("scripts", ContentTypeHandler(LIBRARY->scriptHandler.get(), "script"))); + handlers.insert(std::make_pair("scripts", ContentTypeHandler(LIBRARY->scriptHandler.get(), "script"))); #endif - handlers.push_back(std::make_pair("battlefields", ContentTypeHandler(LIBRARY->battlefieldsHandler.get(), "battlefield"))); - handlers.push_back(std::make_pair("terrains", ContentTypeHandler(LIBRARY->terrainTypeHandler.get(), "terrain"))); - handlers.push_back(std::make_pair("rivers", ContentTypeHandler(LIBRARY->riverTypeHandler.get(), "river"))); - handlers.push_back(std::make_pair("roads", ContentTypeHandler(LIBRARY->roadTypeHandler.get(), "road"))); - handlers.push_back(std::make_pair("obstacles", ContentTypeHandler(LIBRARY->obstacleHandler.get(), "obstacle"))); - handlers.push_back(std::make_pair("biomes", ContentTypeHandler(LIBRARY->biomeHandler.get(), "biome"))); + handlers.insert(std::make_pair("battlefields", ContentTypeHandler(LIBRARY->battlefieldsHandler.get(), "battlefield"))); + handlers.insert(std::make_pair("terrains", ContentTypeHandler(LIBRARY->terrainTypeHandler.get(), "terrain"))); + handlers.insert(std::make_pair("rivers", ContentTypeHandler(LIBRARY->riverTypeHandler.get(), "river"))); + handlers.insert(std::make_pair("roads", ContentTypeHandler(LIBRARY->roadTypeHandler.get(), "road"))); + handlers.insert(std::make_pair("obstacles", ContentTypeHandler(LIBRARY->obstacleHandler.get(), "obstacle"))); + handlers.insert(std::make_pair("biomes", ContentTypeHandler(LIBRARY->biomeHandler.get(), "biome"))); + handlers.insert(std::make_pair("resources", ContentTypeHandler(LIBRARY->resourceTypeHandler.get(), "resources"))); } bool CContentHandler::preloadData(const ModDescription & mod, bool validate) @@ -310,15 +309,7 @@ void CContentHandler::afterLoadFinalization() const ContentTypeHandler & CContentHandler::operator[](const std::string & name) const { - auto it = std::find_if(handlers.begin(), handlers.end(), [&name](const auto& pair) - { - return pair.first == name; - }); - - if (it != handlers.end()) { - return it->second; - } - throw std::out_of_range("Handler not found for name: " + name); + return handlers.at(name); } VCMI_LIB_NAMESPACE_END diff --git a/lib/modding/ContentTypeHandler.h b/lib/modding/ContentTypeHandler.h index 1f68e32bb..abcf7321e 100644 --- a/lib/modding/ContentTypeHandler.h +++ b/lib/modding/ContentTypeHandler.h @@ -50,7 +50,7 @@ public: /// class used to load all game data into handlers. Used only during loading class DLL_LINKAGE CContentHandler { - std::vector> handlers; + std::map handlers; public: void init();