2014-06-01 17:55:23 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the DXX-Rebirth project <http://www.dxx-rebirth.com/>.
|
|
|
|
* It is copyright by its individual contributors, as recorded in the
|
|
|
|
* project's Git history. See COPYING.txt at the top level for license
|
|
|
|
* terms and a link to the Git history.
|
|
|
|
*/
|
2010-08-09 14:04:22 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Some simple physfs extensions
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#if !defined(macintosh) && !defined(_MSC_VER)
|
|
|
|
#include <sys/param.h>
|
|
|
|
#endif
|
|
|
|
#if defined(__MACH__) && defined(__APPLE__)
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <unistd.h> // for chdir hack
|
|
|
|
#include <HIServices/Processes.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "physfsx.h"
|
2013-03-03 19:41:09 +00:00
|
|
|
#include "args.h"
|
2010-08-15 06:57:51 +00:00
|
|
|
#include "newdemo.h"
|
2013-12-26 04:18:28 +00:00
|
|
|
#include "console.h"
|
|
|
|
#include "strutil.h"
|
|
|
|
#include "ignorecase.h"
|
2010-08-09 14:04:22 +00:00
|
|
|
|
2013-11-26 22:19:52 +00:00
|
|
|
static const file_extension_t archive_exts[] = { "dxa", "" };
|
|
|
|
|
2014-05-24 23:02:45 +00:00
|
|
|
char *PHYSFSX_fgets(char *buf, size_t n, PHYSFS_file *const fp)
|
|
|
|
{
|
|
|
|
PHYSFS_sint64 r = PHYSFS_read(fp, buf, sizeof(*buf), n - 1);
|
|
|
|
if (r <= 0)
|
|
|
|
return NULL;
|
|
|
|
char *p = buf;
|
|
|
|
for (char *e = buf + r;;)
|
|
|
|
{
|
|
|
|
if (p == e)
|
|
|
|
{
|
|
|
|
*p = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
char c = *p++;
|
|
|
|
if (c == 0)
|
|
|
|
break;
|
|
|
|
if (c == '\n')
|
|
|
|
{
|
|
|
|
p[-1] = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (c == '\r')
|
|
|
|
{
|
|
|
|
p[-1] = 0;
|
|
|
|
if (p != e && *p == '\n')
|
|
|
|
++p;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
size_t i = p - buf;
|
|
|
|
if (i < static_cast<size_t>(r))
|
|
|
|
PHYSFSX_fseek(fp, -(r - i), SEEK_CUR);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2013-11-26 22:10:57 +00:00
|
|
|
int PHYSFSX_checkMatchingExtension(const file_extension_t *exts, const char *filename)
|
|
|
|
{
|
|
|
|
const char *ext = strrchr(filename, '.');
|
|
|
|
if (!ext)
|
|
|
|
return 0;
|
2013-11-26 22:19:52 +00:00
|
|
|
++ext;
|
2013-11-26 22:10:57 +00:00
|
|
|
for (const file_extension_t *k = exts;; ++k) // see if the file is of a type we want
|
|
|
|
{
|
2013-11-26 22:19:52 +00:00
|
|
|
if (!(*k)[0])
|
2013-11-26 22:10:57 +00:00
|
|
|
return 0;
|
|
|
|
if (!d_stricmp(ext, *k))
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-09 14:04:22 +00:00
|
|
|
// Initialise PhysicsFS, set up basic search paths and add arguments from .ini file.
|
|
|
|
// The .ini file can be in either the user directory or the same directory as the program.
|
|
|
|
// The user directory is searched first.
|
|
|
|
void PHYSFSX_init(int argc, char *argv[])
|
|
|
|
{
|
2012-05-14 10:20:30 +00:00
|
|
|
#if defined(__unix__) || defined(__APPLE__) || defined(__MACH__)
|
2010-09-25 13:55:22 +00:00
|
|
|
char fullPath[PATH_MAX + 5];
|
2012-05-14 10:20:30 +00:00
|
|
|
#endif
|
2010-08-09 14:04:22 +00:00
|
|
|
#if defined(__unix__)
|
2013-09-02 23:21:13 +00:00
|
|
|
const char *path = NULL;
|
2010-08-09 14:04:22 +00:00
|
|
|
#endif
|
|
|
|
#ifdef macintosh // Mac OS 9
|
|
|
|
char base_dir[PATH_MAX];
|
|
|
|
int bundle = 0;
|
|
|
|
#else
|
|
|
|
#define base_dir PHYSFS_getBaseDir()
|
|
|
|
#endif
|
|
|
|
|
|
|
|
PHYSFS_init(argv[0]);
|
|
|
|
PHYSFS_permitSymbolicLinks(1);
|
|
|
|
|
|
|
|
#ifdef macintosh
|
|
|
|
strcpy(base_dir, PHYSFS_getBaseDir());
|
|
|
|
if (strstr(base_dir, ".app:Contents:MacOSClassic")) // the Mac OS 9 program is still in the .app bundle
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
bundle = 1;
|
|
|
|
if (base_dir[strlen(base_dir) - 1] == ':')
|
|
|
|
base_dir[strlen(base_dir) - 1] = '\0';
|
|
|
|
p = strrchr(base_dir, ':'); *p = '\0'; // path to 'Contents'
|
|
|
|
p = strrchr(base_dir, ':'); *p = '\0'; // path to bundle
|
|
|
|
p = strrchr(base_dir, ':'); *p = '\0'; // path to directory containing bundle
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if (defined(__APPLE__) && defined(__MACH__)) // others?
|
|
|
|
chdir(base_dir); // make sure relative hogdir paths work
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__unix__)
|
2013-03-03 01:03:33 +00:00
|
|
|
#if defined(DXX_BUILD_DESCENT_I)
|
|
|
|
#define DESCENT_PATH_NUMBER "1"
|
|
|
|
#elif defined(DXX_BUILD_DESCENT_II)
|
|
|
|
#define DESCENT_PATH_NUMBER "2"
|
|
|
|
#endif
|
2010-08-09 14:04:22 +00:00
|
|
|
# if !(defined(__APPLE__) && defined(__MACH__))
|
2013-03-03 01:03:33 +00:00
|
|
|
path = "~/.d" DESCENT_PATH_NUMBER "x-rebirth/";
|
2010-08-09 14:04:22 +00:00
|
|
|
# else
|
2013-03-03 01:03:33 +00:00
|
|
|
path = "~/Library/Preferences/D" DESCENT_PATH_NUMBER "X Rebirth/";
|
2010-08-09 14:04:22 +00:00
|
|
|
# endif
|
|
|
|
|
|
|
|
if (path[0] == '~') // yes, this tilde can be put before non-unix paths.
|
|
|
|
{
|
|
|
|
const char *home = PHYSFS_getUserDir();
|
|
|
|
|
|
|
|
strcpy(fullPath, home); // prepend home to the path
|
|
|
|
path++;
|
|
|
|
if (*path == *PHYSFS_getDirSeparator())
|
|
|
|
path++;
|
|
|
|
strncat(fullPath, path, PATH_MAX + 5 - strlen(home));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
strncpy(fullPath, path, PATH_MAX + 5);
|
|
|
|
|
|
|
|
PHYSFS_setWriteDir(fullPath);
|
|
|
|
if (!PHYSFS_getWriteDir())
|
|
|
|
{ // need to make it
|
|
|
|
char *p;
|
|
|
|
char ancestor[PATH_MAX + 5]; // the directory which actually exists
|
|
|
|
char child[PATH_MAX + 5]; // the directory relative to the above we're trying to make
|
|
|
|
|
|
|
|
strcpy(ancestor, fullPath);
|
|
|
|
while (!PHYSFS_getWriteDir() && ((p = strrchr(ancestor, *PHYSFS_getDirSeparator()))))
|
|
|
|
{
|
|
|
|
if (p[1] == 0)
|
|
|
|
{ // separator at the end (intended here, for safety)
|
|
|
|
*p = 0; // kill this separator
|
|
|
|
if (!((p = strrchr(ancestor, *PHYSFS_getDirSeparator()))))
|
|
|
|
break; // give up, this is (usually) the root directory
|
|
|
|
}
|
|
|
|
|
|
|
|
p[1] = 0; // go to parent
|
|
|
|
PHYSFS_setWriteDir(ancestor);
|
|
|
|
}
|
|
|
|
|
|
|
|
strcpy(child, fullPath + strlen(ancestor));
|
|
|
|
for (p = child; (p = strchr(p, *PHYSFS_getDirSeparator())); p++)
|
|
|
|
*p = '/';
|
|
|
|
PHYSFS_mkdir(child);
|
|
|
|
PHYSFS_setWriteDir(fullPath);
|
|
|
|
}
|
2013-12-28 18:16:19 +00:00
|
|
|
const char *writedir = PHYSFS_getWriteDir();
|
|
|
|
con_printf(CON_DEBUG, "PHYSFS: append write directory \"%s\" to search path", writedir);
|
|
|
|
PHYSFS_addToSearchPath(writedir, 1);
|
2010-08-09 14:04:22 +00:00
|
|
|
#endif
|
2013-12-28 18:16:19 +00:00
|
|
|
con_printf(CON_DEBUG, "PHYSFS: temporarily append base directory \"%s\" to search path", base_dir);
|
2010-08-09 14:04:22 +00:00
|
|
|
PHYSFS_addToSearchPath(base_dir, 1);
|
|
|
|
InitArgs( argc,argv );
|
|
|
|
PHYSFS_removeFromSearchPath(base_dir);
|
|
|
|
|
|
|
|
if (!PHYSFS_getWriteDir())
|
|
|
|
{
|
|
|
|
PHYSFS_setWriteDir(base_dir);
|
|
|
|
if (!PHYSFS_getWriteDir())
|
|
|
|
Error("can't set write dir: %s\n", PHYSFS_getLastError());
|
|
|
|
else
|
|
|
|
PHYSFS_addToSearchPath(PHYSFS_getWriteDir(), 0);
|
|
|
|
}
|
|
|
|
|
2011-06-01 07:59:55 +00:00
|
|
|
//tell PHYSFS where hogdir is
|
2010-08-09 14:04:22 +00:00
|
|
|
if (GameArg.SysHogDir)
|
2013-12-28 18:16:19 +00:00
|
|
|
{
|
|
|
|
con_printf(CON_DEBUG, "PHYSFS: append argument hog directory \"%s\" to search path", GameArg.SysHogDir);
|
2010-08-09 14:04:22 +00:00
|
|
|
PHYSFS_addToSearchPath(GameArg.SysHogDir,1);
|
2013-12-28 18:16:19 +00:00
|
|
|
}
|
2010-08-09 14:04:22 +00:00
|
|
|
#if defined(__unix__)
|
|
|
|
else if (!GameArg.SysNoHogDir)
|
2013-12-28 18:16:19 +00:00
|
|
|
{
|
|
|
|
con_printf(CON_DEBUG, "PHYSFS: append sharepath directory \"" SHAREPATH "\" to search path");
|
2010-08-09 14:04:22 +00:00
|
|
|
PHYSFS_addToSearchPath(SHAREPATH, 1);
|
2013-12-28 18:16:19 +00:00
|
|
|
}
|
2010-08-09 14:04:22 +00:00
|
|
|
#endif
|
|
|
|
|
2012-03-03 08:03:03 +00:00
|
|
|
PHYSFSX_addRelToSearchPath("data", 1); // 'Data' subdirectory
|
2010-09-25 03:35:23 +00:00
|
|
|
|
2010-08-09 14:04:22 +00:00
|
|
|
// For Macintosh, add the 'Resources' directory in the .app bundle to the searchpaths
|
|
|
|
#if defined(__APPLE__) && defined(__MACH__)
|
|
|
|
{
|
|
|
|
ProcessSerialNumber psn = { 0, kCurrentProcess };
|
|
|
|
FSRef fsref;
|
|
|
|
OSStatus err;
|
|
|
|
|
|
|
|
err = GetProcessBundleLocation(&psn, &fsref);
|
|
|
|
if (err == noErr)
|
|
|
|
err = FSRefMakePath(&fsref, (ubyte *)fullPath, PATH_MAX);
|
|
|
|
|
|
|
|
if (err == noErr)
|
|
|
|
{
|
|
|
|
strncat(fullPath, "/Contents/Resources/", PATH_MAX + 4 - strlen(fullPath));
|
|
|
|
fullPath[PATH_MAX + 4] = '\0';
|
2013-12-28 18:16:19 +00:00
|
|
|
con_printf(CON_DEBUG, "PHYSFS: append resources directory \"%s\" to search path", fullPath);
|
2010-08-09 14:04:22 +00:00
|
|
|
PHYSFS_addToSearchPath(fullPath, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#elif defined(macintosh)
|
|
|
|
if (bundle)
|
|
|
|
{
|
|
|
|
base_dir[strlen(base_dir)] = ':'; // go back in the bundle
|
|
|
|
base_dir[strlen(base_dir)] = ':'; // go back in 'Contents'
|
|
|
|
strncat(base_dir, ":Resources:", PATH_MAX - 1 - strlen(base_dir));
|
|
|
|
base_dir[PATH_MAX - 1] = '\0';
|
2013-12-28 18:16:19 +00:00
|
|
|
con_printf(CON_DEBUG, "PHYSFS: append bundle directory \"%s\" to search path", base_dir);
|
2010-08-09 14:04:22 +00:00
|
|
|
PHYSFS_addToSearchPath(base_dir, 1);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-03-03 08:03:03 +00:00
|
|
|
// Add a searchpath, but that searchpath is relative to an existing searchpath
|
|
|
|
// It will add the first one it finds and return 1, if it doesn't find any it returns 0
|
2012-07-22 23:17:54 +00:00
|
|
|
int PHYSFSX_addRelToSearchPath(const char *relname, int add_to_end)
|
2011-06-01 07:59:55 +00:00
|
|
|
{
|
2012-03-03 08:03:03 +00:00
|
|
|
char relname2[PATH_MAX], pathname[PATH_MAX];
|
2011-06-01 07:59:55 +00:00
|
|
|
|
2012-06-01 10:46:03 +00:00
|
|
|
snprintf(relname2, sizeof(relname2), "%s", relname);
|
2012-03-03 08:03:03 +00:00
|
|
|
PHYSFSEXT_locateCorrectCase(relname2);
|
2011-06-01 07:59:55 +00:00
|
|
|
|
2012-03-03 08:03:03 +00:00
|
|
|
if (!PHYSFSX_getRealPath(relname2, pathname))
|
2011-06-01 07:59:55 +00:00
|
|
|
return 0;
|
|
|
|
|
2013-12-28 18:16:19 +00:00
|
|
|
con_printf(CON_DEBUG, "PHYSFS: %s canonical directory \"%s\" to search path from relative name \"%s\"", add_to_end ? "append" : "set", pathname, relname);
|
2011-06-01 07:59:55 +00:00
|
|
|
return PHYSFS_addToSearchPath(pathname, add_to_end);
|
|
|
|
}
|
|
|
|
|
2012-07-22 23:17:54 +00:00
|
|
|
int PHYSFSX_removeRelFromSearchPath(const char *relname)
|
2011-06-01 07:59:55 +00:00
|
|
|
{
|
2012-03-03 08:03:03 +00:00
|
|
|
char relname2[PATH_MAX], pathname[PATH_MAX];
|
2011-06-01 07:59:55 +00:00
|
|
|
|
2012-06-01 10:46:03 +00:00
|
|
|
snprintf(relname2, sizeof(relname2), "%s", relname);
|
2012-03-03 08:03:03 +00:00
|
|
|
PHYSFSEXT_locateCorrectCase(relname2);
|
2011-06-01 07:59:55 +00:00
|
|
|
|
2012-03-03 08:03:03 +00:00
|
|
|
if (!PHYSFSX_getRealPath(relname2, pathname))
|
2011-06-01 07:59:55 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return PHYSFS_removeFromSearchPath(pathname);
|
|
|
|
}
|
|
|
|
|
2012-07-22 23:17:54 +00:00
|
|
|
int PHYSFSX_fsize(const char *hogname)
|
2011-06-01 07:59:55 +00:00
|
|
|
{
|
|
|
|
PHYSFS_file *fp;
|
|
|
|
char hogname2[PATH_MAX];
|
|
|
|
int size;
|
|
|
|
|
2012-06-01 10:46:03 +00:00
|
|
|
snprintf(hogname2, sizeof(hogname2), "%s", hogname);
|
2011-06-01 07:59:55 +00:00
|
|
|
PHYSFSEXT_locateCorrectCase(hogname2);
|
|
|
|
|
|
|
|
fp = PHYSFS_openRead(hogname2);
|
|
|
|
if (fp == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
size = PHYSFS_fileLength(fp);
|
|
|
|
PHYSFS_close(fp);
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2010-11-26 11:50:18 +00:00
|
|
|
void PHYSFSX_listSearchPathContent()
|
|
|
|
{
|
|
|
|
char **i, **list;
|
|
|
|
|
2013-12-07 00:47:27 +00:00
|
|
|
con_printf(CON_DEBUG, "PHYSFS: Listing contents of Search Path.");
|
2010-11-26 11:50:18 +00:00
|
|
|
list = PHYSFS_getSearchPath();
|
|
|
|
for (i = list; *i != NULL; i++)
|
2013-12-07 00:47:27 +00:00
|
|
|
con_printf(CON_DEBUG, "PHYSFS: [%s] is in the Search Path.", *i);
|
2010-11-26 11:50:18 +00:00
|
|
|
PHYSFS_freeList(list);
|
|
|
|
|
|
|
|
list = PHYSFS_enumerateFiles("");
|
|
|
|
for (i = list; *i != NULL; i++)
|
2013-12-07 00:47:27 +00:00
|
|
|
con_printf(CON_DEBUG, "PHYSFS: * We've got [%s].", *i);
|
2010-11-26 11:50:18 +00:00
|
|
|
PHYSFS_freeList(list);
|
|
|
|
}
|
|
|
|
|
|
|
|
// checks which archives are supported by PhysFS. Return 0 if some essential (HOG) is not supported
|
|
|
|
int PHYSFSX_checkSupportedArchiveTypes()
|
|
|
|
{
|
|
|
|
const PHYSFS_ArchiveInfo **i;
|
2013-03-03 01:03:33 +00:00
|
|
|
int hog_sup = 0;
|
|
|
|
#ifdef DXX_BUILD_DESCENT_II
|
|
|
|
int mvl_sup = 0;
|
|
|
|
#endif
|
2010-11-26 11:50:18 +00:00
|
|
|
|
2013-12-07 00:47:27 +00:00
|
|
|
con_printf(CON_DEBUG, "PHYSFS: Checking supported archive types.");
|
2010-11-26 11:50:18 +00:00
|
|
|
for (i = PHYSFS_supportedArchiveTypes(); *i != NULL; i++)
|
|
|
|
{
|
2013-12-07 00:47:27 +00:00
|
|
|
con_printf(CON_DEBUG, "PHYSFS: Supported archive: [%s], which is [%s].", (*i)->extension, (*i)->description);
|
2012-05-18 23:36:43 +00:00
|
|
|
if (!d_stricmp((*i)->extension, "HOG"))
|
2010-11-26 11:50:18 +00:00
|
|
|
hog_sup = 1;
|
2013-03-03 01:03:33 +00:00
|
|
|
#ifdef DXX_BUILD_DESCENT_II
|
2012-05-18 23:36:43 +00:00
|
|
|
if (!d_stricmp((*i)->extension, "MVL"))
|
2010-11-26 11:50:18 +00:00
|
|
|
mvl_sup = 1;
|
2013-03-03 01:03:33 +00:00
|
|
|
#endif
|
2010-11-26 11:50:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!hog_sup)
|
2013-12-07 00:47:27 +00:00
|
|
|
con_printf(CON_CRITICAL, "PHYSFS: HOG not supported. The game will not work without!");
|
2013-03-03 01:03:33 +00:00
|
|
|
#ifdef DXX_BUILD_DESCENT_II
|
2010-11-26 11:50:18 +00:00
|
|
|
if (!mvl_sup)
|
2013-12-07 00:47:27 +00:00
|
|
|
con_printf(CON_URGENT, "PHYSFS: MVL not supported. Won't be able to play movies!");
|
2013-03-03 01:03:33 +00:00
|
|
|
#endif
|
2010-11-26 11:50:18 +00:00
|
|
|
|
|
|
|
return hog_sup;
|
|
|
|
}
|
|
|
|
|
2010-08-09 14:04:22 +00:00
|
|
|
int PHYSFSX_getRealPath(const char *stdPath, char *realPath)
|
|
|
|
{
|
|
|
|
const char *realDir = PHYSFS_getRealDir(stdPath);
|
|
|
|
const char *sep = PHYSFS_getDirSeparator();
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
if (!realDir)
|
|
|
|
{
|
|
|
|
realDir = PHYSFS_getWriteDir();
|
|
|
|
if (!realDir)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
strncpy(realPath, realDir, PATH_MAX - 1);
|
|
|
|
if (strlen(realPath) >= strlen(sep))
|
|
|
|
{
|
|
|
|
p = realPath + strlen(realPath) - strlen(sep);
|
|
|
|
if (strcmp(p, sep)) // no sep at end of realPath
|
|
|
|
strncat(realPath, sep, PATH_MAX - 1 - strlen(realPath));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strlen(stdPath) >= 1)
|
|
|
|
if (*stdPath == '/')
|
|
|
|
stdPath++;
|
|
|
|
|
|
|
|
while (*stdPath)
|
|
|
|
{
|
|
|
|
if (*stdPath == '/')
|
|
|
|
strncat(realPath, sep, PATH_MAX - 1 - strlen(realPath));
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (strlen(realPath) < PATH_MAX - 2)
|
|
|
|
{
|
|
|
|
p = realPath + strlen(realPath);
|
|
|
|
p[0] = *stdPath;
|
|
|
|
p[1] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
stdPath++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-11-21 11:55:35 +00:00
|
|
|
// checks if path is already added to Searchpath. Returns 0 if yes, 1 if not.
|
2012-07-22 23:17:54 +00:00
|
|
|
int PHYSFSX_isNewPath(const char *path)
|
2010-11-21 11:55:35 +00:00
|
|
|
{
|
|
|
|
int is_new_path = 1;
|
|
|
|
char **i, **list;
|
|
|
|
|
|
|
|
list = PHYSFS_getSearchPath();
|
|
|
|
for (i = list; *i != NULL; i++)
|
|
|
|
{
|
|
|
|
if (!strcmp(path, *i))
|
|
|
|
{
|
|
|
|
is_new_path = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PHYSFS_freeList(list);
|
|
|
|
|
|
|
|
return is_new_path;
|
|
|
|
}
|
|
|
|
|
2012-07-22 23:17:54 +00:00
|
|
|
int PHYSFSX_rename(const char *oldpath, const char *newpath)
|
2010-08-09 14:04:22 +00:00
|
|
|
{
|
2012-11-18 18:21:50 +00:00
|
|
|
char old[PATH_MAX], n[PATH_MAX];
|
2010-08-09 14:04:22 +00:00
|
|
|
|
|
|
|
PHYSFSX_getRealPath(oldpath, old);
|
2012-11-18 18:21:50 +00:00
|
|
|
PHYSFSX_getRealPath(newpath, n);
|
|
|
|
return (rename(old, n) == 0);
|
2010-08-09 14:04:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find files at path that have an extension listed in exts
|
|
|
|
// The extension list exts must be NULL-terminated, with each ext beginning with a '.'
|
2013-11-26 22:19:52 +00:00
|
|
|
char **PHYSFSX_findFiles(const char *path, const file_extension_t *exts)
|
2010-08-09 14:04:22 +00:00
|
|
|
{
|
|
|
|
char **list = PHYSFS_enumerateFiles(path);
|
2012-07-22 23:17:54 +00:00
|
|
|
char **i, **j = list;
|
2010-08-09 14:04:22 +00:00
|
|
|
|
|
|
|
if (list == NULL)
|
|
|
|
return NULL; // out of memory: not so good
|
|
|
|
|
|
|
|
for (i = list; *i; i++)
|
|
|
|
{
|
2013-11-26 22:10:57 +00:00
|
|
|
if (PHYSFSX_checkMatchingExtension(exts, *i))
|
2010-08-09 14:04:22 +00:00
|
|
|
*j++ = *i;
|
|
|
|
else
|
|
|
|
free(*i);
|
|
|
|
}
|
|
|
|
|
|
|
|
*j = NULL;
|
2013-10-20 02:34:43 +00:00
|
|
|
char **r = (char **)realloc(list, (j - list + 1)*sizeof(char *)); // save a bit of memory (or a lot?)
|
|
|
|
if (r)
|
|
|
|
return r;
|
2010-08-09 14:04:22 +00:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Same function as above but takes a real directory as second argument, only adding files originating from this directory.
|
|
|
|
// This can be used to further seperate files in search path but it must be made sure realpath is properly formatted.
|
2013-11-26 22:19:52 +00:00
|
|
|
char **PHYSFSX_findabsoluteFiles(const char *path, const char *realpath, const file_extension_t *exts)
|
2010-08-09 14:04:22 +00:00
|
|
|
{
|
|
|
|
char **list = PHYSFS_enumerateFiles(path);
|
2012-07-22 23:17:54 +00:00
|
|
|
char **i, **j = list;
|
2010-08-09 14:04:22 +00:00
|
|
|
|
|
|
|
if (list == NULL)
|
|
|
|
return NULL; // out of memory: not so good
|
|
|
|
|
|
|
|
for (i = list; *i; i++)
|
|
|
|
{
|
2013-11-26 22:10:57 +00:00
|
|
|
if (PHYSFSX_checkMatchingExtension(exts, *i) && (!strcmp(PHYSFS_getRealDir(*i),realpath)))
|
2010-08-09 14:04:22 +00:00
|
|
|
*j++ = *i;
|
|
|
|
else
|
|
|
|
free(*i);
|
|
|
|
}
|
|
|
|
|
|
|
|
*j = NULL;
|
2013-10-20 02:34:43 +00:00
|
|
|
char **r = (char **)realloc(list, (j - list + 1)*sizeof(char *)); // save a bit of memory (or a lot?)
|
|
|
|
if (r)
|
|
|
|
return r;
|
2010-08-09 14:04:22 +00:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
// returns -1 if error
|
|
|
|
// Gets bytes free in current write dir
|
|
|
|
PHYSFS_sint64 PHYSFSX_getFreeDiskSpace()
|
|
|
|
{
|
|
|
|
#if defined(__linux__) || (defined(__MACH__) && defined(__APPLE__))
|
|
|
|
struct statfs sfs;
|
|
|
|
|
|
|
|
if (!statfs(PHYSFS_getWriteDir(), &sfs))
|
|
|
|
return (PHYSFS_sint64)(sfs.f_bavail * sfs.f_bsize);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
#else
|
|
|
|
return 0x7FFFFFFF;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-06-01 07:59:55 +00:00
|
|
|
int PHYSFSX_exists(const char *filename, int ignorecase)
|
|
|
|
{
|
|
|
|
char filename2[PATH_MAX];
|
|
|
|
|
|
|
|
if (!ignorecase)
|
|
|
|
return PHYSFS_exists(filename);
|
|
|
|
|
2012-06-01 10:46:03 +00:00
|
|
|
snprintf(filename2, sizeof(filename2), "%s", filename);
|
2011-06-01 07:59:55 +00:00
|
|
|
PHYSFSEXT_locateCorrectCase(filename2);
|
|
|
|
|
|
|
|
return PHYSFS_exists(filename2);
|
|
|
|
}
|
|
|
|
|
2010-08-09 14:04:22 +00:00
|
|
|
//Open a file for reading, set up a buffer
|
2012-07-22 23:17:54 +00:00
|
|
|
PHYSFS_file *PHYSFSX_openReadBuffered(const char *filename)
|
2010-08-09 14:04:22 +00:00
|
|
|
{
|
|
|
|
PHYSFS_file *fp;
|
|
|
|
PHYSFS_uint64 bufSize;
|
|
|
|
char filename2[PATH_MAX];
|
|
|
|
|
|
|
|
if (filename[0] == '\x01')
|
|
|
|
{
|
|
|
|
//FIXME: don't look in dir, only in hogfile
|
|
|
|
filename++;
|
|
|
|
}
|
|
|
|
|
2012-06-01 10:46:03 +00:00
|
|
|
snprintf(filename2, sizeof(filename2), "%s", filename);
|
2010-08-09 14:04:22 +00:00
|
|
|
PHYSFSEXT_locateCorrectCase(filename2);
|
|
|
|
|
|
|
|
fp = PHYSFS_openRead(filename2);
|
|
|
|
if (!fp)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
bufSize = PHYSFS_fileLength(fp);
|
|
|
|
while (!PHYSFS_setBuffer(fp, bufSize) && bufSize)
|
|
|
|
bufSize /= 2; // even if the error isn't memory full, for a 20MB file it'll only do this 8 times
|
|
|
|
|
|
|
|
return fp;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Open a file for writing, set up a buffer
|
2012-07-22 23:17:54 +00:00
|
|
|
PHYSFS_file *PHYSFSX_openWriteBuffered(const char *filename)
|
2010-08-09 14:04:22 +00:00
|
|
|
{
|
|
|
|
PHYSFS_file *fp;
|
|
|
|
PHYSFS_uint64 bufSize = 1024*1024; // hmm, seems like an OK size.
|
|
|
|
|
|
|
|
fp = PHYSFS_openWrite(filename);
|
|
|
|
if (!fp)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
while (!PHYSFS_setBuffer(fp, bufSize) && bufSize)
|
|
|
|
bufSize /= 2;
|
|
|
|
|
|
|
|
return fp;
|
|
|
|
}
|
|
|
|
|
2010-09-25 03:35:23 +00:00
|
|
|
/*
|
|
|
|
* Add archives to the game.
|
|
|
|
* 1) archives from Sharepath/Data to extend/replace builtin game content
|
|
|
|
* 2) archived demos
|
|
|
|
*/
|
2010-08-15 06:57:51 +00:00
|
|
|
void PHYSFSX_addArchiveContent()
|
|
|
|
{
|
|
|
|
char **list = NULL;
|
2010-11-26 11:50:18 +00:00
|
|
|
int i = 0, content_updated = 0;
|
2010-08-15 06:57:51 +00:00
|
|
|
|
2013-12-07 00:47:27 +00:00
|
|
|
con_printf(CON_DEBUG, "PHYSFS: Adding archives to the game.");
|
2010-08-15 06:57:51 +00:00
|
|
|
// find files in Searchpath ...
|
|
|
|
list = PHYSFSX_findFiles("", archive_exts);
|
|
|
|
// if found, add them...
|
|
|
|
for (i = 0; list[i] != NULL; i++)
|
|
|
|
{
|
2013-12-08 17:17:05 +00:00
|
|
|
char realfile[PATH_MAX];
|
|
|
|
PHYSFSX_getRealPath(list[i],realfile);
|
|
|
|
if (PHYSFS_addToSearchPath(realfile, 0))
|
2010-11-26 11:50:18 +00:00
|
|
|
{
|
2013-12-08 17:17:05 +00:00
|
|
|
con_printf(CON_DEBUG, "PHYSFS: Added %s to Search Path",realfile);
|
2010-11-26 11:50:18 +00:00
|
|
|
content_updated = 1;
|
|
|
|
}
|
2010-08-15 06:57:51 +00:00
|
|
|
}
|
|
|
|
PHYSFS_freeList(list);
|
|
|
|
list = NULL;
|
|
|
|
|
2010-10-10 10:48:24 +00:00
|
|
|
#if PHYSFS_VER_MAJOR >= 2
|
2010-08-15 06:57:51 +00:00
|
|
|
// find files in DEMO_DIR ...
|
|
|
|
list = PHYSFSX_findFiles(DEMO_DIR, archive_exts);
|
|
|
|
// if found, add them...
|
|
|
|
for (i = 0; list[i] != NULL; i++)
|
|
|
|
{
|
2013-12-08 17:18:14 +00:00
|
|
|
char demofile[PATH_MAX], realfile[PATH_MAX];
|
|
|
|
snprintf(demofile, sizeof(demofile), "%s%s", DEMO_DIR, list[i]);
|
|
|
|
PHYSFSX_getRealPath(demofile,realfile);
|
|
|
|
if (PHYSFS_mount(realfile, DEMO_DIR, 0))
|
2010-11-26 11:50:18 +00:00
|
|
|
{
|
2013-12-08 17:18:14 +00:00
|
|
|
con_printf(CON_DEBUG, "PHYSFS: Added %s to %s",realfile, DEMO_DIR);
|
2010-11-26 11:50:18 +00:00
|
|
|
content_updated = 1;
|
|
|
|
}
|
2010-08-15 06:57:51 +00:00
|
|
|
}
|
2010-10-10 10:48:24 +00:00
|
|
|
|
2010-08-15 06:57:51 +00:00
|
|
|
PHYSFS_freeList(list);
|
|
|
|
list = NULL;
|
2011-09-26 17:15:43 +00:00
|
|
|
#endif
|
2010-11-26 11:50:18 +00:00
|
|
|
|
|
|
|
if (content_updated)
|
|
|
|
{
|
2013-12-07 00:47:27 +00:00
|
|
|
con_printf(CON_DEBUG, "Game content updated!");
|
2010-11-26 11:50:18 +00:00
|
|
|
PHYSFSX_listSearchPathContent();
|
|
|
|
}
|
2010-08-15 06:57:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Removes content added above when quitting game
|
|
|
|
void PHYSFSX_removeArchiveContent()
|
|
|
|
{
|
|
|
|
char **list = NULL;
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
// find files in Searchpath ...
|
|
|
|
list = PHYSFSX_findFiles("", archive_exts);
|
|
|
|
// if found, remove them...
|
|
|
|
for (i = 0; list[i] != NULL; i++)
|
|
|
|
{
|
2013-12-08 17:17:05 +00:00
|
|
|
char realfile[PATH_MAX];
|
|
|
|
PHYSFSX_getRealPath(list[i],realfile);
|
|
|
|
PHYSFS_removeFromSearchPath(realfile);
|
2010-08-15 06:57:51 +00:00
|
|
|
}
|
|
|
|
PHYSFS_freeList(list);
|
|
|
|
list = NULL;
|
|
|
|
|
|
|
|
// find files in DEMO_DIR ...
|
|
|
|
list = PHYSFSX_findFiles(DEMO_DIR, archive_exts);
|
|
|
|
// if found, remove them...
|
|
|
|
for (i = 0; list[i] != NULL; i++)
|
|
|
|
{
|
2013-12-08 17:18:14 +00:00
|
|
|
char demofile[PATH_MAX], realfile[PATH_MAX];
|
|
|
|
snprintf(demofile, sizeof(demofile), "%s%s", DEMO_DIR, list[i]);
|
|
|
|
PHYSFSX_getRealPath(demofile,realfile);
|
|
|
|
PHYSFS_removeFromSearchPath(realfile);
|
2010-08-15 06:57:51 +00:00
|
|
|
}
|
|
|
|
PHYSFS_freeList(list);
|
|
|
|
list = NULL;
|
|
|
|
}
|