diff options
author | Nipun Garg | 2019-07-09 02:13:04 +0530 |
---|---|---|
committer | Eugene Sandulenko | 2019-09-03 17:17:15 +0200 |
commit | b7b8b23d295e8701b69a4bea27892a62aab988c7 (patch) | |
tree | f571e660b525a0419e0c072bebf984839034e95d /engines | |
parent | 6e60d6a287e3bea956cc607f2f2ebfc5b3d11829 (diff) | |
download | scummvm-rg350-b7b8b23d295e8701b69a4bea27892a62aab988c7.tar.gz scummvm-rg350-b7b8b23d295e8701b69a4bea27892a62aab988c7.tar.bz2 scummvm-rg350-b7b8b23d295e8701b69a4bea27892a62aab988c7.zip |
HDB: Add openFile, write, closeFile Lua functions
Diffstat (limited to 'engines')
-rw-r--r-- | engines/hdb/hdb.cpp | 5 | ||||
-rw-r--r-- | engines/hdb/lua-script.cpp | 69 | ||||
-rw-r--r-- | engines/hdb/lua-script.h | 2 |
3 files changed, 65 insertions, 11 deletions
diff --git a/engines/hdb/hdb.cpp b/engines/hdb/hdb.cpp index 3259241517..d97e127208 100644 --- a/engines/hdb/hdb.cpp +++ b/engines/hdb/hdb.cpp @@ -435,7 +435,6 @@ bool HDBGame::saveSlot(int slot) { Common::OutSaveFile *out; Common::String saveFileName = Common::String::format("%s.%03d", _targetName.c_str(), slot); - Common::String saveLuaName = Common::String::format("%s.l.%03d", _targetName.c_str(), slot); if (!(out = _saveFileMan->openForSaving(saveFileName))) error("Unable to open save file"); @@ -444,11 +443,11 @@ bool HDBGame::saveSlot(int slot) { // Actual Save Data saveGame(out); - _lua->save(out, saveLuaName.c_str()); + _lua->save(out, _targetName.c_str(), slot); out->finalize(); if (out->err()) - warning("Can't wrtie file '%s'. (Disk full?)", saveFileName.c_str()); + warning("Can't write file '%s'. (Disk full?)", saveFileName.c_str()); delete out; diff --git a/engines/hdb/lua-script.cpp b/engines/hdb/lua-script.cpp index bef9c05c0f..0a1b3118c0 100644 --- a/engines/hdb/lua-script.cpp +++ b/engines/hdb/lua-script.cpp @@ -41,6 +41,9 @@ struct ScriptPatch { {"GLOBAL.LUA", "if( getglobal( \"map\"..tostring(v1)..\"_complete\" ) ) then", "if( _G[\"map\"..tostring(v1)..\"_complete\"] ) then"}, {"GLOBAL.LUA", "closefunc = getglobal( npcdef.codename..\"_use\" )", "closefunc = _G[npcdef.codename..\"_use\"]"}, {"GLOBAL.LUA", "strsub(", "string.sub("}, // line 15 + {"GLOBAL.LUA", "for i,v in globals() do", "for i,v in pairs(_G) do"}, + {"GLOBAL.LUA", "return gsub( s, \"\\n\", \"\\\\\\n\" )", "return string.gsub( s, \"\\n\", \"\\\\\\n\" )"}, + {"GLOBAL.LUA", "for i,v in t do", "for i,v in pairs(t) do"}, {"MAP00.LUA", "tempfunc = function() emptybed_use( %x, %y, %v1, %v2 ) end", "tempfunc = function() emptybed_use(x, y, v1, v2) end"}, {"MAP00.LUA", "if( getn( beds ) == 0 ) then", "if( #beds == 0 ) then"}, {"MAP01.LUA", "if( covert_index < getn(covert_dialog) ) then", "if( covert_index < #covert_dialog ) then"}, @@ -144,7 +147,7 @@ void LuaScript::purgeGlobals() { _globals.clear(); } -void LuaScript::save(Common::OutSaveFile *out, const char *fName) { +void LuaScript::save(Common::OutSaveFile *out, const char *targetName, int slot) { out->writeUint32LE(_globals.size()); // Save Globals @@ -155,14 +158,11 @@ void LuaScript::save(Common::OutSaveFile *out, const char *fName) { out->write(_globals[i]->string, 32); } - // Delete Lua Save File - Common::InSaveFile *inLua = g_system->getSavefileManager()->openForLoading(fName); - if (inLua) - delete inLua; - + Common::String saveLuaName = Common::String::format("%s.l.%03d", targetName, slot); + lua_printstack(_state); lua_getglobal(_state, "SaveState"); - lua_pushstring(_state, fName); + lua_pushstring(_state, saveLuaName.c_str()); lua_call(_state, 1, 0); } @@ -1191,6 +1191,57 @@ static int playVoice(lua_State *L) { return 0; } +static int openFile(lua_State *L) { + + const char *fName = lua_tostring(L, 1); + const char *mode = lua_tostring(L, 2); + + g_hdb->_lua->checkParameters("openFile", 2); + + lua_pop(L, 2); + + if (!scumm_stricmp(mode, "wt")) { + // Delete Lua Save File + Common::InSaveFile *inLua = g_system->getSavefileManager()->openForLoading(fName); + if (inLua) + delete inLua; + + Common::OutSaveFile *outLua = g_system->getSavefileManager()->openForSaving(fName); + if (!outLua) + error("Cannot open %s", fName); + lua_pushlightuserdata(L, outLua); + } + + return 1; +} + +static int write(lua_State *L) { + Common::OutSaveFile *out = (Common::OutSaveFile *)lua_topointer(L, 1); + const char *data = lua_tostring(L, 2); + + g_hdb->_lua->checkParameters("write", 2); + + lua_pop(L, 2); + + out->write(data, strlen(data)); + + return 0; +} + +static int closeFile(lua_State *L) { + Common::OutSaveFile *out = (Common::OutSaveFile *)lua_topointer(L, 1); + + g_hdb->_lua->checkParameters("closeFile", 1); + + lua_pop(L, 1); + + out->finalize(); + + delete out; + + return 0; +} + /* Lua Initialization Code */ @@ -1490,6 +1541,10 @@ struct FuncInit { { "Cine_TextOut", cineTextOut }, { "Cine_CenterTextOut", cineCenterTextOut }, { "Cine_PlayVoice", cinePlayVoice }, + + { "openfile", openFile, }, + { "write", write, }, + { "closefile", closeFile, }, { NULL, NULL } }; diff --git a/engines/hdb/lua-script.h b/engines/hdb/lua-script.h index 22168a94b6..ff061afbc5 100644 --- a/engines/hdb/lua-script.h +++ b/engines/hdb/lua-script.h @@ -59,7 +59,7 @@ public: void saveGlobalString(const char *global, const char *string); void loadGlobal(const char *global); void purgeGlobals(); - void save(Common::OutSaveFile *out, const char *fName); + void save(Common::OutSaveFile *out, const char *targetName, int slot); void loadSaveFile(Common::InSaveFile *in, const char *fName); bool init(); |