aboutsummaryrefslogtreecommitdiff
path: root/engines/sword25/package
diff options
context:
space:
mode:
authorJohannes Schickel2010-10-13 03:57:44 +0000
committerJohannes Schickel2010-10-13 03:57:44 +0000
commit75e8452b6e6a2bf4fb2f588aa00b428a60d873b5 (patch)
treef29541d55309487a94bd1d38e8b53bb3dde9aec6 /engines/sword25/package
parent48ee83b88957dab86bc763e9ef21a70179fa8679 (diff)
parente9f50882ea5b6beeefa994040be9d3bab6a1f107 (diff)
downloadscummvm-rg350-75e8452b6e6a2bf4fb2f588aa00b428a60d873b5.tar.gz
scummvm-rg350-75e8452b6e6a2bf4fb2f588aa00b428a60d873b5.tar.bz2
scummvm-rg350-75e8452b6e6a2bf4fb2f588aa00b428a60d873b5.zip
OPENGL: Merged from trunk, from rev 52105 to 53396.
This includes an rather hacky attempt to merge all the recent gp2x backend changes into the branch. I suppose the gp2x backend and probably all new backends, i.e. gph, dingux etc., might not compile anymore. Since I have no way of testing those it would be nice if porters could look into getting those up to speed in this branch. svn-id: r53399
Diffstat (limited to 'engines/sword25/package')
-rw-r--r--engines/sword25/package/packagemanager.cpp293
-rw-r--r--engines/sword25/package/packagemanager.h222
-rw-r--r--engines/sword25/package/packagemanager_script.cpp215
3 files changed, 730 insertions, 0 deletions
diff --git a/engines/sword25/package/packagemanager.cpp b/engines/sword25/package/packagemanager.cpp
new file mode 100644
index 0000000000..063844f3ba
--- /dev/null
+++ b/engines/sword25/package/packagemanager.cpp
@@ -0,0 +1,293 @@
+/* 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 2
+ * 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, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * $URL$
+ * $Id$
+ *
+ */
+
+/*
+ * This code is based on Broken Sword 2.5 engine
+ *
+ * Copyright (c) Malte Thiesen, Daniel Queteschiner and Michael Elsdoerfer
+ *
+ * Licensed under GNU GPL v2
+ *
+ */
+
+#define BS_LOG_PREFIX "PACKAGEMANAGER"
+
+#include "common/archive.h"
+#include "common/config-manager.h"
+#include "common/savefile.h"
+#include "common/str-array.h"
+#include "common/system.h"
+#include "common/unzip.h"
+#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 &currentDirectory) {
+ 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.get("path")) {
+ if (!registerScriptBindings())
+ BS_LOG_ERRORLN("Script bindings could not be registered.");
+ else
+ BS_LOGLN("Script bindings registered.");
+}
+
+PackageManager::~PackageManager() {
+ // Free the package list
+ Common::List<ArchiveEntry *>::iterator i;
+ for (i = _archiveList.begin(); i != _archiveList.end(); ++i)
+ delete *i;
+
+}
+
+Service *PackageManager_CreateObject(Kernel *kernelPtr) {
+ return new PackageManager(kernelPtr);
+}
+
+/**
+ * Scans through the archive list for a specified file
+ */
+Common::ArchiveMemberPtr PackageManager::getArchiveMember(const Common::String &fileName) {
+ // Loop through checking each archive
+ Common::List<ArchiveEntry *>::iterator i;
+ for (i = _archiveList.begin(); i != _archiveList.end(); ++i) {
+ if (!fileName.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::String resPath(&fileName.c_str()[(*i)->_mountPath.size()]);
+
+ if (archiveFolder->hasFile(resPath)) {
+ return archiveFolder->getMember(resPath);
+ }
+ }
+
+ 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());
+
+ Common::Archive *zipFile = Common::makeZipArchive(fileName);
+ if (zipFile == NULL) {
+ BS_LOG_ERRORLN("Unable to mount file \"%s\" to \"%s\"", fileName.c_str(), mountPosition.c_str());
+ return false;
+ } else {
+ BS_LOGLN("Package '%s' mounted as '%s'.", fileName.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::String &directoryName, const Common::String &mountPosition) {
+ Common::FSNode directory(directoryName);
+ Common::Archive *folderArchive = new Common::FSDirectory(directory, 6);
+ if (!directory.exists() || (folderArchive == NULL)) {
+ BS_LOG_ERRORLN("Unable to mount directory \"%s\" to \"%s\".", directoryName.c_str(), mountPosition.c_str());
+ return false;
+ } else {
+ BS_LOGLN("Directory '%s' mounted as '%s'.", directoryName.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::GetInstance().GetPathFilename(fileName));
+ if (!file) {
+ BS_LOG_ERRORLN("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;
+
+ 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;
+}
+
+Common::String PackageManager::getCurrentDirectory() {
+ return _currentDirectory;
+}
+
+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(fileName, _currentDirectory);
+}
+
+uint PackageManager::getFileSize(const Common::String &fileName) {
+ Common::SeekableReadStream *in;
+ Common::ArchiveMemberPtr fileNode = getArchiveMember(normalizePath(fileName, _currentDirectory));
+ if (!fileNode)
+ return 0;
+ if (!(in = fileNode->createReadStream()))
+ return 0;
+
+ uint fileSize = in->size();
+
+ return fileSize;
+}
+
+uint PackageManager::getFileType(const Common::String &fileName) {
+ warning("STUB: BS_PackageManager::GetFileType(%s)", fileName.c_str());
+
+ //return fileNode.isDirectory() ? BS_PackageManager::FT_DIRECTORY : BS_PackageManager::FT_FILE;
+ return PackageManager::FT_FILE;
+}
+
+bool PackageManager::fileExists(const Common::String &fileName) {
+ Common::ArchiveMemberPtr fileNode = getArchiveMember(normalizePath(fileName, _currentDirectory));
+ return fileNode;
+}
+
+int PackageManager::doSearch(Common::ArchiveMemberList &list, const Common::String &filter, const Common::String &path, uint typeFilter) {
+ Common::String normalizedFilter = normalizePath(filter, _currentDirectory);
+ int num = 0;
+
+ if (path.size() > 0)
+ warning("STUB: 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::String 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) {
+ if (((typeFilter & PackageManager::FT_DIRECTORY) && (*it)->getName().hasSuffix("/")) ||
+ ((typeFilter & PackageManager::FT_FILE) && !(*it)->getName().hasSuffix("/"))) {
+
+ // Do not add duplicate files
+ bool found = false;
+ for (Common::ArchiveMemberList::iterator it1 = list.begin(); it1 != list.end(); ++it1) {
+ if ((*it1)->getName() == (*it)->getName()) {
+ found = true;
+ break;
+ }
+ }
+
+ if (!found)
+ list.push_back(*it);
+ num++;
+ }
+ }
+ }
+
+ return num;
+}
+
+} // End of namespace Sword25
diff --git a/engines/sword25/package/packagemanager.h b/engines/sword25/package/packagemanager.h
new file mode 100644
index 0000000000..96f136dd83
--- /dev/null
+++ b/engines/sword25/package/packagemanager.h
@@ -0,0 +1,222 @@
+/* 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 2
+ * 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, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * $URL$
+ * $Id$
+ *
+ */
+
+/*
+ * 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 seperate 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 seperate 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;
+
+ Common::ArchiveMemberPtr getArchiveMember(const Common::String &fileName);
+
+public:
+ PackageManager(Kernel *pKernel);
+ ~PackageManager();
+
+ enum FILE_TYPES {
+ FT_DIRECTORY = (1 << 0),
+ FT_FILE = (1 << 1)
+ };
+
+ /**
+ * 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::String &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);
+ /**
+ * 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);
+ char *result = (char *)malloc(fileSize + strlen(versionStr) + 1);
+ strcpy(result, versionStr);
+ Common::copy(data, data + fileSize, result + strlen(versionStr));
+ result[fileSize + strlen(versionStr)] = '\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();
+ /**
+ * 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);
+
+ /**
+ * Returns a file's size
+ * @param FileName The filename
+ * @return The file size. If an error occurs, then 0xffffffff is returned.
+ * @remarks For files in packages, then uncompressed size is returned.
+ **/
+ uint getFileSize(const Common::String &fileName);
+
+ /**
+ * Returns the type of a file.
+ * @param FileName The filename
+ * @return Returns the file type, either (BS_PackageManager::FT_DIRECTORY
+ * or BS_PackageManager::FT_FILE).
+ * If the file was not found, then 0 is returned.
+ */
+ uint getFileType(const Common::String &fileName);
+
+ /**
+ * 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
diff --git a/engines/sword25/package/packagemanager_script.cpp b/engines/sword25/package/packagemanager_script.cpp
new file mode 100644
index 0000000000..cfcea55944
--- /dev/null
+++ b/engines/sword25/package/packagemanager_script.cpp
@@ -0,0 +1,215 @@
+/* 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 2
+ * 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, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * $URL$
+ * $Id$
+ *
+ */
+
+/*
+ * 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 {
+
+using namespace Lua;
+
+static PackageManager *getPM() {
+ Kernel *pKernel = Kernel::GetInstance();
+ BS_ASSERT(pKernel);
+ PackageManager *pPM = static_cast<PackageManager *>(pKernel->GetService("package"));
+ BS_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 int getCurrentDirectory(lua_State *L) {
+ PackageManager *pPM = getPM();
+
+ lua_pushstring(L, pPM->getCurrentDirectory().c_str());
+
+ return 1;
+}
+
+static int changeDirectory(lua_State *L) {
+ PackageManager *pPM = getPM();
+
+ lua_pushbooleancpp(L, pPM->changeDirectory(luaL_checkstring(L, 1)));
+
+ return 1;
+}
+
+static int getAbsolutePath(lua_State *L) {
+ PackageManager *pPM = getPM();
+
+ lua_pushstring(L, pPM->getAbsolutePath(luaL_checkstring(L, 1)).c_str());
+
+ return 1;
+}
+
+static int getFileSize(lua_State *L) {
+ PackageManager *pPM = getPM();
+
+ lua_pushnumber(L, pPM->getFileSize(luaL_checkstring(L, 1)));
+
+ return 1;
+}
+
+static int getFileType(lua_State *L) {
+ PackageManager *pPM = getPM();
+
+ lua_pushnumber(L, pPM->getFileType(luaL_checkstring(L, 1)));
+
+ 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;
+}
+
+static const char *PACKAGE_LIBRARY_NAME = "Package";
+
+static const luaL_reg PACKAGE_FUNCTIONS[] = {
+ {"LoadPackage", loadPackage},
+ {"LoadDirectoryAsPackage", loadDirectoryAsPackage},
+ {"GetCurrentDirectory", getCurrentDirectory},
+ {"ChangeDirectory", changeDirectory},
+ {"GetAbsolutePath", getAbsolutePath},
+ {"GetFileSize", getFileSize},
+ {"GetFileType", getFileType},
+ {"FindFiles", findFiles},
+ {"FindDirectories", findDirectories},
+ {"GetFileAsString", getFileAsString},
+ {"FileExists", fileExists},
+ {0, 0}
+};
+
+bool PackageManager::registerScriptBindings() {
+ Kernel *pKernel = Kernel::GetInstance();
+ BS_ASSERT(pKernel);
+ ScriptEngine *pScript = static_cast<ScriptEngine *>(pKernel->GetService("script"));
+ BS_ASSERT(pScript);
+ lua_State *L = static_cast<lua_State *>(pScript->getScriptObject());
+ BS_ASSERT(L);
+
+ if (!LuaBindhelper::addFunctionsToLib(L, PACKAGE_LIBRARY_NAME, PACKAGE_FUNCTIONS))
+ return false;
+
+ return true;
+}
+
+} // End of namespace Sword25