diff options
author | Nipun Garg | 2019-07-09 00:18:01 +0530 |
---|---|---|
committer | Eugene Sandulenko | 2019-09-03 17:17:15 +0200 |
commit | 961f3b46f0cf1a458a425c576914cfc621bf962d (patch) | |
tree | 29c97643865fe45464d2f5d6760ab0dddfe5f8b1 | |
parent | d510e3efe5da326b0a2c12a86f9937b097494b2b (diff) | |
download | scummvm-rg350-961f3b46f0cf1a458a425c576914cfc621bf962d.tar.gz scummvm-rg350-961f3b46f0cf1a458a425c576914cfc621bf962d.tar.bz2 scummvm-rg350-961f3b46f0cf1a458a425c576914cfc621bf962d.zip |
HDB: Add structs and functions for Lua Globals
-rw-r--r-- | engines/hdb/lua-script.cpp | 58 | ||||
-rw-r--r-- | engines/hdb/lua-script.h | 18 |
2 files changed, 76 insertions, 0 deletions
diff --git a/engines/hdb/lua-script.cpp b/engines/hdb/lua-script.cpp index da1f91ea85..337496b75b 100644 --- a/engines/hdb/lua-script.cpp +++ b/engines/hdb/lua-script.cpp @@ -86,6 +86,64 @@ bool LuaScript::loadLua(const char *name) { return true; } +void LuaScript::saveGlobalNumber(const char *global, double value) { + // see if global already exists; if so, overwrite it. + for (uint i = 0; i < _globals.size(); i++) { + if (!scumm_stricmp(global, _globals[i]->global)) { + _globals[i]->valueOrString = 0; + _globals[i]->value = value; + return; + } + } + + Global *g = new Global; + strcpy(g->global, global); + g->valueOrString = 0; + g->value = value; + + _globals.push_back(g); +} + +void LuaScript::saveGlobalString(const char *global, const char *string) { + if (!string) + return; + + // see if global already exists; if so, overwrite it. + for (uint i = 0; i < _globals.size(); i++) { + if (!scumm_stricmp(global, _globals[i]->global)) { + _globals[i]->valueOrString = 1; + strcpy(_globals[i]->string, string); + return; + } + } + + Global *g = new Global; + strcpy(g->global, global); + g->valueOrString = 1; + strcpy(g->string, string); + + _globals.push_back(g); +} + +void LuaScript::loadGlobal(const char *global) { + for (uint i = 0; i < _globals.size(); i++) { + if (!scumm_stricmp(global, _globals[i]->global)) { + if (_globals[i]->valueOrString) { + lua_pushstring(_state, _globals[i]->string); + lua_setglobal(_state, _globals[i]->global); + } else { + lua_pushnumber(_state, _globals[i]->value); + lua_setglobal(_state, _globals[i]->global); + } + return; + } + } +} + +void LuaScript::purgeGlobals() { + _globals.clear(); +} + void LuaScript::setLuaGlobalValue(const char *name, int value) { if (!_state) return; diff --git a/engines/hdb/lua-script.h b/engines/hdb/lua-script.h index 4f2ffd2d09..b84a19da7d 100644 --- a/engines/hdb/lua-script.h +++ b/engines/hdb/lua-script.h @@ -37,12 +37,28 @@ enum { kCameraYOff = (32 * 2 + 16) // 2.50 Tiles Extra }; +struct Global { + char global[32]; // name of global variable + int valueOrString; // value = 0, string = 1 + double value; // value + char string[32]; // string + + Global() : valueOrString(0), value(0) { + global[0] = 0; + string[0] = 0; + } +}; + class LuaScript { public: LuaScript(); ~LuaScript(); bool loadLua(const char *name); + void saveGlobalNumber(const char *global, double value); + void saveGlobalString(const char *global, const char *string); + void loadGlobal(const char *global); + void purgeGlobals(); bool init(); bool initScript(Common::SeekableReadStream *stream, const char *scriptName, int32 length); @@ -74,6 +90,8 @@ private: bool registerExtensions(); void stripComments(char *chunk); void addPatches(Common::String &chunk, const char *scriptName); + + Common::Array<Global *> _globals; }; void lua_printstack(lua_State *L); |