aboutsummaryrefslogtreecommitdiff
path: root/engines/sword25/script
diff options
context:
space:
mode:
authorEugene Sandulenko2010-08-06 13:13:25 +0000
committerEugene Sandulenko2010-10-12 22:35:55 +0000
commit47904bc7b2992189bb554833f00a79ff0fea9fb8 (patch)
tree1cec51758c6741b970bd064fafee77607b9f884f /engines/sword25/script
parentca17def625154e5f758b797e4fc48c76b0566320 (diff)
downloadscummvm-rg350-47904bc7b2992189bb554833f00a79ff0fea9fb8.tar.gz
scummvm-rg350-47904bc7b2992189bb554833f00a79ff0fea9fb8.tar.bz2
scummvm-rg350-47904bc7b2992189bb554833f00a79ff0fea9fb8.zip
SWORD25: Mass-astyle.
svn-id: r53222
Diffstat (limited to 'engines/sword25/script')
-rw-r--r--engines/sword25/script/lua_extensions.cpp2
-rw-r--r--engines/sword25/script/luabindhelper.cpp166
-rw-r--r--engines/sword25/script/luabindhelper.h50
-rw-r--r--engines/sword25/script/luacallback.cpp17
-rw-r--r--engines/sword25/script/luacallback.h10
-rw-r--r--engines/sword25/script/luascript.cpp421
-rw-r--r--engines/sword25/script/luascript.h26
-rw-r--r--engines/sword25/script/script.h18
8 files changed, 356 insertions, 354 deletions
diff --git a/engines/sword25/script/lua_extensions.cpp b/engines/sword25/script/lua_extensions.cpp
index a9abb9ea82..f069da5075 100644
--- a/engines/sword25/script/lua_extensions.cpp
+++ b/engines/sword25/script/lua_extensions.cpp
@@ -23,7 +23,7 @@
*
*/
-/*
+/*
* This code is based on Broken Sword 2.5 engine
*
* Copyright (c) Malte Thiesen, Daniel Queteschiner and Michael Elsdoerfer
diff --git a/engines/sword25/script/luabindhelper.cpp b/engines/sword25/script/luabindhelper.cpp
index f2182a6b11..76e8faac8e 100644
--- a/engines/sword25/script/luabindhelper.cpp
+++ b/engines/sword25/script/luabindhelper.cpp
@@ -23,7 +23,7 @@
*
*/
-/*
+/*
* This code is based on Broken Sword 2.5 engine
*
* Copyright (c) Malte Thiesen, Daniel Queteschiner and Michael Elsdoerfer
@@ -45,39 +45,39 @@
// -----------------------------------------------------------------------------
namespace {
- const char * METATABLES_TABLE_NAME = "__METATABLES";
- const char * PERMANENTS_TABLE_NAME = "Permanents";
+const char *METATABLES_TABLE_NAME = "__METATABLES";
+const char *PERMANENTS_TABLE_NAME = "Permanents";
- bool RegisterPermanent(lua_State *L, const Common::String &Name) {
- // A C function has to be on the stack
- if (!lua_iscfunction(L, -1)) return false;
+bool RegisterPermanent(lua_State *L, const Common::String &Name) {
+ // A C function has to be on the stack
+ if (!lua_iscfunction(L, -1)) return false;
- // Make sure that the Permanents-Table is on top of the stack
- lua_getfield(L, LUA_REGISTRYINDEX, PERMANENTS_TABLE_NAME);
- if (lua_isnil(L, -1)) {
- // Permanents-Table does not yet exist, so it has to be created
+ // Make sure that the Permanents-Table is on top of the stack
+ lua_getfield(L, LUA_REGISTRYINDEX, PERMANENTS_TABLE_NAME);
+ if (lua_isnil(L, -1)) {
+ // Permanents-Table does not yet exist, so it has to be created
- // Pop nil from the stack
- lua_pop(L, 1);
+ // Pop nil from the stack
+ lua_pop(L, 1);
- // Create Permanents-Table and insert a second reference to it on the stack
- lua_newtable(L);
- lua_pushvalue(L, -1);
+ // Create Permanents-Table and insert a second reference to it on the stack
+ lua_newtable(L);
+ lua_pushvalue(L, -1);
- // Store the Permanents-Table in the registry. The second reference is left
- // on the stack to be used in the connection
- lua_setfield(L, LUA_REGISTRYINDEX, PERMANENTS_TABLE_NAME);
- }
+ // Store the Permanents-Table in the registry. The second reference is left
+ // on the stack to be used in the connection
+ lua_setfield(L, LUA_REGISTRYINDEX, PERMANENTS_TABLE_NAME);
+ }
- // C function with the name of an index in the Permanents-Table
- lua_insert(L, -2);
- lua_setfield(L, -2, Name.c_str());
+ // C function with the name of an index in the Permanents-Table
+ lua_insert(L, -2);
+ lua_setfield(L, -2, Name.c_str());
- // Remove the Permanents-Table from the stack
- lua_pop(L, 1);
+ // Remove the Permanents-Table from the stack
+ lua_pop(L, 1);
- return true;
- }
+ return true;
+}
}
namespace Sword25 {
@@ -86,12 +86,12 @@ namespace Sword25 {
/**
* Registers a set of functions into a Lua library.
- * @param L A pointer to the Lua VM
- * @param LibName The name of the library.
+ * @param L A pointer to the Lua VM
+ * @param LibName The name of the library.
* If this is an empty string, the functions will be added to the global namespace.
- * @param Functions An array of function pointers along with their names.
+ * @param Functions An array of function pointers along with their names.
* The array must be terminated with the enry (0, 0)
- * @return Returns true if successful, otherwise false.
+ * @return Returns true if successful, otherwise false.
*/
bool BS_LuaBindhelper::AddFunctionsToLib(lua_State *L, const Common::String &LibName, const luaL_reg *Functions) {
#ifdef DEBUG
@@ -144,12 +144,12 @@ bool BS_LuaBindhelper::AddFunctionsToLib(lua_State *L, const Common::String &Lib
/**
* Adds a set of constants to the Lua library
- * @param L A pointer to the Lua VM
- * @param LibName The name of the library.
+ * @param L A pointer to the Lua VM
+ * @param LibName The name of the library.
* If this is an empty string, the functions will be added to the global namespace.
- * @param Constants An array of the constant values along with their names.
+ * @param Constants An array of the constant values along with their names.
* The array must be terminated with the enry (0, 0)
- * @return Returns true if successful, otherwise false.
+ * @return Returns true if successful, otherwise false.
*/
bool BS_LuaBindhelper::AddConstantsToLib(lua_State *L, const Common::String &LibName, const lua_constant_reg *Constants) {
#ifdef DEBUG
@@ -168,7 +168,7 @@ bool BS_LuaBindhelper::AddConstantsToLib(lua_State *L, const Common::String &Lib
else {
// Ensure that the library table exists
if (!_CreateTable(L, LibName)) return false;
-
+
// Register each constant in the table
for (; Constants->Name; ++Constants) {
lua_pushstring(L, Constants->Name);
@@ -191,12 +191,12 @@ bool BS_LuaBindhelper::AddConstantsToLib(lua_State *L, const Common::String &Lib
/**
* Adds a set of methods to a Lua class
- * @param L A pointer to the Lua VM
- * @param ClassName The name of the class
+ * @param L A pointer to the Lua VM
+ * @param ClassName The name of the class
* When the class name specified does not exist, it is created.
- * @param Methods An array of function pointers along with their method names.
+ * @param Methods An array of function pointers along with their method names.
* The array must be terminated with the enry (0, 0)
- * @return Returns true if successful, otherwise false.
+ * @return Returns true if successful, otherwise false.
*/
bool BS_LuaBindhelper::AddMethodsToClass(lua_State *L, const Common::String &ClassName, const luaL_reg *Methods) {
#ifdef DEBUG
@@ -205,7 +205,7 @@ bool BS_LuaBindhelper::AddMethodsToClass(lua_State *L, const Common::String &Cla
// Load the metatable onto the Lua stack
if (!GetMetatable(L, ClassName)) return false;
-
+
// Register each method in the Metatable
for (; Methods->name; ++Methods) {
lua_pushstring(L, Methods->name);
@@ -232,11 +232,11 @@ bool BS_LuaBindhelper::AddMethodsToClass(lua_State *L, const Common::String &Cla
/**
* Sets the garbage collector callback method when items of a particular class are deleted
- * @param L A pointer to the Lua VM
- * @param ClassName The name of the class
+ * @param L A pointer to the Lua VM
+ * @param ClassName The name of the class
* When the class name specified does not exist, it is created.
- * @param GCHandler A function pointer
- * @return Returns true if successful, otherwise false.
+ * @param GCHandler A function pointer
+ * @return Returns true if successful, otherwise false.
*/
bool BS_LuaBindhelper::SetClassGCHandler(lua_State *L, const Common::String &ClassName, lua_CFunction GCHandler) {
#ifdef DEBUG
@@ -271,22 +271,22 @@ bool BS_LuaBindhelper::SetClassGCHandler(lua_State *L, const Common::String &Cla
// -----------------------------------------------------------------------------
namespace {
- void PushMetatableTable(lua_State *L) {
- // Push the Metatable table onto the stack
- lua_getglobal(L, METATABLES_TABLE_NAME);
+void PushMetatableTable(lua_State *L) {
+ // Push the Metatable table onto the stack
+ lua_getglobal(L, METATABLES_TABLE_NAME);
- // If the table doesn't yet exist, it must be created
- if (lua_isnil(L, -1)) {
- // Pop nil from stack
- lua_pop(L, 1);
+ // If the table doesn't yet exist, it must be created
+ if (lua_isnil(L, -1)) {
+ // Pop nil from stack
+ lua_pop(L, 1);
- // New table has been created, so add it to the global table and leave reference on stack
- lua_newtable(L);
- lua_pushvalue(L, -1);
- lua_setglobal(L, METATABLES_TABLE_NAME);
- }
+ // New table has been created, so add it to the global table and leave reference on stack
+ lua_newtable(L);
+ lua_pushvalue(L, -1);
+ lua_setglobal(L, METATABLES_TABLE_NAME);
}
}
+}
namespace Sword25 {
@@ -373,34 +373,34 @@ bool BS_LuaBindhelper::_CreateTable(lua_State *L, const Common::String &TableNam
} // End of namespace Sword25
namespace {
- Common::String GetLuaValueInfo(lua_State *L, int StackIndex) {
- switch (lua_type(L, StackIndex)) {
- case LUA_TNUMBER:
- lua_pushstring(L, lua_tostring(L, StackIndex));
- break;
-
- case LUA_TSTRING:
- lua_pushfstring(L, "\"%s\"", lua_tostring(L, StackIndex));
- break;
-
- case LUA_TBOOLEAN:
- lua_pushstring(L, (lua_toboolean(L, StackIndex) ? "true" : "false"));
- break;
-
- case LUA_TNIL:
- lua_pushliteral(L, "nil");
- break;
-
- default:
- lua_pushfstring(L, "%s: %p", luaL_typename(L, StackIndex), lua_topointer(L, StackIndex));
- break;
- }
+Common::String GetLuaValueInfo(lua_State *L, int StackIndex) {
+ switch (lua_type(L, StackIndex)) {
+ case LUA_TNUMBER:
+ lua_pushstring(L, lua_tostring(L, StackIndex));
+ break;
+
+ case LUA_TSTRING:
+ lua_pushfstring(L, "\"%s\"", lua_tostring(L, StackIndex));
+ break;
+
+ case LUA_TBOOLEAN:
+ lua_pushstring(L, (lua_toboolean(L, StackIndex) ? "true" : "false"));
+ break;
+
+ case LUA_TNIL:
+ lua_pushliteral(L, "nil");
+ break;
+
+ default:
+ lua_pushfstring(L, "%s: %p", luaL_typename(L, StackIndex), lua_topointer(L, StackIndex));
+ break;
+ }
- Common::String Result(lua_tostring(L, -1));
- lua_pop(L, 1);
+ Common::String Result(lua_tostring(L, -1));
+ lua_pop(L, 1);
- return Result;
- }
+ return Result;
+}
}
namespace Sword25 {
@@ -419,7 +419,7 @@ Common::String BS_LuaBindhelper::StackDump(lua_State *L) {
oss += "-------------- Stack Dump Finished ---------------\n";
return oss;
-}
+}
Common::String BS_LuaBindhelper::TableDump(lua_State *L) {
Common::String oss;
diff --git a/engines/sword25/script/luabindhelper.h b/engines/sword25/script/luabindhelper.h
index 6bb1a254b9..9c864c6085 100644
--- a/engines/sword25/script/luabindhelper.h
+++ b/engines/sword25/script/luabindhelper.h
@@ -23,7 +23,7 @@
*
*/
-/*
+/*
* This code is based on Broken Sword 2.5 engine
*
* Copyright (c) Malte Thiesen, Daniel Queteschiner and Michael Elsdoerfer
@@ -41,8 +41,8 @@ namespace Lua {
extern "C"
{
- #include "sword25/util/lua/lua.h"
- #include "sword25/util/lua/lauxlib.h"
+#include "sword25/util/lua/lua.h"
+#include "sword25/util/lua/lauxlib.h"
}
}
@@ -55,65 +55,65 @@ namespace Sword25 {
#define lua_tobooleancpp(L, i) (lua_toboolean(L, i) == 0 ? false : true)
struct lua_constant_reg {
- const char * Name;
- lua_Number Value;
+ const char *Name;
+ lua_Number Value;
};
class BS_LuaBindhelper {
public:
/**
* Registers a set of functions into a Lua library.
- * @param L A pointer to the Lua VM
- * @param LibName The name of the library.
+ * @param L A pointer to the Lua VM
+ * @param LibName The name of the library.
* If this is an empty string, the functions will be added to the global namespace.
- * @param Functions An array of function pointers along with their names.
+ * @param Functions An array of function pointers along with their names.
* The array must be terminated with the enry (0, 0)
- * @return Returns true if successful, otherwise false.
+ * @return Returns true if successful, otherwise false.
*/
static bool AddFunctionsToLib(lua_State *L, const Common::String &LibName, const luaL_reg *Functions);
/**
* Adds a set of constants to the Lua library
- * @param L A pointer to the Lua VM
- * @param LibName The name of the library.
+ * @param L A pointer to the Lua VM
+ * @param LibName The name of the library.
* If this is an empty string, the functions will be added to the global namespace.
- * @param Constants An array of the constant values along with their names.
+ * @param Constants An array of the constant values along with their names.
* The array must be terminated with the enry (0, 0)
- * @return Returns true if successful, otherwise false.
+ * @return Returns true if successful, otherwise false.
*/
- static bool AddConstantsToLib(lua_State * L, const Common::String & LibName, const lua_constant_reg * Constants);
+ static bool AddConstantsToLib(lua_State *L, const Common::String &LibName, const lua_constant_reg *Constants);
/**
* Adds a set of methods to a Lua class
- * @param L A pointer to the Lua VM
- * @param ClassName The name of the class
+ * @param L A pointer to the Lua VM
+ * @param ClassName The name of the class
* When the class name specified does not exist, it is created.
- * @param Methods An array of function pointers along with their method names.
+ * @param Methods An array of function pointers along with their method names.
* The array must be terminated with the enry (0, 0)
- * @return Returns true if successful, otherwise false.
+ * @return Returns true if successful, otherwise false.
*/
static bool AddMethodsToClass(lua_State *L, const Common::String &ClassName, const luaL_reg *Methods);
/**
* Sets the garbage collector callback method when items of a particular class are deleted
- * @param L A pointer to the Lua VM
- * @param ClassName The name of the class
+ * @param L A pointer to the Lua VM
+ * @param ClassName The name of the class
* When the class name specified does not exist, it is created.
- * @param GCHandler A function pointer
- * @return Returns true if successful, otherwise false.
+ * @param GCHandler A function pointer
+ * @return Returns true if successful, otherwise false.
*/
static bool SetClassGCHandler(lua_State *L, const Common::String &ClassName, lua_CFunction GCHandler);
/**
* Returns a string containing a stack dump of the Lua stack
- * @param L A pointer to the Lua VM
+ * @param L A pointer to the Lua VM
*/
static Common::String StackDump(lua_State *L);
/**
* Returns a string that describes the contents of a table
- * @param L A pointer to the Lua VM
- * @remark The table must be on the Lua stack to be read out.
+ * @param L A pointer to the Lua VM
+ * @remark The table must be on the Lua stack to be read out.
*/
static Common::String TableDump(lua_State *L);
diff --git a/engines/sword25/script/luacallback.cpp b/engines/sword25/script/luacallback.cpp
index 37c6d04596..dfabc81807 100644
--- a/engines/sword25/script/luacallback.cpp
+++ b/engines/sword25/script/luacallback.cpp
@@ -23,7 +23,7 @@
*
*/
-/*
+/*
* This code is based on Broken Sword 2.5 engine
*
* Copyright (c) Malte Thiesen, Daniel Queteschiner and Michael Elsdoerfer
@@ -43,11 +43,11 @@ namespace Lua {
extern "C"
{
- #include "sword25/util/lua/lua.h"
- #include "sword25/util/lua/lauxlib.h"
+#include "sword25/util/lua/lua.h"
+#include "sword25/util/lua/lauxlib.h"
}
-const char * CALLBACKTABLE_NAME = "__CALLBACKS";
+const char *CALLBACKTABLE_NAME = "__CALLBACKS";
}
@@ -88,7 +88,7 @@ void BS_LuaCallback::RegisterCallbackFunction(lua_State *L, unsigned int ObjectH
void BS_LuaCallback::UnregisterCallbackFunction(lua_State *L, unsigned int ObjectHandle) {
BS_ASSERT(lua_isfunction(L, -1));
- EnsureObjectCallbackTableExists(L,ObjectHandle);
+ EnsureObjectCallbackTableExists(L, ObjectHandle);
// Iterate over all elements of the object callback table and remove the function from it
lua_pushnil(L);
@@ -138,8 +138,7 @@ void BS_LuaCallback::InvokeCallbackFunctions(lua_State *L, unsigned int ObjectHa
// The value of the current element is at the top of the stack, including the index
// If the value is a function, execute it
- if (lua_type(L, -1) == LUA_TFUNCTION)
- {
+ if (lua_type(L, -1) == LUA_TFUNCTION) {
// Pre-Function Call
// Derived classes can function in this parameter onto the stack.
// The return value indicates the number of parameters
@@ -153,9 +152,7 @@ void BS_LuaCallback::InvokeCallbackFunctions(lua_State *L, unsigned int ObjectHa
// Pop error message from the stack
lua_pop(L, 1);
}
- }
- else
- {
+ } else {
// Pop value from the stack. The index is then ready for the next call to lua_next()
lua_pop(L, 1);
}
diff --git a/engines/sword25/script/luacallback.h b/engines/sword25/script/luacallback.h
index 7b7bbbe024..b8ac97fd95 100644
--- a/engines/sword25/script/luacallback.h
+++ b/engines/sword25/script/luacallback.h
@@ -23,7 +23,7 @@
*
*/
-/*
+/*
* This code is based on Broken Sword 2.5 engine
*
* Copyright (c) Malte Thiesen, Daniel Queteschiner and Michael Elsdoerfer
@@ -61,7 +61,7 @@ namespace Sword25 {
class BS_LuaCallback {
public:
- BS_LuaCallback(lua_State * L);
+ BS_LuaCallback(lua_State *L);
virtual ~BS_LuaCallback();
// Funktion muss auf dem Lua-Stack liegen.
@@ -75,10 +75,12 @@ public:
void InvokeCallbackFunctions(lua_State *L, unsigned int ObjectHandle);
protected:
- virtual int PreFunctionInvokation(lua_State *L) { return 0; }
+ virtual int PreFunctionInvokation(lua_State *L) {
+ return 0;
+ }
private:
- void EnsureObjectCallbackTableExists(lua_State *L,unsigned int ObjectHandle);
+ void EnsureObjectCallbackTableExists(lua_State *L, unsigned int ObjectHandle);
void PushCallbackTable(lua_State *L);
void PushObjectCallbackTable(lua_State *L, unsigned int ObjectHandle);
};
diff --git a/engines/sword25/script/luascript.cpp b/engines/sword25/script/luascript.cpp
index 62b5791a82..2d475e1ed6 100644
--- a/engines/sword25/script/luascript.cpp
+++ b/engines/sword25/script/luascript.cpp
@@ -23,7 +23,7 @@
*
*/
-/*
+/*
* This code is based on Broken Sword 2.5 engine
*
* Copyright (c) Malte Thiesen, Daniel Queteschiner and Michael Elsdoerfer
@@ -49,10 +49,10 @@
namespace Lua {
extern "C" {
- #include "sword25/util/lua/lua.h"
- #include "sword25/util/lua/lualib.h"
- #include "sword25/util/lua/lauxlib.h"
- #include "sword25/util/pluto/pluto.h"
+#include "sword25/util/lua/lua.h"
+#include "sword25/util/lua/lualib.h"
+#include "sword25/util/lua/lauxlib.h"
+#include "sword25/util/pluto/pluto.h"
}
}
@@ -80,15 +80,17 @@ BS_LuaScriptEngine::~BS_LuaScriptEngine() {
// -----------------------------------------------------------------------------
-BS_Service *BS_LuaScriptEngine_CreateObject(BS_Kernel * KernelPtr) { return new BS_LuaScriptEngine(KernelPtr); }
+BS_Service *BS_LuaScriptEngine_CreateObject(BS_Kernel *KernelPtr) {
+ return new BS_LuaScriptEngine(KernelPtr);
+}
// -----------------------------------------------------------------------------
namespace {
- int PanicCB(lua_State *L) {
- BS_LOG_ERRORLN("Lua panic. Error message: %s", lua_isnil(L, -1) ? "" : lua_tostring(L, -1));
- return 0;
- }
+int PanicCB(lua_State *L) {
+ BS_LOG_ERRORLN("Lua panic. Error message: %s", lua_isnil(L, -1) ? "" : lua_tostring(L, -1));
+ return 0;
+}
}
// -----------------------------------------------------------------------------
@@ -103,14 +105,14 @@ bool BS_LuaScriptEngine::Init() {
// Register panic callback function
lua_atpanic(m_State, PanicCB);
-
+
// Error handler for lua_pcall calls
- // The code below contains a local error handler function
+ // The code below contains a local error handler function
const char ErrorHandlerCode[] =
- "local function ErrorHandler(message) "
- " return message .. '\\n' .. debug.traceback('', 2) "
- "end "
- "return ErrorHandler";
+ "local function ErrorHandler(message) "
+ " return message .. '\\n' .. debug.traceback('', 2) "
+ "end "
+ "return ErrorHandler";
// Compile the code
if (luaL_loadbuffer(m_State, ErrorHandlerCode, strlen(ErrorHandlerCode), "PCALL ERRORHANDLER") != 0) {
@@ -128,7 +130,7 @@ bool BS_LuaScriptEngine::Init() {
return false;
}
-
+
// Place the error handler function in the Lua registry, and remember the index
m_PcallErrorhandlerRegistryIndex = luaL_ref(m_State, LUA_REGISTRYINDEX);
@@ -193,20 +195,20 @@ bool BS_LuaScriptEngine::ExecuteString(const Common::String &Code) {
namespace {
- void RemoveForbiddenFunctions(lua_State *L) {
- static const char *FORBIDDEN_FUNCTIONS[] = {
- "dofile",
- 0
- };
+void RemoveForbiddenFunctions(lua_State *L) {
+ static const char *FORBIDDEN_FUNCTIONS[] = {
+ "dofile",
+ 0
+ };
- const char ** Iterator = FORBIDDEN_FUNCTIONS;
- while (*Iterator) {
- lua_pushnil(L);
- lua_setfield(L, LUA_GLOBALSINDEX, *Iterator);
- ++Iterator;
- }
+ const char **Iterator = FORBIDDEN_FUNCTIONS;
+ while (*Iterator) {
+ lua_pushnil(L);
+ lua_setfield(L, LUA_GLOBALSINDEX, *Iterator);
+ ++Iterator;
}
}
+}
bool BS_LuaScriptEngine::RegisterStandardLibs() {
luaL_openlibs(m_State);
@@ -232,8 +234,8 @@ bool BS_LuaScriptEngine::ExecuteBuffer(const char *Data, unsigned int Size, cons
// Run buffer contents
if (lua_pcall(m_State, 0, 0, -2) != 0) {
BS_LOG_ERRORLN("An error occured while executing \"%s\":\n%s.",
- Name.c_str(),
- lua_tostring(m_State, -1));
+ Name.c_str(),
+ lua_tostring(m_State, -1));
lua_pop(m_State, 2);
return false;
@@ -262,161 +264,160 @@ void BS_LuaScriptEngine::SetCommandLine(const Common::StringArray &CommandLinePa
// -----------------------------------------------------------------------------
namespace {
- const char *PERMANENTS_TABLE_NAME = "Permanents";
-
- // -------------------------------------------------------------------------
-
- // This array contains the name of global Lua objects that should not be persisted
- const char *STANDARD_PERMANENTS[] = {
- "string",
- "xpcall",
- "package",
- "tostring",
- "print",
- "os",
- "unpack",
- "require",
- "getfenv",
- "setmetatable",
- "next",
- "assert",
- "tonumber",
- "io",
- "rawequal",
- "collectgarbage",
- "getmetatable",
- "module",
- "rawset",
- "warning",
- "math",
- "debug",
- "pcall",
- "table",
- "newproxy",
- "type",
- "coroutine",
- "select",
- "gcinfo",
- "pairs",
- "rawget",
- "loadstring",
- "ipairs",
- "_VERSION",
- "setfenv",
- "load",
- "error",
- "loadfile",
-
- "pairs_next",
- "ipairs_next",
- "pluto",
- "Cfg",
- "Translator",
- "Persistence",
- "CommandLine",
- 0
- };
-
- // -------------------------------------------------------------------------
-
- enum PERMANENT_TABLE_TYPE {
- PTT_PERSIST,
- PTT_UNPERSIST
- };
-
- // -------------------------------------------------------------------------
-
- bool PushPermanentsTable(lua_State *L, PERMANENT_TABLE_TYPE TableType) {
- // Permanents-Table
- lua_newtable(L);
+const char *PERMANENTS_TABLE_NAME = "Permanents";
+
+// -------------------------------------------------------------------------
+
+// This array contains the name of global Lua objects that should not be persisted
+const char *STANDARD_PERMANENTS[] = {
+ "string",
+ "xpcall",
+ "package",
+ "tostring",
+ "print",
+ "os",
+ "unpack",
+ "require",
+ "getfenv",
+ "setmetatable",
+ "next",
+ "assert",
+ "tonumber",
+ "io",
+ "rawequal",
+ "collectgarbage",
+ "getmetatable",
+ "module",
+ "rawset",
+ "warning",
+ "math",
+ "debug",
+ "pcall",
+ "table",
+ "newproxy",
+ "type",
+ "coroutine",
+ "select",
+ "gcinfo",
+ "pairs",
+ "rawget",
+ "loadstring",
+ "ipairs",
+ "_VERSION",
+ "setfenv",
+ "load",
+ "error",
+ "loadfile",
+
+ "pairs_next",
+ "ipairs_next",
+ "pluto",
+ "Cfg",
+ "Translator",
+ "Persistence",
+ "CommandLine",
+ 0
+};
+
+// -------------------------------------------------------------------------
+
+enum PERMANENT_TABLE_TYPE {
+ PTT_PERSIST,
+ PTT_UNPERSIST
+};
+
+// -------------------------------------------------------------------------
+
+bool PushPermanentsTable(lua_State *L, PERMANENT_TABLE_TYPE TableType) {
+ // Permanents-Table
+ lua_newtable(L);
+
+ // All standard permanents are inserted into this table
+ unsigned int Index = 0;
+ while (STANDARD_PERMANENTS[Index]) {
+ // Permanents are placed onto the stack; if it does not exist, it is simply ignored
+ lua_getglobal(L, STANDARD_PERMANENTS[Index]);
+ if (!lua_isnil(L, -1)) {
+ // Name of the element as a unique value on the stack
+ lua_pushstring(L, STANDARD_PERMANENTS[Index]);
- // All standard permanents are inserted into this table
- unsigned int Index = 0;
- while (STANDARD_PERMANENTS[Index]) {
- // Permanents are placed onto the stack; if it does not exist, it is simply ignored
- lua_getglobal(L, STANDARD_PERMANENTS[Index]);
- if (!lua_isnil(L, -1)) {
- // Name of the element as a unique value on the stack
- lua_pushstring(L, STANDARD_PERMANENTS[Index]);
+ // If it is loaded, then it can be used
+ // In this case, the position of name and object are reversed on the stack
+ if (TableType == PTT_UNPERSIST) lua_insert(L, -2);
- // If it is loaded, then it can be used
- // In this case, the position of name and object are reversed on the stack
- if (TableType == PTT_UNPERSIST) lua_insert(L, -2);
+ // Make an entry in the table
+ lua_settable(L, -3);
+ } else {
+ // Pop nil value from stack
+ lua_pop(L, 1);
+ }
- // Make an entry in the table
- lua_settable(L, -3);
- }
- else {
- // Pop nil value from stack
- lua_pop(L, 1);
- }
+ ++Index;
+ }
- ++Index;
- }
-
- // All registered C functions to be inserted into the table
- // BS_LuaBindhelper places in the register a table in which all registered C functions
- // are stored
+ // All registered C functions to be inserted into the table
+ // BS_LuaBindhelper places in the register a table in which all registered C functions
+ // are stored
- // Table is put on the stack
- lua_getfield(L, LUA_REGISTRYINDEX, PERMANENTS_TABLE_NAME);
+ // Table is put on the stack
+ lua_getfield(L, LUA_REGISTRYINDEX, PERMANENTS_TABLE_NAME);
- if (!lua_isnil(L, -1)) {
- // Iterate over all elements of the table
- lua_pushnil(L);
- while (lua_next(L, -2) != 0) {
- // Value and index duplicated on the stack and changed in the sequence
- lua_pushvalue(L, -1);
- lua_pushvalue(L, -3);
+ if (!lua_isnil(L, -1)) {
+ // Iterate over all elements of the table
+ lua_pushnil(L);
+ while (lua_next(L, -2) != 0) {
+ // Value and index duplicated on the stack and changed in the sequence
+ lua_pushvalue(L, -1);
+ lua_pushvalue(L, -3);
- // If it is loaded, then it can be used
- // In this case, the position of name and object are reversed on the stack
- if (TableType == PTT_UNPERSIST) lua_insert(L, -2);
+ // If it is loaded, then it can be used
+ // In this case, the position of name and object are reversed on the stack
+ if (TableType == PTT_UNPERSIST) lua_insert(L, -2);
- // Make an entry in the results table
- lua_settable(L, -6);
+ // Make an entry in the results table
+ lua_settable(L, -6);
- // Pop value from the stack. The index is then ready for the next call to lua_next()
- lua_pop(L, 1);
- }
+ // Pop value from the stack. The index is then ready for the next call to lua_next()
+ lua_pop(L, 1);
}
+ }
- // Pop the C-Permanents table from the stack
- lua_pop(L, 1);
+ // Pop the C-Permanents table from the stack
+ lua_pop(L, 1);
- // coroutine.yield must be registered in the extra-Permanents table because they
- // are inactive coroutine C functions on the stack
+ // coroutine.yield must be registered in the extra-Permanents table because they
+ // are inactive coroutine C functions on the stack
- // Function coroutine.yield placed on the stack
- lua_getglobal(L, "coroutine");
- lua_pushstring(L, "yield");
- lua_gettable(L, -2);
+ // Function coroutine.yield placed on the stack
+ lua_getglobal(L, "coroutine");
+ lua_pushstring(L, "yield");
+ lua_gettable(L, -2);
- // Store coroutine.yield with it's own unique value in the Permanents table
- lua_pushstring(L, "coroutine.yield");
+ // Store coroutine.yield with it's own unique value in the Permanents table
+ lua_pushstring(L, "coroutine.yield");
- if (TableType == PTT_UNPERSIST) lua_insert(L, -2);
+ if (TableType == PTT_UNPERSIST) lua_insert(L, -2);
- lua_settable(L, -4);
+ lua_settable(L, -4);
- // Coroutine table is popped from the stack
- lua_pop(L, 1);
+ // Coroutine table is popped from the stack
+ lua_pop(L, 1);
- return true;
- }
+ return true;
+}
}
// -----------------------------------------------------------------------------
namespace {
- int Chunkwriter(lua_State *L, const void *p, size_t sz, void *ud) {
- Common::Array<unsigned char> & chunkData = *reinterpret_cast<Common::Array<unsigned char> * >(ud);
- const unsigned char *buffer = reinterpret_cast<const unsigned char *>(p);
+int Chunkwriter(lua_State *L, const void *p, size_t sz, void *ud) {
+ Common::Array<unsigned char> & chunkData = *reinterpret_cast<Common::Array<unsigned char> * >(ud);
+ const unsigned char *buffer = reinterpret_cast<const unsigned char *>(p);
- while (sz--) chunkData.push_back(*buffer++) ;
+ while (sz--) chunkData.push_back(*buffer++) ;
- return 1;
- }
+ return 1;
+}
}
bool BS_LuaScriptEngine::Persist(BS_OutputPersistenceBlock &Writer) {
@@ -447,66 +448,66 @@ bool BS_LuaScriptEngine::Persist(BS_OutputPersistenceBlock &Writer) {
// -----------------------------------------------------------------------------
namespace {
- // -------------------------------------------------------------------------
+// -------------------------------------------------------------------------
- struct ChunkreaderData {
- void * BufferPtr;
- size_t Size;
- bool BufferReturned;
- };
+struct ChunkreaderData {
+ void *BufferPtr;
+ size_t Size;
+ bool BufferReturned;
+};
- // ------------------------------------------------------------------------
+// ------------------------------------------------------------------------
- const char *Chunkreader(lua_State *L, void *ud, size_t *sz) {
- ChunkreaderData & cd = *reinterpret_cast<ChunkreaderData *>(ud);
+const char *Chunkreader(lua_State *L, void *ud, size_t *sz) {
+ ChunkreaderData &cd = *reinterpret_cast<ChunkreaderData *>(ud);
- if (!cd.BufferReturned) {
- cd.BufferReturned = true;
- *sz = cd.Size;
- return reinterpret_cast<const char *>(cd.BufferPtr);
- } else {
- return 0;
- }
+ if (!cd.BufferReturned) {
+ cd.BufferReturned = true;
+ *sz = cd.Size;
+ return reinterpret_cast<const char *>(cd.BufferPtr);
+ } else {
+ return 0;
}
+}
- // -------------------------------------------------------------------------
+// -------------------------------------------------------------------------
- void ClearGlobalTable(lua_State *L, const char **Exceptions) {
- // Iterate over all elements of the global table
- lua_pushvalue(L, LUA_GLOBALSINDEX);
- lua_pushnil(L);
- while (lua_next(L, -2) != 0) {
- // Now the value and the index of the current element is on the stack
- // This value does not interest us, so it is popped from the stack
- lua_pop(L, 1);
+void ClearGlobalTable(lua_State *L, const char **Exceptions) {
+ // Iterate over all elements of the global table
+ lua_pushvalue(L, LUA_GLOBALSINDEX);
+ lua_pushnil(L);
+ while (lua_next(L, -2) != 0) {
+ // Now the value and the index of the current element is on the stack
+ // This value does not interest us, so it is popped from the stack
+ lua_pop(L, 1);
- // Determine whether the item is set to nil, so you want to remove from the global table.
- // For this will determine whether the element name is a string and is present in
- // the list of exceptions
- bool SetElementToNil = true;
- if (lua_isstring(L, -1)) {
- const char *IndexString = lua_tostring(L, -1);
- const char ** ExceptionsWalker = Exceptions;
- while (*ExceptionsWalker) {
- if (strcmp(IndexString, *ExceptionsWalker) == 0) SetElementToNil = false;
- ++ExceptionsWalker;
- }
+ // Determine whether the item is set to nil, so you want to remove from the global table.
+ // For this will determine whether the element name is a string and is present in
+ // the list of exceptions
+ bool SetElementToNil = true;
+ if (lua_isstring(L, -1)) {
+ const char *IndexString = lua_tostring(L, -1);
+ const char **ExceptionsWalker = Exceptions;
+ while (*ExceptionsWalker) {
+ if (strcmp(IndexString, *ExceptionsWalker) == 0) SetElementToNil = false;
+ ++ExceptionsWalker;
}
+ }
- // If the above test showed that the item should be removed, it is removed by setting the value to nil.
- if (SetElementToNil) {
- lua_pushvalue(L, -1);
- lua_pushnil(L);
- lua_settable(L, LUA_GLOBALSINDEX);
- }
- }
+ // If the above test showed that the item should be removed, it is removed by setting the value to nil.
+ if (SetElementToNil) {
+ lua_pushvalue(L, -1);
+ lua_pushnil(L);
+ lua_settable(L, LUA_GLOBALSINDEX);
+ }
+ }
- // Pop the Global table from the stack
- lua_pop(L, 1);
+ // Pop the Global table from the stack
+ lua_pop(L, 1);
- // Perform garbage collection, so that all removed elements are deleted
- lua_gc(L, LUA_GCCOLLECT, 0);
- }
+ // Perform garbage collection, so that all removed elements are deleted
+ lua_gc(L, LUA_GCCOLLECT, 0);
+}
}
// -----------------------------------------------------------------------------
@@ -516,7 +517,7 @@ bool BS_LuaScriptEngine::Unpersist(BS_InputPersistenceBlock &Reader) {
lua_settop(m_State, 0);
// Permanents table is placed on the stack. This has already happened at this point, because
- // to create the table all permanents must be accessible. This is the case only for the
+ // to create the table all permanents must be accessible. This is the case only for the
// beginning of the function, because the global table is emptied below
PushPermanentsTable(m_State, PTT_UNPERSIST);
@@ -572,7 +573,7 @@ bool BS_LuaScriptEngine::Unpersist(BS_InputPersistenceBlock &Reader) {
// The table with the loaded data is popped from the stack
lua_pop(m_State, 1);
-
+
// Force garbage collection
lua_gc(m_State, LUA_GCCOLLECT, 0);
diff --git a/engines/sword25/script/luascript.h b/engines/sword25/script/luascript.h
index f0a8a9bbdc..228625a67b 100644
--- a/engines/sword25/script/luascript.h
+++ b/engines/sword25/script/luascript.h
@@ -23,7 +23,7 @@
*
*/
-/*
+/*
* This code is based on Broken Sword 2.5 engine
*
* Copyright (c) Malte Thiesen, Daniel Queteschiner and Michael Elsdoerfer
@@ -75,44 +75,46 @@ public:
/**
* Initialises the scripting engine
- * @return Returns true if successful, otherwise false.
+ * @return Returns true if successful, otherwise false.
*/
virtual bool Init();
/**
* Loads a script file and executes it
- * @param FileName The filename of the script
- * @return Returns true if successful, otherwise false.
+ * @param FileName The filename of the script
+ * @return Returns true if successful, otherwise false.
*/
virtual bool ExecuteFile(const Common::String &FileName);
/**
* Execute a string of script code
- * @param Code A string of script code
- * @return Returns true if successful, otherwise false.
+ * @param Code A string of script code
+ * @return Returns true if successful, otherwise false.
*/
virtual bool ExecuteString(const Common::String &Code);
/**
* Returns a pointer to the main object of the scripting language
- * @remark Using this method breaks the encapsulation of the language
+ * @remark Using this method breaks the encapsulation of the language
*/
- virtual void *GetScriptObject() { return m_State; }
+ virtual void *GetScriptObject() {
+ return m_State;
+ }
/**
* Makes the command line parameters for the scripting environment available
- * @param CommandLineParameters An array containing all the command line parameters
- * @remark How the command line parameters will be used by scripts is
+ * @param CommandLineParameters An array containing all the command line parameters
+ * @remark How the command line parameters will be used by scripts is
* dependant on the particular implementation.
*/
virtual void SetCommandLine(const Common::StringArray &CommandLineParameters);
/**
- * @remark The Lua stack is cleared by this method
+ * @remark The Lua stack is cleared by this method
*/
virtual bool Persist(BS_OutputPersistenceBlock &Writer);
/**
- * @remark The Lua stack is cleared by this method
+ * @remark The Lua stack is cleared by this method
*/
virtual bool Unpersist(BS_InputPersistenceBlock &Reader);
diff --git a/engines/sword25/script/script.h b/engines/sword25/script/script.h
index 0363daec0f..4804324226 100644
--- a/engines/sword25/script/script.h
+++ b/engines/sword25/script/script.h
@@ -23,7 +23,7 @@
*
*/
-/*
+/*
* This code is based on Broken Sword 2.5 engine
*
* Copyright (c) Malte Thiesen, Daniel Queteschiner and Michael Elsdoerfer
@@ -64,28 +64,28 @@ public:
// -----------------------------------------------------------------------------
// Constructor / destructor
// -----------------------------------------------------------------------------
-
- BS_ScriptEngine(BS_Kernel * KernelPtr) : BS_Service(KernelPtr) {};
+
+ BS_ScriptEngine(BS_Kernel *KernelPtr) : BS_Service(KernelPtr) {};
virtual ~BS_ScriptEngine() {};
// -----------------------------------------------------------------------------
// This method must be implemented by the script engine
// -----------------------------------------------------------------------------
-
+
/**
* Initialises the scrip tengine. Returns true if successful, false otherwise.
*/
virtual bool Init() = 0;
/**
- * Loads a script file and executes it.
- * @param FileName The script filename
+ * Loads a script file and executes it.
+ * @param FileName The script filename
*/
virtual bool ExecuteFile(const Common::String &FileName) = 0;
/**
* Executes a specified script fragment
- * @param Code String of script code
+ * @param Code String of script code
*/
virtual bool ExecuteString(const Common::String &Code) = 0;
@@ -93,13 +93,13 @@ public:
* Returns a pointer to the main object of the script engine
* Note: Using this method breaks the encapsulation of the language from the rest of the engine.
*/
- virtual void * GetScriptObject() = 0;
+ virtual void *GetScriptObject() = 0;
/**
* Makes the command line parameters for the script environment available
* Note: How the command line parameters will be used by scripts is dependant on the
* particular implementation.
- * @param CommandLineParameters List containing the command line parameters
+ * @param CommandLineParameters List containing the command line parameters
*/
virtual void SetCommandLine(const Common::Array<Common::String> &CommandLineParameters) = 0;