summaryrefslogtreecommitdiff
path: root/builtin/mainmenu/gamemgr.lua
blob: c99c2de21ab8e787cbd143e57d0065cb4991c832 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
--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;" .. fgettext("Game Name") .. "]"..
		"field[4.5,2.4;6,0.5;te_game_name;;]" ..
		"button[5,4.2;2.6,0.5;new_game_confirm;" .. fgettext("Create") .. "]" ..
		"button[7.5,4.2;2.8,0.5;new_game_cancel;" .. fgettext("Cancel") .. "]"

	return retval
end

--------------------------------------------------------------------------------
function gamemgr.handle_games_buttons(fields)
	if fields["gamelist"] ~= nil then
		local event = engine.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")
		
		local mod = modmgr.get_global_mod(modindex)
		if mod ~= nil then
			
			local sourcepath = mod.path
			
			if not gamemgr.add_mod(current_game,sourcepath) then
				gamedata.errormessage =
					fgettext("Gamemgr: Unable to copy mod \"$1\" to game \"$2\"", mod.name, current_game.id)
			end
		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)
		
		return engine.copy_dir(sourcepath,gamespec.gamemods_path .. DIR_DELIM .. modname);
	end
	
	return false
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

			if game_mods[modindex].path:sub(0,gamespec.gamemods_path:len()) 
					== gamespec.gamemods_path then
				engine.delete_dir(game_mods[modindex].path)
			end
		end
	end
end

--------------------------------------------------------------------------------
function gamemgr.find_by_gameid(gameid)
	for i=1,#gamemgr.games,1 do		
		if gamemgr.games[i].id == gameid then
			return gamemgr.games[i], i
		end
	end
	return nil, nil
end

--------------------------------------------------------------------------------
function gamemgr.get_game_mods(gamespec, retval)
	if gamespec ~= nil and
		gamespec.gamemods_path ~= nil and
		gamespec.gamemods_path ~= "" then
		get_mods(gamespec.gamemods_path, retval)
	end
end

--------------------------------------------------------------------------------
function gamemgr.get_game_modlist(gamespec)
	local retval = ""
	local game_mods = {}
	gamemgr.get_game_mods(gamespec, game_mods)
	for i=1,#game_mods,1 do
		if retval ~= "" then
			retval = retval..","
		end
		retval = retval .. game_mods[i].name
	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;" .. fgettext("GAMES") .. "]" ..
		"label[1,-0.25;" .. fgettext("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;" ..
				engine.formspec_escape(current_game.menuicon_path) .. "]"
		end
		
		retval = retval ..
			"field[8,-0.25;6,2;;" .. current_game.name .. ";]"..
			"label[6,1.4;" .. fgettext("Mods:") .."]" ..
			"button[9.7,1.5;2,0.2;btn_game_mgr_edit_game;" .. fgettext("edit game") .. "]" ..
			"textlist[6,2;5.5,3.3;game_mgr_modlist;"
			.. gamemgr.get_game_modlist(current_game) ..";0]" ..
			"button[1,4.75;3.2,0.5;btn_game_mgr_new_game;" .. fgettext("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;" .. fgettext("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;" ..
				engine.formspec_escape(current_game.menuicon_path) .. "]"
		end
		
		retval = retval .. 
			"textlist[0.5,0.5;4.5,4.3;mods_current;"
			.. gamemgr.get_game_modlist(current_game) ..";0]"
			
			
		retval = retval .. 
			"textlist[7,0.5;4.5,4.3;mods_available;"
			.. modmgr.render_modlist() .. ";0]"

		retval = retval ..
			"button[0.55,4.95;4.7,0.5;btn_remove_mod_from_game;" .. fgettext("Remove selected mod") .."]"
			
		retval = retval ..
			"button[7.05,4.95;4.7,0.5;btn_add_mod_to_game;" .. fgettext("<<-- 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