1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-04-17 11:56:46 +02:00

It is now possible to define objects directly in mod.json instead of

using path to file with object definition
This commit is contained in:
Ivan Savenko 2024-09-30 19:26:22 +00:00
parent 1488629628
commit 8e4152bc81
7 changed files with 69 additions and 62 deletions

View File

@ -5,6 +5,17 @@
"description" : "Format used to define main mod file (mod.json) in VCMI", "description" : "Format used to define main mod file (mod.json) in VCMI",
"required" : [ "name", "description", "modType", "version", "author", "contact" ], "required" : [ "name", "description", "modType", "version", "author", "contact" ],
"definitions" : { "definitions" : {
"fileListOrObject" : {
"oneOf" : [
{
"type" : "array",
"items" : { "type" : "string", "format" : "textFile" }
},
{
"type" : "object"
}
]
},
"localizable" : { "localizable" : {
"type" : "object", "type" : "object",
"additionalProperties" : false, "additionalProperties" : false,
@ -35,9 +46,8 @@
"description" : "If set to true, vcmi will skip validation of current translation json files" "description" : "If set to true, vcmi will skip validation of current translation json files"
}, },
"translations" : { "translations" : {
"type" : "array",
"description" : "List of files with translations for this language", "description" : "List of files with translations for this language",
"items" : { "type" : "string", "format" : "textFile" } "$ref" : "#/definitions/fileListOrObject"
} }
} }
} }
@ -122,10 +132,18 @@
"description" : "If set to true, mod will not be enabled automatically on install" "description" : "If set to true, mod will not be enabled automatically on install"
}, },
"settings" : { "settings" : {
"type" : "object",
"description" : "List of changed game settings by mod", "description" : "List of changed game settings by mod",
"oneOf" : [
{
"type" : "object",
"$ref" : "gameSettings.json" "$ref" : "gameSettings.json"
}, },
{
"type" : "array",
"items" : { "type" : "string", "format" : "textFile" }
},
]
},
"filesystem" : { "filesystem" : {
"type" : "object", "type" : "object",
"description" : "Optional, description on how files are organized in your mod. In most cases you do not need to use this field", "description" : "Optional, description on how files are organized in your mod. In most cases you do not need to use this field",
@ -206,94 +224,76 @@
"$ref" : "#/definitions/localizable" "$ref" : "#/definitions/localizable"
}, },
"translations" : { "translations" : {
"type" : "array",
"description" : "List of files with translations for this language", "description" : "List of files with translations for this language",
"items" : { "type" : "string", "format" : "textFile" } "$ref" : "#/definitions/fileListOrObject"
}, },
"factions" : { "factions" : {
"type" : "array",
"description" : "List of configuration files for towns/factions", "description" : "List of configuration files for towns/factions",
"items" : { "type" : "string", "format" : "textFile" } "$ref" : "#/definitions/fileListOrObject"
}, },
"heroClasses" : { "heroClasses" : {
"type" : "array",
"description" : "List of configuration files for hero classes", "description" : "List of configuration files for hero classes",
"items" : { "type" : "string", "format" : "textFile" } "$ref" : "#/definitions/fileListOrObject"
}, },
"heroes" : { "heroes" : {
"type" : "array",
"description" : "List of configuration files for heroes", "description" : "List of configuration files for heroes",
"items" : { "type" : "string", "format" : "textFile" } "$ref" : "#/definitions/fileListOrObject"
}, },
"skills" : { "skills" : {
"type" : "array",
"description" : "List of configuration files for skills", "description" : "List of configuration files for skills",
"items" : { "type" : "string", "format" : "textFile" } "$ref" : "#/definitions/fileListOrObject"
}, },
"creatures" : { "creatures" : {
"type" : "array",
"description" : "List of configuration files for creatures", "description" : "List of configuration files for creatures",
"items" : { "type" : "string", "format" : "textFile" } "$ref" : "#/definitions/fileListOrObject"
}, },
"artifacts" : { "artifacts" : {
"type" : "array",
"description" : "List of configuration files for artifacts", "description" : "List of configuration files for artifacts",
"items" : { "type" : "string", "format" : "textFile" } "$ref" : "#/definitions/fileListOrObject"
}, },
"spells" : { "spells" : {
"type" : "array",
"description" : "List of configuration files for spells", "description" : "List of configuration files for spells",
"items" : { "type" : "string", "format" : "textFile" } "$ref" : "#/definitions/fileListOrObject"
}, },
"objects" : { "objects" : {
"type" : "array",
"description" : "List of configuration files for objects", "description" : "List of configuration files for objects",
"items" : { "type" : "string", "format" : "textFile" } "$ref" : "#/definitions/fileListOrObject"
}, },
"biomes" : { "biomes" : {
"type" : "array",
"description" : "List of configuration files for biomes", "description" : "List of configuration files for biomes",
"items" : { "type" : "string", "format" : "textFile" } "$ref" : "#/definitions/fileListOrObject"
}, },
"bonuses" : { "bonuses" : {
"type" : "array",
"description" : "List of configuration files for bonuses", "description" : "List of configuration files for bonuses",
"items" : { "type" : "string", "format" : "textFile" } "$ref" : "#/definitions/fileListOrObject"
}, },
"terrains" : { "terrains" : {
"type" : "array",
"description" : "List of configuration files for terrains", "description" : "List of configuration files for terrains",
"items" : { "type" : "string", "format" : "textFile" } "$ref" : "#/definitions/fileListOrObject"
}, },
"roads" : { "roads" : {
"type" : "array",
"description" : "List of configuration files for roads", "description" : "List of configuration files for roads",
"items" : { "type" : "string", "format" : "textFile" } "$ref" : "#/definitions/fileListOrObject"
}, },
"rivers" : { "rivers" : {
"type" : "array",
"description" : "List of configuration files for rivers", "description" : "List of configuration files for rivers",
"items" : { "type" : "string", "format" : "textFile" } "$ref" : "#/definitions/fileListOrObject"
}, },
"battlefields" : { "battlefields" : {
"type" : "array",
"description" : "List of configuration files for battlefields", "description" : "List of configuration files for battlefields",
"items" : { "type" : "string", "format" : "textFile" } "$ref" : "#/definitions/fileListOrObject"
}, },
"obstacles" : { "obstacles" : {
"type" : "array",
"description" : "List of configuration files for obstacles", "description" : "List of configuration files for obstacles",
"items" : { "type" : "string", "format" : "textFile" } "$ref" : "#/definitions/fileListOrObject"
}, },
"templates" : { "templates" : {
"type" : "array",
"description" : "List of configuration files for RMG templates", "description" : "List of configuration files for RMG templates",
"items" : { "type" : "string", "format" : "textFile" } "$ref" : "#/definitions/fileListOrObject"
}, },
"scripts" : { "scripts" : {
"type" : "array",
"description" : "List of configuration files for scripts", "description" : "List of configuration files for scripts",
"items" : { "type" : "string", "format" : "textFile" } "$ref" : "#/definitions/fileListOrObject"
} }
} }
} }

