void ResourceManager::searchAndAddArchives(const std::string &path, const std::string &ext, bool append) { const char *dirSep = PHYSFS_getDirSeparator(); char **list = PHYSFS_enumerateFiles(path.c_str()); for (char **i = list; *i != NULL; i++) { size_t len = strlen(*i); if (len > ext.length() && !ext.compare((*i)+(len - ext.length()))) { std::string file, realPath, archive; file = path + (*i); realPath = std::string(PHYSFS_getRealDir(file.c_str())); archive = realPath + dirSep + file; addToSearchPath(archive, append); } } PHYSFS_freeList(list); }
bool ResourceManager::setupWriteDir(const std::string& appWriteDirName) { std::string userDir = PHYSFS_getUserDir(); std::string dirName; #ifndef WIN32 dirName = stdext::format(".%s", appWriteDirName); #else dirName = appWriteDirName; #endif std::string writeDir = userDir + dirName; if(!PHYSFS_setWriteDir(writeDir.c_str())) { if(!PHYSFS_setWriteDir(userDir.c_str())) { g_logger.error("User directory not found."); return false; } if(!PHYSFS_mkdir(dirName.c_str())) { g_logger.error("Cannot create directory for saving configurations."); return false; } if(!PHYSFS_setWriteDir(writeDir.c_str())) { g_logger.error("Unable to set write directory."); return false; } } addToSearchPath(writeDir, true); //g_logger.debug(stdext::format("Setup write dir %s", writeDir)); return true; }
void FileSystem::setWriteDir(const std::string& dirname) { if( !PHYSFS_setWriteDir(dirname.c_str()) ) { BOOST_THROW_EXCEPTION( PhysfsException() ); }; addToSearchPath(dirname, false); }
void ResourceManager::searchAndAddPackages(const std::string& packagesDir, const std::string& packageExt, bool append) { auto files = listDirectoryFiles(resolvePath(packagesDir)); for(const std::string& file : files) { if(boost::ends_with(file, packageExt)) addToSearchPath(packagesDir + "/" + file, !append); } }
bool Resources::Open(PString & argv0, PString & application) { if(!PHYSFS_init(argv0.GetPointer())) { PError << "failure while initialising physfs: " << PHYSFS_getLastError() << endl; return PFalse; } else { PTRACE(5, "successful initialize physfs"); } const char* basedir = PHYSFS_getBaseDir(); const char* userdir = PHYSFS_getUserDir(); const char* dirsep = PHYSFS_getDirSeparator(); char* writedir = new char[strlen(userdir) + application.GetLength() + 2]; sprintf(writedir, "%s.%s", userdir, application.GetPointer()); PTRACE(5, "physfs base directory: " << basedir); PTRACE(5, "physfs user directory: " << userdir); PTRACE(5, "physfs write directory: " << writedir); if(!PHYSFS_setWriteDir(writedir)) { // try to create the directory... char* mkdir = new char[application.GetLength()+2]; sprintf(mkdir, ".%s", application.GetPointer()); if(!PHYSFS_setWriteDir(userdir) || ! PHYSFS_mkdir(mkdir)) { delete[] writedir; delete[] mkdir; PError << "failed creating configuration directory: '" << writedir << "': " << PHYSFS_getLastError() << endl; return PFalse; } delete[] mkdir; if (!PHYSFS_setWriteDir(writedir)) { PError << "couldn't set configuration directory to '" << writedir << "': " << PHYSFS_getLastError() << endl; return PFalse; } } PHYSFS_addToSearchPath(writedir, 0); PHYSFS_addToSearchPath(basedir, 1); delete[] writedir; /* Root out archives, and add them to search path... */ if (resourceExt != NULL) { char **rc = PHYSFS_enumerateFiles("/"); char **i; size_t extlen = strlen(resourceExt); char *ext; for (i = rc; *i != NULL; i++) { size_t l = strlen(*i); if ((l > extlen) && ((*i)[l - extlen - 1] == '.')) { ext = (*i) + (l - extlen); if (strcasecmp(ext, resourceExt) == 0) { PTRACE(5, "Add resource '" << *i << "' to search path"); const char *d = PHYSFS_getRealDir(*i); char* str = new char[strlen(d) + strlen(dirsep) + l + 1]; sprintf(str, "%s%s%s", d, dirsep, *i); addToSearchPath(str, 1); delete[] str; }; }; }; PHYSFS_freeList(rc); } return PTrue; }