]> git.lizzy.rs Git - minetest.git/blob - src/content/packages.cpp
Update translation sources
[minetest.git] / src / content / packages.cpp
1 /*
2 Minetest
3 Copyright (C) 2018 rubenwardy <rw@rubenwardy.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "content/packages.h"
21 #include "log.h"
22 #include "filesys.h"
23 #include "porting.h"
24 #include "settings.h"
25 #include "content/mods.h"
26 #include "content/subgames.h"
27
28 std::string Package::getDownloadURL(const std::string &baseURL) const
29 {
30         return baseURL + "/packages/" + author + "/" + name + "/releases/" +
31                std::to_string(release) + "/download/";
32 }
33
34 #if USE_CURL
35 std::vector<Package> getPackagesFromURL(const std::string &url)
36 {
37         std::vector<std::string> extra_headers;
38         extra_headers.emplace_back("Accept: application/json");
39
40         Json::Value json = fetchJsonValue(url, &extra_headers);
41         if (!json.isArray()) {
42                 errorstream << "Invalid JSON download " << std::endl;
43                 return std::vector<Package>();
44         }
45
46         std::vector<Package> packages;
47
48         // Note: `unsigned int` is required to index JSON
49         for (unsigned int i = 0; i < json.size(); ++i) {
50                 Package package;
51
52                 package.author = json[i]["author"].asString();
53                 package.name = json[i]["name"].asString();
54                 package.title = json[i]["title"].asString();
55                 package.type = json[i]["type"].asString();
56                 package.shortDesc = json[i]["shortDesc"].asString();
57                 package.release = json[i]["release"].asInt();
58                 if (json[i].isMember("thumbnail"))
59                         package.thumbnail = json[i]["thumbnail"].asString();
60
61                 if (package.valid())
62                         packages.push_back(package);
63                 else
64                         errorstream << "Invalid package at " << i << std::endl;
65         }
66
67         return packages;
68 }
69 #endif