From 122eed7a3449eead6f87f45f2eb55c11c3cf171e Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Sun, 20 May 2018 00:25:17 +0100 Subject: [PATCH] Add screenshots to online content browser --- builtin/mainmenu/dlg_contentstore.lua | 62 +++++++++++++++++++++- src/script/lua_api/l_mainmenu.cpp | 16 +++++- textures/base/pack/error_screenshot.png | Bin 0 -> 971 bytes textures/base/pack/loading_screenshot.png | Bin 0 -> 580 bytes 4 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 textures/base/pack/error_screenshot.png create mode 100644 textures/base/pack/loading_screenshot.png diff --git a/builtin/mainmenu/dlg_contentstore.lua b/builtin/mainmenu/dlg_contentstore.lua index 2c9e80e15..59c2480c1 100644 --- a/builtin/mainmenu/dlg_contentstore.lua +++ b/builtin/mainmenu/dlg_contentstore.lua @@ -18,6 +18,13 @@ local store = {} local package_dialog = {} +-- Screenshot +local screenshot_dir = os.tempfolder() +assert(core.create_dir(screenshot_dir)) +local screenshot_downloading = {} +local screenshot_downloaded = {} + +-- Filter local search_string = "" local cur_page = 1 local num_per_page = 5 @@ -137,6 +144,58 @@ local function start_install(calling_dialog, package) new_dlg:show() end +local function get_screenshot(package) + if #package.screenshots == 0 then + return defaulttexturedir .. "no_screenshot.png" + elseif screenshot_downloading[package.screenshots[1]] then + return defaulttexturedir .. "loading_screenshot.png" + end + + -- Get tmp screenshot path + local filepath = screenshot_dir .. DIR_DELIM .. + package.type .. "-" .. package.author .. "-" .. package.name .. ".png" + + -- Return if already downloaded + local file = io.open(filepath, "r") + if file then + file:close() + return filepath + end + + -- Show error if we've failed to download before + if screenshot_downloaded[package.screenshots[1]] then + return defaulttexturedir .. "error_screenshot.png" + end + + -- Download + + local function download_screenshot(params) + return core.download_file(params.url, params.dest) + end + local function callback(success) + screenshot_downloading[package.screenshots[1]] = nil + screenshot_downloaded[package.screenshots[1]] = true + if not success then + core.log("warning", "Screenshot download failed for some reason") + end + + local ele = ui.childlist.store + if ele and not ele.hidden then + core.update_formspec(ele:formspec()) + end + end + if core.handle_async(download_screenshot, + { dest = filepath, url = package.screenshots[1] }, callback) then + screenshot_downloading[package.screenshots[1]] = true + else + core.log("error", "ERROR: async event failed") + return defaulttexturedir .. "error_screenshot.png" + end + + return defaulttexturedir .. "loading_screenshot.png" +end + + function package_dialog.get_formspec() local package = package_dialog.package @@ -315,8 +374,7 @@ function store.get_formspec() -- image formspec[#formspec + 1] = "image[-0.4,0;1.5,1;" - formspec[#formspec + 1] = defaulttexturedir - formspec[#formspec + 1] = "no_screenshot.png" + formspec[#formspec + 1] = get_screenshot(package) formspec[#formspec + 1] = "]" -- title diff --git a/src/script/lua_api/l_mainmenu.cpp b/src/script/lua_api/l_mainmenu.cpp index 812fdffe7..1f49fc211 100644 --- a/src/script/lua_api/l_mainmenu.cpp +++ b/src/script/lua_api/l_mainmenu.cpp @@ -1043,9 +1043,21 @@ int ModApiMainMenu::l_get_package_list(lua_State *L) 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_settable (L, top_lvl2); + lua_settable (L, top_lvl2); + + lua_pushstring(L, "screenshots"); + lua_newtable(L); + { + int top_screenshots = lua_gettop(L); + for (size_t i = 0; i < package.screenshots.size(); ++i) { + lua_pushnumber(L, i + 1); + lua_pushstring(L, package.screenshots[i].c_str()); + lua_settable(L, top_screenshots); + } + } + lua_settable(L, top_lvl2); lua_settable(L, top); index++; diff --git a/textures/base/pack/error_screenshot.png b/textures/base/pack/error_screenshot.png new file mode 100644 index 0000000000000000000000000000000000000000..e35a0a38aa193ea3a160f8d9057a64909b3ea729 GIT binary patch literal 971 zcmeAS@N?(olHy`uVBq!ia0y~yVAKJ!Pp~ip$ps$|rU5C=0*}aI1_o|n5N2eUHAey{ z$X?><>&pI!m0irp><8b@Q$Qj90G|+7ApIYRAb^44lthOnP>7`@$S)X37xB(0>jKh@ zN#5=*OljOTDGUtEMxHK?Ar-gY-VN-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ading_screenshot.png new file mode 100644 index 0000000000000000000000000000000000000000..4d65a5d8d0364d1c624554500733f103bab074ed GIT binary patch literal 580 zcmeAS@N?(olHy`uVBq!ia0y~yVAKJ!Pp~ip$ps$|rU5C=0*}aI1_o|n5N2eUHAey{ z$X?><>&pI!m0iqGuuL;P2`Ho<;1l8sr2i8E*4cVl0+sTV1o;IsFmUiH`xn(Mdi3*m z)w<3Upg3cax4R2ZDq&qQccs^ z+M;GHzPWIzVnzl}R+n4*wFPSrZTh|N)iODbzUBqXC)>RL#{95$LiLxf1yjAkf^*k| z8_DPZ!5Ya8)BHBgeC%c!QGCSe)Z>`HZ3&5ZfbTgi`QBD!U>bYv+SALf*)`Ys^76l3 z-fY(O{-x>huTIO~Y`(IlYI{tyP0#DO^WSV2-dxswaLeOZ`DfWt*PP0Ce7Czd`|um{ z$(yFNXLrZ#v0iNFd+L_lTHUCP>so6(Pg(I)e^1&g@kv+nwbmQk{;ysqzAss`d&76D zYpWIQCvBQ{_}=k|^{s!t<6^i!WKlSCE$@S_=`4bzE zz_VGVKSF1(bP0l+XkK?6D7` literal 0 HcmV?d00001