You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

108 rivejä
2.7 KiB

  1. //
  2. // Base Lua class for Lua script loading
  3. //
  4. // Copyright: (c) 2009-2015 Sam Hocevar <sam@hocevar.net>
  5. // 2009-2015 Benjamin "Touky" Huet <huet.benjamin@gmail.com>
  6. //
  7. // This program is free software. It comes without any warranty, to
  8. // the extent permitted by applicable law. You can redistribute it
  9. // and/or modify it under the terms of the Do What the Fuck You Want
  10. // to Public License, Version 2, as published by the WTFPL Task Force.
  11. // See http://www.wtfpl.net/ for more details.
  12. //
  13. #include <lol/engine-internal.h>
  14. #include <cstring>
  15. #include <cstdlib>
  16. #include <ctype.h>
  17. namespace lol
  18. {
  19. //-----------------------------------------------------------------------------
  20. class LuaBaseData
  21. {
  22. friend class Lolua::Loader;
  23. static int LuaPanic(LuaState* L)
  24. {
  25. char const *message = lua_tostring(L, -1);
  26. Log::Error("%s\n", message);
  27. DebugAbort();
  28. return 0;
  29. }
  30. static int LuaDoFile(LuaState *L)
  31. {
  32. if (lua_isnoneornil(L, 1))
  33. return LUA_ERRFILE;
  34. LuaCharPtr var; var.Get(L, 1);
  35. char const *filename = var;// lua_tostring(L, 1);
  36. int status = LUA_ERRFILE;
  37. array<String> pathlist = System::GetPathList(filename);
  38. File f;
  39. for (int i = 0; i < pathlist.Count(); ++i)
  40. {
  41. f.Open(pathlist[i], FileAccess::Read);
  42. if (f.IsValid())
  43. {
  44. String s = f.ReadString();
  45. f.Close();
  46. Log::Debug("loading Lua file %s\n", pathlist[i].C());
  47. status = luaL_dostring(L, s.C());
  48. break;
  49. }
  50. }
  51. if (status == LUA_ERRFILE)
  52. Log::Error("could not find Lua file %s\n", filename);
  53. lua_pop(L, 1);
  54. return status;
  55. }
  56. };
  57. namespace Lolua
  58. {
  59. //-----------------------------------------------------------------------------
  60. Loader::Loader()
  61. {
  62. m_lua_state = luaL_newstate();
  63. lua_atpanic(m_lua_state, LuaBaseData::LuaPanic);
  64. luaL_openlibs(m_lua_state);
  65. /* Override dofile() */
  66. LuaFunction do_file(m_lua_state, "dofile", LuaBaseData::LuaDoFile);
  67. }
  68. //-----------------------------------------------------------------------------
  69. Loader::~Loader()
  70. {
  71. lua_close(m_lua_state);
  72. }
  73. //-----------------------------------------------------------------------------
  74. bool Loader::ExecLua(String const &lua)
  75. {
  76. const char* c = lua_pushstring(m_lua_state, lua.C());
  77. int status = LuaBaseData::LuaDoFile(m_lua_state);
  78. return status == 0;
  79. }
  80. //-----------------------------------------------------------------------------
  81. LuaState* Loader::GetLuaState()
  82. {
  83. return m_lua_state;
  84. }
  85. } /* namespace Lolua */
  86. } /* namespace lol */