LuaStateManager.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // GameCode4 is the sample application that encapsulates much of the source code
  2. // discussed in "Game Coding Complete - 4th Edition" by Mike McShaffry and David
  3. // "Rez" Graham, published by Charles River Media.
  4. // ISBN-10: 1133776574 | ISBN-13: 978-1133776574
  5. //
  6. // If this source code has found it's way to you, and you think it has helped you
  7. // in any way, do the authors a favor and buy a new copy of the book - there are
  8. // detailed explanations in it that compliment this code well. Buy a copy at Amazon.com
  9. // by clicking here:
  10. // http://www.amazon.com/gp/product/1133776574/ref=olp_product_details?ie=UTF8&me=&seller=
  11. //
  12. // There's a companion web site at http://www.mcshaffry.com/GameCode/
  13. //
  14. // The source code is managed and maintained through Google Code:
  15. // http://code.google.com/p/gamecode4/
  16. //
  17. // (c) Copyright 2012 Michael L. McShaffry and David Graham
  18. //
  19. // This program is free software; you can redistribute it and/or
  20. // modify it under the terms of the GNU Lesser GPL v3
  21. // as published by the Free Software Foundation.
  22. //
  23. // This program is distributed in the hope that it will be useful,
  24. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  26. // http://www.gnu.org/licenses/lgpl-3.0.txt for more details.
  27. //
  28. // You should have received a copy of the GNU Lesser GPL v3
  29. // along with this program; if not, write to the Free Software
  30. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  31. //
  32. //========================================================================
  33. #include "LuaStateManager.h"
  34. #include "../Unix/GameCodeStd.h"
  35. #include "../Debugging/Logger.h"
  36. #include "../Utilities/String.h"
  37. LuaStateManager* LuaStateManager::s_pSingleton = NULL;
  38. bool LuaStateManager::Create(void)
  39. {
  40. if (s_pSingleton)
  41. {
  42. GCC_ERROR("Overwriting LuaStateManager singleton");
  43. SAFE_DELETE(s_pSingleton);
  44. }
  45. s_pSingleton = GCC_NEW LuaStateManager;
  46. if (s_pSingleton)
  47. return s_pSingleton->VInit();
  48. return false;
  49. }
  50. void LuaStateManager::Destroy(void)
  51. {
  52. GCC_ASSERT(s_pSingleton);
  53. SAFE_DELETE(s_pSingleton);
  54. }
  55. bool LuaStateManager::VInit(void)
  56. {
  57. m_luaState.open_libraries(sol::lib::base);
  58. // // register functions
  59. m_luaState["ExecuteFile"] = [this](const std::string& path) {
  60. return this->VExecuteFile(path.c_str());
  61. };
  62. m_luaState["ExecuteString"] = [this](const std::string& chunk) {
  63. return this->VExecuteString(chunk.c_str());
  64. };
  65. return true;
  66. }
  67. void LuaStateManager::VExecuteFile(const char* path)
  68. {
  69. try
  70. {
  71. auto result = m_luaState.script_file(path);
  72. }
  73. catch(sol::error const& err)
  74. {
  75. SetError(err);
  76. }
  77. }
  78. void LuaStateManager::VExecuteString(const char* chunk)
  79. {
  80. try
  81. {
  82. // Most strings are passed straight through to the Lua interpreter
  83. if (strlen(chunk) <= 1 || chunk[0] != '=')
  84. {
  85. m_luaState.script(chunk);
  86. }
  87. else // If the string starts with '=', wrap the statement in the print() function.
  88. {
  89. std::string buffer("print(");
  90. buffer += (chunk + 1);
  91. buffer += ")";
  92. m_luaState.script(buffer);
  93. }
  94. }
  95. catch(sol::error const& err)
  96. {
  97. SetError(err);
  98. }
  99. }
  100. void LuaStateManager::SetError(sol::error const& err)
  101. {
  102. m_lastError = err.what();
  103. GCC_ERROR(m_lastError);
  104. }
  105. sol::global_table LuaStateManager::GetGlobalVars(void)
  106. {
  107. return m_luaState.globals();
  108. }
  109. sol::state_view LuaStateManager::GetLuaState(void)
  110. {
  111. return m_luaState;
  112. }
  113. sol::table LuaStateManager::CreatePath(const char* pathString, bool toIgnoreLastElement)
  114. {
  115. StringVec splitPath;
  116. Split(pathString, splitPath, '.');
  117. if (toIgnoreLastElement)
  118. splitPath.pop_back();
  119. sol::table context = GetGlobalVars();
  120. for (auto it=splitPath.begin(); it != splitPath.end(); ++it)
  121. {
  122. // make sure we still have a valid context
  123. if (context == sol::lua_nil)
  124. {
  125. GCC_ERROR("Something broke in CreatePath(); bailing out (element == " + (*it) + ")");
  126. return context; // this will be nil
  127. }
  128. // grab whatever exists for this element
  129. const std::string& element = (*it);
  130. sol::object curr = context[element];
  131. if (!curr.is<sol::table>())
  132. {
  133. // if the element is not a table and not nil, we clobber it
  134. if (curr != sol::lua_nil)
  135. {
  136. GCC_WARNING("Overwriting element '" + element + "' in table");
  137. context[element] = sol::lua_nil;
  138. }
  139. context[element] = sol::state_view(context.lua_state()).create_table();
  140. }
  141. context = context[element];
  142. }
  143. return context;
  144. }