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 18gamemgr = {} 19 20-------------------------------------------------------------------------------- 21function gamemgr.dialog_new_game() 22 local retval = 23 "label[2,2;" .. fgettext("Game Name") .. "]".. 24 "field[4.5,2.4;6,0.5;te_game_name;;]" .. 25 "button[5,4.2;2.6,0.5;new_game_confirm;" .. fgettext("Create") .. "]" .. 26 "button[7.5,4.2;2.8,0.5;new_game_cancel;" .. fgettext("Cancel") .. "]" 27 28 return retval 29end 30 31-------------------------------------------------------------------------------- 32function gamemgr.handle_games_buttons(fields) 33 if fields["gamelist"] ~= nil then 34 local event = core.explode_textlist_event(fields["gamelist"]) 35 gamemgr.selected_game = event.index 36 end 37 38 if fields["btn_game_mgr_edit_game"] ~= nil then 39 return { 40 is_dialog = true, 41 show_buttons = false, 42 current_tab = "dialog_edit_game" 43 } 44 end 45 46 if fields["btn_game_mgr_new_game"] ~= nil then 47 return { 48 is_dialog = true, 49 show_buttons = false, 50 current_tab = "dialog_new_game" 51 } 52 end 53 54 return nil 55end 56 57-------------------------------------------------------------------------------- 58function gamemgr.handle_new_game_buttons(fields) 59 60 if fields["new_game_confirm"] and 61 fields["te_game_name"] ~= nil and 62 fields["te_game_name"] ~= "" then 63 local gamepath = core.get_gamepath() 64 65 if gamepath ~= nil and 66 gamepath ~= "" then 67 local gamefolder = cleanup_path(fields["te_game_name"]) 68 69 --TODO check for already existing first 70 core.create_dir(gamepath .. DIR_DELIM .. gamefolder) 71 core.create_dir(gamepath .. DIR_DELIM .. gamefolder .. DIR_DELIM .. "mods") 72 core.create_dir(gamepath .. DIR_DELIM .. gamefolder .. DIR_DELIM .. "menu") 73 74 local gameconf = 75 io.open(gamepath .. DIR_DELIM .. gamefolder .. DIR_DELIM .. "game.conf","w") 76 77 if gameconf then 78 gameconf:write("name = " .. fields["te_game_name"]) 79 gameconf:close() 80 end 81 end 82 end 83 84 return { 85 is_dialog = false, 86 show_buttons = true, 87 current_tab = core.setting_get("main_menu_tab") 88 } 89end 90 91-------------------------------------------------------------------------------- 92function gamemgr.handle_edit_game_buttons(fields) 93 local current_game = gamemgr.get_game(gamemgr.selected_game) 94 95 if fields["btn_close_edit_game"] ~= nil or 96 current_game == nil then 97 return { 98 is_dialog = false, 99 show_buttons = true, 100 current_tab = core.setting_get("main_menu_tab") 101 } 102 end 103 104 if fields["btn_remove_mod_from_game"] ~= nil then 105 gamemgr.delete_mod(current_game,core.get_textlist_index("mods_current")) 106 end 107 108 if fields["btn_add_mod_to_game"] ~= nil then 109 local modindex = core.get_textlist_index("mods_available") 110 111 local mod = modmgr.get_global_mod(modindex) 112 if mod ~= nil then 113 114 local sourcepath = mod.path 115 116 if not gamemgr.add_mod(current_game,sourcepath) then 117 gamedata.errormessage = 118 fgettext("Gamemgr: Unable to copy mod \"$1\" to game \"$2\"", mod.name, current_game.id) 119 end 120 end 121 end 122 123 return nil 124end 125 126-------------------------------------------------------------------------------- 127function gamemgr.add_mod(gamespec,sourcepath) 128 if gamespec.gamemods_path ~= nil and 129 gamespec.gamemods_path ~= "" then 130 131 local modname = get_last_folder(sourcepath) 132 133 return core.copy_dir(sourcepath,gamespec.gamemods_path .. DIR_DELIM .. modname); 134 end 135 136 return false 137end 138 139-------------------------------------------------------------------------------- 140function gamemgr.delete_mod(gamespec,modindex) 141 if gamespec.gamemods_path ~= nil and 142 gamespec.gamemods_path ~= "" then 143 local game_mods = {} 144 get_mods(gamespec.gamemods_path,game_mods) 145 146 if modindex > 0 and 147 #game_mods >= modindex then 148 149 if game_mods[modindex].path:sub(0,gamespec.gamemods_path:len()) 150 == gamespec.gamemods_path then 151 core.delete_dir(game_mods[modindex].path) 152 end 153 end 154 end 155end 156 157-------------------------------------------------------------------------------- 158function gamemgr.find_by_gameid(gameid) 159 for i=1,#gamemgr.games,1 do 160 if gamemgr.games[i].id == gameid then 161 return gamemgr.games[i], i 162 end 163 end 164 return nil, nil 165end 166 167-------------------------------------------------------------------------------- 168function gamemgr.get_game_mods(gamespec, retval) 169 if gamespec ~= nil and 170 gamespec.gamemods_path ~= nil and 171 gamespec.gamemods_path ~= "" then 172 get_mods(gamespec.gamemods_path, retval) 173 end 174end 175 176-------------------------------------------------------------------------------- 177function gamemgr.get_game_modlist(gamespec) 178 local retval = "" 179 local game_mods = {} 180 gamemgr.get_game_mods(gamespec, game_mods) 181 for i=1,#game_mods,1 do 182 if retval ~= "" then 183 retval = retval.."," 184 end 185 retval = retval .. game_mods[i].name 186 end 187 return retval 188end 189 190-------------------------------------------------------------------------------- 191function gamemgr.gettab(name) 192 local retval = "" 193 194 if name == "dialog_edit_game" then 195 retval = retval .. gamemgr.dialog_edit_game() 196 end 197 198 if name == "dialog_new_game" then 199 retval = retval .. gamemgr.dialog_new_game() 200 end 201 202 if name == "game_mgr" then 203 retval = retval .. gamemgr.tab() 204 end 205 206 return retval 207end 208 209-------------------------------------------------------------------------------- 210function gamemgr.tab() 211 if gamemgr.selected_game == nil then 212 gamemgr.selected_game = 1 213 end 214 215 local retval = 216 "label[6.5,-0.25;" .. fgettext("Games") .. ":]" .. 217 "textlist[6.5,0.25;8,4.4;gamelist;" .. 218 gamemgr.gamelist() .. 219 ";" .. gamemgr.selected_game .. "]" 220 221 local current_game = gamemgr.get_game(gamemgr.selected_game) 222 223 if current_game ~= nil then 224 if current_game.menuicon_path ~= nil and 225 current_game.menuicon_path ~= "" then 226 retval = retval .. 227 "image[6.5,5.5;2,2;" .. 228 core.formspec_escape(current_game.menuicon_path) .. "]" 229 end 230 231 retval = retval .. 232 "field[9,5.5;6,2;;" .. current_game.name .. ";]".. 233 "label[6.5,7.2;" .. fgettext("Mods:") .."]" .. 234 "button[6.5,11.4;3.2,0.2;btn_game_mgr_edit_game;" .. fgettext("edit game") .. "]" .. 235 "textlist[6.5,7.7;8,3.3;game_mgr_modlist;" 236 .. gamemgr.get_game_modlist(current_game) ..";0]" .. 237 "button[6.5,4.9;3.2,0.5;btn_game_mgr_new_game;" .. fgettext("new game") .. "]" 238 end 239 return retval 240end 241 242-------------------------------------------------------------------------------- 243function gamemgr.dialog_edit_game() 244 local current_game = gamemgr.get_game(gamemgr.selected_game) 245 if current_game ~= nil then 246 local retval = 247 "vertlabel[0,-0.25;" .. fgettext("EDIT GAME") .."]" .. 248 "label[0,-0.25;" .. current_game.name .. "]" .. 249 "button[11.55,-0.2;0.75,0.5;btn_close_edit_game;x]" 250 251 if current_game.menuicon_path ~= nil and 252 current_game.menuicon_path ~= "" then 253 retval = retval .. 254 "image[5.25,0;2,2;" .. 255 core.formspec_escape(current_game.menuicon_path) .. "]" 256 end 257 258 retval = retval .. 259 "textlist[0.5,0.5;4.5,4.3;mods_current;" 260 .. gamemgr.get_game_modlist(current_game) ..";0]" 261 262 263 retval = retval .. 264 "textlist[7,0.5;4.5,4.3;mods_available;" 265 .. modmgr.render_modlist() .. ";0]" 266 267 retval = retval .. 268 "button[0.55,4.95;4.7,0.5;btn_remove_mod_from_game;" .. fgettext("Remove selected mod") .."]" 269 270 retval = retval .. 271 "button[7.05,4.95;4.7,0.5;btn_add_mod_to_game;" .. fgettext("<<-- Add mod") .."]" 272 273 return retval 274 end 275end 276 277-------------------------------------------------------------------------------- 278function gamemgr.handle_buttons(tab,fields) 279 local retval = nil 280 281 if tab == "dialog_edit_game" then 282 retval = gamemgr.handle_edit_game_buttons(fields) 283 end 284 285 if tab == "dialog_new_game" then 286 retval = gamemgr.handle_new_game_buttons(fields) 287 end 288 289 if tab == "game_mgr" then 290 retval = gamemgr.handle_games_buttons(fields) 291 end 292 293 return retval 294end 295 296-------------------------------------------------------------------------------- 297function gamemgr.get_game(index) 298 if index > 0 and index <= #gamemgr.games then 299 return gamemgr.games[index] 300 end 301 302 return nil 303end 304 305-------------------------------------------------------------------------------- 306function gamemgr.update_gamelist() 307 gamemgr.games = core.get_games() 308end 309 310-------------------------------------------------------------------------------- 311function gamemgr.gamelist() 312 local retval = "" 313 if #gamemgr.games > 0 then 314 retval = retval .. gamemgr.games[1].name 315 316 for i=2,#gamemgr.games,1 do 317 retval = retval .. "," .. gamemgr.games[i].name 318 end 319 end 320 return retval 321end 322