Reworked formspecs and kahrl's hexcolor parser
[oweals/minetest.git] / builtin / modstore.lua
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 --------------------------------------------------------------------------------
19
20 --modstore implementation
21 modstore = {}
22
23 --------------------------------------------------------------------------------
24 function modstore.init()
25         modstore.tabnames = {}
26         
27         table.insert(modstore.tabnames,"dialog_modstore_unsorted")
28         table.insert(modstore.tabnames,"dialog_modstore_search")
29         
30         modstore.modsperpage = 5
31         
32         modstore.basetexturedir = engine.get_texturepath() .. DIR_DELIM .. "base" .. 
33                                                 DIR_DELIM .. "pack" .. DIR_DELIM
34         
35         modstore.current_list = nil
36         
37         modstore.details_cache = {}
38 end
39 --------------------------------------------------------------------------------
40 function modstore.nametoindex(name)
41
42         for i=1,#modstore.tabnames,1 do
43                 if modstore.tabnames[i] == name then
44                         return i
45                 end
46         end
47
48         return 1
49 end
50
51 --------------------------------------------------------------------------------
52 function modstore.gettab(tabname)
53         local retval = ""
54         
55         local is_modstore_tab = false
56         
57         if tabname == "dialog_modstore_unsorted" then
58                 retval = modstore.getmodlist(modstore.modlist_unsorted)
59                 is_modstore_tab = true
60         end
61         
62         if tabname == "dialog_modstore_search" then
63         
64         
65                 is_modstore_tab = true
66         end
67         
68         if is_modstore_tab then
69                 return modstore.tabheader(tabname) .. retval
70         end
71         
72         if tabname == "modstore_mod_installed" then
73                 return "size[6,2]label[0.25,0.25;Mod: " .. modstore.lastmodtitle .. 
74                                 " installed successfully]" ..
75                                 "button[2.5,1.5;1,0.5;btn_confirm_mod_successfull;ok]"
76         end
77         
78         return ""
79 end
80
81 --------------------------------------------------------------------------------
82 function modstore.tabheader(tabname)
83         local retval  = "size[12,9.25]"
84         retval = retval .. "tabheader[-0.3,-0.99;modstore_tab;" ..
85                                 "Unsorted,Search;" ..
86                                 modstore.nametoindex(tabname) .. ";true;false]"
87                                 
88         return retval
89 end
90
91 --------------------------------------------------------------------------------
92 function modstore.handle_buttons(current_tab,fields)
93
94         modstore.lastmodtitle = ""
95         
96         if fields["modstore_tab"] then
97                 local index = tonumber(fields["modstore_tab"])
98                 
99                 if index > 0 and
100                         index <= #modstore.tabnames then
101                         return {
102                                         current_tab = modstore.tabnames[index],
103                                         is_dialog = true,
104                                         show_buttons = false
105                         }
106                 end
107                 
108                 modstore.modlist_page = 0
109         end
110         
111         if fields["btn_modstore_page_up"] then
112                 if modstore.current_list ~= nil and modstore.current_list.page > 0 then
113                         modstore.current_list.page = modstore.current_list.page - 1
114                 end
115         end
116         
117         if fields["btn_modstore_page_down"] then
118                 if modstore.current_list ~= nil and 
119                         modstore.current_list.page <modstore.current_list.pagecount-1 then
120                         modstore.current_list.page = modstore.current_list.page +1
121                 end
122         end
123         
124         if fields["btn_confirm_mod_successfull"] then
125                 return {
126                                         current_tab = modstore.tabnames[1],
127                                         is_dialog = true,
128                                         show_buttons = false
129                         }
130         end
131         
132         for i=1, modstore.modsperpage, 1 do
133                 local installbtn = "btn_install_mod_" .. i
134                 
135                 if fields[installbtn] then
136                         local modlistentry = 
137                                 modstore.current_list.page * modstore.modsperpage + i
138                         
139                         local moddetails = modstore.get_details(modstore.current_list.data[modlistentry].id)
140                         
141                         local fullurl = engine.setting_get("modstore_download_url") ..
142                                                                 moddetails.download_url
143                         local modfilename = os.tempfolder() .. ".zip"
144                         
145                         if engine.download_file(fullurl,modfilename) then
146                         
147                                 modmgr.installmod(modfilename,moddetails.basename)
148                                 
149                                 os.remove(modfilename)
150                                 modstore.lastmodtitle = modstore.current_list.data[modlistentry].title
151                                 
152                                 return {
153                                         current_tab = "modstore_mod_installed",
154                                         is_dialog = true,
155                                         show_buttons = false
156                                 }
157                         else
158                                 gamedata.errormessage = "Unable to download " .. 
159                                         moddetails.download_url .. " (internet connection?)"
160                         end
161                 end
162         end
163 end
164
165 --------------------------------------------------------------------------------
166 function modstore.update_modlist()
167         modstore.modlist_unsorted = {}
168         modstore.modlist_unsorted.data = engine.get_modstore_list()
169                 
170         if modstore.modlist_unsorted.data ~= nil then
171                 modstore.modlist_unsorted.pagecount = 
172                         math.ceil((#modstore.modlist_unsorted.data / modstore.modsperpage))
173         else
174                 modstore.modlist_unsorted.data = {}
175                 modstore.modlist_unsorted.pagecount = 1
176         end
177         modstore.modlist_unsorted.page = 0
178 end
179
180 --------------------------------------------------------------------------------
181 function modstore.getmodlist(list)
182         local retval = ""
183         retval = retval .. "label[10,-0.4;" .. fgettext("Page $1 of $2", list.page+1, list.pagecount) .. "]"
184         
185         retval = retval .. "button[11.6,-0.1;0.5,0.5;btn_modstore_page_up;^]"
186         retval = retval .. "box[11.6,0.35;0.28,8.6;#000000]"
187         local scrollbarpos = 0.35 + (8.1/(list.pagecount-1)) * list.page
188         retval = retval .. "box[11.6," ..scrollbarpos .. ";0.28,0.5;#32CD32]"
189         retval = retval .. "button[11.6,9.0;0.5,0.5;btn_modstore_page_down;v]"
190         
191         
192         if #list.data < (list.page * modstore.modsperpage) then
193                 return retval
194         end
195         
196         local endmod = (list.page * modstore.modsperpage) + modstore.modsperpage
197         
198         if (endmod > #list.data) then
199                 endmod = #list.data
200         end
201
202         for i=(list.page * modstore.modsperpage) +1, endmod, 1 do
203                 --getmoddetails
204                 local details = modstore.get_details(list.data[i].id)
205         
206                 if details ~= nil then
207                         local screenshot_ypos = (i-1 - (list.page * modstore.modsperpage))*1.9 +0.2
208                         
209                         retval = retval .. "box[0," .. screenshot_ypos .. ";11.4,1.75;#FFFFFF]"
210                         
211                         --screenshot
212                         if details.screenshot_url ~= nil and
213                                 details.screenshot_url ~= "" then
214                                 if list.data[i].texturename == nil then
215                                         local fullurl = engine.setting_get("modstore_download_url") ..
216                                                                 details.screenshot_url
217                                         local filename = os.tempfolder()
218                                         
219                                         if engine.download_file(fullurl,filename) then
220                                                 list.data[i].texturename = filename
221                                         end
222                                 end
223                         end
224                         
225                         if list.data[i].texturename == nil then
226                                 list.data[i].texturename = modstore.basetexturedir .. "no_screenshot.png"
227                         end
228                         
229                         retval = retval .. "image[0,".. screenshot_ypos .. ";3,2;" .. 
230                                         engine.formspec_escape(list.data[i].texturename) .. "]"
231                         
232                         --title + author
233                         retval = retval .."label[2.75," .. screenshot_ypos .. ";" .. 
234                                 engine.formspec_escape(details.title) .. " (" .. details.author .. ")]"
235                         
236                         --description
237                         local descriptiony = screenshot_ypos + 0.5
238                         retval = retval .. "textarea[3," .. descriptiony .. ";6.5,1.55;;" .. 
239                                 engine.formspec_escape(details.description) .. ";]"
240                         --rating
241                         local ratingy = screenshot_ypos + 0.6
242                         retval = retval .."label[10.1," .. ratingy .. ";" .. 
243                                                         fgettext("Rating") .. ": " .. details.rating .."]"
244                         
245                         --install button
246                         local buttony = screenshot_ypos + 1.2
247                         local buttonnumber = (i - (list.page * modstore.modsperpage))
248                         retval = retval .."button[9.6," .. buttony .. ";2,0.5;btn_install_mod_" .. buttonnumber .. ";"
249                         
250                         if modmgr.mod_exists(details.basename) then
251                                 retval = retval .. fgettext("re-Install") .."]"
252                         else
253                                 retval = retval .. fgettext("Install") .."]"
254                         end
255                 end
256         end
257         
258         modstore.current_list = list
259         
260         return retval
261 end
262
263 --------------------------------------------------------------------------------
264 function modstore.get_details(modid) 
265
266         if modstore.details_cache[modid] ~= nil then
267                 return modstore.details_cache[modid]
268         end
269         
270         local retval = engine.get_modstore_details(tostring(modid))
271         modstore.details_cache[modid] = retval
272         return retval
273 end
274