From e82bfa5cc917c935b2db037e8157770a16c6d437 Mon Sep 17 00:00:00 2001
From: dg <dg@51575b47-30f0-44d4-a5cc-537603b46e54>
Date: Tue, 13 Sep 2011 10:41:01 +0000
Subject: [PATCH] Fix bug when doing "New Game" after doing "Load Game"

git-svn-id: http://svn.net-core.org/repos/t-engine4@4389 51575b47-30f0-44d4-a5cc-537603b46e54
---
 game/engines/default/engine/Module.lua        |  6 ++-
 game/engines/default/engine/utils.lua         | 15 ++++++
 .../default/modules/boot/dialogs/LoadGame.lua |  2 +-
 .../default/modules/boot/dialogs/NewGame.lua  |  2 +-
 .../modules/boot/dialogs/ViewHighScores.lua   |  2 +-
 src/main.c                                    |  5 ++
 src/main.h                                    |  2 +
 src/physfs.c                                  | 51 ++++++++++++++-----
 src/physfs/physfs.c                           | 13 +++++
 src/physfs/physfs.h                           |  2 +
 10 files changed, 83 insertions(+), 17 deletions(-)

diff --git a/game/engines/default/engine/Module.lua b/game/engines/default/engine/Module.lua
index 5ff51f7b21..79dfad26e9 100644
--- a/game/engines/default/engine/Module.lua
+++ b/game/engines/default/engine/Module.lua
@@ -36,8 +36,8 @@ end
 -- Static
 function _M:listModules(incompatible, moddir_filter)
 	local ms = {}
+	local allmounts = fs.getSearchPath(true)
 	fs.mount(engine.homepath, "/")
---	print("Search Path: ") for k,e in ipairs(fs.getSearchPath()) do print("*",k,e) end
 
 	local knowns = {}
 	for i, short_name in ipairs(fs.list("/modules/")) do
@@ -73,7 +73,9 @@ function _M:listModules(incompatible, moddir_filter)
 		end
 		ms[m.short_name] = m.versions[1]
 	end
---	fs.umount(engine.homepath)
+
+	fs.reset()
+	fs.mountAll(allmounts)
 
 	return ms
 end
diff --git a/game/engines/default/engine/utils.lua b/game/engines/default/engine/utils.lua
index 468fa64235..7337ac5b76 100644
--- a/game/engines/default/engine/utils.lua
+++ b/game/engines/default/engine/utils.lua
@@ -983,6 +983,21 @@ function util.getval(val, ...)
 	end
 end
 
+function fs.reset()
+	local list = fs.getSearchPath(true)
+	for i, m in ipairs(list) do
+		fs.umount(m.path)
+	end
+	print("After fs.reset")
+	table.print(fs.getSearchPath(true))
+end
+
+function fs.mountAll(list)
+	for i, m in ipairs(list) do
+		fs.mount(m.path, "/" .. (m.mount or ""), true)
+	end
+end
+
 function util.loadfilemods(file, env)
 	-- Base loader
 	local prev, err = loadfile(file)
diff --git a/game/engines/default/modules/boot/dialogs/LoadGame.lua b/game/engines/default/modules/boot/dialogs/LoadGame.lua
index aa1cf23c06..e5dabe85ed 100644
--- a/game/engines/default/modules/boot/dialogs/LoadGame.lua
+++ b/game/engines/default/modules/boot/dialogs/LoadGame.lua
@@ -31,7 +31,7 @@ module(..., package.seeall, class.inherit(Dialog))
 function _M:init()
 	Dialog.init(self, "Load Game", game.w * 0.8, game.h * 0.8)
 
-	local list = Module:listSavefiles(function(dir) if dir:find("^boot") then return false else return true end end)
+	local list = Module:listSavefiles()
 
 	self.c_play = Button.new{text="  Play!  ", fct=function(text) self:playSave() end}
 	self.c_delete = Button.new{text="Delete", fct=function(text) self:deleteSave() end}
diff --git a/game/engines/default/modules/boot/dialogs/NewGame.lua b/game/engines/default/modules/boot/dialogs/NewGame.lua
index f86c4734a5..9271196430 100644
--- a/game/engines/default/modules/boot/dialogs/NewGame.lua
+++ b/game/engines/default/modules/boot/dialogs/NewGame.lua
@@ -74,7 +74,7 @@ function _M:select(item)
 end
 
 function _M:generateList()
-	local list = Module:listModules(self.c_compat.checked, function(dir) if dir:find("^boot") then return false else return true end end)
+	local list = Module:listModules(self.c_compat.checked)
 	self.list = {}
 	for i = 1, #list do
 		for j, mod in ipairs(list[i].versions) do
diff --git a/game/engines/default/modules/boot/dialogs/ViewHighScores.lua b/game/engines/default/modules/boot/dialogs/ViewHighScores.lua
index 1c27b77a95..2363c8d139 100644
--- a/game/engines/default/modules/boot/dialogs/ViewHighScores.lua
+++ b/game/engines/default/modules/boot/dialogs/ViewHighScores.lua
@@ -69,7 +69,7 @@ function _M:init()
 end
 
 function _M:generateList()
-	local list = Module:listModules(nil, function(dir) if dir:find("^boot") then return false else return true end end)
+	local list = Module:listModules()
 	self.list = {}
 
 	for i = 1, #list do
diff --git a/src/main.c b/src/main.c
index 689d85518d..9ced10283d 100644
--- a/src/main.c
+++ b/src/main.c
@@ -50,6 +50,8 @@
 #define WIDTH 800
 #define HEIGHT 600
 
