Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // pickler can handle simple types (boolean, number, string) and will drill
- // down into tables. it can do userdata for a specific set of types - Body and
- // its kids and SBodyPath. anything else will cause a lua error
- //
- // pickle format is newline-seperated. each line begins with a type value,
- // followed by data for that type as follows
- // f - number (float). stringified number on next line
- // b - boolean. N is 0 or 1, denoting false or true
- // s - string. stringified length number on next line, then string bytes follow immediately after, then newline
- // t - table. table contents are more pickled stuff (ie recursive)
- // n - end of table
- // u - userdata. type is on next line, followed by data
- // Body - data is a single stringified number for Serializer::LookupBody
- // SBodyPath - data is four stringified numbers, newline separated
- void LuaSerializer::pickle(lua_State *l, int idx, std::string &out)
- {
- static char buf[256];
- LUA_DEBUG_START(l);
- switch (lua_type(l, idx)) {
- case LUA_TNUMBER: {
- snprintf(buf, sizeof(buf), "f\n%f\n", lua_tonumber(l, idx));
- out += buf;
- break;
- }
- case LUA_TBOOLEAN: {
- snprintf(buf, sizeof(buf), "b\n%d\n", lua_toboolean(l, idx) ? 1 : 0);
- out += buf;
- break;
- }
- case LUA_TSTRING: {
- lua_pushvalue(l, idx);
- const char *str = lua_tostring(l, -1);
- snprintf(buf, sizeof(buf), "s\n%d\n", strlen(str));
- out += buf;
- out += str;
- out += "\n";
- lua_pop(l, 1);
- break;
- }
- case LUA_TTABLE: {
- out += "t\n";
- LUA_DEBUG_START(l);
- lua_pushvalue(l, idx);
- lua_pushnil(l);
- while (lua_next(l, -2)) {
- pickle(l, -2, out);
- pickle(l, -1, out);
- lua_pop(l, 1);
- }
- lua_pop(l, 1);
- LUA_DEBUG_END(l, 0);
- out += "n\n";
- break;
- }
- case LUA_TUSERDATA: {
- out += "u\n";
- lid *idp = (lid*)lua_touserdata(l, idx);
- LuaObjectBase *lo = LuaObjectBase::Lookup(*idp);
- if (lo->Isa("SBodyPath")) {
- SBodyPath *sbp = dynamic_cast<SBodyPath*>(lo->m_object);
- snprintf(buf, sizeof(buf), "SBodyPath\n%d\n%d\n%d\n%d\n", sbp->sectorX, sbp->sectorY, sbp->systemNum, sbp->sbodyId);
- out += buf;
- break;
- }
- if (lo->Isa("Body")) {
- Body *b = dynamic_cast<Body*>(lo->m_object);
- snprintf(buf, sizeof(buf), "Body\n%d\n", Serializer::LookupBody(b));
- out += buf;
- break;
- }
- // fall through
- }
- default: {
- assert(0);
- }
- }
- LUA_DEBUG_END(l, 0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement