Fix modstore/favourites hang by adding asynchronous lua job support

This commit is contained in:
sapier 2013-11-26 18:15:31 +01:00 committed by Nils Dagsson Moskopp
parent d79c9afa5b
commit 4bb6a41c04
Signed by: erle
GPG Key ID: A3BC671C35191080
5 changed files with 663 additions and 423 deletions

19
builtin/async_env.lua Normal file
View File

@ -0,0 +1,19 @@
engine.log("info","Initializing Asynchronous environment")
dofile(SCRIPTDIR .. DIR_DELIM .. "misc_helpers.lua")
function engine.job_processor(serialized_function, serialized_data)
local fct = marshal.decode(serialized_function)
local params = marshal.decode(serialized_data)
local retval = marshal.encode(nil)
if fct ~= nil and type(fct) == "function" then
local result = fct(params)
retval = marshal.encode(result)
else
engine.log("error","ASYNC WORKER: unable to deserialize function")
end
return retval,retval:len()
end

59
builtin/async_event.lua Normal file
View File

@ -0,0 +1,59 @@
local tbl = engine or minetest
tbl.async_jobs = {}
if engine ~= nil then
function tbl.async_event_handler(jobid, serialized_retval)
local retval = nil
if serialized_retval ~= "ERROR" then
retval= marshal.decode(serialized_retval)
else
tbl.log("error","Error fetching async result")
end
assert(type(tbl.async_jobs[jobid]) == "function")
tbl.async_jobs[jobid](retval)
tbl.async_jobs[jobid] = nil
end
else
minetest.register_globalstep(
function(dtime)
local list = tbl.get_finished_jobs()
for i=1,#list,1 do
local retval = marshal.decode(list[i].retval)
assert(type(tbl.async_jobs[jobid]) == "function")
tbl.async_jobs[list[i].jobid](retval)
tbl.async_jobs[list[i].jobid] = nil
end
end)
end
function tbl.handle_async(fct, parameters, callback)
--serialize fct
local serialized_fct = marshal.encode(fct)
assert(marshal.decode(serialized_fct) ~= nil)
--serialize parameters
local serialized_params = marshal.encode(parameters)
if serialized_fct == nil or
serialized_params == nil or
serialized_fct:len() == 0 or
serialized_params:len() == 0 then
return false
end
local jobid = tbl.do_async_callback( serialized_fct,
serialized_fct:len(),
serialized_params,
serialized_params:len())
tbl.async_jobs[jobid] = callback
return true
end

View File

@ -24,6 +24,7 @@ dofile(scriptpath .. DIR_DELIM .. "modstore.lua")
dofile(scriptpath .. DIR_DELIM .. "gamemgr.lua")
dofile(scriptpath .. DIR_DELIM .. "mm_textures.lua")
dofile(scriptpath .. DIR_DELIM .. "mm_menubar.lua")
dofile(scriptpath .. DIR_DELIM .. "async_event.lua")
menu = {}
local tabbuilder = {}
@ -196,6 +197,21 @@ function menu.render_texture_pack_list(list)
return retval
end
--------------------------------------------------------------------------------
function menu.asyncOnlineFavourites()
menu.favorites = {}
engine.handle_async(
function(param)
return engine.get_favorites("online")
end,
nil,
function(result)
menu.favorites = result
engine.event_handler("Refresh")
end
)
end
--------------------------------------------------------------------------------
function menu.init()
--init menu data
@ -208,7 +224,7 @@ function menu.init()
end
if engine.setting_getbool("public_serverlist") then
menu.favorites = engine.get_favorites("online")
menu.asyncOnlineFavourites()
else
menu.favorites = engine.get_favorites("local")
end
@ -489,7 +505,7 @@ function tabbuilder.handle_multiplayer_buttons(fields)
engine.setting_set("public_serverlist", fields["cb_public_serverlist"])
if engine.setting_getbool("public_serverlist") then
menu.favorites = engine.get_favorites("online")
menu.asyncOnlineFavourites()
else
menu.favorites = engine.get_favorites("local")
end
@ -583,6 +599,7 @@ function tabbuilder.handle_server_buttons(fields)
gamedata.selected_world = filterlist.get_raw_index(worldlist,selected)
engine.setting_set("port",gamedata.port)
menu.update_last_game(gamedata.selected_world)
engine.start()
end
@ -1124,6 +1141,12 @@ function tabbuilder.checkretval(retval)
if retval.skipformupdate ~= nil then
tabbuilder.skipformupdate = retval.skipformupdate
end
if retval.ignore_menu_quit == true then
tabbuilder.ignore_menu_quit = true
else
tabbuilder.ignore_menu_quit = false
end
end
end
@ -1194,6 +1217,10 @@ end
engine.event_handler = function(event)
if event == "MenuQuit" then
if tabbuilder.is_dialog then
if tabbuilder.ignore_menu_quit then
return
end
tabbuilder.is_dialog = false
tabbuilder.show_buttons = true
tabbuilder.current_tab = engine.setting_get("main_menu_tab")
@ -1203,6 +1230,10 @@ engine.event_handler = function(event)
engine.close()
end
end
if event == "Refresh" then
update_menu()
end
end
--------------------------------------------------------------------------------

