gamemgr.lua 9.0 KB

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