26#include "nlua_audio.h"
27#include "nlua_commodity.h"
30#include "nlua_faction.h"
33#include "nlua_linopt.h"
36#include "nlua_outfit.h"
37#include "nlua_pilot.h"
39#include "nlua_player.h"
41#include "nlua_safelanes.h"
43#include "nlua_shiplog.h"
44#include "nlua_system.h"
51lua_State *naevL = NULL;
52nlua_env __NLUA_CURENV = LUA_NOREF;
55static int nlua_envs = LUA_NOREF;
90 const char *str = luaL_checkstring(L, 1);
91 lua_pushstring(L, _(str) );
107 const char *stra = luaL_checkstring(L, 1);
108 const char *strb = luaL_checkstring(L, 2);
109 int n = luaL_checkinteger(L,3);
110 lua_pushstring(L, n_( stra, strb, n ) );
125 const char *msgctxt = luaL_checkstring(L, 1);
126 const char *msgid = luaL_checkstring(L, 2);
141 const char *str = luaL_checkstring(L, 1);
142 lua_pushstring(L, str );
149 double n = luaL_checknumber(L,1);
150 lua_pushnumber(L, log2(n));
159 const char *var = luaL_checkstring(L, 1);
160 if (strcmp(var,
"HOME"))
162 lua_pushstring(L,
"lua_home");
173 ERR( _(
"LUA PANIC: %s"), lua_tostring(L,-1) );
186 lua_newtable( naevL );
187 nlua_envs = luaL_ref(naevL, LUA_REGISTRYINDEX);
199 const char *s = luaL_checklstring(L, 1, &l);
200 const char *chunkname = luaL_optstring(L, 2, s);
201 int status = luaL_loadbuffer(L, s, l, chunkname);
230int nlua_dobufenv( nlua_env env,
241 debug_disableFPUExcept();
243 ret = luaL_loadbuffer(naevL, buff, sz, name);
248 debug_enableFPUExcept();
251 nlua_pushenv(naevL, env);
252 lua_setfenv(naevL, -2);
253 ret = nlua_pcall(env, 0, LUA_MULTRET);
257 lua_pushstring( naevL, name );
258 nlua_setenv( naevL, env,
"__name" );
269int nlua_dofileenv( nlua_env env,
const char *filename )
271 if (luaL_loadfile(naevL, filename) != 0)
273 nlua_pushenv(naevL, env);
274 lua_setfenv(naevL, -2);
275 if (nlua_pcall(env, 0, LUA_MULTRET) != 0)
287int nlua_dochunkenv( nlua_env env,
int chunk,
const char *name )
291 lua_rawgeti( naevL, LUA_REGISTRYINDEX, chunk );
292 nlua_pushenv(naevL, env);
293 lua_setfenv(naevL, -2);
294 ret = nlua_pcall( env, 0, LUA_MULTRET );
298 lua_pushstring( naevL, name );
299 nlua_setenv( naevL, env,
"__name" );
305void nlua_pushEnvTable( lua_State *L )
307 lua_rawgeti( L, LUA_REGISTRYINDEX, nlua_envs );
316nlua_env nlua_newEnv (
void)
320 lua_pushvalue(naevL, -1);
321 ref = luaL_ref(naevL, LUA_REGISTRYINDEX);
324 lua_rawgeti(naevL, LUA_REGISTRYINDEX, nlua_envs);
325 lua_pushvalue(naevL, -2);
326 lua_pushinteger(naevL, ref);
327 lua_rawset(naevL, -3);
332 lua_pushvalue(naevL, LUA_GLOBALSINDEX);
333 lua_setfield(naevL, -2,
"__index");
334 lua_setmetatable(naevL, -2);
337 lua_pushvalue(naevL, -1);
339 lua_setfield(naevL, -2,
"require");
344 lua_getglobal(naevL,
"package");
345 lua_pushstring(naevL,
"?.lua;"LUA_INCLUDE_PATH
"?.lua");
346 lua_setfield(naevL, -2,
"path");
347 lua_pushstring(naevL,
"");
348 lua_setfield(naevL, -2,
"cpath");
349 lua_getfield(naevL, -1,
"loaders");
351 lua_rawseti(naevL, -2, 2);
353 lua_rawseti(naevL, -2, 3);
355 lua_rawseti(naevL, -2, 4);
359 lua_pushvalue(naevL, -1);
360 lua_setfield(naevL, -2,
"_G");
364 lua_pushboolean(naevL, 1);
365 lua_setfield(naevL, -2,
"__debugging");
370 lua_setfield(naevL, -2,
"naev");
376 WARN(_(
"Unable to load common script '%s'!"), LUA_COMMON_PATH);
380 if (nlua_pcall( ref, 0, 0 ) != 0) {
381 WARN(_(
"Failed to run '%s':\n%s"), LUA_COMMON_PATH, lua_tostring(naevL,-1));
386 WARN(_(
"Failed to load '%s':\n%s"), LUA_COMMON_PATH, lua_tostring(naevL,-1));
400void nlua_freeEnv( nlua_env env )
404 if (env == LUA_NOREF)
408 lua_rawgeti(naevL, LUA_REGISTRYINDEX, nlua_envs);
409 lua_rawgeti(naevL, LUA_REGISTRYINDEX, env);
411 lua_rawset(naevL, -3);
415 luaL_unref(naevL, LUA_REGISTRYINDEX, env);
423void nlua_pushenv( lua_State* L, nlua_env env )
425 lua_rawgeti(L, LUA_REGISTRYINDEX, env);
436void nlua_getenv( lua_State* L, nlua_env env,
const char *name )
438 nlua_pushenv(L, env);
439 lua_getfield(L, -1, name);
451void nlua_setenv( lua_State* L, nlua_env env,
const char *name )
454 nlua_pushenv(L, env);
456 lua_setfield(L, -2, name);
470void nlua_register( nlua_env env,
const char *libname,
471 const luaL_Reg *l,
int metatable )
473 if (luaL_newmetatable(naevL, libname)) {
475 lua_pushvalue(naevL,-1);
476 lua_setfield(naevL,-2,
"__index");
478 luaL_register(naevL, NULL, l);
480 nlua_getenv(naevL, env,
"naev");
481 lua_pushvalue(naevL, -2);
482 lua_setfield(naevL, -2, libname);
484 nlua_setenv(naevL, env, libname);
495 lua_State *L = luaL_newstate();
497 WARN(_(
"Failed to create new Lua state."));
511 const char *
override[] = {
523 lua_getglobal(L,
"table");
524 lua_getglobal(L,
"unpack");
525 lua_setfield(L,-2,
"unpack");
528 lua_setglobal(L,
"unpack");
531 for (
int i=0;
override[i]!=NULL; i++) {
533 lua_setglobal(L,
override[i]);
551 lua_setglobal(L,
"io");
554 lua_setfield(L,-2,
"getenv");
555 lua_setglobal(L,
"os");
558 lua_getglobal(L,
"math");
560 lua_setfield(L,-2,
"log2");
562 lua_setfield(L,-2,
"mod");
576 size_t bufsize, l = 0;
579 const char *packagepath, *start, *end;
580 const char *name = luaL_checkstring(L,1);
584 lua_getglobal(L,
"package");
585 if (!lua_istable(L,-1)) {
587 lua_pushstring(L, _(
" package.path not found."));
590 lua_getfield(L, -1,
"path");
591 if (!lua_isstring(L,-1)) {
593 lua_pushstring(L, _(
" package.path not found."));
596 packagepath = lua_tostring(L,-1);
603 end = strchr( start,
';' );
606 end = &start[ strlen(start) ];
608 strncpy( tmpname, start, end-start );
609 tmpname[ end-start ] =
'\0';
610 q = strchr( tmpname,
'?' );
612 snprintf( path_filename,
sizeof(path_filename),
"%s%s", tmpname, name );
616 snprintf( path_filename,
sizeof(path_filename),
"%s%s%s", tmpname, name, q+1 );
621 q = strrchr( path_filename,
'.' );
622 for (
int i=0; i < q-path_filename; i++)
623 if (path_filename[i]==
'.')
624 path_filename[i] =
'/';
627 if (PHYSFS_exists( path_filename )) {
634 l +=
scnprintf( &tried_paths[l],
sizeof(tried_paths)-l, _(
"\n no ndata path '%s'"), path_filename );
639 lua_pushstring(L, tried_paths);
644 luaL_loadbuffer(L, buf, bufsize, path_filename);
657 const char *name = luaL_checkstring(L,1);
659 if (strcmp( name,
"utf8" ) == 0)
660 lua_pushcfunction( L, luaopen_utf8 );
661 else if (strcmp( name,
"enet" ) == 0 && conf.
lua_enet)
662 lua_pushcfunction( L, luaopen_enet );
690 const char *filename = luaL_checkstring(L,1);
693 int envtab = lua_upvalueindex(1);
696 lua_getfield( L, envtab, NLUA_LOAD_TABLE );
697 if (!lua_isnil(L,-1)) {
698 lua_getfield(L,-1,filename);
700 if (!lua_isnil(L,-1)) {
709 lua_setfield(L, envtab, NLUA_LOAD_TABLE);
712 lua_getglobal(L,
"package");
713 if (!lua_istable(L,-1)) {
715 lua_pushstring(L, _(
"package.loaders must be a table"));
718 lua_getfield(L, -1,
"loaders");
720 if (!lua_istable(L, -1))
721 luaL_error(L, _(
"package.loaders must be a table"));
722 lua_pushliteral(L,
"");
723 for (
int i=1; ; i++) {
724 lua_rawgeti(L, -2, i);
725 if (lua_isnil(L, -1))
726 luaL_error(L, _(
"module '%s' not found:%s"), filename, lua_tostring(L, -2));
727 lua_pushstring(L, filename);
729 if (lua_isfunction(L, -1))
731 else if (lua_isstring(L, -1))
739 lua_pushvalue(L, envtab);
743 lua_pushstring(L, filename);
745 if (lua_pcall(L, 1, 1, 0) != 0) {
755 if (lua_isnil(L,-1)) {
757 lua_pushboolean(L, 1);
759 lua_getfield(L, envtab, NLUA_LOAD_TABLE);
760 lua_pushvalue(L, -2);
761 lua_setfield(L, -2, filename);
833 const char *str = luaL_checkstring(L,1);
834 if (strcmp(str,NLUA_DONE)==0)
838 lua_getglobal(L,
"debug");
839 if (!lua_istable(L, -1)) {
843 lua_getfield(L, -1,
"traceback");
844 if (!lua_isfunction(L, -1)) {
849 lua_pushinteger(L, 2);
861int nlua_pcall( nlua_env env,
int nargs,
int nresults )
863 int errf, ret, prev_env;
866 errf = lua_gettop(naevL) - nargs;
868 lua_insert(naevL, errf);
874 debug_disableFPUExcept();
879 prev_env = __NLUA_CURENV;
882 ret = lua_pcall(naevL, nargs, nresults, errf);
884 __NLUA_CURENV = prev_env;
887 lua_remove(naevL, errf);
890 debug_enableFPUExcept();
905 nlua_getenv( naevL, env, name );
906 if (!lua_isnil( naevL, -1 ))
907 return luaL_ref( naevL, LUA_REGISTRYINDEX );
922 nlua_getenv( naevL, env, name );
923 if (lua_type( naevL, -1 ) == type)
924 return luaL_ref( naevL, LUA_REGISTRYINDEX );
938 if (objref == LUA_NOREF)
940 lua_rawgeti( naevL, LUA_REGISTRYINDEX, objref );
941 lua_getfield( naevL, -1, name );
942 lua_remove( naevL, -2 );
943 if (!lua_isnil( naevL, -1 ))
944 return luaL_ref( naevL, LUA_REGISTRYINDEX );
954 lua_pushvalue( L, idx );
955 return luaL_ref( L, LUA_REGISTRYINDEX );
963 if (idx != LUA_NOREF)
964 luaL_unref( L, LUA_REGISTRYINDEX, idx );
972 lua_rawgeti(naevL, LUA_REGISTRYINDEX, nlua_envs);
974 while (lua_next(naevL, -2) != 0) {
975 int env = lua_tointeger(naevL,-1);
976 lua_getfield(naevL, -2,
"__resize");
977 if (!lua_isnil(naevL,-1)) {
978 lua_pushinteger( naevL, SCREEN_W );
979 lua_pushinteger( naevL, SCREEN_H );
980 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....