2 --Copyright (C) 2013 sapier
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.
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.
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.
18 --------------------------------------------------------------------------------
20 --modstore implementation
23 --------------------------------------------------------------------------------
24 function modstore.init()
25 modstore.tabnames = {}
27 table.insert(modstore.tabnames,"dialog_modstore_unsorted")
28 table.insert(modstore.tabnames,"dialog_modstore_search")
30 modstore.modsperpage = 5
32 modstore.basetexturedir = engine.get_texturepath() .. DIR_DELIM .. "base" ..
33 DIR_DELIM .. "pack" .. DIR_DELIM
35 modstore.lastmodtitle = ""
37 modstore.current_list = nil
40 --------------------------------------------------------------------------------
41 function modstore.nametoindex(name)
43 for i=1,#modstore.tabnames,1 do
44 if modstore.tabnames[i] == name then
52 --------------------------------------------------------------------------------
53 function modstore.gettab(tabname)
56 local is_modstore_tab = false
58 if tabname == "dialog_modstore_unsorted" then
59 retval = modstore.getmodlist(modstore.modlist_unsorted)
60 is_modstore_tab = true
63 if tabname == "dialog_modstore_search" then
64 retval = modstore.getsearchpage()
65 is_modstore_tab = true
68 if is_modstore_tab then
69 return modstore.tabheader(tabname) .. retval
72 if tabname == "modstore_mod_installed" then
73 return "size[6,2]label[0.25,0.25;Mod(s): " .. modstore.lastmodtitle ..
74 " installed successfully]" ..
75 "button[2.5,1.5;1,0.5;btn_confirm_mod_successfull;ok]"
78 if tabname == "modstore_downloading" then
79 return "size[6,2]label[0.25,0.25;Dowloading " .. modstore.lastmodtitle ..
86 --------------------------------------------------------------------------------
87 function modstore.tabheader(tabname)
88 local retval = "size[12,9.25]"
89 retval = retval .. "tabheader[-0.3,-0.99;modstore_tab;" ..
91 modstore.nametoindex(tabname) .. ";true;false]"
96 --------------------------------------------------------------------------------
97 function modstore.handle_buttons(current_tab,fields)
99 if fields["modstore_tab"] then
100 local index = tonumber(fields["modstore_tab"])
103 index <= #modstore.tabnames then
105 current_tab = modstore.tabnames[index],
111 modstore.modlist_page = 0
114 if fields["btn_modstore_page_up"] then
115 if modstore.current_list ~= nil and modstore.current_list.page > 0 then
116 modstore.current_list.page = modstore.current_list.page - 1
120 if fields["btn_modstore_page_down"] then
121 if modstore.current_list ~= nil and
122 modstore.current_list.page <modstore.current_list.pagecount-1 then
123 modstore.current_list.page = modstore.current_list.page +1
127 if fields["btn_hidden_close_download"] then
129 current_tab = "modstore_mod_installed",
135 if fields["btn_confirm_mod_successfull"] then
136 modstore.lastmodtitle = ""
138 current_tab = modstore.tabnames[1],
144 for i=1, modstore.modsperpage, 1 do
145 local installbtn = "btn_install_mod_" .. i
147 if fields[installbtn] then
149 modstore.current_list.page * modstore.modsperpage + i
151 if modstore.modlist_unsorted.data[modlistentry] ~= nil and
152 modstore.modlist_unsorted.data[modlistentry].details ~= nil then
154 local moddetails = modstore.modlist_unsorted.data[modlistentry].details
156 if modstore.lastmodtitle ~= "" then
157 modstore.lastmodtitle = modstore.lastmodtitle .. ", "
160 modstore.lastmodtitle = modstore.lastmodtitle .. moddetails.title
164 local fullurl = engine.setting_get("modstore_download_url") ..
165 param.moddetails.download_url
167 if engine.download_file(fullurl,param.filename) then
169 moddetails = param.moddetails,
170 filename = param.filename,
175 modtitle = param.title,
181 moddetails = moddetails,
182 filename = os.tempfolder() .. ".zip"
185 if result.successfull then
186 modmgr.installmod(result.filename,result.moddetails.basename)
187 os.remove(result.filename)
189 gamedata.errormessage = "Failed to download " .. result.moddetails.title
192 engine.button_handler({btn_hidden_close_download=true})
197 current_tab = "modstore_downloading",
199 show_buttons = false,
200 ignore_menu_quit = true
204 gamedata.errormessage =
205 "Internal modstore error please leave modstore and reopen! (Sorry)"
211 --------------------------------------------------------------------------------
212 function modstore.update_modlist()
213 modstore.modlist_unsorted = {}
214 modstore.modlist_unsorted.data = {}
215 modstore.modlist_unsorted.pagecount = 1
216 modstore.modlist_unsorted.page = 0
220 return engine.get_modstore_list()
224 if result ~= nil then
225 modstore.modlist_unsorted = {}
226 modstore.modlist_unsorted.data = result
228 if modstore.modlist_unsorted.data ~= nil then
229 modstore.modlist_unsorted.pagecount =
230 math.ceil((#modstore.modlist_unsorted.data / modstore.modsperpage))
232 modstore.modlist_unsorted.data = {}
233 modstore.modlist_unsorted.pagecount = 1
235 modstore.modlist_unsorted.page = 0
236 modstore.fetchdetails()
237 engine.event_handler("Refresh")
243 --------------------------------------------------------------------------------
244 function modstore.fetchdetails()
246 for i=1,#modstore.modlist_unsorted.data,1 do
249 param.details = engine.get_modstore_details(tostring(param.modid))
253 modid=modstore.modlist_unsorted.data[i].id,
258 modstore.modlist_unsorted ~= nil
259 and modstore.modlist_unsorted.data ~= nil and
260 modstore.modlist_unsorted.data[result.listindex] ~= nil and
261 modstore.modlist_unsorted.data[result.listindex].id ~= nil then
263 modstore.modlist_unsorted.data[result.listindex].details = result.details
264 engine.event_handler("Refresh")
270 --------------------------------------------------------------------------------
272 --------------------------------------------------------------------------------
273 function modstore.getmodlist(list)
275 retval = retval .. "label[10,-0.4;" .. fgettext("Page $1 of $2", list.page+1, list.pagecount) .. "]"
277 retval = retval .. "button[11.6,-0.1;0.5,0.5;btn_modstore_page_up;^]"
278 retval = retval .. "box[11.6,0.35;0.28,8.6;#000000]"
279 local scrollbarpos = 0.35 + (8.1/(list.pagecount-1)) * list.page
280 retval = retval .. "box[11.6," ..scrollbarpos .. ";0.28,0.5;#32CD32]"
281 retval = retval .. "button[11.6,9.0;0.5,0.5;btn_modstore_page_down;v]"
284 if #list.data < (list.page * modstore.modsperpage) then
288 local endmod = (list.page * modstore.modsperpage) + modstore.modsperpage
290 if (endmod > #list.data) then
294 for i=(list.page * modstore.modsperpage) +1, endmod, 1 do
296 local details = list.data[i].details
298 -- if details == nil then
299 -- details = modstore.get_details(list.data[i].id)
302 if details == nil then
304 details.title = list.data[i].title
307 details.description = ""
310 if details ~= nil then
311 local screenshot_ypos = (i-1 - (list.page * modstore.modsperpage))*1.9 +0.2
313 retval = retval .. "box[0," .. screenshot_ypos .. ";11.4,1.75;#FFFFFF]"
315 if details.basename then
317 if details.screenshot_url ~= nil and
318 details.screenshot_url ~= "" then
319 if list.data[i].texturename == nil then
320 local fullurl = engine.setting_get("modstore_download_url") ..
321 details.screenshot_url
322 local filename = os.tempfolder() .. "_MID_" .. list.data[i].id
323 list.data[i].texturename = "in progress"
326 param.successfull = engine.download_file(param.fullurl,param.filename)
333 modid = list.data[i].id
336 if modstore.modlist_unsorted and
337 modstore.modlist_unsorted.data and
338 #modstore.modlist_unsorted.data >= result.listindex and
339 modstore.modlist_unsorted.data[result.listindex].id == result.modid then
340 if result.successfull then
341 modstore.modlist_unsorted.data[result.listindex].texturename = result.filename
343 modstore.modlist_unsorted.data[result.listindex].texturename = modstore.basetexturedir .. "no_screenshot.png"
345 engine.event_handler("Refresh")
351 if list.data[i].texturename == nil then
352 list.data[i].texturename = modstore.basetexturedir .. "no_screenshot.png"
356 if list.data[i].texturename ~= nil and
357 list.data[i].texturename ~= "in progress" then
358 retval = retval .. "image[0,".. screenshot_ypos .. ";3,2;" ..
359 engine.formspec_escape(list.data[i].texturename) .. "]"
364 retval = retval .."label[2.75," .. screenshot_ypos .. ";" ..
365 engine.formspec_escape(details.title) .. " (" .. details.author .. ")]"
368 local descriptiony = screenshot_ypos + 0.5
369 retval = retval .. "textarea[3," .. descriptiony .. ";6.5,1.55;;" ..
370 engine.formspec_escape(details.description) .. ";]"
372 local ratingy = screenshot_ypos + 0.6
373 retval = retval .."label[9.1," .. ratingy .. ";" ..
374 fgettext("Rating") .. ":]"
375 retval = retval .. "label[11.1," .. ratingy .. ";" .. details.rating .."]"
377 if details.basename then
379 local buttony = screenshot_ypos + 1.2
380 local buttonnumber = (i - (list.page * modstore.modsperpage))
381 retval = retval .."button[9.1," .. buttony .. ";2.5,0.5;btn_install_mod_" .. buttonnumber .. ";"
383 if modmgr.mod_exists(details.basename) then
384 retval = retval .. fgettext("re-Install") .."]"
386 retval = retval .. fgettext("Install") .."]"
392 modstore.current_list = list
397 --------------------------------------------------------------------------------
398 function modstore.getsearchpage()
401 --TODO implement search!