1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-11-27 22:49:25 +02:00

All objects will register single name per object type

This commit is contained in:
Ivan Savenko
2023-01-11 00:48:51 +02:00
parent 49d0459412
commit bc27faea57
4 changed files with 80 additions and 41 deletions

View File

@@ -3,7 +3,7 @@
"$schema": "http://json-schema.org/draft-04/schema", "$schema": "http://json-schema.org/draft-04/schema",
"title" : "VCMI map object format", "title" : "VCMI map object format",
"description" : "Description of map object class", "description" : "Description of map object class",
"required": [ "handler" ], "required": [ "handler", "name" ],
"additionalProperties" : false, "additionalProperties" : false,
"properties":{ "properties":{
@@ -19,6 +19,9 @@
"base": { "base": {
"type" : "object" "type" : "object"
}, },
"name": {
"type":"string"
},
"types": { "types": {
"type":"object", "type":"object",
"additionalProperties": { "additionalProperties": {

View File

@@ -3,16 +3,13 @@
"$schema": "http://json-schema.org/draft-04/schema", "$schema": "http://json-schema.org/draft-04/schema",
"title" : "VCMI map object type format", "title" : "VCMI map object type format",
"description" : "Description of map object type, used only as sub-schema of object", "description" : "Description of map object type, used only as sub-schema of object",
"required": [ "name" ], "required": [ ],
"additionalProperties" : true, // may have type-dependant properties "additionalProperties" : true, // may have type-dependant properties
"properties":{ "properties":{
"index": { "index": {
"type":"number" "type":"number"
}, },
"name": {
"type":"string"
},
"aiValue": { "aiValue": {
"type":"number" "type":"number"
}, },

View File

@@ -120,7 +120,7 @@ std::vector<JsonNode> CObjectClassesHandler::loadLegacyData(size_t dataSize)
CLegacyConfigParser namesParser("Data/ObjNames.txt"); CLegacyConfigParser namesParser("Data/ObjNames.txt");
for (size_t i=0; i<256; i++) for (size_t i=0; i<256; i++)
{ {
ret[i]["base"]["name"].String() = namesParser.readString(); ret[i]["name"].String() = namesParser.readString();
namesParser.endLine(); namesParser.endLine();
} }
@@ -148,6 +148,9 @@ std::vector<JsonNode> CObjectClassesHandler::loadLegacyData(size_t dataSize)
ret[Obj::CREATURE_GENERATOR1]["subObjects"] = cregen1; ret[Obj::CREATURE_GENERATOR1]["subObjects"] = cregen1;
ret[Obj::CREATURE_GENERATOR4]["subObjects"] = cregen4; ret[Obj::CREATURE_GENERATOR4]["subObjects"] = cregen4;
ret[Obj::REFUGEE_CAMP]["subObjects"].Vector().push_back(ret[Obj::REFUGEE_CAMP]);
ret[Obj::WAR_MACHINE_FACTORY]["subObjects"].Vector().push_back(ret[Obj::WAR_MACHINE_FACTORY]);
return ret; return ret;
} }
@@ -158,7 +161,7 @@ void CObjectClassesHandler::loadSubObject(const std::string & scope, const std::
assert(object); assert(object);
obj->objects.push_back(object); obj->objects.push_back(object);
registerObject(scope, obj->getIdentifier(), identifier, object->subtype); registerObject(scope, "mapObject", obj->getJsonKey() + "." + object->getSubTypeName(), object->subtype);
} }
void CObjectClassesHandler::loadSubObject(const std::string & scope, const std::string & identifier, const JsonNode & entry, ObjectClass * obj, size_t index) void CObjectClassesHandler::loadSubObject(const std::string & scope, const std::string & identifier, const JsonNode & entry, ObjectClass * obj, size_t index)
@@ -170,7 +173,7 @@ void CObjectClassesHandler::loadSubObject(const std::string & scope, const std::
assert(obj->objects[index] == nullptr); // ensure that this id was not loaded before assert(obj->objects[index] == nullptr); // ensure that this id was not loaded before
obj->objects[index] = object; obj->objects[index] = object;
registerObject(scope, obj->getIdentifier(), identifier, object->subtype); registerObject(scope, "mapObject", obj->getJsonKey() + "." + object->getSubTypeName(), object->subtype);
} }
TObjectTypeHandler CObjectClassesHandler::loadSubObjectFromJson(const std::string & scope, const std::string & identifier, const JsonNode & entry, ObjectClass * obj, size_t index) TObjectTypeHandler CObjectClassesHandler::loadSubObjectFromJson(const std::string & scope, const std::string & identifier, const JsonNode & entry, ObjectClass * obj, size_t index)
@@ -182,9 +185,13 @@ TObjectTypeHandler CObjectClassesHandler::loadSubObjectFromJson(const std::strin
} }
auto createdObject = handlerConstructors.at(obj->handlerName)(); auto createdObject = handlerConstructors.at(obj->handlerName)();
createdObject->modName = scope;
if (identifier.find(':') == std::string::npos)
createdObject->setTypeName(obj->getJsonKey(), scope + ":" + identifier);
else
createdObject->setTypeName(obj->getJsonKey(), identifier);
createdObject->setType(obj->id, index); createdObject->setType(obj->id, index);
createdObject->setTypeName(obj->getIdentifier(), identifier);
createdObject->init(entry); createdObject->init(entry);
auto range = legacyTemplates.equal_range(std::make_pair(obj->id, index)); auto range = legacyTemplates.equal_range(std::make_pair(obj->id, index));
@@ -194,14 +201,24 @@ TObjectTypeHandler CObjectClassesHandler::loadSubObjectFromJson(const std::strin
} }
legacyTemplates.erase(range.first, range.second); legacyTemplates.erase(range.first, range.second);
logGlobal->debug("Loaded object %s(%d)::%s(%d)", obj->getIdentifier(), obj->id, identifier, index); logGlobal->debug("Loaded object %s(%d)::%s(%d)", obj->getJsonKey(), obj->id, identifier, index);
return createdObject; return createdObject;
} }
std::string ObjectClass::getIdentifier() const std::string ObjectClass::getJsonKey() const
{ {
return identifier + "Object"; return identifier;
}
std::string ObjectClass::getNameTextID() const
{
return TextIdentifier("object", identifier, "name").get();
}
std::string ObjectClass::getNameTranslated() const
{
return VLC->generaltexth->translate(getNameTextID());
} }
ObjectClass * CObjectClassesHandler::loadFromJson(const std::string & scope, const JsonNode & json, const std::string & name, size_t index) ObjectClass * CObjectClassesHandler::loadFromJson(const std::string & scope, const JsonNode & json, const std::string & name, size_t index)
@@ -212,14 +229,18 @@ ObjectClass * CObjectClassesHandler::loadFromJson(const std::string & scope, con
obj->base = json["base"]; obj->base = json["base"];
obj->id = index; obj->id = index;
VLC->generaltexth->registerString(obj->getNameTextID(), json["name"].String());
obj->objects.resize(json["lastReservedIndex"].Float() + 1); obj->objects.resize(json["lastReservedIndex"].Float() + 1);
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") std::string const & subMeta = subData.second["index"].meta;
logMod->warn("Object %s:%s from mod %s - attempt to load object with preset index!", name, subData.first, scope);
if ( subMeta != "core")
logMod->warn("Object %s:%s.%s - attempt to load object with preset index! This option is reserved for built-in mod", subMeta, name, subData.first );
size_t subIndex = subData.second["index"].Integer(); size_t subIndex = subData.second["index"].Integer();
loadSubObject(scope, subData.first, subData.second, obj, subIndex); loadSubObject(scope, subData.first, subData.second, obj, subIndex);
} }
@@ -285,7 +306,7 @@ TObjectTypeHandler CObjectClassesHandler::getHandlerFor(std::string scope, std::
if(id) if(id)
{ {
auto object = objects[id.get()]; auto object = objects[id.get()];
boost::optional<si32> subID = VLC->modh->identifiers.getIdentifier(scope, object->getIdentifier(), subtype, false); boost::optional<si32> subID = VLC->modh->identifiers.getIdentifier(scope, object->getJsonKey(), subtype, false);
if (subID) if (subID)
return object->objects[subID.get()]; return object->objects[subID.get()];
@@ -364,7 +385,7 @@ void CObjectClassesHandler::afterLoadFinalization()
obj->afterLoadFinalization(); obj->afterLoadFinalization();
if(obj->getTemplates().empty()) if(obj->getTemplates().empty())
logGlobal->warn("No templates found for %s:%s", entry->getIdentifier(), obj->getIdentifier()); logGlobal->warn("No templates found for %s:%s", entry->getJsonKey(), obj->getJsonKey());
} }
} }
@@ -378,11 +399,19 @@ void CObjectClassesHandler::afterLoadFinalization()
std::string CObjectClassesHandler::getObjectName(si32 type, si32 subtype) const std::string CObjectClassesHandler::getObjectName(si32 type, si32 subtype) const
{ {
return getHandlerFor(type, subtype)->getNameTranslated(); auto const handler = getHandlerFor(type, subtype);
if (handler->hasNameTextID())
return handler->getNameTranslated();
else
return objects[type]->getNameTranslated();
} }
SObjectSounds CObjectClassesHandler::getObjectSounds(si32 type, si32 subtype) const SObjectSounds CObjectClassesHandler::getObjectSounds(si32 type, si32 subtype) const
{ {
// TODO: these objects may have subID's that does not have associated handler:
// Prison: uses hero type as subID
// Hero: uses hero type as subID, but registers hero classes as subtypes
// Spell scroll: uses spell ID as subID
if(type == Obj::PRISON || type == Obj::HERO || type == Obj::SPELL_SCROLL) if(type == Obj::PRISON || type == Obj::HERO || type == Obj::SPELL_SCROLL)
subtype = 0; subtype = 0;
@@ -420,17 +449,17 @@ void AObjectTypeHandler::setTypeName(std::string type, std::string subtype)
this->subTypeName = subtype; this->subTypeName = subtype;
} }
std::string AObjectTypeHandler::getIdentifier() const std::string AObjectTypeHandler::getJsonKey() const
{ {
return modName + ":" + subTypeName; return subTypeName;
} }
std::string AObjectTypeHandler::getTypeName() std::string AObjectTypeHandler::getTypeName() const
{ {
return typeName; return typeName;
} }
std::string AObjectTypeHandler::getSubTypeName() std::string AObjectTypeHandler::getSubTypeName() const
{ {
return subTypeName; return subTypeName;
} }
@@ -475,8 +504,6 @@ void AObjectTypeHandler::init(const JsonNode & input)
} }
} }
VLC->generaltexth->registerString(getNameTextID(), input["name"].String());
for(const JsonNode & node : input["sounds"]["ambient"].Vector()) for(const JsonNode & node : input["sounds"]["ambient"].Vector())
sounds.ambient.push_back(node.String()); sounds.ambient.push_back(node.String());
@@ -517,9 +544,14 @@ void AObjectTypeHandler::initTypeData(const JsonNode & input)
// empty implementation for overrides // empty implementation for overrides
} }
bool AObjectTypeHandler::hasNameTextID() const
{
return false;
}
std::string AObjectTypeHandler::getNameTextID() const std::string AObjectTypeHandler::getNameTextID() const
{ {
return TextIdentifier( typeName, modName, subTypeName, "name" ).get(); return TextIdentifier("mapObject", getTypeName(), getJsonKey(), "name").get();
} }
std::string AObjectTypeHandler::getNameTranslated() const std::string AObjectTypeHandler::getNameTranslated() const

