From fc8e9eb5e02675187676679b29455be20d750160 Mon Sep 17 00:00:00 2001
From: dg <dg@51575b47-30f0-44d4-a5cc-537603b46e54>
Date: Tue, 22 Nov 2011 11:38:33 +0000
Subject: [PATCH] Support for compressed addons in .teaa files

git-svn-id: http://svn.net-core.org/repos/t-engine4@4666 51575b47-30f0-44d4-a5cc-537603b46e54
---
 game/engines/default/engine/Module.lua | 34 +++++++++++++++++++-------
 src/physfs/archivers/bind_physfs.c     | 26 ++++++++++++++++----
 2 files changed, 46 insertions(+), 14 deletions(-)

diff --git a/game/engines/default/engine/Module.lua b/game/engines/default/engine/Module.lua
index 50fc200217..8a33888851 100644
--- a/game/engines/default/engine/Module.lua
+++ b/game/engines/default/engine/Module.lua
@@ -255,8 +255,8 @@ end
 --- List all available addons
 function _M:loadAddons(mod)
 	local adds = {}
-	local load = function(dir)
-		local add_def = loadfile(team and (dir.."/mod/init.lua") or (dir.."/init.lua"))
+	local load = function(dir, teaa)
+		local add_def = loadfile(dir.."/init.lua")
 		if add_def then
 			local add = {}
 			setfenv(add_def, add)
@@ -264,6 +264,7 @@ function _M:loadAddons(mod)
 
 			if engine.version_string(add.version) == engine.version_string(mod.version) and add.for_module == mod.short_name then
 				add.dir = dir
+				add.teaa = teaa
 				adds[#adds+1] = add
 			end
 		end
@@ -273,24 +274,39 @@ function _M:loadAddons(mod)
 		local dir = "/addons/"..short_name
 		print("Checking addon", short_name, ":: (as dir)", fs.exists(dir.."/init.lua"), ":: (as teaa)", short_name:find(".teaa$"), "")
 		if fs.exists(dir.."/init.lua") then
-			load(dir)
-		elseif short_name:find(".team$") then
+			load(dir, nil)
+		elseif short_name:find(".teaa$") then
+			fs.mount(fs.getRealPath(dir), "/testload", false)
+			local mod
+			if fs.exists("/testload/init.lua") then
+				load("/testload", dir)
+			end
+			fs.umount(fs.getRealPath(dir))
 		end
 	end end
 
 	table.sort(adds, function(a, b) return a.weight < b.weight end)
 
 	for i, add in ipairs(adds) do
-		print("Binding addon", add.long_name)
-		if add.data then fs.mount(fs.getRealPath(add.dir).."/data", "/data-"..add.short_name, false) print(" * with data") end
-		if add.superload then fs.mount(fs.getRealPath(add.dir).."/superload", "/mod/addons/"..add.short_name.."/superload", false) print(" * with superload") end
-		if add.overload then fs.mount(fs.getRealPath(add.dir).."/overload", "/", true) print(" * with overload") end
+		print("Binding addon", add.long_name, add.teaa)
+		local base
+		if add.teaa then
+			fs.mount(fs.getRealPath(add.teaa), "/loaded-addons/"..add.short_name, true)
+			base = "bind::/loaded-addons/"..add.short_name
+		else
+			base = fs.getRealPath(add.dir)
+		end
+
+		if add.data then fs.mount(base.."/data", "/data-"..add.short_name, true) print(" * with data") end
+		if add.superload then fs.mount(base.."/superload", "/mod/addons/"..add.short_name.."/superload", true) print(" * with superload") end
+		if add.overload then fs.mount(base.."/overload", "/", false) print(" * with overload") end
 		if add.hooks then
-			fs.mount(fs.getRealPath(add.dir).."/hooks", "/hooks/"..add.short_name, true)
+			fs.mount(base.."/hooks", "/hooks/"..add.short_name, true)
 			dofile("/hooks/"..add.short_name.."/load.lua")
 			print(" * with hooks")
 		end
 	end
+	table.print(fs.getSearchPath(true))
 end
 
 --- Make a module loadscreen
diff --git a/src/physfs/archivers/bind_physfs.c b/src/physfs/archivers/bind_physfs.c
index e40f89d12e..15156e4783 100644
--- a/src/physfs/archivers/bind_physfs.c
+++ b/src/physfs/archivers/bind_physfs.c
@@ -14,6 +14,22 @@
 #define __PHYSICSFS_INTERNAL__
 #include "physfs_internal.h"
 
+static char *__BIND_PHYSFS_toDependent(dvoid *opaque, const char *name, const char *append)
+{
+	char *f = __PHYSFS_platformCvtToDependent((char *)opaque, name, NULL);
+
+	// Forbid recursions
+	if (!strncmp(name, opaque+1, strlen(opaque+1)))
+	{
+		return NULL;
+	}
+	else
+	{
+		char *f = __PHYSFS_platformCvtToDependent((char *)opaque, name, NULL);
+		return f;
+	}
+}
+
 static PHYSFS_sint64 BIND_PHYSFS_read(fvoid *opaque, void *buffer,
 	PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
 {
@@ -107,7 +123,7 @@ static void BIND_PHYSFS_enumerateFiles(dvoid *opaque, const char *dname,
                                int omitSymLinks, PHYSFS_EnumFilesCallback cb,
                                const char *origdir, void *callbackdata)
 {
-	char *d = __PHYSFS_platformCvtToDependent((char *)opaque, dname, NULL);
+	char *d = __BIND_PHYSFS_toDependent((char *)opaque, dname, NULL);
 
 	if (d != NULL)
 	{
@@ -119,7 +135,7 @@ static void BIND_PHYSFS_enumerateFiles(dvoid *opaque, const char *dname,
 
 static int BIND_PHYSFS_exists(dvoid *opaque, const char *name)
 {
-	char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
+	char *f = __BIND_PHYSFS_toDependent((char *) opaque, name, NULL);
 	int retval;
 
 	BAIL_IF_MACRO(f == NULL, NULL, 0);
@@ -131,7 +147,7 @@ static int BIND_PHYSFS_exists(dvoid *opaque, const char *name)
 
 static int BIND_PHYSFS_isDirectory(dvoid *opaque, const char *name, int *fileExists)
 {
-	char *d = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
+	char *d = __BIND_PHYSFS_toDependent((char *) opaque, name, NULL);
 	int retval = 0;
 
 	BAIL_IF_MACRO(d == NULL, NULL, 0);
@@ -153,7 +169,7 @@ static PHYSFS_sint64 BIND_PHYSFS_getLastModTime(dvoid *opaque,
                                         const char *name,
                                         int *fileExists)
 {
-	char *d = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
+	char *d = __BIND_PHYSFS_toDependent((char *) opaque, name, NULL);
 	PHYSFS_sint64 retval = -1;
 
 	BAIL_IF_MACRO(d == NULL, NULL, 0);
@@ -169,7 +185,7 @@ static fvoid *doOpen(dvoid *opaque, const char *name,
                      void *(*openFunc)(const char *filename),
                      int *fileExists)
 {
-    char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
+    char *f = __BIND_PHYSFS_toDependent((char *) opaque, name, NULL);
     void *rc = NULL;
 
     BAIL_IF_MACRO(f == NULL, NULL, NULL);
-- 
GitLab