LuaScriptInstance.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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) :
  39. Component(context),
  40. scriptObjectRef_(LUA_REFNIL)
  41. {
  42. luaScript_ = GetSubsystem<LuaScript>();
  43. luaState_ = luaScript_->GetLuaState();
  44. }
  45. LuaScriptInstance::~LuaScriptInstance()
  46. {
  47. // Unref script object.
  48. if (scriptObjectRef_ != LUA_REFNIL)
  49. luaL_unref(luaState_, LUA_REGISTRYINDEX, scriptObjectRef_);
  50. // Unref Lua function.
  51. for (HashMap<StringHash, int>::Iterator i = eventTypeToFunctionRefMap_.Begin(); i != eventTypeToFunctionRefMap_.End(); ++i)
  52. luaL_unref(luaState_, LUA_REGISTRYINDEX, i->second_);
  53. int top = lua_gettop(luaState_);
  54. lua_getglobal(luaState_, "DestroyScriptObjectInstance");
  55. if (!lua_isfunction(luaState_, -1))
  56. {
  57. LOGERROR("Can not find lua function DestroyScriptObjectInstance.");
  58. lua_settop(luaState_, top);
  59. return;
  60. }
  61. // Push this as second parameter.
  62. tolua_pushusertype(luaState_, (void*)this, "LuaScriptInstance");
  63. // Call ObjectType:new function.
  64. if (lua_pcall(luaState_, 1, 0, 0) != 0)
  65. {
  66. const char* message = lua_tostring(luaState_, -1);
  67. LOGERROR("Execute Lua function failed: " + String(message));
  68. lua_settop(luaState_, top);
  69. return;
  70. }
  71. }
  72. void LuaScriptInstance::RegisterObject(Context* context)
  73. {
  74. context->RegisterFactory<LuaScriptInstance>();
  75. }
  76. bool LuaScriptInstance::CreateObject(const String& objectType)
  77. {
  78. if (objectType_ == objectType)
  79. return true;
  80. int top = lua_gettop(luaState_);
  81. lua_getglobal(luaState_, "CreateScriptObjectInstance");
  82. if (!lua_isfunction(luaState_, -1))
  83. {
  84. LOGERROR("Can not find lua function CreateScriptObjectInstance.");
  85. lua_settop(luaState_, top);
  86. return false;
  87. }
  88. // Get table as first paramter.
  89. lua_getglobal(luaState_, objectType.CString());
  90. if (!lua_istable(luaState_, -1))
  91. {
  92. LOGERROR("Can not find lua table " + objectType + ".");
  93. lua_settop(luaState_, top);
  94. return false;
  95. }
  96. // Push this as second parameter.
  97. tolua_pushusertype(luaState_, (void*)this, "LuaScriptInstance");
  98. // Call ObjectType:new function.
  99. if (lua_pcall(luaState_, 2, 1, 0) != 0)
  100. {
  101. const char* message = lua_tostring(luaState_, -1);
  102. LOGERROR("Execute Lua function failed: " + String(message));
  103. lua_settop(luaState_, top);
  104. return false;
  105. }
  106. objectType_ = objectType;
  107. scriptObjectRef_ = luaL_ref(luaState_, LUA_REGISTRYINDEX);
  108. return true;
  109. }
  110. void LuaScriptInstance::ScriptSubscribeToEvent(const String& eventName, const String& functionName)
  111. {
  112. StringHash eventType(eventName);
  113. String realFunctionName = functionName.Replaced(":", ".");
  114. HashSet<Object*>* receivers = context_->GetEventReceivers(eventType);
  115. if (!receivers || !receivers->Contains(this))
  116. SubscribeToEvent(eventType, HANDLER(LuaScriptInstance, HandleEvent));
  117. int functionRef = LUA_REFNIL;
  118. if (luaScript_->FindFunction(realFunctionName))
  119. functionRef = luaL_ref(luaState_, LUA_REGISTRYINDEX);
  120. eventTypeToFunctionRefMap_[eventType] = functionRef;
  121. }
  122. void LuaScriptInstance::ScriptSubscribeToEvent(void* object, const String& eventName, const String& functionName)
  123. {
  124. StringHash eventType(eventName);
  125. String realFunctionName = functionName.Replaced(":", ".");
  126. Object* sender = (Object*)object;
  127. HashSet<Object*>* receivers = context_->GetEventReceivers(sender, eventType);
  128. if (!receivers || !receivers->Contains(this))
  129. SubscribeToEvent(sender, eventType, HANDLER(LuaScriptInstance, HandleObjectEvent));
  130. int functionRef = LUA_REFNIL;
  131. if (luaScript_->FindFunction(realFunctionName))
  132. functionRef = luaL_ref(luaState_, LUA_REGISTRYINDEX);
  133. objectToEventTypeToFunctionRefMap_[sender][eventType] = functionRef;
  134. }
  135. void LuaScriptInstance::HandleEvent(StringHash eventType, VariantMap& eventData)
  136. {
  137. if (scriptObjectRef_ == LUA_REFNIL)
  138. return;
  139. int functionRef = eventTypeToFunctionRefMap_[eventType];
  140. if (functionRef == LUA_REFNIL)
  141. return;
  142. CallEventHandler(functionRef, eventType, eventData);
  143. }
  144. void LuaScriptInstance::HandleObjectEvent(StringHash eventType, VariantMap& eventData)
  145. {
  146. if (scriptObjectRef_ == LUA_REFNIL)
  147. return;
  148. Object* object = GetEventSender();
  149. int functionRef = objectToEventTypeToFunctionRefMap_[object][eventType];
  150. if (functionRef == LUA_REFNIL)
  151. return;
  152. CallEventHandler(functionRef, eventType, eventData);
  153. }
  154. void LuaScriptInstance::CallEventHandler(int functionRef, StringHash eventType, VariantMap& eventData )
  155. {
  156. int top = lua_gettop(luaState_);
  157. // Push function.
  158. lua_rawgeti(luaState_, LUA_REGISTRYINDEX, functionRef);
  159. // Push script object.
  160. lua_rawgeti(luaState_, LUA_REGISTRYINDEX, scriptObjectRef_);
  161. // Push event type.
  162. tolua_pushusertype(luaState_, (void*)&eventType, "StringHash");
  163. // Push event data.
  164. tolua_pushusertype(luaState_, (void*)&eventData, "VariantMap");
  165. if (lua_pcall(luaState_, 3, 0, 0) != 0)
  166. {
  167. const char* message = lua_tostring(luaState_, -1);
  168. LOGERROR("Execute Lua function failed: " + String(message));
  169. lua_settop(luaState_, top);
  170. return;
  171. }
  172. }
  173. }