ModList ModList::Load(QString filePath) { QFile modfile(filePath); modfile.open(QIODevice::ReadOnly); QByteArray filedata = modfile.readAll(); QJsonDocument modlist(QJsonDocument::fromJson(filedata)); QJsonObject object = modlist.object(); QJsonArray modArray = object["mods"].toArray(); ModList newModList; for (int idx = 0; idx < modArray.size(); ++idx) { QJsonObject mod = modArray[idx].toObject(); newModList.mods.append(Mod(mod)); } return newModList; }
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 ); }