CLuaHandler.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include "stdafx.h"
  2. #include "lua.h"
  3. #include "lualib.h"
  4. #include "lauxlib.h"
  5. //#include <luabind/luabind.hpp>
  6. //#include <luabind/function.hpp>
  7. //#include <luabind/class.hpp>
  8. #include "CLuaHandler.h"
  9. #include "boost/filesystem.hpp"
  10. #include <boost/algorithm/string.hpp>
  11. void piszpowitanie2(std::string i) //simple global function for testing
  12. {
  13. std::cout<<"powitanie2zc++. Liczba dnia to " << i;
  14. }
  15. CLuaHandler::CLuaHandler()
  16. {
  17. }
  18. CLuaHandler::~CLuaHandler()
  19. {
  20. }
  21. void CLuaHandler::test()
  22. {
  23. int iErr = 0;
  24. lua_State *lua = lua_open (); // Open Lua
  25. LUA_OPEN_LIB(lua, luaopen_base);
  26. LUA_OPEN_LIB(lua, luaopen_io);
  27. //luabind::open(lua);
  28. //luabind::module(lua)
  29. //[
  30. // luabind::class_<int3>("int3")
  31. // //.def(luabind::constructor<>())
  32. // //.def(luabind::constructor<const int&,const int&,const int&>())
  33. // .def_readwrite("x", &int3::x)
  34. // .def_readwrite("y", &int3::y)
  35. // .def_readwrite("z", &int3::z)
  36. //];
  37. //luabind::module(lua)
  38. //[
  39. // luabind::def("powitanie",&piszpowitanie2)
  40. //];
  41. if ((iErr = luaL_loadfile (lua, "scripts/lua/objects/0023_marletto_tower.lua")) == 0)
  42. {
  43. // Call main...
  44. if ((iErr = lua_pcall (lua, 0, LUA_MULTRET, 0)) == 0)
  45. {
  46. //int ret = luabind::call_function<int>(lua, "helloWorld2");
  47. //lua_pushstring (lua, "helloWorld2");
  48. //lua_gettable (lua, LUA_GLOBALSINDEX);
  49. //lua_pcall (lua, 0, 0, 0);
  50. // Push the function name onto the stack
  51. lua_pushstring (lua, "rightText");
  52. lua_gettable (lua, LUA_GLOBALSINDEX);
  53. lua_pcall (lua, 0, 0, 0);
  54. }
  55. }
  56. lua_close (lua);
  57. }
  58. std::vector<std::string> * CLuaHandler::searchForScripts(std::string fol)
  59. {
  60. std::vector<std::string> * ret = new std::vector<std::string> ();
  61. boost::filesystem::path folder(fol);
  62. if (!boost::filesystem::exists(folder))
  63. #ifndef __GNUC__
  64. throw new std::exception("No such folder!");
  65. #else
  66. throw std::exception();
  67. #endif
  68. boost::filesystem::directory_iterator end_itr;
  69. for
  70. (
  71. boost::filesystem::directory_iterator it(folder);
  72. it!=end_itr;
  73. it++
  74. )
  75. {
  76. if(boost::algorithm::ends_with((it->path().leaf()),".lua"))
  77. {
  78. ret->push_back(fol+"/"+(it->path().leaf()));
  79. }
  80. }
  81. return ret;
  82. }
  83. std::vector<std::string> * CLuaHandler::functionList(std::string file)
  84. {
  85. std::vector<std::string> * ret = new std::vector<std::string> ();
  86. char linia[500];
  87. std::ifstream is(file.c_str());
  88. while (!is.eof())
  89. {
  90. is.getline(linia,500);
  91. std::string ss(linia);
  92. boost::algorithm::trim_left(ss);
  93. if (boost::algorithm::starts_with(ss,"local"))
  94. boost::algorithm::erase_first(ss,"local ");
  95. if (boost::algorithm::starts_with(ss,"function"))
  96. {
  97. boost::algorithm::erase_first(ss,"function ");
  98. int ps = ss.find_first_of(' ');
  99. int op = ss.find_first_of('(');
  100. if (ps<0)
  101. ps = ss.length()-1;
  102. if (op<0)
  103. op = ss.length()-1;
  104. ps = std::min(ps,op);
  105. ret->push_back(ss.substr(0,ps));
  106. }
  107. }
  108. is.close();
  109. return ret;
  110. }