CLuaHandler.cpp 2.5 KB

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