1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-07-03 00:46:55 +02:00

Fixed loading of object names

This commit is contained in:
Ivan Savenko
2023-01-10 16:24:42 +02:00
parent b13637cdd5
commit f71cbc5d9b
3 changed files with 19 additions and 14 deletions

View File

@ -13,10 +13,6 @@
"lastReservedIndex" : { "lastReservedIndex" : {
"type":"number" "type":"number"
}, },
"subObjects" : {
"type" : "array",
"additionalItems" : true
},
"handler": { "handler": {
"type":"string" "type":"string"
}, },

View File

@ -182,6 +182,7 @@ TObjectTypeHandler CObjectClassesHandler::loadSubObjectFromJson(const std::strin
} }
auto createdObject = handlerConstructors.at(obj->handlerName)(); auto createdObject = handlerConstructors.at(obj->handlerName)();
createdObject->modName = scope;
createdObject->setType(obj->id, index); createdObject->setType(obj->id, index);
createdObject->setTypeName(obj->getIdentifier(), identifier); createdObject->setTypeName(obj->getIdentifier(), identifier);
createdObject->init(entry); createdObject->init(entry);
@ -212,18 +213,14 @@ ObjectClass * CObjectClassesHandler::loadFromJson(const std::string & scope, con
obj->id = index; obj->id = index;
obj->objects.resize(json["lastReservedIndex"].Float() + 1); obj->objects.resize(json["lastReservedIndex"].Float() + 1);
auto originalData = json["subObjects"].Vector();
for (auto subData : json["types"].Struct()) for (auto subData : json["types"].Struct())
{ {
if (!subData.second["index"].isNull()) if (!subData.second["index"].isNull())
{ {
if (subData.second["index"].meta != "core")
logMod->warn("Object %s:%s from mod %s - attempt to load object with preset index!", name, subData.first, scope);
size_t subIndex = subData.second["index"].Integer(); size_t subIndex = subData.second["index"].Integer();
if (subIndex < originalData.size())
{
JsonUtils::merge(originalData[subIndex], subData.second);
std::swap(originalData[subIndex], subData.second);
}
loadSubObject(scope, subData.first, subData.second, obj, subIndex); loadSubObject(scope, subData.first, subData.second, obj, subIndex);
} }
else else
@ -333,10 +330,24 @@ void CObjectClassesHandler::beforeValidate(JsonNode & object)
{ {
for (auto & entry : object["types"].Struct()) for (auto & entry : object["types"].Struct())
{ {
if (object.Struct().count("subObjects"))
{
auto const & vector = object["subObjects"].Vector();
if (!entry.second.Struct().count("index"))
continue;
size_t index = entry.second["index"].Integer();
if (index < vector.size())
JsonUtils::inherit(entry.second, vector[index]);
}
JsonUtils::inherit(entry.second, object["base"]); JsonUtils::inherit(entry.second, object["base"]);
for (auto & templ : entry.second["templates"].Struct()) for (auto & templ : entry.second["templates"].Struct())
JsonUtils::inherit(templ.second, entry.second["base"]); JsonUtils::inherit(templ.second, entry.second["base"]);
} }
object.Struct().erase("subObjects");
} }
void CObjectClassesHandler::afterLoadFinalization() void CObjectClassesHandler::afterLoadFinalization()

View File

@ -139,10 +139,9 @@ class CGObjectInstance;
/// Class responsible for creation of objects of specific type & subtype /// Class responsible for creation of objects of specific type & subtype
class DLL_LINKAGE AObjectTypeHandler : public boost::noncopyable class DLL_LINKAGE AObjectTypeHandler : public boost::noncopyable
{ {
RandomMapInfo rmgInfo; friend class CObjectClassesHandler;
/// Text ID for human-readable name of this object, used for objects like banks and dwellings, if set RandomMapInfo rmgInfo;
boost::optional<std::string> nameTextID;
JsonNode base; /// describes base template JsonNode base; /// describes base template
@ -226,7 +225,6 @@ public:
h & subtype; h & subtype;
h & templates; h & templates;
h & rmgInfo; h & rmgInfo;
h & nameTextID;
h & modName; h & modName;
h & typeName; h & typeName;
h & subTypeName; h & subTypeName;