CLuaHandler.cpp 3.0 KB

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