+char const *gamedetect_argv_buffer[1024];
+
+void gamedetect_found_game( char const *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;
+}
+
+bool gamedetect_check_game( char const *gamefile, const char *checkfile1, const char *checkfile2, char *buf /* must have 64 bytes free after bufpos */, int bufpos ){
+ buf[bufpos] = '/';
+
+ strcpy( buf + bufpos + 1, checkfile1 );
+ globalOutputStream() << "Checking for a game file in " << buf << "\n";
+ if ( !file_exists( buf ) ) {
+ return false;
+ }
+
+ if ( checkfile2 ) {
+ strcpy( buf + bufpos + 1, checkfile2 );
+ globalOutputStream() << "Checking for a game file in " << buf << "\n";
+ if ( !file_exists( buf ) ) {
+ return false;
+ }
+ }
+
+ buf[bufpos + 1] = 0;
+ gamedetect_found_game( gamefile, buf );
+ return true;
+}
+
+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
+#if GDEF_OS_WINDOWS
+ if ( gamedetect_check_game( "nexuiz.game", "data/common-spog.pk3", "nexuiz.exe", buf, p - buf ) )
+#elif GDEF_OS_MACOS
+ if ( gamedetect_check_game( "nexuiz.game", "data/common-spog.pk3", "Nexuiz.app/Contents/Info.plist", buf, p - buf ) )
+#elif GDEF_OS_LINUX
+ if ( gamedetect_check_game( "nexuiz.game", "data/common-spog.pk3", "nexuiz-linux-glx.sh", buf, p - buf ) )
+#else
+ if ( gamedetect_check_game( "nexuiz.game", "data/common-spog.pk3", NULL, buf, p - buf ) )
+#endif
+ { return; }
+
+ // try to detect Quetoo installs
+ if ( gamedetect_check_game( "quetoo.game", "default/icons/quetoo.png", NULL, buf, p - buf ) ) {
+ return;
+ }
+
+ // try to detect Warsow installs
+ if ( gamedetect_check_game( "warsow.game", "basewsw/dedicated_autoexec.cfg", NULL, buf, p - buf ) ) {
+ return;
+ }
+
+ // we found nothing
+ // go backwards
+ --p;
+ while ( p != buf && *p != '/' && *p != '\\' )
+ --p;
+ }
+ }