+int g_argc = 0;
+char **g_argv;
 SDL_Window *window = NULL;
 SDL_GLContext maincontext; /* Our opengl context handle */
 bool is_fullscreen = FALSE;
@@ -934,6 +936,9 @@ int main(int argc, char *argv[])
 	freopen ("te4_log.txt", "w", stdout);
 #endif
 
+	g_argc = argc;
+	g_argv = argv;
+
 	// Parse arguments
 	int i;
 	for (i = 1; i < argc; i++)
diff --git a/src/main.h b/src/main.h
index 497a682dbf..f1bb929481 100644
--- a/src/main.h
+++ b/src/main.h
@@ -41,6 +41,8 @@ extern int docall (lua_State *L, int narg, int nret);
 extern bool fbo_active;
 extern bool multitexture_active;
 extern long total_keyframes;
+extern int g_argc;
+extern char **g_argv;
 
 /* Error handling */
 struct lua_err_type_s {
diff --git a/src/physfs.c b/src/physfs.c
index 512369a1f9..0dac648cd1 100644
--- a/src/physfs.c
+++ b/src/physfs.c
@@ -27,6 +27,7 @@
 #include "mzip.h"
 #include "zlib.h"
 #include "types.h"
+#include "main.h"
 
 /******************************************************************
  ******************************************************************
@@ -358,23 +359,50 @@ static int lua_fs_get_path_separator(lua_State *L)
 	return 1;
 }
 
+static void fs_list_path_double(void *data, const char *path, const char *mount)
+{
+	lua_State *L = (lua_State*)data;
+
+	int nb = lua_objlen(L, -1);
+	lua_pushnumber(L, nb + 1);
+	lua_newtable(L);
+
+	lua_pushliteral(L, "path");
+	lua_pushstring(L, path);
+	lua_settable(L, -3);
+
+	lua_pushliteral(L, "mount");
+	lua_pushstring(L, mount);
+	lua_settable(L, -3);
+
+	lua_settable(L, -3);
+}
+
 static int lua_fs_get_search_path(lua_State *L)
 {
-	char **rc = PHYSFS_getSearchPath();
+	if (!lua_toboolean(L, 1))
+	{
+		char **rc = PHYSFS_getSearchPath();
 
-	char **i;
-	int nb = 1;
+		char **i;
+		int nb = 1;
 
-	lua_newtable(L);
-	for (i = rc; *i != NULL; i++)
+		lua_newtable(L);
+		for (i = rc; *i != NULL; i++)
+		{
+			lua_pushnumber(L, nb);
+			lua_pushstring(L, *i);
+			lua_settable(L, -3);
+			nb++;
+		}
+
+		PHYSFS_freeList(rc);
+	}
+	else
 	{
-		lua_pushnumber(L, nb);
-		lua_pushstring(L, *i);
-		lua_settable(L, -3);
-		nb++;
+		lua_newtable(L);
+		PHYSFS_getSearchPathCallbackWithMount(fs_list_path_double, L);
 	}
-
-	PHYSFS_freeList(rc);
 	return 1;
 }
 
@@ -404,7 +432,6 @@ static int lua_patch_file(lua_State *L)
 	}
 }
 
-
 static const struct luaL_reg fslib[] =
 {
 	{"patchFile", lua_patch_file},
diff --git a/src/physfs/physfs.c b/src/physfs/physfs.c
index 49281e89e1..9f52071954 100644
--- a/src/physfs/physfs.c
+++ b/src/physfs/physfs.c
@@ -1059,6 +1059,19 @@ void PHYSFS_getSearchPathCallback(PHYSFS_StringCallback callback, void *data)
 } /* PHYSFS_getSearchPathCallback */
 
 
+void PHYSFS_getSearchPathCallbackWithMount(PHYSFS_DoubleStringCallback callback, void *data)
+{
+    DirHandle *i;
+
+    __PHYSFS_platformGrabMutex(stateLock);
+
+    for (i = searchPath; i != NULL; i = i->next)
+        callback(data, i->dirName, i->mountPoint);
+
+    __PHYSFS_platformReleaseMutex(stateLock);
+} /* PHYSFS_getSearchPathCallback */
+
+
 /* Split out to avoid stack allocation in a loop. */
 static void setSaneCfgAddPath(const char *i, const size_t l, const char *dirsep,
                               int archivesFirst)
diff --git a/src/physfs/physfs.h b/src/physfs/physfs.h
index f0d78d7217..a03c44d1f3 100644
--- a/src/physfs/physfs.h
+++ b/src/physfs/physfs.h
@@ -2124,6 +2124,7 @@ __EXPORT__ const char *PHYSFS_getMountPoint(const char *dir);
  * \sa PHYSFS_getSearchPathCallback
  */
 typedef void (*PHYSFS_StringCallback)(void *data, const char *str);
+typedef void (*PHYSFS_DoubleStringCallback)(void *data, const char *str1, const char *str2);
 
 
 /**
@@ -2229,6 +2230,7 @@ __EXPORT__ void PHYSFS_getCdRomDirsCallback(PHYSFS_StringCallback c, void *d);
  * \sa PHYSFS_getSearchPath
  */
 __EXPORT__ void PHYSFS_getSearchPathCallback(PHYSFS_StringCallback c, void *d);
+__EXPORT__ void PHYSFS_getSearchPathCallbackWithMount(PHYSFS_DoubleStringCallback callback, void *data);
 
 
 /**
-- 
GitLab