Esempio n. 1
0
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 );
	}
}
static int GLua_JSON_ToBoolean( lua_State *L )
{
	// Check and make sure we're using a valid index
	if( !FJSON_ValidateNode( L, 1 ) )
	{
		lua_pushnil(L);
		return 1;
	}

	// Convert to boolean
	cJSON *node = FJSON_RetrieveNode( L, 1 );
	lua_pushboolean( L, cJSON_ToBoolean( node ) );

	return 1;
}
Esempio n. 3
0
// called on a reconnect
void G_ReadClientSessionData( gclient_t *client ) {
	clientSession_t *sess = &client->sess;
	cJSON *root = NULL, *object = NULL;
	char fileName[MAX_QPATH] = {};
	char *buffer = NULL;
	fileHandle_t f = NULL_FILE;
	unsigned int len = 0;
	const char *tmp = NULL;

	Com_sprintf( fileName, sizeof(fileName), "session/client%02i.json", client - level.clients );
	len = trap->FS_Open( fileName, &f, FS_READ );

	// no file
	if ( !f || !len || len == -1 ) {
		trap->FS_Close( f );
		return;
	}

	buffer = (char *)malloc( len + 1 );
	if ( !buffer ) {
		return;
	}

	trap->FS_Read( buffer, len, f );
	trap->FS_Close( f );
	buffer[len] = '\0';

	// read buffer
	root = cJSON_Parse( buffer );
	free( buffer );

	if ( !root ) {
		Com_Printf( "G_ReadSessionData(%02i): could not parse session data\n", client - level.clients );
		return;
	}

	if ( (object = cJSON_GetObjectItem( root, "sessionTeam" )) ) {
		sess->sessionTeam = (team_t)cJSON_ToInteger( object );
	}
	if ( (object = cJSON_GetObjectItem( root, "spectatorTime" )) ) {
		sess->spectatorTime = cJSON_ToInteger( object );
	}
	if ( (object = cJSON_GetObjectItem( root, "spectatorState" )) ) {
		sess->spectatorState = (spectatorState_t)cJSON_ToInteger( object );
	}
	if ( (object = cJSON_GetObjectItem( root, "spectatorClient" )) ) {
		sess->spectatorClient = cJSON_ToInteger( object );
	}
	if ( (object = cJSON_GetObjectItem( root, "wins" )) ) {
		sess->wins = cJSON_ToInteger( object );
	}
	if ( (object = cJSON_GetObjectItem( root, "losses" )) ) {
		sess->losses = cJSON_ToInteger( object );
	}
	if ( (object = cJSON_GetObjectItem( root, "setForce" )) ) {
		sess->setForce = cJSON_ToInteger( object );
	}
	if ( (object = cJSON_GetObjectItem( root, "saberLevel" )) ) {
		sess->saberLevel = cJSON_ToInteger( object );
	}
	if ( (object = cJSON_GetObjectItem( root, "selectedFP" )) ) {
		sess->selectedFP = cJSON_ToInteger( object );
	}
	if ( (object = cJSON_GetObjectItem( root, "duelTeam" )) ) {
		sess->duelTeam = cJSON_ToInteger( object );
	}
	if ( (object = cJSON_GetObjectItem( root, "siegeDesiredTeam" )) ) {
		sess->siegeDesiredTeam = cJSON_ToInteger( object );
	}

	if ( (object = cJSON_GetObjectItem( root, "siegeClass" )) ) {
		if ( (tmp = cJSON_ToString( object )) ) {
			Q_strncpyz( sess->siegeClass, tmp, sizeof(sess->siegeClass) );
		}
	}
	if ( (object = cJSON_GetObjectItem( root, "IP" )) ) {
		if ( (tmp = cJSON_ToString( object )) ) {
			Q_strncpyz( sess->IP, tmp, sizeof(sess->IP) );
		}
	}
	if ( (object = cJSON_GetObjectItem( root, "admin" )) ) {
		if ( (tmp = cJSON_ToString( object )) ) {
			client->pers.adminUser = AM_ChecksumLogin( tmp );
		}
	}

	if ( (object = cJSON_GetObjectItem( root, "empowered" )) ) {
		client->pers.adminData.empowered = cJSON_ToBoolean( object );
	}
	if ( (object = cJSON_GetObjectItem( root, "merc" )) ) {
		client->pers.adminData.merc = cJSON_ToBoolean( object );
	}
	if ( (object = cJSON_GetObjectItem( root, "silenced" )) ) {
		client->pers.adminData.silenced = cJSON_ToBoolean( object );
	}
	if ( (object = cJSON_GetObjectItem( root, "slept" )) ) {
		client->pers.adminData.isSlept = cJSON_ToBoolean( object );
	}
	if ((object = cJSON_GetObjectItem(root, "tempprivs"))) {
		client->pers.tempprivs = cJSON_ToInteger(object);
	}

	client->ps.fd.saberAnimLevel = sess->saberLevel;
	client->ps.fd.saberDrawAnimLevel = sess->saberLevel;
	client->ps.fd.forcePowerSelected = sess->selectedFP;

	cJSON_Delete( root );
	root = NULL;
}