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