25#include "nlua_audio.h"
26#include "nlua_commodity.h"
29#include "nlua_faction.h"
32#include "nlua_linopt.h"
35#include "nlua_outfit.h"
36#include "nlua_pilot.h"
38#include "nlua_player.h"
40#include "nlua_safelanes.h"
42#include "nlua_shiplog.h"
43#include "nlua_system.h"
50lua_State *naevL = NULL;
51nlua_env __NLUA_CURENV = LUA_NOREF;
54static int nlua_envs = LUA_NOREF;
89 const char *str = luaL_checkstring(L, 1);
90 lua_pushstring(L, _(str) );
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 ) );
124 const char *msgctxt = luaL_checkstring(L, 1);
125 const char *msgid = luaL_checkstring(L, 2);
140 const char *str = luaL_checkstring(L, 1);
141 lua_pushstring(L, str );
148 double n = luaL_checknumber(L,1);
149 lua_pushnumber(L, log2(n));
158 const char *var = luaL_checkstring(L, 1);
159 if (strcmp(var,
"HOME"))
161 lua_pushstring(L,
"lua_home");
172 ERR( _(
"LUA PANIC: %s"), lua_tostring(L,-1) );
184 lua_newtable( naevL );
185 nlua_envs = luaL_ref(naevL, LUA_REGISTRYINDEX);
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);
228int nlua_dobufenv( nlua_env env,
234 ret = luaL_loadbuffer(naevL, buff, sz, name);
237 nlua_pushenv(naevL, env);
238 lua_setfenv(naevL, -2);
239 ret = nlua_pcall(env, 0, LUA_MULTRET);
243 lua_pushstring( naevL, name );
244 nlua_setenv( naevL, env,
"__name" );
255int nlua_dofileenv( nlua_env env,
const char *filename )
257 if (luaL_loadfile(naevL, filename) != 0)
259 nlua_pushenv(naevL, env);
260 lua_setfenv(naevL, -2);
261 if (nlua_pcall(env, 0, LUA_MULTRET) != 0)
267void nlua_pushEnvTable( lua_State *L )
269 lua_rawgeti( L, LUA_REGISTRYINDEX, nlua_envs );
278nlua_env nlua_newEnv (
void)
282 lua_pushvalue(naevL, -1);
283 ref = luaL_ref(naevL, LUA_REGISTRYINDEX);
286 lua_rawgeti(naevL, LUA_REGISTRYINDEX, nlua_envs);
287 lua_pushvalue(naevL, -2);
288 lua_pushinteger(naevL, ref);
289 lua_rawset(naevL, -3);
294 lua_pushvalue(naevL, LUA_GLOBALSINDEX);
295 lua_setfield(naevL, -2,
"__index");
296 lua_setmetatable(naevL, -2);
299 lua_pushvalue(naevL, -1);
301 lua_setfield(naevL, -2,
"require");
306 lua_getglobal(naevL,
"package");
307 lua_pushstring(naevL,
"?.lua;"LUA_INCLUDE_PATH
"?.lua");
308 lua_setfield(naevL, -2,
"path");
309 lua_pushstring(naevL,
"");
310 lua_setfield(naevL, -2,
"cpath");
311 lua_getfield(naevL, -1,
"loaders");
313 lua_rawseti(naevL, -2, 2);
315 lua_rawseti(naevL, -2, 3);
317 lua_rawseti(naevL, -2, 4);
321 lua_pushvalue(naevL, -1);
322 lua_setfield(naevL, -2,
"_G");
326 lua_pushboolean(naevL, 1);
327 lua_setfield(naevL, -2,
"__debugging");
332 lua_setfield(naevL, -2,
"naev");
338 WARN(_(
"Unable to load common script '%s'!"), LUA_COMMON_PATH);
342 if (nlua_pcall( ref, 0, 0 ) != 0) {
343 WARN(_(
"Failed to run '%s':\n%s"), LUA_COMMON_PATH, lua_tostring(naevL,-1));
348 WARN(_(
"Failed to load '%s':\n%s"), LUA_COMMON_PATH, lua_tostring(naevL,-1));
362void nlua_freeEnv( nlua_env env )
366 if (env == LUA_NOREF)
370 lua_rawgeti(naevL, LUA_REGISTRYINDEX, nlua_envs);
371 lua_rawgeti(naevL, LUA_REGISTRYINDEX, env);
373 lua_rawset(naevL, -3);
377 luaL_unref(naevL, LUA_REGISTRYINDEX, env);
385void nlua_pushenv( lua_State* L, nlua_env env )
387 lua_rawgeti(L, LUA_REGISTRYINDEX, env);
398void nlua_getenv( lua_State* L, nlua_env env,
const char *name )
400 nlua_pushenv(L, env);
401 lua_getfield(L, -1, name);
413void nlua_setenv( lua_State* L, nlua_env env,
const char *name )
416 nlua_pushenv(L, env);
418 lua_setfield(L, -2, name);
432void nlua_register( nlua_env env,
const char *libname,
433 const luaL_Reg *l,
int metatable )
435 if (luaL_newmetatable(naevL, libname)) {
437 lua_pushvalue(naevL,-1);
438 lua_setfield(naevL,-2,
"__index");
440 luaL_register(naevL, NULL, l);
442 nlua_getenv(naevL, env,
"naev");
443 lua_pushvalue(naevL, -2);
444 lua_setfield(naevL, -2, libname);
446 nlua_setenv(naevL, env, libname);
457 lua_State *L = luaL_newstate();
459 WARN(_(
"Failed to create new Lua state."));
473 const char *
override[] = {
485 lua_getglobal(L,
"table");
486 lua_getglobal(L,
"unpack");
487 lua_setfield(L,-2,
"unpack");
490 lua_setglobal(L,
"unpack");
493 for (
int i=0;
override[i]!=NULL; i++) {
495 lua_setglobal(L,
override[i]);
513 lua_setglobal(L,
"io");
516 lua_setfield(L,-2,
"getenv");
517 lua_setglobal(L,
"os");
520 lua_getglobal(L,
"math");
522 lua_setfield(L,-2,
"log2");
524 lua_setfield(L,-2,
"mod");
538 size_t bufsize, l = 0;
539 char *buf = NULL, *q;
541 const char *packagepath, *start, *end;
542 const char *name = luaL_checkstring(L,1);
546 lua_getglobal(L,
"package");
547 if (!lua_istable(L,-1)) {
549 lua_pushstring(L, _(
" package.path not found."));
552 lua_getfield(L, -1,
"path");
553 if (!lua_isstring(L,-1)) {
555 lua_pushstring(L, _(
" package.path not found."));
558 packagepath = lua_tostring(L,-1);
564 end = strchr( start,
';' );
567 end = &start[ strlen(start) ];
569 strncpy( tmpname, start, end-start );
570 tmpname[ end-start ] =
'\0';
571 q = strchr( tmpname,
'?' );
573 snprintf( path_filename,
sizeof(path_filename),
"%s%s", tmpname, name );
577 snprintf( path_filename,
sizeof(path_filename),
"%s%s%s", tmpname, name, q+1 );
582 q = strrchr( path_filename,
'.' );
583 for (
int i=0; i < q-path_filename; i++)
584 if (path_filename[i]==
'.')
585 path_filename[i] =
'/';
588 if (PHYSFS_exists( path_filename )) {
595 l +=
scnprintf( &tried_paths[l],
sizeof(tried_paths)-l, _(
"\n no ndata path '%s'"), path_filename );
600 lua_pushstring(L, tried_paths);
605 luaL_loadbuffer(L, buf, bufsize, path_filename);
618 const char *name = luaL_checkstring(L,1);
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 );
651 const char *filename = luaL_checkstring(L,1);
655 envtab = lua_upvalueindex(1);
658 lua_getfield( L, envtab, NLUA_LOAD_TABLE );
659 if (!lua_isnil(L,-1)) {
660 lua_getfield(L,-1,filename);
662 if (!lua_isnil(L,-1)) {
671 lua_setfield(L, envtab, NLUA_LOAD_TABLE);
674 lua_getglobal(L,
"package");
675 if (!lua_istable(L,-1)) {
677 lua_pushstring(L, _(
"package.loaders must be a table"));
680 lua_getfield(L, -1,
"loaders");
682 if (!lua_istable(L, -1))
683 luaL_error(L, _(
"package.loaders must be a table"));
684 lua_pushliteral(L,
"");
685 for (
int i=1; ; i++) {
686 lua_rawgeti(L, -2, i);
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);
691 if (lua_isfunction(L, -1))
693 else if (lua_isstring(L, -1))
701 lua_pushvalue(L, envtab);
705 lua_pushstring(L, filename);
707 if (lua_pcall(L, 1, 1, 0) != 0) {
717 if (lua_isnil(L,-1)) {
719 lua_pushboolean(L, 1);
721 lua_getfield(L, envtab, NLUA_LOAD_TABLE);
722 lua_pushvalue(L, -2);
723 lua_setfield(L, -2, filename);
795 const char *str = luaL_checkstring(L,1);
796 if (strcmp(str,NLUA_DONE)==0)
800 lua_getglobal(L,
"debug");
801 if (!lua_istable(L, -1)) {
805 lua_getfield(L, -1,
"traceback");
806 if (!lua_isfunction(L, -1)) {
811 lua_pushinteger(L, 2);
823int nlua_pcall( nlua_env env,
int nargs,
int nresults )
825 int errf, ret, prev_env;
828 errf = lua_gettop(naevL) - nargs;
830 lua_insert(naevL, errf);
835 prev_env = __NLUA_CURENV;
838 ret = lua_pcall(naevL, nargs, nresults, errf);
840 __NLUA_CURENV = prev_env;
843 lua_remove(naevL, errf);
858 nlua_getenv( naevL, env, name );
859 if (!lua_isnil( naevL, -1 ))
860 return luaL_ref( naevL, LUA_REGISTRYINDEX );
875 nlua_getenv( naevL, env, name );
876 if (lua_type( naevL, -1 ) == type)
877 return luaL_ref( naevL, LUA_REGISTRYINDEX );
891 if (objref == 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 );
907 lua_pushvalue( L, idx );
908 return luaL_ref( L, LUA_REGISTRYINDEX );
916 if (idx != LUA_NOREF)
917 luaL_unref( L, LUA_REGISTRYINDEX, idx );
925 lua_rawgeti(naevL, LUA_REGISTRYINDEX, nlua_envs);
927 while (lua_next(naevL, -2) != 0) {
928 int env = lua_tointeger(naevL,-1);
929 lua_getfield(naevL, -2,
"__resize");
930 if (!lua_isnil(naevL,-1)) {
931 lua_pushinteger( naevL, SCREEN_W );
932 lua_pushinteger( naevL, SCREEN_H );
933 nlua_pcall( env, 2, 0 );
int cli_warn(lua_State *L)
Barebones warn implementation for Lua, allowing scripts to print warnings to stderr.
int cli_printRaw(lua_State *L)
Prints raw markup to the console.
int cli_print(lua_State *L)
Replacement for the internal Lua print to print to both the console and the terminal.
const char * pgettext_var(const char *msgctxt, const char *msgid)
Header file with generic functions and naev-specifics.
void * ndata_read(const char *path, size_t *filesize)
Reads a file from the ndata (will be NUL terminated).
static int nlua_require(lua_State *L)
include( string module )
void nlua_unref(lua_State *L, int idx)
Removes a reference set with nlua_ref.
static int nlua_log2(lua_State *L)
Implements the Lua function math.log2 (base-2 logarithm).
static int nlua_loadBasic(lua_State *L)
Loads specially modified basic stuff.
int nlua_refenv(nlua_env env, const char *name)
Gets the reference of a global in a lua environment.
static int nlua_pgettext(lua_State *L)
gettext support with context.
int nlua_ref(lua_State *L, int idx)
Creates a new reference to a Lua structure at a position.
static int nlua_panic(lua_State *L)
Handles what to do when Lua panics.
int nlua_loadStandard(nlua_env env)
Loads the standard Naev Lua API.
static lua_State * nlua_newState(void)
Wrapper around luaL_newstate.
static int nlua_gettext_noop(lua_State *L)
gettext support (noop). Does not actually do anything, but gets detected by gettext.
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.
static int nlua_gettext(lua_State *L)
gettext support.
int nlua_reffield(int objref, const char *name)
Gets the reference to the specified field from an object reference.
static int nlua_package_loader_croot(lua_State *L)
load( string module ) – searcher function to replace package.loaders[4] (Lua 5.1),...
static char * common_script
static int nlua_package_loader_lua(lua_State *L)
load( string module ) – searcher function to replace package.loaders[2] (Lua 5.1),...
void nlua_resize(void)
Propagates a resize event to all the environments forcibly.
int nlua_errTrace(lua_State *L)
Gets a trace from Lua.
static int nlua_ngettext(lua_State *L)
gettext support for singular and plurals.
static int nlua_os_getenv(lua_State *L)
Implements the Lua function os.getenv. In the sandbox we only make a fake $HOME visible.
static int luaB_loadstring(lua_State *L)
Replacement for the internal Lua loadstring().
static int nlua_package_loader_c(lua_State *L)
load( string module ) – searcher function to replace package.loaders[3] (Lua 5.1),...
static const luaL_Reg gettext_methods[]
int nlua_loadAudio(nlua_env env)
Loads the audio library.
int nlua_loadCommodity(nlua_env env)
Loads the commodity library.
int nlua_loadData(nlua_env env)
Loads the data library.
int nlua_loadDiff(nlua_env env)
Loads the diff Lua library.
int nlua_loadFaction(nlua_env env)
Loads the faction library.
int nlua_loadFile(nlua_env env)
Loads the file library.
int nlua_loadJump(nlua_env env)
Loads the jump library.
int nlua_loadLinOpt(nlua_env env)
Loads the linopt library.
int nlua_loadNaev(nlua_env env)
Loads the Naev Lua library.
int nlua_loadNews(nlua_env env)
Loads the news library.
int nlua_loadOutfit(nlua_env env)
Loads the outfit library.
int nlua_loadPilot(nlua_env env)
Loads the pilot library.
int nlua_loadPlayer(nlua_env env)
Loads the player Lua library.
int nlua_loadRnd(nlua_env env)
Loads the Random Number Lua library.
int nlua_loadSafelanes(nlua_env env)
Loads the safelanes Lua library.
int nlua_loadShiplog(nlua_env env)
Loads the mission Lua library.
int nlua_loadSpfx(nlua_env env)
Loads the spfx library.
int nlua_loadSpob(nlua_env env)
Loads the spob library.
int nlua_loadSystem(nlua_env env)
Loads the system library.
int nlua_loadTime(nlua_env env)
Loads the Time Lua library.
int nlua_loadVar(nlua_env env)
Loads the mission variable Lua library.
int nlua_loadVector(nlua_env env)
Loads the vector metatable.
int scnprintf(char *text, size_t maxlen, const char *fmt,...)
Like snprintf(), but returns the number of characters ACTUALLY "printed" into the buffer....