LuaEnvironment.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "lua.hpp"
  25. #include "Config.h"
  26. #include "Types.h"
  27. #include "Macros.h"
  28. // HACK
  29. #include "MathTypes.h"
  30. namespace crown
  31. {
  32. enum LuaArgumentType
  33. {
  34. ARGUMENT_FLOAT
  35. };
  36. struct LuaResource;
  37. // HACK
  38. struct Actor;
  39. struct Unit;
  40. /// LuaEnvironment is a wrapper of a subset of Lua functions and
  41. /// provides utilities for extending Lua
  42. class LuaEnvironment
  43. {
  44. public:
  45. LuaEnvironment(lua_State* L);
  46. /// Loads and execute the given @a res_name Lua resource.
  47. void load_and_execute(const char* res_name);
  48. /// Loads and executes the given @a s lua string.
  49. void execute_string(const char* s);
  50. /// Loads the function with the given @a name and @a func into the table @a module.
  51. void load_module_function(const char* module, const char* name, const lua_CFunction func);
  52. void load_module_function(const char* module, const char* name, const char* value);
  53. void load_module_constructor(const char* module, const lua_CFunction func);
  54. /// Loads the enum with the given @a name and @a value into the table @a module.
  55. void load_module_enum(const char* module, const char* name, uint32_t value);
  56. /// Calls the global function @a func with @a argc argument number.
  57. /// Each argument is a pair (type, value).
  58. /// Example call:
  59. /// call_global("myfunc", 1, ARGUMENT_FLOAT, 3.14f)
  60. /// Returns true if success, false otherwise
  61. void call_global(const char* func, uint8_t argc, ...);
  62. // HACK
  63. void call_physics_callback(Actor* actor_0, Actor* actor_1, Unit* unit_0, Unit* unit_1, const Vector3& where, const Vector3& normal, const char* type);
  64. void call_trigger_callback(Actor* trigger, Actor* other, const char* type);
  65. private:
  66. // Disable copying
  67. LuaEnvironment(const LuaEnvironment&);
  68. LuaEnvironment& operator=(const LuaEnvironment&);
  69. private:
  70. lua_State* m_L;
  71. };
  72. } // namespace crown