LuaStateManager.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #pragma once
  2. // GameCode4 is the sample application that encapsulates much of the source code
  3. // discussed in "Game Coding Complete - 4th Edition" by Mike McShaffry and David
  4. // "Rez" Graham, published by Charles River Media.
  5. // ISBN-10: 1133776574 | ISBN-13: 978-1133776574
  6. //
  7. // If this source code has found it's way to you, and you think it has helped you
  8. // in any way, do the authors a favor and buy a new copy of the book - there are
  9. // detailed explanations in it that compliment this code well. Buy a copy at Amazon.com
  10. // by clicking here:
  11. // http://www.amazon.com/gp/product/1133776574/ref=olp_product_details?ie=UTF8&me=&seller=
  12. //
  13. // There's a companion web site at http://www.mcshaffry.com/GameCode/
  14. //
  15. // The source code is managed and maintained through Google Code:
  16. // http://code.google.com/p/gamecode4/
  17. //
  18. // (c) Copyright 2012 Michael L. McShaffry and David Graham
  19. //
  20. // This program is free software; you can redistribute it and/or
  21. // modify it under the terms of the GNU Lesser GPL v3
  22. // as published by the Free Software Foundation.
  23. //
  24. // This program is distributed in the hope that it will be useful,
  25. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  27. // http://www.gnu.org/licenses/lgpl-3.0.txt for more details.
  28. //
  29. // You should have received a copy of the GNU Lesser GPL v3
  30. // along with this program; if not, write to the Free Software
  31. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  32. //
  33. //========================================================================
  34. #include <sol/sol.hpp>
  35. #include "../Unix/GameCodeStd.h"
  36. #include "../Debugging/Logger.h"
  37. #include "../GameCode4/interfaces.h"
  38. //---------------------------------------------------------------------------------------------------------------------
  39. // LuaStateManager - Chapter 12, page 367
  40. //---------------------------------------------------------------------------------------------------------------------
  41. class LuaStateManager : public IScriptManager
  42. {
  43. private:
  44. static LuaStateManager* s_pSingleton;
  45. sol::state m_luaState;
  46. std::string m_lastError;
  47. public:
  48. // singleton functions
  49. static bool Create(void);
  50. static void Destroy(void);
  51. static LuaStateManager* Get(void) { GCC_ASSERT(s_pSingleton); return s_pSingleton; }
  52. virtual bool VInit(void) override;
  53. virtual void VExecuteFile(const char* resource) override;
  54. virtual void VExecuteString(const char* str) override;
  55. sol::global_table GetGlobalVars(void);
  56. sol::state_view GetLuaState(void);
  57. // public helpers
  58. sol::table CreatePath(const char* pathString, bool toIgnoreLastElement = false);
  59. private:
  60. void SetError(sol::error const& err);
  61. // private constructor & destructor; call the static Create() and Destroy() functions instead
  62. explicit LuaStateManager(void) = default;
  63. virtual ~LuaStateManager(void) = default;
  64. };