aboutsummaryrefslogtreecommitdiff
path: root/src/lua/modules/filesystem/filesystem.cpp
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2018-10-23 12:23:58 +0800
committerchai <chaifix@163.com>2018-10-23 12:23:58 +0800
commit40fc27154fe754181934dc7ee31375e6bdfb33fc (patch)
tree897ad98d759bc308ef66561181ba88b85f2ccd47 /src/lua/modules/filesystem/filesystem.cpp
parent1480c9445100075c9e1a894eb07c0ef727b509a1 (diff)
*merge from minimal
Diffstat (limited to 'src/lua/modules/filesystem/filesystem.cpp')
-rw-r--r--src/lua/modules/filesystem/filesystem.cpp215
1 files changed, 109 insertions, 106 deletions
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 <string>
-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; i<size - 4; ++i)
+ static int l_isDir(lua_State* L)
{
- if (tmp[i] == '.')
- {
- tmp[i] = '/';
- }
+ const char* path = luax_checkstring(L, 1);
+ int r = context.fs->isDir(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; i<size; ++i)
+ static int loadbuffer(lua_State* L)
{
- if (tmp[i] == '.')
- tmp[i] = '/';
+ 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;
}
- 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; 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 loadf(L);
+ return loadbuffer(L);
+ }
+
+ tmp = filename;
+ size = tmp.size();
+ for (int i = 0; i<size; ++i)
+ {
+ if (tmp[i] == '.')
+ tmp[i] = '/';
}
- }
- lua_pushfstring(L, "\n\tno file \"%s\" in jin game directories.\n", (tmp + ".lua").c_str());
- return 1;
- }
+ 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);
+ }
+ }
- 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