CLuaHandler.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. //if ((iErr = luaL_loadfile (lua, "scripts/lua/objects/0023_marletto_tower.lua")) == 0)
  28. //{
  29. // // Call main...
  30. // if ((iErr = lua_pcall (lua, 0, LUA_MULTRET, 0)) == 0)
  31. // {
  32. // lua_pushstring (lua, "rightText");
  33. // lua_gettable (lua, LUA_GLOBALSINDEX);
  34. // lua_pcall (lua, 0, 0, 0);
  35. // }
  36. //}
  37. //lua_close (lua);
  38. }
  39. std::vector<std::string> * CLuaHandler::searchForScripts(std::string fol)
  40. {
  41. std::vector<std::string> * ret = new std::vector<std::string> ();
  42. boost::filesystem::path folder(fol);
  43. if (!boost::filesystem::exists(folder))
  44. throw new std::exception("No such folder!");
  45. boost::filesystem::directory_iterator end_itr;
  46. for
  47. (
  48. boost::filesystem::directory_iterator it(folder);
  49. it!=end_itr;
  50. it++
  51. )
  52. {
  53. if(boost::algorithm::ends_with((it->path().leaf()),".lua"))
  54. {
  55. ret->push_back(fol+"/"+(it->path().leaf()));
  56. }
  57. }
  58. return ret;
  59. }
  60. std::vector<std::string> * CLuaHandler::functionList(std::string file)
  61. {
  62. std::vector<std::string> * ret = new std::vector<std::string> ();
  63. char linia[500];
  64. std::ifstream is(file.c_str());
  65. while (!is.eof())
  66. {
  67. is.getline(linia,500);
  68. std::string ss(linia);
  69. boost::algorithm::trim_left(ss);
  70. if (boost::algorithm::starts_with(ss,"local"))
  71. boost::algorithm::erase_first(ss,"local ");
  72. if (boost::algorithm::starts_with(ss,"function"))
  73. {
  74. boost::algorithm::erase_first(ss,"function ");
  75. int ps = ss.find_first_of(' ');
  76. int op = ss.find_first_of('(');
  77. if (ps<0)
  78. ps = ss.length()-1;
  79. if (op<0)
  80. op = ss.length()-1;
  81. ps = std::min(ps,op);
  82. ret->push_back(ss.substr(0,ps));
  83. }
  84. }
  85. is.close();
  86. return ret;
  87. }