naev 0.10.4
nlua.c
Go to the documentation of this file.
1/*
2 * See Licensing and Copyright notice in naev.h
3 */
11#include "physfs.h"
12
13#include "naev.h"
16#include "nlua.h"
17
18#include "log.h"
19#include "conf.h"
20#include "lua_enet.h"
21#include "lutf8lib.h"
22#include "ndata.h"
23#include "nfile.h"
24#include "nlua_cli.h"
25#include "nlua_audio.h"
26#include "nlua_commodity.h"
27#include "nlua_data.h"
28#include "nlua_diff.h"
29#include "nlua_faction.h"
30#include "nlua_file.h"
31#include "nlua_jump.h"
32#include "nlua_linopt.h"
33#include "nlua_naev.h"
34#include "nlua_news.h"
35#include "nlua_outfit.h"
36#include "nlua_pilot.h"
37#include "nlua_spob.h"
38#include "nlua_player.h"
39#include "nlua_rnd.h"
40#include "nlua_safelanes.h"
41#include "nlua_spfx.h"
42#include "nlua_shiplog.h"
43#include "nlua_system.h"
44#include "nlua_time.h"
45#include "nlua_var.h"
46#include "nlua_vec2.h"
47#include "nluadef.h"
48#include "nstring.h"
49
50lua_State *naevL = NULL;
51nlua_env __NLUA_CURENV = LUA_NOREF;
52static char *common_script;
53static size_t common_sz;
54static int nlua_envs = LUA_NOREF;
55
56/*
57 * prototypes
58 */
59static int nlua_package_loader_lua( lua_State* L );
60static int nlua_package_loader_c( lua_State* L );
61static int nlua_package_loader_croot( lua_State* L );
62static int nlua_require( lua_State* L );
63static lua_State *nlua_newState (void); /* creates a new state */
64static int nlua_loadBasic( lua_State* L );
65static int luaB_loadstring( lua_State *L );
66/* gettext */
67static int nlua_gettext( lua_State *L );
68static int nlua_ngettext( lua_State *L );
69static int nlua_pgettext( lua_State *L );
70static int nlua_gettext_noop( lua_State *L );
71static const luaL_Reg gettext_methods[] = {
72 { "gettext", nlua_gettext },
73 { "ngettext", nlua_ngettext },
74 { "pgettext", nlua_pgettext },
75 { "gettext_noop", nlua_gettext_noop },
76 {0,0}
77};
87static int nlua_gettext( lua_State *L )
88{
89 const char *str = luaL_checkstring(L, 1);
90 lua_pushstring(L, _(str) );
91 return 1;
92}
93
104static int nlua_ngettext( lua_State *L )
105{
106 const char *stra = luaL_checkstring(L, 1);
107 const char *strb = luaL_checkstring(L, 2);
108 int n = luaL_checkinteger(L,3);
109 lua_pushstring(L, n_( stra, strb, n ) );
110 return 1;
111}
112
122static int nlua_pgettext( lua_State *L )
123{
124 const char *msgctxt = luaL_checkstring(L, 1);
125 const char *msgid = luaL_checkstring(L, 2);
126 lua_pushstring(L, pgettext_var( msgctxt, msgid ) );
127 return 1;
128}
129
138static int nlua_gettext_noop( lua_State *L )
139{
140 const char *str = luaL_checkstring(L, 1);
141 lua_pushstring(L, str );
142 return 1;
143}
144
146static int nlua_log2( lua_State *L )
147{
148 double n = luaL_checknumber(L,1);
149 lua_pushnumber(L, log2(n));
150 return 1;
151}
152
156static int nlua_os_getenv( lua_State *L )
157{
158 const char *var = luaL_checkstring(L, 1);
159 if (strcmp(var, "HOME"))
160 return 0;
161 lua_pushstring(L, "lua_home");
162 return 1;
163}
164
170static int nlua_panic( lua_State *L )
171{
172 ERR( _("LUA PANIC: %s"), lua_tostring(L,-1) );
173}
174
175/*
176 * @brief Initializes the global Lua state.
177 */
178void lua_init (void)
179{
180 naevL = nlua_newState();
181 nlua_loadBasic(naevL);
182
183 /* Environment table. */
184 lua_newtable( naevL );
185 nlua_envs = luaL_ref(naevL, LUA_REGISTRYINDEX);
186
187 /* Better clean up. */
188 lua_atpanic( naevL, nlua_panic );
189}
190
194static int luaB_loadstring( lua_State *L )
195{
196 size_t l;
197 const char *s = luaL_checklstring(L, 1, &l);
198 const char *chunkname = luaL_optstring(L, 2, s);
199 int status = luaL_loadbuffer(L, s, l, chunkname);
200 if (status == 0) /* OK? */
201 return 1;
202 else {
203 lua_pushnil(L);
204 lua_insert(L, -2); /* put before error message */
205 return 2; /* return nil plus error message */
206 }
207}
208
209/*
210 * @brief Closes the global Lua state.
211 */
212void lua_exit (void)
213{
214 free( common_script );
215 lua_close(naevL);
216 naevL = NULL;
217}
218
219/*
220 * @brief Run code from buffer in Lua environment.
221 *
222 * @param env Lua environment.
223 * @param buff Pointer to buffer.
224 * @param sz Size of buffer.
225 * @param name Name to use in error messages.
226 * @return 0 on success.
227 */
228int nlua_dobufenv( nlua_env env,
229 const char *buff,
230 size_t sz,
231 const char *name )
232{
233 int ret;
234 ret = luaL_loadbuffer(naevL, buff, sz, name);
235 if (ret != 0)
236 return ret;
237 nlua_pushenv(naevL, env);
238 lua_setfenv(naevL, -2);
239 ret = nlua_pcall(env, 0, LUA_MULTRET);
240 if (ret != 0)
241 return ret;
242#if DEBUGGING
243 lua_pushstring( naevL, name );
244 nlua_setenv( naevL, env, "__name" );
245#endif /* DEBUGGING */
246 return 0;
247}
248
249/*
250 * @brief Run code a file in Lua environment.
251 *
252 * @param env Lua environment.
253 * @param filename Filename of Lua script.
254 */
255int nlua_dofileenv( nlua_env env, const char *filename )
256{
257 if (luaL_loadfile(naevL, filename) != 0)
258 return -1;
259 nlua_pushenv(naevL, env);
260 lua_setfenv(naevL, -2);
261 if (nlua_pcall(env, 0, LUA_MULTRET) != 0)
262 return -1;
263 return 0;
264}
265
266#if DEBUGGING
267void nlua_pushEnvTable( lua_State *L )
268{
269 lua_rawgeti( L, LUA_REGISTRYINDEX, nlua_envs );
270}
271#endif /* DEBBUGING */
272
273/*
274 * @brief Create an new environment in global Lua state.
275 *
276 * An "environment" is a table used with setfenv for sandboxing.
277 */
278nlua_env nlua_newEnv (void)
279{
280 nlua_env ref;
281 lua_newtable(naevL); /* t */
282 lua_pushvalue(naevL, -1); /* t, t */
283 ref = luaL_ref(naevL, LUA_REGISTRYINDEX); /* t */
284
285 /* Store in the environment table. */
286 lua_rawgeti(naevL, LUA_REGISTRYINDEX, nlua_envs); /* t, e */
287 lua_pushvalue(naevL, -2); /* t, e, t */
288 lua_pushinteger(naevL, ref); /* t, e, t, r */
289 lua_rawset(naevL, -3); /* t, e */
290 lua_pop(naevL,1); /* t */
291
292 /* Metatable */
293 lua_newtable(naevL); /* t, m */
294 lua_pushvalue(naevL, LUA_GLOBALSINDEX); /* t, m, g */
295 lua_setfield(naevL, -2, "__index"); /* t, m */
296 lua_setmetatable(naevL, -2); /* t */
297
298 /* Replace require() function with one that considers fenv */
299 lua_pushvalue(naevL, -1); /* t, t, */
300 lua_pushcclosure(naevL, nlua_require, 1); /* t, t, c */
301 lua_setfield(naevL, -2, "require"); /* t, t */
302
303 /* Set up paths.
304 * "package.path" to look in the data.
305 * "package.cpath" unset */
306 lua_getglobal(naevL, "package"); /* t, t, p */
307 lua_pushstring(naevL, "?.lua;"LUA_INCLUDE_PATH"?.lua"); /* t, t, p, s */
308 lua_setfield(naevL, -2, "path"); /* t, t, p */
309 lua_pushstring(naevL, ""); /* t, t, p, s */
310 lua_setfield(naevL, -2, "cpath"); /* t, t, p */
311 lua_getfield(naevL, -1, "loaders"); /* t, t, p, l */
312 lua_pushcfunction(naevL, nlua_package_loader_lua); /* t, t, p, l, f */
313 lua_rawseti(naevL, -2, 2); /* t, t, p, l */
314 lua_pushcfunction(naevL, nlua_package_loader_c); /* t, t, p, l, f */
315 lua_rawseti(naevL, -2, 3); /* t, t, p, l */
316 lua_pushcfunction(naevL, nlua_package_loader_croot); /* t, t, p, l, f */
317 lua_rawseti(naevL, -2, 4); /* t, t, p, l */
318 lua_pop(naevL, 2); /* t, t */
319
320 /* The global table _G should refer back to the environment. */
321 lua_pushvalue(naevL, -1); /* t, t, t */
322 lua_setfield(naevL, -2, "_G"); /* t, t */
323
324 /* Push if naev is built with debugging. */
325#if DEBUGGING
326 lua_pushboolean(naevL, 1); /* t, t, b */
327 lua_setfield(naevL, -2, "__debugging"); /* t, t, */
328#endif /* DEBUGGING */
329
330 /* Set up naev namespace. */
331 lua_newtable(naevL); /* t, t, n */
332 lua_setfield(naevL, -2, "naev"); /* t, t */
333
334 /* Run common script. */
335 if (conf.loaded && common_script==NULL) {
336 common_script = ndata_read( LUA_COMMON_PATH, &common_sz );
337 if (common_script==NULL)
338 WARN(_("Unable to load common script '%s'!"), LUA_COMMON_PATH);
339 }
340 if (common_script != NULL) {
341 if (luaL_loadbuffer(naevL, common_script, common_sz, LUA_COMMON_PATH) == 0) {
342 if (nlua_pcall( ref, 0, 0 ) != 0) {
343 WARN(_("Failed to run '%s':\n%s"), LUA_COMMON_PATH, lua_tostring(naevL,-1));
344 lua_pop(naevL, 1);
345 }
346 }
347 else {
348 WARN(_("Failed to load '%s':\n%s"), LUA_COMMON_PATH, lua_tostring(naevL,-1));
349 lua_pop(naevL, 1);
350 }
351 }
352
353 lua_pop(naevL, 1); /* t */
354 return ref;
355}
356
357/*
358 * @brief Frees an environment created with nlua_newEnv()
359 *
360 * @param env Enviornment to free.
361 */
362void nlua_freeEnv( nlua_env env )
363{
364 if (naevL==NULL)
365 return;
366 if (env == LUA_NOREF)
367 return;
368
369 /* Remove from the environment table. */
370 lua_rawgeti(naevL, LUA_REGISTRYINDEX, nlua_envs);
371 lua_rawgeti(naevL, LUA_REGISTRYINDEX, env);
372 lua_pushnil(naevL);
373 lua_rawset(naevL, -3);
374 lua_pop(naevL,1);
375
376 /* Unref. */
377 luaL_unref(naevL, LUA_REGISTRYINDEX, env);
378}
379
380/*
381 * @brief Push environment table to stack
382 *
383 * @param env Environment.
384 */
385void nlua_pushenv( lua_State* L, nlua_env env )
386{
387 lua_rawgeti(L, LUA_REGISTRYINDEX, env);
388}
389
390/*
391 * @brief Gets variable from enviornment and pushes it to stack
392 *
393 * This is meant to replace lua_getglobal()
394 *
395 * @param env Environment.
396 * @param name Name of variable.
397 */
398void nlua_getenv( lua_State* L, nlua_env env, const char *name )
399{
400 nlua_pushenv(L, env); /* env */
401 lua_getfield(L, -1, name); /* env, value */
402 lua_remove(L, -2); /* value */
403}
404
405/*
406 * @brief Pops a value from the stack and sets it in the environment.
407 *
408 * This is meant to replace lua_setglobal()
409 *
410 * @param env Environment.
411 * @param name Name of variable.
412 */
413void nlua_setenv( lua_State* L, nlua_env env, const char *name )
414{
415 /* value */
416 nlua_pushenv(L, env); /* value, env */
417 lua_insert(L, -2); /* env, value */
418 lua_setfield(L, -2, name); /* env */
419 lua_pop(L, 1); /* */
420}
421
422/*
423 * @brief Registers C functions as lua library in environment
424 *
425 * This is meant to replace luaL_register()
426 *
427 * @param env Environment.
428 * @param libname Name of library table.
429 * @param l Array of functions to register.
430 * @param metatable Library will be used as metatable (so register __index).
431 */
432void nlua_register( nlua_env env, const char *libname,
433 const luaL_Reg *l, int metatable )
434{
435 if (luaL_newmetatable(naevL, libname)) {
436 if (metatable) {
437 lua_pushvalue(naevL,-1);
438 lua_setfield(naevL,-2,"__index");
439 }
440 luaL_register(naevL, NULL, l);
441 } /* lib */
442 nlua_getenv(naevL, env, "naev"); /* lib, naev */
443 lua_pushvalue(naevL, -2); /* lib, naev, lib */
444 lua_setfield(naevL, -2, libname);/* lib, naev */
445 lua_pop(naevL, 1); /* lib */
446 nlua_setenv(naevL, env, libname);/* */
447}
448
454static lua_State *nlua_newState (void)
455{
456 /* Try to create the new state */
457 lua_State *L = luaL_newstate();
458 if (L == NULL) {
459 WARN(_("Failed to create new Lua state."));
460 return NULL;
461 }
462 return L;
463}
464
471static int nlua_loadBasic( lua_State* L )
472{
473 const char *override[] = { /* unsafe functions */
474 /*"collectgarbage",*/
475 "dofile",
476 "getfenv",
477 "load",
478 "loadfile",
479 NULL
480 };
481
482 luaL_openlibs(L);
483
484 /* move [un]pack to table.[un]pack as in Lua5.2 */
485 lua_getglobal(L, "table"); /* t */
486 lua_getglobal(L, "unpack"); /* t, u */
487 lua_setfield(L,-2,"unpack"); /* t */
488 lua_pop(L,1); /* */
489 lua_pushnil(L); /* nil */
490 lua_setglobal(L, "unpack"); /* */
491
492 /* replace non-safe functions */
493 for (int i=0; override[i]!=NULL; i++) {
494 lua_pushnil(L);
495 lua_setglobal(L, override[i]);
496 }
497
498 /* Override built-ins to use Naev for I/O. */
499 lua_register(L, "loadstring", luaB_loadstring);
500 lua_register(L, "print", cli_print);
501 lua_register(L, "printRaw", cli_printRaw);
502 lua_register(L, "warn", cli_warn);
503
504 /* Gettext functionality. */
505 lua_register(L, "_", nlua_gettext);
506 lua_register(L, "N_", nlua_gettext_noop);
507 lua_register(L, "n_", nlua_ngettext);
508 lua_register(L, "p_", nlua_pgettext);
509 luaL_register(L, "gettext", gettext_methods);
510
511 /* Sandbox "io" and "os". */
512 lua_newtable(L); /* io table */
513 lua_setglobal(L,"io");
514 lua_newtable(L); /* os table */
515 lua_pushcfunction(L, nlua_os_getenv);
516 lua_setfield(L,-2,"getenv");
517 lua_setglobal(L,"os");
518
519 /* Special math functions function. */
520 lua_getglobal(L,"math");
521 lua_pushcfunction(L, nlua_log2);
522 lua_setfield(L,-2,"log2");
523 lua_pushnil(L);
524 lua_setfield(L,-2,"mod"); /* Get rid of math.mod */
525 lua_pop(L,1);
526
527 return 0;
528}
529
536static int nlua_package_loader_lua( lua_State* L )
537{
538 size_t bufsize, l = 0;
539 char *buf = NULL, *q;
540 char path_filename[PATH_MAX], tmpname[PATH_MAX], tried_paths[STRMAX];
541 const char *packagepath, *start, *end;
542 const char *name = luaL_checkstring(L,1);
543 int done = 0;
544
545 /* Get paths to check. */
546 lua_getglobal(L, "package");
547 if (!lua_istable(L,-1)) {
548 lua_pop(L,1);
549 lua_pushstring(L, _(" package.path not found."));
550 return 1;
551 }
552 lua_getfield(L, -1, "path");
553 if (!lua_isstring(L,-1)) {
554 lua_pop(L,2);
555 lua_pushstring(L, _(" package.path not found."));
556 return 1;
557 }
558 packagepath = lua_tostring(L,-1);
559 lua_pop(L,2);
560
561 /* Parse path. */
562 start = packagepath;
563 while (!done) {
564 end = strchr( start, ';' );
565 if (end == NULL) {
566 done = 1;
567 end = &start[ strlen(start) ];
568 }
569 strncpy( tmpname, start, end-start );
570 tmpname[ end-start ] = '\0';
571 q = strchr( tmpname, '?' );
572 if (q==NULL) {
573 snprintf( path_filename, sizeof(path_filename), "%s%s", tmpname, name );
574 }
575 else {
576 *q = '\0';
577 snprintf( path_filename, sizeof(path_filename), "%s%s%s", tmpname, name, q+1 );
578 }
579 start = end+1;
580
581 /* Replace all '.' before the last '.' with '/' as they are a security risk. */
582 q = strrchr( path_filename, '.' );
583 for (int i=0; i < q-path_filename; i++)
584 if (path_filename[i]=='.')
585 path_filename[i] = '/';
586
587 /* Try to load the file. */
588 if (PHYSFS_exists( path_filename )) {
589 buf = ndata_read( path_filename, &bufsize );
590 if (buf != NULL)
591 break;
592 }
593
594 /* Didn't get to load it. */
595 l += scnprintf( &tried_paths[l], sizeof(tried_paths)-l, _("\n no ndata path '%s'"), path_filename );
596 }
597
598 /* Must have buf by now. */
599 if (buf == NULL) {
600 lua_pushstring(L, tried_paths);
601 return 1;
602 }
603
604 /* Try to process the Lua. It will leave a function or message on the stack, as required. */
605 luaL_loadbuffer(L, buf, bufsize, path_filename);
606 free(buf);
607 return 1;
608}
609
616static int nlua_package_loader_c( lua_State* L )
617{
618 const char *name = luaL_checkstring(L,1);
619 /* Hardcoded libraries only: we DO NOT honor package.cpath. */
620 if (strcmp( name, "utf8" ) == 0)
621 lua_pushcfunction( L, luaopen_utf8 );
622 else if (strcmp( name, "enet" ) == 0 && conf.lua_enet)
623 lua_pushcfunction( L, luaopen_enet );
624 else
625 lua_pushnil( L );
626 return 1;
627}
628
635static int nlua_package_loader_croot( lua_State* L )
636{
637 lua_pushnil( L );
638 return 1;
639}
640
649static int nlua_require( lua_State* L )
650{
651 const char *filename = luaL_checkstring(L,1);
652 int envtab;
653
654 /* Environment table to load module into */
655 envtab = lua_upvalueindex(1);
656
657 /* Check to see if already included. */
658 lua_getfield( L, envtab, NLUA_LOAD_TABLE ); /* t */
659 if (!lua_isnil(L,-1)) {
660 lua_getfield(L,-1,filename); /* t, f */
661 /* Already included. */
662 if (!lua_isnil(L,-1)) {
663 lua_remove(L, -2); /* val */
664 return 1;
665 }
666 lua_pop(L,2); /* */
667 }
668 /* Must create new NLUA_LOAD_TABLE table. */
669 else {
670 lua_newtable(L); /* t */
671 lua_setfield(L, envtab, NLUA_LOAD_TABLE); /* */
672 }
673
674 lua_getglobal(L, "package");
675 if (!lua_istable(L,-1)) {
676 lua_pop(L,1);
677 lua_pushstring(L, _("package.loaders must be a table"));
678 return 1;
679 }
680 lua_getfield(L, -1, "loaders");
681 lua_remove(L, -2);
682 if (!lua_istable(L, -1))
683 luaL_error(L, _("package.loaders must be a table"));
684 lua_pushliteral(L, ""); /* error message accumulator */
685 for (int i=1; ; i++) {
686 lua_rawgeti(L, -2, i); /* get a loader */
687 if (lua_isnil(L, -1))
688 luaL_error(L, _("module '%s' not found:%s"), filename, lua_tostring(L, -2));
689 lua_pushstring(L, filename);
690 lua_call(L, 1, 1); /* call it */
691 if (lua_isfunction(L, -1)) /* did it find module? */
692 break; /* module loaded successfully */
693 else if (lua_isstring(L, -1)) /* loader returned error message? */
694 lua_concat(L, 2); /* accumulate it */
695 else
696 lua_pop(L, 1);
697 }
698 lua_remove(L, -2);
699 lua_remove(L, -2);
700
701 lua_pushvalue(L, envtab);
702 lua_setfenv(L, -2);
703
704 /* run the buffer */
705 lua_pushstring(L, filename); /* pass name as first parameter */
706#if 0
707 if (lua_pcall(L, 1, 1, 0) != 0) {
708 /* will push the current error from the dobuffer */
709 lua_error(L);
710 return 1;
711 }
712#endif
713 lua_call(L, 1, 1);
714
715 /* Mark as loaded. */
716 /* val */
717 if (lua_isnil(L,-1)) {
718 lua_pop(L, 1);
719 lua_pushboolean(L, 1);
720 }
721 lua_getfield(L, envtab, NLUA_LOAD_TABLE); /* val, t */
722 lua_pushvalue(L, -2); /* val, t, val */
723 lua_setfield(L, -2, filename); /* val, t */
724 lua_pop(L, 1); /* val */
725
726 /* cleanup, success */
727 return 1;
728}
729
760int nlua_loadStandard( nlua_env env )
761{
762 int r = 0;
763 r |= nlua_loadNaev(env);
764 r |= nlua_loadVar(env);
765 r |= nlua_loadSpob(env);
766 r |= nlua_loadSystem(env);
767 r |= nlua_loadJump(env);
768 r |= nlua_loadTime(env);
769 r |= nlua_loadPlayer(env);
770 r |= nlua_loadPilot(env);
771 r |= nlua_loadRnd(env);
772 r |= nlua_loadDiff(env);
773 r |= nlua_loadFaction(env);
774 r |= nlua_loadVector(env);
775 r |= nlua_loadOutfit(env);
776 r |= nlua_loadCommodity(env);
777 r |= nlua_loadNews(env);
778 r |= nlua_loadShiplog(env);
779 r |= nlua_loadFile(env);
780 r |= nlua_loadData(env);
781 r |= nlua_loadLinOpt(env);
782 r |= nlua_loadSafelanes(env);
783 r |= nlua_loadSpfx(env);
784 r |= nlua_loadAudio(env);
785
786 return r;
787}
788
792int nlua_errTrace( lua_State *L )
793{
794 /* Handle special done case. */
795 const char *str = luaL_checkstring(L,1);
796 if (strcmp(str,NLUA_DONE)==0)
797 return 1;
798
799 /* Otherwise execute "debug.traceback( str, int )". */
800 lua_getglobal(L, "debug");
801 if (!lua_istable(L, -1)) {
802 lua_pop(L, 1);
803 return 1;
804 }
805 lua_getfield(L, -1, "traceback");
806 if (!lua_isfunction(L, -1)) {
807 lua_pop(L, 2);
808 return 1;
809 }
810 lua_pushvalue(L, 1);
811 lua_pushinteger(L, 2);
812 lua_call(L, 2, 1);
813 return 1;
814}
815
816/*
817 * @brief Wrapper around lua_pcall() that handles errors and enviornments
818 *
819 * @param env Environment.
820 * @param nargs Number of arguments to pass.
821 * @param nresults Number of return values to take.
822 */
823int nlua_pcall( nlua_env env, int nargs, int nresults )
824{
825 int errf, ret, prev_env;
826
827#if DEBUGGING
828 errf = lua_gettop(naevL) - nargs;
829 lua_pushcfunction(naevL, nlua_errTrace);
830 lua_insert(naevL, errf);
831#else /* DEBUGGING */
832 errf = 0;
833#endif /* DEBUGGING */
834
835 prev_env = __NLUA_CURENV;
836 __NLUA_CURENV = env;
837
838 ret = lua_pcall(naevL, nargs, nresults, errf);
839
840 __NLUA_CURENV = prev_env;
841
842#if DEBUGGING
843 lua_remove(naevL, errf);
844#endif /* DEBUGGING */
845
846 return ret;
847}
848
856int nlua_refenv( nlua_env env, const char *name )
857{
858 nlua_getenv( naevL, env, name );
859 if (!lua_isnil( naevL, -1 ))
860 return luaL_ref( naevL, LUA_REGISTRYINDEX );
861 lua_pop(naevL, 1);
862 return LUA_NOREF;
863}
864
873int nlua_refenvtype( nlua_env env, const char *name, int type )
874{
875 nlua_getenv( naevL, env, name );
876 if (lua_type( naevL, -1 ) == type)
877 return luaL_ref( naevL, LUA_REGISTRYINDEX );
878 lua_pop(naevL, 1);
879 return LUA_NOREF;
880}
881
889int nlua_reffield( int objref, const char *name )
890{
891 if (objref == LUA_NOREF)
892 return LUA_NOREF;
893 lua_rawgeti( naevL, LUA_REGISTRYINDEX, objref );
894 lua_getfield( naevL, -1, name );
895 lua_remove( naevL, -2 );
896 if (!lua_isnil( naevL, -1 ))
897 return luaL_ref( naevL, LUA_REGISTRYINDEX );
898 lua_pop(naevL, 1);
899 return LUA_NOREF;
900}
901
905int nlua_ref( lua_State *L, int idx )
906{
907 lua_pushvalue( L, idx );
908 return luaL_ref( L, LUA_REGISTRYINDEX );
909}
910
914void nlua_unref( lua_State *L, int idx )
915{
916 if (idx != LUA_NOREF)
917 luaL_unref( L, LUA_REGISTRYINDEX, idx );
918}
919
923void nlua_resize (void)
924{
925 lua_rawgeti(naevL, LUA_REGISTRYINDEX, nlua_envs); /* t */
926 lua_pushnil(naevL); /* t, n */
927 while (lua_next(naevL, -2) != 0) { /* t, k, v */
928 int env = lua_tointeger(naevL,-1); /* t, k, v */
929 lua_getfield(naevL, -2, "__resize"); /* t, k, v, f */
930 if (!lua_isnil(naevL,-1)) {
931 lua_pushinteger( naevL, SCREEN_W ); /* t, k, v, f, w */
932 lua_pushinteger( naevL, SCREEN_H ); /* t, k, v, f, w, h */
933 nlua_pcall( env, 2, 0 ); /* t, k, v */
934 lua_pop(naevL,1); /* t, k */
935 }
936 else
937 lua_pop(naevL,2); /* t, k */
938 } /* t */
939 lua_pop(naevL,1); /* */
940}
int cli_warn(lua_State *L)
Barebones warn implementation for Lua, allowing scripts to print warnings to stderr.
Definition: console.c:166
int cli_printRaw(lua_State *L)
Prints raw markup to the console.
Definition: console.c:188
int cli_print(lua_State *L)
Replacement for the internal Lua print to print to both the console and the terminal.
Definition: console.c:178
const char * pgettext_var(const char *msgctxt, const char *msgid)
Definition: gettext.c:303
Header file with generic functions and naev-specifics.
#define PATH_MAX
Definition: naev.h:50
void * ndata_read(const char *path, size_t *filesize)
Reads a file from the ndata (will be NUL terminated).
Definition: ndata.c:154
static int nlua_require(lua_State *L)
include( string module )
Definition: nlua.c:649
void nlua_unref(lua_State *L, int idx)
Removes a reference set with nlua_ref.
Definition: nlua.c:914
static int nlua_log2(lua_State *L)
Implements the Lua function math.log2 (base-2 logarithm).
Definition: nlua.c:146
static int nlua_loadBasic(lua_State *L)
Loads specially modified basic stuff.
Definition: nlua.c:471
int nlua_refenv(nlua_env env, const char *name)
Gets the reference of a global in a lua environment.
Definition: nlua.c:856
static int nlua_pgettext(lua_State *L)
gettext support with context.
Definition: nlua.c:122
int nlua_ref(lua_State *L, int idx)
Creates a new reference to a Lua structure at a position.
Definition: nlua.c:905
static int nlua_panic(lua_State *L)
Handles what to do when Lua panics.
Definition: nlua.c:170
int nlua_loadStandard(nlua_env env)
Loads the standard Naev Lua API.
Definition: nlua.c:760
static lua_State * nlua_newState(void)
Wrapper around luaL_newstate.
Definition: nlua.c:454
static int nlua_gettext_noop(lua_State *L)
gettext support (noop). Does not actually do anything, but gets detected by gettext.
Definition: nlua.c:138
int nlua_refenvtype(nlua_env env, const char *name, int type)
Gets the reference of a global in a lua environment if it matches a type.
Definition: nlua.c:873
static int nlua_gettext(lua_State *L)
gettext support.
Definition: nlua.c:87
int nlua_reffield(int objref, const char *name)
Gets the reference to the specified field from an object reference.
Definition: nlua.c:889
static size_t common_sz
Definition: nlua.c:53
static int nlua_package_loader_croot(lua_State *L)
load( string module ) – searcher function to replace package.loaders[4] (Lua 5.1),...
Definition: nlua.c:635
static char * common_script
Definition: nlua.c:52
static int nlua_package_loader_lua(lua_State *L)
load( string module ) – searcher function to replace package.loaders[2] (Lua 5.1),...
Definition: nlua.c:536
void nlua_resize(void)
Propagates a resize event to all the environments forcibly.
Definition: nlua.c:923
int nlua_errTrace(lua_State *L)
Gets a trace from Lua.
Definition: nlua.c:792
static int nlua_ngettext(lua_State *L)
gettext support for singular and plurals.
Definition: nlua.c:104
static int nlua_os_getenv(lua_State *L)
Implements the Lua function os.getenv. In the sandbox we only make a fake $HOME visible.
Definition: nlua.c:156
static int luaB_loadstring(lua_State *L)
Replacement for the internal Lua loadstring().
Definition: nlua.c:194
static int nlua_package_loader_c(lua_State *L)
load( string module ) – searcher function to replace package.loaders[3] (Lua 5.1),...
Definition: nlua.c:616
static const luaL_Reg gettext_methods[]
Definition: nlua.c:71
int nlua_loadAudio(nlua_env env)
Loads the audio library.
Definition: nlua_audio.c:311
int nlua_loadCommodity(nlua_env env)
Loads the commodity library.
int nlua_loadData(nlua_env env)
Loads the data library.
Definition: nlua_data.c:54
int nlua_loadDiff(nlua_env env)
Loads the diff Lua library.
Definition: nlua_diff.c:42
int nlua_loadFaction(nlua_env env)
Loads the faction library.
Definition: nlua_faction.c:94
int nlua_loadFile(nlua_env env)
Loads the file library.
Definition: nlua_file.c:64
int nlua_loadJump(nlua_env env)
Loads the jump library.
Definition: nlua_jump.c:61
int nlua_loadLinOpt(nlua_env env)
Loads the linopt library.
Definition: nlua_linopt.c:71
int nlua_loadNaev(nlua_env env)
Loads the Naev Lua library.
Definition: nlua_naev.c:114
int nlua_loadNews(nlua_env env)
Loads the news library.
Definition: nlua_news.c:59
int nlua_loadOutfit(nlua_env env)
Loads the outfit library.
Definition: nlua_outfit.c:83
int nlua_loadPilot(nlua_env env)
Loads the pilot library.
Definition: nlua_pilot.c:399
int nlua_loadPlayer(nlua_env env)
Loads the player Lua library.
Definition: nlua_player.c:224
int nlua_loadRnd(nlua_env env)
Loads the Random Number Lua library.
Definition: nlua_rnd.c:48
int nlua_loadSafelanes(nlua_env env)
Loads the safelanes Lua library.
int nlua_loadShiplog(nlua_env env)
Loads the mission Lua library.
Definition: nlua_shiplog.c:62
int nlua_loadSpfx(nlua_env env)
Loads the spfx library.
Definition: nlua_spfx.c:104
int nlua_loadSpob(nlua_env env)
Loads the spob library.
Definition: nlua_spob.c:111
int nlua_loadSystem(nlua_env env)
Loads the system library.
Definition: nlua_system.c:104
int nlua_loadTime(nlua_env env)
Loads the Time Lua library.
Definition: nlua_time.c:60
int nlua_loadVar(nlua_env env)
Loads the mission variable Lua library.
Definition: nlua_var.c:51
int nlua_loadVector(nlua_env env)
Loads the vector metatable.
Definition: nlua_vec2.c:80
int scnprintf(char *text, size_t maxlen, const char *fmt,...)
Like snprintf(), but returns the number of characters ACTUALLY "printed" into the buffer....
Definition: nstring.c:178
int loaded
Definition: conf.h:73
int lua_enet
Definition: conf.h:160