fix
[supertux.git] / src / scripting / serialize.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
6 //  This program 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.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 #include <config.h>
20
21 #include "serialize.hpp"
22
23 #include <memory>
24 #include <assert.h>
25 #include "lisp/lisp.hpp"
26 #include "lisp/list_iterator.hpp"
27 #include "lisp/parser.hpp"
28 #include "lisp/writer.hpp"
29 #include "squirrel_error.hpp"
30
31 namespace Scripting
32 {
33
34 void load_squirrel_table(HSQUIRRELVM vm, SQInteger table_idx, const lisp::Lisp* lisp)
35 {
36   using namespace lisp;
37
38   if(table_idx < 0)
39     table_idx -= 2; 
40  
41   lisp::ListIterator iter(lisp);
42   while(iter.next() && iter.lisp() != NULL) {
43     const std::string& token = iter.item();
44     sq_pushstring(vm, token.c_str(), token.size());
45
46     const lisp::Lisp* value = iter.value();
47     switch(value->get_type()) {
48       case Lisp::TYPE_CONS:
49         sq_newtable(vm);
50         load_squirrel_table(vm, sq_gettop(vm), iter.lisp());
51         break;
52       case Lisp::TYPE_INTEGER:
53         sq_pushinteger(vm, value->get_int());
54         break;
55       case Lisp::TYPE_REAL:
56         sq_pushfloat(vm, value->get_float());
57         break;
58       case Lisp::TYPE_STRING:
59         sq_pushstring(vm, value->get_string().c_str(), -1);
60         break;
61       case Lisp::TYPE_BOOLEAN:
62         sq_pushbool(vm, value->get_bool() ? SQTrue : SQFalse);
63         break;
64       case Lisp::TYPE_SYMBOL:
65         std::cerr << "Unexpected symbol in lisp file...";
66         sq_pushnull(vm);
67         break;
68       default:
69         assert(false);
70         break;
71     }
72
73     if(SQ_FAILED(sq_createslot(vm, table_idx)))
74       throw Scripting::SquirrelError(vm, "Couldn't create new index");
75   }
76 }
77
78 void save_squirrel_table(HSQUIRRELVM vm, SQInteger table_idx, lisp::Writer& writer)
79 {
80   // offset because of sq_pushnull
81   if(table_idx < 0)
82     table_idx -= 1;
83   
84   //iterator table
85   sq_pushnull(vm);
86   while(SQ_SUCCEEDED(sq_next(vm, table_idx))) {
87     if(sq_gettype(vm, -2) != OT_STRING) {
88       std::cerr << "Table contains non-string key\n";
89       continue;
90     }
91     const SQChar* key;
92     sq_getstring(vm, -2, &key);
93
94     switch(sq_gettype(vm, -1)) {
95       case OT_INTEGER: {
96         SQInteger val;
97         sq_getinteger(vm, -1, &val);
98         writer.write_int(key, static_cast<int> (val));
99         break;
100       }
101       case OT_FLOAT: {
102         SQFloat val;
103         sq_getfloat(vm, -1, &val);
104         writer.write_float(key, static_cast<float> (val));
105         break;
106       }
107       case OT_BOOL: {
108         SQBool val;
109         sq_getbool(vm, -1, &val);
110         writer.write_bool(key, val == SQTrue);
111         break;
112       }
113       case OT_STRING: {
114         const SQChar* str;
115         sq_getstring(vm, -1, &str);
116         writer.write_string(key, reinterpret_cast<const char*> (str));
117         break;
118       }
119       case OT_TABLE: {
120         writer.start_list(key, true);
121         save_squirrel_table(vm, -1, writer);
122         writer.end_list(key);
123         break;
124       }
125       case OT_CLOSURE:
126         break; // ignore
127       case OT_NATIVECLOSURE:
128         break;
129       default:
130         std::cerr << "Can't serialize key '" << key << "' in table.\n";
131         break;
132     }
133     sq_pop(vm, 2);
134   }
135   sq_pop(vm, 1);
136 }
137
138 }
139