ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

default-saves.cpp

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 // This define lets us use the system function remove() on Symbian, which
00024 // is disabled by default due to a macro conflict.
00025 // See backends/platform/symbian/src/portdefs.h .
00026 #define SYMBIAN_USE_SYSTEM_REMOVE
00027 
00028 #include "common/scummsys.h"
00029 
00030 #if defined(USE_CLOUD) && defined(USE_LIBCURL)
00031 #include "backends/cloud/cloudmanager.h"
00032 #include "common/file.h"
00033 #endif
00034 
00035 #if !defined(DISABLE_DEFAULT_SAVEFILEMANAGER)
00036 
00037 #include "backends/saves/default/default-saves.h"
00038 
00039 #include "common/savefile.h"
00040 #include "common/util.h"
00041 #include "common/fs.h"
00042 #include "common/archive.h"
00043 #include "common/config-manager.h"
00044 #include "common/zlib.h"
00045 
00046 #ifndef _WIN32_WCE
00047 #include <errno.h>  // for removeSavefile()
00048 #endif
00049 
00050 #if defined(USE_CLOUD) && defined(USE_LIBCURL)
00051 const char *DefaultSaveFileManager::TIMESTAMPS_FILENAME = "timestamps";
00052 #endif
00053 
00054 DefaultSaveFileManager::DefaultSaveFileManager() {
00055 }
00056 
00057 DefaultSaveFileManager::DefaultSaveFileManager(const Common::String &defaultSavepath) {
00058     ConfMan.registerDefault("savepath", defaultSavepath);
00059 }
00060 
00061 
00062 void DefaultSaveFileManager::checkPath(const Common::FSNode &dir) {
00063     clearError();
00064     if (!dir.exists()) {
00065         setError(Common::kPathDoesNotExist, "The savepath '"+dir.getPath()+"' does not exist");
00066     } else if (!dir.isDirectory()) {
00067         setError(Common::kPathNotDirectory, "The savepath '"+dir.getPath()+"' is not a directory");
00068     }
00069 }
00070 
00071 void DefaultSaveFileManager::updateSavefilesList(Common::StringArray &lockedFiles) {
00072     //make it refresh the cache next time it lists the saves
00073     _cachedDirectory = "";
00074 
00075     //remember the locked files list because some of these files don't exist yet
00076     _lockedFiles = lockedFiles;
00077 }
00078 
00079 Common::StringArray DefaultSaveFileManager::listSavefiles(const Common::String &pattern) {
00080     // Assure the savefile name cache is up-to-date.
00081     assureCached(getSavePath());
00082     if (getError().getCode() != Common::kNoError)
00083         return Common::StringArray();
00084 
00085     Common::HashMap<Common::String, bool> locked;
00086     for (Common::StringArray::const_iterator i = _lockedFiles.begin(), end = _lockedFiles.end(); i != end; ++i) {
00087         locked[*i] = true;
00088     }
00089 
00090     Common::StringArray results;
00091     for (SaveFileCache::const_iterator file = _saveFileCache.begin(), end = _saveFileCache.end(); file != end; ++file) {
00092         if (!locked.contains(file->_key) && file->_key.matchString(pattern, true)) {
00093             results.push_back(file->_key);
00094         }
00095     }
00096     return results;
00097 }
00098 
00099 Common::InSaveFile *DefaultSaveFileManager::openRawFile(const Common::String &filename) {
00100     // Assure the savefile name cache is up-to-date.
00101     assureCached(getSavePath());
00102     if (getError().getCode() != Common::kNoError)
00103         return nullptr;
00104 
00105     SaveFileCache::const_iterator file = _saveFileCache.find(filename);
00106     if (file == _saveFileCache.end()) {
00107         return nullptr;
00108     } else {
00109         // Open the file for loading.
00110         Common::SeekableReadStream *sf = file->_value.createReadStream();
00111         return sf;
00112     }
00113 }
00114 
00115 Common::InSaveFile *DefaultSaveFileManager::openForLoading(const Common::String &filename) {
00116     // Assure the savefile name cache is up-to-date.
00117     assureCached(getSavePath());
00118     if (getError().getCode() != Common::kNoError)
00119         return nullptr;
00120 
00121     for (Common::StringArray::const_iterator i = _lockedFiles.begin(), end = _lockedFiles.end(); i != end; ++i) {
00122         if (filename == *i) {
00123             return nullptr; //file is locked, no loading available
00124         }
00125     }
00126 
00127     SaveFileCache::const_iterator file = _saveFileCache.find(filename);
00128     if (file == _saveFileCache.end()) {
00129         return nullptr;
00130     } else {
00131         // Open the file for loading.
00132         Common::SeekableReadStream *sf = file->_value.createReadStream();
00133         return Common::wrapCompressedReadStream(sf);
00134     }
00135 }
00136 
00137 Common::OutSaveFile *DefaultSaveFileManager::openForSaving(const Common::String &filename, bool compress) {
00138     // Assure the savefile name cache is up-to-date.
00139     const Common::String savePathName = getSavePath();
00140     assureCached(savePathName);
00141     if (getError().getCode() != Common::kNoError)
00142         return nullptr;
00143 
00144     for (Common::StringArray::const_iterator i = _lockedFiles.begin(), end = _lockedFiles.end(); i != end; ++i) {
00145         if (filename == *i) {
00146             return nullptr; //file is locked, no saving available
00147         }
00148     }
00149 
00150 #if defined(USE_CLOUD) && defined(USE_LIBCURL)
00151     // Update file's timestamp
00152     Common::HashMap<Common::String, uint32> timestamps = loadTimestamps();
00153     timestamps[filename] = INVALID_TIMESTAMP;
00154     saveTimestamps(timestamps);
00155 #endif
00156 
00157     // Obtain node.
00158     SaveFileCache::const_iterator file = _saveFileCache.find(filename);
00159     Common::FSNode fileNode;
00160 
00161     // If the file did not exist before, we add it to the cache.
00162     if (file == _saveFileCache.end()) {
00163         const Common::FSNode savePath(savePathName);
00164         fileNode = savePath.getChild(filename);
00165     } else {
00166         fileNode = file->_value;
00167     }
00168 
00169     // Open the file for saving.
00170     Common::WriteStream *const sf = fileNode.createWriteStream();
00171     Common::OutSaveFile *const result = new Common::OutSaveFile(compress ? Common::wrapCompressedWriteStream(sf) : sf);
00172 
00173     // Add file to cache now that it exists.
00174     _saveFileCache[filename] = Common::FSNode(fileNode.getPath());
00175 
00176     return result;
00177 }
00178 
00179 bool DefaultSaveFileManager::removeSavefile(const Common::String &filename) {
00180     // Assure the savefile name cache is up-to-date.
00181     assureCached(getSavePath());
00182     if (getError().getCode() != Common::kNoError)
00183         return false;
00184 
00185 #if defined(USE_CLOUD) && defined(USE_LIBCURL)
00186     // Update file's timestamp
00187     Common::HashMap<Common::String, uint32> timestamps = loadTimestamps();
00188     Common::HashMap<Common::String, uint32>::iterator it = timestamps.find(filename);
00189     if (it != timestamps.end()) {
00190         timestamps.erase(it);
00191         saveTimestamps(timestamps);
00192     }
00193 #endif
00194 
00195     // Obtain node if exists.
00196     SaveFileCache::const_iterator file = _saveFileCache.find(filename);
00197     if (file == _saveFileCache.end()) {
00198         return false;
00199     } else {
00200         const Common::FSNode fileNode = file->_value;
00201         // Remove from cache, this invalidates the 'file' iterator.
00202         _saveFileCache.erase(file);
00203         file = _saveFileCache.end();
00204 
00205         // FIXME: remove does not exist on all systems. If your port fails to
00206         // compile because of this, please let us know (scummvm-devel).
00207         // There is a nicely portable workaround, too: Make this method overloadable.
00208         if (remove(fileNode.getPath().c_str()) != 0) {
00209 #ifndef _WIN32_WCE
00210             if (errno == EACCES)
00211                 setError(Common::kWritePermissionDenied, "Search or write permission denied: "+fileNode.getName());
00212 
00213             if (errno == ENOENT)
00214                 setError(Common::kPathDoesNotExist, "removeSavefile: '"+fileNode.getName()+"' does not exist or path is invalid");
00215 #endif
00216             return false;
00217         } else {
00218             return true;
00219         }
00220     }
00221 }
00222 
00223 Common::String DefaultSaveFileManager::getSavePath() const {
00224 
00225     Common::String dir;
00226 
00227     // Try to use game specific savepath from config
00228     dir = ConfMan.get("savepath");
00229 
00230     // Work around a bug (#999122) in the original 0.6.1 release of
00231     // ScummVM, which would insert a bad savepath value into config files.
00232     if (dir == "None") {
00233         ConfMan.removeKey("savepath", ConfMan.getActiveDomainName());
00234         ConfMan.flushToDisk();
00235         dir = ConfMan.get("savepath");
00236     }
00237 
00238 #ifdef _WIN32_WCE
00239     if (dir.empty())
00240         dir = ConfMan.get("path");
00241 #endif
00242 
00243     return dir;
00244 }
00245 
00246 void DefaultSaveFileManager::assureCached(const Common::String &savePathName) {
00247     // Check that path exists and is usable.
00248     checkPath(Common::FSNode(savePathName));
00249 
00250 #if defined(USE_CLOUD) && defined(USE_LIBCURL)
00251     Common::Array<Common::String> files = CloudMan.getSyncingFiles(); //returns empty array if not syncing
00252     if (!files.empty()) updateSavefilesList(files); //makes this cache invalid
00253     else _lockedFiles = files;
00254 #endif
00255 
00256     if (_cachedDirectory == savePathName) {
00257         return;
00258     }
00259 
00260     _saveFileCache.clear();
00261     _cachedDirectory.clear();
00262 
00263     if (getError().getCode() != Common::kNoError) {
00264         warning("DefaultSaveFileManager::assureCached: Can not cache path '%s': '%s'", savePathName.c_str(), getErrorDesc().c_str());
00265         return;
00266     }
00267 
00268     // FSNode can cache its members, thus create it after checkPath to reflect
00269     // actual file system state.
00270     const Common::FSNode savePath(savePathName);
00271 
00272     Common::FSList children;
00273     if (!savePath.getChildren(children, Common::FSNode::kListFilesOnly)) {
00274         return;
00275     }
00276 
00277     // Build the savefile name cache.
00278     for (Common::FSList::const_iterator file = children.begin(), end = children.end(); file != end; ++file) {
00279         if (_saveFileCache.contains(file->getName())) {
00280             warning("DefaultSaveFileManager::assureCached: Name clash when building cache, ignoring file '%s'", file->getName().c_str());
00281         } else {
00282             _saveFileCache[file->getName()] = *file;
00283         }
00284     }
00285 
00286     // Only now store that we cached 'savePathName' to indicate we successfully
00287     // cached the directory.
00288     _cachedDirectory = savePathName;
00289 }
00290 
00291 #if defined(USE_CLOUD) && defined(USE_LIBCURL)
00292 
00293 Common::HashMap<Common::String, uint32> DefaultSaveFileManager::loadTimestamps() {
00294     Common::HashMap<Common::String, uint32> timestamps;
00295 
00296     //refresh the files list
00297     Common::Array<Common::String> files;
00298     g_system->getSavefileManager()->updateSavefilesList(files);
00299 
00300     //start with listing all the files in saves/ directory and setting invalid timestamp to them
00301     Common::StringArray localFiles = g_system->getSavefileManager()->listSavefiles("*");
00302     for (uint32 i = 0; i < localFiles.size(); ++i)
00303         timestamps[localFiles[i]] = INVALID_TIMESTAMP;
00304 
00305     //now actually load timestamps from file
00306     Common::InSaveFile *file = g_system->getSavefileManager()->openRawFile(TIMESTAMPS_FILENAME);
00307     if (!file) {
00308         warning("DefaultSaveFileManager: failed to open '%s' file to load timestamps", TIMESTAMPS_FILENAME);
00309         return timestamps;
00310     }
00311 
00312     while (!file->eos()) {
00313         //read filename into buffer (reading until the first ' ')
00314         Common::String buffer;
00315         while (!file->eos()) {
00316             byte b = file->readByte();
00317             if (b == ' ') break;
00318             buffer += (char)b;
00319         }
00320 
00321         //read timestamp info buffer (reading until ' ' or some line ending char)
00322         Common::String filename = buffer;
00323         while (true) {
00324             bool lineEnded = false;
00325             buffer = "";
00326             while (!file->eos()) {
00327                 byte b = file->readByte();
00328                 if (b == ' ' || b == '\n' || b == '\r') {
00329                     lineEnded = (b == '\n');
00330                     break;
00331                 }
00332                 buffer += (char)b;
00333             }
00334 
00335             if (buffer == "" && file->eos()) break;
00336             if (!lineEnded) filename += " " + buffer;
00337             else break;
00338         }
00339 
00340         //parse timestamp
00341         uint32 timestamp = buffer.asUint64();
00342         if (buffer == "" || timestamp == 0) break;
00343         if (timestamps.contains(filename))
00344             timestamps[filename] = timestamp;
00345     }
00346 
00347     delete file;
00348     return timestamps;
00349 }
00350 
00351 void DefaultSaveFileManager::saveTimestamps(Common::HashMap<Common::String, uint32> &timestamps) {
00352     Common::DumpFile f;
00353     Common::String filename = concatWithSavesPath(TIMESTAMPS_FILENAME);
00354     if (!f.open(filename, true)) {
00355         warning("DefaultSaveFileManager: failed to open '%s' file to save timestamps", filename.c_str());
00356         return;
00357     }
00358 
00359     for (Common::HashMap<Common::String, uint32>::iterator i = timestamps.begin(); i != timestamps.end(); ++i) {
00360         Common::String data = i->_key + Common::String::format(" %u\n", i->_value);
00361         if (f.write(data.c_str(), data.size()) != data.size()) {
00362             warning("DefaultSaveFileManager: failed to write timestamps data into '%s'", filename.c_str());
00363             return;
00364         }
00365     }
00366 
00367     f.flush();
00368     f.finalize();
00369     f.close();
00370 }
00371 
00372 Common::String DefaultSaveFileManager::concatWithSavesPath(Common::String name) {
00373     DefaultSaveFileManager *manager = dynamic_cast<DefaultSaveFileManager *>(g_system->getSavefileManager());
00374     Common::String path = (manager ? manager->getSavePath() : ConfMan.get("savepath"));
00375     if (path.size() > 0 && (path.lastChar() == '/' || path.lastChar() == '\\'))
00376         return path + name;
00377 
00378     //simple heuristic to determine which path separator to use
00379     int backslashes = 0;
00380     for (uint32 i = 0; i < path.size(); ++i)
00381         if (path[i] == '/') --backslashes;
00382         else if (path[i] == '\\') ++backslashes;
00383 
00384     if (backslashes > 0) return path + '\\' + name;
00385     return path + '/' + name;
00386 }
00387 
00388 #endif // ifdef USE_LIBCURL
00389 
00390 #endif // !defined(DISABLE_DEFAULT_SAVEFILEMANAGER)


Generated on Sat Apr 20 2019 05:02:25 for ResidualVM by doxygen 1.7.1
curved edge   curved edge