]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_base.h
Omnicleanup: header cleanup, add ModApiUtil shared between game and mainmenu
[dragonfireclient.git] / src / script / lua_api / l_base.h
1 /*
2 Minetest
3 Copyright (C) 2013 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 Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser 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 #ifndef L_BASE_H_
21 #define L_BASE_H_
22
23 #include "common/c_types.h"
24
25 extern "C" {
26 #include <lua.h>
27 #include <lauxlib.h>
28 }
29
30 class ScriptApiBase;
31 class Server;
32 class Environment;
33 class GUIEngine;
34
35 class ModApiBase {
36
37 protected:
38         static ScriptApiBase*   getScriptApiBase(lua_State *L);
39         static Server*          getServer(lua_State *L);
40         static Environment*     getEnv(lua_State *L);
41         static GUIEngine*       getGuiEngine(lua_State *L);
42
43         // Get an arbitrary subclass of ScriptApiBase
44         // by using dynamic_cast<> on getScriptApiBase()
45         template<typename T>
46         static T* getScriptApi(lua_State *L) {
47                 ScriptApiBase *scriptIface = getScriptApiBase(L);
48                 T *scriptIfaceDowncast = dynamic_cast<T*>(scriptIface);
49                 if (!scriptIfaceDowncast) {
50                         throw LuaError(L, "Requested unavailable ScriptApi - core engine bug!");
51                 }
52                 return scriptIfaceDowncast;
53         }
54
55         static bool registerFunction(lua_State *L,
56                         const char* name,
57                         lua_CFunction fct,
58                         int top
59                         );
60 };
61
62 #endif /* L_BASE_H_ */