29afd8a4e31dd4cfd3792d174198caca462e2707
[oweals/minetest.git] / builtin / mainmenu / tab_mods.lua
1 --Minetest
2 --Copyright (C) 2014 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 --------------------------------------------------------------------------------
19 local function get_formspec(tabview, name, tabdata)
20
21         if modmgr.global_mods == nil then
22                 modmgr.refresh_globals()
23         end
24
25         if tabdata.selected_mod == nil then
26                 tabdata.selected_mod = 1
27         end
28
29         local retval =
30                 "label[0.05,-0.25;".. fgettext("Installed Mods:") .. "]" ..
31                 "tablecolumns[color;tree;text]" ..
32                 "table[0,0.25;5.1,5;modlist;" ..
33                 modmgr.render_modlist(modmgr.global_mods) ..
34                 ";" .. tabdata.selected_mod .. "]"
35
36         retval = retval ..
37 --              "label[0.8,4.2;" .. fgettext("Add mod:") .. "]" ..
38 --              TODO Disabled due to upcoming release 0.4.8 and irrlicht messing up localization
39 --              "button[0.75,4.85;1.8,0.5;btn_mod_mgr_install_local;".. fgettext("Local install") .. "]" ..
40
41 --              TODO Disabled due to service being offline, and not likely to come online again, in this form
42 --              "button[0,4.85;5.25,0.5;btn_modstore;".. fgettext("Online mod repository") .. "]"
43                 ""
44
45         local selected_mod = nil
46
47         if filterlist.size(modmgr.global_mods) >= tabdata.selected_mod then
48                 selected_mod = modmgr.global_mods:get_list()[tabdata.selected_mod]
49         end
50
51         if selected_mod ~= nil then
52                 local modscreenshot = nil
53
54                 --check for screenshot beeing available
55                 local screenshotfilename = selected_mod.path .. DIR_DELIM .. "screenshot.png"
56                 local error = nil
57                 local screenshotfile,error = io.open(screenshotfilename,"r")
58                 if error == nil then
59                         screenshotfile:close()
60                         modscreenshot = screenshotfilename
61                 end
62
63                 if modscreenshot == nil then
64                                 modscreenshot = defaulttexturedir .. "no_screenshot.png"
65                 end
66
67                 retval = retval
68                                 .. "image[5.5,0;3,2;" .. core.formspec_escape(modscreenshot) .. "]"
69                                 .. "label[8.25,0.6;" .. selected_mod.name .. "]"
70
71                 local descriptionlines = nil
72                 error = nil
73                 local descriptionfilename = selected_mod.path .. "description.txt"
74                 local descriptionfile,error = io.open(descriptionfilename,"r")
75                 if error == nil then
76                         local descriptiontext = descriptionfile:read("*all")
77
78                         descriptionlines = core.splittext(descriptiontext,42)
79                         descriptionfile:close()
80                 else
81                         descriptionlines = {}
82                         descriptionlines[#descriptionlines + 1] = fgettext("No mod description available")
83                 end
84
85                 retval = retval ..
86                         "label[5.5,1.7;".. fgettext("Mod information:") .. "]" ..
87                         "textlist[5.5,2.2;6.2,2.4;description;"
88
89                 for i=1,#descriptionlines,1 do
90                         retval = retval .. core.formspec_escape(descriptionlines[i]) .. ","
91                 end
92
93
94                 if selected_mod.is_modpack then
95                         retval = retval .. ";0]" ..
96                                 "button[10,4.85;2,0.5;btn_mod_mgr_rename_modpack;" ..
97                                 fgettext("Rename") .. "]"
98                         retval = retval .. "button[5.5,4.85;4.5,0.5;btn_mod_mgr_delete_mod;"
99                                 .. fgettext("Uninstall selected modpack") .. "]"
100                 else
101                         --show dependencies
102                         local toadd_hard, toadd_soft = modmgr.get_dependencies(selected_mod.path)
103                         if toadd_hard == "" and toadd_soft == "" then
104                                 retval = retval .. "," .. fgettext("No dependencies.")
105                         else
106                                 if toadd_hard ~= "" then
107                                         retval = retval .. "," .. fgettext("Dependencies:") .. ","
108                                         retval = retval .. toadd_hard
109                                 end
110                                 if toadd_soft ~= "" then
111                                         if toadd_hard ~= "" then
112                                                 retval = retval .. ","
113                                         end
114                                         retval = retval .. "," .. fgettext("Optional dependencies:") .. ","
115                                         retval = retval .. toadd_soft
116                                 end
117                         end
118
119                         retval = retval .. ";0]"
120
121                         retval = retval .. "button[5.5,4.85;4.5,0.5;btn_mod_mgr_delete_mod;"
122                                 .. fgettext("Uninstall selected mod") .. "]"
123                 end
124         end
125         return retval
126 end
127
128 --------------------------------------------------------------------------------
129 local function handle_buttons(tabview, fields, tabname, tabdata)
130         if fields["modlist"] ~= nil then
131                 local event = core.explode_table_event(fields["modlist"])
132                 tabdata.selected_mod = event.row
133                 return true
134         end
135
136         if fields["btn_mod_mgr_install_local"] ~= nil then
137                 core.show_file_open_dialog("mod_mgt_open_dlg",fgettext("Select Mod File:"))
138                 return true
139         end
140
141         if fields["btn_modstore"] ~= nil then
142                 local modstore_ui = ui.find_by_name("modstore")
143                 if modstore_ui ~= nil then
144                         tabview:hide()
145                         modstore.update_modlist()
146                         modstore_ui:show()
147                 else
148                         print("modstore ui element not found")
149                 end
150                 return true
151         end
152
153         if fields["btn_mod_mgr_rename_modpack"] ~= nil then
154                 local dlg_renamemp = create_rename_modpack_dlg(tabdata.selected_mod)
155                 dlg_renamemp:set_parent(tabview)
156                 tabview:hide()
157                 dlg_renamemp:show()
158                 return true
159         end
160
161         if fields["btn_mod_mgr_delete_mod"] ~= nil then
162                 local dlg_delmod = create_delete_mod_dlg(tabdata.selected_mod)
163                 dlg_delmod:set_parent(tabview)
164                 tabview:hide()
165                 dlg_delmod:show()
166                 return true
167         end
168
169         if fields["mod_mgt_open_dlg_accepted"] ~= nil and
170                 fields["mod_mgt_open_dlg_accepted"] ~= "" then
171                 modmgr.installmod(fields["mod_mgt_open_dlg_accepted"],nil)
172                 return true
173         end
174
175         return false
176 end
177
178 --------------------------------------------------------------------------------
179 return {
180         name = "mods",
181         caption = fgettext("Mods"),
182         cbf_formspec = get_formspec,
183         cbf_button_handler = handle_buttons,
184         on_change = gamemgr.update_gamelist
185 }