LuaScriptInstance.cpp 7.5 KB

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