2 * See Licensing and Copyright notice in naev.h
8 * @brief Handles creating and setting up basic Lua environments.
21 #include "nlua_faction.h"
23 #include "nlua_naev.h"
24 #include "nlua_space.h"
25 #include "nlua_time.h"
26 #include "nlua_player.h"
27 #include "nlua_pilot.h"
28 #include "nlua_vec2.h"
29 #include "nlua_diff.h"
35 static int nlua_packfileLoader( lua_State
* L
);
39 * @brief Wrapper around luaL_newstate.
41 * @return A newly created lua_State.
43 lua_State
*nlua_newState (void)
47 /* try to create the new state */
50 WARN("Failed to create new lua state.");
59 * @brief Opens a lua library.
61 * @param L Lua state to load the library into.
62 * @param f CFunction to load.
64 int nlua_load( lua_State
* L
, lua_CFunction f
)
66 lua_pushcfunction(L
, f
);
67 if (lua_pcall(L
, 0, 0, 0))
68 WARN("nlua include error: %s",lua_tostring(L
,1));
75 * @brief Loads specially modified basic stuff.
77 * @param L Lua State to load the basic stuff into.
78 * @return 0 on success.
80 int nlua_loadBasic( lua_State
* L
)
83 const char *override
[] = { /* unsafe functions */
100 nlua_load(L
,luaopen_base
); /* open base. */
102 /* replace non-safe functions */
103 for (i
=0; strcmp(override
[i
],"END")!=0; i
++) {
105 lua_setglobal(L
, override
[i
]);
108 nlua_load(L
,luaopen_math
); /* open math. */
109 nlua_load(L
,luaopen_table
); /* open table. */
110 nlua_load(L
, luaopen_string
); /* open string. */
113 lua_register(L
, "include", nlua_packfileLoader
);
120 * @brief include( string module )
122 * Loads a module into the current Lua state from inside the data file.
124 * @param module Name of the module to load.
125 * @return An error string on error.
127 static int nlua_packfileLoader( lua_State
* L
)
129 const char *filename
;
133 /* Get parameters. */
134 filename
= luaL_checkstring(L
,1);
136 /* Check to see if already included. */
137 lua_getglobal( L
, "_include" );
138 if (!lua_isnil(L
,-1)) {
139 lua_getfield(L
,-1,filename
);
140 /* Already included. */
141 if (!lua_isnil(L
,-1)) {
147 /* Must create new _include table. */
150 lua_setglobal(L
, "_include");
154 /* Try to locate the data */
155 buf
= ndata_read( filename
, &bufsize
);
157 lua_pushfstring(L
, "%s not found in ndata.", filename
);
162 if (luaL_dobuffer(L
, buf
, bufsize
, filename
) != 0) {
163 /* will push the current error from the dobuffer */
168 /* Mark as loaded. */
169 lua_getglobal(L
, "_include");
170 lua_pushboolean(L
, 1);
171 lua_setfield(L
, -2, filename
);
174 /* cleanup, success */
181 * @brief Loads the standard NAEV Lua API.
202 * @param L Lua State to load modules into.
203 * @param readonly Load as readonly (good for sandboxing).
204 * @return 0 on success.
206 int nlua_loadStandard( lua_State
*L
, int readonly
)
211 r
|= nlua_loadBasic(L
);
212 r
|= nlua_loadNaev(L
);
213 r
|= nlua_loadVar(L
,readonly
);
214 r
|= nlua_loadSpace(L
,readonly
); /* planet, system */
215 r
|= nlua_loadTime(L
,readonly
);
216 r
|= nlua_loadPlayer(L
,readonly
);
217 r
|= nlua_loadPilot(L
,readonly
);
218 r
|= nlua_loadRnd(L
);
219 r
|= nlua_loadDiff(L
,readonly
);
220 r
|= nlua_loadFaction(L
,readonly
);
221 r
|= nlua_loadVector(L
);