mirror of
https://github.com/moparisthebest/minetest
synced 2024-11-07 18:05:08 -05:00
368 lines
7.8 KiB
C++
368 lines
7.8 KiB
C++
|
/*
|
||
|
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"
|
||
|
|
||
|
#if USE_CURL
|
||
|
#include <curl/curl.h>
|
||
|
|
||
|
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
|
||
|
{
|
||
|
((std::string*)userp)->append((char*)contents, size * nmemb);
|
||
|
return size * nmemb;
|
||
|
}
|
||
|
|
||
|
#endif
|
||
|
|
||
|
Json::Value fetchJsonValue(const std::string url,
|
||
|
struct curl_slist *chunk) {
|
||
|
#if USE_CURL
|
||
|
std::string liststring;
|
||
|
CURL *curl;
|
||
|
|
||
|
curl = curl_easy_init();
|
||
|
if (curl)
|
||
|
{
|
||
|
CURLcode res;
|
||
|
|
||
|
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
|
||
|
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
||
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
|
||
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &liststring);
|
||
|
|
||
|
if (chunk != 0)
|
||
|
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
|
||
|
|
||
|
|
||
|
res = curl_easy_perform(curl);
|
||
|
if (res != CURLE_OK)
|
||
|
errorstream<<"Jsonreader: "<< url <<" not found (internet connection?)"<<std::endl;
|
||
|
curl_easy_cleanup(curl);
|
||
|
}
|
||
|
|
||
|
Json::Value root;
|
||
|
Json::Reader reader;
|
||
|
std::istringstream stream(liststring);
|
||
|
if (!liststring.size()) {
|
||
|
return Json::Value();
|
||
|
}
|
||
|
|
||
|
if (!reader.parse( stream, root ) )
|
||
|
{
|
||
|
errorstream << "URL: " << url << std::endl;
|
||
|
errorstream << "Failed to parse json data " << reader.getFormattedErrorMessages();
|
||
|
errorstream << "data: \"" << liststring << "\"" << std::endl;
|
||
|
return Json::Value();
|
||
|
}
|
||
|
|
||
|
if (root.isArray()) {
|
||
|
return root;
|
||
|
}
|
||
|
if ((root["list"].isArray())) {
|
||
|
return root["list"];
|
||
|
}
|
||
|
else {
|
||
|
return root;
|
||
|
}
|
||
|
#endif
|
||
|
return Json::Value();
|
||
|
}
|
||
|
|
||
|
std::vector<ModStoreMod> readModStoreList(Json::Value& modlist) {
|
||
|
std::vector<ModStoreMod> 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()) {
|
||
|
const char* id_raw = modlist[i]["id"].asString().c_str();
|
||
|
char* endptr = 0;
|
||
|
int numbervalue = strtol(id_raw,&endptr,10);
|
||
|
|
||
|
if ((*id_raw != 0) && (*endptr == 0)) {
|
||
|
toadd.id = numbervalue;
|
||
|
}
|
||
|
}
|
||
|
else {
|
||
|
toadd.valid = false;
|
||
|
}
|
||
|
|
||
|
//title
|
||
|
if (modlist[i]["title"].asString().size()) {
|
||
|
toadd.title = modlist[i]["title"].asString();
|
||
|
}
|
||
|
else {
|
||
|
toadd.valid = false;
|
||
|
}
|
||
|
|
||
|
//basename
|
||
|
if (modlist[i]["basename"].asString().size()) {
|
||
|
toadd.basename = modlist[i]["basename"].asString();
|
||
|
}
|
||
|
else {
|
||
|
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()) {
|
||
|
const char* id_raw = details["version_set"][i]["id"].asString().c_str();
|
||
|
char* endptr = 0;
|
||
|
int numbervalue = strtol(id_raw,&endptr,10);
|
||
|
|
||
|
if ((*id_raw != 0) && (*endptr == 0)) {
|
||
|
toadd.id = numbervalue;
|
||
|
}
|
||
|
}
|
||
|
else {
|
||
|
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 {
|
||
|
retval.valid = false;
|
||
|
}
|
||
|
|
||
|
//approved
|
||
|
|
||
|
//mtversion
|
||
|
|
||
|
if( retval.valid ) {
|
||
|
retval.versions.push_back(toadd);
|
||
|
}
|
||
|
else {
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (retval.versions.size() < 1) {
|
||
|
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()) {
|
||
|
|
||
|
const char* id_raw = details["categories"][i]["id"].asString().c_str();
|
||
|
char* endptr = 0;
|
||
|
int numbervalue = strtol(id_raw,&endptr,10);
|
||
|
|
||
|
if ((*id_raw != 0) && (*endptr == 0)) {
|
||
|
toadd.id = numbervalue;
|
||
|
}
|
||
|
}
|
||
|
else {
|
||
|
retval.valid = false;
|
||
|
}
|
||
|
if (details["categories"][i]["title"].asString().size()) {
|
||
|
toadd.name = details["categories"][i]["title"].asString();
|
||
|
}
|
||
|
else {
|
||
|
retval.valid = false;
|
||
|
}
|
||
|
|
||
|
if( retval.valid ) {
|
||
|
retval.categories.push_back(toadd);
|
||
|
}
|
||
|
else {
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//author
|
||
|
if (details["author"].isObject()) {
|
||
|
if (details["author"]["id"].asString().size()) {
|
||
|
|
||
|
const char* id_raw = details["author"]["id"].asString().c_str();
|
||
|
char* endptr = 0;
|
||
|
int numbervalue = strtol(id_raw,&endptr,10);
|
||
|
|
||
|
if ((*id_raw != 0) && (*endptr == 0)) {
|
||
|
retval.author.id = numbervalue;
|
||
|
}
|
||
|
else {
|
||
|
retval.valid = false;
|
||
|
}
|
||
|
}
|
||
|
else {
|
||
|
retval.valid = false;
|
||
|
}
|
||
|
|
||
|
if (details["author"]["username"].asString().size()) {
|
||
|
retval.author.username = details["author"]["username"].asString();
|
||
|
}
|
||
|
else {
|
||
|
retval.valid = false;
|
||
|
}
|
||
|
}
|
||
|
else {
|
||
|
retval.valid = false;
|
||
|
}
|
||
|
|
||
|
//license
|
||
|
if (details["license"].isObject()) {
|
||
|
if (details["license"]["id"].asString().size()) {
|
||
|
|
||
|
const char* id_raw = details["license"]["id"].asString().c_str();
|
||
|
char* endptr = 0;
|
||
|
int numbervalue = strtol(id_raw,&endptr,10);
|
||
|
|
||
|
if ((*id_raw != 0) && (*endptr == 0)) {
|
||
|
retval.license.id = numbervalue;
|
||
|
}
|
||
|
}
|
||
|
else {
|
||
|
retval.valid = false;
|
||
|
}
|
||
|
|
||
|
if (details["license"]["short"].asString().size()) {
|
||
|
retval.license.shortinfo = details["license"]["short"].asString();
|
||
|
}
|
||
|
else {
|
||
|
retval.valid = false;
|
||
|
}
|
||
|
|
||
|
if (details["license"]["link"].asString().size()) {
|
||
|
retval.license.url = details["license"]["link"].asString();
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
//id
|
||
|
if (details["id"].asString().size()) {
|
||
|
|
||
|
const char* id_raw = details["id"].asString().c_str();
|
||
|
char* endptr = 0;
|
||
|
int numbervalue = strtol(id_raw,&endptr,10);
|
||
|
|
||
|
if ((*id_raw != 0) && (*endptr == 0)) {
|
||
|
retval.id = numbervalue;
|
||
|
}
|
||
|
}
|
||
|
else {
|
||
|
retval.valid = false;
|
||
|
}
|
||
|
|
||
|
//title
|
||
|
if (details["title"].asString().size()) {
|
||
|
retval.title = details["title"].asString();
|
||
|
}
|
||
|
else {
|
||
|
retval.valid = false;
|
||
|
}
|
||
|
|
||
|
//basename
|
||
|
if (details["basename"].asString().size()) {
|
||
|
retval.basename = details["basename"].asString();
|
||
|
}
|
||
|
else {
|
||
|
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()) {
|
||
|
|
||
|
const char* id_raw = details["rating"].asString().c_str();
|
||
|
char* endptr = 0;
|
||
|
float numbervalue = strtof(id_raw,&endptr);
|
||
|
|
||
|
if ((*id_raw != 0) && (*endptr == 0)) {
|
||
|
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;
|
||
|
}
|