Merge branch 'master' of git://github.com/BTAxis/naev into testmission
[naev.git] / src / nlua.c
blob6a3c53559f0a2a1fe465f8102d84e3b8446ef33a
1 /*
2 * See Licensing and Copyright notice in naev.h
3 */
5 /**
6 * @file nlua.c
8 * @brief Handles creating and setting up basic Lua environments.
9 */
11 #include "nlua.h"
13 #include "naev.h"
15 #include "lauxlib.h"
17 #include "nluadef.h"
18 #include "log.h"
19 #include "ndata.h"
20 #include "nlua_rnd.h"
21 #include "nlua_faction.h"
22 #include "nlua_var.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"
33 * prototypes
35 static int nlua_packfileLoader( lua_State* L );
38 /**
39 * @brief Wrapper around luaL_newstate.
41 * @return A newly created lua_State.
43 lua_State *nlua_newState (void)
45 lua_State *L;
47 /* try to create the new state */
48 L = luaL_newstate();
49 if (L == NULL) {
50 WARN("Failed to create new lua state.");
51 return NULL;
54 return L;
58 /**
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));
70 return 0;
74 /**
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 )
82 int i;
83 const char *override[] = { /* unsafe functions */
84 "collectgarbage",
85 "dofile",
86 "getfenv",
87 "getmetatable",
88 "load",
89 "loadfile",
90 "loadstring",
91 "rawequal",
92 "rawget",
93 "rawset",
94 "setfenv",
95 "setmetatable",
96 "END"
100 nlua_load(L,luaopen_base); /* open base. */
102 /* replace non-safe functions */
103 for (i=0; strcmp(override[i],"END")!=0; i++) {
104 lua_pushnil(L);
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. */
112 /* add our own */
113 lua_register(L, "include", nlua_packfileLoader);
115 return 0;
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;
130 char *buf;
131 uint32_t bufsize;
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)) {
142 lua_pop(L,1);
143 return 0;
145 lua_pop(L,1);
147 /* Must create new _include table. */
148 else {
149 lua_newtable(L);
150 lua_setglobal(L, "_include");
152 lua_pop(L,1);
154 /* Try to locate the data */
155 buf = ndata_read( filename, &bufsize );
156 if (buf == NULL) {
157 lua_pushfstring(L, "%s not found in ndata.", filename);
158 return 1;
161 /* run the buffer */
162 if (luaL_dobuffer(L, buf, bufsize, filename) != 0) {
163 /* will push the current error from the dobuffer */
164 lua_error(L);
165 return 1;
168 /* Mark as loaded. */
169 lua_getglobal(L, "_include");
170 lua_pushboolean(L, 1);
171 lua_setfield(L, -2, filename);
172 lua_pop(L, 2);
174 /* cleanup, success */
175 free(buf);
176 return 0;
181 * @brief Loads the standard NAEV Lua API.
183 * Loads the modules:
184 * - naev
185 * - space
186 * - planet
187 * - system
188 * - var
189 * - pilot
190 * - time
191 * - player
192 * - diff
193 * - faction
194 * - vec2
196 * Only is missing:
197 * - misn
198 * - tk
199 * - hook
200 * - music
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 )
208 int r;
210 r = 0;
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);
223 return r;