aboutsummaryrefslogtreecommitdiff
path: root/src/libjin-lua/modules/filesystem/l_filesystem.cpp
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2019-01-14 16:40:14 +0800
committerchai <chaifix@163.com>2019-01-14 16:40:14 +0800
commit98c9825c49a1674f59eafb3253829ca7d5cac3c1 (patch)
treebdd2a6da72fc998ff884b011a2260dc72537c25f /src/libjin-lua/modules/filesystem/l_filesystem.cpp
parent8b00d67febf133e89f6a0bfabc41feed555dc4a9 (diff)
*rename source file
Diffstat (limited to 'src/libjin-lua/modules/filesystem/l_filesystem.cpp')
-rw-r--r--src/libjin-lua/modules/filesystem/l_filesystem.cpp140
1 files changed, 140 insertions, 0 deletions
diff --git a/src/libjin-lua/modules/filesystem/l_filesystem.cpp b/src/libjin-lua/modules/filesystem/l_filesystem.cpp
new file mode 100644
index 0000000..b297c8d
--- /dev/null
+++ b/src/libjin-lua/modules/filesystem/l_filesystem.cpp
@@ -0,0 +1,140 @@
+#include "common/l_common.h"
+#include "common/je_lua.h"
+#include "libjin/jin.h"
+#include <string>
+
+using namespace JinEngine::Filesystem;
+
+namespace JinEngine
+{
+ namespace Lua
+ {
+
+ LUA_IMPLEMENT struct
+ {
+ AssetDatabase* fs;
+ } context;
+
+ LUA_IMPLEMENT int l_init(lua_State* L)
+ {
+ context.fs = AssetDatabase::get();
+ return 0;
+ }
+
+ LUA_IMPLEMENT int l_mount(lua_State* L)
+ {
+ const char* path = luax_checkstring(L, 1);
+ context.fs->mount(path);
+ return 0;
+ }
+
+ LUA_IMPLEMENT int l_exist(lua_State * L)
+ {
+ const char* path = luax_checkstring(L, 1);
+ int r = context.fs->exists(path);
+ luax_pushboolean(L, r);
+ return 1;
+ }
+
+ LUA_IMPLEMENT int l_isDir(lua_State* L)
+ {
+ const char* path = luax_checkstring(L, 1);
+ int r = context.fs->isDir(path);
+ luax_pushboolean(L, r);
+ return 1;
+ }
+
+ LUA_IMPLEMENT int l_isFile(lua_State* L)
+ {
+ const char* path = luax_checkstring(L, 1);
+ int r = context.fs->isFile(path);
+ luax_pushboolean(L, r);
+ return 1;
+ }
+
+ LUA_IMPLEMENT int loadbuffer(lua_State* L)
+ {
+ const char* filename = lua_tostring(L, -1);
+ Buffer bf;
+ context.fs->read(filename, bf);
+ luax_loadbuffer(L, (const char*)&bf, bf.size(), filename);
+ return 1;
+ }
+
+ LUA_IMPLEMENT int loader(lua_State* L)
+ {
+ const char * filename = lua_tostring(L, -1);
+
+ std::string tmp(filename);
+ tmp += ".lua";
+
+ int size = tmp.size();
+
+ for (int i = 0; i<size - 4; ++i)
+ {
+ if (tmp[i] == '.')
+ {
+ tmp[i] = '/';
+ }
+ }
+
+ if (context.fs->exists(tmp.c_str()))
+ {
+ lua_pop(L, 1);
+ lua_pushstring(L, tmp.c_str());
+ return loadbuffer(L);
+ }
+
+ tmp = filename;
+ size = tmp.size();
+ for (int i = 0; i<size; ++i)
+ {
+ if (tmp[i] == '.')
+ tmp[i] = '/';
+ }
+
+ if (context.fs->isDir(tmp.c_str()))
+ {
+ tmp += "/init.lua";
+ if (context.fs->exists(tmp.c_str()))
+ {
+ lua_pop(L, 1);
+ lua_pushstring(L, tmp.c_str());
+ return loadbuffer(L);
+ }
+ }
+
+ lua_pushfstring(L, "\n\tno file \"%s\" in jin game directories.\n", (tmp + ".lua").c_str());
+ return 1;
+ }
+
+ LUA_IMPLEMENT int l_read(lua_State* L)
+ {
+ AssetDatabase* fs = context.fs;
+ const char* file = luax_checkstring(L, 1);
+ unsigned int len;
+ Buffer buffer;
+ fs->read(file, buffer);
+ luax_pushstring(L, (char*)&buffer);
+ luax_pushinteger(L, buffer.size());
+ return 2;
+ }
+
+ LUA_EXPORT int luaopen_filesystem(lua_State* L)
+ {
+ luaL_Reg methods[] = {
+ { "init", l_init },
+ { "mount", l_mount },
+ { "isDirectory", l_isDir },
+ { "isFile", l_isFile },
+ { "exist", l_exist },
+ { "read", l_read },
+ { 0, 0 }
+ };
+ luax_newlib(L, methods);
+ luax_registersearcher(L, loader, 1);
+ return 0;
+ }
+
+ } // namespace Lua
+} // namespace JinEngine \ No newline at end of file