2013-06-23 18:30:21 +02:00
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
#include "convert_json.h"
|
|
|
|
#include "mods.h"
|
|
|
|
#include "config.h"
|
|
|
|
#include "log.h"
|
2013-07-15 23:36:44 +02:00
|
|
|
#include "main.h" // for g_settings
|
|
|
|
#include "settings.h"
|
2013-11-05 13:57:43 +01:00
|
|
|
#include "version.h"
|
2013-11-09 22:49:27 +01:00
|
|
|
#include "httpfetch.h"
|
2014-02-26 14:21:38 +01:00
|
|
|
#include "porting.h"
|
2013-06-23 18:30:21 +02:00
|
|
|
|
2014-02-27 21:12:59 +01:00
|
|
|
Json::Value fetchJsonValue(const std::string &url,
|
2014-06-19 20:58:22 +02:00
|
|
|
std::vector<std::string> *extra_headers) {
|
2013-07-27 15:44:59 +02:00
|
|
|
|
2013-11-09 22:49:27 +01:00
|
|
|
HTTPFetchRequest fetchrequest;
|
|
|
|
HTTPFetchResult fetchresult;
|
|
|
|
fetchrequest.url = url;
|
|
|
|
fetchrequest.caller = HTTPFETCH_SYNC;
|
|
|
|
|
2014-06-19 20:58:22 +02:00
|
|
|
if (extra_headers != NULL)
|
|
|
|
fetchrequest.extra_headers = *extra_headers;
|
|
|
|
|
2013-11-09 22:49:27 +01:00
|
|
|
httpfetch_sync(fetchrequest,fetchresult);
|
2013-06-23 18:30:21 +02:00
|
|
|
|
2013-11-09 22:49:27 +01:00
|
|
|
if (!fetchresult.succeeded) {
|
2013-06-23 18:30:21 +02:00
|
|
|
return Json::Value();
|
|
|
|
}
|
2013-11-09 22:49:27 +01:00
|
|
|
Json::Value root;
|
|
|
|
Json::Reader reader;
|
|
|
|
std::istringstream stream(fetchresult.data);
|
2013-06-23 18:30:21 +02:00
|
|
|
|
|
|
|
if (!reader.parse( stream, root ) )
|
|
|
|
{
|
|
|
|
errorstream << "URL: " << url << std::endl;
|
|
|
|
errorstream << "Failed to parse json data " << reader.getFormattedErrorMessages();
|
2013-11-09 22:49:27 +01:00
|
|
|
errorstream << "data: \"" << fetchresult.data << "\"" << std::endl;
|
2013-06-23 18:30:21 +02:00
|
|
|
return Json::Value();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (root.isArray()) {
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
if ((root["list"].isArray())) {
|
|
|
|
return root["list"];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return root;
|
|
|
|
}
|
2014-04-06 14:02:53 +02:00
|
|
|
|
2013-06-23 18:30:21 +02:00
|
|
|
return Json::Value();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<ModStoreMod> readModStoreList(Json::Value& modlist) {
|
2014-04-06 14:02:53 +02:00
|
|
|
std::vector<ModStoreMod> retval;
|
2013-06-23 18:30:21 +02:00
|
|
|
|
|
|
|
if (modlist.isArray()) {
|
|
|
|
for (unsigned int i = 0; i < modlist.size(); i++)
|
|
|
|
{
|
|
|
|
ModStoreMod toadd;
|
|
|
|
toadd.valid = true;
|
|
|
|
|
|
|
|
//id
|
|
|
|
if (modlist[i]["id"].asString().size()) {
|
2013-11-04 17:04:55 +01:00
|
|
|
std::string id_raw = modlist[i]["id"].asString();
|
2013-06-23 18:30:21 +02:00
|
|
|
char* endptr = 0;
|
2013-11-04 17:04:55 +01:00
|
|
|
int numbervalue = strtol(id_raw.c_str(),&endptr,10);
|
2013-06-23 18:30:21 +02:00
|
|
|
|
2013-11-04 17:04:55 +01:00
|
|
|
if ((id_raw != "") && (*endptr == 0)) {
|
2013-06-23 18:30:21 +02:00
|
|
|
toadd.id = numbervalue;
|
|
|
|
}
|
2013-11-04 17:04:55 +01:00
|
|
|
else {
|
|
|
|
errorstream << "readModStoreList: missing id" << std::endl;
|
|
|
|
toadd.valid = false;
|
|
|
|
}
|
2013-06-23 18:30:21 +02:00
|
|
|
}
|
|
|
|
else {
|
2013-07-13 01:36:58 +02:00
|
|
|
errorstream << "readModStoreList: missing id" << std::endl;
|
2013-06-23 18:30:21 +02:00
|
|
|
toadd.valid = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//title
|
|
|
|
if (modlist[i]["title"].asString().size()) {
|
|
|
|
toadd.title = modlist[i]["title"].asString();
|
|
|
|
}
|
|
|
|
else {
|
2013-07-13 01:36:58 +02:00
|
|
|
errorstream << "readModStoreList: missing title" << std::endl;
|
2013-06-23 18:30:21 +02:00
|
|
|
toadd.valid = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//basename
|
|
|
|
if (modlist[i]["basename"].asString().size()) {
|
|
|
|
toadd.basename = modlist[i]["basename"].asString();
|
|
|
|
}
|
|
|
|
else {
|
2013-07-13 01:36:58 +02:00
|
|
|
errorstream << "readModStoreList: missing basename" << std::endl;
|
2013-06-23 18:30:21 +02:00
|
|
|
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()) {
|
2013-11-04 17:04:55 +01:00
|
|
|
std::string id_raw = details["version_set"][i]["id"].asString();
|
2013-06-23 18:30:21 +02:00
|
|
|
char* endptr = 0;
|
2013-11-04 17:04:55 +01:00
|
|
|
int numbervalue = strtol(id_raw.c_str(),&endptr,10);
|
2013-06-23 18:30:21 +02:00
|
|
|
|
2013-11-04 17:04:55 +01:00
|
|
|
if ((id_raw != "") && (*endptr == 0)) {
|
2013-06-23 18:30:21 +02:00
|
|
|
toadd.id = numbervalue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2013-07-13 01:36:58 +02:00
|
|
|
errorstream << "readModStoreModDetails: missing version_set id" << std::endl;
|
2013-06-23 18:30:21 +02:00
|
|
|
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 {
|
2013-07-13 01:36:58 +02:00
|
|
|
errorstream << "readModStoreModDetails: missing version_set file" << std::endl;
|
2013-06-23 18:30:21 +02:00
|
|
|
retval.valid = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//approved
|
|
|
|
|
|
|
|
//mtversion
|
|
|
|
|
|
|
|
if( retval.valid ) {
|
|
|
|
retval.versions.push_back(toadd);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (retval.versions.size() < 1) {
|
2013-12-11 23:07:38 +01:00
|
|
|
infostream << "readModStoreModDetails: not a single version specified!" << std::endl;
|
2013-06-23 18:30:21 +02:00
|
|
|
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()) {
|
|
|
|
|
2013-11-04 17:04:55 +01:00
|
|
|
std::string id_raw = details["categories"][i]["id"].asString();
|
2013-06-23 18:30:21 +02:00
|
|
|
char* endptr = 0;
|
2013-11-04 17:04:55 +01:00
|
|
|
int numbervalue = strtol(id_raw.c_str(),&endptr,10);
|
2013-06-23 18:30:21 +02:00
|
|
|
|
2013-11-04 17:04:55 +01:00
|
|
|
if ((id_raw != "") && (*endptr == 0)) {
|
2013-06-23 18:30:21 +02:00
|
|
|
toadd.id = numbervalue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2013-07-13 01:36:58 +02:00
|
|
|
errorstream << "readModStoreModDetails: missing categories id" << std::endl;
|
2013-06-23 18:30:21 +02:00
|
|
|
retval.valid = false;
|
|
|
|
}
|
|
|
|
if (details["categories"][i]["title"].asString().size()) {
|
|
|
|
toadd.name = details["categories"][i]["title"].asString();
|
|
|
|
}
|
|
|
|
else {
|
2013-07-13 01:36:58 +02:00
|
|
|
errorstream << "readModStoreModDetails: missing categories title" << std::endl;
|
2013-06-23 18:30:21 +02:00
|
|
|
retval.valid = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( retval.valid ) {
|
|
|
|
retval.categories.push_back(toadd);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//author
|
|
|
|
if (details["author"].isObject()) {
|
|
|
|
if (details["author"]["id"].asString().size()) {
|
|
|
|
|
2013-11-04 17:04:55 +01:00
|
|
|
std::string id_raw = details["author"]["id"].asString();
|
2013-06-23 18:30:21 +02:00
|
|
|
char* endptr = 0;
|
2013-11-04 17:04:55 +01:00
|
|
|
int numbervalue = strtol(id_raw.c_str(),&endptr,10);
|
2013-06-23 18:30:21 +02:00
|
|
|
|
2013-11-04 17:04:55 +01:00
|
|
|
if ((id_raw != "") && (*endptr == 0)) {
|
2013-06-23 18:30:21 +02:00
|
|
|
retval.author.id = numbervalue;
|
|
|
|
}
|
|
|
|
else {
|
2013-07-13 01:36:58 +02:00
|
|
|
errorstream << "readModStoreModDetails: missing author id (convert)" << std::endl;
|
2013-06-23 18:30:21 +02:00
|
|
|
retval.valid = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2013-07-13 01:36:58 +02:00
|
|
|
errorstream << "readModStoreModDetails: missing author id" << std::endl;
|
2013-06-23 18:30:21 +02:00
|
|
|
retval.valid = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (details["author"]["username"].asString().size()) {
|
|
|
|
retval.author.username = details["author"]["username"].asString();
|
|
|
|
}
|
|
|
|
else {
|
2013-07-13 01:36:58 +02:00
|
|
|
errorstream << "readModStoreModDetails: missing author username" << std::endl;
|
2013-06-23 18:30:21 +02:00
|
|
|
retval.valid = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2013-07-13 01:36:58 +02:00
|
|
|
errorstream << "readModStoreModDetails: missing author" << std::endl;
|
2013-06-23 18:30:21 +02:00
|
|
|
retval.valid = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//license
|
|
|
|
if (details["license"].isObject()) {
|
|
|
|
if (details["license"]["id"].asString().size()) {
|
|
|
|
|
2013-11-04 17:04:55 +01:00
|
|
|
std::string id_raw = details["license"]["id"].asString();
|
2013-06-23 18:30:21 +02:00
|
|
|
char* endptr = 0;
|
2013-11-04 17:04:55 +01:00
|
|
|
int numbervalue = strtol(id_raw.c_str(),&endptr,10);
|
2013-06-23 18:30:21 +02:00
|
|
|
|
2013-11-04 17:04:55 +01:00
|
|
|
if ((id_raw != "") && (*endptr == 0)) {
|
2013-06-23 18:30:21 +02:00
|
|
|
retval.license.id = numbervalue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2013-07-13 01:36:58 +02:00
|
|
|
errorstream << "readModStoreModDetails: missing license id" << std::endl;
|
2013-06-23 18:30:21 +02:00
|
|
|
retval.valid = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (details["license"]["short"].asString().size()) {
|
|
|
|
retval.license.shortinfo = details["license"]["short"].asString();
|
|
|
|
}
|
|
|
|
else {
|
2013-07-13 01:36:58 +02:00
|
|
|
errorstream << "readModStoreModDetails: missing license short" << std::endl;
|
2013-06-23 18:30:21 +02:00
|
|
|
retval.valid = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (details["license"]["link"].asString().size()) {
|
|
|
|
retval.license.url = details["license"]["link"].asString();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-07-13 01:36:58 +02:00
|
|
|
//titlepic
|
|
|
|
if (details["titlepic"].isObject()) {
|
|
|
|
if (details["titlepic"]["id"].asString().size()) {
|
|
|
|
|
2013-11-04 17:04:55 +01:00
|
|
|
std::string id_raw = details["titlepic"]["id"].asString();
|
2013-07-13 01:36:58 +02:00
|
|
|
char* endptr = 0;
|
2013-11-04 17:04:55 +01:00
|
|
|
int numbervalue = strtol(id_raw.c_str(),&endptr,10);
|
2013-07-13 01:36:58 +02:00
|
|
|
|
2013-11-04 17:04:55 +01:00
|
|
|
if ((id_raw != "") && (*endptr == 0)) {
|
2013-07-13 01:36:58 +02:00
|
|
|
retval.titlepic.id = numbervalue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (details["titlepic"]["file"].asString().size()) {
|
|
|
|
retval.titlepic.file = details["titlepic"]["file"].asString();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (details["titlepic"]["desc"].asString().size()) {
|
|
|
|
retval.titlepic.description = details["titlepic"]["desc"].asString();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (details["titlepic"]["mod"].asString().size()) {
|
|
|
|
|
2013-11-04 17:04:55 +01:00
|
|
|
std::string mod_raw = details["titlepic"]["mod"].asString();
|
2013-07-13 01:36:58 +02:00
|
|
|
char* endptr = 0;
|
2013-11-04 17:04:55 +01:00
|
|
|
int numbervalue = strtol(mod_raw.c_str(),&endptr,10);
|
2013-07-13 01:36:58 +02:00
|
|
|
|
2013-11-04 17:04:55 +01:00
|
|
|
if ((mod_raw != "") && (*endptr == 0)) {
|
2013-07-13 01:36:58 +02:00
|
|
|
retval.titlepic.mod = numbervalue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-23 18:30:21 +02:00
|
|
|
//id
|
|
|
|
if (details["id"].asString().size()) {
|
|
|
|
|
2013-11-04 17:04:55 +01:00
|
|
|
std::string id_raw = details["id"].asString();
|
2013-06-23 18:30:21 +02:00
|
|
|
char* endptr = 0;
|
2013-11-04 17:04:55 +01:00
|
|
|
int numbervalue = strtol(id_raw.c_str(),&endptr,10);
|
2013-06-23 18:30:21 +02:00
|
|
|
|
2013-11-04 17:04:55 +01:00
|
|
|
if ((id_raw != "") && (*endptr == 0)) {
|
2013-06-23 18:30:21 +02:00
|
|
|
retval.id = numbervalue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2013-07-13 01:36:58 +02:00
|
|
|
errorstream << "readModStoreModDetails: missing id" << std::endl;
|
2013-06-23 18:30:21 +02:00
|
|
|
retval.valid = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//title
|
|
|
|
if (details["title"].asString().size()) {
|
|
|
|
retval.title = details["title"].asString();
|
|
|
|
}
|
|
|
|
else {
|
2013-07-13 01:36:58 +02:00
|
|
|
errorstream << "readModStoreModDetails: missing title" << std::endl;
|
2013-06-23 18:30:21 +02:00
|
|
|
retval.valid = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//basename
|
|
|
|
if (details["basename"].asString().size()) {
|
|
|
|
retval.basename = details["basename"].asString();
|
|
|
|
}
|
|
|
|
else {
|
2013-07-13 01:36:58 +02:00
|
|
|
errorstream << "readModStoreModDetails: missing basename" << std::endl;
|
2013-06-23 18:30:21 +02:00
|
|
|
retval.valid = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//description
|
|
|
|
if (details["desc"].asString().size()) {
|
|
|
|
retval.description = details["desc"].asString();
|
|
|
|
}
|
|
|
|
|
|
|
|
//repository
|
|
|
|
if (details["replink"].asString().size()) {
|
|
|
|
retval.repository = details["replink"].asString();
|
|
|
|
}
|
|
|
|
|
|
|
|
//value
|
|
|
|
if (details["rating"].asString().size()) {
|
|
|
|
|
2013-11-04 17:04:55 +01:00
|
|
|
std::string id_raw = details["rating"].asString();
|
2013-06-23 18:30:21 +02:00
|
|
|
char* endptr = 0;
|
2013-11-04 17:04:55 +01:00
|
|
|
float numbervalue = strtof(id_raw.c_str(),&endptr);
|
2013-06-23 18:30:21 +02:00
|
|
|
|
2013-11-04 17:04:55 +01:00
|
|
|
if ((id_raw != "") && (*endptr == 0)) {
|
2013-06-23 18:30:21 +02:00
|
|
|
retval.rating = numbervalue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
retval.rating = 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//depends
|
|
|
|
if (details["depends"].isArray()) {
|
|
|
|
//TODO
|
|
|
|
}
|
|
|
|
|
|
|
|
//softdepends
|
|
|
|
if (details["softdep"].isArray()) {
|
|
|
|
//TODO
|
|
|
|
}
|
|
|
|
|
|
|
|
//screenshot url
|
|
|
|
if (details["screenshot_url"].asString().size()) {
|
|
|
|
retval.screenshot_url = details["screenshot_url"].asString();
|
|
|
|
}
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|