1 /*
2 Copyright (C) 2007, 2010 - Bit-Blot
3 
4 This file is part of Aquaria.
5 
6 Aquaria is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
10 
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 
15 See the GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 */
21 #ifndef SCRIPTINTERFACE_H
22 #define SCRIPTINTERFACE_H
23 
24 #include "../BBGE/Base.h"
25 #include "../BBGE/MemoryAllocatorSmallBlock.h"
26 
27 struct lua_State;
28 
29 class Entity;
30 class CollideEntity;
31 class ScriptedEntity;
32 
33 class Script
34 {
35 public:
Script(lua_State * L,const std::string & file)36 	Script(lua_State *L, const std::string &file) : L(L), file(file) {}
37 
38 	// function()
39 	bool call(const char *name);
40 	// function(number)
41 	bool call(const char *name, float param1);
42 	// function(pointer)
43 	bool call(const char *name, void *param1);
44 	// function(pointer, number)
45 	bool call(const char *name, void *param1, float param2);
46 	// function(pointer, pointer)
47 	bool call(const char *name, void *param1, void *param2);
48 	// function(pointer, number, number)
49 	bool call(const char *name, void *param1, float param2, float param3);
50 	// boolean = function(pointer, number, number)
51 	bool call(const char *name, void *param1, float param2, float param3, bool *ret1);
52 	// function(pointer, string, number)
53 	bool call(const char *name, void *param1, const char *param2, float param3);
54 	// function(pointer, string, pointer)
55 	bool call(const char *name, void *param1, const char *param2, void *param3);
56 	// function(pointer, pointer, pointer)
57 	bool call(const char *name, void *param1, void *param2, void *param3);
58 	// function(pointer, number, number, number)
59 	bool call(const char *name, void *param1, float param2, float param3, float param4);
60 	// function(pointer, pointer, pointer, pointer)
61 	bool call(const char *name, void *param1, void *param2, void *param3, void *param4);
62 	// boolean = function(pointer, pointer, pointer, number, number, number, number, pointer)
63 	bool call(const char *name, void *param1, void *param2, void *param3, float param4, float param5, float param6, float param7, void *param8, bool *ret1);
64 	// boolean = function(string)
65 	bool call(const char *name, const char *param, bool *ret);
66 	// string = function(string)
67 	bool call(const char *name, const char *param, std::string *ret);
68 	// string = function(string, string, string)
69 	bool call(const char *name, const char *param1, const char *param2, const char *param3, std::string *ret);
70 	// function(pointer, ...) - anything that is already on the stack is forwarded. Results are left on the stack.
71 	// Returns how many values the called function returned, or -1 in case of error.
72 	int callVariadic(const char *name, lua_State *L, int nparams, void *param);
73 
getLuaState()74 	lua_State *getLuaState() {return L;}
getFile()75 	const std::string &getFile() {return file;}
getLastError()76 	const std::string &getLastError() {return lastError;}
77 
78 protected:
79 	// Low-level helpers.
80 	void lookupFunc(const char *name);
81 	bool doCall(int nparams, int nrets = 0);
82 
83 	lua_State *L;
84 	std::string file;
85 	std::string lastError;
86 };
87 
88 class ScriptInterface
89 {
90 public:
91 	ScriptInterface();
92 	void init();
93 	void reset();
94 	void collectGarbage();
95 	int gcGetStats();
96 	void shutdown();
97 
98 	Script *openScript(const std::string &file, bool ignoremissing = false);
99 	void closeScript(Script *script);
100 
101 	bool runScript(const std::string &file, const std::string &func, bool ignoremissing = false);
102 	bool runScriptNum(const std::string &file, const std::string &func, int num);
103 
104 protected:
105 	lua_State *createLuaVM();
106 	void destroyLuaVM(lua_State *state);
107 	lua_State *createLuaThread(const std::string &file);
108 	int destroyLuaThread(const std::string &file, lua_State *thread);
109 	static void *the_alloc(void *ud, void *ptr, size_t osize, size_t nsize);
110 
111 	lua_State *baseState;
112 	SmallBlockAllocator _sballoc;
113 };
114 
115 #endif
116