Skip to content

Commit 122eed7

Browse files
authoredMay 19, 2018
Add screenshots to online content browser
1 parent 8295f9f commit 122eed7

File tree

4 files changed

+74
-4
lines changed

4 files changed

+74
-4
lines changed
 

‎builtin/mainmenu/dlg_contentstore.lua

+60-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@
1818
local store = {}
1919
local package_dialog = {}
2020

21+
-- Screenshot
22+
local screenshot_dir = os.tempfolder()
23+
assert(core.create_dir(screenshot_dir))
24+
local screenshot_downloading = {}
25+
local screenshot_downloaded = {}
26+
27+
-- Filter
2128
local search_string = ""
2229
local cur_page = 1
2330
local num_per_page = 5
@@ -137,6 +144,58 @@ local function start_install(calling_dialog, package)
137144
new_dlg:show()
138145
end
139146

147+
local function get_screenshot(package)
148+
if #package.screenshots == 0 then
149+
return defaulttexturedir .. "no_screenshot.png"
150+
elseif screenshot_downloading[package.screenshots[1]] then
151+
return defaulttexturedir .. "loading_screenshot.png"
152+
end
153+
154+
-- Get tmp screenshot path
155+
local filepath = screenshot_dir .. DIR_DELIM ..
156+
package.type .. "-" .. package.author .. "-" .. package.name .. ".png"
157+
158+
-- Return if already downloaded
159+
local file = io.open(filepath, "r")
160+
if file then
161+
file:close()
162+
return filepath
163+
end
164+
165+
-- Show error if we've failed to download before
166+
if screenshot_downloaded[package.screenshots[1]] then
167+
return defaulttexturedir .. "error_screenshot.png"
168+
end
169+
170+
-- Download
171+
172+
local function download_screenshot(params)
173+
return core.download_file(params.url, params.dest)
174+
end
175+
local function callback(success)
176+
screenshot_downloading[package.screenshots[1]] = nil
177+
screenshot_downloaded[package.screenshots[1]] = true
178+
if not success then
179+
core.log("warning", "Screenshot download failed for some reason")
180+
end
181+
182+
local ele = ui.childlist.store
183+
if ele and not ele.hidden then
184+
core.update_formspec(ele:formspec())
185+
end
186+
end
187+
if core.handle_async(download_screenshot,
188+
{ dest = filepath, url = package.screenshots[1] }, callback) then
189+
screenshot_downloading[package.screenshots[1]] = true
190+
else
191+
core.log("error", "ERROR: async event failed")
192+
return defaulttexturedir .. "error_screenshot.png"
193+
end
194+
195+
return defaulttexturedir .. "loading_screenshot.png"
196+
end
197+
198+
140199

141200
function package_dialog.get_formspec()
142201
local package = package_dialog.package
@@ -315,8 +374,7 @@ function store.get_formspec()
315374

316375
-- image
317376
formspec[#formspec + 1] = "image[-0.4,0;1.5,1;"
318-
formspec[#formspec + 1] = defaulttexturedir
319-
formspec[#formspec + 1] = "no_screenshot.png"
377+
formspec[#formspec + 1] = get_screenshot(package)
320378
formspec[#formspec + 1] = "]"
321379

322380
-- title

‎src/script/lua_api/l_mainmenu.cpp

+14-2
Original file line numberDiff line numberDiff line change
@@ -1043,9 +1043,21 @@ int ModApiMainMenu::l_get_package_list(lua_State *L)
10431043
lua_pushstring(L, package.url.c_str());
10441044
lua_settable (L, top_lvl2);
10451045

1046-
lua_pushstring(L, "release");
1046+
lua_pushstring (L, "release");
10471047
lua_pushinteger(L, package.release);
1048-
lua_settable (L, top_lvl2);
1048+
lua_settable (L, top_lvl2);
1049+
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+
}
1059+
}
1060+
lua_settable(L, top_lvl2);
10491061

10501062
lua_settable(L, top);
10511063
index++;
971 Bytes
Loading
580 Bytes
Loading

0 commit comments

Comments
 (0)
Please sign in to comment.