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 [parent=#modstore] init
25 function modstore.init()
26 modstore.tabnames = {}
28 table.insert(modstore.tabnames,"dialog_modstore_unsorted")
29 table.insert(modstore.tabnames,"dialog_modstore_search")
31 modstore.modsperpage = 5
33 modstore.basetexturedir = engine.get_texturepath() .. DIR_DELIM .. "base" ..
34 DIR_DELIM .. "pack" .. DIR_DELIM
36 modstore.lastmodtitle = ""
37 modstore.last_search = ""
39 modstore.searchlist = filterlist.create(
41 if modstore.modlist_unsorted ~= nil and
42 modstore.modlist_unsorted.data ~= nil then
43 return modstore.modlist_unsorted.data
47 function(element,modid)
48 if element.id == modid then
54 function(element,substring)
55 if substring == nil or
59 substring = substring:upper()
61 if element.title ~= nil and
62 element.title:upper():find(substring) ~= nil then
66 if element.details ~= nil and
67 element.details.author ~= nil and
68 element.details.author:upper():find(substring) ~= nil then
72 if element.details ~= nil and
73 element.details.description ~= nil and
74 element.details.description:upper():find(substring) ~= nil then
81 modstore.current_list = nil
84 --------------------------------------------------------------------------------
85 -- @function [parent=#modstore] nametoindex
86 function modstore.nametoindex(name)
88 for i=1,#modstore.tabnames,1 do
89 if modstore.tabnames[i] == name then
97 --------------------------------------------------------------------------------
98 -- @function [parent=#modstore] getsuccessfuldialog
99 function modstore.getsuccessfuldialog()
101 retval = retval .. "size[6,2]"
102 if modstore.lastmodentry ~= nil then
103 retval = retval .. "label[0,0.25;" .. fgettext("Successfully installed:") .. "]"
104 retval = retval .. "label[3,0.25;" .. modstore.lastmodentry.moddetails.title .. "]"
107 retval = retval .. "label[0,0.75;" .. fgettext("Shortname:") .. "]"
108 retval = retval .. "label[3,0.75;" .. engine.formspec_escape(modstore.lastmodentry.moddetails.basename) .. "]"
111 retval = retval .. "button[2.5,1.5;1,0.5;btn_confirm_mod_successfull;" .. fgettext("ok") .. "]"
117 --------------------------------------------------------------------------------
118 -- @function [parent=#modstore] gettab
119 function modstore.gettab(tabname)
122 local is_modstore_tab = false
124 if tabname == "dialog_modstore_unsorted" then
125 modstore.modsperpage = 5
126 retval = modstore.getmodlist(modstore.modlist_unsorted)
127 is_modstore_tab = true
130 if tabname == "dialog_modstore_search" then
131 retval = modstore.getsearchpage()
132 is_modstore_tab = true
135 if is_modstore_tab then
136 return modstore.tabheader(tabname) .. retval
139 if tabname == "modstore_mod_installed" then
140 return modstore.getsuccessfuldialog()
143 if tabname == "modstore_downloading" then
144 return "size[6,2]label[0.25,0.75;" .. fgettext("Downloading") ..
145 " " .. modstore.lastmodtitle .. " " ..
146 fgettext("please wait...") .. "]"
152 --------------------------------------------------------------------------------
153 -- @function [parent=#modstore] tabheader
154 function modstore.tabheader(tabname)
155 local retval = "size[12,10.25]"
156 retval = retval .. "tabheader[-0.3,-0.99;modstore_tab;" ..
157 "Unsorted,Search;" ..
158 modstore.nametoindex(tabname) .. ";true;false]" ..
159 "button[4,9.9;4,0.5;btn_modstore_close;" ..
160 fgettext("Close modstore") .. "]"
165 --------------------------------------------------------------------------------
166 -- @function [parent=#modstore] handle_buttons
167 function modstore.handle_buttons(current_tab,fields)
169 if fields["modstore_tab"] then
170 local index = tonumber(fields["modstore_tab"])
173 index <= #modstore.tabnames then
174 if modstore.tabnames[index] == "dialog_modstore_search" then
175 filterlist.set_filtercriteria(modstore.searchlist,modstore.last_search)
176 filterlist.refresh(modstore.searchlist)
177 modstore.modsperpage = 4
178 modstore.currentlist = {
181 math.ceil(filterlist.size(modstore.searchlist) / modstore.modsperpage),
182 data = filterlist.get_list(modstore.searchlist),
187 current_tab = modstore.tabnames[index],
195 if fields["btn_modstore_page_up"] then
196 if modstore.current_list ~= nil and modstore.current_list.page > 0 then
197 modstore.current_list.page = modstore.current_list.page - 1
201 if fields["btn_modstore_page_down"] then
202 if modstore.current_list ~= nil and
203 modstore.current_list.page <modstore.current_list.pagecount-1 then
204 modstore.current_list.page = modstore.current_list.page +1
208 if fields["btn_hidden_close_download"] ~= nil then
209 if fields["btn_hidden_close_download"].successfull then
210 modstore.lastmodentry = fields["btn_hidden_close_download"]
212 current_tab = "modstore_mod_installed",
217 modstore.lastmodtitle = ""
219 current_tab = modstore.tabnames[1],
226 if fields["btn_confirm_mod_successfull"] then
227 modstore.lastmodentry = nil
228 modstore.lastmodtitle = ""
230 current_tab = modstore.tabnames[1],
236 if fields["btn_modstore_search"] or
237 (fields["key_enter"] and fields["te_modstore_search"] ~= nil) then
238 modstore.last_search = fields["te_modstore_search"]
239 filterlist.set_filtercriteria(modstore.searchlist,fields["te_modstore_search"])
240 filterlist.refresh(modstore.searchlist)
241 modstore.currentlist = {
243 pagecount = math.ceil(filterlist.size(modstore.searchlist) / modstore.modsperpage),
244 data = filterlist.get_list(modstore.searchlist),
249 if fields["btn_modstore_close"] then
253 current_tab = engine.setting_get("main_menu_tab")
257 for key,value in pairs(fields) do
258 local foundat = key:find("btn_install_mod_")
259 if ( foundat == 1) then
260 local modid = tonumber(key:sub(17))
261 for i=1,#modstore.modlist_unsorted.data,1 do
262 if modstore.modlist_unsorted.data[i].id == modid then
263 local moddetails = modstore.modlist_unsorted.data[i].details
265 if modstore.lastmodtitle ~= "" then
266 modstore.lastmodtitle = modstore.lastmodtitle .. ", "
269 modstore.lastmodtitle = modstore.lastmodtitle .. moddetails.title
274 local fullurl = engine.setting_get("modstore_download_url") ..
275 param.moddetails.download_url
277 if param.version ~= nil then
279 for i=1,#param.moddetails.versions, 1 do
280 if param.moddetails.versions[i].date:sub(1,10) == param.version then
281 fullurl = engine.setting_get("modstore_download_url") ..
282 param.moddetails.versions[i].download_url
289 moddetails = param.moddetails,
295 if engine.download_file(fullurl,param.filename) then
297 texturename = param.texturename,
298 moddetails = param.moddetails,
299 filename = param.filename,
304 moddetails = param.moddetails,
310 moddetails = moddetails,
311 version = fields["dd_version" .. modid],
312 filename = os.tempfolder() .. "_MODNAME_" .. moddetails.basename .. ".zip",
313 texturename = modstore.modlist_unsorted.data[i].texturename
316 if result.successfull then
317 modmgr.installmod(result.filename,result.moddetails.basename)
318 os.remove(result.filename)
320 gamedata.errormessage = "Failed to download " .. result.moddetails.title
323 if gamedata.errormessage == nil then
324 engine.button_handler({btn_hidden_close_download=result})
326 engine.button_handler({btn_hidden_close_download={successfull=false}})
332 current_tab = "modstore_downloading",
334 show_buttons = false,
335 ignore_menu_quit = true
344 --------------------------------------------------------------------------------
345 -- @function [parent=#modstore] update_modlist
346 function modstore.update_modlist()
347 modstore.modlist_unsorted = {}
348 modstore.modlist_unsorted.data = {}
349 modstore.modlist_unsorted.pagecount = 1
350 modstore.modlist_unsorted.page = 0
354 return engine.get_modstore_list()
358 if result ~= nil then
359 modstore.modlist_unsorted = {}
360 modstore.modlist_unsorted.data = result
362 if modstore.modlist_unsorted.data ~= nil then
363 modstore.modlist_unsorted.pagecount =
364 math.ceil((#modstore.modlist_unsorted.data / modstore.modsperpage))
366 modstore.modlist_unsorted.data = {}
367 modstore.modlist_unsorted.pagecount = 1
369 modstore.modlist_unsorted.page = 0
370 modstore.fetchdetails()
371 engine.event_handler("Refresh")
377 --------------------------------------------------------------------------------
378 -- @function [parent=#modstore] fetchdetails
379 function modstore.fetchdetails()
381 for i=1,#modstore.modlist_unsorted.data,1 do
384 param.details = engine.get_modstore_details(tostring(param.modid))
388 modid=modstore.modlist_unsorted.data[i].id,
393 modstore.modlist_unsorted ~= nil
394 and modstore.modlist_unsorted.data ~= nil and
395 modstore.modlist_unsorted.data[result.listindex] ~= nil and
396 modstore.modlist_unsorted.data[result.listindex].id ~= nil then
398 modstore.modlist_unsorted.data[result.listindex].details = result.details
399 engine.event_handler("Refresh")
406 --------------------------------------------------------------------------------
407 -- @function [parent=#modstore] getscreenshot
408 function modstore.getscreenshot(ypos,listentry)
410 if listentry.details ~= nil and
411 (listentry.details.screenshot_url == nil or
412 listentry.details.screenshot_url == "") then
414 if listentry.texturename == nil then
415 listentry.texturename = modstore.basetexturedir .. "no_screenshot.png"
418 return "image[0,".. ypos .. ";3,2;" ..
419 engine.formspec_escape(listentry.texturename) .. "]"
422 if listentry.details ~= nil and
423 listentry.texturename == nil then
424 --make sure we don't download multiple times
425 listentry.texturename = "in progress"
427 --prepare url and filename
428 local fullurl = engine.setting_get("modstore_download_url") ..
429 listentry.details.screenshot_url
430 local filename = os.tempfolder() .. "_MID_" .. listentry.id
434 --first param is downloadfct
436 param.successfull = engine.download_file(param.fullurl,param.filename)
439 --second parameter is data passed to async job
445 --integrate result to raw list
447 if result.successfull then
449 for i=1,#modstore.modlist_unsorted.data,1 do
450 if modstore.modlist_unsorted.data[i].id == result.modid then
452 modstore.modlist_unsorted.data[i].texturename = result.filename
457 engine.event_handler("Refresh")
459 engine.log("error","got screenshot but didn't find matching mod: " .. result.modid)
466 if listentry.texturename ~= nil and
467 listentry.texturename ~= "in progress" then
468 return "image[0,".. ypos .. ";3,2;" ..
469 engine.formspec_escape(listentry.texturename) .. "]"
475 --------------------------------------------------------------------------------
476 --@function [parent=#modstore] getshortmodinfo
477 function modstore.getshortmodinfo(ypos,listentry,details)
480 retval = retval .. "box[0," .. ypos .. ";11.4,1.75;#FFFFFF]"
483 retval = retval .. modstore.getscreenshot(ypos,listentry)
486 retval = retval .."label[2.75," .. ypos .. ";" ..
487 engine.formspec_escape(details.title) .. " (" .. details.author .. ")]"
490 local descriptiony = ypos + 0.5
491 retval = retval .. "textarea[3," .. descriptiony .. ";6.5,1.55;;" ..
492 engine.formspec_escape(details.description) .. ";]"
496 retval = retval .."label[7," .. ratingy .. ";" ..
497 fgettext("Rating") .. ":]"
498 retval = retval .. "label[8.7," .. ratingy .. ";" .. details.rating .."]"
500 --versions (IMPORTANT has to be defined AFTER rating)
501 if details.versions ~= nil and
502 #details.versions > 1 then
503 local versiony = ypos + 0.05
504 retval = retval .. "dropdown[9.1," .. versiony .. ";2.48,0.25;dd_version" .. details.id .. ";"
506 for i=1,#details.versions , 1 do
507 if versions ~= "" then
508 versions = versions .. ","
511 versions = versions .. details.versions[i].date:sub(1,10)
513 retval = retval .. versions .. ";1]"
516 if details.basename then
518 local buttony = ypos + 1.2
519 retval = retval .."button[9.1," .. buttony .. ";2.5,0.5;btn_install_mod_" .. details.id .. ";"
521 if modmgr.mod_exists(details.basename) then
522 retval = retval .. fgettext("re-Install") .."]"
524 retval = retval .. fgettext("Install") .."]"
531 --------------------------------------------------------------------------------
532 --@function [parent=#modstore] getmodlist
533 function modstore.getmodlist(list,yoffset)
535 modstore.current_list = list
537 if #list.data == 0 then
541 if yoffset == nil then
546 scrollbar = scrollbar .. "label[0.1,9.5;"
547 .. fgettext("Page $1 of $2", list.page+1, list.pagecount) .. "]"
548 scrollbar = scrollbar .. "box[11.6," .. (yoffset + 0.35) .. ";0.28,"
549 .. (8.6 - yoffset) .. ";#000000]"
550 local scrollbarpos = (yoffset + 0.75) +
551 ((7.7 -yoffset)/(list.pagecount-1)) * list.page
552 scrollbar = scrollbar .. "box[11.6," ..scrollbarpos .. ";0.28,0.5;#32CD32]"
553 scrollbar = scrollbar .. "button[11.6," .. (yoffset + (0.3))
554 .. ";0.5,0.5;btn_modstore_page_up;^]"
555 scrollbar = scrollbar .. "button[11.6," .. 9.0
556 .. ";0.5,0.5;btn_modstore_page_down;v]"
560 local endmod = (list.page * modstore.modsperpage) + modstore.modsperpage
562 if (endmod > #list.data) then
566 for i=(list.page * modstore.modsperpage) +1, endmod, 1 do
568 local details = list.data[i].details
570 if details == nil then
572 details.title = list.data[i].title
575 details.description = ""
578 if details ~= nil then
579 local screenshot_ypos =
580 yoffset +(i-1 - (list.page * modstore.modsperpage))*1.9 +0.2
582 retval = retval .. modstore.getshortmodinfo(screenshot_ypos,
588 return retval .. scrollbar
591 --------------------------------------------------------------------------------
592 --@function [parent=#modstore] getsearchpage
593 function modstore.getsearchpage()
597 if modstore.last_search ~= nil then
598 search = modstore.last_search
602 "button[9.5,0.2;2.5,0.5;btn_modstore_search;".. fgettext("Search") .. "]" ..
603 "field[0.5,0.5;9,0.5;te_modstore_search;;" .. search .. "]"
607 modstore.modsperpage = 4
610 modstore.currentlist,