diff options
Diffstat (limited to 'engines')
-rw-r--r-- | engines/sword25/util/lua/lauxlib.cpp | 39 | ||||
-rw-r--r-- | engines/sword25/util/lua/scummvm_file.cpp | 93 | ||||
-rw-r--r-- | engines/sword25/util/lua/scummvm_file.h | 49 |
3 files changed, 165 insertions, 16 deletions
diff --git a/engines/sword25/util/lua/lauxlib.cpp b/engines/sword25/util/lua/lauxlib.cpp index ce61827e0f..3b54cf98f1 100644 --- a/engines/sword25/util/lua/lauxlib.cpp +++ b/engines/sword25/util/lua/lauxlib.cpp @@ -22,7 +22,8 @@ #include "lua.h" #include "lauxlib.h" - +#include "scummvm_file.h" +#include "common\textconsole.h" #define FREELIST_REF 0 /* free list of references */ @@ -520,7 +521,7 @@ LUALIB_API void luaL_unref (lua_State *L, int t, int ref) { typedef struct LoadF { int extraline; - FILE *f; + Sword25::Sword25FileProxy *f; char buff[LUAL_BUFFERSIZE]; } LoadF; @@ -533,8 +534,8 @@ static const char *getF (lua_State *L, void *ud, size_t *size) { *size = 1; return "\n"; } - if (feof(lf->f)) return NULL; - *size = fread(lf->buff, 1, sizeof(lf->buff), lf->f); + if (lf->f->eof()) return NULL; + *size = lf->f->read(lf->buff, 1, sizeof(lf->buff)); return (*size > 0) ? lf->buff : NULL; } @@ -551,9 +552,13 @@ static int errfile (lua_State *L, const char *what, int fnameindex) { LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) { LoadF lf; int status, readstatus; - int c; +// int c; int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */ lf.extraline = 0; + + lua_pushfstring(L, "@%s", filename); + lf.f = new Sword25::Sword25FileProxy(filename, "r"); +/* if (filename == NULL) { lua_pushliteral(L, "=stdin"); lf.f = stdin; @@ -563,23 +568,25 @@ LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) { lf.f = fopen(filename, "r"); if (lf.f == NULL) return errfile(L, "open", fnameindex); } - c = getc(lf.f); - if (c == '#') { /* Unix exec. file? */ + + c = lf.f->getc(); + if (c == '#') { // Unix exec. file? lf.extraline = 1; - while ((c = getc(lf.f)) != EOF && c != '\n') ; /* skip first line */ - if (c == '\n') c = getc(lf.f); + while ((c = lf.f->getc()) != EOF && c != '\n') ; // skip first line + if (c == '\n') c = lf.f->getc(); } - if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */ - lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */ + if (c == LUA_SIGNATURE[0] && filename) { // binary file? + lf.f = freopen(filename, "rb", lf.f); // reopen in binary mode if (lf.f == NULL) return errfile(L, "reopen", fnameindex); - /* skip eventual `#!...' */ - while ((c = getc(lf.f)) != EOF && c != LUA_SIGNATURE[0]) ; + // skip eventual `#!...' + while ((c = lf.f->getc()) != EOF && c != LUA_SIGNATURE[0]) ; lf.extraline = 0; } ungetc(c, lf.f); +*/ status = lua_load(L, getF, &lf, lua_tostring(L, -1)); - readstatus = ferror(lf.f); - if (filename) fclose(lf.f); /* close file (even in case of errors) */ + readstatus = 0; //ferror(lf.f); + if (filename) delete lf.f; // close file (even in case of errors) if (readstatus) { lua_settop(L, fnameindex); /* ignore results from `lua_load' */ return errfile(L, "read", fnameindex); @@ -637,7 +644,7 @@ static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { static int panic (lua_State *L) { (void)L; /* to avoid warnings */ - fprintf(stderr, "PANIC: unprotected error in call to Lua API (%s)\n", + warning("PANIC: unprotected error in call to Lua API (%s)\n", lua_tostring(L, -1)); return 0; } diff --git a/engines/sword25/util/lua/scummvm_file.cpp b/engines/sword25/util/lua/scummvm_file.cpp new file mode 100644 index 0000000000..bb0da64bab --- /dev/null +++ b/engines/sword25/util/lua/scummvm_file.cpp @@ -0,0 +1,93 @@ +/* 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. + * + */ + +#include "sword25/util/lua/scummvm_file.h" +#include "common/config-manager.h" +#include "common/util.h" + +namespace Sword25 { + +Sword25FileProxy::Sword25FileProxy(const Common::String &filename, const Common::String &mode) { + assert(filename.contains("config.lua")); + if (mode == "r") + setupConfigFile(); +} + +void Sword25FileProxy::setupConfigFile() { + float sfxVolume = ConfMan.hasKey("sfx_volume") ? 1.0 : 1.0 * ConfMan.getInt("sfx_volume") / 255.0; + float musicVolume = ConfMan.hasKey("music_volume") ? 0.5 : 1.0 * ConfMan.getInt("music_volume") / 255.0; + float speechVolume = ConfMan.hasKey("speech_volume") ? 1.0 : 1.0 * ConfMan.getInt("speech_volume") / 255.0; + bool subtitles = ConfMan.hasKey("subtitles") ? true : ConfMan.getBool("subtitles"); + + _readData = Common::String::format( +"GAME_LANGUAGE = \"%s\"\r\n\ +GAME_SUBTITLES = %s\r\n\ +MAX_MEMORY_USAGE = 256000000\r\n\ +GFX_VSYNC_ACTIVE = true\r\n\ +SFX_SAMPLING_RATE = 44100\r\n\ +SFX_CHANNEL_COUNT = 32\r\n\ +SFX_SOUND_VOLUME = %f\r\n\ +SFX_MUSIC_VOLUME = %f\r\n\ +SFX_SPEECH_VOLUME = %f\r\n", + getLanguage().c_str(), subtitles ? "true" : "false", sfxVolume, musicVolume, speechVolume); + + _readPos = 0; +} + +/** + * Get the language code used by the game for each language it supports + */ +Common::String Sword25FileProxy::getLanguage() { + Common::Language lang = Common::parseLanguage(ConfMan.get("language")); + switch (lang) { + case Common::EN_ANY: + return "en"; + case Common::DE_DEU: + return "de"; + case Common::ES_ESP: + return "es"; + case Common::FR_FRA: + return "fr"; + case Common::HU_HUN: + return "hr"; + case Common::IT_ITA: + return "it"; + case Common::PL_POL: + return "pl"; + case Common::PT_BRA: + return "pt"; + case Common::RU_RUS: + return "ru"; + default: + error("Unknown language '%s' encountered", ConfMan.get("language")); + break; + } +} + +size_t Sword25FileProxy::read(void *ptr, size_t size, size_t count) { + size_t bytesRead = MIN(_readData.size() - _readPos, size * count); + memmove(ptr, &_readData.c_str()[_readPos], bytesRead); + _readPos += bytesRead; + return bytesRead / size; +} + +} // End of namespace Sword25 diff --git a/engines/sword25/util/lua/scummvm_file.h b/engines/sword25/util/lua/scummvm_file.h new file mode 100644 index 0000000000..8ecfd34498 --- /dev/null +++ b/engines/sword25/util/lua/scummvm_file.h @@ -0,0 +1,49 @@ +/* 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. + * + */ + +#ifndef SWORD25_SCUMMVM_FILE_H +#define SWORD25_SCUMMVM_FILE_H + +#include "common/str.h" + +namespace Sword25 { + +/** + * The following class acts as a proxy interface to the I/O code, pretending that the ScummVM + * settings are a properly formatted 'config.lua' file + */ +class Sword25FileProxy { +private: + Common::String _readData; + uint _readPos; + + void setupConfigFile(); + Common::String getLanguage(); +public: + Sword25FileProxy(const Common::String &filename, const Common::String &mode); + bool eof() const { return _readPos >= _readData.size(); } + size_t read(void *ptr, size_t size, size_t count); +}; + +} // End of namespace Sword25 + +#endif |