View File

@ -54,7 +54,7 @@ function modmgr.extract(modfile)
local tempfolder = os.tempfolder()
if tempfolder ~= nil and
tempfodler ~= "" then
tempfolder ~= "" then
engine.create_dir(tempfolder)
engine.extract_zip(modfile.name,tempfolder)
return tempfolder

View File

@ -32,10 +32,11 @@ function modstore.init()
modstore.basetexturedir = engine.get_texturepath() .. DIR_DELIM .. "base" ..
DIR_DELIM .. "pack" .. DIR_DELIM
modstore.current_list = nil
modstore.lastmodtitle = ""
modstore.details_cache = {}
modstore.current_list = nil
end
--------------------------------------------------------------------------------
function modstore.nametoindex(name)
@ -60,8 +61,7 @@ function modstore.gettab(tabname)
end
if tabname == "dialog_modstore_search" then
retval = modstore.getsearchpage()
is_modstore_tab = true
end
@ -70,11 +70,16 @@ function modstore.gettab(tabname)
end
if tabname == "modstore_mod_installed" then
return "size[6,2]label[0.25,0.25;Mod: " .. modstore.lastmodtitle ..
return "size[6,2]label[0.25,0.25;Mod(s): " .. modstore.lastmodtitle ..
" installed successfully]" ..
"button[2.5,1.5;1,0.5;btn_confirm_mod_successfull;ok]"
end
if tabname == "modstore_downloading" then
return "size[6,2]label[0.25,0.25;Dowloading " .. modstore.lastmodtitle ..
" please wait]"
end
return ""
end
@ -91,8 +96,6 @@ end
--------------------------------------------------------------------------------
function modstore.handle_buttons(current_tab,fields)
modstore.lastmodtitle = ""
if fields["modstore_tab"] then
local index = tonumber(fields["modstore_tab"])
@ -121,7 +124,16 @@ function modstore.handle_buttons(current_tab,fields)
end
end
if fields["btn_hidden_close_download"] then
return {
current_tab = "modstore_mod_installed",
is_dialog = true,
show_buttons = false
}
end
if fields["btn_confirm_mod_successfull"] then
modstore.lastmodtitle = ""
return {
current_tab = modstore.tabnames[1],
is_dialog = true,
@ -136,27 +148,61 @@ function modstore.handle_buttons(current_tab,fields)
local modlistentry =
modstore.current_list.page * modstore.modsperpage + i
local moddetails = modstore.get_details(modstore.current_list.data[modlistentry].id)
if modstore.modlist_unsorted.data[modlistentry] ~= nil and
modstore.modlist_unsorted.data[modlistentry].details ~= nil then
local fullurl = engine.setting_get("modstore_download_url") ..
moddetails.download_url
local modfilename = os.tempfolder() .. ".zip"
local moddetails = modstore.modlist_unsorted.data[modlistentry].details
if engine.download_file(fullurl,modfilename) then
if modstore.lastmodtitle ~= "" then
modstore.lastmodtitle = modstore.lastmodtitle .. ", "
end
modmgr.installmod(modfilename,moddetails.basename)
modstore.lastmodtitle = modstore.lastmodtitle .. moddetails.title
os.remove(modfilename)
modstore.lastmodtitle = modstore.current_list.data[modlistentry].title
engine.handle_async(
function(param)
local fullurl = engine.setting_get("modstore_download_url") ..
param.moddetails.download_url
if engine.download_file(fullurl,param.filename) then
return {
moddetails = param.moddetails,
filename = param.filename,
successfull = true
}
else
return {
modtitle = param.title,
successfull = false
}
end
end,
{
moddetails = moddetails,
filename = os.tempfolder() .. ".zip"
},
function(result)
if result.successfull then
modmgr.installmod(result.filename,result.moddetails.basename)
os.remove(result.filename)
else
gamedata.errormessage = "Failed to download " .. result.moddetails.title
end
engine.button_handler({btn_hidden_close_download=true})
end
)
return {
current_tab = "modstore_mod_installed",
current_tab = "modstore_downloading",
is_dialog = true,
show_buttons = false
show_buttons = false,
ignore_menu_quit = true
}
else
gamedata.errormessage = "Unable to download " ..
moddetails.download_url .. " (internet connection?)"
gamedata.errormessage =
"Internal modstore error please leave modstore and reopen! (Sorry)"
end
end
end
@ -165,18 +211,64 @@ end
--------------------------------------------------------------------------------
function modstore.update_modlist()
modstore.modlist_unsorted = {}
modstore.modlist_unsorted.data = engine.get_modstore_list()
if modstore.modlist_unsorted.data ~= nil then
modstore.modlist_unsorted.pagecount =
math.ceil((#modstore.modlist_unsorted.data / modstore.modsperpage))
else
modstore.modlist_unsorted.data = {}
modstore.modlist_unsorted.pagecount = 1
end
modstore.modlist_unsorted.data = {}
modstore.modlist_unsorted.pagecount = 1
modstore.modlist_unsorted.page = 0
engine.handle_async(
function(param)
return engine.get_modstore_list()
end,
nil,
function(result)
if result ~= nil then
modstore.modlist_unsorted = {}
modstore.modlist_unsorted.data = result
if modstore.modlist_unsorted.data ~= nil then
modstore.modlist_unsorted.pagecount =
math.ceil((#modstore.modlist_unsorted.data / modstore.modsperpage))
else
modstore.modlist_unsorted.data = {}
modstore.modlist_unsorted.pagecount = 1
end
modstore.modlist_unsorted.page = 0
modstore.fetchdetails()
engine.event_handler("Refresh")
end
end
)
end
--------------------------------------------------------------------------------
function modstore.fetchdetails()
for i=1,#modstore.modlist_unsorted.data,1 do
engine.handle_async(
function(param)
param.details = engine.get_modstore_details(tostring(param.modid))
return param
end,
{
modid=modstore.modlist_unsorted.data[i].id,
listindex=i
},
function(result)
if result ~= nil and
modstore.modlist_unsorted ~= nil
and modstore.modlist_unsorted.data ~= nil and
modstore.modlist_unsorted.data[result.listindex] ~= nil and
modstore.modlist_unsorted.data[result.listindex].id ~= nil then
modstore.modlist_unsorted.data[result.listindex].details = result.details
engine.event_handler("Refresh")
end
end
)
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function modstore.getmodlist(list)
local retval = ""
@ -201,34 +293,73 @@ function modstore.getmodlist(list)
for i=(list.page * modstore.modsperpage) +1, endmod, 1 do
--getmoddetails
local details = modstore.get_details(list.data[i].id)
local details = list.data[i].details
-- if details == nil then
-- details = modstore.get_details(list.data[i].id)
-- end
if details == nil then
details = {}
details.title = list.data[i].title
details.author = ""
details.rating = -1
details.description = ""
end
if details ~= nil then
local screenshot_ypos = (i-1 - (list.page * modstore.modsperpage))*1.9 +0.2
retval = retval .. "box[0," .. screenshot_ypos .. ";11.4,1.75;#FFFFFF]"
--screenshot
if details.screenshot_url ~= nil and
details.screenshot_url ~= "" then
if list.data[i].texturename == nil then
local fullurl = engine.setting_get("modstore_download_url") ..
details.screenshot_url
local filename = os.tempfolder()
if engine.download_file(fullurl,filename) then
list.data[i].texturename = filename
if details.basename then
--screenshot
if details.screenshot_url ~= nil and
details.screenshot_url ~= "" then
if list.data[i].texturename == nil then
local fullurl = engine.setting_get("modstore_download_url") ..
details.screenshot_url
local filename = os.tempfolder() .. "_MID_" .. list.data[i].id
list.data[i].texturename = "in progress"
engine.handle_async(
function(param)
param.successfull = engine.download_file(param.fullurl,param.filename)
return param
end,
{
fullurl = fullurl,
filename = filename,
listindex = i,
modid = list.data[i].id
},
function(result)
if modstore.modlist_unsorted and
modstore.modlist_unsorted.data and
#modstore.modlist_unsorted.data >= result.listindex and
modstore.modlist_unsorted.data[result.listindex].id == result.modid then
if result.successfull then
modstore.modlist_unsorted.data[result.listindex].texturename = result.filename
else
modstore.modlist_unsorted.data[result.listindex].texturename = modstore.basetexturedir .. "no_screenshot.png"
end
engine.event_handler("Refresh")
end
end
)
end
else
if list.data[i].texturename == nil then
list.data[i].texturename = modstore.basetexturedir .. "no_screenshot.png"
end
end
end
if list.data[i].texturename == nil then
list.data[i].texturename = modstore.basetexturedir .. "no_screenshot.png"
if list.data[i].texturename ~= nil and
list.data[i].texturename ~= "in progress" then
retval = retval .. "image[0,".. screenshot_ypos .. ";3,2;" ..
engine.formspec_escape(list.data[i].texturename) .. "]"
end
end
retval = retval .. "image[0,".. screenshot_ypos .. ";3,2;" ..
engine.formspec_escape(list.data[i].texturename) .. "]"
--title + author
retval = retval .."label[2.75," .. screenshot_ypos .. ";" ..
engine.formspec_escape(details.title) .. " (" .. details.author .. ")]"
@ -239,18 +370,21 @@ function modstore.getmodlist(list)
engine.formspec_escape(details.description) .. ";]"
--rating
local ratingy = screenshot_ypos + 0.6
retval = retval .."label[10.1," .. ratingy .. ";" ..
fgettext("Rating") .. ": " .. details.rating .."]"
retval = retval .."label[9.1," .. ratingy .. ";" ..
fgettext("Rating") .. ":]"
retval = retval .. "label[11.1," .. ratingy .. ";" .. details.rating .."]"
--install button
local buttony = screenshot_ypos + 1.2
local buttonnumber = (i - (list.page * modstore.modsperpage))
retval = retval .."button[9.6," .. buttony .. ";2,0.5;btn_install_mod_" .. buttonnumber .. ";"
if details.basename then
--install button
local buttony = screenshot_ypos + 1.2
local buttonnumber = (i - (list.page * modstore.modsperpage))
retval = retval .."button[9.1," .. buttony .. ";2.5,0.5;btn_install_mod_" .. buttonnumber .. ";"
if modmgr.mod_exists(details.basename) then
retval = retval .. fgettext("re-Install") .."]"
else
retval = retval .. fgettext("Install") .."]"
if modmgr.mod_exists(details.basename) then
retval = retval .. fgettext("re-Install") .."]"
else
retval = retval .. fgettext("Install") .."]"
end
end
end
end
@ -261,14 +395,11 @@ function modstore.getmodlist(list)
end
--------------------------------------------------------------------------------
function modstore.get_details(modid)
function modstore.getsearchpage()
local retval = ""
if modstore.details_cache[modid] ~= nil then
return modstore.details_cache[modid]
end
--TODO implement search!
local retval = engine.get_modstore_details(tostring(modid))
modstore.details_cache[modid] = retval
return retval
return retval;
end