diff options
author | Nipun Garg | 2019-06-06 18:51:53 +0530 |
---|---|---|
committer | Eugene Sandulenko | 2019-09-03 17:16:43 +0200 |
commit | 12a9036dd66bd31d77f99a4b63b60ac49eef9453 (patch) | |
tree | d78e94d3d8bb4d8fac15c537118cc3e206e04509 | |
parent | 869b109eeb8702c86e01266e7e23ba800e4cfcb6 (diff) | |
download | scummvm-rg350-12a9036dd66bd31d77f99a4b63b60ac49eef9453.tar.gz scummvm-rg350-12a9036dd66bd31d77f99a4b63b60ac49eef9453.tar.bz2 scummvm-rg350-12a9036dd66bd31d77f99a4b63b60ac49eef9453.zip |
HDB: Add scriptPatch and add in sanitizeScript
-rw-r--r-- | engines/hdb/hdb.cpp | 2 | ||||
-rw-r--r-- | engines/hdb/lua-script.cpp | 56 | ||||
-rw-r--r-- | engines/hdb/lua-script.h | 7 |
3 files changed, 48 insertions, 17 deletions
diff --git a/engines/hdb/hdb.cpp b/engines/hdb/hdb.cpp index 69b2f18742..f252723fd0 100644 --- a/engines/hdb/hdb.cpp +++ b/engines/hdb/hdb.cpp @@ -146,7 +146,7 @@ Common::Error HDBGame::run() { return Common::kReadingFailed; } - lua->initScript(luaStream, luaLength); + lua->initScript(luaStream, "MAP00_DEMO_LUA", luaLength); #if 0 lua->executeFile("test.lua"); diff --git a/engines/hdb/lua-script.cpp b/engines/hdb/lua-script.cpp index f5f00930ff..c39ae4a11a 100644 --- a/engines/hdb/lua-script.cpp +++ b/engines/hdb/lua-script.cpp @@ -29,6 +29,15 @@ namespace HDB { +struct ScriptPatch { + const char* scriptName; + const char* search; + const char* replace; +} scriptPatches[] = { + {"GLOBAL_LUA", "for i,npc in npcs do", "for i,npc in pairs(npcs) do"}, + {NULL, NULL, NULL}, +}; + LuaScript::LuaScript() { _state = NULL; _systemInit = false; @@ -40,7 +49,6 @@ LuaScript::~LuaScript() { } } - bool LuaScript::init() { // Load Global Lua Code _globalLuaStream = g_hdb->fileMan->findFirstData("GLOBAL_LUA", TYPE_BINARY); @@ -587,7 +595,7 @@ void debugHook(lua_State *L, lua_Debug *ar) { } -bool LuaScript::initScript(Common::SeekableReadStream *stream, int32 length) { +bool LuaScript::initScript(Common::SeekableReadStream *stream, const char *scriptName, int32 length) { if (_systemInit) { return false; @@ -674,14 +682,14 @@ bool LuaScript::initScript(Common::SeekableReadStream *stream, int32 length) { // Load GLOBAL_LUA and execute it - if (!executeMPC(_globalLuaStream, "global code", _globalLuaLength)) { + if (!executeMPC(_globalLuaStream, "global code", "GLOBAL_LUA", _globalLuaLength)) { error("LuaScript::initScript: 'global code' failed to execute"); return false; } // Load script and execute it - if (!executeMPC(stream, "level code", length)) { + if (!executeMPC(stream, "level code", scriptName, length)) { error("LuaScript::initScript: 'level code' failed to execute"); return false; } @@ -704,7 +712,7 @@ bool LuaScript::initScript(Common::SeekableReadStream *stream, int32 length) { } -bool LuaScript::executeMPC(Common::SeekableReadStream *stream, const char *name, int32 length) { +bool LuaScript::executeMPC(Common::SeekableReadStream *stream, const char *name, const char *scriptName, int32 length) { if (!_systemInit) { return false; @@ -713,13 +721,17 @@ bool LuaScript::executeMPC(Common::SeekableReadStream *stream, const char *name, char *chunk = new char[length]; stream->read((void *)chunk, length); + sanitizeScript(chunk); + /* - Remove C-style comments from the script - and update the upvalue syntax for Lua 5.1.3 + Remove C-style comments from the script + and update the upvalue syntax for Lua 5.1.3 */ - sanitizeScript(chunk); + Common::String chunkString(chunk); + + addPatches(chunkString, scriptName); - if (!executeChunk(chunk, length, name)) { + if (!executeChunk(chunkString, length, name)) { delete[] chunk; return false; @@ -748,7 +760,11 @@ bool LuaScript::executeFile(const Common::String &filename) { sanitizeScript(fileData); - if (!executeChunk(fileData, fileSize, filename)) { + Common::String fileDataString(fileData); + + addPatches(fileDataString, filename.c_str()); + + if (!executeChunk(fileDataString, fileSize, filename)) { delete[] fileData; delete file; @@ -761,14 +777,14 @@ bool LuaScript::executeFile(const Common::String &filename) { return true; } -bool LuaScript::executeChunk(const char *chunk, uint chunkSize, const Common::String &chunkName) const { +bool LuaScript::executeChunk(Common::String &chunk, uint chunkSize, const Common::String &chunkName) const { if (!_systemInit) { return false; } // Compile Chunk - if (luaL_loadbuffer(_state, chunk, chunkSize, chunkName.c_str())) { + if (luaL_loadbuffer(_state, chunk.c_str(), chunkSize, chunkName.c_str())) { error("Couldn't compile \"%s\": %s", chunkName.c_str(), lua_tostring(_state, -1)); lua_pop(_state, -1); @@ -799,10 +815,24 @@ void LuaScript::sanitizeScript(char *chunk) { while (chunk[offset] != 0x0d) { chunk[offset++] = ' '; } - } else if (chunk[offset] == '%' && chunk[offset] != ' ') { // Update the Upvalue syntax + } + else if (chunk[offset] == '%' && chunk[offset] != ' ') { // Update the Upvalue syntax chunk[offset] = ' '; } offset++; } } + +void LuaScript::addPatches(Common::String &chunk, const char* scriptName) { + ScriptPatch *patch = scriptPatches; + + while (patch->scriptName) { + if (scriptName == patch->scriptName) { + Common::String searchString(patch->search); + Common::String replaceString(patch->replace); + Common::replace(chunk, searchString, replaceString); + } + patch++; + } +} } diff --git a/engines/hdb/lua-script.h b/engines/hdb/lua-script.h index 5cdb9fb466..fae9502f20 100644 --- a/engines/hdb/lua-script.h +++ b/engines/hdb/lua-script.h @@ -38,8 +38,8 @@ public: ~LuaScript(); bool init(); - bool initScript(Common::SeekableReadStream *stream, int32 length); - bool executeMPC(Common::SeekableReadStream *stream, const char *name, int32 length); + bool initScript(Common::SeekableReadStream *stream, const char *scriptName, int32 length); + bool executeMPC(Common::SeekableReadStream *stream, const char *name, const char *scriptName, int32 length); bool executeFile(const Common::String &filename); private: @@ -51,8 +51,9 @@ private: bool _systemInit; bool registerExtensions(); - bool executeChunk(const char *chunk, uint chunkSize, const Common::String &chunkName) const; + bool executeChunk(Common::String &chunk, uint chunkSize, const Common::String &chunkName) const; void sanitizeScript(char *chunk); + void addPatches(Common::String &chunk, const char *scriptName); }; |