Initial commit
This commit is contained in:
320
engines/sword25/package/packagemanager.cpp
Normal file
320
engines/sword25/package/packagemanager.cpp
Normal file
@@ -0,0 +1,320 @@
|
||||
/* 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.
|
||||
*
|
||||
* 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 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* This code is based on Broken Sword 2.5 engine
|
||||
*
|
||||
* Copyright (c) Malte Thiesen, Daniel Queteschiner and Michael Elsdoerfer
|
||||
*
|
||||
* Licensed under GNU GPL v2
|
||||
*
|
||||
*/
|
||||
|
||||
#include "common/archive.h"
|
||||
#include "common/config-manager.h"
|
||||
#include "common/savefile.h"
|
||||
#include "common/str-array.h"
|
||||
#include "common/system.h"
|
||||
#include "common/compression/unzip.h"
|
||||
#include "sword25/sword25.h" // for kDebugScript
|
||||
#include "sword25/kernel/filesystemutil.h"
|
||||
#include "sword25/package/packagemanager.h"
|
||||
|
||||
namespace Sword25 {
|
||||
|
||||
const char PATH_SEPARATOR = '/';
|
||||
|
||||
static Common::String normalizePath(const Common::String &path, const Common::String ¤tDirectory) {
|
||||
Common::String wholePath = (path.size() >= 1 && path[0] == PATH_SEPARATOR) ? path : currentDirectory + PATH_SEPARATOR + path;
|
||||
|
||||
if (wholePath.size() == 0) {
|
||||
// The path list has no elements, therefore the root directory is returned
|
||||
return Common::String(PATH_SEPARATOR);
|
||||
}
|
||||
|
||||
return Common::normalizePath(wholePath, PATH_SEPARATOR);
|
||||
}
|
||||
|
||||
PackageManager::PackageManager(Kernel *pKernel) : Service(pKernel),
|
||||
_currentDirectory(PATH_SEPARATOR),
|
||||
_rootFolder(ConfMan.getPath("path")),
|
||||
_useEnglishSpeech(ConfMan.getBool("english_speech")),
|
||||
_extractedFiles(false) {
|
||||
if (!registerScriptBindings())
|
||||
error("Script bindings could not be registered.");
|
||||
else
|
||||
debugC(kDebugScript, "Script bindings registered.");
|
||||
}
|
||||
|
||||
PackageManager::~PackageManager() {
|
||||
// Free the package list
|
||||
Common::List<ArchiveEntry *>::iterator i;
|
||||
for (i = _archiveList.begin(); i != _archiveList.end(); ++i)
|
||||
delete *i;
|
||||
|
||||
}
|
||||
|
||||
Common::String PackageManager::ensureSpeechLang(const Common::String &fileName) {
|
||||
if (!_useEnglishSpeech || fileName.size() < 9 || !fileName.hasPrefix("/speech/"))
|
||||
return fileName;
|
||||
|
||||
// Always keep German speech as a fallback in case the English speech pack is not present.
|
||||
// However this means we cannot play with German text and English voice.
|
||||
if (fileName.hasPrefix("/speech/de"))
|
||||
return fileName;
|
||||
|
||||
Common::String newFileName = "/speech/en";
|
||||
uint fileIdx = 9;
|
||||
while (fileIdx < fileName.size() && fileName[fileIdx] != '/')
|
||||
++fileIdx;
|
||||
if (fileIdx < fileName.size())
|
||||
newFileName += fileName.c_str() + fileIdx;
|
||||
|
||||
return newFileName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Scans through the archive list for a specified file
|
||||
*/
|
||||
Common::ArchiveMemberPtr PackageManager::getArchiveMember(const Common::String &fileName) {
|
||||
Common::String fileName2 = ensureSpeechLang(fileName);
|
||||
// Loop through checking each archive
|
||||
Common::List<ArchiveEntry *>::iterator i;
|
||||
for (i = _archiveList.begin(); i != _archiveList.end(); ++i) {
|
||||
if (!fileName2.hasPrefix((*i)->_mountPath)) {
|
||||
// The mount path is in different subtree. Skipping
|
||||
continue;
|
||||
}
|
||||
|
||||
// Look into the archive for the desired file
|
||||
Common::Archive *archiveFolder = (*i)->archive;
|
||||
|
||||
// Construct relative path
|
||||
Common::Path resPath(&fileName2.c_str()[(*i)->_mountPath.size()]);
|
||||
|
||||
if (archiveFolder->hasFile(resPath)) {
|
||||
return archiveFolder->getMember(resPath);
|
||||
}
|
||||
}
|
||||
|
||||
return Common::ArchiveMemberPtr();
|
||||
}
|
||||
|
||||
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.toString(Common::Path::kNativeSeparator).c_str(), mountPosition.c_str());
|
||||
return false;
|
||||
} else {
|
||||
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());
|
||||
|
||||
for (Common::ArchiveMemberList::iterator it = files.begin(); it != files.end(); ++it)
|
||||
debug(3, "%s", (*it)->getName().c_str());
|
||||
|
||||
_archiveList.push_front(new ArchiveEntry(zipFile, mountPosition));
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
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.toString(Common::Path::kNativeSeparator).c_str(), mountPosition.c_str());
|
||||
return false;
|
||||
} else {
|
||||
debugC(kDebugResource, "Directory '%s' mounted as '%s'.", directoryName.toString(Common::Path::kNativeSeparator).c_str(), mountPosition.c_str());
|
||||
|
||||
Common::ArchiveMemberList files;
|
||||
folderArchive->listMembers(files);
|
||||
debug(0, "Capacity %d", files.size());
|
||||
|
||||
_archiveList.push_front(new ArchiveEntry(folderArchive, mountPosition));
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
byte *PackageManager::getFile(const Common::String &fileName, uint *fileSizePtr) {
|
||||
const Common::String B25S_EXTENSION(".b25s");
|
||||
Common::SeekableReadStream *in;
|
||||
|
||||
if (fileName.hasSuffix(B25S_EXTENSION)) {
|
||||
// Savegame loading logic
|
||||
Common::SaveFileManager *sfm = g_system->getSavefileManager();
|
||||
Common::InSaveFile *file = sfm->openForLoading(
|
||||
FileSystemUtil::getPathFilename(fileName));
|
||||
if (!file) {
|
||||
error("Could not load savegame \"%s\".", fileName.c_str());
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (fileSizePtr)
|
||||
*fileSizePtr = file->size();
|
||||
|
||||
byte *buffer = new byte[file->size()];
|
||||
file->read(buffer, file->size());
|
||||
|
||||
delete file;
|
||||
return buffer;
|
||||
}
|
||||
|
||||
Common::ArchiveMemberPtr fileNode = getArchiveMember(normalizePath(fileName, _currentDirectory));
|
||||
if (!fileNode)
|
||||
return 0;
|
||||
if (!(in = fileNode->createReadStream()))
|
||||
return 0;
|
||||
|
||||
// If the filesize is desired, then output the size
|
||||
if (fileSizePtr)
|
||||
*fileSizePtr = in->size();
|
||||
|
||||
// Read the file
|
||||
byte *buffer = new byte[in->size()];
|
||||
int bytesRead = in->read(buffer, in->size());
|
||||
delete in;
|
||||
|
||||
// Modify the buffer to enable internal debugger if needed
|
||||
if (debugChannelSet(-1, kDebugInternalDebugger) && fileName.equals("/system/internal_config.lua")) {
|
||||
char *found = strstr((char *)buffer, "ENGINE_RELEASE_TYPE = 'pub'");
|
||||
if (found != nullptr) {
|
||||
memcpy(found + 23, "dev", 3);
|
||||
}
|
||||
}
|
||||
|
||||
// Modify the buffer to properly set the death screen as background
|
||||
// by changing its z value
|
||||
if (fileName.equals("rooms/tod/scripts/default.lua")) {
|
||||
char *found = strstr((char *)buffer, "self:AddOccluder('/rooms/tod/gfx/rip.png', { X = 0, Y = 80 }, 10)");
|
||||
if (found != nullptr) {
|
||||
memcpy(found + 62, " 8", 2);
|
||||
}
|
||||
}
|
||||
|
||||
if (!bytesRead) {
|
||||
delete[] buffer;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
Common::SeekableReadStream *PackageManager::getStream(const Common::String &fileName) {
|
||||
Common::SeekableReadStream *in;
|
||||
Common::ArchiveMemberPtr fileNode = getArchiveMember(normalizePath(fileName, _currentDirectory));
|
||||
if (!fileNode)
|
||||
return 0;
|
||||
if (!(in = fileNode->createReadStream()))
|
||||
return 0;
|
||||
|
||||
return in;
|
||||
}
|
||||
|
||||
bool PackageManager::changeDirectory(const Common::String &directory) {
|
||||
// Get the path elements for the file
|
||||
_currentDirectory = normalizePath(directory, _currentDirectory);
|
||||
return true;
|
||||
}
|
||||
|
||||
Common::String PackageManager::getAbsolutePath(const Common::String &fileName) {
|
||||
return normalizePath(ensureSpeechLang(fileName), _currentDirectory);
|
||||
}
|
||||
|
||||
bool PackageManager::fileExists(const Common::String &fileName) {
|
||||
// FIXME: The current Zip implementation doesn't support getting a folder entry, which is needed for detecting
|
||||
// the English voice pack
|
||||
Common::String fileName2 = ensureSpeechLang(fileName);
|
||||
if (fileName2 == "/speech/en") {
|
||||
// To get around this, change to detecting one of the files in the folder
|
||||
bool exists = getArchiveMember(normalizePath(fileName2 + "/APO0001.ogg", _currentDirectory));
|
||||
if (!exists && _useEnglishSpeech) {
|
||||
_useEnglishSpeech = false;
|
||||
warning("English speech not found");
|
||||
}
|
||||
return exists;
|
||||
}
|
||||
|
||||
Common::ArchiveMemberPtr fileNode = getArchiveMember(normalizePath(fileName2, _currentDirectory));
|
||||
return fileNode;
|
||||
}
|
||||
|
||||
int PackageManager::doSearch(Common::ArchiveMemberList &list, const Common::String &filter, const Common::String &path, uint typeFilter) {
|
||||
Common::String normalizedFilter = normalizePath(ensureSpeechLang(filter), _currentDirectory);
|
||||
int num = 0;
|
||||
|
||||
if (path.size() > 0)
|
||||
warning("STUB: PackageManager::doSearch(<%s>, <%s>, %d)", filter.c_str(), path.c_str(), typeFilter);
|
||||
|
||||
debug(9, "PackageManager::doSearch(..., \"%s\", \"%s\", %d)", filter.c_str(), path.c_str(), typeFilter);
|
||||
|
||||
// Loop through checking each archive
|
||||
Common::List<ArchiveEntry *>::iterator i;
|
||||
for (i = _archiveList.begin(); i != _archiveList.end(); ++i) {
|
||||
Common::ArchiveMemberList memberList;
|
||||
|
||||
if (!normalizedFilter.hasPrefix((*i)->_mountPath)) {
|
||||
// The mount path is in different subtree. Skipping
|
||||
continue;
|
||||
}
|
||||
|
||||
// Construct relative path
|
||||
Common::Path resFilter(&normalizedFilter.c_str()[(*i)->_mountPath.size()]);
|
||||
|
||||
if ((*i)->archive->listMatchingMembers(memberList, resFilter) == 0)
|
||||
continue;
|
||||
|
||||
// Create a list of the matching names
|
||||
for (Common::ArchiveMemberList::iterator it = memberList.begin(); it != memberList.end(); ++it) {
|
||||
Common::Path name = (*it)->getPathInArchive();
|
||||
bool isDirectory = (*it)->isDirectory();
|
||||
bool matchType = (((typeFilter & PackageManager::FT_DIRECTORY) && isDirectory) ||
|
||||
((typeFilter & PackageManager::FT_FILE) && !isDirectory));
|
||||
|
||||
if (matchType) {
|
||||
|
||||
// Do not add duplicate files
|
||||
bool found = false;
|
||||
for (Common::ArchiveMemberList::iterator it1 = list.begin(); it1 != list.end(); ++it1) {
|
||||
if ((*it1)->getPathInArchive() == name) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
list.push_back(Common::ArchiveMemberList::value_type(new Common::GenericArchiveMember(name, *(*i)->archive)));
|
||||
debug(9, "> %s", name.toString().c_str());
|
||||
}
|
||||
num++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return num;
|
||||
}
|
||||
|
||||
} // End of namespace Sword25
|
||||
219
engines/sword25/package/packagemanager.h
Normal file
219
engines/sword25/package/packagemanager.h
Normal file
@@ -0,0 +1,219 @@
|
||||
/* 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.
|
||||
*
|
||||
* 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 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* This code is based on Broken Sword 2.5 engine
|
||||
*
|
||||
* Copyright (c) Malte Thiesen, Daniel Queteschiner and Michael Elsdoerfer
|
||||
*
|
||||
* Licensed under GNU GPL v2
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* BS_PackageManager
|
||||
* -----------------
|
||||
* This is the package manager interface, that contains all the methods that a package manager
|
||||
* must implement.
|
||||
* In the package manager, note the following:
|
||||
* 1. It creates a completely new (virtual) directory tree in the packages and directories
|
||||
* can be mounted.
|
||||
* 2. To separate elements of a directory path '/' must be used rather than '\'
|
||||
* 3. LoadDirectoryAsPackage should only be used for testing. The final release will be
|
||||
* have all files in packages.
|
||||
*
|
||||
* Autor: Malte Thiesen, $author$
|
||||
*/
|
||||
|
||||
#ifndef SWORD25_PACKAGE_MANAGER_H
|
||||
#define SWORD25_PACKAGE_MANAGER_H
|
||||
|
||||
#include "common/archive.h"
|
||||
#include "common/array.h"
|
||||
#include "common/fs.h"
|
||||
#include "common/str.h"
|
||||
|
||||
#include "sword25/kernel/common.h"
|
||||
#include "sword25/kernel/kernel.h"
|
||||
#include "sword25/kernel/service.h"
|
||||
|
||||
namespace Sword25 {
|
||||
|
||||
// Class definitions
|
||||
|
||||
/**
|
||||
* The Package Manager interface
|
||||
*
|
||||
* 1. It creates a completely new (virtual) directory tree in the packages and directories
|
||||
* can be mounted.
|
||||
* 2. To separate elements of a directory path '/' must be used rather than '\'
|
||||
* 3. LoadDirectoryAsPackage should only be used for testing. The final release will be
|
||||
* have all files in packages.
|
||||
*/
|
||||
class PackageManager : public Service {
|
||||
private:
|
||||
class ArchiveEntry {
|
||||
public:
|
||||
Common::Archive *archive;
|
||||
Common::String _mountPath;
|
||||
|
||||
ArchiveEntry(Common::Archive *archive_, const Common::String &mountPath_):
|
||||
archive(archive_), _mountPath(mountPath_) {
|
||||
}
|
||||
~ArchiveEntry() {
|
||||
delete archive;
|
||||
}
|
||||
};
|
||||
|
||||
Common::String _currentDirectory;
|
||||
Common::FSNode _rootFolder;
|
||||
Common::List<ArchiveEntry *> _archiveList;
|
||||
bool _extractedFiles;
|
||||
Common::Path _directoryName;
|
||||
|
||||
bool _useEnglishSpeech;
|
||||
Common::String ensureSpeechLang(const Common::String &fileName);
|
||||
|
||||
Common::ArchiveMemberPtr getArchiveMember(const Common::String &fileName);
|
||||
|
||||
public:
|
||||
PackageManager(Kernel *pKernel);
|
||||
~PackageManager() override;
|
||||
|
||||
enum FILE_TYPES {
|
||||
FT_DIRECTORY = (1 << 0),
|
||||
FT_FILE = (1 << 1)
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* Set the PackageManager to run on extracted game files.s
|
||||
*/
|
||||
void setRunWithExtractedFiles(const Common::Path &directoryName) {
|
||||
_extractedFiles = true;
|
||||
_directoryName = directoryName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mounts the contents of a package in the directory specified in the directory tree.
|
||||
* @param FileName The filename of the package 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 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::Path &directoryName, const Common::String &mountPosition);
|
||||
/**
|
||||
* Downloads a file from the directory tree
|
||||
* @param FileName The filename of the file to load
|
||||
* @param pFileSize Pointer to the variable that will contain the size of the loaded file. The deafult is NULL.
|
||||
* @return Specifies a pointer to the loaded data of the file
|
||||
* @remark The client must not forget to release the data of the file using BE_DELETE_A.
|
||||
*/
|
||||
byte *getFile(const Common::String &fileName, uint *pFileSize = NULL);
|
||||
|
||||
/**
|
||||
* Returns a stream from file file from the directory tree
|
||||
* @param FileName The filename of the file to load
|
||||
* @return Pointer to the stream object
|
||||
*/
|
||||
Common::SeekableReadStream *getStream(const Common::String &fileName);
|
||||
/**
|
||||
* Downloads an XML file and prefixes it with an XML Version key, since the XML files don't contain it,
|
||||
* and it is required for ScummVM to correctly parse the XML.
|
||||
* @param FileName The filename of the file to load
|
||||
* @param pFileSize Pointer to the variable that will contain the size of the loaded file. The deafult is NULL.
|
||||
* @return Specifies a pointer to the loaded data of the file
|
||||
* @remark The client must not forget to release the data of the file using BE_DELETE_A.
|
||||
*/
|
||||
char *getXmlFile(const Common::String &fileName, uint *pFileSize = NULL) {
|
||||
const char *versionStr = "<?xml version=\"1.0\"?>";
|
||||
uint fileSize;
|
||||
char *data = (char *)getFile(fileName, &fileSize);
|
||||
size_t resultSize = fileSize + strlen(versionStr) + 1;
|
||||
char *result = (char *)malloc(resultSize);
|
||||
if (!result)
|
||||
error("[PackageManager::getXmlFile] Cannot allocate memory");
|
||||
|
||||
Common::strcpy_s(result, resultSize, versionStr);
|
||||
Common::copy(data, data + fileSize, result + strlen(versionStr));
|
||||
result[resultSize - 1] = '\0';
|
||||
|
||||
delete[] data;
|
||||
if (pFileSize)
|
||||
*pFileSize = fileSize + strlen(versionStr);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the path to the current directory.
|
||||
* @return Returns a string containing the path to the current directory.
|
||||
* If the path could not be determined, an empty string is returned.
|
||||
* @remark For cutting path elements '\' is used rather than '/' elements.
|
||||
*/
|
||||
Common::String getCurrentDirectory() { return _currentDirectory; }
|
||||
/**
|
||||
* Changes the current directory.
|
||||
* @param Directory The path to the new directory. The path can be relative.
|
||||
* @return Returns true if the operation was successful, otherwise false.
|
||||
* @remark For cutting path elements '\' is used rather than '/' elements.
|
||||
*/
|
||||
bool changeDirectory(const Common::String &directory);
|
||||
/**
|
||||
* Returns the absolute path to a file in the directory tree.
|
||||
* @param FileName The filename of the file whose absolute path is to be determined.
|
||||
* These parameters may include both relative and absolute paths.
|
||||
* @return Returns an absolute path to the given file.
|
||||
* @remark For cutting path elements '\' is used rather than '/' elements.
|
||||
*/
|
||||
Common::String getAbsolutePath(const Common::String &fileName);
|
||||
/**
|
||||
* Creates a BS_PackageManager::FileSearch object to search for files
|
||||
* @param Filter Specifies the search string. Wildcards of '*' and '?' are allowed
|
||||
* @param Path Specifies the directory that should be searched.
|
||||
* @param TypeFilter A combination of flags BS_PackageManager::FT_DIRECTORY and BS_PackageManager::FT_FILE.
|
||||
* These flags indicate whether to search for files, directories, or both.
|
||||
* The default is BS_PackageManager::FT_DIRECTORY | BS_PackageManager::FT_FILE
|
||||
* @return Specifies a pointer to a BS_PackageManager::FileSearch object, or NULL if no file was found.
|
||||
* @remark Do not forget to delete the object after use.
|
||||
*/
|
||||
int doSearch(Common::ArchiveMemberList &list, const Common::String &filter, const Common::String &path, uint typeFilter = FT_DIRECTORY | FT_FILE);
|
||||
|
||||
/**
|
||||
* Determines whether a file exists
|
||||
* @param FileName The filename
|
||||
* @return Returns true if the file exists, otherwise false.
|
||||
*/
|
||||
bool fileExists(const Common::String &FileName);
|
||||
|
||||
private:
|
||||
bool registerScriptBindings();
|
||||
};
|
||||
|
||||
} // End of namespace Sword25
|
||||
|
||||
#endif
|
||||
175
engines/sword25/package/packagemanager_script.cpp
Normal file
175
engines/sword25/package/packagemanager_script.cpp
Normal file
@@ -0,0 +1,175 @@
|
||||
/* 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.
|
||||
*
|
||||
* 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 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* This code is based on Broken Sword 2.5 engine
|
||||
*
|
||||
* Copyright (c) Malte Thiesen, Daniel Queteschiner and Michael Elsdoerfer
|
||||
*
|
||||
* Licensed under GNU GPL v2
|
||||
*
|
||||
*/
|
||||
|
||||
#include "sword25/kernel/common.h"
|
||||
#include "sword25/kernel/kernel.h"
|
||||
#include "sword25/script/script.h"
|
||||
#include "sword25/script/luabindhelper.h"
|
||||
|
||||
#include "sword25/package/packagemanager.h"
|
||||
|
||||
namespace Sword25 {
|
||||
|
||||
static PackageManager *getPM() {
|
||||
Kernel *pKernel = Kernel::getInstance();
|
||||
assert(pKernel);
|
||||
PackageManager *pPM = pKernel->getPackage();
|
||||
assert(pPM);
|
||||
return pPM;
|
||||
}
|
||||
|
||||
static int loadPackage(lua_State *L) {
|
||||
PackageManager *pPM = getPM();
|
||||
|
||||
lua_pushbooleancpp(L, pPM->loadPackage(luaL_checkstring(L, 1), luaL_checkstring(L, 2)));
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int loadDirectoryAsPackage(lua_State *L) {
|
||||
PackageManager *pPM = getPM();
|
||||
|
||||
lua_pushbooleancpp(L, pPM->loadDirectoryAsPackage(luaL_checkstring(L, 1), luaL_checkstring(L, 2)));
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void splitSearchPath(const Common::String &path, Common::String &directory, Common::String &filter) {
|
||||
// Scan backwards for a trailing slash
|
||||
const char *sPath = path.c_str();
|
||||
const char *lastSlash = sPath + strlen(sPath) - 1;
|
||||
while ((lastSlash >= sPath) && (*lastSlash != '/')) --lastSlash;
|
||||
|
||||
if (lastSlash >= sPath) {
|
||||
directory = "";
|
||||
filter = path;
|
||||
} else {
|
||||
directory = Common::String(sPath, lastSlash - sPath);
|
||||
filter = Common::String(lastSlash + 1);
|
||||
}
|
||||
}
|
||||
|
||||
static void doSearch(lua_State *L, const Common::String &path, uint type) {
|
||||
PackageManager *pPM = getPM();
|
||||
|
||||
// Der Packagemanager-Service muss den Suchstring und den Pfad getrennt übergeben bekommen.
|
||||
// Um die Benutzbarkeit zu verbessern sollen Skriptprogrammierer dieses als ein Pfad übergeben können.
|
||||
// Daher muss der übergebene Pfad am letzten Slash aufgesplittet werden.
|
||||
Common::String directory;
|
||||
Common::String filter;
|
||||
splitSearchPath(path, directory, filter);
|
||||
|
||||
// Ergebnistable auf dem Lua-Stack erstellen
|
||||
lua_newtable(L);
|
||||
|
||||
// Suche durchführen und die Namen aller gefundenen Dateien in die Ergebnistabelle einfügen.
|
||||
// Als Indizes werden fortlaufende Nummern verwandt.
|
||||
uint resultNr = 1;
|
||||
Common::ArchiveMemberList list;
|
||||
int numMatches;
|
||||
|
||||
numMatches = pPM->doSearch(list, filter, directory, type);
|
||||
if (numMatches) {
|
||||
for (Common::ArchiveMemberList::iterator it = list.begin(); it != list.end(); ++it) {
|
||||
lua_pushnumber(L, resultNr);
|
||||
lua_pushstring(L, (*it)->getName().c_str());
|
||||
lua_settable(L, -3);
|
||||
resultNr++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int findFiles(lua_State *L) {
|
||||
doSearch(L, luaL_checkstring(L, 1), PackageManager::FT_FILE);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int findDirectories(lua_State *L) {
|
||||
doSearch(L, luaL_checkstring(L, 1), PackageManager::FT_DIRECTORY);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int getFileAsString(lua_State *L) {
|
||||
PackageManager *pPM = getPM();
|
||||
|
||||
uint fileSize;
|
||||
char *fileData = (char *)pPM->getFile(luaL_checkstring(L, 1), &fileSize);
|
||||
if (fileData) {
|
||||
lua_pushlstring(L, fileData, fileSize);
|
||||
delete[] fileData;
|
||||
|
||||
return 1;
|
||||
} else
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int fileExists(lua_State *L) {
|
||||
lua_pushbooleancpp(L, getPM()->fileExists(luaL_checkstring(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Marks a function that should never be used
|
||||
static int dummyFuncError(lua_State *L) {
|
||||
error("Dummy function invoked by LUA");
|
||||
return 1;
|
||||
}
|
||||
|
||||
static const char *PACKAGE_LIBRARY_NAME = "Package";
|
||||
|
||||
static const luaL_reg PACKAGE_FUNCTIONS[] = {
|
||||
{"LoadPackage", loadPackage},
|
||||
{"LoadDirectoryAsPackage", loadDirectoryAsPackage},
|
||||
{"GetCurrentDirectory", dummyFuncError},
|
||||
{"ChangeDirectory", dummyFuncError},
|
||||
{"GetAbsolutePath", dummyFuncError},
|
||||
{"GetFileSize", dummyFuncError},
|
||||
{"GetFileType", dummyFuncError},
|
||||
{"FindFiles", findFiles},
|
||||
{"FindDirectories", findDirectories},
|
||||
{"GetFileAsString", getFileAsString},
|
||||
{"FileExists", fileExists},
|
||||
{0, 0}
|
||||
};
|
||||
|
||||
bool PackageManager::registerScriptBindings() {
|
||||
Kernel *pKernel = Kernel::getInstance();
|
||||
assert(pKernel);
|
||||
ScriptEngine *pScript = pKernel->getScript();
|
||||
assert(pScript);
|
||||
lua_State *L = static_cast<lua_State *>(pScript->getScriptObject());
|
||||
assert(L);
|
||||
|
||||
if (!LuaBindhelper::addFunctionsToLib(L, PACKAGE_LIBRARY_NAME, PACKAGE_FUNCTIONS))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // End of namespace Sword25
|
||||
Reference in New Issue
Block a user