1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-09-15 18:57:08 +00:00

Replace C++ mainmenu by formspec powered one

This commit is contained in:
sapier 2013-06-23 18:30:21 +02:00 committed by kwolekr
parent fe4ce03d52
commit 967121a34b
37 changed files with 8058 additions and 3949 deletions

309
builtin/gamemgr.lua Normal file
View file

@ -0,0 +1,309 @@
--Minetest
--Copyright (C) 2013 sapier
--
--This program is free software; you can redistribute it and/or modify
--it under the terms of the GNU Lesser General Public License as published by
--the Free Software Foundation; either version 2.1 of the License, or
--(at your option) any later version.
--
--This program is distributed in the hope that it will be useful,
--but WITHOUT ANY WARRANTY; without even the implied warranty of
--MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
--GNU Lesser General Public License for more details.
--
--You should have received a copy of the GNU Lesser General Public License along
--with this program; if not, write to the Free Software Foundation, Inc.,
--51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
gamemgr = {}
--------------------------------------------------------------------------------
function gamemgr.dialog_new_game()
local retval =
"label[2,2;Game Name]"..
"field[4.5,2.4;6,0.5;te_game_name;;]" ..
"button[5,4.2;2.6,0.5;new_game_confirm;Create]" ..
"button[7.5,4.2;2.8,0.5;new_game_cancel;Cancel]"
return retval
end
--------------------------------------------------------------------------------
function gamemgr.handle_games_buttons(fields)
if fields["gamelist"] ~= nil then
local event = explode_textlist_event(fields["gamelist"])
gamemgr.selected_game = event.index
end
if fields["btn_game_mgr_edit_game"] ~= nil then
return {
is_dialog = true,
show_buttons = false,
current_tab = "dialog_edit_game"
}
end
if fields["btn_game_mgr_new_game"] ~= nil then
return {
is_dialog = true,
show_buttons = false,
current_tab = "dialog_new_game"
}
end
return nil
end
--------------------------------------------------------------------------------
function gamemgr.handle_new_game_buttons(fields)
if fields["new_game_confirm"] and
fields["te_game_name"] ~= nil and
fields["te_game_name"] ~= "" then
local gamepath = engine.get_gamepath()
if gamepath ~= nil and
gamepath ~= "" then
local gamefolder = cleanup_path(fields["te_game_name"])
--TODO check for already existing first
engine.create_dir(gamepath .. DIR_DELIM .. gamefolder)
engine.create_dir(gamepath .. DIR_DELIM .. gamefolder .. DIR_DELIM .. "mods")
engine.create_dir(gamepath .. DIR_DELIM .. gamefolder .. DIR_DELIM .. "menu")
local gameconf =
io.open(gamepath .. DIR_DELIM .. gamefolder .. DIR_DELIM .. "game.conf","w")
if gameconf then
gameconf:write("name = " .. fields["te_game_name"])
gameconf:close()
end
end
end
return {
is_dialog = false,
show_buttons = true,
current_tab = engine.setting_get("main_menu_tab")
}
end
--------------------------------------------------------------------------------
function gamemgr.handle_edit_game_buttons(fields)
local current_game = gamemgr.get_game(gamemgr.selected_game)
if fields["btn_close_edit_game"] ~= nil or
current_game == nil then
return {
is_dialog = false,
show_buttons = true,
current_tab = engine.setting_get("main_menu_tab")
}
end
if fields["btn_remove_mod_from_game"] ~= nil then
gamemgr.delete_mod(current_game,engine.get_textlist_index("mods_current"))
end
if fields["btn_add_mod_to_game"] ~= nil then
local modindex = engine.get_textlist_index("mods_available")
if modindex > 0 and
modindex <= #modmgr.global_mods then
local sourcepath =
engine.get_modpath() .. DIR_DELIM .. modmgr.global_mods[modindex]
gamemgr.add_mod(current_game,sourcepath)
end
end
return nil
end
--------------------------------------------------------------------------------
function gamemgr.add_mod(gamespec,sourcepath)
if gamespec.gamemods_path ~= nil and
gamespec.gamemods_path ~= "" then
local modname = get_last_folder(sourcepath)
engine.copy_dir(sourcepath,gamespec.gamemods_path .. DIR_DELIM .. modname);
end
end
--------------------------------------------------------------------------------
function gamemgr.delete_mod(gamespec,modindex)
if gamespec.gamemods_path ~= nil and
gamespec.gamemods_path ~= "" then
local game_mods = {}
get_mods(gamespec.gamemods_path,game_mods)
if modindex > 0 and
#game_mods >= modindex then
local modname = game_mods[modindex]
if modname:find("<MODPACK>") ~= nil then
modname = modname:sub(0,modname:find("<") -2)
end
local modpath = gamespec.gamemods_path .. DIR_DELIM .. modname
if modpath:sub(0,gamespec.gamemods_path:len()) == gamespec.gamemods_path then
engine.delete_dir(modpath)
end
end
end
end
--------------------------------------------------------------------------------
function gamemgr.get_game_mods(gamespec)
local retval = ""
if gamespec.gamemods_path ~= nil and
gamespec.gamemods_path ~= "" then
local game_mods = {}
get_mods(gamespec.gamemods_path,game_mods)
for i=1,#game_mods,1 do
if retval ~= "" then
retval = retval..","
end
retval = retval .. game_mods[i]
end
end
return retval
end
--------------------------------------------------------------------------------
function gamemgr.gettab(name)
local retval = ""
if name == "dialog_edit_game" then
retval = retval .. gamemgr.dialog_edit_game()
end
if name == "dialog_new_game" then
retval = retval .. gamemgr.dialog_new_game()
end
if name == "game_mgr" then
retval = retval .. gamemgr.tab()
end
return retval
end
--------------------------------------------------------------------------------
function gamemgr.tab()
if gamemgr.selected_game == nil then
gamemgr.selected_game = 1
end
local retval =
"vertlabel[0,-0.25;GAMES]" ..
"label[1,-0.25;Games:]" ..
"textlist[1,0.25;4.5,4.4;gamelist;" ..
gamemgr.gamelist() ..
";" .. gamemgr.selected_game .. "]"
local current_game = gamemgr.get_game(gamemgr.selected_game)
if current_game ~= nil then
if current_game.menuicon_path ~= nil and
current_game.menuicon_path ~= "" then
retval = retval ..
"image[5.8,-0.25;2,2;" .. current_game.menuicon_path .. "]"
end
retval = retval ..
"field[8,-0.25;6,2;;" .. current_game.name .. ";]"..
"label[6,1.4;Mods:]" ..
"button[9.7,1.5;2,0.2;btn_game_mgr_edit_game;edit game]" ..
"textlist[6,2;5.5,3.3;game_mgr_modlist;"
.. gamemgr.get_game_mods(current_game) ..";0]" ..
"button[1,4.75;3.2,0.5;btn_game_mgr_new_game;new game]"
end
return retval
end
--------------------------------------------------------------------------------
function gamemgr.dialog_edit_game()
local current_game = gamemgr.get_game(gamemgr.selected_game)
if current_game ~= nil then
local retval =
"vertlabel[0,-0.25;EDIT GAME]" ..
"label[0,-0.25;" .. current_game.name .. "]" ..
"button[11.55,-0.2;0.75,0.5;btn_close_edit_game;x]"
if current_game.menuicon_path ~= nil and
current_game.menuicon_path ~= "" then
retval = retval ..
"image[5.25,0;2,2;" .. current_game.menuicon_path .. "]"
end
retval = retval ..
"textlist[0.5,0.5;4.5,4.3;mods_current;"
.. gamemgr.get_game_mods(current_game) ..";0]"
retval = retval ..
"textlist[7,0.5;4.5,4.3;mods_available;"
.. modmgr.get_mods_list() .. ";0]"
retval = retval ..
"button[0.55,4.95;4.7,0.5;btn_remove_mod_from_game;Remove selected mod]"
retval = retval ..
"button[7.05,4.95;4.7,0.5;btn_add_mod_to_game;<<-- Add mod]"
return retval
end
end
--------------------------------------------------------------------------------
function gamemgr.handle_buttons(tab,fields)
local retval = nil
if tab == "dialog_edit_game" then
retval = gamemgr.handle_edit_game_buttons(fields)
end
if tab == "dialog_new_game" then
retval = gamemgr.handle_new_game_buttons(fields)
end
if tab == "game_mgr" then
retval = gamemgr.handle_games_buttons(fields)
end
return retval
end
--------------------------------------------------------------------------------
function gamemgr.get_game(index)
if index > 0 and index <= #gamemgr.games then
return gamemgr.games[index]
end
return nil
end
--------------------------------------------------------------------------------
function gamemgr.update_gamelist()
gamemgr.games = engine.get_games()
end
--------------------------------------------------------------------------------
function gamemgr.gamelist()
local retval = ""
if #gamemgr.games > 0 then
retval = retval .. gamemgr.games[1].id
for i=2,#gamemgr.games,1 do
retval = retval .. "," .. gamemgr.games[i].name
end
end
return retval
end

