dlg_server_list_mods.lua 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. -- Luanti
  2. -- Copyright (C) 2024 cx384
  3. -- SPDX-License-Identifier: LGPL-2.1-or-later
  4. local function get_formspec(dialogdata)
  5. local TOUCH_GUI = core.settings:get_bool("touch_gui")
  6. local server = dialogdata.server
  7. local group_by_prefix = dialogdata.group_by_prefix
  8. local expand_all = dialogdata.expand_all
  9. -- A wrongly behaving server may send ill formed mod names
  10. table.sort(server.mods)
  11. local cells = {}
  12. if group_by_prefix then
  13. local function get_prefix(mod)
  14. return mod:match("[^_]*")
  15. end
  16. local count = {}
  17. for _, mod in ipairs(server.mods) do
  18. local prefix = get_prefix(mod)
  19. count[prefix] = (count[prefix] or 0) + 1
  20. end
  21. local last_prefix
  22. local function add_row(depth, mod)
  23. table.insert(cells, ("%d"):format(depth))
  24. table.insert(cells, mod)
  25. end
  26. for i, mod in ipairs(server.mods) do
  27. local prefix = get_prefix(mod)
  28. if last_prefix == prefix then
  29. add_row(1, mod)
  30. elseif count[prefix] > 1 then
  31. add_row(0, prefix)
  32. add_row(1, mod)
  33. else
  34. add_row(0, mod)
  35. end
  36. last_prefix = prefix
  37. end
  38. else
  39. cells = table.copy(server.mods)
  40. end
  41. for i, cell in ipairs(cells) do
  42. cells[i] = core.formspec_escape(cell)
  43. end
  44. cells = table.concat(cells, ",")
  45. local heading
  46. if server.gameid then
  47. heading = fgettext("The $1 server uses a game called $2 and the following mods:",
  48. "<b>" .. core.hypertext_escape(server.name) .. "</b>",
  49. "<style font=mono>" .. core.hypertext_escape(server.gameid) .. "</style>")
  50. else
  51. heading = fgettext("The $1 server uses the following mods:",
  52. "<b>" .. core.hypertext_escape(server.name) .. "</b>")
  53. end
  54. local formspec = {
  55. "formspec_version[8]",
  56. "size[8,9.5]",
  57. TOUCH_GUI and "padding[0.01,0.01]" or "",
  58. "hypertext[0,0;8,1.5;;<global margin=5 halign=center valign=middle>", heading, "]",
  59. "tablecolumns[", group_by_prefix and
  60. (expand_all and "indent;text" or "tree;text") or "text", "]",
  61. "table[0.5,1.5;7,6.8;mods;", cells, "]",
  62. "checkbox[0.5,8.7;group_by_prefix;", fgettext("Group by prefix"), ";",
  63. group_by_prefix and "true" or "false", "]",
  64. group_by_prefix and ("checkbox[0.5,9.15;expand_all;" .. fgettext("Expand all") .. ";" ..
  65. (expand_all and "true" or "false") .. "]") or "",
  66. "button[5.5,8.5;2,0.8;quit;OK]"
  67. }
  68. return table.concat(formspec, "")
  69. end
  70. local function buttonhandler(this, fields)
  71. if fields.quit then
  72. this:delete()
  73. return true
  74. end
  75. if fields.group_by_prefix then
  76. this.data.group_by_prefix = core.is_yes(fields.group_by_prefix)
  77. return true
  78. end
  79. if fields.expand_all then
  80. this.data.expand_all = core.is_yes(fields.expand_all)
  81. return true
  82. end
  83. return false
  84. end
  85. function create_server_list_mods_dialog(server)
  86. local retval = dialog_create("dlg_server_list_mods",
  87. get_formspec,
  88. buttonhandler,
  89. nil)
  90. retval.data.group_by_prefix = false
  91. retval.data.expand_all = false
  92. retval.data.server = server
  93. return retval
  94. end