2
0

ScriptEvent.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. {
  14. ScriptEvent::ScriptEvent(EventManager* manager)
  15. : Event(manager)
  16. {
  17. }
  18. ScriptEvent::~ScriptEvent()
  19. {
  20. m_script.destroy(getAllocator());
  21. }
  22. Error ScriptEvent::init(Second startTime, Second duration, CString script)
  23. {
  24. Event::init(startTime, duration);
  25. // Create the env
  26. ANKI_CHECK(m_env.init(&getSceneGraph().getScriptManager()));
  27. // Do the rest
  28. StringAuto extension(getAllocator());
  29. getFilepathExtension(script, extension);
  30. if(!extension.isEmpty() && extension == "lua")
  31. {
  32. // It's a file
  33. ANKI_CHECK(getSceneGraph().getResourceManager().loadResource(script, m_scriptRsrc));
  34. // Exec the script
  35. ANKI_CHECK(m_env.evalString(m_scriptRsrc->getSource()));
  36. }
  37. else
  38. {
  39. // It's a string
  40. m_script.create(getAllocator(), script);
  41. // Exec the script
  42. ANKI_CHECK(m_env.evalString(m_script.toCString()));
  43. }
  44. return Error::NONE;
  45. }
  46. Error ScriptEvent::update(Second prevUpdateTime, Second crntTime)
  47. {
  48. lua_State* lua = &m_env.getLuaState();
  49. // Push function name
  50. lua_getglobal(lua, "update");
  51. // Push args
  52. LuaBinder::pushVariableToTheStack(lua, static_cast<Event*>(this));
  53. lua_pushnumber(lua, prevUpdateTime);
  54. lua_pushnumber(lua, crntTime);
  55. // Do the call (3 arguments, 1 result)
  56. if(lua_pcall(lua, 3, 1, 0) != 0)
  57. {
  58. ANKI_SCENE_LOGE("Error running ScriptEvent's \"update\": %s", lua_tostring(lua, -1));
  59. return Error::USER_DATA;
  60. }
  61. if(!lua_isnumber(lua, -1))
  62. {
  63. ANKI_SCENE_LOGE("ScriptEvent's \"update\" should return a number");
  64. lua_pop(lua, 1);
  65. return Error::USER_DATA;
  66. }
  67. // Get the result
  68. lua_Number result = lua_tonumber(lua, -1);
  69. lua_pop(lua, 1);
  70. if(result < 0)
  71. {
  72. ANKI_SCENE_LOGE("ScriptEvent's \"update\" return an error code");
  73. return Error::USER_DATA;
  74. }
  75. return Error::NONE;
  76. }
  77. Error ScriptEvent::onKilled(Second prevUpdateTime, Second crntTime)
  78. {
  79. lua_State* lua = &m_env.getLuaState();
  80. // Push function name
  81. lua_getglobal(lua, "onKilled");
  82. // Push args
  83. LuaBinder::pushVariableToTheStack(lua, static_cast<Event*>(this));
  84. lua_pushnumber(lua, prevUpdateTime);
  85. lua_pushnumber(lua, crntTime);
  86. // Do the call (3 arguments, 1 result)
  87. if(lua_pcall(lua, 3, 1, 0) != 0)
  88. {
  89. ANKI_SCENE_LOGE("Error running ScriptEvent's \"onKilled\": %s", lua_tostring(lua, -1));
  90. return Error::USER_DATA;
  91. }
  92. if(!lua_isnumber(lua, -1))
  93. {
  94. ANKI_SCENE_LOGE("ScriptEvent's \"onKilled\" should return a number");
  95. lua_pop(lua, 1);
  96. return Error::USER_DATA;
  97. }
  98. // Get the result
  99. lua_Number result = lua_tonumber(lua, -1);
  100. lua_pop(lua, 1);
  101. if(result < 0)
  102. {
  103. ANKI_SCENE_LOGE("ScriptEvent's \"onKilled\" return an error code");
  104. return Error::USER_DATA;
  105. }
  106. return Error::NONE;
  107. }
  108. } // end namespace anki