25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

95 lines
1.8 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://www.wtfpl.net/ for more details.
  9. //
  10. #if defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #include <cstring>
  14. #include <cstdlib>
  15. #include <ctype.h>
  16. #include "core.h"
  17. #include "lua/lua.hpp"
  18. namespace lol
  19. {
  20. /*
  21. * World implementation class
  22. */
  23. class WorldData
  24. {
  25. friend class World;
  26. static int LuaPanic(lua_State* L)
  27. {
  28. DebugAbort();
  29. return 0;
  30. }
  31. lua_State *m_lua_state;
  32. };
  33. static WorldData g_world_data;
  34. World g_world;
  35. /*
  36. * Public World class
  37. */
  38. World::World()
  39. {
  40. g_world_data.m_lua_state = luaL_newstate();
  41. lua_atpanic(g_world_data.m_lua_state, WorldData::LuaPanic);
  42. luaL_openlibs(g_world_data.m_lua_state);
  43. }
  44. World::~World()
  45. {
  46. lua_close(g_world_data.m_lua_state);
  47. }
  48. bool World::ExecLua(String const &lua)
  49. {
  50. Array<String> pathlist = System::GetPathList(lua);
  51. File f;
  52. for (int i = 0; i < pathlist.Count(); ++i)
  53. {
  54. f.Open(pathlist[i], FileAccess::Read);
  55. if (f.IsValid())
  56. {
  57. String s = f.ReadString();
  58. f.Close();
  59. luaL_dostring(g_world_data.m_lua_state, s.C());
  60. Log::Debug("loaded Lua file %s\n", pathlist[i].C());
  61. return true;
  62. }
  63. }
  64. Log::Error("could not find Lua file %s\n", lua.C());
  65. return false;
  66. }
  67. double World::GetLuaNumber(String const &var)
  68. {
  69. double ret;
  70. lua_getglobal(g_world_data.m_lua_state, var.C());
  71. ret = lua_tonumber(g_world_data.m_lua_state, -1);
  72. lua_pop(g_world_data.m_lua_state, 1);
  73. return ret;
  74. }
  75. } /* namespace lol */