2007-05-30 21:56:52 +00:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
|
|
|
*
|
|
|
|
* ScummVM is the legal property of its developers, whose names
|
|
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
|
|
|
* file distributed with this source distribution.
|
2006-06-24 10:08:43 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2014-02-18 02:34:18 +01:00
|
|
|
*
|
2006-06-24 10:08:43 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2014-02-18 02:34:18 +01:00
|
|
|
*
|
2006-06-24 10:08:43 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2015-09-14 22:43:01 +02:00
|
|
|
// This define lets us use the system function remove() on Symbian, which
|
|
|
|
// is disabled by default due to a macro conflict.
|
|
|
|
// See backends/platform/symbian/src/portdefs.h .
|
|
|
|
#define SYMBIAN_USE_SYSTEM_REMOVE
|
2015-09-14 22:17:11 +03:00
|
|
|
|
2010-11-29 16:18:43 +00:00
|
|
|
#include "common/scummsys.h"
|
|
|
|
|
2007-03-16 23:07:08 +00:00
|
|
|
#if !defined(DISABLE_DEFAULT_SAVEFILEMANAGER)
|
|
|
|
|
2008-11-03 10:45:59 +00:00
|
|
|
#include "backends/saves/default/default-saves.h"
|
|
|
|
|
2006-07-09 13:10:45 +00:00
|
|
|
#include "common/savefile.h"
|
2007-01-21 15:17:28 +00:00
|
|
|
#include "common/util.h"
|
2007-07-08 07:19:50 +00:00
|
|
|
#include "common/fs.h"
|
2008-11-21 20:02:55 +00:00
|
|
|
#include "common/archive.h"
|
2007-12-28 07:37:04 +00:00
|
|
|
#include "common/config-manager.h"
|
2009-01-22 23:14:16 +00:00
|
|
|
#include "common/zlib.h"
|
2006-06-24 10:08:43 +00:00
|
|
|
|
2010-11-05 10:31:36 +00:00
|
|
|
#ifndef _WIN32_WCE
|
2008-11-03 10:45:59 +00:00
|
|
|
#include <errno.h> // for removeSavefile()
|
2010-11-05 10:31:36 +00:00
|
|
|
#endif
|
2008-08-04 17:17:37 +00:00
|
|
|
|
|
|
|
DefaultSaveFileManager::DefaultSaveFileManager() {
|
|
|
|
}
|
|
|
|
|
2008-08-04 19:38:28 +00:00
|
|
|
DefaultSaveFileManager::DefaultSaveFileManager(const Common::String &defaultSavepath) {
|
|
|
|
ConfMan.registerDefault("savepath", defaultSavepath);
|
|
|
|
}
|
2008-08-04 17:17:37 +00:00
|
|
|
|
|
|
|
|
2008-11-06 16:40:00 +00:00
|
|
|
void DefaultSaveFileManager::checkPath(const Common::FSNode &dir) {
|
|
|
|
clearError();
|
|
|
|
if (!dir.exists()) {
|
|
|
|
setError(Common::kPathDoesNotExist, "The savepath '"+dir.getPath()+"' does not exist");
|
|
|
|
} else if (!dir.isDirectory()) {
|
|
|
|
setError(Common::kPathNotDirectory, "The savepath '"+dir.getPath()+"' is not a directory");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-18 15:44:59 +00:00
|
|
|
Common::StringArray DefaultSaveFileManager::listSavefiles(const Common::String &pattern) {
|
2016-02-25 18:33:33 +01:00
|
|
|
// Assure the savefile name cache is up-to-date.
|
|
|
|
assureCached(getSavePath());
|
2011-04-18 17:39:31 +02:00
|
|
|
if (getError().getCode() != Common::kNoError)
|
2010-03-18 15:44:59 +00:00
|
|
|
return Common::StringArray();
|
2008-11-06 16:40:00 +00:00
|
|
|
|
2010-03-18 15:44:59 +00:00
|
|
|
Common::StringArray results;
|
2016-02-25 18:33:33 +01:00
|
|
|
for (SaveFileCache::const_iterator file = _saveFileCache.begin(), end = _saveFileCache.end(); file != end; ++file) {
|
|
|
|
if (file->_key.matchString(pattern, true)) {
|
|
|
|
results.push_back(file->_key);
|
2007-09-18 20:02:04 +00:00
|
|
|
}
|
|
|
|
}
|
2007-09-19 08:40:12 +00:00
|
|
|
|
2007-09-18 20:02:04 +00:00
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2009-05-29 14:38:22 +00:00
|
|
|
Common::InSaveFile *DefaultSaveFileManager::openForLoading(const Common::String &filename) {
|
2016-02-25 18:33:33 +01:00
|
|
|
// Assure the savefile name cache is up-to-date.
|
|
|
|
assureCached(getSavePath());
|
2011-04-18 17:39:31 +02:00
|
|
|
if (getError().getCode() != Common::kNoError)
|
2016-02-25 18:33:33 +01:00
|
|
|
return nullptr;
|
2009-07-10 22:29:25 +00:00
|
|
|
|
2016-02-25 18:33:33 +01:00
|
|
|
SaveFileCache::const_iterator file = _saveFileCache.find(filename);
|
|
|
|
if (file == _saveFileCache.end()) {
|
|
|
|
return nullptr;
|
|
|
|
} else {
|
|
|
|
// Open the file for loading.
|
|
|
|
Common::SeekableReadStream *sf = file->_value.createReadStream();
|
|
|
|
return Common::wrapCompressedReadStream(sf);
|
|
|
|
}
|
2007-12-28 07:37:04 +00:00
|
|
|
}
|
2006-06-24 10:08:43 +00:00
|
|
|
|
2012-05-21 21:21:48 +03:00
|
|
|
Common::OutSaveFile *DefaultSaveFileManager::openForSaving(const Common::String &filename, bool compress) {
|
2016-02-25 18:33:33 +01:00
|
|
|
// Assure the savefile name cache is up-to-date.
|
|
|
|
const Common::String savePathName = getSavePath();
|
|
|
|
assureCached(savePathName);
|
2011-04-18 17:39:31 +02:00
|
|
|
if (getError().getCode() != Common::kNoError)
|
2016-02-25 18:33:33 +01:00
|
|
|
return nullptr;
|
2006-06-24 10:08:43 +00:00
|
|
|
|
2016-02-25 18:33:33 +01:00
|
|
|
// Obtain node.
|
|
|
|
SaveFileCache::const_iterator file = _saveFileCache.find(filename);
|
|
|
|
Common::FSNode fileNode;
|
2009-07-10 22:29:25 +00:00
|
|
|
|
2016-02-25 18:33:33 +01:00
|
|
|
// If the file did not exist before, we add it to the cache.
|
|
|
|
if (file == _saveFileCache.end()) {
|
|
|
|
const Common::FSNode savePath(savePathName);
|
|
|
|
fileNode = savePath.getChild(filename);
|
|
|
|
} else {
|
|
|
|
fileNode = file->_value;
|
|
|
|
}
|
2008-08-04 11:32:42 +00:00
|
|
|
|
2016-02-25 18:33:33 +01:00
|
|
|
// Open the file for saving.
|
|
|
|
Common::WriteStream *const sf = fileNode.createWriteStream();
|
|
|
|
Common::OutSaveFile *const result = compress ? Common::wrapCompressedWriteStream(sf) : sf;
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2016-02-25 18:33:33 +01:00
|
|
|
// Add file to cache now that it exists.
|
|
|
|
_saveFileCache[filename] = Common::FSNode(fileNode.getPath());
|
|
|
|
|
|
|
|
return result;
|
2006-06-24 10:08:43 +00:00
|
|
|
}
|
|
|
|
|
2009-05-29 14:38:22 +00:00
|
|
|
bool DefaultSaveFileManager::removeSavefile(const Common::String &filename) {
|
2016-02-25 18:33:33 +01:00
|
|
|
// Assure the savefile name cache is up-to-date.
|
|
|
|
assureCached(getSavePath());
|
2011-04-18 17:39:31 +02:00
|
|
|
if (getError().getCode() != Common::kNoError)
|
2008-11-06 16:40:00 +00:00
|
|
|
return false;
|
2008-08-04 11:32:42 +00:00
|
|
|
|
2016-02-25 18:33:33 +01:00
|
|
|
// Obtain node if exists.
|
|
|
|
SaveFileCache::const_iterator file = _saveFileCache.find(filename);
|
|
|
|
if (file == _saveFileCache.end()) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
const Common::FSNode fileNode = file->_value;
|
|
|
|
// Remove from cache, this invalidates the 'file' iterator.
|
|
|
|
_saveFileCache.erase(file);
|
|
|
|
file = _saveFileCache.end();
|
|
|
|
|
|
|
|
// FIXME: remove does not exist on all systems. If your port fails to
|
|
|
|
// compile because of this, please let us know (scummvm-devel or Fingolfin).
|
|
|
|
// There is a nicely portable workaround, too: Make this method overloadable.
|
|
|
|
if (remove(fileNode.getPath().c_str()) != 0) {
|
2007-11-03 21:17:40 +00:00
|
|
|
#ifndef _WIN32_WCE
|
2016-02-25 18:33:33 +01:00
|
|
|
if (errno == EACCES)
|
|
|
|
setError(Common::kWritePermissionDenied, "Search or write permission denied: "+fileNode.getName());
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2016-02-25 18:33:33 +01:00
|
|
|
if (errno == ENOENT)
|
|
|
|
setError(Common::kPathDoesNotExist, "removeSavefile: '"+fileNode.getName()+"' does not exist or path is invalid");
|
2007-11-03 21:17:40 +00:00
|
|
|
#endif
|
2016-02-25 18:33:33 +01:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
2007-10-31 19:09:23 +00:00
|
|
|
}
|
2006-06-24 10:08:43 +00:00
|
|
|
}
|
2007-03-16 23:07:08 +00:00
|
|
|
|
2007-12-28 21:12:30 +00:00
|
|
|
Common::String DefaultSaveFileManager::getSavePath() const {
|
2007-12-28 07:37:04 +00:00
|
|
|
|
2007-12-28 21:12:30 +00:00
|
|
|
Common::String dir;
|
2007-12-28 07:37:04 +00:00
|
|
|
|
|
|
|
// Try to use game specific savepath from config
|
2007-12-28 21:12:30 +00:00
|
|
|
dir = ConfMan.get("savepath");
|
2007-12-28 07:37:04 +00:00
|
|
|
|
|
|
|
// Work around a bug (#999122) in the original 0.6.1 release of
|
|
|
|
// ScummVM, which would insert a bad savepath value into config files.
|
2007-12-28 21:12:30 +00:00
|
|
|
if (dir == "None") {
|
2007-12-28 07:37:04 +00:00
|
|
|
ConfMan.removeKey("savepath", ConfMan.getActiveDomainName());
|
|
|
|
ConfMan.flushToDisk();
|
2007-12-28 21:12:30 +00:00
|
|
|
dir = ConfMan.get("savepath");
|
2007-12-28 07:37:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _WIN32_WCE
|
2007-12-28 21:12:30 +00:00
|
|
|
if (dir.empty())
|
|
|
|
dir = ConfMan.get("path");
|
2007-12-28 07:37:04 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
|
2016-02-25 18:33:33 +01:00
|
|
|
void DefaultSaveFileManager::assureCached(const Common::String &savePathName) {
|
|
|
|
// Check that path exists and is usable.
|
|
|
|
checkPath(Common::FSNode(savePathName));
|
|
|
|
|
|
|
|
if (_cachedDirectory == savePathName) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_saveFileCache.clear();
|
|
|
|
_cachedDirectory.clear();
|
|
|
|
|
|
|
|
if (getError().getCode() != Common::kNoError) {
|
|
|
|
warning("DefaultSaveFileManager::assureCached: Can not cache path '%s': '%s'", savePathName.c_str(), getErrorDesc().c_str());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FSNode can cache its members, thus create it after checkPath to reflect
|
|
|
|
// actual file system state.
|
|
|
|
const Common::FSNode savePath(savePathName);
|
|
|
|
|
|
|
|
Common::FSList children;
|
|
|
|
if (!savePath.getChildren(children, Common::FSNode::kListFilesOnly)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the savefile name cache.
|
|
|
|
for (Common::FSList::const_iterator file = children.begin(), end = children.end(); file != end; ++file) {
|
|
|
|
if (_saveFileCache.contains(file->getName())) {
|
|
|
|
warning("DefaultSaveFileManager::assureCached: Name clash when building cache, ignoring file '%s'", file->getName().c_str());
|
|
|
|
} else {
|
|
|
|
_saveFileCache[file->getName()] = *file;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only now store that we cached 'savePathName' to indicate we successfully
|
|
|
|
// cached the directory.
|
|
|
|
_cachedDirectory = savePathName;
|
|
|
|
}
|
|
|
|
|
2007-03-16 23:07:08 +00:00
|
|
|
#endif // !defined(DISABLE_DEFAULT_SAVEFILEMANAGER)
|