Massive copyright update. I'm sorry if I'm crediting Matze for something he didn...
[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
20 #include "serialize.hpp"
21
22 #include <memory>
23 #include <assert.h>
24 #include "lisp/lisp.hpp"
25 #include "lisp/list_iterator.hpp"
26 #include "lisp/parser.hpp"
27 #include "lisp/writer.hpp"
28 #include "squirrel_error.hpp"
29
30 namespace Scripting
31 {
32
33 void load_squirrel_table(HSQUIRRELVM vm, int table_idx, const lisp::Lisp* lisp)
34 {
35   using namespace lisp;
36
37   if(table_idx < 0)
38     table_idx -= 2; 
39  
40   lisp::ListIterator iter(lisp);
41   while(iter.next()) {
42     const std::string& token = iter.item();
43     sq_pushstring(vm, token.c_str(), token.size());
44
45     const lisp::Lisp* value = iter.value();
46     switch(value->get_type()) {
47       case Lisp::TYPE_CONS:
48         sq_newtable(vm);
49         load_squirrel_table(vm, sq_gettop(vm), iter.lisp());
50         break;
51       case Lisp::TYPE_INTEGER:
52         sq_pushinteger(vm, value->get_int());
53         break;
54       case Lisp::TYPE_REAL:
55         sq_pushfloat(vm, value->get_float());
56         break;
57       case Lisp::TYPE_STRING:
58         sq_pushstring(vm, value->get_string().c_str(), -1);
59         break;
60       case Lisp::TYPE_BOOLEAN:
61         sq_pushbool(vm, value->get_bool() ? SQTrue : SQFalse);
62         break;
63       case Lisp::TYPE_SYMBOL:
64         std::cerr << "Unexpected symbol in lisp file...";
65         sq_pushnull(vm);
66         break;
67       default:
68         assert(false);
69         break;
70     }
71
72     if(SQ_FAILED(sq_createslot(vm, table_idx)))
73       throw Scripting::SquirrelError(vm, "Couldn't create new index");
74   }
75 }
76
77 void save_squirrel_table(HSQUIRRELVM vm, int table_idx, lisp::Writer& writer)
78 {
79   // offset because of sq_pushnull
80   if(table_idx < 0)
81     table_idx -= 1;
82   
83   //iterator table
84   sq_pushnull(vm);
85   while(SQ_SUCCEEDED(sq_next(vm, table_idx))) {
86     if(sq_gettype(vm, -2) != OT_STRING) {
87       std::cerr << "Table contains non-string key\n";
88       continue;
89     }
90     const char* key;
91     sq_getstring(vm, -2, &key);
92
93     switch(sq_gettype(vm, -1)) {
94       case OT_INTEGER: {
95         int val;
96         sq_getinteger(vm, -1, &val);
97         writer.write_int(key, val);
98         break;
99       }
100       case OT_FLOAT: {
101         float val;
102         sq_getfloat(vm, -1, &val);
103         writer.write_float(key, val);
104         break;
105       }
106       case OT_BOOL: {
107         SQBool val;
108         sq_getbool(vm, -1, &val);
109         writer.write_bool(key, val);
110         break;
111       }
112       case OT_STRING: {
113         const char* str;
114         sq_getstring(vm, -1, &str);
115         writer.write_string(key, str);
116         break;
117       }
118       case OT_TABLE: {
119         writer.start_list(key, true);
120         save_squirrel_table(vm, -1, writer);
121         writer.end_list(key);
122         break;
123       }
124       case OT_CLOSURE:
125         break; // ignore
126       case OT_NATIVECLOSURE:
127         break;
128       default:
129         std::cerr << "Can't serialize key '" << key << "' in table.\n";
130         break;
131     }
132     sq_pop(vm, 2);
133   }
134   sq_pop(vm, 1);
135 }
136
137 }
138