Advertisement
Metalhead33

PhysFS Lua

Dec 6th, 2016
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.99 KB | None | 0 0
  1. // LuaState.hpp
  2.  
  3. #ifdef __cplusplus
  4. #include <lua5.2/lua.hpp>
  5. #include <cassert>
  6. extern "C" {
  7. #include <physfs.h>
  8. }
  9. #else
  10. #include <lua5.2/lua.h>
  11. #include <lua5.2/lualib.h>
  12. #include <lua5.2/lauxlib.h>
  13. #include <assert.h>
  14. #include <physfs.h>
  15. #endif
  16.  
  17. typedef int (*lua_CFunction) (lua_State *L);
  18. lua_State *L;
  19. int physfs_LuaDofile();
  20.  
  21. // LuaState.cpp
  22.  
  23. #include "luastate.hpp"
  24. #include <cstdlib>
  25.  
  26. int physfs_LuaLoad()
  27. {
  28.     int n = lua_gettop(L);
  29.     string filename = (string)lua_tostring(L,-1);
  30.     char* buf;
  31.     PHYSFS_file* myfile;
  32.     assert((myfile = PHYSFS_openRead(filename.c_str())));
  33.     PHYSFS_sint64 length = PHYSFS_fileLength(myfile);
  34.     buf = (char*)malloc(sizeof(char)*length);
  35.     PHYSFS_read (myfile, buf, 1, length);
  36.     PHYSFS_close(myfile);
  37.     lua_pushlstring(L, buf, length);
  38.     free(buf);
  39.     return 1;
  40. }
  41.  
  42. // Main.cpp
  43.  
  44. #include "luastate.hpp"
  45. int main()
  46. {
  47.     L = luaL_newstate();
  48.     luaL_openlibs(L);
  49.     lua_register(L, "PHYSFS_LoadFile", physfs_LuaLoad);
  50.     lua_close(L);
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement