diff options
author | chai <chaifix@163.com> | 2018-12-20 18:34:50 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2018-12-20 18:34:50 +0800 |
commit | ee8ef0433e36bf354a717bd4af679a0a5af2e6be (patch) | |
tree | 2fc748510200f8bc24928d1938300eecc0604deb /src/libjin-lua/modules/filesystem | |
parent | 7ae40127f15f8f2cb963a7efeb018f7887ebc1ea (diff) |
*修改文件结构
Diffstat (limited to 'src/libjin-lua/modules/filesystem')
-rw-r--r-- | src/libjin-lua/modules/filesystem/je_lua_filesystem.cpp | 140 | ||||
-rw-r--r-- | src/libjin-lua/modules/filesystem/je_lua_filesystem.h | 14 |
2 files changed, 154 insertions, 0 deletions
diff --git a/src/libjin-lua/modules/filesystem/je_lua_filesystem.cpp b/src/libjin-lua/modules/filesystem/je_lua_filesystem.cpp new file mode 100644 index 0000000..e73d1ad --- /dev/null +++ b/src/libjin-lua/modules/filesystem/je_lua_filesystem.cpp @@ -0,0 +1,140 @@ +#include "common/je_lua_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 diff --git a/src/libjin-lua/modules/filesystem/je_lua_filesystem.h b/src/libjin-lua/modules/filesystem/je_lua_filesystem.h new file mode 100644 index 0000000..1e1ff23 --- /dev/null +++ b/src/libjin-lua/modules/filesystem/je_lua_filesystem.h @@ -0,0 +1,14 @@ +#ifndef __JE_LUA_FILESYSTEM_H__ +#define __JE_LUA_FILESYSTEM_H__ + +namespace JinEngine +{ + namespace Lua + { + + int luaopen_filesystem(lua_State* L); + + } +} + +#endif
\ No newline at end of file |