+static void init_physfs(const char* argv0)
+{
+ if(!PHYSFS_init(argv0)) {
+ std::stringstream msg;
+ msg << "Couldn't initialize physfs: " << PHYSFS_getLastError();
+ throw std::runtime_error(msg.str());
+ }
+
+ // Initialize physfs (this is a slightly modified version of
+ // PHYSFS_setSaneConfig
+ const char* application = "supertux2"; //instead of PACKAGE_NAME so we can coexist with MS1
+ const char* userdir = PHYSFS_getUserDir();
+ const char* dirsep = PHYSFS_getDirSeparator();
+ char* writedir = new char[strlen(userdir) + strlen(application) + 2];
+
+ // Set configuration directory
+ sprintf(writedir, "%s.%s", userdir, application);
+ if(!PHYSFS_setWriteDir(writedir)) {
+ // try to create the directory
+ char* mkdir = new char[strlen(application) + 2];
+ sprintf(mkdir, ".%s", application);
+ if(!PHYSFS_setWriteDir(userdir) || !PHYSFS_mkdir(mkdir)) {
+ std::ostringstream msg;
+ msg << "Failed creating configuration directory '"
+ << writedir << "': " << PHYSFS_getLastError();
+ delete[] writedir;
+ delete[] mkdir;
+ throw std::runtime_error(msg.str());
+ }
+ delete[] mkdir;
+
+ if(!PHYSFS_setWriteDir(writedir)) {
+ std::ostringstream msg;
+ msg << "Failed to use configuration directory '"
+ << writedir << "': " << PHYSFS_getLastError();
+ delete[] writedir;
+ throw std::runtime_error(msg.str());
+ }
+ }
+ PHYSFS_addToSearchPath(writedir, 0);
+ delete[] writedir;
+
+ // Search for archives and add them to the search path
+ const char* archiveExt = "zip";
+ char** rc = PHYSFS_enumerateFiles("/");
+ size_t extlen = strlen(archiveExt);
+
+ for(char** i = rc; *i != 0; ++i) {
+ size_t l = strlen(*i);
+ if((l > extlen) && ((*i)[l - extlen - 1] == '.')) {
+ const char* ext = (*i) + (l - extlen);
+ if(strcasecmp(ext, archiveExt) == 0) {
+ const char* d = PHYSFS_getRealDir(*i);
+ char* str = new char[strlen(d) + strlen(dirsep) + l + 1];
+ sprintf(str, "%s%s%s", d, dirsep, *i);
+ PHYSFS_addToSearchPath(str, 1);
+ delete[] str;
+ }
+ }
+ }