Browse Source

Report Lua script error in ErrorDialog.

Aster Jian 12 years ago
parent
commit
51a1e1bd3f
1 changed files with 18 additions and 20 deletions
  1. 18 20
      Extras/LuaScript/LuaScript.cpp

+ 18 - 20
Extras/LuaScript/LuaScript.cpp

@@ -25,6 +25,7 @@
 #include "File.h"
 #include "File.h"
 #include "Log.h"
 #include "Log.h"
 #include "LuaScript.h"
 #include "LuaScript.h"
+#include "ProcessUtils.h"
 #include "Profiler.h"
 #include "Profiler.h"
 #include "ResourceCache.h"
 #include "ResourceCache.h"
 #include "Scene.h"
 #include "Scene.h"
@@ -69,7 +70,7 @@ LuaScript::LuaScript(Context* context) :
     luaState_ = luaL_newstate();
     luaState_ = luaL_newstate();
     if (!luaState_)
     if (!luaState_)
     {
     {
-        LOGERROR("Could not create Lua state.");
+        ErrorDialog("Lua Error", "Could not create Lua state.");
         return;
         return;
     }
     }
 
 
@@ -126,18 +127,17 @@ bool LuaScript::ExecuteFile(const String& fileName)
     if (error)
     if (error)
     {
     {
         const char* message = lua_tostring(luaState_, -1);
         const char* message = lua_tostring(luaState_, -1);
+
+        ErrorDialog("Execute Lua File Failed", message);
         lua_settop(luaState_, top);
         lua_settop(luaState_, top);
-        LOGRAW(String("Lua: Unable to execute Lua file '") + fileName + "'. ");
-        LOGRAW(String("Lua: ") + message);
         return false;
         return false;
     }
     }
 
 
     if (lua_pcall(luaState_, 0, 0, 0))
     if (lua_pcall(luaState_, 0, 0, 0))
     {
     {
         const char* message = lua_tostring(luaState_, -1);
         const char* message = lua_tostring(luaState_, -1);
+        ErrorDialog("Execute Lua File Failed", message);
         lua_settop(luaState_, top);
         lua_settop(luaState_, top);
-        LOGRAW(String("Lua: Unable to execute Lua script file '") + fileName + "'.");
-        LOGRAW(String("Lua: ") + message);
         return false;
         return false;
     }
     }
 
 
@@ -155,9 +155,8 @@ bool LuaScript::ExecuteString(const String& string)
     if (luaL_dostring(luaState_, string.CString()) != 0)
     if (luaL_dostring(luaState_, string.CString()) != 0)
     {
     {
         const char* message = lua_tostring(luaState_, -1);
         const char* message = lua_tostring(luaState_, -1);
+        ErrorDialog("Execute Lua String Failed", message);
         lua_settop(luaState_, top);
         lua_settop(luaState_, top);
-        LOGRAW(String("Lua: Unable to execute Lua string '") + string + "'.");
-        LOGRAW(String("Lua: ") + message);
         return false;
         return false;
     }
     }
 
 
@@ -181,9 +180,8 @@ bool LuaScript::ExecuteFunction(const String& functionName)
     if (lua_pcall(luaState_, 0, 0, 0))
     if (lua_pcall(luaState_, 0, 0, 0))
     {
     {
         const char* message = lua_tostring(luaState_, -1);
         const char* message = lua_tostring(luaState_, -1);
+        ErrorDialog("Execute Lua Function Failed", message);
         lua_settop(luaState_, top);
         lua_settop(luaState_, top);
-        LOGRAW(String("Lua: Unable to execute Lua function '") + functionName + "'.");
-        LOGRAW(String("Lua: ") + message);
         return false;
         return false;
     }
     }
 
 
