void JPLua_Serialiser_IterateTableRead( cJSON *parent, const char *name, lua_State *L ) { cJSON *t = NULL; int numElements, i, top; lua_newtable( L ); top = lua_gettop( L ); t = cJSON_GetObjectItem( parent, name ); numElements = cJSON_GetArraySize( t ); for ( i = 0; i < numElements; i++ ) { cJSON *e, *it; int kType, vType; const char *tmp; char k[256]; e = cJSON_GetArrayItem( t, i ); // key it = cJSON_GetObjectItem( e, "key" ); kType = cJSON_ToInteger( cJSON_GetObjectItem( e, "key_type" ) ); if ( (tmp = cJSON_ToString( it )) ) Q_strncpyz( k, tmp, sizeof(k) ); if ( kType == LUA_TSTRING ) { lua_pushstring( L, k ); } else if ( kType == LUA_TNUMBER ) { lua_pushnumber( L, cJSON_ToNumber( it ) ); } else { Com_Printf( "Invalid key type %s when reading table %s\n", lua_typename( L, kType ), name ); } // value must be created based on type. it = cJSON_GetObjectItem( e, "value" ); vType = cJSON_ToInteger( cJSON_GetObjectItem( e, "value_type" ) ); if ( vType == LUA_TTABLE ) { JPLua_Serialiser_IterateTableRead( e, "value", L ); } else if ( vType == LUA_TNUMBER ) { lua_pushnumber( L, cJSON_ToNumber( it ) ); } else if ( vType == LUA_TBOOLEAN ) { lua_pushboolean( L, cJSON_ToBoolean( it ) ); } else if ( vType == LUA_TSTRING ) { char v[1024 * 8]; // should be plenty.. if ( (tmp = cJSON_ToString( it )) ) Q_strncpyz( v, tmp, sizeof(v) ); lua_pushstring( L, v ); } lua_settable( L, top ); } }
//Func: GetTable('string') //Retn: table int JPLua_Serialiser_GetTable( lua_State *L ) { jplua_serialiser_t *serialiser = JPLua_CheckSerialiser( L, 1 ); const char *tableName = NULL; luaL_argcheck( L, lua_type( L, 2 ) == LUA_TSTRING, 2, "'string' expected" ); tableName = lua_tostring( L, 2 ); JPLua_Serialiser_IterateTableRead( serialiser->inRoot, tableName, L ); return 0; }
//Func: GetTable( string name ) //Retn: table int JPLua_Serialiser_GetTable( lua_State *L ) { jplua_serialiser_t *serialiser = JPLua_CheckSerialiser( L, 1 ); if ( lua_type( L, 2 ) != LUA_TSTRING ) { JPLua_Serialiser_Close( L ); luaL_argcheck( L, 1, 2, "'string' expected" ); } if ( !serialiser->read ) { Com_Printf( "Serialiser is not available to read table\n" ); return 0; } JPLua_Serialiser_IterateTableRead( serialiser->inRoot, lua_tostring( L, 2 ), L ); return 1; }