2648b5f083a3ecf6200cb6de25f3ab15fff40eed
[supertux.git] / src / scripting / squirrel_util.hpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #ifndef HEADER_SUPERTUX_SCRIPTING_SQUIRREL_UTIL_HPP
18 #define HEADER_SUPERTUX_SCRIPTING_SQUIRREL_UTIL_HPP
19
20 #include <sstream>
21
22 #include "scripting/squirrel_error.hpp"
23 #include "scripting/wrapper.hpp"
24
25 namespace Scripting {
26
27 extern HSQUIRRELVM global_vm;
28
29 void init_squirrel(bool enable_debugger);
30 void exit_squirrel();
31 void update_debugger();
32
33 std::string squirrel2string(HSQUIRRELVM vm, SQInteger i);
34 void print_squirrel_stack(HSQUIRRELVM vm);
35
36 HSQOBJECT create_thread(HSQUIRRELVM vm);
37 SQObject vm_to_object(HSQUIRRELVM vm);
38 HSQUIRRELVM object_to_vm(HSQOBJECT object);
39
40 void compile_script(HSQUIRRELVM vm, std::istream& in,
41                     const std::string& sourcename);
42 void compile_and_run(HSQUIRRELVM vm, std::istream& in,
43                      const std::string& sourcename);
44
45 template<typename T>
46 void expose_object(HSQUIRRELVM v, SQInteger table_idx, T* object,
47                    const std::string& name, bool free = false)
48 {
49   sq_pushstring(v, name.c_str(), -1);
50   Scripting::create_squirrel_instance(v, object, free);
51
52   if(table_idx < 0)
53     table_idx -= 2;
54
55   // register instance in root table
56   if(SQ_FAILED(sq_createslot(v, table_idx))) {
57     std::ostringstream msg;
58     msg << "Couldn't register object '" << name << "' in squirrel table";
59     throw Scripting::SquirrelError(v, msg.str());
60   }
61 }
62
63 static inline void unexpose_object(HSQUIRRELVM v, SQInteger table_idx,
64                                    const std::string& name)
65 {
66   sq_pushstring(v, name.c_str(), name.length());
67
68   if(table_idx < 0)
69     table_idx -= 1;
70
71   if(SQ_FAILED(sq_deleteslot(v, table_idx, SQFalse))) {
72     std::ostringstream msg;
73     msg << "Couldn't unregister object '" << name << "' in squirrel root table";
74     throw Scripting::SquirrelError(v, msg.str());
75   }
76 }
77
78 // begin serialization functions
79 void store_float(HSQUIRRELVM vm, const char* name, float val);
80 void store_int(HSQUIRRELVM vm, const char* name, int val);
81 void store_string(HSQUIRRELVM vm, const char* name, const std::string& val);
82 void store_bool(HSQUIRRELVM vm, const char* name, bool val);
83
84 bool has_float(HSQUIRRELVM vm, const char* name);
85 bool has_int(HSQUIRRELVM vm, const char* name);
86 bool has_string(HSQUIRRELVM vm, const char* name);
87 bool has_bool(HSQUIRRELVM vm, const char* name);
88
89 bool get_float(HSQUIRRELVM vm, const char* name, float& val);
90 bool get_int(HSQUIRRELVM vm, const char* name, int& val);
91 bool get_string(HSQUIRRELVM vm, const char* name, std::string& val);
92 bool get_bool(HSQUIRRELVM vm, const char* name, bool& val);
93
94 float read_float(HSQUIRRELVM vm, const char* name);
95 int read_int(HSQUIRRELVM vm, const char* name);
96 std::string read_string(HSQUIRRELVM vm, const char* name);
97 bool read_bool(HSQUIRRELVM vm, const char* name);
98 // end serialization functions
99
100 }
101
102 #endif
103
104 /* EOF */