1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-28 08:48:48 +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",
"title" : "VCMI map object format",
"description" : "Description of map object class",
"required": [ "handler" ],
"required": [ "handler", "name" ],
"additionalProperties" : false,
"properties":{
@ -19,6 +19,9 @@
"base": {
"type" : "object"
},
"name": {
"type":"string"
},
"types": {
"type":"object",
"additionalProperties": {

View File

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

View File

@ -120,7 +120,7 @@ std::vector<JsonNode> CObjectClassesHandler::loadLegacyData(size_t dataSize)
CLegacyConfigParser namesParser("Data/ObjNames.txt");
for (size_t i=0; i<256; i++)
{
ret[i]["base"]["name"].String() = namesParser.readString();
ret[i]["name"].String() = namesParser.readString();
namesParser.endLine();
}
@ -148,6 +148,9 @@ std::vector<JsonNode> CObjectClassesHandler::loadLegacyData(size_t dataSize)
ret[Obj::CREATURE_GENERATOR1]["subObjects"] = cregen1;
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;
}
@ -158,7 +161,7 @@ void CObjectClassesHandler::loadSubObject(const std::string & scope, const std::
assert(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)
@ -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
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)
@ -182,9 +185,13 @@ TObjectTypeHandler CObjectClassesHandler::loadSubObjectFromJson(const std::strin
}
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->setTypeName(obj->getIdentifier(), identifier);
createdObject->init(entry);
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);
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;
}
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)
@ -212,14 +229,18 @@ ObjectClass * CObjectClassesHandler::loadFromJson(const std::string & scope, con
obj->base = json["base"];
obj->id = index;
VLC->generaltexth->registerString(obj->getNameTextID(), json["name"].String());
obj->objects.resize(json["lastReservedIndex"].Float() + 1);
for (auto subData : json["types"].Struct())
{
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);
std::string const & subMeta = subData.second["index"].meta;
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();
loadSubObject(scope, subData.first, subData.second, obj, subIndex);
}
@ -285,7 +306,7 @@ TObjectTypeHandler CObjectClassesHandler::getHandlerFor(std::string scope, std::
if(id)
{
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)
return object->objects[subID.get()];
@ -364,7 +385,7 @@ void CObjectClassesHandler::afterLoadFinalization()
obj->afterLoadFinalization();
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
{
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
{
// 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)
subtype = 0;
@ -420,17 +449,17 @@ void AObjectTypeHandler::setTypeName(std::string type, std::string 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;
}
std::string AObjectTypeHandler::getSubTypeName()
std::string AObjectTypeHandler::getSubTypeName() const
{
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())
sounds.ambient.push_back(node.String());
@ -517,9 +544,14 @@ void AObjectTypeHandler::initTypeData(const JsonNode & input)
// empty implementation for overrides
}
bool AObjectTypeHandler::hasNameTextID() const
{
return false;
}
std::string AObjectTypeHandler::getNameTextID() const
{
return TextIdentifier( typeName, modName, subTypeName, "name" ).get();
return TextIdentifier("mapObject", getTypeName(), getJsonKey(), "name").get();
}
std::string AObjectTypeHandler::getNameTranslated() const

View File

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