View File

@ -230,6 +230,27 @@ void JsonUtils::inherit(JsonNode & descendant, const JsonNode & base)
std::swap(descendant, inheritedNode); std::swap(descendant, inheritedNode);
} }
JsonNode JsonUtils::assembleFromFiles(const JsonNode & files, bool & isValid)
{
if (files.isVector())
{
auto configList = files.convertTo<std::vector<std::string> >();
JsonNode result = JsonUtils::assembleFromFiles(configList, isValid);
return result;
}
else
{
return files;
}
}
JsonNode JsonUtils::assembleFromFiles(const JsonNode & files)
{
bool isValid = false;
return assembleFromFiles(files, isValid);
}
JsonNode JsonUtils::assembleFromFiles(const std::vector<std::string> & files) JsonNode JsonUtils::assembleFromFiles(const std::vector<std::string> & files)
{ {
bool isValid = false; bool isValid = false;

View File

@ -44,6 +44,8 @@ namespace JsonUtils
* @brief generate one Json structure from multiple files * @brief generate one Json structure from multiple files
* @param files - list of filenames with parts of json structure * @param files - list of filenames with parts of json structure
*/ */
DLL_LINKAGE JsonNode assembleFromFiles(const JsonNode & files);
DLL_LINKAGE JsonNode assembleFromFiles(const JsonNode & files, bool & isValid);
DLL_LINKAGE JsonNode assembleFromFiles(const std::vector<std::string> & files); DLL_LINKAGE JsonNode assembleFromFiles(const std::vector<std::string> & files);
DLL_LINKAGE JsonNode assembleFromFiles(const std::vector<std::string> & files, bool & isValid); DLL_LINKAGE JsonNode assembleFromFiles(const std::vector<std::string> & files, bool & isValid);

View File

@ -384,7 +384,7 @@ std::set<TModID> CModHandler::getModDependencies(const TModID & modId, bool & is
void CModHandler::initializeConfig() void CModHandler::initializeConfig()
{ {
VLC->settingsHandler->loadBase(coreMod->config["settings"]); VLC->settingsHandler->loadBase(JsonUtils::assembleFromFiles(coreMod->config["settings"]));
for(const TModID & modName : activeMods) for(const TModID & modName : activeMods)
{ {
@ -401,21 +401,6 @@ CModVersion CModHandler::getModVersion(TModID modName) const
return {}; return {};
} }
static JsonNode loadReferencesList(const JsonNode & source)
{
if (source.isVector())
{
auto configList = source.convertTo<std::vector<std::string> >();
JsonNode result = JsonUtils::assembleFromFiles(configList);
return result;
}
else
{
return source;
}
}
void CModHandler::loadTranslation(const TModID & modName) void CModHandler::loadTranslation(const TModID & modName)
{ {
const auto & mod = allMods[modName]; const auto & mod = allMods[modName];
@ -423,8 +408,8 @@ void CModHandler::loadTranslation(const TModID & modName)
std::string preferredLanguage = VLC->generaltexth->getPreferredLanguage(); std::string preferredLanguage = VLC->generaltexth->getPreferredLanguage();
std::string modBaseLanguage = allMods[modName].baseLanguage; std::string modBaseLanguage = allMods[modName].baseLanguage;
JsonNode baseTranslation = loadReferencesList(mod.config["translations"]); JsonNode baseTranslation = JsonUtils::assembleFromFiles(mod.config["translations"]);
JsonNode extraTranslation = loadReferencesList(mod.config[preferredLanguage]["translations"]); JsonNode extraTranslation = JsonUtils::assembleFromFiles(mod.config[preferredLanguage]["translations"]);
VLC->generaltexth->loadTranslationOverrides(modName, baseTranslation); VLC->generaltexth->loadTranslationOverrides(modName, baseTranslation);
VLC->generaltexth->loadTranslationOverrides(modName, extraTranslation); VLC->generaltexth->loadTranslationOverrides(modName, extraTranslation);

View File

@ -50,7 +50,7 @@ ContentTypeHandler::ContentTypeHandler(IHandlerBase * handler, const std::string
} }
} }
bool ContentTypeHandler::preloadModData(const std::string & modName, const std::vector<std::string> & fileList, bool validate) bool ContentTypeHandler::preloadModData(const std::string & modName, const JsonNode & fileList, bool validate)
{ {
bool result = false; bool result = false;
JsonNode data = JsonUtils::assembleFromFiles(fileList, result); JsonNode data = JsonUtils::assembleFromFiles(fileList, result);
@ -216,7 +216,7 @@ bool CContentHandler::preloadModData(const std::string & modName, JsonNode modCo
bool result = true; bool result = true;
for(auto & handler : handlers) for(auto & handler : handlers)
{ {
result &= handler.second.preloadModData(modName, modConfig[handler.first].convertTo<std::vector<std::string> >(), validate); result &= handler.second.preloadModData(modName, modConfig[handler.first], validate);
} }
return result; return result;
} }

View File

@ -39,7 +39,7 @@ public:
/// local version of methods in ContentHandler /// local version of methods in ContentHandler
/// returns true if loading was successful /// returns true if loading was successful
bool preloadModData(const std::string & modName, const std::vector<std::string> & fileList, bool validate); bool preloadModData(const std::string & modName, const JsonNode & fileList, bool validate);
bool loadMod(const std::string & modName, bool validate); bool loadMod(const std::string & modName, bool validate);
void loadCustom(); void loadCustom();
void afterLoadFinalization(); void afterLoadFinalization();

View File

@ -36,7 +36,6 @@ protected:
void serialize(Handler & h) void serialize(Handler & h)
{ {
h & translatedText; h & translatedText;
//h & baseLanguage;
h & identifierModContext; h & identifierModContext;
h & baseStringModContext; h & baseStringModContext;
} }