From b7cd2135c98553b761866f5eda0cc89ce6f826fd Mon Sep 17 00:00:00 2001
From: divverent <divverent@61c419a2-8eb2-4b30-bcec-8cead039b335>
Date: Thu, 5 Mar 2009 11:27:30 +0000
Subject: [PATCH] detect being inside a Nexuiz install from Radiant C code, no
 longer from a shell script on OS X. This should make it work on all systems.

git-svn-id: svn://svn.icculus.org/netradiant/trunk@210 61c419a2-8eb2-4b30-bcec-8cead039b335
---
 radiant/environment.cpp                       | 82 ++++++++++++++++++-
 .../Contents/MacOS/netradiant.sh              | 25 ------
 2 files changed, 81 insertions(+), 26 deletions(-)

diff --git a/radiant/environment.cpp b/radiant/environment.cpp
index 45266919..99c6971a 100644
--- a/radiant/environment.cpp
+++ b/radiant/environment.cpp
@@ -26,9 +26,9 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 #include "stream/stringstream.h"
 #include "debugging/debugging.h"
 #include "os/path.h"
+#include "os/file.h"
 #include "cmdlib.h"
 
-
 int g_argc;
 char** g_argv;
 
@@ -55,6 +55,84 @@ void args_init(int argc, char* argv[])
   g_argv = argv;
 }
 
+char *gamedetect_argv_buffer[1024];
+void gamedetect_found_game(char *game, char *path)
+{
+  int argc;
+  static char buf[128];
+
+  if(g_argv == gamedetect_argv_buffer)
+    return;
+
+  globalOutputStream() << "Detected game " << game << " in " << path << "\n";
+
+  sprintf(buf, "-%s-EnginePath", game);
+  argc = 0;
+  gamedetect_argv_buffer[argc++] = "-global-gamefile";
+  gamedetect_argv_buffer[argc++] = game;
+  gamedetect_argv_buffer[argc++] = buf;
+  gamedetect_argv_buffer[argc++] = path;
+  if((size_t) (argc + g_argc) >= sizeof(gamedetect_argv_buffer) / sizeof(*gamedetect_argv_buffer) - 1)
+    g_argc = sizeof(gamedetect_argv_buffer) / sizeof(*gamedetect_argv_buffer) - g_argc - 1;
+  memcpy(gamedetect_argv_buffer + 4, g_argv, sizeof(*gamedetect_argv_buffer) * g_argc);
+  g_argc += argc;
+  g_argv = gamedetect_argv_buffer;
+}
+
+void gamedetect()
+{
+  // if we're inside a Nexuiz install
+  // default to nexuiz.game (unless the user used an option to inhibit this)
+  bool nogamedetect = false;
+  int i;
+  for(i = 1; i < g_argc - 1; ++i)
+    if(g_argv[i][0] == '-')
+	{
+      if(!strcmp(g_argv[i], "-gamedetect"))
+	    nogamedetect = !strcmp(g_argv[i+1], "false");
+	  ++i;
+	}
+  if(!nogamedetect)
+  {
+	static char buf[1024 + 64];
+	strncpy(buf, environment_get_app_path(), sizeof(buf));
+	buf[sizeof(buf) - 1 - 64] = 0;
+	if(!strlen(buf))
+	  return;
+
+	char *p = buf + strlen(buf) - 1; // point directly on the slash of get_app_path
+	while(p != buf)
+	{
+	  // TODO add more games to this
+	  // try to detect Nexuiz installs
+	  strcpy(p, "/data/common-spog.pk3");
+	  globalOutputStream() << "Checking for a game file in " << buf << "\n";
+	  if(file_exists(buf))
+	  {
+#if defined(WIN32)
+	    strcpy(p, "/nexuiz.exe");
+#elif defined(__APPLE__)
+	    strcpy(p, "/Nexuiz.app/Contents/Info.plist");
+#else
+	    strcpy(p, "/nexuiz-linux-glx.sh");
+#endif
+		if(file_exists(buf))
+		{
+		  p[1] = 0;
+		  gamedetect_found_game("nexuiz.game", buf);
+		  return;
+		}
+      }
+
+	  // we found nothing
+	  // go backwards
+	  --p;
+	  while(p != buf && *p != '/')
+	    --p;
+	}
+  }
+}
+
 namespace
 {
   CopiedString home_path;
@@ -145,6 +223,7 @@ void environment_init(int argc, char* argv[])
     app_path = getexename(real);
     ASSERT_MESSAGE(!string_empty(app_path.c_str()), "failed to deduce app path");
   }
+  gamedetect();
 }
 
 #elif defined(WIN32)
@@ -190,6 +269,7 @@ void environment_init(int argc, char* argv[])
     app << PathCleaned(filename);
     app_path = app.c_str();
   }
+  gamedetect();
 }
 
 #else
diff --git a/setup/data/osx/NetRadiant.app/Contents/MacOS/netradiant.sh b/setup/data/osx/NetRadiant.app/Contents/MacOS/netradiant.sh
index 14c4b52d..3df16e39 100755
--- a/setup/data/osx/NetRadiant.app/Contents/MacOS/netradiant.sh
+++ b/setup/data/osx/NetRadiant.app/Contents/MacOS/netradiant.sh
@@ -9,31 +9,6 @@ export PANGO_RC_FILE="$MY_DIRECTORY/Contents/MacOS/install/pangorc"
 export GDK_PIXBUF_MODULE_FILE="$MY_DIRECTORY/Contents/MacOS/install/gdk-pixbuf.loaders"
 
 cd "$MY_DIRECTORY/Contents/MacOS/install"
-
-# autodetect nexuiz installs
-NEX_DIRECTORY="/${MY_DIRECTORY%/*}"
-while :; do
-	if [ -z "$NEX_DIRECTORY" ]; then
-		break
-	fi
-	if [ -f "$NEX_DIRECTORY/data/common-spog.pk3" ]; then
-		if [ -d "$NEX_DIRECTORY/Nexuiz.app" ]; then
-			break
-		fi
-	fi
-	NEX_DIRECTORY=${NEX_DIRECTORY%/*}
-done
-case "$NEX_DIRECTORY" in
-	//*)
-		NEX_DIRECTORY=${NEX_DIRECTORY#/}
-		set -- -global-gamefile nexuiz.game -nexuiz.game-EnginePath "$NEX_DIRECTORY/"
-		# -global-gamePrompt false?
-		;;
-	*)
-		set --
-		;;
-esac
-
 if [ -x /usr/bin/open-x11 ]; then
 	/usr/bin/open-x11 ./radiant.ppc "$@" &
 else
-- 
2.39.5