2010-07-31 09:53:02 +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.
|
|
|
|
|
*
|
|
|
|
|
* 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$
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2010-08-06 13:13:25 +00:00
|
|
|
|
/*
|
2010-07-31 09:53:02 +00:00
|
|
|
|
* This code is based on Broken Sword 2.5 engine
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) Malte Thiesen, Daniel Queteschiner and Michael Elsdoerfer
|
|
|
|
|
*
|
|
|
|
|
* Licensed under GNU GPL v2
|
|
|
|
|
*
|
|
|
|
|
*/
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
// Includes
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
2010-07-29 19:55:28 +00:00
|
|
|
|
#include "sword25/kernel/common.h"
|
|
|
|
|
#include "sword25/kernel/kernel.h"
|
|
|
|
|
#include "sword25/script/script.h"
|
|
|
|
|
#include "sword25/script/luabindhelper.h"
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
2010-07-29 19:55:28 +00:00
|
|
|
|
#include "sword25/package/packagemanager.h"
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
2010-08-06 11:20:37 +00:00
|
|
|
|
namespace Sword25 {
|
|
|
|
|
|
|
|
|
|
using namespace Lua;
|
|
|
|
|
|
2010-07-29 19:53:02 +00:00
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
2010-08-06 11:20:37 +00:00
|
|
|
|
static BS_PackageManager *GetPM() {
|
2010-08-06 13:13:25 +00:00
|
|
|
|
BS_Kernel *pKernel = BS_Kernel::GetInstance();
|
2010-07-29 19:53:02 +00:00
|
|
|
|
BS_ASSERT(pKernel);
|
2010-08-06 11:20:37 +00:00
|
|
|
|
BS_PackageManager *pPM = static_cast<BS_PackageManager *>(pKernel->GetService("package"));
|
2010-07-29 19:53:02 +00:00
|
|
|
|
BS_ASSERT(pPM);
|
|
|
|
|
return pPM;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
2010-08-06 11:20:37 +00:00
|
|
|
|
static int LoadPackage(lua_State *L) {
|
|
|
|
|
BS_PackageManager *pPM = GetPM();
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
|
|
|
|
lua_pushbooleancpp(L, pPM->LoadPackage(luaL_checkstring(L, 1), luaL_checkstring(L, 2)));
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
2010-08-06 11:20:37 +00:00
|
|
|
|
static int LoadDirectoryAsPackage(lua_State *L) {
|
|
|
|
|
BS_PackageManager *pPM = GetPM();
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
|
|
|
|
lua_pushbooleancpp(L, pPM->LoadDirectoryAsPackage(luaL_checkstring(L, 1), luaL_checkstring(L, 2)));
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
2010-08-06 11:20:37 +00:00
|
|
|
|
static int GetCurrentDirectory(lua_State *L) {
|
|
|
|
|
BS_PackageManager *pPM = GetPM();
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
|
|
|
|
lua_pushstring(L, pPM->GetCurrentDirectory().c_str());
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
2010-08-06 11:20:37 +00:00
|
|
|
|
static int ChangeDirectory(lua_State *L) {
|
|
|
|
|
BS_PackageManager *pPM = GetPM();
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
|
|
|
|
lua_pushbooleancpp(L, pPM->ChangeDirectory(luaL_checkstring(L, 1)));
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
2010-08-06 11:20:37 +00:00
|
|
|
|
static int GetAbsolutePath(lua_State *L) {
|
|
|
|
|
BS_PackageManager *pPM = GetPM();
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
|
|
|
|
lua_pushstring(L, pPM->GetAbsolutePath(luaL_checkstring(L, 1)).c_str());
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
2010-08-06 11:20:37 +00:00
|
|
|
|
static int GetFileSize(lua_State *L) {
|
|
|
|
|
BS_PackageManager *pPM = GetPM();
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
|
|
|
|
lua_pushnumber(L, pPM->GetFileSize(luaL_checkstring(L, 1)));
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
2010-08-06 11:20:37 +00:00
|
|
|
|
static int GetFileType(lua_State *L) {
|
|
|
|
|
BS_PackageManager *pPM = GetPM();
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
|
|
|
|
lua_pushnumber(L, pPM->GetFileType(luaL_checkstring(L, 1)));
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
2010-08-06 11:20:37 +00:00
|
|
|
|
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;
|
|
|
|
|
|
2010-08-06 13:13:25 +00:00
|
|
|
|
if (lastSlash >= sPath) {
|
2010-07-29 19:53:02 +00:00
|
|
|
|
Directory = "";
|
|
|
|
|
Filter = Path;
|
2010-08-06 11:20:37 +00:00
|
|
|
|
} else {
|
|
|
|
|
Directory = Common::String(sPath, lastSlash - sPath);
|
|
|
|
|
Filter = Common::String(lastSlash + 1);
|
2010-07-29 19:53:02 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
2010-08-06 11:20:37 +00:00
|
|
|
|
static void DoSearch(lua_State *L, const Common::String &Path, unsigned int Type) {
|
|
|
|
|
BS_PackageManager *pPM = GetPM();
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
|
|
|
|
// 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.
|
2010-08-06 11:20:37 +00:00
|
|
|
|
Common::String Directory;
|
|
|
|
|
Common::String Filter;
|
2010-07-29 19:53:02 +00:00
|
|
|
|
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;
|
2010-08-06 13:13:25 +00:00
|
|
|
|
BS_PackageManager::FileSearch *pFS = pPM->CreateSearch(Filter, Directory, Type);
|
2010-08-06 11:20:37 +00:00
|
|
|
|
if (pFS) {
|
|
|
|
|
do {
|
2010-07-29 19:53:02 +00:00
|
|
|
|
lua_pushnumber(L, ResultNr);
|
|
|
|
|
lua_pushstring(L, pFS->GetCurFileName().c_str());
|
|
|
|
|
lua_settable(L, -3);
|
|
|
|
|
ResultNr++;
|
2010-08-06 11:20:37 +00:00
|
|
|
|
} while (pFS->NextFile());
|
2010-07-29 19:53:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
delete(pFS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
2010-08-06 11:20:37 +00:00
|
|
|
|
static int FindFiles(lua_State *L) {
|
2010-07-29 19:53:02 +00:00
|
|
|
|
DoSearch(L, luaL_checkstring(L, 1), BS_PackageManager::FT_FILE);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
2010-08-06 11:20:37 +00:00
|
|
|
|
static int FindDirectories(lua_State *L) {
|
2010-07-29 19:53:02 +00:00
|
|
|
|
DoSearch(L, luaL_checkstring(L, 1), BS_PackageManager::FT_DIRECTORY);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
2010-08-06 11:20:37 +00:00
|
|
|
|
static int GetFileAsString(lua_State *L) {
|
|
|
|
|
BS_PackageManager *pPM = GetPM();
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
|
|
|
|
unsigned int FileSize;
|
2010-08-06 13:13:25 +00:00
|
|
|
|
void *FileData = pPM->GetFile(luaL_checkstring(L, 1), &FileSize);
|
2010-08-06 11:20:37 +00:00
|
|
|
|
if (FileData) {
|
2010-07-29 19:53:02 +00:00
|
|
|
|
lua_pushlstring(L, static_cast<char *>(FileData), FileSize);
|
|
|
|
|
delete FileData;
|
|
|
|
|
|
|
|
|
|
return 1;
|
2010-08-06 11:20:37 +00:00
|
|
|
|
} else
|
2010-07-29 19:53:02 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
2010-08-06 11:20:37 +00:00
|
|
|
|
static int FileExists(lua_State *L) {
|
2010-07-29 19:53:02 +00:00
|
|
|
|
lua_pushbooleancpp(L, GetPM()->FileExists(luaL_checkstring(L, 1)));
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
2010-08-06 13:13:25 +00:00
|
|
|
|
static const char *PACKAGE_LIBRARY_NAME = "Package";
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
2010-08-06 11:20:37 +00:00
|
|
|
|
static const luaL_reg PACKAGE_FUNCTIONS[] = {
|
2010-08-10 12:16:31 +00:00
|
|
|
|
{"LoadPackage", LoadPackage},
|
|
|
|
|
{"LoadDirectoryAsPackage", LoadDirectoryAsPackage},
|
|
|
|
|
{"GetCurrentDirectory", GetCurrentDirectory},
|
|
|
|
|
{"ChangeDirectory", ChangeDirectory},
|
|
|
|
|
{"GetAbsolutePath", GetAbsolutePath},
|
|
|
|
|
{"GetFileSize", GetFileSize},
|
|
|
|
|
{"GetFileType", GetFileType},
|
|
|
|
|
{"FindFiles", FindFiles},
|
|
|
|
|
{"FindDirectories", FindDirectories},
|
|
|
|
|
{"GetFileAsString", GetFileAsString},
|
|
|
|
|
{"FileExists", FileExists},
|
|
|
|
|
{0, 0}
|
2010-07-29 19:53:02 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
2010-08-06 11:20:37 +00:00
|
|
|
|
bool BS_PackageManager::_RegisterScriptBindings() {
|
2010-08-06 13:13:25 +00:00
|
|
|
|
BS_Kernel *pKernel = BS_Kernel::GetInstance();
|
2010-07-29 19:53:02 +00:00
|
|
|
|
BS_ASSERT(pKernel);
|
2010-08-06 13:13:25 +00:00
|
|
|
|
BS_ScriptEngine *pScript = static_cast<BS_ScriptEngine *>(pKernel->GetService("script"));
|
2010-07-29 19:53:02 +00:00
|
|
|
|
BS_ASSERT(pScript);
|
2010-08-06 11:20:37 +00:00
|
|
|
|
lua_State *L = static_cast<lua_State *>(pScript->GetScriptObject());
|
2010-07-29 19:53:02 +00:00
|
|
|
|
BS_ASSERT(L);
|
|
|
|
|
|
|
|
|
|
if (!BS_LuaBindhelper::AddFunctionsToLib(L, PACKAGE_LIBRARY_NAME, PACKAGE_FUNCTIONS)) return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2010-08-06 11:20:37 +00:00
|
|
|
|
|
|
|
|
|
} // End of namespace Sword25
|