1190
builtin/mainmenu.lua Normal file

File diff suppressed because it is too large Load diff

105
builtin/mainmenu_helper.lua Normal file
View file

@ -0,0 +1,105 @@
--------------------------------------------------------------------------------
function dump(o, dumped)
dumped = dumped or {}
if type(o) == "number" then
return tostring(o)
elseif type(o) == "string" then
return string.format("%q", o)
elseif type(o) == "table" then
if dumped[o] then
return "<circular reference>"
end
dumped[o] = true
local t = {}
for k,v in pairs(o) do
t[#t+1] = "" .. k .. " = " .. dump(v, dumped)
end
return "{" .. table.concat(t, ", ") .. "}"
elseif type(o) == "boolean" then
return tostring(o)
elseif type(o) == "function" then
return "<function>"
elseif type(o) == "userdata" then
return "<userdata>"
elseif type(o) == "nil" then
return "nil"
else
error("cannot dump a " .. type(o))
return nil
end
end
--------------------------------------------------------------------------------
function string:split(sep)
local sep, fields = sep or ",", {}
local pattern = string.format("([^%s]+)", sep)
self:gsub(pattern, function(c) fields[#fields+1] = c end)
return fields
end
--------------------------------------------------------------------------------
function string:trim()
return (self:gsub("^%s*(.-)%s*$", "%1"))
end
--------------------------------------------------------------------------------
engine.get_game = function(index)
local games = game.get_games()
if index > 0 and index <= #games then
return games[index]
end
return nil
end
--------------------------------------------------------------------------------
function fs_escape_string(text)
if text ~= nil then
while (text:find("\r\n") ~= nil) do
local newtext = text:sub(1,text:find("\r\n")-1)
newtext = newtext .. " " .. text:sub(text:find("\r\n")+3)
text = newtext
end
while (text:find("\n") ~= nil) do
local newtext = text:sub(1,text:find("\n")-1)
newtext = newtext .. " " .. text:sub(text:find("\n")+1)
text = newtext
end
while (text:find("\r") ~= nil) do
local newtext = text:sub(1,text:find("\r")-1)
newtext = newtext .. " " .. text:sub(text:find("\r")+1)
text = newtext
end
text = text:gsub("%[","%[%[")
text = text:gsub("]","]]")
text = text:gsub(";"," ")
end
return text
end
--------------------------------------------------------------------------------
function explode_textlist_event(text)
local retval = {}
retval.typ = "INV"
local parts = text:split(":")
if #parts == 2 then
retval.typ = parts[1]:trim()
retval.index= tonumber(parts[2]:trim())
if type(retval.index) ~= "number" then
retval.typ = "INV"
end
end
return retval
end

881
builtin/modmgr.lua Normal file
View file

@ -0,0 +1,881 @@
--Minetest
--Copyright (C) 2013 sapier
--
--This program is free software; you can redistribute it and/or modify
--it under the terms of the GNU Lesser General Public License as published by
--the Free Software Foundation; either version 2.1 of the License, or
--(at your option) any later version.
--
--This program is distributed in the hope that it will be useful,
--but WITHOUT ANY WARRANTY; without even the implied warranty of
--MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
--GNU Lesser General Public License for more details.
--
--You should have received a copy of the GNU Lesser General Public License along
--with this program; if not, write to the Free Software Foundation, Inc.,
--51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
--------------------------------------------------------------------------------
function get_mods(path,retval,basefolder)
local mods = engine.get_dirlist(path,true)
for i=1,#mods,1 do
local filename = path .. DIR_DELIM .. mods[i] .. DIR_DELIM .. "modpack.txt"
local modpackfile,error = io.open(filename,"r")
local name = mods[i]
if basefolder ~= nil and
basefolder ~= "" then
name = basefolder .. DIR_DELIM .. mods[i]
end
if modpackfile ~= nil then
modpackfile:close()
table.insert(retval,name .. " <MODPACK>")
get_mods(path .. DIR_DELIM .. name,retval,name)
else
table.insert(retval,name)
end
end
end
--modmanager implementation
modmgr = {}
--------------------------------------------------------------------------------
function modmgr.extract(modfile)
if modfile.type == "zip" then
local tempfolder = os.tempfolder()
if tempfolder ~= nil and
tempfodler ~= "" then
engine.create_dir(tempfolder)
engine.extract_zip(modfile.name,tempfolder)
return tempfolder
end
end
end
-------------------------------------------------------------------------------
function modmgr.getbasefolder(temppath)
if temppath == nil then
return {
type = "invalid",
path = ""
}
end
local testfile = io.open(temppath .. DIR_DELIM .. "init.lua","r")
if testfile ~= nil then
testfile:close()
return {
type="mod",
path=temppath
}
end
testfile = io.open(temppath .. DIR_DELIM .. "modpack.txt","r")
if testfile ~= nil then
testfile:close()
return {
type="modpack",
path=temppath
}
end
local subdirs = engine.get_dirlist(temppath,true)
--only single mod or modpack allowed
if #subdirs ~= 1 then
return {
type = "invalid",
path = ""
}
end
testfile =
io.open(temppath .. DIR_DELIM .. subdirs[1] ..DIR_DELIM .."init.lua","r")
if testfile ~= nil then
testfile:close()
return {
type="mod",
path= temppath .. DIR_DELIM .. subdirs[1]
}
end
testfile =
io.open(temppath .. DIR_DELIM .. subdirs[1] ..DIR_DELIM .."modpack.txt","r")
if testfile ~= nil then
testfile:close()
return {
type="modpack",
path=temppath .. DIR_DELIM .. subdirs[1]
}
end
return {
type = "invalid",
path = ""
}
end
--------------------------------------------------------------------------------
function modmgr.isValidModname(modpath)
if modpath:find("-") ~= nil then
return false
end
return true
end
--------------------------------------------------------------------------------
function modmgr.parse_register_line(line)
local pos1 = line:find("\"")
local pos2 = nil
if pos1 ~= nil then
pos2 = line:find("\"",pos1+1)
end
if pos1 ~= nil and pos2 ~= nil then
local item = line:sub(pos1+1,pos2-1)
if item ~= nil and
item ~= "" then
local pos3 = item:find(":")
if pos3 ~= nil then
return item:sub(1,pos3-1)
end
end
end
return nil
end
--------------------------------------------------------------------------------
function modmgr.parse_dofile_line(modpath,line)
local pos1 = line:find("\"")
local pos2 = nil
if pos1 ~= nil then
pos2 = line:find("\"",pos1+1)
end
if pos1 ~= nil and pos2 ~= nil then
local filename = line:sub(pos1+1,pos2-1)
if filename ~= nil and
filename ~= "" and
filename:find(".lua") then
return modmgr.identify_modname(modpath,filename)
end
end
return nil
end
--------------------------------------------------------------------------------
function modmgr.update_global_mods()
local modpath = engine.get_modpath()
modmgr.global_mods = {}
if modpath ~= nil and
modpath ~= "" then
get_mods(modpath,modmgr.global_mods)
end
end
--------------------------------------------------------------------------------
function modmgr.get_mods_list()
local toadd = ""
modmgr.update_global_mods()
if modmgr.global_mods ~= nil then
for i=1,#modmgr.global_mods,1 do
if toadd ~= "" then
toadd = toadd..","
end
toadd = toadd .. modmgr.global_mods[i]
end
end
return toadd
end
--------------------------------------------------------------------------------
function modmgr.mod_exists(basename)
modmgr.update_global_mods()
if modmgr.global_mods ~= nil then
for i=1,#modmgr.global_mods,1 do
if modmgr.global_mods[i] == basename then
return true
end
end
end
return false
end
--------------------------------------------------------------------------------
function modmgr.identify_modname(modpath,filename)
local testfile = io.open(modpath .. DIR_DELIM .. filename,"r")
if testfile ~= nil then
local line = testfile:read()
while line~= nil do
local modname = nil
if line:find("minetest.register_tool") then
modname = modmgr.parse_register_line(line)
end
if line:find("minetest.register_craftitem") then
modname = modmgr.parse_register_line(line)
end
if line:find("minetest.register_node") then
modname = modmgr.parse_register_line(line)
end
if line:find("dofile") then
modname = modmgr.parse_dofile_line(modpath,line)
end
if modname ~= nil then
testfile:close()
return modname
end
line = testfile:read()
end
testfile:close()
end
return nil
end
--------------------------------------------------------------------------------
function modmgr.tab()
if modmgr.selected_mod == nil then
modmgr.selected_mod = 1
end
local retval =
"vertlabel[0,-0.25;MODS]" ..
"label[0.8,-0.25;Installed Mods:]" ..
"textlist[0.75,0.25;4.5,4.3;modlist;" ..
modmgr.get_mods_list() ..
";" .. modmgr.selected_mod .. "]"
retval = retval ..
"button[1,4.85;2,0.5;btn_mod_mgr_install_local;Install]" ..
"button[3,4.85;2,0.5;btn_mod_mgr_download;Download]"
if #modmgr.global_mods >= modmgr.selected_mod and
modmgr.global_mods[modmgr.selected_mod]:find("<MODPACK>") then
retval = retval .. "button[10,4.85;2,0.5;btn_mod_mgr_rename_modpack;Rename]"
end
if #modmgr.global_mods >= modmgr.selected_mod then
local modpath = engine.get_modpath()
--show dependencys
if modmgr.global_mods[modmgr.selected_mod]:find("<MODPACK>") == nil then
retval = retval ..
"label[6,1.9;Depends:]" ..
"textlist[6,2.4;5.7,2;deplist;"
toadd = modmgr.get_dependencys(modpath .. DIR_DELIM ..
modmgr.global_mods[modmgr.selected_mod])
retval = retval .. toadd .. ";0;true,false]"
--TODO read modinfo
end
--show delete button
retval = retval .. "button[8,4.85;2,0.5;btn_mod_mgr_delete_mod;Delete]"
end
return retval
end
--------------------------------------------------------------------------------
function modmgr.dialog_rename_modpack()
local modname = modmgr.global_mods[modmgr.selected_mod]
modname = modname:sub(0,modname:find("<") -2)
local retval =
"label[1.75,1;Rename Modpack:]"..
"field[4.5,1.4;6,0.5;te_modpack_name;;" ..
modname ..
"]" ..
"button[5,4.2;2.6,0.5;dlg_rename_modpack_confirm;Accept]" ..
"button[7.5,4.2;2.8,0.5;dlg_rename_modpack_cancel;Cancel]"
return retval
end
--------------------------------------------------------------------------------
function modmgr.precheck()
if modmgr.global_mods == nil then
modmgr.update_global_mods()
end
if modmgr.world_config_selected_world == nil then
modmgr.world_config_selected_world = 1
end
if modmgr.world_config_selected_mod == nil then
modmgr.world_config_selected_mod = 1
end
if modmgr.hide_gamemods == nil then
modmgr.hide_gamemods = true
end
end
--------------------------------------------------------------------------------
function modmgr.get_worldmod_idx()
if not modmgr.hide_gamemods then
return modmgr.world_config_selected_mod - #modmgr.worldconfig.game_mods
else
return modmgr.world_config_selected_mod
end
end
--------------------------------------------------------------------------------
function modmgr.is_gamemod()
if not modmgr.hide_gamemods then
if modmgr.world_config_selected_mod <= #modmgr.worldconfig.game_mods then
return true
else
return false
end
else
return false
end
end
--------------------------------------------------------------------------------
function modmgr.render_worldmodlist()
local retval = ""
for i=1,#modmgr.global_mods,1 do
local parts = modmgr.global_mods[i]:split(DIR_DELIM)
local shortname = parts[#parts]
if modmgr.worldconfig.global_mods[shortname] then
retval = retval .. "#GRN" .. modmgr.global_mods[i] .. ","
else
retval = retval .. modmgr.global_mods[i] .. ","
end
end
return retval
end
--------------------------------------------------------------------------------
function modmgr.render_gamemodlist()
local retval = ""
for i=1,#modmgr.worldconfig.game_mods,1 do
retval = retval ..
"#BLU" .. modmgr.worldconfig.game_mods[i] .. ","
end
return retval
end
--------------------------------------------------------------------------------
function modmgr.dialog_configure_world()
modmgr.precheck()
local modpack_selected = false
local gamemod_selected = modmgr.is_gamemod()
local modname = ""
local modfolder = ""
local shortname = ""
if not gamemod_selected then
local worldmodidx = modmgr.get_worldmod_idx()
modname = modmgr.global_mods[worldmodidx]
if modname:find("<MODPACK>") ~= nil then
modname = modname:sub(0,modname:find("<") -2)
modpack_selected = true
end
local parts = modmgr.global_mods[worldmodidx]:split(DIR_DELIM)
shortname = parts[#parts]
modfolder = engine.get_modpath() .. DIR_DELIM .. modname
end
local worldspec = engine.get_worlds()[modmgr.world_config_selected_world]
local retval =
"size[11,6.5]" ..
"label[1.5,-0.25;World: " .. worldspec.name .. "]"
if modmgr.hide_gamemods then
retval = retval .. "checkbox[5.5,6.15;cb_hide_gamemods;Hide Game;true]"
else
retval = retval .. "checkbox[5.5,6.15;cb_hide_gamemods;Hide Game;false]"
end
retval = retval ..
"button[9.25,6.35;2,0.5;btn_config_world_save;Save]" ..
"button[7.4,6.35;2,0.5;btn_config_world_cancel;Cancel]" ..
"textlist[5.5,-0.25;5.5,6.5;world_config_modlist;"
if not modmgr.hide_gamemods then
retval = retval .. modmgr.render_gamemodlist()
end
retval = retval .. modmgr.render_worldmodlist()
retval = retval .. ";" .. modmgr.world_config_selected_mod .."]"
if not gamemod_selected then
retval = retval ..
"label[0,0.45;Mod:]" ..
"label[0.75,0.45;" .. modname .. "]" ..
"label[0,1.5;depends on:]" ..
"textlist[0,2;5,2;world_config_depends;" ..
modmgr.get_dependencys(modfolder) .. ";0]" ..
"label[0,4;depends on:]" ..
"textlist[0,4.5;5,2;world_config_is_required;;0]"
if modpack_selected then
retval = retval ..
"button[-0.05,1.05;2,0.5;btn_cfgw_enable_all;Enable All]" ..
"button[3.25,1.05;2,0.5;btn_cfgw_disable_all;Disable All]"
else
retval = retval ..
"checkbox[0,0.8;cb_mod_enabled;enabled;"
if modmgr.worldconfig.global_mods[shortname] then
print("checkbox " .. shortname .. " enabled")
retval = retval .. "true"
else
print("checkbox " .. shortname .. " disabled")
retval = retval .. "false"
end
retval = retval .. "]"
end
end
return retval
end
--------------------------------------------------------------------------------
function modmgr.handle_buttons(tab,fields)
local retval = nil
if tab == "mod_mgr" then
retval = modmgr.handle_modmgr_buttons(fields)
end
if tab == "dialog_rename_modpack" then
retval = modmgr.handle_rename_modpack_buttons(fields)
end
if tab == "dialog_delete_mod" then
retval = modmgr.handle_delete_mod_buttons(fields)
end
if tab == "dialog_configure_world" then
retval = modmgr.handle_configure_world_buttons(fields)
end
return retval
end
--------------------------------------------------------------------------------
function modmgr.get_dependencys(modfolder)
local filename = modfolder ..
DIR_DELIM .. "depends.txt"
local dependencyfile = io.open(filename,"r")
local toadd = ""
if dependencyfile then
local dependency = dependencyfile:read("*l")
while dependency do
if toadd ~= "" then
toadd = toadd .. ","
end
toadd = toadd .. dependency
dependency = dependencyfile:read()
end
dependencyfile:close()
else
print(filename .. " not found")
end
return toadd
end
--------------------------------------------------------------------------------
function modmgr.get_worldconfig(worldpath)
local filename = worldpath ..
DIR_DELIM .. "world.mt"
local worldfile = io.open(filename,"r")
local worldconfig = {}
worldconfig.global_mods = {}
worldconfig.game_mods = {}
if worldfile then
local dependency = worldfile:read("*l")
while dependency do
local parts = dependency:split("=")
local key = parts[1]:trim()
if key == "gameid" then
worldconfig.id = parts[2]:trim()
else
local key = parts[1]:trim():sub(10)
if parts[2]:trim() == "true" then
print("found enabled mod: >" .. key .. "<")
worldconfig.global_mods[key] = true
else
print("found disabled mod: >" .. key .. "<")
worldconfig.global_mods[key] = false
end
end
dependency = worldfile:read("*l")
end
worldfile:close()
else
print(filename .. " not found")
end
--read gamemods
local gamemodpath = engine.get_gamepath() .. DIR_DELIM .. worldconfig.id .. DIR_DELIM .. "mods"
print("reading game mods from: " .. dump(gamemodpath))
get_mods(gamemodpath,worldconfig.game_mods)
return worldconfig
end
--------------------------------------------------------------------------------
function modmgr.handle_modmgr_buttons(fields)
local retval = {
tab = nil,
is_dialog = nil,
show_buttons = nil,
}
if fields["modlist"] ~= nil then
local event = explode_textlist_event(fields["modlist"])
modmgr.selected_mod = event.index
end
if fields["btn_mod_mgr_install_local"] ~= nil then
engine.show_file_open_dialog("mod_mgt_open_dlg","Select Mod File:")
end
if fields["btn_mod_mgr_download"] ~= nil then
retval.current_tab = "dialog_modstore_unsorted"
retval.is_dialog = true
retval.show_buttons = false
return retval
end
if fields["btn_mod_mgr_rename_modpack"] ~= nil then
retval.current_tab = "dialog_rename_modpack"
retval.is_dialog = true
retval.show_buttons = false
return retval
end
if fields["btn_mod_mgr_delete_mod"] ~= nil then
retval.current_tab = "dialog_delete_mod"
retval.is_dialog = true
retval.show_buttons = false
return retval
end
if fields["mod_mgt_open_dlg_accepted"] ~= nil and
fields["mod_mgt_open_dlg_accepted"] ~= "" then
modmgr.installmod(fields["mod_mgt_open_dlg_accepted"],nil)
end
return nil;
end
--------------------------------------------------------------------------------
function modmgr.installmod(modfilename,basename)
local modfile = identify_filetype(modfilename)
local modpath = modmgr.extract(modfile)
if modpath == nil then
gamedata.errormessage = "Install Mod: file: " .. modfile.name ..
"\nInstall Mod: unsupported filetype \"" .. modfile.type .. "\""
return
end
local basefolder = modmgr.getbasefolder(modpath)
if basefolder.type == "modpack" then
local clean_path = nil
if basename ~= nil then
clean_path = "mp_" .. basename
end
if clean_path == nil then
clean_path = get_last_folder(cleanup_path(basefolder.path))
end
if clean_path ~= nil then
local targetpath = engine.get_modpath() .. DIR_DELIM .. clean_path
engine.copy_dir(basefolder.path,targetpath)
else
gamedata.errormessage = "Install Mod: unable to find suitable foldername for modpack "
.. modfilename
end
end
if basefolder.type == "mod" then
local targetfolder = basename
if targetfolder == nil then
targetfolder = modmgr.identify_modname(basefolder.path,"init.lua")
end
--if heuristic failed try to use current foldername
if targetfolder == nil then
targetfolder = get_last_folder(basefolder.path)
end
if targetfolder ~= nil and modmgr.isValidModname(targetfolder) then
local targetpath = engine.get_modpath() .. DIR_DELIM .. targetfolder
engine.copy_dir(basefolder.path,targetpath)
else
gamedata.errormessage = "Install Mod: unable to find real modname for: "
.. modfilename
end
end
engine.delete_dir(modpath)
end
--------------------------------------------------------------------------------
function modmgr.handle_rename_modpack_buttons(fields)
local oldname = modmgr.global_mods[modmgr.selected_mod]
oldname = oldname:sub(0,oldname:find("<") -2)
if fields["dlg_rename_modpack_confirm"] ~= nil then
local oldpath = engine.get_modpath() .. DIR_DELIM .. oldname
local targetpath = engine.get_modpath() .. DIR_DELIM .. fields["te_modpack_name"]
engine.copy_dir(oldpath,targetpath,false)
end
return {
is_dialog = false,
show_buttons = true,
current_tab = engine.setting_get("main_menu_tab")
}
end
--------------------------------------------------------------------------------
function modmgr.handle_configure_world_buttons(fields)
if fields["world_config_modlist"] ~= nil then
local event = explode_textlist_event(fields["world_config_modlist"])
modmgr.world_config_selected_mod = event.index
end
if fields["cb_mod_enabled"] ~= nil then
local index = modmgr.get_worldmod_idx()
local modname = modmgr.global_mods[index]
local parts = modmgr.global_mods[index]:split(DIR_DELIM)
local shortname = parts[#parts]
if fields["cb_mod_enabled"] == "true" then
modmgr.worldconfig.global_mods[shortname] = true
else
modmgr.worldconfig.global_mods[shortname] = false
end
end
if fields["cb_hide_gamemods"] ~= nil then
if fields["cb_hide_gamemods"] == "true" then
modmgr.hide_gamemods = true
else
modmgr.hide_gamemods = false
end
end
if fields["btn_config_world_save"] then
local worldspec = engine.get_worlds()[modmgr.world_config_selected_world]
local filename = worldspec.path ..
DIR_DELIM .. "world.mt"
local worldfile = io.open(filename,"w")
if worldfile then
worldfile:write("gameid = " .. modmgr.worldconfig.id .. "\n")
for key,value in pairs(modmgr.worldconfig.global_mods) do
if value then
worldfile:write("load_mod_" .. key .. " = true" .. "\n")
else
worldfile:write("load_mod_" .. key .. " = false" .. "\n")
end
end
worldfile:close()
end
modmgr.worldconfig = nil
return {
is_dialog = false,
show_buttons = true,
current_tab = engine.setting_get("main_menu_tab")
}
end
if fields["btn_config_world_cancel"] then
modmgr.worldconfig = nil
return {
is_dialog = false,
show_buttons = true,
current_tab = engine.setting_get("main_menu_tab")
}
end
if fields["btn_cfgw_enable_all"] then
local worldmodidx = modmgr.get_worldmod_idx()
modname = modmgr.global_mods[worldmodidx]
modname = modname:sub(0,modname:find("<") -2)
for i=1,#modmgr.global_mods,1 do
if modmgr.global_mods[i]:find("<MODPACK>") == nil then
local modpackpart = modmgr.global_mods[i]:sub(0,modname:len())
if modpackpart == modname then
local parts = modmgr.global_mods[i]:split(DIR_DELIM)
local shortname = parts[#parts]
modmgr.worldconfig.global_mods[shortname] = true
end
end
end
end
if fields["btn_cfgw_disable_all"] then
local worldmodidx = modmgr.get_worldmod_idx()
modname = modmgr.global_mods[worldmodidx]
modname = modname:sub(0,modname:find("<") -2)
for i=1,#modmgr.global_mods,1 do
local modpackpart = modmgr.global_mods[i]:sub(0,modname:len())
if modpackpart == modname then
local parts = modmgr.global_mods[i]:split(DIR_DELIM)
local shortname = parts[#parts]
modmgr.worldconfig.global_mods[shortname] = nil
end
end
end
return nil
end
--------------------------------------------------------------------------------
function modmgr.handle_delete_mod_buttons(fields)
local modname = modmgr.global_mods[modmgr.selected_mod]
if modname:find("<MODPACK>") ~= nil then
modname = modname:sub(0,modname:find("<") -2)
end
if fields["dlg_delete_mod_confirm"] ~= nil then
local oldpath = engine.get_modpath() .. DIR_DELIM .. modname
if oldpath ~= nil and
oldpath ~= "" and
oldpath ~= engine.get_modpath() then
engine.delete_dir(oldpath)
end
end
return {
is_dialog = false,
show_buttons = true,
current_tab = engine.setting_get("main_menu_tab")
}
end
--------------------------------------------------------------------------------
function modmgr.dialog_delete_mod()
local modname = modmgr.global_mods[modmgr.selected_mod]
if modname:find("<MODPACK>") ~= nil then
modname = modname:sub(0,modname:find("<") -2)
end
local retval =
"field[1.75,1;10,3;;Are you sure you want to delete ".. modname .. "?;]"..
"button[4,4.2;1,0.5;dlg_delete_mod_confirm;Yes]" ..
"button[6.5,4.2;3,0.5;dlg_delete_mod_cancel;No of course not!]"
return retval
end
--------------------------------------------------------------------------------
function modmgr.init_worldconfig()
local worldspec = engine.get_worlds()[modmgr.world_config_selected_world]
if worldspec ~= nil then
--read worldconfig
modmgr.worldconfig = modmgr.get_worldconfig(worldspec.path)
if modmgr.worldconfig.id == nil or
modmgr.worldconfig.id == "" then
modmgr.worldconfig = nil
return false
end
return true
end
return false
end
--------------------------------------------------------------------------------
function modmgr.gettab(name)
local retval = ""
if name == "mod_mgr" then
retval = retval .. modmgr.tab()
end
if name == "dialog_rename_modpack" then
retval = retval .. modmgr.dialog_rename_modpack()
end
if name == "dialog_delete_mod" then
retval = retval .. modmgr.dialog_delete_mod()
end
if name == "dialog_configure_world" then
retval = retval .. modmgr.dialog_configure_world()
end
return retval
end

275
builtin/modstore.lua Normal file
View file

@ -0,0 +1,275 @@
--Minetest
--Copyright (C) 2013 sapier
--
--This program is free software; you can redistribute it and/or modify
--it under the terms of the GNU Lesser General Public License as published by
--the Free Software Foundation; either version 2.1 of the License, or
--(at your option) any later version.
--
--This program is distributed in the hope that it will be useful,
--but WITHOUT ANY WARRANTY; without even the implied warranty of
--MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
--GNU Lesser General Public License for more details.
--
--You should have received a copy of the GNU Lesser General Public License along
--with this program; if not, write to the Free Software Foundation, Inc.,
--51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
--------------------------------------------------------------------------------
--modstore implementation
modstore = {}
--------------------------------------------------------------------------------
function modstore.init()
modstore.tabnames = {}
table.insert(modstore.tabnames,"dialog_modstore_unsorted")
table.insert(modstore.tabnames,"dialog_modstore_search")
modstore.modsperpage = 5
modstore.basetexturedir = engine.get_gamepath() .. DIR_DELIM .. ".." ..
DIR_DELIM .. "textures" .. DIR_DELIM .. "base" ..
DIR_DELIM .. "pack" .. DIR_DELIM
modstore.update_modlist()
modstore.current_list = nil
modstore.details_cache = {}
end
--------------------------------------------------------------------------------
function modstore.nametoindex(name)
for i=1,#modstore.tabnames,1 do
if modstore.tabnames[i] == name then
return i
end
end
return 1
end
--------------------------------------------------------------------------------
function modstore.gettab(tabname)
local retval = ""
local is_modstore_tab = false
if tabname == "dialog_modstore_unsorted" then
retval = modstore.getmodlist(modstore.modlist_unsorted)
is_modstore_tab = true
end
if tabname == "dialog_modstore_search" then
is_modstore_tab = true
end
if is_modstore_tab then
return modstore.tabheader(tabname) .. retval
end
if tabname == "modstore_mod_installed" then
return "size[6,2]label[0.25,0.25;Mod: " .. modstore.lastmodtitle ..
" installed successfully]" ..
"button[2.5,1.5;1,0.5;btn_confirm_mod_successfull;ok]"
end
return ""
end
--------------------------------------------------------------------------------
function modstore.tabheader(tabname)
local retval = "size[12,9.25]"
retval = retval .. "tabheader[-0.3,-0.99;modstore_tab;" ..
"Unsorted,Search;" ..
modstore.nametoindex(tabname) .. ";true;false]"
return retval
end
--------------------------------------------------------------------------------
function modstore.handle_buttons(current_tab,fields)
modstore.lastmodtitle = ""
if fields["modstore_tab"] then
local index = tonumber(fields["modstore_tab"])
if index > 0 and
index <= #modstore.tabnames then
return {
current_tab = modstore.tabnames[index],
is_dialog = true,
show_buttons = false
}
end
modstore.modlist_page = 0
end
if fields["btn_modstore_page_up"] then
if modstore.current_list ~= nil and modstore.current_list.page > 0 then
modstore.current_list.page = modstore.current_list.page - 1
end
end
if fields["btn_modstore_page_down"] then
if modstore.current_list ~= nil and
modstore.current_list.page <modstore.current_list.pagecount then
modstore.current_list.page = modstore.current_list.page +1
end
end
if fields["btn_confirm_mod_successfull"] then
return {
current_tab = modstore.tabnames[1],
is_dialog = true,
show_buttons = false
}
end
for i=1, modstore.modsperpage, 1 do
local installbtn = "btn_install_mod_" .. i
if fields[installbtn] then
local modlistentry =
modstore.current_list.page * modstore.modsperpage + i
local moddetails = modstore.get_details(modstore.current_list.data[modlistentry].id)
local fullurl = engine.setting_get("modstore_download_url") ..
moddetails.download_url
local modfilename = os.tempfolder() .. ".zip"
print("Downloading mod from: " .. fullurl .. " to ".. modfilename)
if engine.download_file(fullurl,modfilename) then
modmgr.installmod(modfilename,moddetails.basename)
os.remove(modfilename)
modstore.lastmodtitle = modstore.current_list.data[modlistentry].title
return {
current_tab = "modstore_mod_installed",
is_dialog = true,
show_buttons = false
}
else
gamedata.errormessage = "Unable to download " ..
moddetails.download_url .. " (internet connection?)"
end
end
end
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.floor((#modstore.modlist_unsorted.data / modstore.modsperpage))
else
modstore.modlist_unsorted.data = {}
modstore.modlist_unsorted.pagecount = 0
end
modstore.modlist_unsorted.page = 0
end
--------------------------------------------------------------------------------
function modstore.getmodlist(list)
local retval = ""
retval = retval .. "label[10,-0.4;Page " .. (list.page +1) ..
" of " .. (list.pagecount +1) .. "]"
retval = retval .. "button[11.6,-0.1;0.5,0.5;btn_modstore_page_up;^]"
retval = retval .. "box[11.6,0.35;0.28,8.6;BLK]"
local scrollbarpos = 0.35 + (8.1/list.pagecount) * list.page
retval = retval .. "box[11.6," ..scrollbarpos .. ";0.28,0.5;LIM]"
retval = retval .. "button[11.6,9.0;0.5,0.5;btn_modstore_page_down;v]"
if #list.data < (list.page * modstore.modsperpage) then
return retval
end
local endmod = (list.page * modstore.modsperpage) + modstore.modsperpage
if (endmod > #list.data) then
endmod = #list.data
end
for i=(list.page * modstore.modsperpage) +1, endmod, 1 do
--getmoddetails
local details = modstore.get_details(list.data[i].id)
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;WHT]"
--screenshot
if details.screenshot_url ~= nil and
details.screenshot_url ~= "" then
if list.data[i].texturename == nil then
print("downloading screenshot: " .. details.screenshot_url)
local filename = os.tempfolder()
if engine.download_file(details.screenshot_url,filename) then
list.data[i].texturename = filename
end
end
end
if list.data[i].texturename == nil then
list.data[i].texturename = modstore.basetexturedir .. "no_screenshot.png"
end
retval = retval .. "image[0,".. screenshot_ypos .. ";3,2;" ..
list.data[i].texturename .. "]"
--title + author
retval = retval .."label[2.75," .. screenshot_ypos .. ";" ..
fs_escape_string(details.title) .. " (" .. details.author .. ")]"
--description
local descriptiony = screenshot_ypos + 0.5
retval = retval .. "textarea[3," .. descriptiony .. ";6.5,1.6;;" ..
fs_escape_string(details.description) .. ";]"
--rating
local ratingy = screenshot_ypos + 0.6
retval = retval .."label[10.1," .. ratingy .. ";Rating: " .. 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 modmgr.mod_exists(details.basename) then
retval = retval .. "re-Install]"
else
retval = retval .. "Install]"
end
end
end
modstore.current_list = list
return retval
end
--------------------------------------------------------------------------------
function modstore.get_details(modid)
if modstore.details_cache[modid] ~= nil then
return modstore.details_cache[modid]
end
local retval = engine.get_modstore_details(tostring(modid))
modstore.details_cache[modid] = retval
return retval
end