pkgmgr.lua 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. --Minetest
  2. --Copyright (C) 2013 sapier
  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. --------------------------------------------------------------------------------
  18. local function get_last_folder(text,count)
  19. local parts = text:split(DIR_DELIM)
  20. if count == nil then
  21. return parts[#parts]
  22. end
  23. local retval = ""
  24. for i=1,count,1 do
  25. retval = retval .. parts[#parts - (count-i)] .. DIR_DELIM
  26. end
  27. return retval
  28. end
  29. local function cleanup_path(temppath)
  30. local parts = temppath:split("-")
  31. temppath = ""
  32. for i=1,#parts,1 do
  33. if temppath ~= "" then
  34. temppath = temppath .. "_"
  35. end
  36. temppath = temppath .. parts[i]
  37. end
  38. parts = temppath:split(".")
  39. temppath = ""
  40. for i=1,#parts,1 do
  41. if temppath ~= "" then
  42. temppath = temppath .. "_"
  43. end
  44. temppath = temppath .. parts[i]
  45. end
  46. parts = temppath:split("'")
  47. temppath = ""
  48. for i=1,#parts,1 do
  49. if temppath ~= "" then
  50. temppath = temppath .. ""
  51. end
  52. temppath = temppath .. parts[i]
  53. end
  54. parts = temppath:split(" ")
  55. temppath = ""
  56. for i=1,#parts,1 do
  57. if temppath ~= "" then
  58. temppath = temppath
  59. end
  60. temppath = temppath .. parts[i]
  61. end
  62. return temppath
  63. end
  64. local function load_texture_packs(txtpath, retval)
  65. local list = core.get_dir_list(txtpath, true)
  66. local current_texture_path = core.settings:get("texture_path")
  67. for _, item in ipairs(list) do
  68. if item ~= "base" then
  69. local path = txtpath .. DIR_DELIM .. item .. DIR_DELIM
  70. local conf = Settings(path .. "texture_pack.conf")
  71. local enabled = path == current_texture_path
  72. local title = conf:get("title") or item
  73. -- list_* is only used if non-nil, else the regular versions are used.
  74. retval[#retval + 1] = {
  75. name = item,
  76. title = title,
  77. list_name = enabled and fgettext("$1 (Enabled)", item) or nil,
  78. list_title = enabled and fgettext("$1 (Enabled)", title) or nil,
  79. author = conf:get("author"),
  80. release = tonumber(conf:get("release")) or 0,
  81. type = "txp",
  82. path = path,
  83. enabled = enabled,
  84. }
  85. end
  86. end
  87. end
  88. --modmanager implementation
  89. pkgmgr = {}
  90. --- Scans a directory recursively for mods and adds them to `listing`
  91. -- @param path Absolute directory path to scan recursively
  92. -- @param virtual_path Prettified unique path (e.g. "mods", "mods/mt_modpack")
  93. -- @param listing Input. Flat array to insert located mods and modpacks
  94. -- @param modpack Currently processing modpack or nil/"" if none (recursion)
  95. function pkgmgr.get_mods(path, virtual_path, listing, modpack)
  96. local mods = core.get_dir_list(path, true)
  97. for _, name in ipairs(mods) do
  98. if name:sub(1, 1) ~= "." then
  99. local mod_path = path .. DIR_DELIM .. name
  100. local mod_virtual_path = virtual_path .. "/" .. name
  101. local toadd = {
  102. dir_name = name,
  103. parent_dir = path,
  104. }
  105. listing[#listing + 1] = toadd
  106. -- Get config file
  107. local mod_conf
  108. local modpack_conf = io.open(mod_path .. DIR_DELIM .. "modpack.conf")
  109. if modpack_conf then
  110. toadd.is_modpack = true
  111. modpack_conf:close()
  112. mod_conf = Settings(mod_path .. DIR_DELIM .. "modpack.conf"):to_table()
  113. if mod_conf.name then
  114. name = mod_conf.name
  115. toadd.is_name_explicit = true
  116. end
  117. else
  118. mod_conf = Settings(mod_path .. DIR_DELIM .. "mod.conf"):to_table()
  119. if mod_conf.name then
  120. name = mod_conf.name
  121. toadd.is_name_explicit = true
  122. end
  123. end
  124. -- Read from config
  125. toadd.name = name
  126. toadd.title = mod_conf.title
  127. toadd.author = mod_conf.author
  128. toadd.release = tonumber(mod_conf.release) or 0
  129. toadd.path = mod_path
  130. toadd.virtual_path = mod_virtual_path
  131. toadd.type = "mod"
  132. -- Check modpack.txt
  133. -- Note: modpack.conf is already checked above
  134. local modpackfile = io.open(mod_path .. DIR_DELIM .. "modpack.txt")
  135. if modpackfile then
  136. modpackfile:close()
  137. toadd.is_modpack = true
  138. end
  139. -- Deal with modpack contents
  140. if modpack and modpack ~= "" then
  141. toadd.modpack = modpack
  142. elseif toadd.is_modpack then
  143. toadd.type = "modpack"
  144. toadd.is_modpack = true
  145. pkgmgr.get_mods(mod_path, mod_virtual_path, listing, name)
  146. end
  147. end
  148. end
  149. if not modpack then
  150. -- Sort all when the recursion is done
  151. table.sort(listing, function(a, b)
  152. return a.virtual_path:lower() < b.virtual_path:lower()
  153. end)
  154. end
  155. end
  156. function pkgmgr.get_texture_packs()
  157. local txtpath = core.get_texturepath()
  158. local txtpath_system = core.get_texturepath_share()
  159. local retval = {}
  160. load_texture_packs(txtpath, retval)
  161. -- on portable versions these two paths coincide. It avoids loading the path twice
  162. if txtpath ~= txtpath_system then
  163. load_texture_packs(txtpath_system, retval)
  164. end
  165. table.sort(retval, function(a, b)
  166. return a.title:lower() < b.title:lower()
  167. end)
  168. return retval
  169. end
  170. --------------------------------------------------------------------------------
  171. function pkgmgr.get_folder_type(path)
  172. local testfile = io.open(path .. DIR_DELIM .. "init.lua","r")
  173. if testfile ~= nil then
  174. testfile:close()
  175. return { type = "mod", path = path }
  176. end
  177. testfile = io.open(path .. DIR_DELIM .. "modpack.conf","r")
  178. if testfile ~= nil then
  179. testfile:close()
  180. return { type = "modpack", path = path }
  181. end
  182. testfile = io.open(path .. DIR_DELIM .. "modpack.txt","r")
  183. if testfile ~= nil then
  184. testfile:close()
  185. return { type = "modpack", path = path }
  186. end
  187. testfile = io.open(path .. DIR_DELIM .. "game.conf","r")
  188. if testfile ~= nil then
  189. testfile:close()
  190. return { type = "game", path = path }
  191. end
  192. testfile = io.open(path .. DIR_DELIM .. "texture_pack.conf","r")
  193. if testfile ~= nil then
  194. testfile:close()
  195. return { type = "txp", path = path }
  196. end
  197. return nil
  198. end
  199. -------------------------------------------------------------------------------
  200. function pkgmgr.get_base_folder(temppath)
  201. if temppath == nil then
  202. return { type = "invalid", path = "" }
  203. end
  204. local ret = pkgmgr.get_folder_type(temppath)
  205. if ret then
  206. return ret
  207. end
  208. local subdirs = core.get_dir_list(temppath, true)
  209. if #subdirs == 1 then
  210. ret = pkgmgr.get_folder_type(temppath .. DIR_DELIM .. subdirs[1])
  211. if ret then
  212. return ret
  213. else
  214. return { type = "invalid", path = temppath .. DIR_DELIM .. subdirs[1] }
  215. end
  216. end
  217. return nil
  218. end
  219. --------------------------------------------------------------------------------
  220. function pkgmgr.is_valid_modname(modpath)
  221. return modpath:match("[^a-z0-9_]") == nil
  222. end
  223. --------------------------------------------------------------------------------
  224. function pkgmgr.render_packagelist(render_list, use_technical_names, with_error)
  225. if not render_list then
  226. if not pkgmgr.global_mods then
  227. pkgmgr.refresh_globals()
  228. end
  229. render_list = pkgmgr.global_mods
  230. end
  231. local list = render_list:get_list()
  232. local retval = {}
  233. for i, v in ipairs(list) do
  234. local color = ""
  235. local icon = 0
  236. local error = with_error and with_error[v.virtual_path]
  237. local function update_error(val)
  238. if val and (not error or (error.type == "warning" and val.type == "error")) then
  239. error = val
  240. end
  241. end
  242. if v.is_modpack then
  243. local rawlist = render_list:get_raw_list()
  244. color = mt_color_dark_green
  245. for j = 1, #rawlist do
  246. if rawlist[j].modpack == list[i].name then
  247. if with_error then
  248. update_error(with_error[rawlist[j].virtual_path])
  249. end
  250. if rawlist[j].enabled then
  251. icon = 1
  252. else
  253. -- Modpack not entirely enabled so showing as grey
  254. color = mt_color_grey
  255. end
  256. end
  257. end
  258. elseif v.is_game_content or v.type == "game" then
  259. icon = 1
  260. color = mt_color_blue
  261. local rawlist = render_list:get_raw_list()
  262. if v.type == "game" and with_error then
  263. for j = 1, #rawlist do
  264. if rawlist[j].is_game_content then
  265. update_error(with_error[rawlist[j].virtual_path])
  266. end
  267. end
  268. end
  269. elseif v.enabled or v.type == "txp" then
  270. icon = 1
  271. color = mt_color_green
  272. end
  273. if error then
  274. if error.type == "warning" then
  275. color = mt_color_orange
  276. icon = 2
  277. else
  278. color = mt_color_red
  279. icon = 3
  280. end
  281. end
  282. retval[#retval + 1] = color
  283. if v.modpack ~= nil or v.loc == "game" then
  284. retval[#retval + 1] = "1"
  285. else
  286. retval[#retval + 1] = "0"
  287. end
  288. if with_error then
  289. retval[#retval + 1] = icon
  290. end
  291. if use_technical_names then
  292. retval[#retval + 1] = core.formspec_escape(v.list_name or v.name)
  293. else
  294. retval[#retval + 1] = core.formspec_escape(v.list_title or v.list_name or v.title or v.name)
  295. end
  296. end
  297. return table.concat(retval, ",")
  298. end
  299. --------------------------------------------------------------------------------
  300. function pkgmgr.get_dependencies(path)
  301. if path == nil then
  302. return {}, {}
  303. end
  304. local info = core.get_content_info(path)
  305. return info.depends or {}, info.optional_depends or {}
  306. end
  307. ----------- tests whether all of the mods in the modpack are enabled -----------
  308. function pkgmgr.is_modpack_entirely_enabled(data, name)
  309. local rawlist = data.list:get_raw_list()
  310. for j = 1, #rawlist do
  311. if rawlist[j].modpack == name and not rawlist[j].enabled then
  312. return false
  313. end
  314. end
  315. return true
  316. end
  317. local function disable_all_by_name(list, name, except)
  318. for i=1, #list do
  319. if list[i].name == name and list[i] ~= except then
  320. list[i].enabled = false
  321. end
  322. end
  323. end
  324. ---------- toggles or en/disables a mod or modpack and its dependencies --------
  325. local function toggle_mod_or_modpack(list, toggled_mods, enabled_mods, toset, mod)
  326. if not mod.is_modpack then
  327. -- Toggle or en/disable the mod
  328. if toset == nil then
  329. toset = not mod.enabled
  330. end
  331. if mod.enabled ~= toset then
  332. toggled_mods[#toggled_mods+1] = mod.name
  333. end
  334. if toset then
  335. -- Mark this mod for recursive dependency traversal
  336. enabled_mods[mod.name] = true
  337. -- Disable other mods with the same name
  338. disable_all_by_name(list, mod.name, mod)
  339. end
  340. mod.enabled = toset
  341. else
  342. -- Toggle or en/disable every mod in the modpack,
  343. -- interleaved unsupported
  344. for i = 1, #list do
  345. if list[i].modpack == mod.name then
  346. toggle_mod_or_modpack(list, toggled_mods, enabled_mods, toset, list[i])
  347. end
  348. end
  349. end
  350. end
  351. function pkgmgr.enable_mod(this, toset)
  352. local list = this.data.list:get_list()
  353. local mod = list[this.data.selected_mod]
  354. -- Game mods can't be enabled or disabled
  355. if mod.is_game_content then
  356. return
  357. end
  358. local toggled_mods = {}
  359. local enabled_mods = {}
  360. toggle_mod_or_modpack(list, toggled_mods, enabled_mods, toset, mod)
  361. if next(enabled_mods) == nil then
  362. -- Mod(s) were disabled, so no dependencies need to be enabled
  363. table.sort(toggled_mods)
  364. core.log("info", "Following mods were disabled: " ..
  365. table.concat(toggled_mods, ", "))
  366. return
  367. end
  368. -- Enable mods' depends after activation
  369. -- Make a list of mod ids indexed by their names. Among mods with the
  370. -- same name, enabled mods take precedence, after which game mods take
  371. -- precedence, being last in the mod list.
  372. local mod_ids = {}
  373. for id, mod2 in pairs(list) do
  374. if mod2.type == "mod" and not mod2.is_modpack then
  375. local prev_id = mod_ids[mod2.name]
  376. if not prev_id or not list[prev_id].enabled then
  377. mod_ids[mod2.name] = id
  378. end
  379. end
  380. end
  381. -- to_enable is used as a DFS stack with sp as stack pointer
  382. local to_enable = {}
  383. local sp = 0
  384. for name in pairs(enabled_mods) do
  385. local depends = pkgmgr.get_dependencies(list[mod_ids[name]].path)
  386. for i = 1, #depends do
  387. local dependency_name = depends[i]
  388. if not enabled_mods[dependency_name] then
  389. sp = sp+1
  390. to_enable[sp] = dependency_name
  391. end
  392. end
  393. end
  394. -- If sp is 0, every dependency is already activated
  395. while sp > 0 do
  396. local name = to_enable[sp]
  397. sp = sp-1
  398. if not enabled_mods[name] then
  399. enabled_mods[name] = true
  400. local mod_to_enable = list[mod_ids[name]]
  401. if not mod_to_enable then
  402. core.log("warning", "Mod dependency \"" .. name ..
  403. "\" not found!")
  404. elseif not mod_to_enable.is_game_content then
  405. if not mod_to_enable.enabled then
  406. mod_to_enable.enabled = true
  407. toggled_mods[#toggled_mods+1] = mod_to_enable.name
  408. end
  409. -- Push the dependencies of the dependency onto the stack
  410. local depends = pkgmgr.get_dependencies(mod_to_enable.path)
  411. for i = 1, #depends do
  412. if not enabled_mods[depends[i]] then
  413. sp = sp+1
  414. to_enable[sp] = depends[i]
  415. end
  416. end
  417. end
  418. end
  419. end
  420. -- Log the list of enabled mods
  421. table.sort(toggled_mods)
  422. core.log("info", "Following mods were enabled: " ..
  423. table.concat(toggled_mods, ", "))
  424. end
  425. --------------------------------------------------------------------------------
  426. function pkgmgr.get_worldconfig(worldpath)
  427. local filename = worldpath ..
  428. DIR_DELIM .. "world.mt"
  429. local worldfile = Settings(filename)
  430. local worldconfig = {}
  431. worldconfig.global_mods = {}
  432. worldconfig.game_mods = {}
  433. for key,value in pairs(worldfile:to_table()) do
  434. if key == "gameid" then
  435. worldconfig.id = value
  436. elseif key:sub(0, 9) == "load_mod_" then
  437. -- Compatibility: Check against "nil" which was erroneously used
  438. -- as value for fresh configured worlds
  439. worldconfig.global_mods[key] = value ~= "false" and value ~= "nil"
  440. and value
  441. else
  442. worldconfig[key] = value
  443. end
  444. end
  445. --read gamemods
  446. local gamespec = pkgmgr.find_by_gameid(worldconfig.id)
  447. pkgmgr.get_game_mods(gamespec, worldconfig.game_mods)
  448. return worldconfig
  449. end
  450. --------------------------------------------------------------------------------
  451. function pkgmgr.install_dir(expected_type, path, basename, targetpath)
  452. assert(type(expected_type) == "string")
  453. assert(type(path) == "string")
  454. assert(basename == nil or type(basename) == "string")
  455. assert(targetpath == nil or type(targetpath) == "string")
  456. local basefolder = pkgmgr.get_base_folder(path)
  457. if expected_type == "txp" then
  458. assert(basename)
  459. -- There's no good way to detect a texture pack, so let's just assume
  460. -- it's correct for now.
  461. if basefolder and basefolder.type ~= "invalid" and basefolder.type ~= "txp" then
  462. return nil, fgettext("Unable to install a $1 as a texture pack", basefolder.type)
  463. end
  464. local from = basefolder and basefolder.path or path
  465. if not targetpath then
  466. targetpath = core.get_texturepath() .. DIR_DELIM .. basename
  467. end
  468. core.delete_dir(targetpath)
  469. if not core.copy_dir(from, targetpath, false) then
  470. return nil,
  471. fgettext("Failed to install $1 to $2", basename, targetpath)
  472. end
  473. return targetpath, nil
  474. elseif not basefolder then
  475. return nil, fgettext("Unable to find a valid mod, modpack, or game")
  476. end
  477. -- Check type
  478. if basefolder.type ~= expected_type and (basefolder.type ~= "modpack" or expected_type ~= "mod") then
  479. return nil, fgettext("Unable to install a $1 as a $2", basefolder.type, expected_type)
  480. end
  481. -- Set targetpath if not predetermined
  482. if not targetpath then
  483. local content_path
  484. if basefolder.type == "modpack" or basefolder.type == "mod" then
  485. if not basename then
  486. basename = get_last_folder(cleanup_path(basefolder.path))
  487. end
  488. content_path = core.get_modpath()
  489. elseif basefolder.type == "game" then
  490. content_path = core.get_gamepath()
  491. else
  492. error("Unknown content type")
  493. end
  494. if basename and (basefolder.type ~= "mod" or pkgmgr.is_valid_modname(basename)) then
  495. targetpath = content_path .. DIR_DELIM .. basename
  496. else
  497. return nil,
  498. fgettext("Install: Unable to find suitable folder name for $1", path)
  499. end
  500. end
  501. -- Copy it
  502. core.delete_dir(targetpath)
  503. if not core.copy_dir(basefolder.path, targetpath, false) then
  504. return nil,
  505. fgettext("Failed to install $1 to $2", basename, targetpath)
  506. end
  507. if basefolder.type == "game" then
  508. pkgmgr.update_gamelist()
  509. else
  510. pkgmgr.refresh_globals()
  511. end
  512. return targetpath, nil
  513. end
  514. --------------------------------------------------------------------------------
  515. function pkgmgr.preparemodlist(data)
  516. local retval = {}
  517. local global_mods = {}
  518. local game_mods = {}
  519. --read global mods
  520. local modpaths = core.get_modpaths()
  521. for key, modpath in pairs(modpaths) do
  522. pkgmgr.get_mods(modpath, key, global_mods)
  523. end
  524. for i=1,#global_mods,1 do
  525. global_mods[i].type = "mod"
  526. global_mods[i].loc = "global"
  527. global_mods[i].enabled = false
  528. retval[#retval + 1] = global_mods[i]
  529. end
  530. --read game mods
  531. local gamespec = pkgmgr.find_by_gameid(data.gameid)
  532. pkgmgr.get_game_mods(gamespec, game_mods)
  533. if #game_mods > 0 then
  534. -- Add title
  535. retval[#retval + 1] = {
  536. type = "game",
  537. is_game_content = true,
  538. name = fgettext("$1 mods", gamespec.title),
  539. path = gamespec.path
  540. }
  541. end
  542. for i=1,#game_mods,1 do
  543. game_mods[i].type = "mod"
  544. game_mods[i].loc = "game"
  545. game_mods[i].is_game_content = true
  546. retval[#retval + 1] = game_mods[i]
  547. end
  548. if data.worldpath == nil then
  549. return retval
  550. end
  551. --read world mod configuration
  552. local filename = data.worldpath ..
  553. DIR_DELIM .. "world.mt"
  554. local worldfile = Settings(filename)
  555. for key, value in pairs(worldfile:to_table()) do
  556. if key:sub(1, 9) == "load_mod_" then
  557. key = key:sub(10)
  558. local mod_found = false
  559. local fallback_found = false
  560. local fallback_mod = nil
  561. for i=1, #retval do
  562. if retval[i].name == key and
  563. not retval[i].is_modpack then
  564. if core.is_yes(value) or retval[i].virtual_path == value then
  565. retval[i].enabled = true
  566. mod_found = true
  567. break
  568. elseif fallback_found then
  569. -- Only allow fallback if only one mod matches
  570. fallback_mod = nil
  571. else
  572. fallback_found = true
  573. fallback_mod = retval[i]
  574. end
  575. end
  576. end
  577. if not mod_found then
  578. if fallback_mod and value:find("/") then
  579. fallback_mod.enabled = true
  580. else
  581. core.log("info", "Mod: " .. key .. " " .. dump(value) .. " but not found")
  582. end
  583. end
  584. end
  585. end
  586. return retval
  587. end
  588. function pkgmgr.compare_package(a, b)
  589. return a and b and a.name == b.name and a.path == b.path
  590. end
  591. --------------------------------------------------------------------------------
  592. function pkgmgr.comparemod(elem1,elem2)
  593. if elem1 == nil or elem2 == nil then
  594. return false
  595. end
  596. if elem1.name ~= elem2.name then
  597. return false
  598. end
  599. if elem1.is_modpack ~= elem2.is_modpack then
  600. return false
  601. end
  602. if elem1.type ~= elem2.type then
  603. return false
  604. end
  605. if elem1.modpack ~= elem2.modpack then
  606. return false
  607. end
  608. if elem1.path ~= elem2.path then
  609. return false
  610. end
  611. return true
  612. end
  613. --------------------------------------------------------------------------------
  614. function pkgmgr.refresh_globals()
  615. local function is_equal(element,uid) --uid match
  616. if element.name == uid then
  617. return true
  618. end
  619. end
  620. pkgmgr.global_mods = filterlist.create(pkgmgr.preparemodlist,
  621. pkgmgr.comparemod, is_equal, nil, {})
  622. pkgmgr.global_mods:add_sort_mechanism("alphabetic", sort_mod_list)
  623. pkgmgr.global_mods:set_sortmode("alphabetic")
  624. end
  625. --------------------------------------------------------------------------------
  626. function pkgmgr.find_by_gameid(gameid)
  627. for i, game in ipairs(pkgmgr.games) do
  628. if game.id == gameid then
  629. return game, i
  630. end
  631. end
  632. return nil, nil
  633. end
  634. --------------------------------------------------------------------------------
  635. function pkgmgr.get_game_mods(gamespec, retval)
  636. if gamespec ~= nil and
  637. gamespec.gamemods_path ~= nil and
  638. gamespec.gamemods_path ~= "" then
  639. pkgmgr.get_mods(gamespec.gamemods_path, ("games/%s/mods"):format(gamespec.id), retval)
  640. end
  641. end
  642. --------------------------------------------------------------------------------
  643. function pkgmgr.update_gamelist()
  644. pkgmgr.games = core.get_games()
  645. table.sort(pkgmgr.games, function(a, b)
  646. return a.title:lower() < b.title:lower()
  647. end)
  648. end
  649. --------------------------------------------------------------------------------
  650. -- read initial data
  651. --------------------------------------------------------------------------------
  652. pkgmgr.update_gamelist()