/* Minetest Copyright (C) 2013 celeron55, Perttu Ahola This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include #include #include #include "convert_json.h" #include "mods.h" #include "config.h" #include "log.h" #include "main.h" // for g_settings #include "settings.h" #include "httpfetch.h" #include "porting.h" Json::Value fetchJsonValue(const std::string &url, std::vector *extra_headers) { HTTPFetchRequest fetch_request; HTTPFetchResult fetch_result; fetch_request.url = url; fetch_request.caller = HTTPFETCH_SYNC; if (extra_headers != NULL) fetch_request.extra_headers = *extra_headers; httpfetch_sync(fetch_request, fetch_result); if (!fetch_result.succeeded) { return Json::Value(); } Json::Value root; Json::Reader reader; std::istringstream stream(fetch_result.data); if (!reader.parse(stream, root)) { errorstream << "URL: " << url << std::endl; errorstream << "Failed to parse json data " << reader.getFormattedErrorMessages(); errorstream << "data: \"" << fetch_result.data << "\"" << std::endl; return Json::Value(); } return root; } std::vector readModStoreList(Json::Value& modlist) { std::vector retval; if (modlist.isArray()) { for (unsigned int i = 0; i < modlist.size(); i++) { ModStoreMod toadd; toadd.valid = true; //id if (modlist[i]["id"].asString().size()) { std::string id_raw = modlist[i]["id"].asString(); char* endptr = 0; int numbervalue = strtol(id_raw.c_str(),&endptr,10); if ((id_raw != "") && (*endptr == 0)) { toadd.id = numbervalue; } else { errorstream << "readModStoreList: missing id" << std::endl; toadd.valid = false; } } else { errorstream << "readModStoreList: missing id" << std::endl; toadd.valid = false; } //title if (modlist[i]["title"].asString().size()) { toadd.title = modlist[i]["title"].asString(); } else { errorstream << "readModStoreList: missing title" << std::endl; toadd.valid = false; } //basename if (modlist[i]["basename"].asString().size()) { toadd.basename = modlist[i]["basename"].asString(); } else { errorstream << "readModStoreList: missing basename" << std::endl; toadd.valid = false; } //author //rating //version if (toadd.valid) { retval.push_back(toadd); } } } return retval; } ModStoreModDetails readModStoreModDetails(Json::Value& details) { ModStoreModDetails retval; retval.valid = true; //version set if (details["version_set"].isArray()) { for (unsigned int i = 0; i < details["version_set"].size(); i++) { ModStoreVersionEntry toadd; if (details["version_set"][i]["id"].asString().size()) { std::string id_raw = details["version_set"][i]["id"].asString(); char* endptr = 0; int numbervalue = strtol(id_raw.c_str(),&endptr,10); if ((id_raw != "") && (*endptr == 0)) { toadd.id = numbervalue; } } else { errorstream << "readModStoreModDetails: missing version_set id" << std::endl; retval.valid = false; } //date if (details["version_set"][i]["date"].asString().size()) { toadd.date = details["version_set"][i]["date"].asString(); } //file if (details["version_set"][i]["file"].asString().size()) { toadd.file = details["version_set"][i]["file"].asString(); } else { errorstream << "readModStoreModDetails: missing version_set file" << std::endl; retval.valid = false; } //approved //mtversion if( retval.valid ) { retval.versions.push_back(toadd); } else { break; } } } if (retval.versions.size() < 1) { infostream << "readModStoreModDetails: not a single version specified!" << std::endl; retval.valid = false; } //categories if (details["categories"].isObject()) { for (unsigned int i = 0; i < details["categories"].size(); i++) { ModStoreCategoryInfo toadd; if (details["categories"][i]["id"].asString().size()) { std::string id_raw = details["categories"][i]["id"].asString(); char* endptr = 0; int numbervalue = strtol(id_raw.c_str(),&endptr,10); if ((id_raw != "") && (*endptr == 0)) { toadd.id = numbervalue; } } else { errorstream << "readModStoreModDetails: missing categories id" << std::endl; retval.valid = false; } if (details["categories"][i]["title"].asString().size()) { toadd.name = details["categories"][i]["title"].asString(); } else { errorstream << "readModStoreModDetails: missing categories title" << std::endl; retval.valid = false; } if( retval.valid ) { retval.categories.push_back(toadd); } else { break; } } } //author if (details["author"].isObject()) { if (details["author"]["id"].asString().size()) { std::string id_raw = details["author"]["id"].asString(); char* endptr = 0; int numbervalue = strtol(id_raw.c_str(),&endptr,10); if ((id_raw != "") && (*endptr == 0)) { retval.author.id = numbervalue; } else { errorstream << "readModStoreModDetails: missing author id (convert)" << std::endl; retval.valid = false; } } else { errorstream << "readModStoreModDetails: missing author id" << std::endl; retval.valid = false; } if (details["author"]["username"].asString().size()) { retval.author.username = details["author"]["username"].asString(); /* Minetest Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com> This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #ifndef L_UTIL_H_ #define L_UTIL_H_ #include "lua_api/l_base.h" class AsyncEngine; class ModApiUtil : public ModApiBase { private: /* NOTE: The functions in this module are available in the in-game API as well as in the mainmenu API. All functions that don't require either a Server or GUIEngine instance should be in here. */ // debug(text) // Writes a line to dstream static int l_debug(lua_State *L); // log([level,] text) // Writes a line to the logger. // The one-argument version logs to infostream. // The two-argument version accept a log level: error, action, info, or verbose. static int l_log(lua_State *L); // setting_set(name, value) static int l_setting_set(lua_State *L); // setting_get(name) static int l_setting_get(lua_State *L); // setting_setbool(name, value)