2 --Copyright (C) 2018 rubenwardy
4 --This program is free software; you can redistribute it and/or modify
5 --it under the terms of the GNU Lesser General Public License as published by
6 --the Free Software Foundation; either version 2.1 of the License, or
7 --(at your option) any later version.
9 --This program is distributed in the hope that it will be useful,
10 --but WITHOUT ANY WARRANTY; without even the implied warranty of
11 --MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 --GNU Lesser General Public License for more details.
14 --You should have received a copy of the GNU Lesser General Public License along
15 --with this program; if not, write to the Free Software Foundation, Inc.,
16 --51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 local package_dialog = {}
22 local screenshot_dir = os.tempfolder()
23 assert(core.create_dir(screenshot_dir))
24 local screenshot_downloading = {}
25 local screenshot_downloaded = {}
28 local search_string = ""
30 local num_per_page = 5
32 local filter_types_titles = {
33 fgettext("All packages"),
36 fgettext("Texture packs"),
39 local filter_types_type = {
49 local function download_package(param)
50 if core.download_file(param.package.url, param.filename) then
52 package = param.package,
53 filename = param.filename,
57 core.log("error", "downloading " .. dump(param.package.url) .. " failed")
59 package = param.package,
65 local function start_install(calling_dialog, package)
68 filename = os.tempfolder() .. "_MODNAME_" .. package.name .. ".zip",
71 local function callback(result)
72 if result.successful then
73 local path, msg = pkgmgr.install(result.package.type,
74 result.filename, result.package.name,
77 gamedata.errormessage = msg
80 local name_is_title = false
81 if result.package.type == "mod" then
82 conf_path = path .. DIR_DELIM .. "mod.conf"
83 elseif result.package.type == "game" then
84 conf_path = path .. DIR_DELIM .. "game.conf"
86 elseif result.package.type == "txp" then
87 conf_path = path .. DIR_DELIM .. "texture_pack.conf"
91 local conf = Settings(conf_path)
92 local function set_def(key, value)
93 if conf:get(key) == nil then
98 set_def("name", result.package.title)
100 set_def("title", result.package.title)
101 set_def("name", result.package.name)
103 set_def("description", result.package.short_description)
104 set_def("author", result.package.author)
105 conf:set("release", result.package.release)
109 os.remove(result.filename)
111 gamedata.errormessage = fgettext("Failed to download $1", package.name)
114 if gamedata.errormessage == nil then
115 core.button_handler({btn_hidden_close_download=result})
117 core.button_handler({btn_hidden_close_download={successful=false}})
121 if not core.handle_async(download_package, params, callback) then
122 core.log("error", "ERROR: async event failed")
123 gamedata.errormessage = fgettext("Failed to download $1", package.name)
126 local new_dlg = dialog_create("store_downloading",
128 return "size[7,2]label[0.25,0.75;" ..
129 fgettext("Downloading and installing $1, please wait...", data.title) .. "]"
131 function(this,fields)
132 if fields["btn_hidden_close_download"] ~= nil then
141 new_dlg:set_parent(calling_dialog)
142 new_dlg.data.title = package.title
143 calling_dialog:hide()
147 local function get_screenshot(package)
148 if not package.thumbnail then
149 return defaulttexturedir .. "no_screenshot.png"
150 elseif screenshot_downloading[package.thumbnail] then
151 return defaulttexturedir .. "loading_screenshot.png"
154 -- Get tmp screenshot path
155 local filepath = screenshot_dir .. DIR_DELIM ..
156 package.type .. "-" .. package.author .. "-" .. package.name .. ".png"
158 -- Return if already downloaded
159 local file = io.open(filepath, "r")
165 -- Show error if we've failed to download before
166 if screenshot_downloaded[package.thumbnail] then
167 return defaulttexturedir .. "error_screenshot.png"
172 local function download_screenshot(params)
173 return core.download_file(params.url, params.dest)
175 local function callback(success)
176 screenshot_downloading[package.thumbnail] = nil
177 screenshot_downloaded[package.thumbnail] = true
179 core.log("warning", "Screenshot download failed for some reason")
182 local ele = ui.childlist.store
183 if ele and not ele.hidden then
184 core.update_formspec(ele:formspec())
186 ele = ui.childlist.package_view
187 if ele and not ele.hidden then
188 core.update_formspec(ele:formspec())
192 if core.handle_async(download_screenshot,
193 { dest = filepath, url = package.thumbnail }, callback) then
194 screenshot_downloading[package.thumbnail] = true
196 core.log("error", "ERROR: async event failed")
197 return defaulttexturedir .. "error_screenshot.png"
200 return defaulttexturedir .. "loading_screenshot.png"
205 function package_dialog.get_formspec()
206 local package = package_dialog.package
212 "image[0,1;4.5,3;", get_screenshot(package), ']',
214 minetest.colorize(mt_color_green, core.formspec_escape(package.title)), "\n",
215 minetest.colorize('#BFBFBF', "by " .. core.formspec_escape(package.author)), "]",
216 "textarea[4,2;5.3,2;;;", core.formspec_escape(package.short_description), "]",
217 "button[0,0;2,1;back;", fgettext("Back"), "]",
220 if not package.path then
221 formspec[#formspec + 1] = "button[7,0;2,1;install;"
222 formspec[#formspec + 1] = fgettext("Install")
223 formspec[#formspec + 1] = "]"
224 elseif package.installed_release < package.release then
225 formspec[#formspec + 1] = "button[7,0;2,1;install;"
226 formspec[#formspec + 1] = fgettext("Update")
227 formspec[#formspec + 1] = "]"
228 formspec[#formspec + 1] = "button[5,0;2,1;uninstall;"
229 formspec[#formspec + 1] = fgettext("Uninstall")
230 formspec[#formspec + 1] = "]"
232 formspec[#formspec + 1] = "button[7,0;2,1;uninstall;"
233 formspec[#formspec + 1] = fgettext("Uninstall")
234 formspec[#formspec + 1] = "]"
237 return table.concat(formspec, "")
240 function package_dialog.handle_submit(this, fields, tabname, tabdata)
246 if fields.install then
247 start_install(this, package_dialog.package)
251 if fields.uninstall then
252 local dlg_delmod = create_delete_content_dlg(package_dialog.package)
253 dlg_delmod:set_parent(this)
262 function package_dialog.create(package)
263 package_dialog.package = package
264 return dialog_create("package_view",
265 package_dialog.get_formspec,
266 package_dialog.handle_submit,
270 function store.load()
271 store.packages_full = core.get_package_list()
272 store.packages = store.packages_full
276 function store.update_paths()
278 pkgmgr.refresh_globals()
279 for _, mod in pairs(pkgmgr.global_mods:get_list()) do
280 mod_hash[mod.name] = mod
284 pkgmgr.update_gamelist()
285 for _, game in pairs(pkgmgr.games) do
286 game_hash[game.id] = game
290 for _, txp in pairs(pkgmgr.get_texture_packs()) do
291 txp_hash[txp.name] = txp
294 for _, package in pairs(store.packages_full) do
296 if package.type == "mod" then
297 content = mod_hash[package.name]
298 elseif package.type == "game" then
299 content = game_hash[package.name]
300 elseif package.type == "txp" then
301 content = txp_hash[package.name]
304 if content and content.author == package.author then
305 package.path = content.path
306 package.installed_release = content.release
313 function store.filter_packages(query)
314 if query == "" and filter_type == 1 then
315 store.packages = store.packages_full
320 for word in query:lower():gmatch("%S+") do
321 table.insert(keywords, word)
324 local function matches_keywords(package, keywords)
325 for k = 1, #keywords do
326 local keyword = keywords[k]
328 if string.find(package.name:lower(), keyword, 1, true) or
329 string.find(package.title:lower(), keyword, 1, true) or
330 string.find(package.author:lower(), keyword, 1, true) or
331 string.find(package.short_description:lower(), keyword, 1, true) then
340 for _, package in pairs(store.packages_full) do
341 if (query == "" or matches_keywords(package, keywords)) and
342 (filter_type == 1 or package.type == filter_types_type[filter_type]) then
343 store.packages[#store.packages + 1] = package
349 function store.get_formspec()
354 local pages = math.ceil(#store.packages / num_per_page)
355 if cur_page > pages then
360 if #store.packages > 0 then
363 "position[0.5,0.55]",
364 "field[0.2,0.1;7.8,1;search_string;;",
365 core.formspec_escape(search_string), "]",
366 "field_close_on_enter[search_string;false]",
367 "button[7.7,-0.2;2,1;search;",
368 fgettext("Search"), "]",
369 "dropdown[9.7,-0.1;2.4;type;",
370 table.concat(filter_types_titles, ","),
371 ";", filter_type, "]",
372 -- "textlist[0,1;2.4,5.6;a;",
373 -- table.concat(taglist, ","), "]",
377 num_per_page + 1.5, "]",
378 "button[-0.1,0;3,1;back;",
379 fgettext("Back to Main Menu"), "]",
380 "button[7.1,0;1,1;pstart;<<]",
381 "button[8.1,0;1,1;pback;<]",
383 tonumber(cur_page), " / ",
384 tonumber(pages), "]",
385 "button[10.1,0;1,1;pnext;>]",
386 "button[11.1,0;1,1;pend;>>]",
392 "position[0.5,0.55]",
393 "label[4,3;No packages could be retrieved]",
397 fgettext("Back to Main Menu"), "]",
401 local start_idx = (cur_page - 1) * num_per_page + 1
402 for i=start_idx, math.min(#store.packages, start_idx+num_per_page-1) do
403 local package = store.packages[i]
404 formspec[#formspec + 1] = "container[0.5,"
405 formspec[#formspec + 1] = (i - start_idx) * 1.1 + 1
406 formspec[#formspec + 1] = "]"
409 formspec[#formspec + 1] = "image[-0.4,0;1.5,1;"
410 formspec[#formspec + 1] = core.formspec_escape(get_screenshot(package))
411 formspec[#formspec + 1] = "]"
414 formspec[#formspec + 1] = "label[1,-0.1;"
415 formspec[#formspec + 1] = core.formspec_escape(
416 minetest.colorize(mt_color_green, package.title) ..
417 minetest.colorize("#BFBFBF", " by " .. package.author))
418 formspec[#formspec + 1] = "]"
421 formspec[#formspec + 1] = "textarea[1.25,0.3;7.5,1;;;"
422 formspec[#formspec + 1] = core.formspec_escape(package.short_description)
423 formspec[#formspec + 1] = "]"
426 if not package.path then
427 formspec[#formspec + 1] = "button[8.4,0;1.5,1;install_"
428 formspec[#formspec + 1] = tostring(i)
429 formspec[#formspec + 1] = ";"
430 formspec[#formspec + 1] = fgettext("Install")
431 formspec[#formspec + 1] = "]"
432 elseif package.installed_release < package.release then
433 formspec[#formspec + 1] = "button[8.4,0;1.5,1;update_"
434 formspec[#formspec + 1] = tostring(i)
435 formspec[#formspec + 1] = ";"
436 formspec[#formspec + 1] = fgettext("Update")
437 formspec[#formspec + 1] = "]"
439 formspec[#formspec + 1] = "button[8.4,0;1.5,1;uninstall_"
440 formspec[#formspec + 1] = tostring(i)
441 formspec[#formspec + 1] = ";"
442 formspec[#formspec + 1] = fgettext("Uninstall")
443 formspec[#formspec + 1] = "]"
445 formspec[#formspec + 1] = "button[9.9,0;1.5,1;view_"
446 formspec[#formspec + 1] = tostring(i)
447 formspec[#formspec + 1] = ";"
448 formspec[#formspec + 1] = fgettext("View")
449 formspec[#formspec + 1] = "]"
451 formspec[#formspec + 1] = "container_end[]"
454 return table.concat(formspec, "")
457 function store.handle_submit(this, fields, tabname, tabdata)
458 if fields.search or fields.key_enter_field == "search_string" then
459 search_string = fields.search_string:trim()
461 store.filter_packages(search_string)
462 core.update_formspec(store.get_formspec())
471 if fields.pstart then
473 core.update_formspec(store.get_formspec())
478 cur_page = math.ceil(#store.packages / num_per_page)
479 core.update_formspec(store.get_formspec())
484 cur_page = cur_page + 1
485 local pages = math.ceil(#store.packages / num_per_page)
486 if cur_page > pages then
489 core.update_formspec(store.get_formspec())
494 if cur_page == 1 then
495 local pages = math.ceil(#store.packages / num_per_page)
498 cur_page = cur_page - 1
500 core.update_formspec(store.get_formspec())
505 local new_type = table.indexof(filter_types_titles, fields.type)
506 if new_type ~= filter_type then
507 filter_type = new_type
508 store.filter_packages(search_string)
513 local start_idx = (cur_page - 1) * num_per_page + 1
514 assert(start_idx ~= nil)
515 for i=start_idx, math.min(#store.packages, start_idx+num_per_page-1) do
516 local package = store.packages[i]
519 if fields["install_" .. i] then
520 start_install(this, package)
524 if fields["uninstall_" .. i] then
525 local dlg_delmod = create_delete_content_dlg(package)
526 dlg_delmod:set_parent(this)
532 if fields["view_" .. i] then
533 local dlg = package_dialog.create(package)
544 function create_store_dlg(type)
545 if not store.loaded then
551 store.filter_packages(search_string)
553 return dialog_create("store",