ScriptComponent.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright (C) 2009-present, 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/Components/ScriptComponent.h>
  6. #include <AnKi/Scene/SceneGraph.h>
  7. #include <AnKi/Resource/ResourceManager.h>
  8. #include <AnKi/Resource/ScriptResource.h>
  9. #include <AnKi/Script/ScriptManager.h>
  10. #include <AnKi/Script/ScriptEnvironment.h>
  11. namespace anki {
  12. ScriptComponent::ScriptComponent(SceneNode* node)
  13. : SceneComponent(node, kClassType)
  14. {
  15. ANKI_ASSERT(node);
  16. }
  17. ScriptComponent::~ScriptComponent()
  18. {
  19. deleteInstance(SceneMemoryPool::getSingleton(), m_environments[0]);
  20. deleteInstance(SceneMemoryPool::getSingleton(), m_environments[1]);
  21. }
  22. void ScriptComponent::setScriptResourceFilename(CString fname)
  23. {
  24. if(fname.isEmpty())
  25. {
  26. deleteInstance(SceneMemoryPool::getSingleton(), m_environments[1]);
  27. m_environments[1] = nullptr;
  28. m_resource.reset(nullptr);
  29. return;
  30. }
  31. // Load
  32. ScriptResourcePtr rsrc;
  33. Error err = ResourceManager::getSingleton().loadResource(fname, rsrc);
  34. // Create the env
  35. ScriptEnvironment* newEnv = nullptr;
  36. if(!err)
  37. {
  38. newEnv = newInstance<ScriptEnvironment>(SceneMemoryPool::getSingleton());
  39. }
  40. // Exec the script
  41. if(!err)
  42. {
  43. err = newEnv->evalString(m_resource->getSource());
  44. }
  45. // Error
  46. if(err)
  47. {
  48. ANKI_SCENE_LOGE("Failed to load the script");
  49. deleteInstance(SceneMemoryPool::getSingleton(), newEnv);
  50. }
  51. else
  52. {
  53. m_resource = std::move(rsrc);
  54. deleteInstance(SceneMemoryPool::getSingleton(), m_environments[1]);
  55. m_environments[1] = newEnv;
  56. }
  57. }
  58. CString ScriptComponent::getScriptResourceFilename() const
  59. {
  60. if(ANKI_EXPECT(m_resource.isCreated()))
  61. {
  62. return m_resource->getFilename();
  63. }
  64. else
  65. {
  66. return "*Error*";
  67. }
  68. }
  69. void ScriptComponent::setScriptText(CString text)
  70. {
  71. if(text.isEmpty())
  72. {
  73. deleteInstance(SceneMemoryPool::getSingleton(), m_environments[0]);
  74. m_environments[0] = nullptr;
  75. m_text.destroy();
  76. return;
  77. }
  78. // Create the env
  79. ScriptEnvironment* newEnv = newInstance<ScriptEnvironment>(SceneMemoryPool::getSingleton());
  80. // Exec the script
  81. const Error err = newEnv->evalString(text);
  82. // Error
  83. if(err)
  84. {
  85. ANKI_SCENE_LOGE("Failed to load the script");
  86. deleteInstance(SceneMemoryPool::getSingleton(), newEnv);
  87. }
  88. else
  89. {
  90. m_text = text;
  91. deleteInstance(SceneMemoryPool::getSingleton(), m_environments[0]);
  92. m_environments[0] = newEnv;
  93. }
  94. }
  95. CString ScriptComponent::getScriptText() const
  96. {
  97. if(ANKI_EXPECT(m_text))
  98. {
  99. return m_text;
  100. }
  101. else
  102. {
  103. return "*Error*";
  104. }
  105. }
  106. void ScriptComponent::update(SceneComponentUpdateInfo& info, Bool& updated)
  107. {
  108. updated = false;
  109. if(!m_environments[0] && !m_environments[1])
  110. {
  111. return;
  112. }
  113. lua_State* lua = (m_environments[0]) ? &m_environments[0]->getLuaState() : &m_environments[1]->getLuaState();
  114. // Push function name
  115. lua_getglobal(lua, "update");
  116. // Push args
  117. LuaBinder::pushVariableToTheStack(lua, info.m_node);
  118. lua_pushnumber(lua, info.m_previousTime);
  119. lua_pushnumber(lua, info.m_currentTime);
  120. // Do the call (3 arguments, no result)
  121. if(lua_pcall(lua, 3, 0, 0) != 0)
  122. {
  123. ANKI_SCENE_LOGE("Error running ScriptComponent's \"update\": %s", lua_tostring(lua, -1));
  124. return;
  125. }
  126. updated = true;
  127. }
  128. Error ScriptComponent::serialize(SceneSerializer& serializer)
  129. {
  130. ANKI_SERIALIZE(m_resource, 1);
  131. ANKI_SERIALIZE(m_text, 1);
  132. // TODO Serialize environments
  133. return Error::kNone;
  134. }
  135. } // end namespace anki