1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
|
/*
gpiPS3.c
GameSpy Presence SDK
devsupport@gamespy.com
***********************************************************************
Please see the GameSpy Presence SDK documentation for more information
**********************************************************************/
//INCLUDES
//////////
#include <stdlib.h>
#include <string.h>
#include "gpi.h"
#ifdef _PS3
//GLOBALS
//////////
uint8_t gpi_np_pool[SCE_NP_MIN_POOL_SIZE];
SceNpCommunicationId gpi_communication_id = {
{'N','P','X','S','0','0','0','0','5'},
'\0',
0,
0
};
//FUNCTIONS
///////////
int gpiNpBasicCallback(
int event,
int retCode,
uint32_t reqId,
void *arg
)
{
// No-op - can ignore any events
////////////////////////////////
return 0;
}
GPResult gpiInitializeNpBasic(
GPConnection * connection
)
{
int ret = 0;
GPIConnection * iconnection = (GPIConnection*)*connection;
iconnection->npInitialized = gsi_true;
// Initial NP init - after this we wait for status to get to online
////////////////////////////////////////////////////////////////////
ret = sceNpInit(SCE_NP_MIN_POOL_SIZE, gpi_np_pool);
if (ret == SCE_NP_ERROR_ALREADY_INITIALIZED)
{
// If already initialized - DO NOT terminate after sync (game might need it)
////////////////////////////////////////////////////////////////////////////
iconnection->npBasicGameInitialized = gsi_true;
}
else if (ret < 0)
{
iconnection->npBasicGameInitialized = gsi_true;
gsDebugFormat(GSIDebugCat_GP, GSIDebugType_Misc, GSIDebugLevel_HotError,
"gpiInitializeNpBasic: sceNpInit() failed, NP-functionality disabled. ret = 0x%x\n", ret);
return GP_MISC_ERROR;
}
else
iconnection->npBasicGameInitialized = gsi_false; //GP initialized, so destroy after complete
return GP_NO_ERROR;
}
// Freeing up transaction list darray
void gpiNpTransactionListFree(void *element)
{
npIdLookupTrans *aTrans = (npIdLookupTrans *)element;
freeclear(aTrans->npIdForAdd);
}
GPResult gpiCheckNpStatus(
GPConnection * connection
)
{
int ret = 0;
int status = SCE_NP_MANAGER_STATUS_OFFLINE;
SceNpId npId;
GPIConnection * iconnection = (GPIConnection*)*connection;
// Get NP status
////////////////
ret = sceNpManagerGetStatus(&status);
if (ret < 0)
{
gsDebugFormat(GSIDebugCat_GP, GSIDebugType_Misc, GSIDebugLevel_HotError,
"gpiCheckNpStatus: sceNpGetStatus() failed. ret = 0x%x\n", ret);
}
gsDebugFormat(GSIDebugCat_GP, GSIDebugType_Misc, GSIDebugLevel_Comment,
"gpiCheckNpStatus: sceNpGetStatus - status = %d\n", status);
// If NP status != online after the timeout period, stop syncing
////////////////////////////////////////////////////////////////
if (status != SCE_NP_MANAGER_STATUS_ONLINE && (current_time() - iconnection->loginTime > GPI_NP_STATUS_TIMEOUT))
{
gsDebugFormat(GSIDebugCat_GP, GSIDebugType_Misc, GSIDebugLevel_HotError,
"gpiCheckNpStatus: NP Status not online - timed out\n");
// Flag to stop the sync process
////////////////////////////////
iconnection->npPerformBuddySync = gsi_false;
iconnection->npPerformBlockSync = gsi_false;
return GP_MISC_ERROR;
}
// Once status is online, finish NP init
////////////////////////////////////////
if (status == SCE_NP_MANAGER_STATUS_ONLINE)
{
iconnection->loginTime = current_time();
// Note - we ignore error messages here - if something fails we really don't care
/////////////////////////////////////////////////////////////////////////////////
if (!iconnection->npBasicGameInitialized)
{
ret = sceNpBasicInit(); //obsolete?
if (ret < 0)
{
gsDebugFormat(GSIDebugCat_GP, GSIDebugType_Misc, GSIDebugLevel_HotError,
"gpiCheckNpStatus: sceNpBasicInit() failed. ret = 0x%x\n", ret);
}
ret = sceNpBasicRegisterHandler(&gpi_communication_id, gpiNpBasicCallback, NULL);
if (ret < 0)
{
gsDebugFormat(GSIDebugCat_GP, GSIDebugType_Misc, GSIDebugLevel_HotError,
"gpiCheckNpStatus: sceNpBasicRegisterHandler() failed. ret = 0x%x\n", ret);
}
}
ret = sceNpLookupInit();
if (ret == SCE_NP_COMMUNITY_ERROR_ALREADY_INITIALIZED)
{
// If already initialized - DO NOT terminate after GP destroy (game might need it)
//////////////////////////////////////////////////////////////////////////////////
iconnection->npLookupGameInitialized = gsi_true;
}
else if (ret < 0)
{
gsDebugFormat(GSIDebugCat_GP, GSIDebugType_Misc, GSIDebugLevel_HotError,
"gpiCheckNpStatus: sceNpLookupInit() failed. ret = 0x%x\n", ret);
iconnection->npLookupGameInitialized = gsi_true;
}
else
iconnection->npLookupGameInitialized = gsi_false;
// Regardless of game, create a title context id for GP to use for lookups
///////////////////////////////////////////////////////////////////////////
ret = sceNpManagerGetNpId(&npId);
if (ret < 0)
{
gsDebugFormat(GSIDebugCat_GP, GSIDebugType_Misc, GSIDebugLevel_HotError,
"gpiCheckNpStatus: sceNpManagerGetNpId() failed. ret = 0x%x\n", ret);
}
ret = sceNpLookupCreateTitleCtx(&gpi_communication_id, &npId);
if (ret < 0)
{
gsDebugFormat(GSIDebugCat_GP, GSIDebugType_Misc, GSIDebugLevel_HotError,
"gpiCheckNpStatus: sceNpLookupCreateTitleCtx() failed. ret = 0x%x\n", ret);
}
iconnection->npLookupTitleCtxId = ret;
// Mark status retrieval completed
//////////////////////////////////
iconnection->npStatusRetrieved = gsi_true;
gsDebugFormat(GSIDebugCat_GP, GSIDebugType_Misc, GSIDebugLevel_Comment,
"gpiCheckNpStatus: NP is now initialized with status.\n");
iconnection->npTransactionList = ArrayNew(sizeof(npIdLookupTrans), 1, gpiNpTransactionListFree);
if (!iconnection->npTransactionList)
Error(connection, GP_MEMORY_ERROR, "Out of memory.");
}
return GP_NO_ERROR;
}
GPResult gpiDestroyNpBasic(
GPConnection * connection
)
{
GPIConnection * iconnection = (GPIConnection*)*connection;
// Explicitly destroy title context we used for lookup
//////////////////////////////////////////////////////
if (iconnection->npLookupTitleCtxId >= 0)
sceNpLookupDestroyTitleCtx(iconnection->npLookupTitleCtxId);
// Do not destroy NpLookup or NpBasic if Game is using it
/////////////////////////////////////////////////////////
if (!iconnection->npLookupGameInitialized)
sceNpLookupTerm();
if (!iconnection->npBasicGameInitialized)
{
sceNpBasicUnregisterHandler();
// Obsolete?
sceNpBasicTerm();
sceNpTerm();
}
// Free up transaction list used for NP lookups
///////////////////////////////////////////////
if (iconnection->npTransactionList)
ArrayFree(iconnection->npTransactionList);
iconnection->npInitialized = gsi_false;
iconnection->npStatusRetrieved = gsi_false;
return GP_NO_ERROR;
}
GPResult gpiSyncNpBuddies(
GPConnection * connection
)
{
int ret;
SceNpId npId; //Buffer to store friend list entry's NP ID
gsi_u32 i, count = 0;
GPIConnection * iconnection = (GPIConnection*)*connection;
// Flag sync as complete so we don't do it more than once per login
////////////////////////////////////////////////////////////////////
iconnection->npPerformBuddySync = gsi_false;
// Get buddy count
///////////////////
ret = sceNpBasicGetFriendListEntryCount(&count);
if ( ret < 0 )
{
gsDebugFormat(GSIDebugCat_GP, GSIDebugType_Misc, GSIDebugLevel_HotError,
"PS3BuddySync: Failed to get NP friend list count\n");
}
// Loop through each buddy, check for existence of GSID account
///////////////////////////////////////////////////////////////
for (i = 0; i < count; i++)
{
memset(&npId, 0x00, sizeof(npId));
ret = sceNpBasicGetFriendListEntry(i, &npId);
if (ret < 0)
{
gsDebugFormat(GSIDebugCat_GP, GSIDebugType_Misc, GSIDebugLevel_HotError,
"PS3BuddySync: Failed to get NP friend entry #%d\n", i);
return GP_MISC_ERROR;
}
gsDebugFormat(GSIDebugCat_GP, GSIDebugType_Misc, GSIDebugLevel_Comment,
"PS3BuddySync: NP friend entry #%d, npid = %s. Queueing Search.\n", i, npId.handle.data);
gpiProfileSearchUniquenick(connection, npId.handle.data, &iconnection->namespaceID,
1, GP_NON_BLOCKING, (GPCallback)gpiSyncNpBuddiesCallback, NULL);
}
return GP_NO_ERROR;
}
void gpiSyncNpBuddiesCallback(
GPConnection * pconnection,
GPProfileSearchResponseArg * arg,
void * param
)
{
if(arg->result == GP_NO_ERROR)
{
if(arg->numMatches == 1)
{
// Check if already a buddy
////////////////////////////
if (!gpIsBuddy(pconnection, arg->matches[0].profile))
{
gsDebugFormat(GSIDebugCat_GP, GSIDebugType_Misc, GSIDebugLevel_Comment,
"PS3BuddySync: NP Buddy \"%s\" found in namespace %d. Sending Request.\n",
arg->matches[0].uniquenick, arg->matches[0].namespaceID);
// Send the add request
////////////////////////
gpSendBuddyRequest(pconnection, arg->matches[0].profile, _T("PS3 Buddy Sync"));
}
else
gsDebugFormat(GSIDebugCat_GP, GSIDebugType_Misc, GSIDebugLevel_Comment,
"PS3BuddySync: \"%s\" is already a buddy\n", arg->matches[0].uniquenick);
}
else
gsDebugFormat(GSIDebugCat_GP, GSIDebugType_Misc, GSIDebugLevel_Comment,
"PS3BuddySync: No suitable match found\n");
}
else
gsDebugFormat(GSIDebugCat_GP, GSIDebugType_Misc, GSIDebugLevel_HotError,
"PS3BuddySync: Buddy Search FAILED!\n");
GSI_UNUSED(pconnection);
GSI_UNUSED(param);
}
GPResult gpiSyncNpBlockList(
GPConnection * connection
)
{
int ret;
SceNpId npId; //Buffer to store block list entry's NP ID
gsi_u32 i, count = 0;
GPIConnection * iconnection = (GPIConnection*)*connection;
// Flag sync as complete so we don't do it more than once per login
////////////////////////////////////////////////////////////////////
iconnection->npPerformBlockSync = gsi_false;
// Get block list count
///////////////////////
ret = sceNpBasicGetBlockListEntryCount(&count);
if ( ret < 0 )
{
gsDebugFormat(GSIDebugCat_GP, GSIDebugType_Misc, GSIDebugLevel_HotError,
"PS3BlockSync: Failed to get NP block list count\n");
}
// Loop through each entry, check for existence of GSID account
///////////////////////////////////////////////////////////////
for (i = 0; i < count; i++)
{
memset(&npId, 0x00, sizeof(npId));
ret = sceNpBasicGetBlockListEntry(i, &npId);
if (ret < 0)
{
gsDebugFormat(GSIDebugCat_GP, GSIDebugType_Misc, GSIDebugLevel_HotError,
"PS3BlockSync: Failed to get NP block entry #%d\n", i);
return GP_MISC_ERROR;
}
gsDebugFormat(GSIDebugCat_GP, GSIDebugType_Misc, GSIDebugLevel_Comment,
"PS3BlockSync: NP block entry #%d, npid = %s. Queueing Search.\n", i, npId.handle.data);
gpiProfileSearchUniquenick(connection, npId.handle.data, &iconnection->namespaceID,
1, GP_NON_BLOCKING, (GPCallback)gpiSyncNpBlockListCallback, NULL);
}
return GP_NO_ERROR;
}
void gpiSyncNpBlockListCallback(
GPConnection * pconnection,
GPProfileSearchResponseArg * arg,
void * param
)
{
GPIProfile * pProfile;
GPIConnection * iconnection = (GPIConnection*)*pconnection;
if(arg->result == GP_NO_ERROR)
{
if(arg->numMatches == 1)
{
// Check if already blocked
////////////////////////////
if(!gpiGetProfile(pconnection, arg->matches[0].profile, &pProfile) || !pProfile->blocked)
{
gsDebugFormat(GSIDebugCat_GP, GSIDebugType_Misc, GSIDebugLevel_Comment,
"PS3BlockSync: NP Block Entry \"%s\" found in namespace %d. Adding to BlockedList.\n",
arg->matches[0].uniquenick, arg->matches[0].namespaceID);
// Add to GP Blocked List - set lock to make sure we dont try to add to NP list
///////////////////////////////////////////////////////////////////////////////
iconnection->npSyncLock = gsi_true;
gpiAddToBlockedList(pconnection, arg->matches[0].profile);
iconnection->npSyncLock = gsi_false;
}
else
gsDebugFormat(GSIDebugCat_GP, GSIDebugType_Misc, GSIDebugLevel_Comment,
"PS3BlockSync: \"%s\" is already blocked\n", arg->matches[0].uniquenick);
}
else
gsDebugFormat(GSIDebugCat_GP, GSIDebugType_Misc, GSIDebugLevel_Comment,
"PS3BlockSync: No suitable match found\n");
}
else
gsDebugFormat(GSIDebugCat_GP, GSIDebugType_Misc, GSIDebugLevel_HotError,
"PS3BlockSync: Block Entry Search FAILED!\n");
GSI_UNUSED(param);
}
GPResult gpiAddToNpBlockList(
GPConnection * connection,
int profileid
)
{
GPIConnection * iconnection = (GPIConnection*)*connection;
// TODO: consider developer method for cache input in order to check HDD cache?
// If NP status not resolved, don't bother with lookup
///////////////////////////////////////////////////////
if (!iconnection->npTransactionList || iconnection->npLookupTitleCtxId < 0)
{
gsDebugFormat(GSIDebugCat_GP, GSIDebugType_Misc, GSIDebugLevel_HotError,
"PS3AddToNpBlockList: Cancelling add - NP status not yet resolved.\n");
return GP_NO_ERROR;
}
// Do an info lookup to find out if this player has an NP account.
/////////////////////////////////////////////////////////////////
gpiGetInfo(connection, profileid, GP_CHECK_CACHE, GP_NON_BLOCKING,
(GPCallback)gpiAddToNpBlockListInfoCallback, NULL);
return GP_NO_ERROR;
}
void gpiAddToNpBlockListInfoCallback(
GPConnection * pconnection,
GPGetInfoResponseArg * arg,
void * param
)
{
SceNpOnlineId onlineId;
int ret;
npIdLookupTrans transaction;
GPIConnection * iconnection = (GPIConnection*)*pconnection;
#ifdef GSI_UNICODE
char asciiUniquenick[GP_UNIQUENICK_LEN];
#endif
if(arg->result == GP_NO_ERROR)
{
// Make sure its a PS3 uniquenick (e.g. we have the uniquenick)
///////////////////////////////////////////////////////////////
if (_tcslen(arg->uniquenick) != 0)
{
memset(&onlineId, 0, sizeof(onlineId));
#ifdef GSI_UNICODE
UCS2ToAsciiString(arg->uniquenick, (char*)asciiUniquenick);
strncpy(onlineId.data, asciiUniquenick, SCE_NET_NP_ONLINEID_MAX_LENGTH);
#else
strncpy(onlineId.data, arg->uniquenick, SCE_NET_NP_ONLINEID_MAX_LENGTH);
#endif
if (ArrayLength(iconnection->npTransactionList) < GPI_NP_NUM_TRANSACTIONS)
{
ret = sceNpLookupCreateTransactionCtx(iconnection->npLookupTitleCtxId);
if (ret < 0)
{
gsDebugFormat(GSIDebugCat_GP, GSIDebugType_Misc, GSIDebugLevel_HotError,
"PS3AddToNpBlockList: sceNpLookupCreateTransactionCtx() failed. ret = 0x%x\n", ret);
}
else
{
transaction.npIdForAdd = (SceNpId*)gsimalloc(sizeof(SceNpId));
if(transaction.npIdForAdd == NULL)
{
sceNpLookupDestroyTransactionCtx(ret);
gsDebugFormat(GSIDebugCat_GP, GSIDebugType_Misc, GSIDebugLevel_HotError,
"PS3AddToNpBlockList: Out of memory.\n");
return;
}
transaction.npTransId = ret;
transaction.npLookupDone = gsi_false;
ArrayAppend(iconnection->npTransactionList, &transaction);
// Perform NP lookup to get the NpId
/////////////////////////////////////
ret = sceNpLookupNpIdAsync(transaction.npTransId, &onlineId,
transaction.npIdForAdd, 0, NULL);
if (ret < 0)
{
gsDebugFormat(GSIDebugCat_GP, GSIDebugType_Misc, GSIDebugLevel_HotError,
"PS3AddToNpBlockList: sceNpLookupNpIdAsync() failed. ret = 0x%x\n", ret);
}
}
}
else
{
// Can only have a max of 32 simultaneous transactions (based on PS3 lib)
/////////////////////////////////////////////////////////////////////////
gsDebugFormat(GSIDebugCat_GP, GSIDebugType_Misc, GSIDebugLevel_WarmError,
"PS3AddToNpBlockList: Transactions limit reached for np lookups\n");
}
}
else
gsDebugFormat(GSIDebugCat_GP, GSIDebugType_Misc, GSIDebugLevel_HotError,
"PS3AddToNpBlockList: Profile [%d] does not have a uniquenick in namespace %d!\n",
arg->profile, iconnection->namespaceID);
}
else
gsDebugFormat(GSIDebugCat_GP, GSIDebugType_Misc, GSIDebugLevel_HotError,
"PS3AddToNpBlockList: Player Info lookup FAILED!\n");
GSI_UNUSED(pconnection);
GSI_UNUSED(param);
}
GPResult gpiProcessNp(GPConnection * connection)
{
int i, ret=0;
GPIConnection * iconnection = (GPIConnection*)*connection;
npIdLookupTrans * transaction;
// Check for uninitialized transaction darray
//////////////////////////////////////////////
if (!iconnection->npTransactionList)
return GP_NO_ERROR;
// Need to process Sysutil for the Async lookups
/////////////////////////////////////////////////
if (ArrayLength(iconnection->npTransactionList) > 0)
cellSysutilCheckCallback();
// Loop through all current transactions, check if complete
///////////////////////////////////////////////////////////
for (i=0; i < ArrayLength(iconnection->npTransactionList); i++)
{
// Grab next transaction in the list
/////////////////////////////////////
transaction = (npIdLookupTrans *)ArrayNth(iconnection->npTransactionList, i);
if (!transaction->npLookupDone)
{
if (sceNpLookupPollAsync(transaction->npTransId, &ret)==0)
transaction->npLookupDone = gsi_true;
}
else
{
if (ret<0)
{
gsDebugFormat(GSIDebugCat_GP, GSIDebugType_Misc, GSIDebugLevel_HotError,
"PS3AddToNpBlockList: sceNpLookupWaitAsync. ret = 0x%x\n", ret);
if (ret == (int)SCE_NP_COMMUNITY_SERVER_ERROR_NO_SUCH_USER_NPID)
{
gsDebugFormat(GSIDebugCat_GP, GSIDebugType_Misc, GSIDebugLevel_HotError,
"PS3AddToNpBlockList: Player '%s' is not an NP user.\n",
transaction->npIdForAdd->handle.data);
}
}
else
{
// Found an NpId, try to add
/////////////////////////////
ret = sceNpBasicAddBlockListEntry(transaction->npIdForAdd);
if (ret == (int)SCE_NP_BASIC_ERROR_BUSY)
{
// Oh nice, NP is too busy to help us.... keep on trying
/////////////////////////////////////////////////////////
gsDebugFormat(GSIDebugCat_GP, GSIDebugType_Misc, GSIDebugLevel_Comment,
"PS3AddToNpBlockList: SCE_NP_BASIC_ERROR_BUSY. continue trying to add to NP\n");
return GP_NO_ERROR;
}
else if ( ret < 0 )
{
gsDebugFormat(GSIDebugCat_GP, GSIDebugType_Misc, GSIDebugLevel_HotError,
"PS3AddToNpBlockList: sceNpBasicAddBlockListEntry() failed. ret = 0x%x\n", ret);
}
else
{
gsDebugFormat(GSIDebugCat_GP, GSIDebugType_Misc, GSIDebugLevel_Comment,
"PS3AddToNpBlockList: Player '%s' added to NP Block list.\n",
transaction->npIdForAdd->handle.data);
}
}
ret = sceNpLookupDestroyTransactionCtx(transaction->npTransId);
if (ret<0)
{
gsDebugFormat(GSIDebugCat_GP, GSIDebugType_Misc, GSIDebugLevel_HotError,
"PS3AddToNpBlockList: sceNpLookupDestroyTransactionCtx() failed. ret = 0x%x\n", ret);
}
// Delete Transaction when its complete
////////////////////////////////////////
ArrayDeleteAt(iconnection->npTransactionList, i);
}
}
return GP_NO_ERROR;
}
#endif
|