void mod_manager::load_modfile( JsonObject &jo, const std::string &path ) { if (!jo.has_string("type") || jo.get_string("type") != "MOD_INFO") { // Ignore anything that is not a mod-info return; } std::string m_ident = jo.get_string("ident"); if (has_mod(m_ident)) { // TODO: change this to make unique ident for the mod // (instead of discarding it?) debugmsg("there is already a mod with ident %s", m_ident.c_str()); return; } std::string m_name = jo.get_string("name", ""); if (m_name.empty()) { // "No name" gets confusing if many mods have no name //~ name of a mod that has no name entry, (%s is the mods identifier) m_name = string_format(_("No name (%s)"), m_ident.c_str()); } else { m_name = _(m_name.c_str()); } std::string m_cat = jo.get_string("category", ""); std::pair<int, std::string> p_cat = {-1, ""}; bool bCatFound = false; do { for( size_t i = 0; i < get_mod_list_categories().size(); ++i ) { if( get_mod_list_categories()[i].first == m_cat ) { p_cat = { int(i), get_mod_list_categories()[i].second }; bCatFound = true; break; } } if( !bCatFound && m_cat != "" ) { m_cat = ""; } else { break; } } while( !bCatFound ); std::unique_ptr<MOD_INFORMATION> modfile( new MOD_INFORMATION ); modfile->ident = m_ident; modfile->name = m_name; modfile->category = p_cat; if( assign( jo, "path", modfile->path ) ) { modfile->path = path + "/" + modfile->path; } else { modfile->path = path; } if( assign( jo, "legacy", modfile->legacy ) ) { modfile->legacy = path + "/" + modfile->legacy; } assign( jo, "authors", modfile->authors ); assign( jo, "maintainers", modfile->maintainers ); assign( jo, "description", modfile->description ); assign( jo, "dependencies", modfile->dependencies ); assign( jo, "core", modfile->core ); assign( jo, "obsolete", modfile->obsolete ); if( modfile->dependencies.count( modfile->ident ) ) { jo.throw_error( "mod specifies self as a dependency", "dependencies" ); } mod_map[modfile->ident] = std::move( modfile ); }
void mod_manager::load_modfile(JsonObject &jo, const std::string &main_path) { if (!jo.has_string("type") || jo.get_string("type") != "MOD_INFO") { // Ignore anything that is not a mod-info return; } std::string m_ident = jo.get_string("ident"); if (has_mod(m_ident)) { // TODO: change this to make unique ident for the mod // (instead of discarding it?) debugmsg("there is already a mod with ident %s", m_ident.c_str()); return; } std::string t_type = jo.get_string("mod-type", "SUPPLEMENTAL"); std::vector<std::string> m_authors; if (jo.has_array("authors")) { m_authors = jo.get_string_array("authors"); } else { if(jo.has_string("author")) { m_authors.push_back(jo.get_string("author")); } } std::string m_name = jo.get_string("name", ""); if (m_name.empty()) { // "No name" gets confusing if many mods have no name //~ name of a mod that has no name entry, (%s is the mods identifier) m_name = string_format(_("No name (%s)"), m_ident.c_str()); } else { m_name = _(m_name.c_str()); } std::string m_desc = jo.get_string("description", ""); if (m_desc.empty()) { m_desc = _("No description"); } else { m_desc = _(m_desc.c_str()); } std::string m_cat = jo.get_string("category", ""); std::pair<int, std::string> p_cat = {-1, ""}; bool bCatFound = false; do { for( size_t i = 0; i < get_mod_list_categories().size(); ++i ) { if( get_mod_list_categories()[i].first == m_cat ) { p_cat = { i, get_mod_list_categories()[i].second }; bCatFound = true; break; } } if( !bCatFound && m_cat != "" ) { m_cat = ""; } else { break; } } while( !bCatFound ); std::string m_path; if (jo.has_string("path")) { m_path = jo.get_string("path"); if (m_path.empty()) { // If an empty path is given, use only the // folder of the modinfo.json m_path = main_path; } else { // prefix the folder of modinfo.json m_path = main_path + "/" + m_path; } } else { // Default if no path is given: // "<folder-of-modinfo.json>/data" m_path = main_path + "/data"; } bool m_need_lua = jo.get_bool("with-lua", false); if ( file_exist(m_path + "/main.lua") || file_exist(m_path + "/preload.lua") ) { m_need_lua = true; } std::vector<std::string> m_dependencies; if (jo.has_member("dependencies") && jo.has_array("dependencies")) { JsonArray jarr = jo.get_array("dependencies"); while(jarr.has_more()) { const std::string dep = jarr.next_string(); if (dep == m_ident) { debugmsg("mod %s has itself as dependency", m_ident.c_str()); continue; } if (std::find(m_dependencies.begin(), m_dependencies.end(), dep) != m_dependencies.end()) { // Some dependency listed twice, ignore it, what else can be done? continue; } m_dependencies.push_back(dep); } } mod_type m_type = MT_CORE; if (t_type == "CORE") { m_type = MT_CORE; } else if (t_type == "SUPPLEMENTAL") { m_type = MT_SUPPLEMENTAL; } else { jo.throw_error( std::string("Invalid mod type: ") + t_type + " for mod " + m_ident ); } MOD_INFORMATION *modfile = new MOD_INFORMATION; modfile->ident = m_ident; modfile->_type = m_type; modfile->authors = m_authors; modfile->name = m_name; modfile->description = m_desc; modfile->dependencies = m_dependencies; modfile->category = p_cat; modfile->path = m_path; modfile->need_lua = m_need_lua; mod_map[modfile->ident] = modfile; }