diff options
| author | Alexander M Pickering <alex@cogarr.net> | 2021-12-17 15:15:58 -0600 |
|---|---|---|
| committer | Alexander M Pickering <alex@cogarr.net> | 2021-12-17 15:15:58 -0600 |
| commit | cb39716f1ee896473ed03bcb6dbf927715f74cee (patch) | |
| tree | de97f7a0cd632c2e94d2ec236924b2a2065035a8 /src/shared/lua_api/load_common.cpp | |
| parent | 182f17566580e7ac3e332f99d233df3c1c8b399e (diff) | |
| download | brokengine-cb39716f1ee896473ed03bcb6dbf927715f74cee.tar.gz brokengine-cb39716f1ee896473ed03bcb6dbf927715f74cee.tar.bz2 brokengine-cb39716f1ee896473ed03bcb6dbf927715f74cee.zip | |
commit to back up work before moving
Diffstat (limited to 'src/shared/lua_api/load_common.cpp')
| -rw-r--r-- | src/shared/lua_api/load_common.cpp | 163 |
1 files changed, 82 insertions, 81 deletions
diff --git a/src/shared/lua_api/load_common.cpp b/src/shared/lua_api/load_common.cpp index 188cd30..e0128ce 100644 --- a/src/shared/lua_api/load_common.cpp +++ b/src/shared/lua_api/load_common.cpp @@ -1,81 +1,82 @@ -#include <chrono>
-#include <shared/lua_api/load_common.hpp>
-extern "C" {
- #include <lua.h>
- #include <lauxlib.h>
- #include <lualib.h>
-}
-//Open group
-#include <dirent.h>//for io.dir("file/path")
-#include <sys/types.h>//required before sys/stat.h on windows
-#include <sys/stat.h>//for io.time("path/to.file")
-#include <fcntl.h>
-#include <errno.h>//For better errors
-
-using namespace std::chrono;
-
-//Gets the time
-int get_time(lua_State* L){
- std::chrono::high_resolution_clock::time_point now = high_resolution_clock::now();
- std::chrono::high_resolution_clock::duration since_epoch = now.time_since_epoch();
- double dc = std::chrono::duration_cast<std::chrono::milliseconds>(since_epoch).count();
- lua_pushnumber(L,dc);
- return 1;
-}
-
-char *epath;
-size_t epathlen;
-/*Add an io.dir(path) function, which lists all the files in (path)*/
-int dirpath(lua_State *L){
- if(!lua_isstring(L,-1)){
- lua_pushstring(L,"io.dir() requires a string as argument #1");
- lua_error(L);
- }
- size_t pathstrlen;
- const char *pathstr = lua_tolstring(L,-1,&pathstrlen);
- char tpathstr[pathstrlen + epathlen + 1 + 1]; //+1 for null, +1 for /
- memcpy(tpathstr,epath,epathlen);
- tpathstr[epathlen] = '/';
- memcpy(tpathstr+epathlen+1,pathstr,pathstrlen);
- tpathstr[pathstrlen + epathlen + 1] = '\0';
- lua_pop(L,1);
- DIR *dir;
- struct dirent *ent;
- dir = opendir(tpathstr);
- if(dir == NULL){
- perror("Cannot open");
- lua_pushstring(L,"Failed to open directory: ");
- lua_pushstring(L,tpathstr);
- lua_concat(L,2);
- lua_error(L);
- }
- int i = 1;
- ent = readdir(dir);
- lua_newtable(L);
- while( (ent = readdir(dir)) != NULL){
- lua_pushinteger(L,i);
- lua_pushstring(L,ent->d_name);
- lua_settable(L,-3);
- i++;
- }
- closedir(dir);
- return 1;
-}
-
-void loadCommonLibs(lua_State* L){
- lua_getglobal(L,"GAME");
- lua_pushcfunction(L,make_crashy);
- lua_setfield(L,-2,"crashy");
- lua_pop(L,1);
- lua_pushcfunction(L,get_time);
- lua_setglobal(L,"get_time");
-
- lua_getglobal(L,"io");
- lua_pushcfunction(L,dirpath);
- lua_setfield(L,-2,"dir");
- lua_pop(L,1);
-}
-
-void gameloop_common(lua_State* L){
-
-}
+#include <chrono> +#include <shared/lua_api/load_common.hpp> +extern "C" { + #include <lua.h> + #include <lauxlib.h> + #include <lualib.h> +} +//Open group +#include <dirent.h>//for io.dir("file/path") +#include <sys/types.h>//required before sys/stat.h on windows +#include <sys/stat.h>//for io.time("path/to.file") +#include <fcntl.h> +#include <errno.h>//For better errors +#include <cstring>//for memcpy + +using namespace std::chrono; + +//Gets the time +int get_time(lua_State* L){ + std::chrono::high_resolution_clock::time_point now = high_resolution_clock::now(); + std::chrono::high_resolution_clock::duration since_epoch = now.time_since_epoch(); + double dc = std::chrono::duration_cast<std::chrono::milliseconds>(since_epoch).count(); + lua_pushnumber(L,dc); + return 1; +} + +char *epath; +size_t epathlen; +/*Add an io.dir(path) function, which lists all the files in (path)*/ +int dirpath(lua_State *L){ + if(!lua_isstring(L,-1)){ + lua_pushstring(L,"io.dir() requires a string as argument #1"); + lua_error(L); + } + size_t pathstrlen; + const char *pathstr = lua_tolstring(L,-1,&pathstrlen); + char tpathstr[pathstrlen + epathlen + 1 + 1]; //+1 for null, +1 for / + memcpy(tpathstr,epath,epathlen); + tpathstr[epathlen] = '/'; + memcpy(tpathstr+epathlen+1,pathstr,pathstrlen); + tpathstr[pathstrlen + epathlen + 1] = '\0'; + lua_pop(L,1); + DIR *dir; + struct dirent *ent; + dir = opendir(tpathstr); + if(dir == NULL){ + perror("Cannot open"); + lua_pushstring(L,"Failed to open directory: "); + lua_pushstring(L,tpathstr); + lua_concat(L,2); + lua_error(L); + } + int i = 1; + ent = readdir(dir); + lua_newtable(L); + while( (ent = readdir(dir)) != NULL){ + lua_pushinteger(L,i); + lua_pushstring(L,ent->d_name); + lua_settable(L,-3); + i++; + } + closedir(dir); + return 1; +} + +void loadCommonLibs(lua_State* L){ + lua_getglobal(L,"GAME"); + lua_pushcfunction(L,make_crashy); + lua_setfield(L,-2,"crashy"); + lua_pop(L,1); + lua_pushcfunction(L,get_time); + lua_setglobal(L,"get_time"); + + lua_getglobal(L,"io"); + lua_pushcfunction(L,dirpath); + lua_setfield(L,-2,"dir"); + lua_pop(L,1); +} + +void gameloop_common(lua_State* L){ + +} |
