lua.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. while (1)
  23. {
  24. res_manager.flush_load_queue();
  25. res_manager.bring_loaded_online();
  26. if (res_manager.is_loaded(script))
  27. {
  28. lua_state = luaL_newstate();
  29. luaL_openlibs(lua_state);
  30. assert(res_manager.data(script) != NULL);
  31. ScriptResource* resource = (ScriptResource*)res_manager.data(script);
  32. int s = luaL_loadbuffer(lua_state, (char*)resource->data(), 47, "");
  33. if (s == 0)
  34. {
  35. s = lua_pcall(lua_state, 0, LUA_MULTRET, 0);
  36. }
  37. report_errors(lua_state, s);
  38. break;
  39. }
  40. }
  41. lua_close(lua_state);
  42. return 0;
  43. }