ScriptEvent.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/Scene/Events/ScriptEvent.h>
  6. #include <AnKi/Util/Filesystem.h>
  7. #include <AnKi/Resource/ScriptResource.h>
  8. #include <AnKi/Script/ScriptEnvironment.h>
  9. #include <AnKi/Script/ScriptManager.h>
  10. #include <AnKi/Scene/SceneGraph.h>
  11. #include <AnKi/Resource/ResourceManager.h>
  12. namespace anki {
  13. ScriptEvent::ScriptEvent(EventManager* manager)
  14. : Event(manager)
  15. {
  16. }
  17. ScriptEvent::~ScriptEvent()
  18. {
  19. m_script.destroy(getAllocator());
  20. }
  21. Error ScriptEvent::init(Second startTime, Second duration, CString script)
  22. {
  23. Event::init(startTime, duration);
  24. // Create the env
  25. ANKI_CHECK(m_env.init(&getSceneGraph().getScriptManager()));
  26. // Do the rest
  27. StringAuto extension(getAllocator());
  28. getFilepathExtension(script, extension);
  29. if(!extension.isEmpty() && extension == "lua")
  30. {
  31. // It's a file
  32. ANKI_CHECK(getSceneGraph().getResourceManager().loadResource(script, m_scriptRsrc));
  33. // Exec the script
  34. ANKI_CHECK(m_env.evalString(m_scriptRsrc->getSource()));
  35. }
  36. else
  37. {
  38. // It's a string
  39. m_script.create(getAllocator(), script);
  40. // Exec the script
  41. ANKI_CHECK(m_env.evalString(m_script.toCString()));
  42. }
  43. return Error::NONE;
  44. }
  45. Error ScriptEvent::update(Second prevUpdateTime, Second crntTime)
  46. {
  47. lua_State* lua = &m_env.getLuaState();
  48. // Push function name
  49. lua_getglobal(lua, "update");
  50. // Push args
  51. LuaBinder::pushVariableToTheStack(lua, static_cast<Event*>(this));
  52. lua_pushnumber(lua, prevUpdateTime);
  53. lua_pushnumber(lua, crntTime);
  54. // Do the call (3 arguments, 1 result)
  55. if(lua_pcall(lua, 3, 1, 0) != 0)
  56. {
  57. ANKI_SCENE_LOGE("Error running ScriptEvent's \"update\": %s", lua_tostring(lua, -1));
  58. return Error::USER_DATA;
  59. }
  60. if(!lua_isnumber(lua, -1))
  61. {
  62. ANKI_SCENE_LOGE("ScriptEvent's \"update\" should return a number");
  63. lua_pop(lua, 1);
  64. return Error::USER_DATA;
  65. }
  66. // Get the result
  67. lua_Number result = lua_tonumber(lua, -1);
  68. lua_pop(lua, 1);
  69. if(result < 0)
  70. {
  71. ANKI_SCENE_LOGE("ScriptEvent's \"update\" return an error code");
  72. return Error::USER_DATA;
  73. }
  74. return Error::NONE;
  75. }
  76. Error ScriptEvent::onKilled(Second prevUpdateTime, Second crntTime)
  77. {
  78. lua_State* lua = &m_env.getLuaState();
  79. // Push function name
  80. lua_getglobal(lua, "onKilled");
  81. // Push args
  82. LuaBinder::pushVariableToTheStack(lua, static_cast<Event*>(this));
  83. lua_pushnumber(lua, prevUpdateTime);
  84. lua_pushnumber(lua, crntTime);
  85. // Do the call (3 arguments, 1 result)
  86. if(lua_pcall(lua, 3, 1, 0) != 0)
  87. {
  88. ANKI_SCENE_LOGE("Error running ScriptEvent's \"onKilled\": %s", lua_tostring(lua, -1));
  89. return Error::USER_DATA;
  90. }
  91. if(!lua_isnumber(lua, -1))
  92. {
  93. ANKI_SCENE_LOGE("ScriptEvent's \"onKilled\" should return a number");
  94. lua_pop(lua, 1);
  95. return Error::USER_DATA;
  96. }
  97. // Get the result
  98. lua_Number result = lua_tonumber(lua, -1);
  99. lua_pop(lua, 1);
  100. if(result < 0)
  101. {
  102. ANKI_SCENE_LOGE("ScriptEvent's \"onKilled\" return an error code");
  103. return Error::USER_DATA;
  104. }
  105. return Error::NONE;
  106. }
  107. } // end namespace anki