aboutsummaryrefslogtreecommitdiff
path: root/src/client/lua_api/io/ifilesystem.cpp
blob: 588bddd6cd72d9d44c5da1c1e851806522c2fff1 (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "ifilesystem.hpp"

using namespace irr;
using namespace io;

extern IrrlichtDevice* device;


/***
A list of files in the current direcotry.
@function io.list()
@treturn array The files and directories in the current directory.
*/
// io.list()
int listfilesin(lua_State *L){
	printf("Device is: %p\n",(void*)device);
	IFileSystem *fs = device->getFileSystem();
	printf("Got file system %p\n",(void*)fs);
	IFileList *fl = fs->createFileList();
	printf("Created file list\n");

	u32 fc = fl->getFileCount();
	printf("Got file count\n");
	unsigned long i;
	const char *path;

	lua_newtable(L);
	for(i = 0; i < fc; i++){
		printf("Adding list item %lu \n",i);
		path = fl->getFileName(i).c_str();
		printf("Got item name: %s\n",path);
		lua_pushnumber(L,i + 1);
		lua_pushstring(L,path);
		lua_settable(L,-3);
	}

	fl->drop();
	
	return 1;
}

/***
Changes the current directory of the program
@function io.cd(dir)
@tparam string dir The directory to change to
*/
// io.cd("directory")
int changedirectory(lua_State *L){
	IFileSystem *fs = device->getFileSystem();
	const char *dir = lua_tostring(L,-1);
	lua_pop(L,1);
	fs->changeWorkingDirectoryTo(path(dir));
	return 0;
}

/***
Logs some text with Irrlicht.
`level` may be any of:
io.LOG_DEBUG, io.LOG_INFO, io.LOG_WARN, io.LOG_ERROR, io.LOG_NONE
@function io.log(text,level[,hint])
@tparam string text The text to log
@tparam log_level_enum level The log level
@tparam? string hint An optional hint to supply with the log
*/
// io.log("text",level)
// io.log("text",level[,"hint"])
int logmessage(lua_State *L){
	ILogger *log = device->getLogger();

	const char *hint_c = "";
	const char *text_c = "";
	int nargs = lua_gettop(L);
	if(nargs > 2){
		hint_c = lua_tostring(L,-1);
		lua_pop(L,1);//"text",level
	}
	ELOG_LEVEL ll = (ELOG_LEVEL)lua_tointeger(L,-1);//"text",level
	lua_pop(L,1);//"text"
	text_c = lua_tostring(L,-1);//"text"
	lua_pop(L,1);//
	
	log->log(text_c,hint_c,ll);

	return 0;
}

/***
Sets what output gets logged, and what gets ignored.
level may be any of:
io.LOG_DEBUG, io.LOG_INFO, io.LOG_WARN, io.LOG_ERROR, io.LOG_NONE
@function io.set_log_level(level)
@tparam number level the minimul level of log to capture
*/
//io.set_log_level(level)
int setloglevel(lua_State *L){
	ILogger *log = device->getLogger();
	
	ELOG_LEVEL ll = (ELOG_LEVEL)lua_tointeger(L,-1);
	log->setLogLevel(ll);

	return 0;
}

static const luaL_Reg ifilesystem_f[] = {
	{"log",             logmessage},
	{"set_log_level",   setloglevel},
	{"list",            listfilesin},
	{"cd",          changedirectory},
	{0,0},
};

static const luaL_Reg ifilesystem_m[] = {
	{0,0},
};

void ifilesystem_register(lua_State* L){

	luaL_newmetatable(L, "io.ifilesystem");//{m_iguibutton}
	lua_newtable(L);//{m_iguibutton},{}
	luaL_register(L,NULL,ifilesystem_m);
	lua_setfield(L,-2,"__index");//{m_iguibutton}

	lua_pop(L,1);
	printf("set io global\n");
	lua_getglobal(L,"io");//{io}
	luaL_register(L,NULL,ifilesystem_f);

/***
Debugging log level
@field io.LOG_DEBUG 
*/
	lua_pushnumber(L,ELL_DEBUG);
	lua_setfield(L,-2,"LOG_DEBUG");
/***
Informational log level
@field io.LOG_INTO
*/
	lua_pushnumber(L,ELL_INFORMATION);
	lua_setfield(L,-2,"LOG_INFO");
/***
Warning log level
@field io.LOG_WARN
*/
	lua_pushnumber(L,ELL_WARNING);
	lua_setfield(L,-2,"LOG_WARN");
/***
Error log level
@field io.LOG_ERROR
*/
	lua_pushnumber(L,ELL_ERROR);
	lua_setfield(L,-2,"LOG_ERROR");
/***
Nonetype log level.
Use this with @{io#set_log_level} if you don't want to log anything.
@field io.LOG_NONE
*/
	lua_pushnumber(L,ELL_NONE);
	lua_setfield(L,-2,"LOG_NONE");
	
	lua_pop(L,1);//
}