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