+/*
+================
+FS_CheckGameDir
+================
+*/
+qboolean FS_CheckGameDir(const char *gamedir)
+{
+ qboolean success;
+ stringlist_t list;
+ stringlistinit(&list);
+ listdirectory(&list, va("%s%s/", fs_basedir, gamedir));
+ success = list.numstrings > 0;
+ stringlistfreecontents(&list);
+ return success;
+}
+
+
+/*
+================
+FS_Init
+================
+*/
+void FS_Init (void)
+{
+ int i;
+
+ fs_mempool = Mem_AllocPool("file management", 0, NULL);
+
+ strlcpy(fs_gamedir, "", sizeof(fs_gamedir));
+
+// If the base directory is explicitly defined by the compilation process
+#ifdef DP_FS_BASEDIR
+ strlcpy(fs_basedir, DP_FS_BASEDIR, sizeof(fs_basedir));
+#else
+ strlcpy(fs_basedir, "", sizeof(fs_basedir));
+
+#ifdef MACOSX
+ // FIXME: is there a better way to find the directory outside the .app?
+ if (strstr(com_argv[0], ".app/"))
+ {
+ char *split;
+
+ split = strstr(com_argv[0], ".app/");
+ while (split > com_argv[0] && *split != '/')
+ split--;
+ strlcpy(fs_basedir, com_argv[0], sizeof(fs_basedir));
+ fs_basedir[split - com_argv[0]] = 0;
+ }
+#endif
+#endif
+
+ PK3_OpenLibrary ();
+
+ // -basedir <path>
+ // Overrides the system supplied base directory (under GAMENAME)
+// COMMANDLINEOPTION: Filesystem: -basedir <path> chooses what base directory the game data is in, inside this there should be a data directory for the game (for example id1)
+ i = COM_CheckParm ("-basedir");
+ if (i && i < com_argc-1)
+ {
+ strlcpy (fs_basedir, com_argv[i+1], sizeof (fs_basedir));
+ i = (int)strlen (fs_basedir);
+ if (i > 0 && (fs_basedir[i-1] == '\\' || fs_basedir[i-1] == '/'))
+ fs_basedir[i-1] = 0;
+ }
+
+ // add a path separator to the end of the basedir if it lacks one
+ if (fs_basedir[0] && fs_basedir[strlen(fs_basedir) - 1] != '/' && fs_basedir[strlen(fs_basedir) - 1] != '\\')
+ strlcat(fs_basedir, "/", sizeof(fs_basedir));
+
+ if (!FS_CheckGameDir(gamedirname1))
+ Sys_Error("base gamedir %s%s/ not found!\n", fs_basedir, gamedirname1);
+
+ if (gamedirname2 && !FS_CheckGameDir(gamedirname2))
+ Sys_Error("base gamedir %s%s/ not found!\n", fs_basedir, gamedirname2);
+
+ // -game <gamedir>
+ // Adds basedir/gamedir as an override game
+ // LordHavoc: now supports multiple -game directories
+ for (i = 1;i < com_argc && fs_numgamedirs < MAX_GAMEDIRS;i++)
+ {
+ if (!com_argv[i])
+ continue;
+ if (!strcmp (com_argv[i], "-game") && i < com_argc-1)
+ {
+ i++;
+ if (FS_CheckNastyPath(com_argv[i], true))
+ Sys_Error("-game %s%s/ is a dangerous/non-portable path\n", fs_basedir, com_argv[i]);
+ if (!FS_CheckGameDir(com_argv[i]))
+ Sys_Error("-game %s%s/ not found!\n", fs_basedir, com_argv[i]);
+ // add the gamedir to the list of active gamedirs
+ strlcpy (fs_gamedirs[fs_numgamedirs], com_argv[i], sizeof(fs_gamedirs[fs_numgamedirs]));
+ fs_numgamedirs++;
+ }
+ }
+
+ // generate the searchpath
+ FS_Rescan();
+}
+
+void FS_Init_Commands(void)
+{
+ Cvar_RegisterVariable (&scr_screenshot_name);
+ Cvar_RegisterVariable (&fs_empty_files_in_pack_mark_deletions);
+
+ Cmd_AddCommand ("gamedir", FS_GameDir_f, "changes active gamedir list (can take multiple arguments), not including base directory (example usage: gamedir ctf)");
+ Cmd_AddCommand ("fs_rescan", FS_Rescan_f, "rescans filesystem for new pack archives and any other changes");
+ Cmd_AddCommand ("path", FS_Path_f, "print searchpath (game directories and archives)");
+ Cmd_AddCommand ("dir", FS_Dir_f, "list files in searchpath matching an * filename pattern, one per line");
+ Cmd_AddCommand ("ls", FS_Ls_f, "list files in searchpath matching an * filename pattern, multiple per line");
+}
+
+/*
+================
+FS_Shutdown
+================
+*/
+void FS_Shutdown (void)
+{
+ Mem_FreePool (&fs_mempool);
+}
+