@@ -192,15 +190,15 @@ bool LuaScript::ExecuteFunction(const String& functionName)
 
 
 void LuaScript::ScriptSendEvent(const String& eventName, VariantMap& eventData)
 void LuaScript::ScriptSendEvent(const String& eventName, VariantMap& eventData)
 {
 {
-	SendEvent(StringHash(eventName), eventData);
+    SendEvent(StringHash(eventName), eventData);
 }
 }
 
 
 void LuaScript::ScriptSubscribeToEvent(const String& eventName, const String& functionName)
 void LuaScript::ScriptSubscribeToEvent(const String& eventName, const String& functionName)
 {
 {
     StringHash eventType(eventName);
     StringHash eventType(eventName);
 
 
-	if (!eventTypeToFunctionNameMap_.Contains(eventType))
-		SubscribeToEvent(eventType, HANDLER(LuaScript, HandleEvent));
+    if (!eventTypeToFunctionNameMap_.Contains(eventType))
+        SubscribeToEvent(eventType, HANDLER(LuaScript, HandleEvent));
 
 
     eventTypeToFunctionNameMap_[eventType].Push(functionName);
     eventTypeToFunctionNameMap_[eventType].Push(functionName);
 }
 }
@@ -256,7 +254,7 @@ bool LuaScript::FindFunction(const String& functionName)
     {       
     {       
         if (!lua_istable(luaState_, -1))
         if (!lua_istable(luaState_, -1))
         {
         {
-            LOGRAW(String("Lua: Unable to find Lua table: '") + currentName + "'.");
+            ErrorDialog("Can Not Find Lua Table", String("Table Name = '") + currentName + "'.");
             return false;
             return false;
         }
         }
 
 
@@ -266,17 +264,18 @@ bool LuaScript::FindFunction(const String& functionName)
             lua_getfield(luaState_, -1, splitedNames[i].CString());
             lua_getfield(luaState_, -1, splitedNames[i].CString());
             if (!lua_istable(luaState_, -1))
             if (!lua_istable(luaState_, -1))
             {
             {
-                LOGRAW(String("Lua: Unable to find Lua table: '") + currentName + "'.");
+                ErrorDialog("Can Not Find Lua Table", String("Table Name = '") + currentName + "'.");
                 return false;
                 return false;
             }
             }
         }
         }
 
 
+        currentName = currentName + "." + splitedNames.Back().CString();
         lua_getfield(luaState_, -1, splitedNames.Back().CString());
         lua_getfield(luaState_, -1, splitedNames.Back().CString());
     }
     }
 
 
     if (!lua_isfunction(luaState_, -1))
     if (!lua_isfunction(luaState_, -1))
     {
     {
-        LOGRAW(String("Lua: Unable to find Lua function: '") + currentName + "'.");
+        ErrorDialog("Can Not Find Lua Function", String("Function Name = '") + currentName + "'.");
         return false;
         return false;
     }
     }
 
 
@@ -288,13 +287,13 @@ void LuaScript::HandleEvent(StringHash eventType, VariantMap& eventData)
     HashMap<StringHash, Vector<String> >::ConstIterator it = eventTypeToFunctionNameMap_.Find(eventType);
     HashMap<StringHash, Vector<String> >::ConstIterator it = eventTypeToFunctionNameMap_.Find(eventType);
     if (it == eventTypeToFunctionNameMap_.End())
     if (it == eventTypeToFunctionNameMap_.End())
         return;
         return;
-	
-	const Vector<String>& functionNames = it->second_;
+
+    const Vector<String>& functionNames = it->second_;
     for (unsigned i = 0; i < functionNames.Size(); ++i)
     for (unsigned i = 0; i < functionNames.Size(); ++i)
     {
     {
         const String& functionName = functionNames[i];
         const String& functionName = functionNames[i];
 
 
-		int top = lua_gettop(luaState_);
+        int top = lua_gettop(luaState_);
 
 
         if (!FindFunction(functionName))
         if (!FindFunction(functionName))
         {
         {
@@ -308,9 +307,8 @@ void LuaScript::HandleEvent(StringHash eventType, VariantMap& eventData)
         if (lua_pcall(luaState_, 2, 0, 0) != 0)
         if (lua_pcall(luaState_, 2, 0, 0) != 0)
         {
         {
             const char* message = lua_tostring(luaState_, -1);
             const char* message = lua_tostring(luaState_, -1);
+            ErrorDialog("Execute Lua Function Failed", message);
             lua_settop(luaState_, top);
             lua_settop(luaState_, top);
-            LOGRAW(String("Lua: Unable to execute Lua function '") + functionName + "'.");
-            LOGRAW(String("Lua: ") + message);
             return;
             return;
         }
         }
     }
     }