LuaScriptInstance.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. //
  2. // Copyright (c) 2008-2013 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "Context.h"
  24. #include "Log.h"
  25. #include "LuaScript.h"
  26. #include "LuaScriptInstance.h"
  27. #include "ResourceCache.h"
  28. #include "ProcessUtils.h"
  29. extern "C"
  30. {
  31. #include <lua.h>
  32. #include <lualib.h>
  33. #include <lauxlib.h>
  34. }
  35. #include "tolua++.h"
  36. namespace Urho3D
  37. {
  38. LuaScriptInstance::LuaScriptInstance(Context* context) : Component(context)
  39. {
  40. }
  41. LuaScriptInstance::~LuaScriptInstance()
  42. {
  43. if (objectType_.Empty())
  44. return;
  45. LuaScript* luaScript = GetSubsystem<LuaScript>();
  46. lua_State* luaState = luaScript->GetLuaState();
  47. int top = lua_gettop(luaState);
  48. lua_getglobal(luaState, "DestroyScriptObjectInstance");
  49. if (!lua_isfunction(luaState, -1))
  50. {
  51. LOGERROR("Can not find lua function DestroyScriptObjectInstance.");
  52. lua_settop(luaState, top);
  53. return;
  54. }
  55. // Push this as second parameter.
  56. tolua_pushusertype(luaState, (void*)this, "LuaScriptInstance");
  57. // Call ObjectType:new function.
  58. if (lua_pcall(luaState, 1, 0, 0) != 0)
  59. {
  60. const char* message = lua_tostring(luaState, -1);
  61. LOGERROR("Execute Lua function failed: " + String(message));
  62. lua_settop(luaState, top);
  63. return;
  64. }
  65. }
  66. void LuaScriptInstance::RegisterObject(Context* context)
  67. {
  68. context->RegisterFactory<LuaScriptInstance>();
  69. }
  70. bool LuaScriptInstance::CreateObject(const String& objectType)
  71. {
  72. if (objectType_ == objectType)
  73. return true;
  74. LuaScript* luaScript = GetSubsystem<LuaScript>();
  75. lua_State* luaState = luaScript->GetLuaState();
  76. int top = lua_gettop(luaState);
  77. lua_getglobal(luaState, "CreateScriptObjectInstance");
  78. if (!lua_isfunction(luaState, -1))
  79. {
  80. LOGERROR("Can not find lua function CreateScriptObjectInstance.");
  81. lua_settop(luaState, top);
  82. return false;
  83. }
  84. // Get table as first paramter.
  85. lua_getglobal(luaState, objectType.CString());
  86. if (!lua_istable(luaState, -1))
  87. {
  88. LOGERROR("Can not find lua table " + objectType + ".");
  89. lua_settop(luaState, top);
  90. return false;
  91. }
  92. // Push this as second parameter.
  93. tolua_pushusertype(luaState, (void*)this, "LuaScriptInstance");
  94. // Call ObjectType:new function.
  95. if (lua_pcall(luaState, 2, 1, 0) != 0)
  96. {
  97. const char* message = lua_tostring(luaState, -1);
  98. LOGERROR("Execute Lua function failed: " + String(message));
  99. lua_settop(luaState, top);
  100. return false;
  101. }
  102. objectType_ = objectType;
  103. return true;
  104. }
  105. void LuaScriptInstance::ScriptSubscribeToEvent(const String& eventName, const String& functionName)
  106. {
  107. StringHash eventType(eventName);
  108. String realFunctionName = functionName.Replaced(":", ".");
  109. HashSet<Object*>* receivers = context_->GetEventReceivers(eventType);
  110. if (!receivers || !receivers->Contains(this))
  111. SubscribeToEvent(eventType, HANDLER(LuaScriptInstance, HandleEvent));
  112. eventTypeToFunctionNameMap_[eventType].Insert(realFunctionName);
  113. }
  114. bool LuaScriptInstance::PushScriptObject(lua_State* luaState)
  115. {
  116. tolua_pushusertype(luaState, (void*)this, "LuaScriptInstance");
  117. tolua_pushstring(luaState, "object");
  118. lua_gettable(luaState, -2);
  119. if (!lua_istable(luaState, -1))
  120. {
  121. return false;
  122. }
  123. lua_remove(luaState, -2);
  124. return true;
  125. }
  126. void LuaScriptInstance::HandleEvent(StringHash eventType, VariantMap& eventData)
  127. {
  128. const HashSet<String>& functionNames = eventTypeToFunctionNameMap_[eventType];
  129. for (HashSet<String>::ConstIterator i = functionNames.Begin(); i != functionNames.End(); ++i)
  130. CallEventHandler(*i, eventType, eventData);
  131. }
  132. void LuaScriptInstance::CallEventHandler(const String& functionName, StringHash eventType, VariantMap& eventData )
  133. {
  134. LuaScript* luaScript = GetSubsystem<LuaScript>();
  135. lua_State* luaState = luaScript->GetLuaState();
  136. int top = lua_gettop(luaState);
  137. if (!luaScript->FindFunction(functionName))
  138. {
  139. lua_settop(luaState, top);
  140. return;
  141. }
  142. if (!PushScriptObject(luaState))
  143. {
  144. lua_settop(luaState, top);
  145. return;
  146. }
  147. tolua_pushusertype(luaState, (void*)&eventType, "StringHash");
  148. tolua_pushusertype(luaState, (void*)&eventData, "VariantMap");
  149. if (lua_pcall(luaState, 3, 0, 0) != 0)
  150. {
  151. const char* message = lua_tostring(luaState, -1);
  152. LOGERROR("Execute Lua function failed: " + String(message));
  153. lua_settop(luaState, top);
  154. return;
  155. }
  156. }
  157. }