-
Notifications
You must be signed in to change notification settings - Fork 46
/
live.c
322 lines (255 loc) · 6.76 KB
/
live.c
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include "live.h"
#include "lutro.h"
#ifdef HAVE_INOTIFY
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/inotify.h>
#include <unistd.h>
#include <assert.h>
#include <time.h>
#include <compat/strl.h>
#include <file/file_path.h>
static struct {
int ifd; // inotify fd
int wfd; // watch fd
} live;
int lutro_live_preload(lua_State *L)
{
static luaL_Reg funcs[] = {
{NULL, NULL}
};
lutro_ensure_global_table(L, "lutro");
luaL_newlib(L, funcs);
lua_setfield(L, -2, "live");
return 1;
}
void lutro_live_init()
{
memset(&live, 0, sizeof(live));
live.ifd = inotify_init1(IN_NONBLOCK);
if (live.ifd < 0)
{
perror("Failed to initialize inotify");
lutro_live_deinit();
return;
}
// XXX: Some editors do not trigger IN_MODIFY since they write to a temp file
// and rename() it to the actual file.
live.wfd = inotify_add_watch(live.ifd, settings.gamedir, IN_MODIFY|IN_MOVED_TO);
if (live.wfd < 0)
{
perror("Failed to monitor game directory");
lutro_live_deinit();
return;
}
}
void lutro_live_deinit()
{
settings.live_enable = 0;
if (live.wfd >= 0)
close(live.wfd);
if (live.ifd >= 0)
close(live.ifd);
}
// copies stuff from table dst to table src
static inline void shallow_update(lua_State *L, int src, int dst)
{
lutro_checked_stack_begin();
assert(lua_istable(L, src));
assert(lua_istable(L, dst));
src = lua_absindex(L, src);
dst = lua_absindex(L, dst);
lua_pushnil(L);
while (lua_next(L, src))
{
lua_pushvalue(L, -2);
lua_insert(L, -2);
lua_settable(L, dst);
}
lutro_checked_stack_assert(0);
}
// expects value to be on the top of the stack. it'll be pop'd out
void set_package_loaded(lua_State *L, const char *modname)
{
lua_getglobal(L, "package");
lua_getfield(L, -1, "loaded");
lua_pushvalue(L, -3);
lua_setfield(L, -2, modname);
lua_pop(L, 3);
}
void get_package_loaded(lua_State *L, const char *modname)
{
lua_getglobal(L, "package");
lua_getfield(L, -1, "loaded");
lua_getfield(L, -1, modname);
lua_remove(L, -2);
lua_remove(L, -2);
}
void deep_update_once(lua_State *L, int src, int dst, int updated)
{
assert(lua_istable(L, src));
assert(lua_istable(L, dst));
assert(lua_istable(L, updated));
src = lua_absindex(L, src);
dst = lua_absindex(L, dst);
updated = lua_absindex(L, updated);
lutro_checked_stack_begin();
lua_pushvalue(L, dst);
lua_gettable(L, updated);
if (lua_isnoneornil(L, -1))
{
lua_pop(L, 1);
lua_pushvalue(L, dst);
lua_pushboolean(L, 1);
lua_settable(L, updated);
int top = lua_gettop(L);
int src_mt = lua_getmetatable(L, src);
int dst_mt = lua_getmetatable(L, dst);
if (src_mt && dst_mt)
deep_update_once(L, src_mt, dst_mt, updated);
lua_pop(L, lua_gettop(L) - top);
lua_pushnil(L);
while (lua_next(L, src))
{
if (lua_istable(L, -1))
{
lua_pushvalue(L, -2);
lua_gettable(L, dst);
deep_update_once(L, -2, -1, updated);
lua_pop(L, 2);
}
else
{
lua_pushvalue(L, -2);
lua_insert(L, -2);
lua_settable(L, dst);
}
}
}
else
lua_pop(L, 1);
lutro_checked_stack_assert(0);
}
static void update_inner_tables_once(lua_State *L, int src, int dst, int updated)
{
lutro_checked_stack_begin();
assert(lua_istable(L, src));
assert(lua_istable(L, dst));
assert(lua_istable(L, updated));
src = lua_absindex(L, src);
dst = lua_absindex(L, dst);
updated = lua_absindex(L, updated);
lua_pushnil(L);
while (lua_next(L, src))
{
lua_pushvalue(L, -2);
lua_gettable(L, dst);
if (!lua_compare(L, -2, -1, LUA_OPEQ) && lua_istable(L, -2))
{
deep_update_once(L, -1, -2, updated);
lua_pushvalue(L, -3);
lua_pushvalue(L, -2);
lua_settable(L, dst);
}
lua_pop(L, 2);
}
lutro_checked_stack_assert(0);
}
// TODO: check if there's anything else that needs to be backed up
static int live_hotswap(lua_State *L, const char *filename)
{
char modname[PATH_MAX_LENGTH];
int success = 1;
lutro_checked_stack_begin();
// backup _G then drop it
lua_pushglobaltable(L);
lua_newtable(L);
shallow_update(L, -2, -1);
lua_remove(L, -2);
lutro_relpath_to_modname(modname, filename);
// backup module state
get_package_loaded(L, modname);
// force reloading of the module
lua_pushnil(L);
set_package_loaded(L, modname);
if(!lutro_require(L, modname, 0))
{
fprintf(stderr, "lutro.live lua error: %s\n", lua_tostring(L, -1));
lua_pop(L, 1);
lua_pushglobaltable(L);
shallow_update(L, -3, -1);
lua_pop(L, 1); // error and backups
success = 0;
}
else
{
lua_newtable(L); // to keep track of updated keys
if (lua_istable(L, -3))
deep_update_once(L, -2, -3, -1);
// drop newmod
lua_remove(L, -2);
// restore _G then drop it
lua_pushglobaltable(L);
update_inner_tables_once(L, -4, -1, -2);
lua_pop(L, 1);
lua_pop(L, 1); // drop updated key table
}
// restore oldmod
set_package_loaded(L, modname);
lua_pop(L, 1); // _G backup
if (success && settings.live_call_load)
{
lua_getglobal(L, "lutro");
lua_getfield(L, -1, "load");
// assume load is defined.
if(lua_pcall(L, 0, 0, 0))
{
// TODO: dump stacktrace
fprintf(stderr, "%s\n", lua_tostring(L, -1));
lua_pop(L, 1);
}
lua_pop(L, 1);
}
lutro_checked_stack_assert(0);
return success;
}
void lutro_live_update(lua_State *L)
{
char buf[sizeof(struct inotify_event) + PATH_MAX_LENGTH + 1]; // this ought to be enough for a single event
int rdsize = 0;
int swapped = 0;
// read all events
while ((rdsize = read(live.ifd, buf, sizeof(buf))) >= 0)
{
const char *ptr;
const struct inotify_event *ev;
for (ptr = buf; ptr < buf + rdsize; ptr += sizeof(struct inotify_event) + ev->len)
{
ev = (const struct inotify_event*)ptr;
if (ev->len && strcmp(path_get_extension(ev->name), "lua") == 0)
{
clock_t start = clock();
if (live_hotswap(L, ev->name))
{
printf("Swapped %s in %.3fs\n", ev->name,
((double)clock() - (double)start)* 1.0e-6);
swapped = 1;
}
else
printf("Swapping of %s failed.\n", ev->name);
}
}
}
if (errno != EAGAIN)
{
perror("Could not read inotify event");
return;
}
if (swapped)
lua_gc(L, LUA_GCCOLLECT, 0);
}
void lutro_live_draw()
{
}
#endif