Skip to content

Serialize squirrel array to file #2626

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/squirrel/serialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <sexp/util.hpp>

#include "squirrel/squirrel_error.hpp"
#include "squirrel/squirrel_util.hpp"
#include "util/log.hpp"
#include "util/reader_mapping.hpp"
#include "util/writer.hpp"
Expand Down Expand Up @@ -133,6 +134,9 @@ void save_squirrel_table(HSQUIRRELVM vm, SQInteger table_idx, Writer& writer)
writer.end_list(key);
break;
}
case OT_ARRAY:
writer.write(key, squirrel2string(vm, -1, true));
break;
case OT_CLOSURE:
break; // ignore
case OT_NATIVECLOSURE:
Expand Down
34 changes: 26 additions & 8 deletions src/squirrel/squirrel_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include "supertux/game_object.hpp"
#include "util/log.hpp"

std::string squirrel2string(HSQUIRRELVM v, SQInteger i)
std::string squirrel2string(HSQUIRRELVM v, SQInteger i, bool lisp_format)
{
std::ostringstream os;
switch (sq_gettype(v, i))
Expand All @@ -41,9 +41,19 @@ std::string squirrel2string(HSQUIRRELVM v, SQInteger i)
SQBool p;
if (SQ_SUCCEEDED(sq_getbool(v, i, &p))) {
if (p)
os << "true";
{
if(lisp_format)
os << "#t";
else
os << "true";
}
else
os << "false";
{
if(lisp_format)
os << "#f";
else
os << "false";
}
}
break;
}
Expand All @@ -62,7 +72,10 @@ std::string squirrel2string(HSQUIRRELVM v, SQInteger i)
case OT_STRING: {
const SQChar* val;
sq_getstring(v, i, &val);
os << "\"" << val << "\"";
if(lisp_format)
os << val;
else
os << "\"" << val << "\"";
break;
}
case OT_TABLE: {
Expand All @@ -88,23 +101,28 @@ std::string squirrel2string(HSQUIRRELVM v, SQInteger i)
}
case OT_ARRAY: {
bool first = true;
os << "[";
if(!lisp_format)
os << "[";
sq_pushnull(v); //null iterator
while (SQ_SUCCEEDED(sq_next(v,i-1)))
{
if (!first) {
os << ", ";
if(lisp_format)
os << " ";
else
os << ", ";
}
first = false;

//here -1 is the value and -2 is the key
// we ignore the key, since that is just the index in an array
os << squirrel2string(v, -1);
os << squirrel2string(v, -1, lisp_format);

sq_pop(v,2); //pops key and val before the nex iteration
}
sq_pop(v, 1);
os << "]";
if(!lisp_format)
os << "]";
break;
}
case OT_USERDATA:
Expand Down
2 changes: 1 addition & 1 deletion src/squirrel/squirrel_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

typedef std::vector<HSQOBJECT> SquirrelObjectList;

std::string squirrel2string(HSQUIRRELVM vm, SQInteger i);
std::string squirrel2string(HSQUIRRELVM vm, SQInteger i, bool lisp_format = false);
void print_squirrel_stack(HSQUIRRELVM vm);

SQInteger squirrel_read_char(SQUserPointer file);
Expand Down
Loading