dlg_contentstore.lua 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  1. --Minetest
  2. --Copyright (C) 2018-20 rubenwardy
  3. --
  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.
  8. --
  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.
  13. --
  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.
  17. if not core.get_http_api then
  18. function create_store_dlg()
  19. return messagebox("store",
  20. fgettext("ContentDB is not available when Minetest was compiled without cURL"))
  21. end
  22. return
  23. end
  24. -- Unordered preserves the original order of the ContentDB API,
  25. -- before the package list is ordered based on installed state.
  26. local store = { packages = {}, packages_full = {}, packages_full_unordered = {} }
  27. local http = core.get_http_api()
  28. -- Screenshot
  29. local screenshot_dir = core.get_cache_path() .. DIR_DELIM .. "cdb"
  30. assert(core.create_dir(screenshot_dir))
  31. local screenshot_downloading = {}
  32. local screenshot_downloaded = {}
  33. -- Filter
  34. local search_string = ""
  35. local cur_page = 1
  36. local num_per_page = 5
  37. local filter_type = 1
  38. local filter_types_titles = {
  39. fgettext("All packages"),
  40. fgettext("Games"),
  41. fgettext("Mods"),
  42. fgettext("Texture packs"),
  43. }
  44. local number_downloading = 0
  45. local download_queue = {}
  46. local filter_types_type = {
  47. nil,
  48. "game",
  49. "mod",
  50. "txp",
  51. }
  52. local function download_package(param)
  53. if core.download_file(param.package.url, param.filename) then
  54. return {
  55. filename = param.filename,
  56. successful = true,
  57. }
  58. else
  59. core.log("error", "downloading " .. dump(param.package.url) .. " failed")
  60. return {
  61. successful = false,
  62. }
  63. end
  64. end
  65. local function start_install(package)
  66. local params = {
  67. package = package,
  68. filename = os.tempfolder() .. "_MODNAME_" .. package.name .. ".zip",
  69. }
  70. number_downloading = number_downloading + 1
  71. local function callback(result)
  72. if result.successful then
  73. local path, msg = pkgmgr.install(package.type,
  74. result.filename, package.name,
  75. package.path)
  76. if not path then
  77. gamedata.errormessage = msg
  78. else
  79. core.log("action", "Installed package to " .. path)
  80. local conf_path
  81. local name_is_title = false
  82. if package.type == "mod" then
  83. local actual_type = pkgmgr.get_folder_type(path)
  84. if actual_type.type == "modpack" then
  85. conf_path = path .. DIR_DELIM .. "modpack.conf"
  86. else
  87. conf_path = path .. DIR_DELIM .. "mod.conf"
  88. end
  89. elseif package.type == "game" then
  90. conf_path = path .. DIR_DELIM .. "game.conf"
  91. name_is_title = true
  92. elseif package.type == "txp" then
  93. conf_path = path .. DIR_DELIM .. "texture_pack.conf"
  94. end
  95. if conf_path then
  96. local conf = Settings(conf_path)
  97. if name_is_title then
  98. conf:set("name", package.title)
  99. else
  100. conf:set("title", package.title)
  101. conf:set("name", package.name)
  102. end
  103. if not conf:get("description") then
  104. conf:set("description", package.short_description)
  105. end
  106. conf:set("author", package.author)
  107. conf:set("release", package.release)
  108. conf:write()
  109. end
  110. end
  111. os.remove(result.filename)
  112. else
  113. gamedata.errormessage = fgettext("Failed to download $1", package.name)
  114. end
  115. package.downloading = false
  116. number_downloading = number_downloading - 1
  117. local next = download_queue[1]
  118. if next then
  119. table.remove(download_queue, 1)
  120. start_install(next)
  121. end
  122. ui.update()
  123. end
  124. package.queued = false
  125. package.downloading = true
  126. if not core.handle_async(download_package, params, callback) then
  127. core.log("error", "ERROR: async event failed")
  128. gamedata.errormessage = fgettext("Failed to download $1", package.name)
  129. return
  130. end
  131. end
  132. local function queue_download(package)
  133. local max_concurrent_downloads = tonumber(core.settings:get("contentdb_max_concurrent_downloads"))
  134. if number_downloading < max_concurrent_downloads then
  135. start_install(package)
  136. else
  137. table.insert(download_queue, package)
  138. package.queued = true
  139. end
  140. end
  141. local function get_raw_dependencies(package)
  142. if package.raw_deps then
  143. return package.raw_deps
  144. end
  145. local url_fmt = "/api/packages/%s/dependencies/?only_hard=1&protocol_version=%s&engine_version=%s"
  146. local version = core.get_version()
  147. local base_url = core.settings:get("contentdb_url")
  148. local url = base_url .. url_fmt:format(package.id, core.get_max_supp_proto(), version.string)
  149. local response = http.fetch_sync({ url = url })
  150. if not response.succeeded then
  151. return
  152. end
  153. local data = core.parse_json(response.data) or {}
  154. local content_lookup = {}
  155. for _, pkg in pairs(store.packages_full) do
  156. content_lookup[pkg.id] = pkg
  157. end
  158. for id, raw_deps in pairs(data) do
  159. local package2 = content_lookup[id:lower()]
  160. if package2 and not package2.raw_deps then
  161. package2.raw_deps = raw_deps
  162. for _, dep in pairs(raw_deps) do
  163. local packages = {}
  164. for i=1, #dep.packages do
  165. packages[#packages + 1] = content_lookup[dep.packages[i]:lower()]
  166. end
  167. dep.packages = packages
  168. end
  169. end
  170. end
  171. return package.raw_deps
  172. end
  173. local function has_hard_deps(raw_deps)
  174. for i=1, #raw_deps do
  175. if not raw_deps[i].is_optional then
  176. return true
  177. end
  178. end
  179. return false
  180. end
  181. -- Recursively resolve dependencies, given the installed mods
  182. local function resolve_dependencies_2(raw_deps, installed_mods, out)
  183. local function resolve_dep(dep)
  184. -- Check whether it's already installed
  185. if installed_mods[dep.name] then
  186. return {
  187. is_optional = dep.is_optional,
  188. name = dep.name,
  189. installed = true,
  190. }
  191. end
  192. -- Find exact name matches
  193. local fallback
  194. for _, package in pairs(dep.packages) do
  195. if package.type ~= "game" then
  196. if package.name == dep.name then
  197. return {
  198. is_optional = dep.is_optional,
  199. name = dep.name,
  200. installed = false,
  201. package = package,
  202. }
  203. elseif not fallback then
  204. fallback = package
  205. end
  206. end
  207. end
  208. -- Otherwise, find the first mod that fulfils it
  209. if fallback then
  210. return {
  211. is_optional = dep.is_optional,
  212. name = dep.name,
  213. installed = false,
  214. package = fallback,
  215. }
  216. end
  217. return {
  218. is_optional = dep.is_optional,
  219. name = dep.name,
  220. installed = false,
  221. }
  222. end
  223. for _, dep in pairs(raw_deps) do
  224. if not dep.is_optional and not out[dep.name] then
  225. local result = resolve_dep(dep)
  226. out[dep.name] = result
  227. if result and result.package and not result.installed then
  228. local raw_deps2 = get_raw_dependencies(result.package)
  229. if raw_deps2 then
  230. resolve_dependencies_2(raw_deps2, installed_mods, out)
  231. end
  232. end
  233. end
  234. end
  235. return true
  236. end
  237. -- Resolve dependencies for a package, calls the recursive version.
  238. local function resolve_dependencies(raw_deps, game)
  239. assert(game)
  240. local installed_mods = {}
  241. local mods = {}
  242. pkgmgr.get_game_mods(game, mods)
  243. for _, mod in pairs(mods) do
  244. installed_mods[mod.name] = true
  245. end
  246. for _, mod in pairs(pkgmgr.global_mods:get_list()) do
  247. installed_mods[mod.name] = true
  248. end
  249. local out = {}
  250. if not resolve_dependencies_2(raw_deps, installed_mods, out) then
  251. return nil
  252. end
  253. local retval = {}
  254. for _, dep in pairs(out) do
  255. retval[#retval + 1] = dep
  256. end
  257. table.sort(retval, function(a, b)
  258. return a.name < b.name
  259. end)
  260. return retval
  261. end
  262. local install_dialog = {}
  263. function install_dialog.get_formspec()
  264. local package = install_dialog.package
  265. local raw_deps = install_dialog.raw_deps
  266. local will_install_deps = install_dialog.will_install_deps
  267. local selected_game_idx = 1
  268. local selected_gameid = core.settings:get("menu_last_game")
  269. local games = table.copy(pkgmgr.games)
  270. for i=1, #games do
  271. if selected_gameid and games[i].id == selected_gameid then
  272. selected_game_idx = i
  273. end
  274. games[i] = core.formspec_escape(games[i].name)
  275. end
  276. local selected_game = pkgmgr.games[selected_game_idx]
  277. local deps_to_install = 0
  278. local deps_not_found = 0
  279. install_dialog.dependencies = resolve_dependencies(raw_deps, selected_game)
  280. local formatted_deps = {}
  281. for _, dep in pairs(install_dialog.dependencies) do
  282. formatted_deps[#formatted_deps + 1] = "#fff"
  283. formatted_deps[#formatted_deps + 1] = core.formspec_escape(dep.name)
  284. if dep.installed then
  285. formatted_deps[#formatted_deps + 1] = "#ccf"
  286. formatted_deps[#formatted_deps + 1] = fgettext("Already installed")
  287. elseif dep.package then
  288. formatted_deps[#formatted_deps + 1] = "#cfc"
  289. formatted_deps[#formatted_deps + 1] = fgettext("$1 by $2", dep.package.title, dep.package.author)
  290. deps_to_install = deps_to_install + 1
  291. else
  292. formatted_deps[#formatted_deps + 1] = "#f00"
  293. formatted_deps[#formatted_deps + 1] = fgettext("Not found")
  294. deps_not_found = deps_not_found + 1
  295. end
  296. end
  297. local message_bg = "#3333"
  298. local message
  299. if will_install_deps then
  300. message = fgettext("$1 and $2 dependencies will be installed.", package.title, deps_to_install)
  301. else
  302. message = fgettext("$1 will be installed, and $2 dependencies will be skipped.", package.title, deps_to_install)
  303. end
  304. if deps_not_found > 0 then
  305. message = fgettext("$1 required dependencies could not be found.", deps_not_found) ..
  306. " " .. fgettext("Please check that the base game is correct.", deps_not_found) ..
  307. "\n" .. message
  308. message_bg = mt_color_orange
  309. end
  310. local formspec = {
  311. "formspec_version[3]",
  312. "size[7,7.85]",
  313. "style[title;border=false]",
  314. "box[0,0;7,0.5;#3333]",
  315. "button[0,0;7,0.5;title;", fgettext("Install $1", package.title) , "]",
  316. "container[0.375,0.70]",
  317. "label[0,0.25;", fgettext("Base Game:"), "]",
  318. "dropdown[2,0;4.25,0.5;gameid;", table.concat(games, ","), ";", selected_game_idx, "]",
  319. "label[0,0.8;", fgettext("Dependencies:"), "]",
  320. "tablecolumns[color;text;color;text]",
  321. "table[0,1.1;6.25,3;packages;", table.concat(formatted_deps, ","), "]",
  322. "container_end[]",
  323. "checkbox[0.375,5.1;will_install_deps;",
  324. fgettext("Install missing dependencies"), ";",
  325. will_install_deps and "true" or "false", "]",
  326. "box[0,5.4;7,1.2;", message_bg, "]",
  327. "textarea[0.375,5.5;6.25,1;;;", message, "]",
  328. "container[1.375,6.85]",
  329. "button[0,0;2,0.8;install_all;", fgettext("Install"), "]",
  330. "button[2.25,0;2,0.8;cancel;", fgettext("Cancel"), "]",
  331. "container_end[]",
  332. }
  333. return table.concat(formspec, "")
  334. end
  335. function install_dialog.handle_submit(this, fields)
  336. if fields.cancel then
  337. this:delete()
  338. return true
  339. end
  340. if fields.will_install_deps ~= nil then
  341. install_dialog.will_install_deps = core.is_yes(fields.will_install_deps)
  342. return true
  343. end
  344. if fields.install_all then
  345. queue_download(install_dialog.package)
  346. if install_dialog.will_install_deps then
  347. for _, dep in pairs(install_dialog.dependencies) do
  348. if not dep.is_optional and not dep.installed and dep.package then
  349. queue_download(dep.package)
  350. end
  351. end
  352. end
  353. this:delete()
  354. return true
  355. end
  356. if fields.gameid then
  357. for _, game in pairs(pkgmgr.games) do
  358. if game.name == fields.gameid then
  359. core.settings:set("menu_last_game", game.id)
  360. break
  361. end
  362. end
  363. return true
  364. end
  365. return false
  366. end
  367. function install_dialog.create(package, raw_deps)
  368. install_dialog.dependencies = nil
  369. install_dialog.package = package
  370. install_dialog.raw_deps = raw_deps
  371. install_dialog.will_install_deps = true
  372. return dialog_create("install_dialog",
  373. install_dialog.get_formspec,
  374. install_dialog.handle_submit,
  375. nil)
  376. end
  377. local confirm_overwrite = {}
  378. function confirm_overwrite.get_formspec()
  379. local package = confirm_overwrite.package
  380. return "size[11.5,4.5,true]" ..
  381. "label[2,2;" ..
  382. fgettext("\"$1\" already exists. Would you like to overwrite it?", package.name) .. "]"..
  383. "style[install;bgcolor=red]" ..
  384. "button[3.25,3.5;2.5,0.5;install;" .. fgettext("Overwrite") .. "]" ..
  385. "button[5.75,3.5;2.5,0.5;cancel;" .. fgettext("Cancel") .. "]"
  386. end
  387. function confirm_overwrite.handle_submit(this, fields)
  388. if fields.cancel then
  389. this:delete()
  390. return true
  391. end
  392. if fields.install then
  393. this:delete()
  394. confirm_overwrite.callback()
  395. return true
  396. end
  397. return false
  398. end
  399. function confirm_overwrite.create(package, callback)
  400. assert(type(package) == "table")
  401. assert(type(callback) == "function")
  402. confirm_overwrite.package = package
  403. confirm_overwrite.callback = callback
  404. return dialog_create("confirm_overwrite",
  405. confirm_overwrite.get_formspec,
  406. confirm_overwrite.handle_submit,
  407. nil)
  408. end
  409. local function get_file_extension(path)
  410. local parts = path:split(".")
  411. return parts[#parts]
  412. end
  413. local function get_screenshot(package)
  414. if not package.thumbnail then
  415. return defaulttexturedir .. "no_screenshot.png"
  416. elseif screenshot_downloading[package.thumbnail] then
  417. return defaulttexturedir .. "loading_screenshot.png"
  418. end
  419. -- Get tmp screenshot path
  420. local ext = get_file_extension(package.thumbnail)
  421. local filepath = screenshot_dir .. DIR_DELIM ..
  422. ("%s-%s-%s.%s"):format(package.type, package.author, package.name, ext)
  423. -- Return if already downloaded
  424. local file = io.open(filepath, "r")
  425. if file then
  426. file:close()
  427. return filepath
  428. end
  429. -- Show error if we've failed to download before
  430. if screenshot_downloaded[package.thumbnail] then
  431. return defaulttexturedir .. "error_screenshot.png"
  432. end
  433. -- Download
  434. local function download_screenshot(params)
  435. return core.download_file(params.url, params.dest)
  436. end
  437. local function callback(success)
  438. screenshot_downloading[package.thumbnail] = nil
  439. screenshot_downloaded[package.thumbnail] = true
  440. if not success then
  441. core.log("warning", "Screenshot download failed for some reason")
  442. end
  443. ui.update()
  444. end
  445. if core.handle_async(download_screenshot,
  446. { dest = filepath, url = package.thumbnail }, callback) then
  447. screenshot_downloading[package.thumbnail] = true
  448. else
  449. core.log("error", "ERROR: async event failed")
  450. return defaulttexturedir .. "error_screenshot.png"
  451. end
  452. return defaulttexturedir .. "loading_screenshot.png"
  453. end
  454. function store.load()
  455. local version = core.get_version()
  456. local base_url = core.settings:get("contentdb_url")
  457. local url = base_url ..
  458. "/api/packages/?type=mod&type=game&type=txp&protocol_version=" ..
  459. core.get_max_supp_proto() .. "&engine_version=" .. version.string
  460. for _, item in pairs(core.settings:get("contentdb_flag_blacklist"):split(",")) do
  461. item = item:trim()
  462. if item ~= "" then
  463. url = url .. "&hide=" .. item
  464. end
  465. end
  466. local timeout = tonumber(core.settings:get("curl_file_download_timeout"))
  467. local response = http.fetch_sync({ url = url, timeout = timeout })
  468. if not response.succeeded then
  469. return
  470. end
  471. store.packages_full = core.parse_json(response.data) or {}
  472. for _, package in pairs(store.packages_full) do
  473. package.url = base_url .. "/packages/" ..
  474. package.author .. "/" .. package.name ..
  475. "/releases/" .. package.release .. "/download/"
  476. local name_len = #package.name
  477. if package.type == "game" and name_len > 5 and package.name:sub(name_len - 4) == "_game" then
  478. package.id = package.author:lower() .. "/" .. package.name:sub(1, name_len - 5)
  479. else
  480. package.id = package.author:lower() .. "/" .. package.name
  481. end
  482. end
  483. store.packages_full_unordered = store.packages_full
  484. store.packages = store.packages_full
  485. store.loaded = true
  486. end
  487. function store.update_paths()
  488. local mod_hash = {}
  489. pkgmgr.refresh_globals()
  490. for _, mod in pairs(pkgmgr.global_mods:get_list()) do
  491. if mod.author and mod.release > 0 then
  492. mod_hash[mod.author:lower() .. "/" .. mod.name] = mod
  493. end
  494. end
  495. local game_hash = {}
  496. pkgmgr.update_gamelist()
  497. for _, game in pairs(pkgmgr.games) do
  498. if game.author ~= "" and game.release > 0 then
  499. game_hash[game.author:lower() .. "/" .. game.id] = game
  500. end
  501. end
  502. local txp_hash = {}
  503. for _, txp in pairs(pkgmgr.get_texture_packs()) do
  504. if txp.author and txp.release > 0 then
  505. txp_hash[txp.author:lower() .. "/" .. txp.name] = txp
  506. end
  507. end
  508. for _, package in pairs(store.packages_full) do
  509. local content
  510. if package.type == "mod" then
  511. content = mod_hash[package.id]
  512. elseif package.type == "game" then
  513. content = game_hash[package.id]
  514. elseif package.type == "txp" then
  515. content = txp_hash[package.id]
  516. end
  517. if content then
  518. package.path = content.path
  519. package.installed_release = content.release or 0
  520. else
  521. package.path = nil
  522. end
  523. end
  524. end
  525. function store.sort_packages()
  526. local ret = {}
  527. -- Add installed content
  528. for i=1, #store.packages_full_unordered do
  529. local package = store.packages_full_unordered[i]
  530. if package.path then
  531. ret[#ret + 1] = package
  532. end
  533. end
  534. -- Sort installed content by title
  535. table.sort(ret, function(a, b)
  536. return a.title < b.title
  537. end)
  538. -- Add uninstalled content
  539. for i=1, #store.packages_full_unordered do
  540. local package = store.packages_full_unordered[i]
  541. if not package.path then
  542. ret[#ret + 1] = package
  543. end
  544. end
  545. store.packages_full = ret
  546. end
  547. function store.filter_packages(query)
  548. if query == "" and filter_type == 1 then
  549. store.packages = store.packages_full
  550. return
  551. end
  552. local keywords = {}
  553. for word in query:lower():gmatch("%S+") do
  554. table.insert(keywords, word)
  555. end
  556. local function matches_keywords(package)
  557. for k = 1, #keywords do
  558. local keyword = keywords[k]
  559. if string.find(package.name:lower(), keyword, 1, true) or
  560. string.find(package.title:lower(), keyword, 1, true) or
  561. string.find(package.author:lower(), keyword, 1, true) or
  562. string.find(package.short_description:lower(), keyword, 1, true) then
  563. return true
  564. end
  565. end
  566. return false
  567. end
  568. store.packages = {}
  569. for _, package in pairs(store.packages_full) do
  570. if (query == "" or matches_keywords(package)) and
  571. (filter_type == 1 or package.type == filter_types_type[filter_type]) then
  572. store.packages[#store.packages + 1] = package
  573. end
  574. end
  575. end
  576. function store.get_formspec(dlgdata)
  577. store.update_paths()
  578. dlgdata.pagemax = math.max(math.ceil(#store.packages / num_per_page), 1)
  579. if cur_page > dlgdata.pagemax then
  580. cur_page = 1
  581. end
  582. local W = 15.75
  583. local H = 9.5
  584. local formspec
  585. if #store.packages_full > 0 then
  586. formspec = {
  587. "formspec_version[3]",
  588. "size[15.75,9.5]",
  589. "position[0.5,0.55]",
  590. "style[status,downloading,queued;border=false]",
  591. "container[0.375,0.375]",
  592. "field[0,0;7.225,0.8;search_string;;", core.formspec_escape(search_string), "]",
  593. "field_close_on_enter[search_string;false]",
  594. "image_button[7.3,0;0.8,0.8;", core.formspec_escape(defaulttexturedir .. "search.png"), ";search;]",
  595. "image_button[8.125,0;0.8,0.8;", core.formspec_escape(defaulttexturedir .. "clear.png"), ";clear;]",
  596. "dropdown[9.6,0;2.4,0.8;type;", table.concat(filter_types_titles, ","), ";", filter_type, "]",
  597. "container_end[]",
  598. -- Page nav buttons
  599. "container[0,", H - 0.8 - 0.375, "]",
  600. "button[0.375,0;4,0.8;back;", fgettext("Back to Main Menu"), "]",
  601. "container[", W - 0.375 - 0.8*4 - 2, ",0]",
  602. "image_button[0,0;0.8,0.8;", core.formspec_escape(defaulttexturedir), "start_icon.png;pstart;]",
  603. "image_button[0.8,0;0.8,0.8;", core.formspec_escape(defaulttexturedir), "prev_icon.png;pback;]",
  604. "style[pagenum;border=false]",
  605. "button[1.6,0;2,0.8;pagenum;", tonumber(cur_page), " / ", tonumber(dlgdata.pagemax), "]",
  606. "image_button[3.6,0;0.8,0.8;", core.formspec_escape(defaulttexturedir), "next_icon.png;pnext;]",
  607. "image_button[4.4,0;0.8,0.8;", core.formspec_escape(defaulttexturedir), "end_icon.png;pend;]",
  608. "container_end[]",
  609. "container_end[]",
  610. }
  611. if number_downloading > 0 then
  612. formspec[#formspec + 1] = "button[12.75,0.375;2.625,0.8;downloading;"
  613. if #download_queue > 0 then
  614. formspec[#formspec + 1] = fgettext("$1 downloading,\n$2 queued", number_downloading, #download_queue)
  615. else
  616. formspec[#formspec + 1] = fgettext("$1 downloading...", number_downloading)
  617. end
  618. formspec[#formspec + 1] = "]"
  619. else
  620. local num_avail_updates = 0
  621. for i=1, #store.packages_full do
  622. local package = store.packages_full[i]
  623. if package.path and package.installed_release < package.release and
  624. not (package.downloading or package.queued) then
  625. num_avail_updates = num_avail_updates + 1
  626. end
  627. end
  628. if num_avail_updates == 0 then
  629. formspec[#formspec + 1] = "button[12.75,0.375;2.625,0.8;status;"
  630. formspec[#formspec + 1] = fgettext("No updates")
  631. formspec[#formspec + 1] = "]"
  632. else
  633. formspec[#formspec + 1] = "button[12.75,0.375;2.625,0.8;update_all;"
  634. formspec[#formspec + 1] = fgettext("Update All [$1]", num_avail_updates)
  635. formspec[#formspec + 1] = "]"
  636. end
  637. end
  638. if #store.packages == 0 then
  639. formspec[#formspec + 1] = "label[4,3;"
  640. formspec[#formspec + 1] = fgettext("No results")
  641. formspec[#formspec + 1] = "]"
  642. end
  643. else
  644. formspec = {
  645. "size[12,7]",
  646. "position[0.5,0.55]",
  647. "label[4,3;", fgettext("No packages could be retrieved"), "]",
  648. "container[0,", H - 0.8 - 0.375, "]",
  649. "button[0,0;4,0.8;back;", fgettext("Back to Main Menu"), "]",
  650. "container_end[]",
  651. }
  652. end
  653. -- download/queued tooltips always have the same message
  654. local tooltip_colors = ";#dff6f5;#302c2e]"
  655. formspec[#formspec + 1] = "tooltip[downloading;" .. fgettext("Downloading...") .. tooltip_colors
  656. formspec[#formspec + 1] = "tooltip[queued;" .. fgettext("Queued") .. tooltip_colors
  657. local start_idx = (cur_page - 1) * num_per_page + 1
  658. for i=start_idx, math.min(#store.packages, start_idx+num_per_page-1) do
  659. local package = store.packages[i]
  660. local container_y = (i - start_idx) * 1.375 + (2*0.375 + 0.8)
  661. formspec[#formspec + 1] = "container[0.375,"
  662. formspec[#formspec + 1] = container_y
  663. formspec[#formspec + 1] = "]"
  664. -- image
  665. formspec[#formspec + 1] = "image[0,0;1.5,1;"
  666. formspec[#formspec + 1] = core.formspec_escape(get_screenshot(package))
  667. formspec[#formspec + 1] = "]"
  668. -- title
  669. formspec[#formspec + 1] = "label[1.875,0.1;"
  670. formspec[#formspec + 1] = core.formspec_escape(
  671. core.colorize(mt_color_green, package.title) ..
  672. core.colorize("#BFBFBF", " by " .. package.author))
  673. formspec[#formspec + 1] = "]"
  674. -- buttons
  675. local left_base = "image_button[-1.55,0;0.7,0.7;" .. core.formspec_escape(defaulttexturedir)
  676. formspec[#formspec + 1] = "container["
  677. formspec[#formspec + 1] = W - 0.375*2
  678. formspec[#formspec + 1] = ",0.1]"
  679. if package.downloading then
  680. formspec[#formspec + 1] = "animated_image[-1.7,-0.15;1,1;downloading;"
  681. formspec[#formspec + 1] = core.formspec_escape(defaulttexturedir)
  682. formspec[#formspec + 1] = "cdb_downloading.png;3;400;]"
  683. elseif package.queued then
  684. formspec[#formspec + 1] = left_base
  685. formspec[#formspec + 1] = core.formspec_escape(defaulttexturedir)
  686. formspec[#formspec + 1] = "cdb_queued.png;queued]"
  687. elseif not package.path then
  688. local elem_name = "install_" .. i .. ";"
  689. formspec[#formspec + 1] = "style[" .. elem_name .. "bgcolor=#71aa34]"
  690. formspec[#formspec + 1] = left_base .. "cdb_add.png;" .. elem_name .. "]"
  691. formspec[#formspec + 1] = "tooltip[" .. elem_name .. fgettext("Install") .. tooltip_colors
  692. else
  693. if package.installed_release < package.release then
  694. -- The install_ action also handles updating
  695. local elem_name = "install_" .. i .. ";"
  696. formspec[#formspec + 1] = "style[" .. elem_name .. "bgcolor=#28ccdf]"
  697. formspec[#formspec + 1] = left_base .. "cdb_update.png;" .. elem_name .. "]"
  698. formspec[#formspec + 1] = "tooltip[" .. elem_name .. fgettext("Update") .. tooltip_colors
  699. else
  700. local elem_name = "uninstall_" .. i .. ";"
  701. formspec[#formspec + 1] = "style[" .. elem_name .. "bgcolor=#a93b3b]"
  702. formspec[#formspec + 1] = left_base .. "cdb_clear.png;" .. elem_name .. "]"
  703. formspec[#formspec + 1] = "tooltip[" .. elem_name .. fgettext("Uninstall") .. tooltip_colors
  704. end
  705. end
  706. local web_elem_name = "view_" .. i .. ";"
  707. formspec[#formspec + 1] = "image_button[-0.7,0;0.7,0.7;" ..
  708. core.formspec_escape(defaulttexturedir) .. "cdb_viewonline.png;" .. web_elem_name .. "]"
  709. formspec[#formspec + 1] = "tooltip[" .. web_elem_name ..
  710. fgettext("View more information in a web browser") .. tooltip_colors
  711. formspec[#formspec + 1] = "container_end[]"
  712. -- description
  713. local description_width = W - 0.375*5 - 0.85 - 2*0.7
  714. formspec[#formspec + 1] = "textarea[1.855,0.3;"
  715. formspec[#formspec + 1] = tostring(description_width)
  716. formspec[#formspec + 1] = ",0.8;;;"
  717. formspec[#formspec + 1] = core.formspec_escape(package.short_description)
  718. formspec[#formspec + 1] = "]"
  719. formspec[#formspec + 1] = "container_end[]"
  720. end
  721. return table.concat(formspec, "")
  722. end
  723. function store.handle_submit(this, fields)
  724. if fields.search or fields.key_enter_field == "search_string" then
  725. search_string = fields.search_string:trim()
  726. cur_page = 1
  727. store.filter_packages(search_string)
  728. return true
  729. end
  730. if fields.clear then
  731. search_string = ""
  732. cur_page = 1
  733. store.filter_packages("")
  734. return true
  735. end
  736. if fields.back then
  737. this:delete()
  738. return true
  739. end
  740. if fields.pstart then
  741. cur_page = 1
  742. return true
  743. end
  744. if fields.pend then
  745. cur_page = this.data.pagemax
  746. return true
  747. end
  748. if fields.pnext then
  749. cur_page = cur_page + 1
  750. if cur_page > this.data.pagemax then
  751. cur_page = 1
  752. end
  753. return true
  754. end
  755. if fields.pback then
  756. if cur_page == 1 then
  757. cur_page = this.data.pagemax
  758. else
  759. cur_page = cur_page - 1
  760. end
  761. return true
  762. end
  763. if fields.type then
  764. local new_type = table.indexof(filter_types_titles, fields.type)
  765. if new_type ~= filter_type then
  766. filter_type = new_type
  767. store.filter_packages(search_string)
  768. return true
  769. end
  770. end
  771. if fields.update_all then
  772. for i=1, #store.packages_full do
  773. local package = store.packages_full[i]
  774. if package.path and package.installed_release < package.release and
  775. not (package.downloading or package.queued) then
  776. queue_download(package)
  777. end
  778. end
  779. return true
  780. end
  781. local start_idx = (cur_page - 1) * num_per_page + 1
  782. assert(start_idx ~= nil)
  783. for i=start_idx, math.min(#store.packages, start_idx+num_per_page-1) do
  784. local package = store.packages[i]
  785. assert(package)
  786. if fields["install_" .. i] then
  787. local install_parent
  788. if package.type == "mod" then
  789. install_parent = core.get_modpath()
  790. elseif package.type == "game" then
  791. install_parent = core.get_gamepath()
  792. elseif package.type == "txp" then
  793. install_parent = core.get_texturepath()
  794. else
  795. error("Unknown package type: " .. package.type)
  796. end
  797. local function on_confirm()
  798. local deps = get_raw_dependencies(package)
  799. if deps and has_hard_deps(deps) then
  800. local dlg = install_dialog.create(package, deps)
  801. dlg:set_parent(this)
  802. this:hide()
  803. dlg:show()
  804. else
  805. queue_download(package)
  806. end
  807. end
  808. if not package.path and core.is_dir(install_parent .. DIR_DELIM .. package.name) then
  809. local dlg = confirm_overwrite.create(package, on_confirm)
  810. dlg:set_parent(this)
  811. this:hide()
  812. dlg:show()
  813. else
  814. on_confirm()
  815. end
  816. return true
  817. end
  818. if fields["uninstall_" .. i] then
  819. local dlg = create_delete_content_dlg(package)
  820. dlg:set_parent(this)
  821. this:hide()
  822. dlg:show()
  823. return true
  824. end
  825. if fields["view_" .. i] then
  826. local url = ("%s/packages/%s/%s?protocol_version=%d"):format(
  827. core.settings:get("contentdb_url"),
  828. package.author, package.name, core.get_max_supp_proto())
  829. core.open_url(url)
  830. return true
  831. end
  832. end
  833. return false
  834. end
  835. function create_store_dlg(type)
  836. if not store.loaded or #store.packages_full == 0 then
  837. store.load()
  838. end
  839. store.update_paths()
  840. store.sort_packages()
  841. search_string = ""
  842. cur_page = 1
  843. if type then
  844. -- table.indexof does not work on tables that contain `nil`
  845. for i, v in pairs(filter_types_type) do
  846. if v == type then
  847. filter_type = i
  848. break
  849. end
  850. end
  851. end
  852. store.filter_packages(search_string)
  853. return dialog_create("store",
  854. store.get_formspec,
  855. store.handle_submit,
  856. nil)
  857. end