249 lines
7.2 KiB
C++
249 lines
7.2 KiB
C++
/* 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
|
||
*
|
||
*/
|
||
|
||
// -----------------------------------------------------------------------------
|
||
// Includes
|
||
// -----------------------------------------------------------------------------
|
||
|
||
#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 BS_PackageManager *GetPM() {
|
||
BS_Kernel *pKernel = BS_Kernel::GetInstance();
|
||
BS_ASSERT(pKernel);
|
||
BS_PackageManager *pPM = static_cast<BS_PackageManager *>(pKernel->GetService("package"));
|
||
BS_ASSERT(pPM);
|
||
return pPM;
|
||
}
|
||
|
||
// -----------------------------------------------------------------------------
|
||
|
||
static int LoadPackage(lua_State *L) {
|
||
BS_PackageManager *pPM = GetPM();
|
||
|
||
lua_pushbooleancpp(L, pPM->LoadPackage(luaL_checkstring(L, 1), luaL_checkstring(L, 2)));
|
||
|
||
return 1;
|
||
}
|
||
|
||
// -----------------------------------------------------------------------------
|
||
|
||
static int LoadDirectoryAsPackage(lua_State *L) {
|
||
BS_PackageManager *pPM = GetPM();
|
||
|
||
lua_pushbooleancpp(L, pPM->LoadDirectoryAsPackage(luaL_checkstring(L, 1), luaL_checkstring(L, 2)));
|
||
|
||
return 1;
|
||
}
|
||
|
||
// -----------------------------------------------------------------------------
|
||
|
||
static int GetCurrentDirectory(lua_State *L) {
|
||
BS_PackageManager *pPM = GetPM();
|
||
|
||
lua_pushstring(L, pPM->GetCurrentDirectory().c_str());
|
||
|
||
return 1;
|
||
}
|
||
|
||
// -----------------------------------------------------------------------------
|
||
|
||
static int ChangeDirectory(lua_State *L) {
|
||
BS_PackageManager *pPM = GetPM();
|
||
|
||
lua_pushbooleancpp(L, pPM->ChangeDirectory(luaL_checkstring(L, 1)));
|
||
|
||
return 1;
|
||
}
|
||
|
||
// -----------------------------------------------------------------------------
|
||
|
||
static int GetAbsolutePath(lua_State *L) {
|
||
BS_PackageManager *pPM = GetPM();
|
||
|
||
lua_pushstring(L, pPM->GetAbsolutePath(luaL_checkstring(L, 1)).c_str());
|
||
|
||
return 1;
|
||
}
|
||
|
||
// -----------------------------------------------------------------------------
|
||
|
||
static int GetFileSize(lua_State *L) {
|
||
BS_PackageManager *pPM = GetPM();
|
||
|
||
lua_pushnumber(L, pPM->GetFileSize(luaL_checkstring(L, 1)));
|
||
|
||
return 1;
|
||
}
|
||
|
||
// -----------------------------------------------------------------------------
|
||
|
||
static int GetFileType(lua_State *L) {
|
||
BS_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, unsigned int Type) {
|
||
BS_PackageManager *pPM = GetPM();
|
||
|
||
// Der Packagemanager-Service muss den Suchstring und den Pfad getrennt <20>bergeben bekommen.
|
||
// Um die Benutzbarkeit zu verbessern sollen Skriptprogrammierer dieses als ein Pfad <20>bergeben k<>nnen.
|
||
// Daher muss der <20>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<68>hren und die Namen aller gefundenen Dateien in die Ergebnistabelle einf<6E>gen.
|
||
// Als Indizes werden fortlaufende Nummern verwandt.
|
||
unsigned int ResultNr = 1;
|
||
BS_PackageManager::FileSearch *pFS = pPM->CreateSearch(Filter, Directory, Type);
|
||
if (pFS) {
|
||
do {
|
||
lua_pushnumber(L, ResultNr);
|
||
lua_pushstring(L, pFS->GetCurFileName().c_str());
|
||
lua_settable(L, -3);
|
||
ResultNr++;
|
||
} while (pFS->NextFile());
|
||
}
|
||
|
||
delete(pFS);
|
||
}
|
||
|
||
// -----------------------------------------------------------------------------
|
||
|
||
static int FindFiles(lua_State *L) {
|
||
DoSearch(L, luaL_checkstring(L, 1), BS_PackageManager::FT_FILE);
|
||
return 1;
|
||
}
|
||
|
||
// -----------------------------------------------------------------------------
|
||
|
||
static int FindDirectories(lua_State *L) {
|
||
DoSearch(L, luaL_checkstring(L, 1), BS_PackageManager::FT_DIRECTORY);
|
||
return 1;
|
||
}
|
||
|
||
// -----------------------------------------------------------------------------
|
||
|
||
static int GetFileAsString(lua_State *L) {
|
||
BS_PackageManager *pPM = GetPM();
|
||
|
||
unsigned int FileSize;
|
||
void *FileData = pPM->GetFile(luaL_checkstring(L, 1), &FileSize);
|
||
if (FileData) {
|
||
lua_pushlstring(L, static_cast<char *>(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 BS_PackageManager::_RegisterScriptBindings() {
|
||
BS_Kernel *pKernel = BS_Kernel::GetInstance();
|
||
BS_ASSERT(pKernel);
|
||
BS_ScriptEngine *pScript = static_cast<BS_ScriptEngine *>(pKernel->GetService("script"));
|
||
BS_ASSERT(pScript);
|
||
lua_State *L = static_cast<lua_State *>(pScript->GetScriptObject());
|
||
BS_ASSERT(L);
|
||
|
||
if (!BS_LuaBindhelper::AddFunctionsToLib(L, PACKAGE_LIBRARY_NAME, PACKAGE_FUNCTIONS)) return false;
|
||
|
||
return true;
|
||
}
|
||
|
||
} // End of namespace Sword25
|