lua.cpp 1017 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include <iostream>
  2. #include "Crown.h"
  3. #include "lua.hpp"
  4. #include <unistd.h>
  5. using namespace crown;
  6. static void report_errors(lua_State* state, const int status)
  7. {
  8. if (status != 0)
  9. {
  10. std::cerr << "-- " << lua_tostring(state, -1) << std::endl;
  11. lua_pop(state, 1);
  12. }
  13. }
  14. int main(int argc, char** argv)
  15. {
  16. lua_State* lua_state;
  17. Filesystem fs("/home/mikymod/test/res_compiled");
  18. FileBundle archive(fs);
  19. ResourceManager res_manager(archive);
  20. ResourceId script = res_manager.load("lua/hello.lua");
  21. res_manager.flush();
  22. while (1)
  23. {
  24. if (res_manager.is_loaded(script))
  25. {
  26. lua_state = luaL_newstate();
  27. luaL_openlibs(lua_state);
  28. ScriptResource* resource = (ScriptResource*)res_manager.data(script);
  29. int s = luaL_loadbuffer(lua_state, (char*)resource->data(), 47, "");
  30. if (s == 0)
  31. {
  32. s = lua_pcall(lua_state, 0, LUA_MULTRET, 0);
  33. }
  34. report_errors(lua_state, s);
  35. break;
  36. }
  37. }
  38. lua_close(lua_state);
  39. return 0;
  40. }