robn

Untitled

Mar 30th, 2011
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.44 KB | None | 0 0
  1. // pickler can handle simple types (boolean, number, string) and will drill
  2. // down into tables. it can do userdata for a specific set of types - Body and
  3. // its kids and SBodyPath. anything else will cause a lua error
  4. //
  5. // pickle format is newline-seperated. each line begins with a type value,
  6. // followed by data for that type as follows
  7. //   f - number (float). stringified number on next line
  8. //   b - boolean. N is 0 or 1, denoting false or true
  9. //   s - string. stringified length number on next line, then string bytes follow immediately after, then newline
  10. //   t - table. table contents are more pickled stuff (ie recursive)
  11. //   n - end of table
  12. //   u - userdata. type is on next line, followed by data
  13. //     Body      - data is a single stringified number for Serializer::LookupBody
  14. //     SBodyPath - data is four stringified numbers, newline separated
  15.  
  16. void LuaSerializer::pickle(lua_State *l, int idx, std::string &out)
  17. {
  18.     static char buf[256];
  19.  
  20.     LUA_DEBUG_START(l);
  21.  
  22.     switch (lua_type(l, idx)) {
  23.         case LUA_TNUMBER: {
  24.             snprintf(buf, sizeof(buf), "f\n%f\n", lua_tonumber(l, idx));
  25.             out += buf;
  26.             break;
  27.         }
  28.  
  29.         case LUA_TBOOLEAN: {
  30.             snprintf(buf, sizeof(buf), "b\n%d\n", lua_toboolean(l, idx) ? 1 : 0);
  31.             out += buf;
  32.             break;
  33.         }
  34.  
  35.         case LUA_TSTRING: {
  36.             lua_pushvalue(l, idx);
  37.             const char *str = lua_tostring(l, -1);
  38.             snprintf(buf, sizeof(buf), "s\n%d\n", strlen(str));
  39.             out += buf;
  40.             out += str;
  41.             out += "\n";
  42.             lua_pop(l, 1);
  43.             break;
  44.         }
  45.  
  46.         case LUA_TTABLE: {
  47.             out += "t\n";
  48.             LUA_DEBUG_START(l);
  49.             lua_pushvalue(l, idx);
  50.             lua_pushnil(l);
  51.             while (lua_next(l, -2)) {
  52.                 pickle(l, -2, out);
  53.                 pickle(l, -1, out);
  54.                 lua_pop(l, 1);
  55.             }
  56.             lua_pop(l, 1);
  57.             LUA_DEBUG_END(l, 0);
  58.             out += "n\n";
  59.             break;
  60.         }
  61.  
  62.         case LUA_TUSERDATA: {
  63.             out += "u\n";
  64.             lid *idp = (lid*)lua_touserdata(l, idx);
  65.             LuaObjectBase *lo = LuaObjectBase::Lookup(*idp);
  66.  
  67.             if (lo->Isa("SBodyPath")) {
  68.                 SBodyPath *sbp = dynamic_cast<SBodyPath*>(lo->m_object);
  69.                 snprintf(buf, sizeof(buf), "SBodyPath\n%d\n%d\n%d\n%d\n", sbp->sectorX, sbp->sectorY, sbp->systemNum, sbp->sbodyId);
  70.                 out += buf;
  71.                 break;
  72.             }
  73.  
  74.             if (lo->Isa("Body")) {
  75.                 Body *b = dynamic_cast<Body*>(lo->m_object);
  76.                 snprintf(buf, sizeof(buf), "Body\n%d\n", Serializer::LookupBody(b));
  77.                 out += buf;
  78.                 break;
  79.             }
  80.  
  81.             // fall through
  82.         }
  83.  
  84.         default: {
  85.             assert(0);
  86.         }
  87.     }
  88.  
  89.     LUA_DEBUG_END(l, 0);
  90. }
Add Comment
Please, Sign In to add comment