lua.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. assert(res_manager.data(script) != NULL);
  30. ScriptResource* resource = (ScriptResource*)res_manager.data(script);
  31. int s = luaL_loadbuffer(lua_state, (char*)resource->data(), 47, "");
  32. if (s == 0)
  33. {
  34. s = lua_pcall(lua_state, 0, LUA_MULTRET, 0);
  35. }
  36. report_errors(lua_state, s);
  37. break;
  38. }
  39. }
  40. lua_close(lua_state);
  41. return 0;
  42. }