From 40fc27154fe754181934dc7ee31375e6bdfb33fc Mon Sep 17 00:00:00 2001 From: chai Date: Tue, 23 Oct 2018 12:23:58 +0800 Subject: *merge from minimal --- src/lua/modules/filesystem/filesystem.cpp | 215 +++++++++++++++--------------- 1 file changed, 109 insertions(+), 106 deletions(-) (limited to 'src/lua/modules/filesystem/filesystem.cpp') diff --git a/src/lua/modules/filesystem/filesystem.cpp b/src/lua/modules/filesystem/filesystem.cpp index 55f4c06..7466ce8 100644 --- a/src/lua/modules/filesystem/filesystem.cpp +++ b/src/lua/modules/filesystem/filesystem.cpp @@ -2,136 +2,139 @@ #include "libjin/jin.h" #include -using namespace jin::filesystem; +using namespace JinEngine::Filesystem; -namespace jin +namespace JinEngine { -namespace lua -{ - - static struct + namespace Lua { - Filesystem* fs; - } context; - static int l_init(lua_State* L) - { - context.fs = Filesystem::get(); - return 0; - } - - /** - * set current game root, like - * C:/jin/games/tank/ - */ - static int l_mount(lua_State* L) - { - const char* path = luax_checkstring(L, 1); - context.fs->mount(path); - return 0; - } - - /** - * - */ - static 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; - } - - /** - * - */ - static 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; - } + static struct + { + AssetDatabase* fs; + } context; - static 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; - } - - // load but dont run it - static int loadf(lua_State* L) - { - const char* filename = lua_tostring(L, -1); - Buffer bf; - context.fs->read(filename, &bf); - luax_loadbuffer(L, (const char*)bf.data, bf.size, filename); - return 1; - } - - static int loader(lua_State* L) - { - const char * filename = lua_tostring(L, -1); + static int l_init(lua_State* L) + { + context.fs = AssetDatabase::get(); + return 0; + } - std::string tmp(filename); - tmp += ".lua"; + static int l_mount(lua_State* L) + { + const char* path = luax_checkstring(L, 1); + context.fs->mount(path); + return 0; + } - int size = tmp.size(); + static 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; + } - for (int i = 0; iisDir(path); + luax_pushboolean(L, r); + return 1; } - if (context.fs->exists(tmp.c_str())) + static int l_isFile(lua_State* L) { - lua_pop(L, 1); - lua_pushstring(L, tmp.c_str()); - return loadf(L); + const char* path = luax_checkstring(L, 1); + int r = context.fs->isFile(path); + luax_pushboolean(L, r); + return 1; } - tmp = filename; - size = tmp.size(); - for (int i = 0; iread(filename, bf); + luax_loadbuffer(L, (const char*)&bf, bf.size(), filename); + return 1; } - if (context.fs->isDir(tmp.c_str())) + static int loader(lua_State* L) { - tmp += "/init.lua"; + const char * filename = lua_tostring(L, -1); + + std::string tmp(filename); + tmp += ".lua"; + + int size = tmp.size(); + + for (int i = 0; iexists(tmp.c_str())) { lua_pop(L, 1); lua_pushstring(L, tmp.c_str()); - return loadf(L); + return loadbuffer(L); + } + + tmp = filename; + size = tmp.size(); + for (int i = 0; iisDir(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); + } + } - static const luaL_Reg f[] = { - { "init", l_init }, - { "mount", l_mount }, - { "isdir", l_isDir }, - { "exist", l_exist }, - { 0, 0 } - }; + lua_pushfstring(L, "\n\tno file \"%s\" in jin game directories.\n", (tmp + ".lua").c_str()); + return 1; + } - int luaopen_filesystem(lua_State* L) - { - luax_newlib(L, f); - luax_register_searcher(L, loader, 1); - return 0; - } + static 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; + } + + static const luaL_Reg f[] = { + { "init", l_init }, + { "mount", l_mount }, + { "isDirectory", l_isDir }, + { "isFile", l_isFile }, + { "exist", l_exist }, + { "read", l_read }, + { 0, 0 } + }; + + int luaopen_filesystem(lua_State* L) + { + luax_newlib(L, f); + luax_registersearcher(L, loader, 1); + return 0; + } -} -} \ No newline at end of file + } // namespace Lua +} // namespace JinEngine \ No newline at end of file -- cgit v1.1-26-g67d0