lua.cpp 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. FileResourceArchive archive(fs);
  19. MallocAllocator allocator;
  20. ResourceManager res_manager(archive, allocator);
  21. ResourceId script = res_manager.load("lua/hello.lua");
  22. res_manager.flush();
  23. while (1)
  24. {
  25. if (res_manager.is_loaded(script))
  26. {
  27. lua_state = luaL_newstate();
  28. luaL_openlibs(lua_state);
  29. ScriptResource* resource = (ScriptResource*)res_manager.data(script);
  30. int s = luaL_loadbuffer(lua_state, (char*)resource->data(), 47, "");
  31. if (s == 0)
  32. {
  33. s = lua_pcall(lua_state, 0, LUA_MULTRET, 0);
  34. }
  35. report_errors(lua_state, s);
  36. break;
  37. }
  38. }
  39. lua_close(lua_state);
  40. return 0;
  41. }