dlg_contentstore.lua 29 KB

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