2 --Copyright (C) 2014 sapier
3 --Copyright (C) 2018 rubenwardy <rw@rubenwardy.com>
5 --This program is free software; you can redistribute it and/or modify
6 --it under the terms of the GNU Lesser General Public License as published by
7 --the Free Software Foundation; either version 2.1 of the License, or
8 --(at your option) any later version.
10 --This program is distributed in the hope that it will be useful,
11 --but WITHOUT ANY WARRANTY; without even the implied warranty of
12 --MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 --GNU Lesser General Public License for more details.
15 --You should have received a copy of the GNU Lesser General Public License along
16 --with this program; if not, write to the Free Software Foundation, Inc.,
17 --51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 local function modname_valid(name)
23 return not name:find("[^a-z0-9_]")
26 --------------------------------------------------------------------------------
27 local function get_formspec(tabview, name, tabdata)
28 if pkgmgr.global_mods == nil then
29 pkgmgr.refresh_globals()
31 if pkgmgr.games == nil then
32 pkgmgr.update_gamelist()
35 if packages == nil then
37 table.insert_all(packages_raw, pkgmgr.games)
38 table.insert_all(packages_raw, pkgmgr.get_texture_packs())
39 table.insert_all(packages_raw, pkgmgr.global_mods:get_list())
40 table.insert_all(packages_raw, pkgmgr.clientmods:get_list())
42 local function get_data()
46 local function is_equal(element, uid) --uid match
47 return (element.type == "game" and element.id == uid) or
51 packages = filterlist.create(get_data, pkgmgr.compare_package,
54 local filename = core.get_clientmodpath() .. DIR_DELIM .. "mods.conf"
56 local conffile = Settings(filename)
57 local mods = conffile:to_table()
59 for i = 1, #packages_raw do
60 local mod = packages_raw[i]
61 if mod.is_clientside and not mod.is_modpack then
62 if modname_valid(mod.name) then
63 conffile:set("load_mod_" .. mod.name,
64 mod.enabled and "true" or "false")
65 elseif mod.enabled then
66 gamedata.errormessage = fgettext_ne("Failed to enable clientmo" ..
67 "d \"$1\" as it contains disallowed characters. " ..
68 "Only characters [a-z0-9_] are allowed.",
71 mods["load_mod_" .. mod.name] = nil
75 -- Remove mods that are not present anymore
76 for key in pairs(mods) do
77 if key:sub(1, 9) == "load_mod_" then
82 if not conffile:write() then
83 core.log("error", "Failed to write clientmod config file")
87 if tabdata.selected_pkg == nil then
88 tabdata.selected_pkg = 1
93 "label[0.05,-0.25;".. fgettext("Installed Packages:") .. "]" ..
94 "tablecolumns[color;tree;text]" ..
95 "table[0,0.25;5.1,4.3;pkglist;" ..
96 pkgmgr.render_packagelist(packages) ..
97 ";" .. tabdata.selected_pkg .. "]" ..
98 "button[0,4.85;5.25,0.5;btn_contentdb;".. fgettext("Browse online content") .. "]"
102 if filterlist.size(packages) >= tabdata.selected_pkg then
103 selected_pkg = packages:get_list()[tabdata.selected_pkg]
106 if selected_pkg ~= nil then
107 --check for screenshot beeing available
108 local screenshotfilename = selected_pkg.path .. DIR_DELIM .. "screenshot.png"
109 local screenshotfile, error = io.open(screenshotfilename, "r")
113 screenshotfile:close()
114 modscreenshot = screenshotfilename
117 if modscreenshot == nil then
118 modscreenshot = defaulttexturedir .. "no_screenshot.png"
121 local info = core.get_content_info(selected_pkg.path)
122 local desc = fgettext("No package description available")
123 if info.description and info.description:trim() ~= "" then
124 desc = info.description
128 "image[5.5,0;3,2;" .. core.formspec_escape(modscreenshot) .. "]" ..
129 "label[8.25,0.6;" .. core.formspec_escape(selected_pkg.name) .. "]" ..
130 "box[5.5,2.2;6.15,2.35;#000]"
132 if selected_pkg.type == "mod" then
133 if selected_pkg.is_modpack then
134 if selected_pkg.is_clientside then
135 if pkgmgr.is_modpack_entirely_enabled({list = packages}, selected_pkg.name) then
137 "button[8.65,4.65;3.25,1;btn_mod_mgr_mp_disable;" ..
138 fgettext("Disable modpack") .. "]"
141 "button[8.65,4.65;3.25,1;btn_mod_mgr_mp_enable;" ..
142 fgettext("Enable modpack") .. "]"
146 "button[8.65,4.65;3.25,1;btn_mod_mgr_rename_modpack;" ..
147 fgettext("Rename") .. "]"
151 desc = desc .. "\n\n"
152 local toadd_hard = table.concat(info.depends or {}, "\n")
153 local toadd_soft = table.concat(info.optional_depends or {}, "\n")
154 if toadd_hard == "" and toadd_soft == "" then
155 desc = desc .. fgettext("No dependencies.")
157 if toadd_hard ~= "" then
158 desc = desc ..fgettext("Dependencies:") ..
161 if toadd_soft ~= "" then
162 if toadd_hard ~= "" then
163 desc = desc .. "\n\n"
165 desc = desc .. fgettext("Optional dependencies:") ..
169 if selected_pkg.is_clientside then
170 if selected_pkg.enabled then
172 "button[8.65,4.65;3.25,1;btn_mod_mgr_disable_mod;" ..
173 fgettext("Disable") .. "]"
176 "button[8.65,4.65;3.25,1;btn_mod_mgr_enable_mod;" ..
177 fgettext("Enable") .. "]"
183 if selected_pkg.type == "txp" then
184 if selected_pkg.enabled then
186 "button[8.65,4.65;3.25,1;btn_mod_mgr_disable_txp;" ..
187 fgettext("Disable Texture Pack") .. "]"
190 "button[8.65,4.65;3.25,1;btn_mod_mgr_use_txp;" ..
191 fgettext("Use Texture Pack") .. "]"
196 retval = retval .. "textarea[5.85,2.2;6.35,2.9;;" ..
197 fgettext("Information:") .. ";" .. desc .. "]"
199 if core.may_modify_path(selected_pkg.path) then
201 "button[5.5,4.65;3.25,1;btn_mod_mgr_delete_mod;" ..
202 fgettext("Uninstall Package") .. "]"
208 --------------------------------------------------------------------------------
209 local function handle_doubleclick(pkg, pkg_name)
210 if pkg.type == "txp" then
211 if core.settings:get("texture_path") == pkg.path then
212 core.settings:set("texture_path", "")
214 core.settings:set("texture_path", pkg.path)
217 elseif pkg.is_clientside then
218 pkgmgr.enable_mod({data = {list = packages, selected_mod = pkg_name}})
223 --------------------------------------------------------------------------------
224 local function handle_buttons(tabview, fields, tabname, tabdata)
225 if fields["pkglist"] ~= nil then
226 local event = core.explode_table_event(fields["pkglist"])
227 tabdata.selected_pkg = event.row
228 if event.type == "DCL" then
229 handle_doubleclick(packages:get_list()[tabdata.selected_pkg], tabdata.selected_pkg)
234 if fields.btn_mod_mgr_mp_enable ~= nil or
235 fields.btn_mod_mgr_mp_disable ~= nil then
236 pkgmgr.enable_mod({data = {list = packages, selected_mod = tabdata.selected_pkg}},
237 fields.btn_mod_mgr_mp_enable ~= nil)
242 if fields.btn_mod_mgr_enable_mod ~= nil or
243 fields.btn_mod_mgr_disable_mod ~= nil then
244 pkgmgr.enable_mod({data = {list = packages, selected_mod = tabdata.selected_pkg}},
245 fields.btn_mod_mgr_enable_mod ~= nil)
250 if fields["btn_contentdb"] ~= nil then
251 local dlg = create_store_dlg()
252 dlg:set_parent(tabview)
259 if fields["btn_mod_mgr_rename_modpack"] ~= nil then
260 local mod = packages:get_list()[tabdata.selected_pkg]
261 local dlg_renamemp = create_rename_modpack_dlg(mod)
262 dlg_renamemp:set_parent(tabview)
269 if fields["btn_mod_mgr_delete_mod"] ~= nil then
270 local mod = packages:get_list()[tabdata.selected_pkg]
271 local dlg_delmod = create_delete_content_dlg(mod)
272 dlg_delmod:set_parent(tabview)
279 if fields.btn_mod_mgr_use_txp then
280 local txp = packages:get_list()[tabdata.selected_pkg]
281 core.settings:set("texture_path", txp.path)
287 if fields.btn_mod_mgr_disable_txp then
288 core.settings:set("texture_path", "")
296 --------------------------------------------------------------------------------
299 caption = fgettext("Content"),
300 cbf_formspec = get_formspec,
301 cbf_button_handler = handle_buttons,
302 on_change = pkgmgr.update_gamelist