Update to new ContentDB API

This commit is contained in:
rubenwardy 2018-06-15 23:34:09 +01:00
parent ac6e8e2914
commit 925025f067
No known key found for this signature in database
GPG Key ID: A1E29D52FF81513C
4 changed files with 26 additions and 38 deletions

View File

@ -145,9 +145,9 @@ local function start_install(calling_dialog, package)
end end
local function get_screenshot(package) local function get_screenshot(package)
if #package.screenshots == 0 then if not package.thumbnail then
return defaulttexturedir .. "no_screenshot.png" return defaulttexturedir .. "no_screenshot.png"
elseif screenshot_downloading[package.screenshots[1]] then elseif screenshot_downloading[package.thumbnail] then
return defaulttexturedir .. "loading_screenshot.png" return defaulttexturedir .. "loading_screenshot.png"
end end
@ -163,7 +163,7 @@ local function get_screenshot(package)
end end
-- Show error if we've failed to download before -- Show error if we've failed to download before
if screenshot_downloaded[package.screenshots[1]] then if screenshot_downloaded[package.thumbnail] then
return defaulttexturedir .. "error_screenshot.png" return defaulttexturedir .. "error_screenshot.png"
end end
@ -173,8 +173,8 @@ local function get_screenshot(package)
return core.download_file(params.url, params.dest) return core.download_file(params.url, params.dest)
end end
local function callback(success) local function callback(success)
screenshot_downloading[package.screenshots[1]] = nil screenshot_downloading[package.thumbnail] = nil
screenshot_downloaded[package.screenshots[1]] = true screenshot_downloaded[package.thumbnail] = true
if not success then if not success then
core.log("warning", "Screenshot download failed for some reason") core.log("warning", "Screenshot download failed for some reason")
end end
@ -185,8 +185,8 @@ local function get_screenshot(package)
end end
end end
if core.handle_async(download_screenshot, if core.handle_async(download_screenshot,
{ dest = filepath, url = package.screenshots[1] }, callback) then { dest = filepath, url = package.thumbnail }, callback) then
screenshot_downloading[package.screenshots[1]] = true screenshot_downloading[package.thumbnail] = true
else else
core.log("error", "ERROR: async event failed") core.log("error", "ERROR: async event failed")
return defaulttexturedir .. "error_screenshot.png" return defaulttexturedir .. "error_screenshot.png"

View File

@ -43,17 +43,14 @@ std::vector<Package> getPackagesFromURL(const std::string &url)
for (unsigned int i = 0; i < json.size(); ++i) { for (unsigned int i = 0; i < json.size(); ++i) {
Package package; Package package;
package.author = json[i]["author"].asString();
package.name = json[i]["name"].asString(); package.name = json[i]["name"].asString();
package.title = json[i]["title"].asString(); package.title = json[i]["title"].asString();
package.author = json[i]["author"].asString();
package.type = json[i]["type"].asString(); package.type = json[i]["type"].asString();
package.shortDesc = json[i]["shortDesc"].asString(); package.shortDesc = json[i]["shortDesc"].asString();
package.url = json[i]["url"].asString();
package.release = json[i]["release"].asInt(); package.release = json[i]["release"].asInt();
if (json[i].isMember("thumbnail")) {
Json::Value jScreenshots = json[i]["screenshots"]; package.thumbnail = json[i]["thumbnail"].asString();
for (unsigned int j = 0; j < jScreenshots.size(); ++j) {
package.screenshots.push_back(jScreenshots[j].asString());
} }
if (package.valid()) { if (package.valid()) {

View File

@ -24,20 +24,19 @@ with this program; if not, write to the Free Software Foundation, Inc.,
struct Package struct Package
{ {
std::string author;
std::string name; // Technical name std::string name; // Technical name
std::string title; std::string title;
std::string author;
std::string type; // One of "mod", "game", or "txp" std::string type; // One of "mod", "game", or "txp"
std::string shortDesc; std::string shortDesc;
std::string url; // download URL
u32 release; u32 release;
std::vector<std::string> screenshots; std::string thumbnail;
bool valid() bool valid()
{ {
return !(name.empty() || title.empty() || author.empty() || return !(author.empty() || name.empty() || title.empty() ||
type.empty() || url.empty() || release <= 0); type.empty() || release <= 0);
} }
}; };

View File

@ -1005,7 +1005,7 @@ int ModApiMainMenu::l_get_screen_info(lua_State *L)
int ModApiMainMenu::l_get_package_list(lua_State *L) int ModApiMainMenu::l_get_package_list(lua_State *L)
{ {
std::string url = g_settings->get("contentdb_url"); std::string url = g_settings->get("contentdb_url");
std::vector<Package> packages = getPackagesFromURL(url + "/packages/"); std::vector<Package> packages = getPackagesFromURL(url + "/api/packages/");
// Make table // Make table
lua_newtable(L); lua_newtable(L);
@ -1019,6 +1019,10 @@ int ModApiMainMenu::l_get_package_list(lua_State *L)
int top_lvl2 = lua_gettop(L); int top_lvl2 = lua_gettop(L);
lua_pushstring(L, "author");
lua_pushstring(L, package.author.c_str());
lua_settable (L, top_lvl2);
lua_pushstring(L, "name"); lua_pushstring(L, "name");
lua_pushstring(L, package.name.c_str()); lua_pushstring(L, package.name.c_str());
lua_settable (L, top_lvl2); lua_settable (L, top_lvl2);
@ -1027,10 +1031,6 @@ int ModApiMainMenu::l_get_package_list(lua_State *L)
lua_pushstring(L, package.title.c_str()); lua_pushstring(L, package.title.c_str());
lua_settable (L, top_lvl2); lua_settable (L, top_lvl2);
lua_pushstring(L, "author");
lua_pushstring(L, package.author.c_str());
lua_settable (L, top_lvl2);
lua_pushstring(L, "type"); lua_pushstring(L, "type");
lua_pushstring(L, package.type.c_str()); lua_pushstring(L, package.type.c_str());
lua_settable (L, top_lvl2); lua_settable (L, top_lvl2);
@ -1039,25 +1039,17 @@ int ModApiMainMenu::l_get_package_list(lua_State *L)
lua_pushstring(L, package.shortDesc.c_str()); lua_pushstring(L, package.shortDesc.c_str());
lua_settable (L, top_lvl2); lua_settable (L, top_lvl2);
lua_pushstring(L, "url");
lua_pushstring(L, package.url.c_str());
lua_settable (L, top_lvl2);
lua_pushstring (L, "release"); lua_pushstring (L, "release");
lua_pushinteger(L, package.release); lua_pushinteger(L, package.release);
lua_settable (L, top_lvl2); lua_settable (L, top_lvl2);
lua_pushstring(L, "screenshots"); lua_pushstring(L, "thumbnail");
lua_newtable(L); lua_pushstring(L, package.thumbnail.c_str());
{ lua_settable (L, top_lvl2);
int top_screenshots = lua_gettop(L);
for (size_t i = 0; i < package.screenshots.size(); ++i) { lua_pushstring(L, "url");
lua_pushnumber(L, i + 1); lua_pushstring(L, (url + "/packages/" + package.author + "/" + package.name + "/download/").c_str());
lua_pushstring(L, package.screenshots[i].c_str()); lua_settable (L, top_lvl2);
lua_settable(L, top_screenshots);
}
}
lua_settable(L, top_lvl2);
lua_settable(L, top); lua_settable(L, top);
index++; index++;