View File

@@ -158,7 +158,6 @@ protected:
/// initialization for classes that inherit this one /// initialization for classes that inherit this one
virtual void initTypeData(const JsonNode & input); virtual void initTypeData(const JsonNode & input);
std::string modName;
std::string typeName; std::string typeName;
std::string subTypeName; std::string subTypeName;
public: public:
@@ -171,18 +170,14 @@ public:
void setType(si32 type, si32 subtype); void setType(si32 type, si32 subtype);
void setTypeName(std::string type, std::string subtype); void setTypeName(std::string type, std::string subtype);
std::string getTypeName(); std::string getTypeName() const;
std::string getSubTypeName(); std::string getSubTypeName() const;
/// loads generic data from Json structure and passes it towards type-specific constructors /// loads generic data from Json structure and passes it towards type-specific constructors
void init(const JsonNode & input); void init(const JsonNode & input);
/// returns full form of identifier of this object in form of modName:objectName /// returns full form of identifier of this object in form of modName:objectName
std::string getIdentifier() const; std::string getJsonKey() const;
/// returns objet's name in form of translatable text ID
std::string getNameTextID() const;
std::string getNameTranslated() const;
/// Returns object-specific name, if set /// Returns object-specific name, if set
SObjectSounds getSounds() const; SObjectSounds getSounds() const;
@@ -204,6 +199,15 @@ public:
boost::optional<si32> getAiValue() const; boost::optional<si32> getAiValue() const;
/// returns true if this class provides custom text ID's instead of generic per-object name
virtual bool hasNameTextID() const;
/// returns object's name in form of translatable text ID
virtual std::string getNameTextID() const;
/// returns object's name in form of human-readable text
std::string getNameTranslated() const;
virtual bool isStaticObject(); virtual bool isStaticObject();
virtual void afterLoadFinalization(); virtual void afterLoadFinalization();
@@ -225,7 +229,6 @@ public:
h & subtype; h & subtype;
h & templates; h & templates;
h & rmgInfo; h & rmgInfo;
h & modName;
h & typeName; h & typeName;
h & subTypeName; h & subTypeName;
h & sounds; h & sounds;
@@ -239,15 +242,18 @@ typedef std::shared_ptr<AObjectTypeHandler> TObjectTypeHandler;
/// Class responsible for creation of adventure map objects of specific type /// Class responsible for creation of adventure map objects of specific type
class DLL_LINKAGE ObjectClass class DLL_LINKAGE ObjectClass
{ {
std::string modScope;
std::string identifier; std::string identifier;
public: public:
ObjectClass() = default; ObjectClass() = default;
ObjectClass(const std::string & modScope, const std::string & identifier): ObjectClass(const std::string & modScope, const std::string & identifier)
identifier(identifier), {
modScope(modScope) if (identifier.find(':') == std::string::npos)
{} this->identifier = modScope + ":" + identifier;
else
this->identifier = identifier;
}
si32 id; si32 id;
std::string handlerName; // ID of handler that controls this object, should be determined using handlerConstructor map std::string handlerName; // ID of handler that controls this object, should be determined using handlerConstructor map
@@ -255,7 +261,9 @@ public:
JsonNode base; JsonNode base;
std::vector<TObjectTypeHandler> objects; std::vector<TObjectTypeHandler> objects;
std::string getIdentifier() const; std::string getJsonKey() const;
std::string getNameTextID() const;
std::string getNameTranslated() const;
template <typename Handler> void serialize(Handler &h, const int version) template <typename Handler> void serialize(Handler &h, const int version)
{ {
@@ -263,7 +271,6 @@ public:
h & base; h & base;
h & objects; h & objects;
h & identifier; h & identifier;
h & modScope;
} }
}; };