]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/mainmenu/store.lua
Create a filesystem abstraction layer for CSM and only allow accessing files that...
[dragonfireclient.git] / builtin / mainmenu / store.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 [parent=#modstore] init
25 function modstore.init(size, unsortedmods, searchmods)
26
27         modstore.mods_on_unsorted_page = unsortedmods
28         modstore.mods_on_search_page = searchmods
29         modstore.modsperpage = modstore.mods_on_unsorted_page
30
31         modstore.basetexturedir = core.get_texturepath() .. DIR_DELIM .. "base" ..
32                                                 DIR_DELIM .. "pack" .. DIR_DELIM
33
34         modstore.lastmodtitle = ""
35         modstore.last_search = ""
36
37         modstore.searchlist = filterlist.create(
38                 function()
39                         if modstore.modlist_unsorted ~= nil and
40                                 modstore.modlist_unsorted.data ~= nil then
41                                 return modstore.modlist_unsorted.data
42                         end
43                         return {}
44                 end,
45                 function(element,modid)
46                         if element.id == modid then
47                                 return true
48                         end
49                         return false
50                 end, --compare fct
51                 nil, --uid match fct
52                 function(element,substring)
53                         if substring == nil or
54                                 substring == "" then
55                                 return false
56                         end
57                         substring = substring:upper()
58
59                         if element.title ~= nil and
60                                 element.title:upper():find(substring) ~= nil then
61                                 return true
62                         end
63
64                         if element.details ~= nil and
65                                 element.details.author ~= nil and
66                                 element.details.author:upper():find(substring) ~= nil then
67                                 return true
68                         end
69
70                         if element.details ~= nil and
71                                 element.details.description ~= nil and
72                                 element.details.description:upper():find(substring) ~= nil then
73                                 return true
74                         end
75                         return false
76                 end --filter fct
77                 )
78
79         modstore.current_list = nil
80
81         modstore.tv_store = tabview_create("modstore",size,{x=0,y=0})
82
83         modstore.tv_store:set_global_event_handler(modstore.handle_events)
84
85         modstore.tv_store:add(
86                 {
87                 name = "unsorted",
88                 caption = fgettext("Unsorted"),
89                 cbf_formspec       = modstore.unsorted_tab,
90                 cbf_button_handler = modstore.handle_buttons,
91                 on_change          =
92                         function() modstore.modsperpage = modstore.mods_on_unsorted_page end
93                 }
94         )
95
96         modstore.tv_store:add(
97                 {
98                 name = "search",
99                 caption            = fgettext("Search"),
100                 cbf_formspec       = modstore.getsearchpage,
101                 cbf_button_handler = modstore.handle_buttons,
102                 on_change          = modstore.activate_search_tab
103                 }
104         )
105 end
106
107 --------------------------------------------------------------------------------
108 -- @function [parent=#modstore] nametoindex
109 function modstore.nametoindex(name)
110
111         for i=1,#modstore.tabnames,1 do
112                 if modstore.tabnames[i] == name then
113                         return i
114                 end
115         end
116
117         return 1
118 end
119
120 --------------------------------------------------------------------------------
121 -- @function [parent=#modstore] showdownloading
122 function modstore.showdownloading(title)
123         local new_dlg = dialog_create("store_downloading",
124                 function(data)
125                         return "size[6,2]label[0.25,0.75;" ..
126                                 fgettext("Downloading $1, please wait...", data.title) .. "]"
127                 end,
128                 function(this,fields)
129                         if fields["btn_hidden_close_download"] ~= nil then
130                                 if fields["btn_hidden_close_download"].successfull then
131                                         modstore.lastmodentry = fields["btn_hidden_close_download"]
132                                         modstore.successfulldialog(this)
133                                 else
134                                         this.parent:show()
135                                         this:delete()
136                                         modstore.lastmodtitle = ""
137                                 end
138
139                                 return true
140                         end
141
142                         return false
143                 end,
144                 nil)
145
146         new_dlg:set_parent(modstore.tv_store)
147         modstore.tv_store:hide()
148         new_dlg.data.title = title
149         new_dlg:show()
150 end
151
152 --------------------------------------------------------------------------------
153 -- @function [parent=#modstore] successfulldialog
154 function modstore.successfulldialog(downloading_dlg)
155         local new_dlg = dialog_create("store_downloading",
156                 function(data)
157                         local retval = ""
158                         retval = retval .. "size[6,2,true]"
159                         if modstore.lastmodentry ~= nil then
160                                 retval = retval .. "label[0,0.25;" .. fgettext("Successfully installed:") .. "]"
161                                 retval = retval .. "label[3,0.25;" .. modstore.lastmodentry.moddetails.title .. "]"
162                                 retval = retval .. "label[0,0.75;" .. fgettext("Shortname:") .. "]"
163                                 retval = retval .. "label[3,0.75;" .. core.formspec_escape(modstore.lastmodentry.moddetails.basename) .. "]"
164                         end
165                         retval = retval .. "button[2.2,1.5;1.5,0.5;btn_confirm_mod_successfull;" .. fgettext("Ok") .. "]"
166                         return retval
167                 end,
168                 function(this,fields)
169                         if fields["btn_confirm_mod_successfull"] ~= nil then
170                                 this.parent:show()
171                                 downloading_dlg:delete()
172                                 this:delete()
173
174                                 return true
175                         end
176
177                         return false
178                 end,
179                 nil)
180
181         new_dlg:set_parent(modstore.tv_store)
182         modstore.tv_store:hide()
183         new_dlg:show()
184 end
185
186 --------------------------------------------------------------------------------
187 -- @function [parent=#modstore] handle_buttons
188 function modstore.handle_buttons(parent, fields, name, data)
189
190         if fields["btn_modstore_page_up"] then
191                 if modstore.current_list ~= nil and modstore.current_list.page > 0 then
192                         modstore.current_list.page = modstore.current_list.page - 1
193                 end
194                 return true
195         end
196
197         if fields["btn_modstore_page_down"] then
198                 if modstore.current_list ~= nil and
199                         modstore.current_list.page <modstore.current_list.pagecount-1 then
200                         modstore.current_list.page = modstore.current_list.page +1
201                 end
202                 return true
203         end
204
205         if fields["btn_modstore_search"] or
206                 (fields["key_enter"] and fields["te_modstore_search"] ~= nil) then
207                 modstore.last_search = fields["te_modstore_search"]
208                 filterlist.set_filtercriteria(modstore.searchlist,fields["te_modstore_search"])
209                 filterlist.refresh(modstore.searchlist)
210                 modstore.currentlist = {
211                         page = 0,
212                         pagecount =  math.ceil(filterlist.size(modstore.searchlist) / modstore.modsperpage),
213                         data = filterlist.get_list(modstore.searchlist),
214                 }
215                 return true
216         end
217
218         if fields["btn_modstore_close"] then
219                 local maintab = ui.find_by_name("maintab")
220                 parent:hide()
221                 maintab:show()
222                 return true
223         end
224
225         for key,value in pairs(fields) do
226                 local foundat = key:find("btn_install_mod_")
227                 if ( foundat == 1) then
228                         local modid = tonumber(key:sub(17))
229                         for i=1,#modstore.modlist_unsorted.data,1 do
230                                 if modstore.modlist_unsorted.data[i].id == modid then
231                                         local moddetails = modstore.modlist_unsorted.data[i].details
232                                         modstore.lastmodtitle = moddetails.title
233
234                                         if not core.handle_async(
235                                                 function(param)
236                                                         local fullurl = core.settings:get("modstore_download_url") ..
237                                                                                         param.moddetails.download_url
238
239                                                         if param.version ~= nil then
240                                                                 local found = false
241                                                                 for i=1,#param.moddetails.versions, 1 do
242                                                                         if param.moddetails.versions[i].date:sub(1,10) == param.version then
243                                                                                 fullurl = core.settings:get("modstore_download_url") ..
244                                                                                                                 param.moddetails.versions[i].download_url
245                                                                                 found = true
246                                                                         end
247                                                                 end
248
249                                                                 if not found then
250                                                                         core.log("error","no download url found for version " .. dump(param.version))
251                                                                         return {
252                                                                                 moddetails = param.moddetails,
253                                                                                 successfull = false
254                                                                         }
255                                                                 end
256                                                         end
257
258                                                         if core.download_file(fullurl,param.filename) then
259                                                                 return {
260                                                                         texturename = param.texturename,
261                                                                         moddetails = param.moddetails,
262                                                                         filename = param.filename,
263                                                                         successfull = true
264                                                                 }
265                                                         else
266                                                                 core.log("error","downloading " .. dump(fullurl) .. " failed")
267                                                                 return {
268                                                                         moddetails = param.moddetails,
269                                                                         successfull = false
270                                                                 }
271                                                         end
272                                                 end,
273                                                 {
274                                                         moddetails = moddetails,
275                                                         version = fields["dd_version" .. modid],
276                                                         filename = os.tempfolder() .. "_MODNAME_" .. moddetails.basename .. ".zip",
277                                                         texturename = modstore.modlist_unsorted.data[i].texturename
278                                                 },
279                                                 function(result)
280                                                         --print("Result from async: " .. dump(result.successfull))
281                                                         if result.successfull then
282                                                                 modmgr.installmod(result.filename,result.moddetails.basename)
283                                                                 os.remove(result.filename)
284                                                         else
285                                                                 gamedata.errormessage = "Failed to download " .. result.moddetails.title
286                                                         end
287
288                                                         if gamedata.errormessage == nil then
289                                                                 core.button_handler({btn_hidden_close_download=result})
290                                                         else
291                                                                 core.button_handler({btn_hidden_close_download={successfull=false}})
292                                                         end
293                                                 end
294                                         ) then
295                                                 print("ERROR: async event failed")
296                                                 gamedata.errormessage = "Failed to download " .. modstore.lastmodtitle
297                                         end
298
299                                         modstore.showdownloading(modstore.lastmodtitle)
300                                         return true
301                                 end
302                         end
303                         return true
304                 end
305         end
306
307         return false
308 end
309
310 --------------------------------------------------------------------------------
311 -- @function [parent=#modstore] handle_events
312 function modstore.handle_events(this,event)
313         if (event == "MenuQuit") then
314                 this:hide()
315                 return true
316         end
317 end
318
319 --------------------------------------------------------------------------------
320 -- @function [parent=#modstore] update_modlist
321 function modstore.update_modlist()
322         modstore.modlist_unsorted = {}
323         modstore.modlist_unsorted.data = {}
324         modstore.modlist_unsorted.pagecount = 1
325         modstore.modlist_unsorted.page = 0
326
327         core.handle_async(
328                 function(param)
329                         return core.get_modstore_list()
330                 end,
331                 nil,
332                 function(result)
333                         if result ~= nil then
334                                 modstore.modlist_unsorted = {}
335                                 modstore.modlist_unsorted.data = result
336
337                                 if modstore.modlist_unsorted.data ~= nil then
338                                         modstore.modlist_unsorted.pagecount =
339                                                 math.ceil((#modstore.modlist_unsorted.data / modstore.modsperpage))
340                                 else
341                                         modstore.modlist_unsorted.data = {}
342                                         modstore.modlist_unsorted.pagecount = 1
343                                 end
344                                 modstore.modlist_unsorted.page = 0
345                                 modstore.fetchdetails()
346                                 core.event_handler("Refresh")
347                         end
348                 end
349         )
350 end
351
352 --------------------------------------------------------------------------------
353 -- @function [parent=#modstore] fetchdetails
354 function modstore.fetchdetails()
355
356         for i=1,#modstore.modlist_unsorted.data,1 do
357                 core.handle_async(
358                 function(param)
359                         param.details = core.get_modstore_details(tostring(param.modid))
360                         return param
361                 end,
362                 {
363                         modid=modstore.modlist_unsorted.data[i].id,
364                         listindex=i
365                 },
366                 function(result)
367                         if result ~= nil and
368                                 modstore.modlist_unsorted ~= nil
369                                 and modstore.modlist_unsorted.data ~= nil and
370                                 modstore.modlist_unsorted.data[result.listindex] ~= nil and
371                                 modstore.modlist_unsorted.data[result.listindex].id ~= nil then
372
373                                 modstore.modlist_unsorted.data[result.listindex].details = result.details
374                                 core.event_handler("Refresh")
375                         end
376                 end
377                 )
378         end
379 end
380
381 --------------------------------------------------------------------------------
382 -- @function [parent=#modstore] getscreenshot
383 function modstore.getscreenshot(ypos,listentry)
384
385         if      listentry.details ~= nil and
386                 (listentry.details.screenshot_url == nil or
387                 listentry.details.screenshot_url == "") then
388
389                 if listentry.texturename == nil then
390                         listentry.texturename = defaulttexturedir .. "no_screenshot.png"
391                 end
392
393                 return "image[0,".. ypos .. ";3,2;" ..
394                         core.formspec_escape(listentry.texturename) .. "]"
395         end
396
397         if listentry.details ~= nil and
398                 listentry.texturename == nil then
399                 --make sure we don't download multiple times
400                 listentry.texturename = "in progress"
401
402                 --prepare url and filename
403                 local fullurl = core.settings:get("modstore_download_url") ..
404                                         listentry.details.screenshot_url
405                 local filename = os.tempfolder() .. "_MID_" .. listentry.id
406
407                 --trigger download
408                 core.handle_async(
409                         --first param is downloadfct
410                         function(param)
411                                 param.successfull = core.download_file(param.fullurl,param.filename)
412                                 return param
413                         end,
414                         --second parameter is data passed to async job
415                         {
416                                 fullurl = fullurl,
417                                 filename = filename,
418                                 modid = listentry.id
419                         },
420                         --integrate result to raw list
421                         function(result)
422                                 if result.successfull then
423                                         local found = false
424                                         for i=1,#modstore.modlist_unsorted.data,1 do
425                                                 if modstore.modlist_unsorted.data[i].id == result.modid then
426                                                         found = true
427                                                         modstore.modlist_unsorted.data[i].texturename = result.filename
428                                                         break
429                                                 end
430                                         end
431                                         if found then
432                                                 core.event_handler("Refresh")
433                                         else
434                                                 core.log("error","got screenshot but didn't find matching mod: " .. result.modid)
435                                         end
436                                 end
437                         end
438                 )
439         end
440
441         if listentry.texturename ~= nil and
442                 listentry.texturename ~= "in progress" then
443                 return "image[0,".. ypos .. ";3,2;" ..
444                         core.formspec_escape(listentry.texturename) .. "]"
445         end
446
447         return ""
448 end
449
450 --------------------------------------------------------------------------------
451 --@function [parent=#modstore] getshortmodinfo
452 function modstore.getshortmodinfo(ypos,listentry,details)
453         local retval = ""
454
455         retval = retval .. "box[0," .. ypos .. ";11.4,1.75;#FFFFFF]"
456
457         --screenshot
458         retval = retval .. modstore.getscreenshot(ypos,listentry)
459
460         --title + author
461         retval = retval .."label[2.75," .. ypos .. ";" ..
462                 core.formspec_escape(details.title) .. " (" .. details.author .. ")]"
463
464         --description
465         local descriptiony = ypos + 0.5
466         retval = retval .. "textarea[3," .. descriptiony .. ";6.5,1.55;;" ..
467                 core.formspec_escape(details.description) .. ";]"
468
469         --rating
470         local ratingy = ypos
471         retval = retval .."label[7," .. ratingy .. ";" ..
472                                         fgettext("Rating") .. ":]"
473         retval = retval .. "label[8.7," .. ratingy .. ";" .. details.rating .."]"
474
475         --versions (IMPORTANT has to be defined AFTER rating)
476         if details.versions ~= nil and
477                 #details.versions > 1 then
478                 local versiony = ypos + 0.05
479                 retval = retval .. "dropdown[9.1," .. versiony .. ";2.48,0.25;dd_version" .. details.id .. ";"
480                 local versions = ""
481                 for i=1,#details.versions , 1 do
482                         if versions ~= "" then
483                                 versions = versions .. ","
484                         end
485
486                         versions = versions .. details.versions[i].date:sub(1,10)
487                 end
488                 retval = retval .. versions .. ";1]"
489         end
490
491         if details.basename then
492                 --install button
493                 local buttony = ypos + 1.2
494                 retval = retval .."button[9.1," .. buttony .. ";2.5,0.5;btn_install_mod_" .. details.id .. ";"
495
496                 if modmgr.mod_exists(details.basename) then
497                         retval = retval .. fgettext("re-Install") .."]"
498                 else
499                         retval = retval .. fgettext("Install") .."]"
500                 end
501         end
502
503         return retval
504 end
505
506 --------------------------------------------------------------------------------
507 --@function [parent=#modstore] getmodlist
508 function modstore.getmodlist(list,yoffset)
509         modstore.current_list = list
510
511         if yoffset == nil then
512                 yoffset = 0
513         end
514
515         local sb_y_start = 0.2 + yoffset
516         local sb_y_end   = (modstore.modsperpage * 1.75) + ((modstore.modsperpage-1) * 0.15)
517         local close_button = "button[4," .. (sb_y_end + 0.3 + yoffset) ..
518                         ";4,0.5;btn_modstore_close;" .. fgettext("Close store") .. "]"
519
520         if #list.data == 0 then
521                 return close_button
522         end
523
524         local scrollbar = ""
525         scrollbar = scrollbar .. "label[0.1,".. (sb_y_end + 0.25 + yoffset) ..";"
526                                 .. fgettext("Page $1 of $2", list.page+1, list.pagecount) .. "]"
527         scrollbar = scrollbar .. "box[11.6," .. sb_y_start .. ";0.28," .. sb_y_end .. ";#000000]"
528         local scrollbarpos = (sb_y_start + 0.5) +
529                                 ((sb_y_end -1.6)/(list.pagecount-1)) * list.page
530         scrollbar = scrollbar .. "box[11.6," ..scrollbarpos .. ";0.28,0.5;#32CD32]"
531         scrollbar = scrollbar .. "button[11.6," .. (sb_y_start)
532                                 .. ";0.5,0.5;btn_modstore_page_up;^]"
533         scrollbar = scrollbar .. "button[11.6," .. (sb_y_start + sb_y_end - 0.5)
534                                 .. ";0.5,0.5;btn_modstore_page_down;v]"
535
536         local retval = ""
537
538         local endmod = (list.page * modstore.modsperpage) + modstore.modsperpage
539
540         if (endmod > #list.data) then
541                 endmod = #list.data
542         end
543
544         for i=(list.page * modstore.modsperpage) +1, endmod, 1 do
545                 --getmoddetails
546                 local details = list.data[i].details
547
548                 if details == nil then
549                         details = {}
550                         details.title = list.data[i].title
551                         details.author = ""
552                         details.rating = -1
553                         details.description = ""
554                 end
555
556                 if details ~= nil then
557                         local screenshot_ypos =
558                                 yoffset +(i-1 - (list.page * modstore.modsperpage))*1.9 +0.2
559
560                         retval = retval .. modstore.getshortmodinfo(screenshot_ypos,
561                                                                                                                 list.data[i],
562                                                                                                                 details)
563                 end
564         end
565
566         return retval .. scrollbar .. close_button
567 end
568
569 --------------------------------------------------------------------------------
570 --@function [parent=#modstore] getsearchpage
571 function modstore.getsearchpage(tabview, name, tabdata)
572         local retval = ""
573         local search = ""
574
575         if modstore.last_search ~= nil then
576                 search = modstore.last_search
577         end
578
579         retval = retval ..
580                 "button[9.5,0.2;2.5,0.5;btn_modstore_search;".. fgettext("Search") .. "]" ..
581                 "field[0.5,0.5;9,0.5;te_modstore_search;;" .. search .. "]"
582
583         retval = retval ..
584                 modstore.getmodlist(
585                         modstore.currentlist,
586                         1.75)
587
588         return retval;
589 end
590
591 --------------------------------------------------------------------------------
592 --@function [parent=#modstore] unsorted_tab
593 function modstore.unsorted_tab()
594         return modstore.getmodlist(modstore.modlist_unsorted)
595 end
596
597 --------------------------------------------------------------------------------
598 --@function [parent=#modstore] activate_search_tab
599 function modstore.activate_search_tab(type, old_tab, new_tab)
600
601         if old_tab == new_tab then
602                 return
603         end
604         filterlist.set_filtercriteria(modstore.searchlist,modstore.last_search)
605         filterlist.refresh(modstore.searchlist)
606         modstore.modsperpage = modstore.mods_on_search_page
607         modstore.currentlist = {
608                 page = 0,
609                 pagecount =
610                         math.ceil(filterlist.size(modstore.searchlist) / modstore.modsperpage),
611                 data = filterlist.get_list(modstore.searchlist),
612         }
613 end
614