aboutsummaryrefslogtreecommitdiff
path: root/src/libjin-lua/modules/filesystem/l_filesystem.cpp
blob: 88d110c6f125330f1bbe3036e1e0cc2a7f0f1769 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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