This repository was archived by the owner on May 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathluaBase.cpp
More file actions
172 lines (145 loc) · 4.37 KB
/
luaBase.cpp
File metadata and controls
172 lines (145 loc) · 4.37 KB
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
162
163
164
165
166
167
168
169
170
#include "consts.h"
#include "luaBase.h"
#include<string>
#include<vector>
#include<map>
#include<unordered_map>
#include "strings.hpp"
#include "Socket.hpp"
#include "playlist.h"
using namespace std;
struct playlistitemdata {
playlistitem* it;
int num;
};
void action(int);
extern int curSong;
extern vector<playlistitem> playlist;
lua_State* L = NULL;
void executeCustomCommand (int n) {}
static int luaL_fielderror (lua_State* L, const char* classname, const char* fieldname) {
return luaL_error(L, "Unable to access field %s for object of type %s: this fiels may not exist, be read only, or the type of value supplied to write was unexpected.", fieldname, classname);
}
static int lua_getbacktrace (lua_State* l) {
lua_getglobal(l, "debug");
lua_getfield(l, -1, "traceback");
lua_pushvalue(l,1);
lua_pushinteger(l,2);
if (lua_pcall(l, 2, 1,0)) {
printf("Error in error handling !\r\n");
}
return 1;
}
int luaL_checkboolean (lua_State* L, int n) {
if (lua_isboolean(L,n)) return lua_toboolean(L,n);
else return luaL_typerror(L,n,"boolean");
}
/*const char* callfunc (lua_State* L, void* p, int nArgs, int nRet) {
lua_pushcfunction(L, lua_getbacktrace);
lua_insert(L, -1 -nArgs);
lua_pushluafunction(L,p);
lua_insert(L, -1 -nArgs);
if (lua_pcall(L, nArgs, nRet, -2 -nArgs)) {
return lua_tostring(L,-1);
}
return NULL;
}*/
static inline void lua_regt (lua_State* L, const char* name, int(*func)(lua_State*), int cc=0) {
lua_pushcclosure(L, func, cc);
lua_setfield(L, -2, name);
}
void* lua_pushudata (lua_State* L, void* udata, size_t size, const char* name) {
void* ptr = lua_newuserdata(L, size);
if (!ptr) return NULL;
luaL_getmetatable(L, name);
lua_setmetatable(L, -2);
memcpy(ptr, udata, size);
return ptr;
}
static int lAction (lua_State* L) {
action(lua_tointeger(L,1));
return 0;
}
static void lua_pushplaylistitem (lua_State* L, playlistitem& it, int num = -1) {
playlistitemdata data = { &it, num };
lua_pushudata(L, &data, sizeof(data), "playlistitem");
}
static playlistitem& lua_toplaylistitem (lua_State* L, int index, int* num=0) {
playlistitemdata* data = lua_topointer(L,index);
if (num) *num = data->num;
return *(data->it);
}
static int playlist_len (lua_State* L) {
lua_pushinteger(L, playlist.size());
return 1;
}
static int playlist_index (lua_State* L) {
if (!lua_isnumber(L,2)) {
lua_rawget(L,-2);
return 1;
}
int index = lua_tointeger(L,2);
if (index==0) index = curSong;
else if (index>0) index--;
else index += playlist.size();
if (index<0 || index>=playlist.size()) return 0;
lua_pushplaylistitem(L, playlist[index], index);
return 1;
}
static int playlistitem_index (lua_State* L) {
playlistitem& it = lua_toplaylistitem(L,1);
string name = lua_tostring(L,2);
#define G(n,m,t) if (name==#n) { lua_push##t(L, it.m); return 1; }
#define S(n) G(n,n,string)
S(file) S(title) S(artist) S(album)
S(genre) S(subtitle) S(composer) S(copyright) S(year)
S(disc) S(tracknum) S(comment)
G(duration,length,number)
G(todate,metadataSet,boolean)
#undef S
#undef G
else return luaL_getmetafield(L, 1, name.c_str());
}
static int playlistitem_newindex (lua_State* L) {
int index = -1;
playlistitem& it = lua_toplaylistitem(L, 1, &index);
string name = lua_tostring(L,2);
#define G(n,m,t) else if (name==#n && lua_is##t(L,3)) it.m = lua_to##t(L,3);
#define S(n) G(n,n,string)
if(false){}
S(file) S(title) S(artist) S(album)
S(genre) S(subtitle) S(composer) S(copyright) S(year)
S(disc) S(tracknum) S(comment)
G(duration,length,number)
G(todate,metadataSet,boolean)
#undef S
#undef G
else return luaL_fielderror(L, "playlistitem", name.c_str());
return 0;
}
void luaRunWebScript (Socket* sock, string filename, string method, string uri, string query, unordered_map<string,string>& headers, string data) {
string status = "HTTP/1.1 200 OK\r\n\r\nIt works !";
sock->send(status.c_str(), status.size());
}
void luaScriptingInit (void) {
L = lua_open();
luaL_openlibs(L);
lua_settop(L,0);
luaL_newmetatable(L, "playlistitem");
lua_regt(L, "__index", playlistitem_index);
lua_regt(L, "__newindex", playlistitem_newindex);
lua_settop(L,0);
lua_newtable(L);
lua_regt(L, "__len", playlist_len);
lua_regt(L, "__index", playlist_index);
lua_pushvalue(L,-1);
lua_setmetatable(L,-2);
lua_setglobal(L,"playlist");
lua_newtable(L);
lua_regt(L, "action", lAction);
lua_setglobal(L, "player");
lua_newtable(L);
lua_regt(L, "action", lAction);
lua_setglobal(L, "window");
if (luaL_dofile(L,"autoexec.lua")) printf("ERROR: %s\r\n", lua_tostring(L,-1));
}