]> git.lizzy.rs Git - minetest.git/blob - src/script.cpp
Scripting WIP
[minetest.git] / src / script.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2011 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "script.h"
21 #include <cstdarg>
22 #include <cstring>
23 #include <cstdio>
24 #include <cstdlib>
25 #include "log.h"
26 #include <iostream>
27
28 extern "C" {
29 #include <lua.h>
30 #include <lualib.h>
31 #include <lauxlib.h>
32 }
33
34 void script_error(lua_State *L, const char *fmt, ...)
35 {
36         va_list argp;
37         va_start(argp, fmt);
38         vfprintf(stderr, fmt, argp);
39         va_end(argp);
40         lua_close(L);
41         exit(EXIT_FAILURE);
42 }
43
44 void script_call_va(lua_State *L, const char *func, const char *sig, ...)
45 {
46         va_list vl;
47         int narg, nres; /* number of arguments and results */
48
49         va_start(vl, sig);
50         lua_getglobal(L, func); /* push function */
51
52         for (narg = 0; *sig; narg++) {
53                 /* repeat for each argument */
54                 /* check stack space */
55                 luaL_checkstack(L, 1, "too many arguments");
56                 switch (*sig++) {
57                 case 'd': /* double argument */
58                         lua_pushnumber(L, va_arg(vl, double));
59                         break;
60                 case 'i': /* int argument */
61                         lua_pushinteger(L, va_arg(vl, int));
62                         break;
63                 case 's': /* string argument */
64                         lua_pushstring(L, va_arg(vl, char *));
65                         break;
66                 case '>': /* end of arguments */
67                         goto endargs;
68                 default:
69                         script_error(L, "invalid option (%c)", *(sig - 1));
70                 }
71         }
72 endargs:
73
74         nres = strlen(sig); /* number of expected results */
75
76         if (lua_pcall(L, narg, nres, 0) != 0) /* do the call */
77                 script_error(L, "error calling '%s': %s", func, lua_tostring(L, -1));
78         
79         nres = -nres; /* stack index of first result */
80         while (*sig) { /* repeat for each result */
81                 switch (*sig++) {
82                 case 'd': /* double result */
83                         if (!lua_isnumber(L, nres))
84                         script_error(L, "wrong result type");
85                         *va_arg(vl, double *) = lua_tonumber(L, nres);
86                         break;
87                 case 'i': /* int result */
88                         if (!lua_isnumber(L, nres))
89                         script_error(L, "wrong result type");
90                         *va_arg(vl, int *) = lua_tointeger(L, nres);
91                         break;
92                 case 's': /* string result */
93                         if (!lua_isstring(L, nres))
94                         script_error(L, "wrong result type");
95                         *va_arg(vl, const char **) = lua_tostring(L, nres);
96                         break;
97                 default:
98                         script_error(L, "invalid option (%c)", *(sig - 1));
99                 }
100                 nres++;
101         }
102
103         va_end(vl);
104 }
105
106 bool script_load(lua_State *L, const char *path)
107 {
108         infostream<<"Loading and running script from "<<path<<std::endl;
109         int ret = luaL_loadfile(L, path) || lua_pcall(L, 0, 0, 0);
110         if(ret){
111                 errorstream<<"Failed to load and run script from "<<path<<": "<<lua_tostring(L, -1)<<std::endl;
112                 lua_pop(L, 1); // Pop error message from stack
113                 return false;
114         }
115         return true;
116 }
117
118 lua_State* script_init()
119 {
120         lua_State *L = luaL_newstate();
121         luaL_openlibs(L);
122         return L;
123 }
124
125 lua_State* script_deinit(lua_State *L)
126 {
127         lua_close(L);
128 }
129
130