SWORD25: Migrate engine to Path

This commit is contained in:
Le Philousophe 2023-09-17 18:23:46 +02:00 committed by Eugene Sandulenko
parent 78dc2d9eb9
commit 3f9512c7cb
8 changed files with 38 additions and 27 deletions

View File

@ -36,20 +36,23 @@
namespace Sword25 {
Common::String FileSystemUtil::getUserdataDirectory() {
Common::Path FileSystemUtil::getUserdataDirectoryPath() {
// FIXME: This code is a hack which bypasses the savefile API,
// and should eventually be removed.
Common::String path = ConfMan.get("savepath");
Common::Path path = ConfMan.getPath("savepath");
if (path.empty()) {
error("No save path has been defined");
return "";
}
// Return the path
return path;
}
Common::String FileSystemUtil::getUserdataDirectory() {
// Return the path using / separator
return getUserdataDirectoryPath().toString('/');
}
Common::String FileSystemUtil::getPathSeparator() {
// FIXME: This code is a hack which bypasses the savefile API,
// and should eventually be removed.
@ -58,7 +61,7 @@ Common::String FileSystemUtil::getPathSeparator() {
bool FileSystemUtil::fileExists(const Common::String &filename) {
Common::File f;
if (f.exists(filename))
if (f.exists(Common::Path(filename)))
return true;
// Check if the file exists in the save folder

View File

@ -58,6 +58,14 @@ namespace Sword25 {
class FileSystemUtil {
public:
/**
* This function returns the path of the directory in which all user data is to be stored.
*
* These are for example Screenshots, game saves, configuration files, log files, ...
* @return Returns the name of the directory for user data.
*/
static Common::Path getUserdataDirectoryPath();
/**
* This function returns the name of the directory in which all user data is to be stored.
*

View File

@ -411,7 +411,7 @@ static int isSlotOccupied(lua_State *L) {
}
static int getSavegameDirectory(lua_State *L) {
lua_pushstring(L, PersistenceService::getInstance().getSavegameDirectory().c_str());
lua_pushstring(L, PersistenceService::getInstance().getSavegameDirectory().toString('/').c_str());
return 1;
}

View File

@ -200,8 +200,8 @@ uint PersistenceService::getSlotCount() {
return SLOT_COUNT;
}
Common::String PersistenceService::getSavegameDirectory() {
Common::FSNode node(FileSystemUtil::getUserdataDirectory());
Common::Path PersistenceService::getSavegameDirectory() {
Common::FSNode node(FileSystemUtil::getUserdataDirectoryPath());
Common::FSNode childNode = node.getChild(SAVEGAME_DIRECTORY);
// Try and return the path using the savegame subfolder. But if doesn't exist, fall back on the data directory

View File

@ -51,7 +51,7 @@ public:
// -----------------------------------------------------------------------------
static uint getSlotCount();
static Common::String getSavegameDirectory();
static Common::Path getSavegameDirectory();
void reloadSlots();
bool isSlotOccupied(uint slotID);

View File

@ -55,7 +55,7 @@ static Common::String normalizePath(const Common::String &path, const Common::St
PackageManager::PackageManager(Kernel *pKernel) : Service(pKernel),
_currentDirectory(PATH_SEPARATOR),
_rootFolder(ConfMan.get("path")),
_rootFolder(ConfMan.getPath("path")),
_useEnglishSpeech(ConfMan.getBool("english_speech")),
_extractedFiles(false) {
if (!registerScriptBindings())
@ -108,7 +108,7 @@ Common::ArchiveMemberPtr PackageManager::getArchiveMember(const Common::String &
Common::Archive *archiveFolder = (*i)->archive;
// Construct relative path
Common::String resPath(&fileName2.c_str()[(*i)->_mountPath.size()]);
Common::Path resPath(&fileName2.c_str()[(*i)->_mountPath.size()]);
if (archiveFolder->hasFile(resPath)) {
return archiveFolder->getMember(resPath);
@ -118,15 +118,15 @@ Common::ArchiveMemberPtr PackageManager::getArchiveMember(const Common::String &
return Common::ArchiveMemberPtr();
}
bool PackageManager::loadPackage(const Common::String &fileName, const Common::String &mountPosition) {
debug(3, "loadPackage(%s, %s)", fileName.c_str(), mountPosition.c_str());
bool PackageManager::loadPackage(const Common::Path &fileName, const Common::String &mountPosition) {
debug(3, "loadPackage(%s, %s)", fileName.toString(Common::Path::kNativeSeparator).c_str(), mountPosition.c_str());
Common::Archive *zipFile = Common::makeZipArchive(fileName);
if (zipFile == NULL) {
error("Unable to mount file \"%s\" to \"%s\"", fileName.c_str(), mountPosition.c_str());
error("Unable to mount file \"%s\" to \"%s\"", fileName.toString(Common::Path::kNativeSeparator).c_str(), mountPosition.c_str());
return false;
} else {
debugC(kDebugResource, "Package '%s' mounted as '%s'.", fileName.c_str(), mountPosition.c_str());
debugC(kDebugResource, "Package '%s' mounted as '%s'.", fileName.toString(Common::Path::kNativeSeparator).c_str(), mountPosition.c_str());
Common::ArchiveMemberList files;
zipFile->listMembers(files);
debug(3, "Capacity %d", files.size());
@ -140,14 +140,14 @@ bool PackageManager::loadPackage(const Common::String &fileName, const Common::S
}
}
bool PackageManager::loadDirectoryAsPackage(const Common::String &directoryName, const Common::String &mountPosition) {
bool PackageManager::loadDirectoryAsPackage(const Common::Path &directoryName, const Common::String &mountPosition) {
Common::FSNode directory(directoryName);
Common::Archive *folderArchive = new Common::FSDirectory(directory, 6, false, false, true);
if (!directory.exists() || (folderArchive == NULL)) {
error("Unable to mount directory \"%s\" to \"%s\".", directoryName.c_str(), mountPosition.c_str());
error("Unable to mount directory \"%s\" to \"%s\".", directoryName.toString(Common::Path::kNativeSeparator).c_str(), mountPosition.c_str());
return false;
} else {
debugC(kDebugResource, "Directory '%s' mounted as '%s'.", directoryName.c_str(), mountPosition.c_str());
debugC(kDebugResource, "Directory '%s' mounted as '%s'.", directoryName.toString(Common::Path::kNativeSeparator).c_str(), mountPosition.c_str());
Common::ArchiveMemberList files;
folderArchive->listMembers(files);
@ -265,7 +265,7 @@ int PackageManager::doSearch(Common::ArchiveMemberList &list, const Common::Stri
}
// Construct relative path
Common::String resFilter(&normalizedFilter.c_str()[(*i)->_mountPath.size()]);
Common::Path resFilter(&normalizedFilter.c_str()[(*i)->_mountPath.size()]);
if ((*i)->archive->listMatchingMembers(memberList, resFilter) == 0)
continue;

View File

@ -87,7 +87,7 @@ private:
Common::FSNode _rootFolder;
Common::List<ArchiveEntry *> _archiveList;
bool _extractedFiles;
Common::String _directoryName;
Common::Path _directoryName;
bool _useEnglishSpeech;
Common::String ensureSpeechLang(const Common::String &fileName);
@ -107,7 +107,7 @@ public:
*
* Set the PackageManager to run on extracted game files.s
*/
void setRunWithExtractedFiles(const Common::String &directoryName) {
void setRunWithExtractedFiles(const Common::Path &directoryName) {
_extractedFiles = true;
_directoryName = directoryName;
}
@ -118,14 +118,14 @@ public:
* @param MountPosition The directory name under which the package should be mounted
* @return Returns true if the mount was successful, otherwise false.
*/
bool loadPackage(const Common::String &fileName, const Common::String &mountPosition);
bool loadPackage(const Common::Path &fileName, const Common::String &mountPosition);
/**
* Mounts the contents of a directory in the specified directory in the directory tree.
* @param The name of the directory to mount
* @param MountPosition The directory name under which the package should be mounted
* @return Returns true if the mount was successful, otherwise false.
*/
bool loadDirectoryAsPackage(const Common::String &directoryName, const Common::String &mountPosition);
bool loadDirectoryAsPackage(const Common::Path &directoryName, const Common::String &mountPosition);
/**
* Downloads a file from the directory tree
* @param FileName The filename of the file to load

View File

@ -103,7 +103,7 @@ Common::Error Sword25Engine::appStart() {
// Load packages
PackageManager *packageManagerPtr = Kernel::getInstance()->getPackage();
if (getGameFlags() & GF_EXTRACTED) {
Common::String gameDirectory = ConfMan.get("path");
Common::Path gameDirectory = ConfMan.getPath("path");
packageManagerPtr->setRunWithExtractedFiles(gameDirectory);
if (!packageManagerPtr->loadDirectoryAsPackage(gameDirectory, "/"))
return Common::kUnknownError;
@ -157,7 +157,7 @@ bool Sword25Engine::loadPackages() {
return false;
// Get the contents of the main program directory and sort them alphabetically
Common::FSNode dir(ConfMan.get("path"));
Common::FSNode dir(ConfMan.getPath("path"));
Common::FSList files;
if (!dir.isDirectory() || !dir.getChildren(files, Common::FSNode::kListAll)) {
warning("Game data path does not exist or is not a directory");
@ -174,7 +174,7 @@ bool Sword25Engine::loadPackages() {
// existing files in the virtual file system, if they include files with the same name.
for (Common::FSList::const_iterator it = files.begin(); it != files.end(); ++it) {
if (it->getName().matchString("patch???.b25c", true))
if (!packageManagerPtr->loadPackage(it->getName(), "/"))
if (!packageManagerPtr->loadPackage(it->getPathInArchive(), "/"))
return false;
}
@ -182,7 +182,7 @@ bool Sword25Engine::loadPackages() {
// The filename of the packages have the form lang_*.b25c (eg. lang_de.b25c)
for (Common::FSList::const_iterator it = files.begin(); it != files.end(); ++it) {
if (it->getName().matchString("lang_*.b25c", true))
if (!packageManagerPtr->loadPackage(it->getName(), "/"))
if (!packageManagerPtr->loadPackage(it->getPathInArchive(), "/"))
return false;
}