Skip to content

Commit ca502fc

Browse files
authoredJun 18, 2018
Update to new ContentDB API
1 parent e8b687d commit ca502fc

File tree

4 files changed

+35
-42
lines changed

4 files changed

+35
-42
lines changed
 

Diff for: ‎builtin/mainmenu/dlg_contentstore.lua

+7-7
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,9 @@ local function start_install(calling_dialog, package)
145145
end
146146

147147
local function get_screenshot(package)
148-
if #package.screenshots == 0 then
148+
if not package.thumbnail then
149149
return defaulttexturedir .. "no_screenshot.png"
150-
elseif screenshot_downloading[package.screenshots[1]] then
150+
elseif screenshot_downloading[package.thumbnail] then
151151
return defaulttexturedir .. "loading_screenshot.png"
152152
end
153153

@@ -163,7 +163,7 @@ local function get_screenshot(package)
163163
end
164164

165165
-- Show error if we've failed to download before
166-
if screenshot_downloaded[package.screenshots[1]] then
166+
if screenshot_downloaded[package.thumbnail] then
167167
return defaulttexturedir .. "error_screenshot.png"
168168
end
169169

@@ -173,8 +173,8 @@ local function get_screenshot(package)
173173
return core.download_file(params.url, params.dest)
174174
end
175175
local function callback(success)
176-
screenshot_downloading[package.screenshots[1]] = nil
177-
screenshot_downloaded[package.screenshots[1]] = true
176+
screenshot_downloading[package.thumbnail] = nil
177+
screenshot_downloaded[package.thumbnail] = true
178178
if not success then
179179
core.log("warning", "Screenshot download failed for some reason")
180180
end
@@ -185,8 +185,8 @@ local function get_screenshot(package)
185185
end
186186
end
187187
if core.handle_async(download_screenshot,
188-
{ dest = filepath, url = package.screenshots[1] }, callback) then
189-
screenshot_downloading[package.screenshots[1]] = true
188+
{ dest = filepath, url = package.thumbnail }, callback) then
189+
screenshot_downloading[package.thumbnail] = true
190190
else
191191
core.log("error", "ERROR: async event failed")
192192
return defaulttexturedir .. "error_screenshot.png"

Diff for: ‎src/content/packages.cpp

+5-10
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,19 @@ std::vector<Package> getPackagesFromURL(const std::string &url)
4343
for (unsigned int i = 0; i < json.size(); ++i) {
4444
Package package;
4545

46+
package.author = json[i]["author"].asString();
4647
package.name = json[i]["name"].asString();
4748
package.title = json[i]["title"].asString();
48-
package.author = json[i]["author"].asString();
4949
package.type = json[i]["type"].asString();
5050
package.shortDesc = json[i]["shortDesc"].asString();
51-
package.url = json[i]["url"].asString();
5251
package.release = json[i]["release"].asInt();
52+
if (json[i].isMember("thumbnail"))
53+
package.thumbnail = json[i]["thumbnail"].asString();
5354

54-
Json::Value jScreenshots = json[i]["screenshots"];
55-
for (unsigned int j = 0; j < jScreenshots.size(); ++j) {
56-
package.screenshots.push_back(jScreenshots[j].asString());
57-
}
58-
59-
if (package.valid()) {
55+
if (package.valid())
6056
packages.push_back(package);
61-
} else {
57+
else
6258
errorstream << "Invalid package at " << i << std::endl;
63-
}
6459
}
6560

6661
return packages;

Diff for: ‎src/content/packages.h

+10-6
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,24 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2424

2525
struct Package
2626
{
27+
std::string author;
2728
std::string name; // Technical name
2829
std::string title;
29-
std::string author;
3030
std::string type; // One of "mod", "game", or "txp"
3131

3232
std::string shortDesc;
33-
std::string url; // download URL
3433
u32 release;
35-
std::vector<std::string> screenshots;
34+
std::string thumbnail;
35+
36+
bool valid() const
37+
{
38+
return !(author.empty() || name.empty() || title.empty() ||
39+
type.empty() || release <= 0);
40+
}
3641

37-
bool valid()
42+
std::string getDownloadURL(const std::string &baseURL) const
3843
{
39-
return !(name.empty() || title.empty() || author.empty() ||
40-
type.empty() || url.empty() || release <= 0);
44+
return baseURL + "/packages/" + author + "/" + name + "/download/";
4145
}
4246
};
4347

Diff for: ‎src/script/lua_api/l_mainmenu.cpp

+13-19
Original file line numberDiff line numberDiff line change
@@ -1005,7 +1005,7 @@ int ModApiMainMenu::l_get_screen_info(lua_State *L)
10051005
int ModApiMainMenu::l_get_package_list(lua_State *L)
10061006
{
10071007
std::string url = g_settings->get("contentdb_url");
1008-
std::vector<Package> packages = getPackagesFromURL(url + "/packages/");
1008+
std::vector<Package> packages = getPackagesFromURL(url + "/api/packages/");
10091009

10101010
// Make table
10111011
lua_newtable(L);
@@ -1019,6 +1019,10 @@ int ModApiMainMenu::l_get_package_list(lua_State *L)
10191019

10201020
int top_lvl2 = lua_gettop(L);
10211021

1022+
lua_pushstring(L, "author");
1023+
lua_pushstring(L, package.author.c_str());
1024+
lua_settable (L, top_lvl2);
1025+
10221026
lua_pushstring(L, "name");
10231027
lua_pushstring(L, package.name.c_str());
10241028
lua_settable (L, top_lvl2);
@@ -1027,10 +1031,6 @@ int ModApiMainMenu::l_get_package_list(lua_State *L)
10271031
lua_pushstring(L, package.title.c_str());
10281032
lua_settable (L, top_lvl2);
10291033

1030-
lua_pushstring(L, "author");
1031-
lua_pushstring(L, package.author.c_str());
1032-
lua_settable (L, top_lvl2);
1033-
10341034
lua_pushstring(L, "type");
10351035
lua_pushstring(L, package.type.c_str());
10361036
lua_settable (L, top_lvl2);
@@ -1039,25 +1039,19 @@ int ModApiMainMenu::l_get_package_list(lua_State *L)
10391039
lua_pushstring(L, package.shortDesc.c_str());
10401040
lua_settable (L, top_lvl2);
10411041

1042-
lua_pushstring(L, "url");
1043-
lua_pushstring(L, package.url.c_str());
1044-
lua_settable (L, top_lvl2);
1045-
10461042
lua_pushstring (L, "release");
10471043
lua_pushinteger(L, package.release);
10481044
lua_settable (L, top_lvl2);
10491045

1050-
lua_pushstring(L, "screenshots");
1051-
lua_newtable(L);
1052-
{
1053-
int top_screenshots = lua_gettop(L);
1054-
for (size_t i = 0; i < package.screenshots.size(); ++i) {
1055-
lua_pushnumber(L, i + 1);
1056-
lua_pushstring(L, package.screenshots[i].c_str());
1057-
lua_settable(L, top_screenshots);
1058-
}
1046+
if (package.thumbnail != "") {
1047+
lua_pushstring(L, "thumbnail");
1048+
lua_pushstring(L, package.thumbnail.c_str());
1049+
lua_settable (L, top_lvl2);
10591050
}
1060-
lua_settable(L, top_lvl2);
1051+
1052+
lua_pushstring(L, "url");
1053+
lua_pushstring(L, package.getDownloadURL(url).c_str());
1054+
lua_settable (L, top_lvl2);
10611055

10621056
lua_settable(L, top);
10631057
index++;

0 commit comments

Comments
 (0)
Please sign in to comment.