diff options
author | Andrei Karas <akaras@inbox.ru> | 2014-05-28 20:31:09 +0300 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2014-05-28 20:31:09 +0300 |
commit | bb87f2911b63eaf80e49d689bf52ecf2042ae098 (patch) | |
tree | 0778087134ec3c5a2370d417bcadb64a1dc6a9bc /src | |
parent | 4415cb66734e67dfcdf8924d354107d27fb70fee (diff) | |
download | manaplus-bb87f2911b63eaf80e49d689bf52ecf2042ae098.tar.gz manaplus-bb87f2911b63eaf80e49d689bf52ecf2042ae098.tar.bz2 manaplus-bb87f2911b63eaf80e49d689bf52ecf2042ae098.tar.xz manaplus-bb87f2911b63eaf80e49d689bf52ecf2042ae098.zip |
Move from resourcemanager functions related to files into other files.
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile.am | 2 | ||||
-rw-r--r-- | src/being/being.cpp | 12 | ||||
-rw-r--r-- | src/client.cpp | 15 | ||||
-rw-r--r-- | src/game.cpp | 2 | ||||
-rw-r--r-- | src/gui/fonts/font.cpp | 3 | ||||
-rw-r--r-- | src/gui/theme.cpp | 8 | ||||
-rw-r--r-- | src/gui/windows/chatwindow.cpp | 3 | ||||
-rw-r--r-- | src/gui/windows/helpwindow.cpp | 3 | ||||
-rw-r--r-- | src/gui/windows/minimap.cpp | 5 | ||||
-rw-r--r-- | src/gui/windows/updaterwindow.cpp | 8 | ||||
-rw-r--r-- | src/resources/db/palettedb.cpp | 4 | ||||
-rw-r--r-- | src/resources/resourcemanager.cpp | 173 | ||||
-rw-r--r-- | src/resources/resourcemanager.h | 76 | ||||
-rw-r--r-- | src/resources/wallpaper.cpp | 10 | ||||
-rw-r--r-- | src/utils/files.cpp | 112 | ||||
-rw-r--r-- | src/utils/files.h | 16 | ||||
-rw-r--r-- | src/utils/paths.cpp | 4 | ||||
-rw-r--r-- | src/utils/physfstools.cpp | 26 | ||||
-rw-r--r-- | src/utils/physfstools.h | 2 | ||||
-rw-r--r-- | src/utils/translation/poparser.cpp | 6 | ||||
-rw-r--r-- | src/utils/translation/translationmanager.cpp | 4 | ||||
-rw-r--r-- | src/utils/xml.cpp | 5 |
22 files changed, 210 insertions, 289 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 78a9273d6..13220ba5e 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -111,6 +111,8 @@ dyecmd_SOURCES += dyetool/dyemain.cpp \ resources/spritedef.h \ resources/spritedisplay.h \ resources/spritereference.h \ + utils/files.cpp \ + utils/files.h \ utils/mkdir.cpp \ utils/mkdir.h \ utils/paths.cpp \ diff --git a/src/being/being.cpp b/src/being/being.cpp index eedb55733..e24470d1e 100644 --- a/src/being/being.cpp +++ b/src/being/being.cpp @@ -83,6 +83,7 @@ #include "gui/widgets/tabs/langtab.h" #include "utils/delete2.h" +#include "utils/files.h" #include "utils/gettext.h" #include "utils/timer.h" @@ -2932,16 +2933,14 @@ std::string Being::loadComment(const std::string &name, const int type) } str.append(stringToHexPath(name)).append("/comment.txt"); - - const ResourceManager *const resman = ResourceManager::getInstance(); - if (ResourceManager::existsLocal(str)) + if (Files::existsLocal(str)) { StringVect lines; - resman->loadTextFileLocal(str, lines); + Files::loadTextFileLocal(str, lines); if (lines.size() >= 2) return lines[1]; } - return ""; + return std::string(); } void Being::saveComment(const std::string &restrict name, @@ -2960,7 +2959,8 @@ void Being::saveComment(const std::string &restrict name, return; } dir.append(stringToHexPath(name)); - ResourceManager::saveTextFile(dir, "comment.txt", + Files::saveTextFile(dir, + "comment.txt", (name + "\n").append(comment)); } diff --git a/src/client.cpp b/src/client.cpp index 86f0d2f20..3161bae41 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -2229,13 +2229,12 @@ void Client::initUpdatesDir() replaceAll(mUpdatesDir, ":", "_"); #endif - const ResourceManager *const resman = ResourceManager::getInstance(); const std::string updateDir("/" + mUpdatesDir); // Verify that the updates directory exists. Create if necessary. - if (!resman->isDirectory(updateDir)) + if (!PhysFs::isDirectory(updateDir.c_str())) { - if (!resman->mkdir(updateDir)) + if (!PhysFs::mkdir(updateDir.c_str())) { #if defined WIN32 std::string newDir = mLocalDataDir + "\\" + mUpdatesDir; @@ -2267,10 +2266,10 @@ void Client::initUpdatesDir() } const std::string updateLocal = updateDir + "/local"; const std::string updateFix = updateDir + "/fix"; - if (!resman->isDirectory(updateLocal)) - resman->mkdir(updateLocal); - if (!resman->isDirectory(updateFix)) - resman->mkdir(updateFix); + if (!PhysFs::isDirectory(updateLocal.c_str())) + PhysFs::mkdir(updateLocal.c_str()); + if (!PhysFs::isDirectory(updateFix.c_str())) + PhysFs::mkdir(updateFix.c_str()); } void Client::initScreenshotDir() @@ -2983,7 +2982,7 @@ void Client::setIcon() #else iconFile.append(".png"); #endif - iconFile = ResourceManager::getPath(iconFile); + iconFile = Files::getPath(iconFile); logger->log("Loading icon from file: %s", iconFile.c_str()); #ifdef WIN32 diff --git a/src/game.cpp b/src/game.cpp index 20fc2d090..67b887022 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -1030,7 +1030,7 @@ void Game::changeMap(const std::string &mapPath) std::string realFullMap = paths.getValue("maps", "maps/").append( MapDB::getMapName(mMapName)).append(".tmx"); - if (!resman->exists(realFullMap)) + if (!PhysFs::exists(realFullMap.c_str())) realFullMap.append(".gz"); // Attempt to load the new map diff --git a/src/gui/fonts/font.cpp b/src/gui/fonts/font.cpp index 72a047e57..68127232d 100644 --- a/src/gui/fonts/font.cpp +++ b/src/gui/fonts/font.cpp @@ -76,6 +76,7 @@ #include "resources/imagehelper.h" #include "resources/resourcemanager.h" +#include "utils/files.h" #include "utils/paths.h" #include "utils/sdlcheckutils.h" #include "utils/stringutils.h" @@ -162,7 +163,7 @@ TTF_Font *Font::openFont(const char *const name, const int size) // return nullptr; // return TTF_OpenFontIndexRW(rw, 1, size, 0); // #else - return TTF_OpenFontIndex(ResourceManager::getPath(name).c_str(), + return TTF_OpenFontIndex(Files::getPath(name).c_str(), size, 0); // #endif } diff --git a/src/gui/theme.cpp b/src/gui/theme.cpp index 4708a48d0..1d2c1d2b2 100644 --- a/src/gui/theme.cpp +++ b/src/gui/theme.cpp @@ -52,14 +52,18 @@ Theme *theme = nullptr; // Set the theme path... static void initDefaultThemePath() { - const ResourceManager *const resman = ResourceManager::getInstance(); defaultThemePath = branding.getStringValue("guiThemePath"); logger->log("defaultThemePath: " + defaultThemePath); - if (!defaultThemePath.empty() && resman->isDirectory(defaultThemePath)) + if (!defaultThemePath.empty() && PhysFs::isDirectory( + defaultThemePath.c_str())) + { return; + } else + { defaultThemePath = "themes/"; + } } Theme::Theme() : diff --git a/src/gui/windows/chatwindow.cpp b/src/gui/windows/chatwindow.cpp index 0ec192a89..fb7219ed9 100644 --- a/src/gui/windows/chatwindow.cpp +++ b/src/gui/windows/chatwindow.cpp @@ -73,6 +73,7 @@ #include "utils/copynpaste.h" #include "utils/delete2.h" +#include "utils/files.h" #include "utils/gettext.h" #include "resources/resourcemanager.h" @@ -219,7 +220,7 @@ void ChatWindow::postInit() void ChatWindow::loadCommandsFile(const std::string &name) { StringVect list; - ResourceManager::loadTextFile(name, list); + Files::loadTextFile(name, list); StringVectCIter it = list.begin(); const StringVectCIter it_end = list.end(); diff --git a/src/gui/windows/helpwindow.cpp b/src/gui/windows/helpwindow.cpp index 934e01359..e10900908 100644 --- a/src/gui/windows/helpwindow.cpp +++ b/src/gui/windows/helpwindow.cpp @@ -38,6 +38,7 @@ #include "resources/resourcemanager.h" #include "utils/gettext.h" +#include "utils/files.h" #include "utils/paths.h" #include "utils/process.h" @@ -153,7 +154,7 @@ void HelpWindow::loadTags() if (helpPath.empty()) helpPath = paths.getStringValue("help"); StringVect lines; - ResourceManager::loadTextFile(helpPath.append("tags.idx"), lines); + Files::loadTextFile(helpPath.append("tags.idx"), lines); FOR_EACH (StringVectCIter, it, lines) { const std::string &str = *it; diff --git a/src/gui/windows/minimap.cpp b/src/gui/windows/minimap.cpp index 3607d9584..5df1b60ce 100644 --- a/src/gui/windows/minimap.cpp +++ b/src/gui/windows/minimap.cpp @@ -44,6 +44,7 @@ #include "utils/delete2.h" #include "utils/gettext.h" +#include "utils/physfstools.h" #include "utils/sdlcheckutils.h" #include "debug.h" @@ -169,14 +170,14 @@ void Minimap::setMap(const Map *const map) std::string minimapName = map->getProperty("minimap"); - if (minimapName.empty() && resman->exists(tempname)) + if (minimapName.empty() && PhysFs::exists(tempname.c_str())) minimapName = tempname; if (minimapName.empty()) { tempname = std::string("graphics/minimaps/").append( map->getFilename()).append(".png"); - if (resman->exists(tempname)) + if (PhysFs::exists(tempname.c_str())) minimapName = tempname; } diff --git a/src/gui/windows/updaterwindow.cpp b/src/gui/windows/updaterwindow.cpp index 45c18b21b..49b2e986f 100644 --- a/src/gui/windows/updaterwindow.cpp +++ b/src/gui/windows/updaterwindow.cpp @@ -46,6 +46,8 @@ #include "resources/db/moddb.h" #include "utils/delete2.h" +#include "utils/files.h" +#include "utils/physfstools.h" #include "utils/gettext.h" #include "utils/mkdir.h" #include "utils/paths.h" @@ -1020,8 +1022,7 @@ bool UpdaterWindow::validateFile(const std::string &filePath, unsigned long UpdaterWindow::getFileHash(const std::string &filePath) { int size = 0; - char *const buf = static_cast<char*>(ResourceManager::loadFile( - filePath, size)); + char *const buf = static_cast<char*>(PhysFs::loadFile(filePath, size)); if (!buf) return 0; return Net::Download::adlerBuffer(buf, size); @@ -1042,8 +1043,7 @@ void UpdaterWindow::loadFile(std::string file) trim(file); StringVect lines; - const ResourceManager *const resman = ResourceManager::getInstance(); - resman->loadTextFileLocal(mUpdatesDir + "/local/help/news.txt", lines); + Files::loadTextFileLocal(mUpdatesDir + "/local/help/news.txt", lines); for (size_t i = 0, sz = lines.size(); i < sz; ++i) mBrowserBox->addRow(lines[i]); diff --git a/src/resources/db/palettedb.cpp b/src/resources/db/palettedb.cpp index 09140d55b..1d940f0c1 100644 --- a/src/resources/db/palettedb.cpp +++ b/src/resources/db/palettedb.cpp @@ -22,6 +22,8 @@ #include "logger.h" +#include "utils/files.h" + #include "resources/dyecolor.h" #include "resources/resourcemanager.h" @@ -48,7 +50,7 @@ void PaletteDB::loadPalette() { mLoaded = true; StringVect lines; - ResourceManager::loadTextFile("palette.gpl", lines); + Files::loadTextFile("palette.gpl", lines); StringVectCIter it = lines.begin(); if (it == lines.end()) { diff --git a/src/resources/resourcemanager.cpp b/src/resources/resourcemanager.cpp index 476149eb3..707834d1d 100644 --- a/src/resources/resourcemanager.cpp +++ b/src/resources/resourcemanager.cpp @@ -48,8 +48,6 @@ #include "utils/timer.h" #include <SDL_image.h> -#include <dirent.h> -#include <fstream> #include <sys/time.h> @@ -391,52 +389,6 @@ void ResourceManager::searchAndRemoveArchives(const std::string &restrict path, PhysFs::freeList(list); } -bool ResourceManager::mkdir(const std::string &path) const -{ - return static_cast<bool>(PhysFs::mkdir(path.c_str())); -} - -bool ResourceManager::exists(const std::string &path) const -{ - return PhysFs::exists(path.c_str()); -} - -bool ResourceManager::existsLocal(const std::string &path) -{ - bool flg(false); - std::fstream file; - file.open(path.c_str(), std::ios::in); - if (file.is_open()) - flg = true; - file.close(); - return flg; -} - -bool ResourceManager::isDirectory(const std::string &path) const -{ - return PhysFs::isDirectory(path.c_str()); -} - -std::string ResourceManager::getPath(const std::string &file) -{ - // get the real path to the file - const char *const tmp = PhysFs::getRealDir(file.c_str()); - std::string path; - - // if the file is not in the search path, then its nullptr - if (tmp) - { - path = std::string(tmp).append(dirSeparator).append(file); - } - else - { - // if not found in search path return the default path - path = getPackageDir().append(dirSeparator).append(file); - } - - return path; -} - bool ResourceManager::addResource(const std::string &idPath, Resource *const resource) { @@ -958,115 +910,6 @@ void ResourceManager::deleteInstance() delete2(instance); } -void *ResourceManager::loadFile(const std::string &fileName, int &fileSize) -{ - // Attempt to open the specified file using PhysicsFS - PHYSFS_file *const file = PhysFs::openRead(fileName.c_str()); - - if (!file) - { - logger->log("Warning: Failed to load %s: %s", - fileName.c_str(), PHYSFS_getLastError()); - return nullptr; - } - - logger->log("Loaded %s/%s", PhysFs::getRealDir(fileName.c_str()), - fileName.c_str()); - - fileSize = static_cast<int>(PHYSFS_fileLength(file)); - // Allocate memory and load the file - void *const buffer = calloc(fileSize, 1); - PHYSFS_read(file, buffer, 1, fileSize); - PHYSFS_close(file); - - return buffer; -} - -bool ResourceManager::copyFile(const std::string &restrict src, - const std::string &restrict dst) -{ - PHYSFS_file *const srcFile = PhysFs::openRead(src.c_str()); - if (!srcFile) - { - logger->log("Read error: %s", PHYSFS_getLastError()); - return false; - } - PHYSFS_file *const dstFile = PhysFs::openWrite(dst.c_str()); - if (!dstFile) - { - logger->log("Write error: %s", PHYSFS_getLastError()); - PHYSFS_close(srcFile); - return false; - } - - const int fileSize = static_cast<const int>(PHYSFS_fileLength(srcFile)); - char *buf = new char[static_cast<size_t>(fileSize)]; - PHYSFS_read(srcFile, buf, 1, fileSize); - PHYSFS_write(dstFile, buf, 1, fileSize); - - PHYSFS_close(srcFile); - PHYSFS_close(dstFile); - delete [] buf; - return true; -} - -bool ResourceManager::loadTextFile(const std::string &fileName, - StringVect &lines) -{ - int contentsLength; - char *fileContents = static_cast<char*>( - loadFile(fileName, contentsLength)); - - if (!fileContents) - { - logger->log("Couldn't load text file: %s", fileName.c_str()); - return false; - } - - std::istringstream iss(std::string(fileContents, contentsLength)); - std::string line; - - while (getline(iss, line)) - lines.push_back(line); - - free(fileContents); - return true; -} - -bool ResourceManager::loadTextFileLocal(const std::string &fileName, - StringVect &lines) -{ - std::ifstream file; - char line[501]; - - file.open(fileName.c_str(), std::ios::in); - - if (!file.is_open()) - { - logger->log("Couldn't load text file: %s", fileName.c_str()); - return false; - } - - while (file.getline(line, 500)) - lines.push_back(line); - - return true; -} - -void ResourceManager::saveTextFile(std::string path, - const std::string &restrict name, - const std::string &restrict text) -{ - if (!mkdir_r(path.c_str())) - { - std::ofstream file; - file.open((path.append("/").append(name)).c_str(), std::ios::out); - if (file.is_open()) - file << text << std::endl; - file.close(); - } -} - SDL_Surface *ResourceManager::loadSDLSurface(const std::string &filename) const { if (SDL_RWops *const rw = MPHYSFSRWOPS_openRead(filename.c_str())) @@ -1133,22 +976,6 @@ Image *ResourceManager::getRescaled(const Image *const image, return img; } -void ResourceManager::deleteFilesInDirectory(std::string path) -{ - path += "/"; - struct dirent *next_file = nullptr; - DIR *const dir = opendir(path.c_str()); - - while ((next_file = readdir(dir))) - { - const std::string file = next_file->d_name; - if (file != "." && file != "..") - remove((path + file).c_str()); - } - if (dir) - closedir(dir); -} - void ResourceManager::clearCache() { cleanProtected(); diff --git a/src/resources/resourcemanager.h b/src/resources/resourcemanager.h index 0e14e61bb..824fbddd5 100644 --- a/src/resources/resourcemanager.h +++ b/src/resources/resourcemanager.h @@ -106,36 +106,6 @@ class ResourceManager final const std::string &restrict ext) const; /** - * Creates a directory in the write path - */ - bool mkdir(const std::string &path) const; - - /** - * Checks whether the given file or directory exists in the search path - * (PhysFS) - */ - bool exists(const std::string &path) const A_WARN_UNUSED; - - /** - * Checks whether the given file or directory exists - */ - static bool existsLocal(const std::string &path) A_WARN_UNUSED; - - /** - * Checks whether the given path is a directory. - */ - bool isDirectory(const std::string &path) const A_WARN_UNUSED; - - /** - * Returns the real path to a file. Note that this method will always - * return a path, it does not check whether the file exists. - * - * @param file The file to get the real path to. - * @return The real path. - */ - static std::string getPath(const std::string &file) A_WARN_UNUSED; - - /** * Creates a resource and adds it to the resource map. * * @param idPath The resource identifier path. @@ -173,18 +143,6 @@ class ResourceManager final bool addResource(const std::string &idPath, Resource *const resource); /** - * Copies a file from one place to another (useful for extracting - * raw files from a zip archive, for example) - * - * @param src Source file name - * @param dst Destination file name - * @return true on success, false on failure. An error message should be - * in the log file. - */ - static bool copyFile(const std::string &restrict src, - const std::string &restrict dst); - - /** * Convenience wrapper around ResourceManager::get for loading * images. */ @@ -246,37 +204,9 @@ class ResourceManager final */ void moveToDeleted(Resource *const res); - /** - * Allocates data into a buffer pointer for raw data loading. The - * returned data is expected to be freed using <code>free()</code>. - * - * @param fileName The name of the file to be loaded. - * @param fileSize The size of the file that was loaded. - * - * @return An allocated byte array containing the data that was loaded, - * or <code>NULL</code> on fail. - */ - static void *loadFile(const std::string &fileName, - int &fileSize) A_WARN_UNUSED; - - /** - * Retrieves the contents of a text file (PhysFS). - */ - static bool loadTextFile(const std::string &fileName, - StringVect &lines); - - /** - * Retrieves the contents of a text file. - */ - static bool loadTextFileLocal(const std::string &fileName, - StringVect &lines); - - static void saveTextFile(std::string path, - const std::string &restrict name, - const std::string &restrict text); - Image *getRescaled(const Image *const image, - const int width, const int height) A_WARN_UNUSED; + const int width, + const int height) A_WARN_UNUSED; /** * Loads the given filename as an SDL surface. The returned surface is @@ -325,8 +255,6 @@ class ResourceManager final void clearCache(); - static void deleteFilesInDirectory(std::string path); - private: /** * Deletes the resource after logging a cleanup message. diff --git a/src/resources/wallpaper.cpp b/src/resources/wallpaper.cpp index 4674035cf..979cf9844 100644 --- a/src/resources/wallpaper.cpp +++ b/src/resources/wallpaper.cpp @@ -46,26 +46,24 @@ static std::string wallpaperFile; // Search for the wallpaper path values sequentially.. static void initDefaultWallpaperPaths() { - const ResourceManager *const resman = ResourceManager::getInstance(); - // Init the path wallpaperPath = branding.getStringValue("wallpapersPath"); - if (wallpaperPath.empty() || !resman->isDirectory(wallpaperPath)) + if (wallpaperPath.empty() || !PhysFs::isDirectory(wallpaperPath.c_str())) wallpaperPath = paths.getValue("wallpapers", ""); - if (wallpaperPath.empty() || !resman->isDirectory(wallpaperPath)) + if (wallpaperPath.empty() || !PhysFs::isDirectory(wallpaperPath.c_str())) wallpaperPath = "graphics/images/"; // Init the default file wallpaperFile = branding.getStringValue("wallpaperFile"); - if (!wallpaperFile.empty() && !resman->isDirectory(wallpaperFile)) + if (!wallpaperFile.empty() && !PhysFs::isDirectory(wallpaperFile.c_str())) return; else wallpaperFile = paths.getValue("wallpaperFile", ""); - if (wallpaperFile.empty() || resman->isDirectory(wallpaperFile)) + if (wallpaperFile.empty() || PhysFs::isDirectory(wallpaperFile.c_str())) wallpaperFile = "login_wallpaper.png"; } diff --git a/src/utils/files.cpp b/src/utils/files.cpp index 5cf97ddba..28921a0e5 100644 --- a/src/utils/files.cpp +++ b/src/utils/files.cpp @@ -20,14 +20,22 @@ #include "utils/files.h" +#include "logger.h" + #if defined(ANDROID) || defined(__native_client__) #include "resources/resourcemanager.h" #include "utils/mkdir.h" #endif +#include "utils/mkdir.h" +#include "utils/paths.h" #include "utils/physfstools.h" +#include <dirent.h> +#include <fstream> +#include <sstream> + #include "debug.h" #ifdef ANDROID @@ -230,3 +238,107 @@ void Files::getFilesWithDir(const std::string &path, StringVect &list) } PhysFs::freeList(fonts); } + +bool Files::existsLocal(const std::string &path) +{ + bool flg(false); + std::fstream file; + file.open(path.c_str(), std::ios::in); + if (file.is_open()) + flg = true; + file.close(); + return flg; +} + +std::string Files::getPath(const std::string &file) +{ + // get the real path to the file + const char *const tmp = PhysFs::getRealDir(file.c_str()); + std::string path; + + // if the file is not in the search path, then its nullptr + if (tmp) + { + path = std::string(tmp).append(dirSeparator).append(file); + } + else + { + // if not found in search path return the default path + path = getPackageDir().append(dirSeparator).append(file); + } + + return path; +} + +bool Files::loadTextFile(const std::string &fileName, + StringVect &lines) +{ + int contentsLength; + char *fileContents = static_cast<char*>( + PhysFs::loadFile(fileName, contentsLength)); + + if (!fileContents) + { + logger->log("Couldn't load text file: %s", fileName.c_str()); + return false; + } + + std::istringstream iss(std::string(fileContents, contentsLength)); + std::string line; + + while (getline(iss, line)) + lines.push_back(line); + + free(fileContents); + return true; +} + +bool Files::loadTextFileLocal(const std::string &fileName, + StringVect &lines) +{ + std::ifstream file; + char line[501]; + + file.open(fileName.c_str(), std::ios::in); + + if (!file.is_open()) + { + logger->log("Couldn't load text file: %s", fileName.c_str()); + return false; + } + + while (file.getline(line, 500)) + lines.push_back(line); + + return true; +} + +void Files::saveTextFile(std::string path, + const std::string &restrict name, + const std::string &restrict text) +{ + if (!mkdir_r(path.c_str())) + { + std::ofstream file; + file.open((path.append("/").append(name)).c_str(), std::ios::out); + if (file.is_open()) + file << text << std::endl; + file.close(); + } +} + +void Files::deleteFilesInDirectory(std::string path) +{ + path += "/"; + struct dirent *next_file = nullptr; + DIR *const dir = opendir(path.c_str()); + + while ((next_file = readdir(dir))) + { + const std::string file = next_file->d_name; + if (file != "." && file != "..") + remove((path + file).c_str()); + } + if (dir) + closedir(dir); +} diff --git a/src/utils/files.h b/src/utils/files.h index 2046b8ac9..1db9993a7 100644 --- a/src/utils/files.h +++ b/src/utils/files.h @@ -61,6 +61,22 @@ namespace Files void getFilesWithDir(const std::string &restrict path, StringVect &restrict list); + + bool existsLocal(const std::string &path); + + std::string getPath(const std::string &file); + + bool loadTextFile(const std::string &fileName, + StringVect &lines); + + bool loadTextFileLocal(const std::string &fileName, + StringVect &lines); + + void saveTextFile(std::string path, + const std::string &restrict name, + const std::string &restrict text); + + void deleteFilesInDirectory(std::string path); } // namespace Files #endif // UTILS_FILES_H diff --git a/src/utils/paths.cpp b/src/utils/paths.cpp index 96cd0ed29..ae576d1e4 100644 --- a/src/utils/paths.cpp +++ b/src/utils/paths.cpp @@ -25,6 +25,8 @@ #endif #include "utils/paths.h" + +#include "utils/files.h" #include "utils/physfstools.h" #include "utils/stringutils.h" @@ -174,7 +176,7 @@ std::string getPicturesDir() } StringVect arr; - ResourceManager::loadTextFileLocal(file, arr); + Files::loadTextFileLocal(file, arr); FOR_EACH (StringVectCIter, it, arr) { std::string str = *it; diff --git a/src/utils/physfstools.cpp b/src/utils/physfstools.cpp index 58be77f0e..1ab1aaa25 100644 --- a/src/utils/physfstools.cpp +++ b/src/utils/physfstools.cpp @@ -20,6 +20,8 @@ #include "utils/physfstools.h" +#include "logger.h" + #include <iostream> #include <unistd.h> @@ -134,4 +136,28 @@ namespace PhysFs { return PHYSFS_mkdir(dirname); } + + void *loadFile(const std::string &fileName, int &fileSize) + { + // Attempt to open the specified file using PhysicsFS + PHYSFS_file *const file = PhysFs::openRead(fileName.c_str()); + + if (!file) + { + logger->log("Warning: Failed to load %s: %s", + fileName.c_str(), PHYSFS_getLastError()); + return nullptr; + } + + logger->log("Loaded %s/%s", PhysFs::getRealDir(fileName.c_str()), + fileName.c_str()); + + fileSize = static_cast<int>(PHYSFS_fileLength(file)); + // Allocate memory and load the file + void *const buffer = calloc(fileSize, 1); + PHYSFS_read(file, buffer, 1, fileSize); + PHYSFS_close(file); + + return buffer; + } } // namespace PhysFs diff --git a/src/utils/physfstools.h b/src/utils/physfstools.h index 3667f7a16..f85dabdd6 100644 --- a/src/utils/physfstools.h +++ b/src/utils/physfstools.h @@ -22,6 +22,7 @@ #define UTILS_PHYSFSTOOLS_H #include <physfs.h> +#include <string> namespace PhysFs { @@ -42,6 +43,7 @@ namespace PhysFs bool removeFromSearchPath(const char *const oldDir); const char *getRealDir(const char *const filename); bool mkdir(const char *const dirName); + void *loadFile(const std::string &fileName, int &fileSize); } // namespace PhysFs extern const char *dirSeparator; diff --git a/src/utils/translation/poparser.cpp b/src/utils/translation/poparser.cpp index bcb577f84..7e78ade9d 100644 --- a/src/utils/translation/poparser.cpp +++ b/src/utils/translation/poparser.cpp @@ -22,6 +22,7 @@ #include "resources/resourcemanager.h" +#include "utils/physfstools.h" #include "utils/stringutils.h" #include "utils/translation/podict.h" @@ -48,7 +49,7 @@ void PoParser::openFile(const std::string &name) if (!resman) return; int size; - char *buf = static_cast<char*>(resman->loadFile(getFileName(name), size)); + char *buf = static_cast<char*>(PhysFs::loadFile(getFileName(name), size)); if (buf) { @@ -231,8 +232,7 @@ PoDict *PoParser::getEmptyDict() bool PoParser::checkLang(const std::string &lang) { // check is po file exists - const ResourceManager *const resman = ResourceManager::getInstance(); - return resman->exists(getFileName(lang)); + return PhysFs::exists(getFileName(lang).c_str()); } std::string PoParser::getFileName(const std::string &lang) diff --git a/src/utils/translation/translationmanager.cpp b/src/utils/translation/translationmanager.cpp index 9bbdec810..5fddee639 100644 --- a/src/utils/translation/translationmanager.cpp +++ b/src/utils/translation/translationmanager.cpp @@ -21,6 +21,7 @@ #include "utils/translation/translationmanager.h" #include "utils/delete2.h" +#include "utils/physfstools.h" #include "utils/stringutils.h" #include "utils/translation/podict.h" @@ -85,9 +86,8 @@ bool TranslationManager::translateFile(const std::string &fileName, return false; int contentsLength; - const ResourceManager *const resman = ResourceManager::getInstance(); char *fileContents = static_cast<char*>( - resman->loadFile(fileName, contentsLength)); + PhysFs::loadFile(fileName, contentsLength)); if (!fileContents) { diff --git a/src/utils/xml.cpp b/src/utils/xml.cpp index 099457af3..3144a03b6 100644 --- a/src/utils/xml.cpp +++ b/src/utils/xml.cpp @@ -27,6 +27,7 @@ #include "resources/resourcemanager.h" #include "utils/fuzzer.h" +#include "utils/physfstools.h" #include "utils/stringutils.h" #include "utils/translation/podict.h" @@ -91,9 +92,7 @@ namespace XML valid = true; if (useResman) { - const ResourceManager *const resman - = ResourceManager::getInstance(); - data = static_cast<char*>(resman->loadFile( + data = static_cast<char*>(PhysFs::loadFile( filename.c_str(), size)); } else |