2 --Copyright (C) 2013 sapier
4 --This program is free software; you can redistribute it and/or modify
5 --it under the terms of the GNU Lesser General Public License as published by
6 --the Free Software Foundation; either version 2.1 of the License, or
7 --(at your option) any later version.
9 --This program is distributed in the hope that it will be useful,
10 --but WITHOUT ANY WARRANTY; without even the implied warranty of
11 --MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 --GNU Lesser General Public License for more details.
14 --You should have received a copy of the GNU Lesser General Public License along
15 --with this program; if not, write to the Free Software Foundation, Inc.,
16 --51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 --------------------------------------------------------------------------------
21 function gamemgr.dialog_new_game()
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") .. "]"
31 --------------------------------------------------------------------------------
32 function gamemgr.handle_games_buttons(fields)
33 if fields["gamelist"] ~= nil then
34 local event = engine.explode_textlist_event(fields["gamelist"])
35 gamemgr.selected_game = event.index
38 if fields["btn_game_mgr_edit_game"] ~= nil then
42 current_tab = "dialog_edit_game"
46 if fields["btn_game_mgr_new_game"] ~= nil then
50 current_tab = "dialog_new_game"
57 --------------------------------------------------------------------------------
58 function gamemgr.handle_new_game_buttons(fields)
60 if fields["new_game_confirm"] and
61 fields["te_game_name"] ~= nil and
62 fields["te_game_name"] ~= "" then
63 local gamepath = engine.get_gamepath()
65 if gamepath ~= nil and
67 local gamefolder = cleanup_path(fields["te_game_name"])
69 --TODO check for already existing first
70 engine.create_dir(gamepath .. DIR_DELIM .. gamefolder)
71 engine.create_dir(gamepath .. DIR_DELIM .. gamefolder .. DIR_DELIM .. "mods")
72 engine.create_dir(gamepath .. DIR_DELIM .. gamefolder .. DIR_DELIM .. "menu")
75 io.open(gamepath .. DIR_DELIM .. gamefolder .. DIR_DELIM .. "game.conf","w")
78 gameconf:write("name = " .. fields["te_game_name"])
87 current_tab = engine.setting_get("main_menu_tab")
91 --------------------------------------------------------------------------------
92 function gamemgr.handle_edit_game_buttons(fields)
93 local current_game = gamemgr.get_game(gamemgr.selected_game)
95 if fields["btn_close_edit_game"] ~= nil or
96 current_game == nil then
100 current_tab = engine.setting_get("main_menu_tab")
104 if fields["btn_remove_mod_from_game"] ~= nil then
105 gamemgr.delete_mod(current_game,engine.get_textlist_index("mods_current"))
108 if fields["btn_add_mod_to_game"] ~= nil then
109 local modindex = engine.get_textlist_index("mods_available")
111 local mod = modmgr.get_global_mod(modindex)
114 local sourcepath = mod.path
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)
126 --------------------------------------------------------------------------------
127 function gamemgr.add_mod(gamespec,sourcepath)
128 if gamespec.gamemods_path ~= nil and
129 gamespec.gamemods_path ~= "" then
131 local modname = get_last_folder(sourcepath)
133 return engine.copy_dir(sourcepath,gamespec.gamemods_path .. DIR_DELIM .. modname);
139 --------------------------------------------------------------------------------
140 function gamemgr.delete_mod(gamespec,modindex)
141 if gamespec.gamemods_path ~= nil and
142 gamespec.gamemods_path ~= "" then
144 get_mods(gamespec.gamemods_path,game_mods)
147 #game_mods >= modindex then
149 if game_mods[modindex].path:sub(0,gamespec.gamemods_path:len())
150 == gamespec.gamemods_path then
151 engine.delete_dir(game_mods[modindex].path)
157 --------------------------------------------------------------------------------
158 function 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
167 --------------------------------------------------------------------------------
168 function 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)
176 --------------------------------------------------------------------------------
177 function gamemgr.get_game_modlist(gamespec)
180 gamemgr.get_game_mods(gamespec, game_mods)
181 for i=1,#game_mods,1 do
185 retval = retval .. game_mods[i].name
190 --------------------------------------------------------------------------------
191 function gamemgr.gettab(name)
194 if name == "dialog_edit_game" then
195 retval = retval .. gamemgr.dialog_edit_game()
198 if name == "dialog_new_game" then
199 retval = retval .. gamemgr.dialog_new_game()
202 if name == "game_mgr" then
203 retval = retval .. gamemgr.tab()
209 --------------------------------------------------------------------------------
210 function gamemgr.tab()
211 if gamemgr.selected_game == nil then
212 gamemgr.selected_game = 1
216 "vertlabel[0,-0.25;" .. fgettext("GAMES") .. "]" ..
217 "label[1,-0.25;" .. fgettext("Games") .. ":]" ..
218 "textlist[1,0.25;4.5,4.4;gamelist;" ..
219 gamemgr.gamelist() ..
220 ";" .. gamemgr.selected_game .. "]"
222 local current_game = gamemgr.get_game(gamemgr.selected_game)
224 if current_game ~= nil then
225 if current_game.menuicon_path ~= nil and
226 current_game.menuicon_path ~= "" then
228 "image[5.8,-0.25;2,2;" ..
229 engine.formspec_escape(current_game.menuicon_path) .. "]"
233 "field[8,-0.25;6,2;;" .. current_game.name .. ";]"..
234 "label[6,1.4;" .. fgettext("Mods:") .."]" ..
235 "button[9.7,1.5;2,0.2;btn_game_mgr_edit_game;" .. fgettext("edit game") .. "]" ..
236 "textlist[6,2;5.5,3.3;game_mgr_modlist;"
237 .. gamemgr.get_game_modlist(current_game) ..";0]" ..
238 "button[1,4.75;3.2,0.5;btn_game_mgr_new_game;" .. fgettext("new game") .. "]"
243 --------------------------------------------------------------------------------
244 function gamemgr.dialog_edit_game()
245 local current_game = gamemgr.get_game(gamemgr.selected_game)
246 if current_game ~= nil then
248 "vertlabel[0,-0.25;" .. fgettext("EDIT GAME") .."]" ..
249 "label[0,-0.25;" .. current_game.name .. "]" ..
250 "button[11.55,-0.2;0.75,0.5;btn_close_edit_game;x]"
252 if current_game.menuicon_path ~= nil and
253 current_game.menuicon_path ~= "" then
255 "image[5.25,0;2,2;" ..
256 engine.formspec_escape(current_game.menuicon_path) .. "]"
260 "textlist[0.5,0.5;4.5,4.3;mods_current;"
261 .. gamemgr.get_game_modlist(current_game) ..";0]"
265 "textlist[7,0.5;4.5,4.3;mods_available;"
266 .. modmgr.render_modlist() .. ";0]"
269 "button[0.55,4.95;4.7,0.5;btn_remove_mod_from_game;" .. fgettext("Remove selected mod") .."]"
272 "button[7.05,4.95;4.7,0.5;btn_add_mod_to_game;" .. fgettext("<<-- Add mod") .."]"
278 --------------------------------------------------------------------------------
279 function gamemgr.handle_buttons(tab,fields)
282 if tab == "dialog_edit_game" then
283 retval = gamemgr.handle_edit_game_buttons(fields)
286 if tab == "dialog_new_game" then
287 retval = gamemgr.handle_new_game_buttons(fields)
290 if tab == "game_mgr" then
291 retval = gamemgr.handle_games_buttons(fields)
297 --------------------------------------------------------------------------------
298 function gamemgr.get_game(index)
299 if index > 0 and index <= #gamemgr.games then
300 return gamemgr.games[index]
306 --------------------------------------------------------------------------------
307 function gamemgr.update_gamelist()
308 gamemgr.games = engine.get_games()
311 --------------------------------------------------------------------------------
312 function gamemgr.gamelist()
314 if #gamemgr.games > 0 then
315 retval = retval .. gamemgr.games[1].id
317 for i=2,#gamemgr.games,1 do
318 retval = retval .. "," .. gamemgr.games[i].name