bool setSaveDir(const char* dirName) noexcept { #ifdef __ANDROID__ auto userDir = "data/data/com.dgengine/files/"; #else auto userDir = PHYSFS_getPrefDir("DGEngine", dirName); #endif if (userDir == nullptr) { return false; } return PHYSFS_setWriteDir(userDir) != 0; }
void Filesystem::init(const char *argv0) { if (!PHYSFS_isInit()) { int success = PHYSFS_init(argv0); if (!success) { throw_error; } const std::string basedir(PHYSFS_getBaseDir()); if (basedir.substr(basedir.length() - 5, 5) == ".app/") { // smells like a Mac OS X application bundle std::string resource_path = basedir + "Contents/Resources"; PHYSFS_mount(resource_path.c_str(), NULL, 0); } else { // er, uh... search in the base directory? PHYSFS_mount(basedir.c_str(), NULL, 0); } // get a platform-specific sensible directory for the app #ifdef __linux__ const char * prefdir = my_pref_dir("raceintospace.org", "Race Into Space"); #else const char *prefdir = PHYSFS_getPrefDir("raceintospace.org", "Race Into Space"); #endif if (prefdir == NULL) { throw_error; } // use this for reading, *before* the expected game data directory, thereby allowing overlays success = PHYSFS_mount(prefdir, NULL, 0); if (!success) { throw_error; } // use this for writing too PHYSFS_setWriteDir(prefdir); if (!success) { throw_error; } } }
FileSystem::FileSystem(std::vector<UString> paths) { // FIXME: Is this the right thing to do that? LogInfo("Registering external archivers..."); PHYSFS_registerArchiver(getCueArchiver()); // Paths are supplied in inverse-search order (IE the last in 'paths' should be the first // searched) for (auto &p : paths) { if (!PHYSFS_mount(p.cStr(), "/", 0)) { LogInfo("Failed to add resource dir \"%s\", error: %s", p, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())); continue; } else LogInfo("Resource dir \"%s\" mounted to \"%s\"", p, PHYSFS_getMountPoint(p.cStr())); } this->writeDir = PHYSFS_getPrefDir(PROGRAM_ORGANISATION, PROGRAM_NAME); LogInfo("Setting write directory to \"%s\"", this->writeDir); PHYSFS_setWriteDir(this->writeDir.cStr()); // Finally, the write directory trumps all PHYSFS_mount(this->writeDir.cStr(), "/", 0); }
void find_userdir() const { std::string userdir; if (m_forced_userdir) { userdir = *m_forced_userdir; } else if (const char* env_userdir = getenv("SUPERTUX2_USER_DIR")) { userdir = env_userdir; } else { userdir = PHYSFS_getPrefDir("SuperTux","supertux2"); } //Kept for backwards-compatability only, hence the silence #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wdeprecated-declarations" std::string physfs_userdir = PHYSFS_getUserDir(); #pragma GCC diagnostic pop #ifdef _WIN32 std::string olduserdir = FileSystem::join(physfs_userdir, PACKAGE_NAME); #else std::string olduserdir = FileSystem::join(physfs_userdir, "." PACKAGE_NAME); #endif if (FileSystem::is_directory(olduserdir)) { boost::filesystem::path olduserpath(olduserdir); boost::filesystem::path userpath(userdir); boost::filesystem::directory_iterator end_itr; bool success = true; // cycle through the directory for (boost::filesystem::directory_iterator itr(olduserpath); itr != end_itr; ++itr) { try { boost::filesystem::rename(itr->path().string().c_str(), userpath / itr->path().filename()); } catch (const boost::filesystem::filesystem_error& err) { success = false; log_warning << "Failed to move contents of config directory: " << err.what() << std::endl; } } if (success) { try { boost::filesystem::remove_all(olduserpath); } catch (const boost::filesystem::filesystem_error& err) { success = false; log_warning << "Failed to remove old config directory: " << err.what(); } } if (success) { log_info << "Moved old config dir " << olduserdir << " to " << userdir << std::endl; } } if (!FileSystem::is_directory(userdir)) { FileSystem::mkdir(userdir); log_info << "Created SuperTux userdir: " << userdir << std::endl; } if (!PHYSFS_setWriteDir(userdir.c_str())) { std::ostringstream msg; msg << "Failed to use userdir directory '" << userdir << "': " << PHYSFS_getLastErrorCode(); throw std::runtime_error(msg.str()); } PHYSFS_mount(userdir.c_str(), NULL, 0); }
Data::Data(Framework &fw, std::vector<UString> paths, int imageCacheSize, int imageSetCacheSize, int voxelCacheSize) { for (auto &imageBackend : *registeredImageBackends) { auto t = imageBackend.first; ImageLoader *l = imageBackend.second->create(); if (l) { this->imageLoaders.emplace_back(l); LogInfo("Initialised image loader %s", t.c_str()); } else LogWarning("Failed to load image loader %s", t.c_str()); } for (auto &sampleBackend : *registeredSampleLoaders) { auto t = sampleBackend.first; SampleLoader *s = sampleBackend.second->create(fw); if (s) { this->sampleLoaders.emplace_back(s); LogInfo("Initialised sample loader %s", t.c_str()); } else LogWarning("Failed to load sample loader %s", t.c_str()); } for (auto &musicLoader : *registeredMusicLoaders) { auto t = musicLoader.first; MusicLoader *m = musicLoader.second->create(fw); if (m) { this->musicLoaders.emplace_back(m); LogInfo("Initialised music loader %s", t.c_str()); } else LogWarning("Failed to load music loader %s", t.c_str()); } this->writeDir = PHYSFS_getPrefDir(PROGRAM_ORGANISATION, PROGRAM_NAME); LogInfo("Setting write directory to \"%s\"", this->writeDir.c_str()); PHYSFS_setWriteDir(this->writeDir.c_str()); for (int i = 0; i < imageCacheSize; i++) pinnedImages.push(nullptr); for (int i = 0; i < imageSetCacheSize; i++) pinnedImageSets.push(nullptr); for (int i = 0; i < voxelCacheSize; i++) pinnedLOFVoxels.push(nullptr); // Paths are supplied in inverse-search order (IE the last in 'paths' should be the first // searched) for (auto &p : paths) { if (!PHYSFS_mount(p.c_str(), "/", 0)) { LogWarning("Failed to add resource dir \"%s\"", p.c_str()); continue; } else LogInfo("Resource dir \"%s\" mounted to \"%s\"", p.c_str(), PHYSFS_getMountPoint(p.c_str())); } // Finally, the write directory trumps all PHYSFS_mount(this->writeDir.c_str(), "/", 0); }