Browse Source

Merge pull request #304 from blackberry/master

Rebase from master
Sean Paul Taylor 13 years ago
parent
commit
9bda9ab717

+ 12 - 3
gameplay/src/ScriptController.cpp

@@ -516,6 +516,16 @@ void ScriptController::setString(const char* name, const char* v)
     lua_setglobal(_lua, name);
 }
 
+void ScriptController::print(const char* str)
+{
+    printError("%s", str);
+}
+
+void ScriptController::print(const char* str1, const char* str2)
+{
+    printError("%s%s", str1, str2);
+}
+
 ScriptController::ScriptController() : _lua(NULL)
 {
     memset(_callbacks, 0, sizeof(std::string*) * CALLBACK_COUNT);
@@ -530,9 +540,8 @@ ScriptController::~ScriptController()
 }
 
 static const char* lua_print_function = 
-    "function print(str, ...)\n"
-    "    local arg = {...}\n"
-    "    printError(string.format(str, table.unpack(arg)))\n"
+    "function print(...)\n"
+    "    ScriptController.print(table.concat({...},\"\\t\"), \"\\n\")\n"
     "end\n";
 
 void ScriptController::initialize()

+ 17 - 0
gameplay/src/ScriptController.h

@@ -552,6 +552,23 @@ public:
      */
     template<typename T>void setObjectPointer(const char* type, const char* name, T* v);
 
+    /**
+     * Prints the string to the platform's output stream or log file.
+     * Used for overriding Lua's print function.
+     * 
+     * @param str The string to print.
+     */
+    static void print(const char* str);
+
+    /**
+     * Prints the strings to the platform's output stream or log file.
+     * Used for overriding Lua's print function.
+     * 
+     * @param str1 The first string to print.
+     * @param str2 The second string to print on the same line as str1.
+     */
+    static void print(const char* str1, const char* str2);
+
 private:
 
     /**

+ 63 - 1
gameplay/src/lua/lua_ScriptController.cpp

@@ -16,7 +16,11 @@ void luaRegister_ScriptController()
         {"loadUrl", lua_ScriptController_loadUrl},
         {NULL, NULL}
     };
-    const luaL_Reg* lua_statics = NULL;
+    const luaL_Reg lua_statics[] = 
+    {
+        {"print", lua_ScriptController_static_print},
+        {NULL, NULL}
+    };
     std::vector<std::string> scopePath;
 
     ScriptUtil::registerClass("ScriptController", lua_members, NULL, NULL, lua_statics, scopePath);
@@ -132,4 +136,62 @@ int lua_ScriptController_loadUrl(lua_State* state)
     return 0;
 }
 
+int lua_ScriptController_static_print(lua_State* state)
+{
+    // Get the number of parameters.
+    int paramCount = lua_gettop(state);
+
+    // Attempt to match the parameters to a valid binding.
+    switch (paramCount)
+    {
+        case 1:
+        {
+            if ((lua_type(state, 1) == LUA_TSTRING || lua_type(state, 1) == LUA_TNIL))
+            {
+                // Get parameter 1 off the stack.
+                const char* param1 = ScriptUtil::getString(1, false);
+
+                ScriptController::print(param1);
+                
+                return 0;
+            }
+            else
+            {
+                lua_pushstring(state, "lua_ScriptController_static_print - Failed to match the given parameters to a valid function signature.");
+                lua_error(state);
+            }
+            break;
+        }
+        case 2:
+        {
+            if ((lua_type(state, 1) == LUA_TSTRING || lua_type(state, 1) == LUA_TNIL) &&
+                (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL))
+            {
+                // Get parameter 1 off the stack.
+                const char* param1 = ScriptUtil::getString(1, false);
+
+                // Get parameter 2 off the stack.
+                const char* param2 = ScriptUtil::getString(2, false);
+
+                ScriptController::print(param1, param2);
+                
+                return 0;
+            }
+            else
+            {
+                lua_pushstring(state, "lua_ScriptController_static_print - Failed to match the given parameters to a valid function signature.");
+                lua_error(state);
+            }
+            break;
+        }
+        default:
+        {
+            lua_pushstring(state, "Invalid number of parameters (expected 1 or 2).");
+            lua_error(state);
+            break;
+        }
+    }
+    return 0;
+}
+
 }

+ 1 - 0
gameplay/src/lua/lua_ScriptController.h

@@ -7,6 +7,7 @@ namespace gameplay
 // Lua bindings for ScriptController.
 int lua_ScriptController_loadScript(lua_State* state);
 int lua_ScriptController_loadUrl(lua_State* state);
+int lua_ScriptController_static_print(lua_State* state);
 
 void luaRegister_ScriptController();