the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
at master 1492 lines 41 kB view raw
1#include "stdafx.h" 2 3#include "SonyCommerce.h" 4#include "..\PS3Extras\ShutdownManager.h" 5#include <sys/event.h> 6 7 8bool SonyCommerce::m_bCommerceInitialised = false; 9SceNpCommerce2SessionInfo SonyCommerce::m_sessionInfo; 10SonyCommerce::State SonyCommerce::m_state = e_state_noSession; 11int SonyCommerce::m_errorCode = 0; 12LPVOID SonyCommerce::m_callbackParam = NULL; 13 14void* SonyCommerce::m_receiveBuffer = NULL; 15SonyCommerce::Event SonyCommerce::m_event; 16std::queue<SonyCommerce::Message> SonyCommerce::m_messageQueue; 17std::vector<SonyCommerce::ProductInfo>* SonyCommerce::m_pProductInfoList = NULL; 18SonyCommerce::ProductInfoDetailed* SonyCommerce::m_pProductInfoDetailed = NULL; 19SonyCommerce::ProductInfo* SonyCommerce::m_pProductInfo = NULL; 20 21SonyCommerce::CategoryInfo* SonyCommerce::m_pCategoryInfo = NULL; 22const char* SonyCommerce::m_pProductID = NULL; 23char* SonyCommerce::m_pCategoryID = NULL; 24SonyCommerce::CheckoutInputParams SonyCommerce::m_checkoutInputParams; 25SonyCommerce::DownloadListInputParams SonyCommerce::m_downloadInputParams; 26 27SonyCommerce::CallbackFunc SonyCommerce::m_callbackFunc = NULL; 28sys_memory_container_t SonyCommerce::m_memContainer = SYS_MEMORY_CONTAINER_ID_INVALID; 29bool SonyCommerce::m_bUpgradingTrial = false; 30 31SonyCommerce::CallbackFunc SonyCommerce::m_trialUpgradeCallbackFunc; 32LPVOID SonyCommerce::m_trialUpgradeCallbackParam; 33 34CRITICAL_SECTION SonyCommerce::m_queueLock; 35 36 37 38uint32_t SonyCommerce::m_contextId=0; ///< The npcommerce2 context ID 39bool SonyCommerce::m_contextCreated=false; ///< npcommerce2 context ID created? 40SonyCommerce::Phase SonyCommerce::m_currentPhase = e_phase_stopped; ///< Current commerce2 util 41char SonyCommerce::m_commercebuffer[SCE_NP_COMMERCE2_RECV_BUF_SIZE]; 42 43C4JThread* SonyCommerce::m_tickThread = NULL; 44bool SonyCommerce::m_bLicenseChecked=false; // Check the trial/full license for the game 45 46 47SonyCommerce::ProductInfoDetailed s_trialUpgradeProductInfoDetailed; 48void SonyCommerce::Delete() 49{ 50 m_pProductInfoList=NULL; 51 m_pProductInfoDetailed=NULL; 52 m_pProductInfo=NULL; 53 m_pCategoryInfo = NULL; 54 m_pProductID = NULL; 55 m_pCategoryID = NULL; 56} 57void SonyCommerce::Init() 58{ 59 int ret; 60 61 assert(m_state == e_state_noSession); 62 if(!m_bCommerceInitialised) 63 { 64 ret = sceNpCommerce2Init(); 65 if (ret < 0) 66 { 67 app.DebugPrintf(4,"sceNpCommerce2Init failed (0x%x)\n", ret); 68 return; 69 } 70 else 71 { 72 m_bCommerceInitialised = true; 73 } 74 m_pCategoryID=(char *)malloc(sizeof(char) * 100); 75 InitializeCriticalSection(&m_queueLock); 76 } 77 78 return ; 79 80} 81 82 83 84void SonyCommerce::CheckForTrialUpgradeKey_Callback(LPVOID param, bool bFullVersion) 85{ 86 ProfileManager.SetFullVersion(bFullVersion); 87 if(ProfileManager.IsFullVersion()) 88 { 89 StorageManager.SetSaveDisabled(false); 90 ConsoleUIController::handleUnlockFullVersionCallback(); 91 // licence has been checked, so we're ok to install the trophies now 92 ProfileManager.InitialiseTrophies( SQRNetworkManager_PS3::GetSceNpCommsId(), 93 SQRNetworkManager_PS3::GetSceNpCommsSig()); 94 95 } 96 m_bLicenseChecked=true; 97} 98 99bool SonyCommerce::LicenseChecked() 100{ 101 return m_bLicenseChecked; 102} 103 104void SonyCommerce::CheckForTrialUpgradeKey() 105{ 106 StorageManager.CheckForTrialUpgradeKey(CheckForTrialUpgradeKey_Callback, NULL); 107} 108 109int SonyCommerce::Shutdown() 110{ 111 int ret=0; 112 if (m_contextCreated) 113 { 114 ret = sceNpCommerce2DestroyCtx(m_contextId); 115 if (ret != 0) 116 { 117 return ret; 118 } 119 120 m_contextId = 0; 121 m_contextCreated = false; 122 } 123 124 ret = sceNpCommerce2Term(); 125 m_bCommerceInitialised = false; 126 if (ret != 0) 127 { 128 return ret; 129 } 130 delete m_pCategoryID; 131 DeleteCriticalSection(&m_queueLock); 132 133 return ret; 134} 135 136 137 138int SonyCommerce::TickLoop(void* lpParam) 139{ 140 ShutdownManager::HasStarted(ShutdownManager::eCommerceThread); 141 while( (m_currentPhase != e_phase_stopped) && ShutdownManager::ShouldRun(ShutdownManager::eCommerceThread) ) 142 { 143 processEvent(); 144 processMessage(); 145 Sleep(16); // sleep for a frame 146 } 147 148 ShutdownManager::HasFinished(ShutdownManager::eCommerceThread); 149 150 return 0; 151} 152 153int SonyCommerce::getProductList(std::vector<ProductInfo>* productList, char *categoryId) 154{ 155 int ret = 0; 156 uint32_t requestId; 157 size_t bufSize = sizeof(m_commercebuffer); 158 size_t fillSize = 0; 159 SceNpCommerce2GetCategoryContentsResult result; 160 SceNpCommerce2CategoryInfo categoryInfo; 161 SceNpCommerce2ContentInfo contentInfo; 162 SceNpCommerce2GameProductInfo productInfo; 163 SceNpCommerce2GameSkuInfo skuInfo; 164 ProductInfo tempInfo; 165 std::vector<ProductInfo> tempProductVec; 166 167 if (!m_contextCreated) 168 { 169 ret = createContext(); 170 if (ret < 0) 171 { 172 setError(ret); 173 return ret; 174 } 175 } 176 177 // Create request ID 178 ret = sceNpCommerce2GetCategoryContentsCreateReq(m_contextId, &requestId); 179 if (ret < 0) 180 { 181 setError(ret); 182 return ret; 183 } 184 185 // Obtain category content data 186 ret = sceNpCommerce2GetCategoryContentsStart(requestId, categoryId, 0, SCE_NP_COMMERCE2_GETCAT_MAX_COUNT); 187 if (ret < 0) 188 { 189 sceNpCommerce2DestroyReq(requestId); 190 setError(ret); 191 return ret; 192 } 193 194 ret = sceNpCommerce2GetCategoryContentsGetResult(requestId, m_commercebuffer, bufSize, &fillSize); 195 if (ret < 0) 196 { 197 sceNpCommerce2DestroyReq(requestId); 198 setError(ret); 199 return ret; 200 } 201 202 ret = sceNpCommerce2DestroyReq(requestId); 203 if (ret < 0) 204 { 205 setError(ret); 206 return ret; 207 } 208 209 // We have the initial category content data, 210 // now to take out the category content information. 211 ret = sceNpCommerce2InitGetCategoryContentsResult(&result, m_commercebuffer, fillSize); 212 if (ret < 0) 213 { 214 setError(ret); 215 return ret; 216 } 217 218 // Get the category information 219 ret = sceNpCommerce2GetCategoryInfo(&result, &categoryInfo); 220 if (ret < 0) 221 { 222 sceNpCommerce2DestroyGetCategoryContentsResult(&result); 223 setError(ret); 224 return ret; 225 } 226 227 if(categoryInfo.countOfProduct==0) 228 { 229 // There is no DLC 230 return 0; 231 } 232 233 // Reserve some space 234 tempProductVec.reserve(categoryInfo.countOfProduct); 235 236 // For each product, obtain information 237 for (int i = 0; i < result.rangeOfContents.count; i++) 238 { 239 ret = sceNpCommerce2GetContentInfo(&result, i, &contentInfo); 240 if (ret < 0) 241 { 242 sceNpCommerce2DestroyGetCategoryContentsResult(&result); 243 setError(ret); 244 return ret; 245 } 246 247 // Only process if it is a product 248 if (contentInfo.contentType == SCE_NP_COMMERCE2_CONTENT_TYPE_PRODUCT) 249 { 250 251 // reset tempInfo 252 memset(&tempInfo, 0x0, sizeof(tempInfo)); 253 254 // Get product info 255 ret = sceNpCommerce2GetGameProductInfoFromContentInfo(&contentInfo, &productInfo); 256 if (ret < 0) 257 { 258 sceNpCommerce2DestroyGetCategoryContentsResult(&result); 259 setError(ret); 260 return ret; 261 } 262 263 // populate our temp struct 264 265 strncpy(tempInfo.productId, productInfo.productId, SCE_NP_COMMERCE2_PRODUCT_ID_LEN); 266 strncpy(tempInfo.productName, productInfo.productName, SCE_NP_COMMERCE2_PRODUCT_NAME_LEN); 267 strncpy(tempInfo.shortDescription, productInfo.productShortDescription, SCE_NP_COMMERCE2_PRODUCT_SHORT_DESCRIPTION_LEN); 268 if(tempInfo.longDescription[0]!=0) 269 { 270 strncpy(tempInfo.longDescription, productInfo.productLongDescription, SCE_NP_COMMERCE2_PRODUCT_LONG_DESCRIPTION_LEN); 271 } 272 else 273 { 274#ifdef _DEBUG 275 strcpy(tempInfo.longDescription,"Missing long description"); 276#endif 277 } 278 strncpy(tempInfo.spName, productInfo.spName, SCE_NP_COMMERCE2_SP_NAME_LEN); 279 strncpy(tempInfo.imageUrl, productInfo.imageUrl, SCE_NP_COMMERCE2_URL_LEN); 280 tempInfo.releaseDate = productInfo.releaseDate; 281 282 if (productInfo.countOfSku == 1) 283 { 284 // Get SKU info 285 ret = sceNpCommerce2GetGameSkuInfoFromGameProductInfo(&productInfo, 0, &skuInfo); 286 if (ret < 0) 287 { 288 sceNpCommerce2DestroyGetCategoryContentsResult(&result); 289 setError(ret); 290 return ret; 291 } 292 tempInfo.purchasabilityFlag = skuInfo.purchasabilityFlag; 293 294 // Take out the price. Nicely formatted 295 // but also keep the price as a value in case it's 0 - we need to show "free" for that 296 tempInfo.ui32Price= skuInfo.price; 297 ret = sceNpCommerce2GetPrice(m_contextId, tempInfo.price, sizeof(tempInfo.price), skuInfo.price); 298 299 if (ret < 0) 300 { 301 sceNpCommerce2DestroyGetCategoryContentsResult(&result); 302 setError(ret); 303 return ret; 304 } 305 } 306 tempProductVec.push_back(tempInfo); 307 } 308 } 309 310 // Set our result 311 *productList = tempProductVec; 312 313 // Destroy the category contents result 314 ret = sceNpCommerce2DestroyGetCategoryContentsResult(&result); 315 if (ret < 0) 316 { 317 return ret; 318 } 319 320 return ret; 321} 322 323int SonyCommerce::getCategoryInfo(CategoryInfo *pInfo, char *categoryId) 324{ 325 int ret = 0; 326 uint32_t requestId; 327 size_t bufSize = sizeof(m_commercebuffer); 328 size_t fillSize = 0; 329 SceNpCommerce2GetCategoryContentsResult result; 330 SceNpCommerce2CategoryInfo categoryInfo; 331 SceNpCommerce2ContentInfo contentInfo; 332 //CategoryInfo tempCatInfo; 333 CategoryInfoSub tempSubCatInfo; 334 335 if (!m_contextCreated) 336 { 337 ret = createContext(); 338 if (ret < 0) 339 { 340 m_errorCode = ret; 341 return ret; 342 } 343 } 344 345 // Create request ID 346 ret = sceNpCommerce2GetCategoryContentsCreateReq(m_contextId, &requestId); 347 if (ret < 0) 348 { 349 m_errorCode = ret; 350 return ret; 351 } 352 353 // Obtain category content data 354 if (categoryId) 355 { 356 ret = sceNpCommerce2GetCategoryContentsStart(requestId, categoryId, 0, SCE_NP_COMMERCE2_GETCAT_MAX_COUNT); 357 } 358 else 359 { 360 ret = sceNpCommerce2GetCategoryContentsStart(requestId,categoryId, 361 0, SCE_NP_COMMERCE2_GETCAT_MAX_COUNT); 362 } 363 if (ret < 0) 364 { 365 sceNpCommerce2DestroyReq(requestId); 366 m_errorCode = ret; 367 return ret; 368 } 369 370 ret = sceNpCommerce2GetCategoryContentsGetResult(requestId, m_commercebuffer, bufSize, &fillSize); 371 if (ret < 0) 372 { 373 if(ret==SCE_NP_COMMERCE2_ERROR_SERVER_MAINTENANCE) 374 { 375 app.DebugPrintf(4,"\n--- SCE_NP_COMMERCE2_ERROR_SERVER_MAINTENANCE ---\n\n"); 376 } 377 sceNpCommerce2DestroyReq(requestId); 378 m_errorCode = ret; 379 return ret; 380 } 381 382 ret = sceNpCommerce2DestroyReq(requestId); 383 if (ret < 0) 384 { 385 m_errorCode = ret; 386 return ret; 387 } 388 389 // We have the initial category content data, 390 // now to take out the category content information. 391 ret = sceNpCommerce2InitGetCategoryContentsResult(&result, m_commercebuffer, fillSize); 392 if (ret < 0) { 393 m_errorCode = ret; 394 return ret; 395 } 396 397 // Get the category information 398 ret = sceNpCommerce2GetCategoryInfo(&result, &categoryInfo); 399 if (ret < 0) { 400 sceNpCommerce2DestroyGetCategoryContentsResult(&result); 401 m_errorCode = ret; 402 return ret; 403 } 404 405 strcpy(pInfo->current.categoryId, categoryInfo.categoryId); 406 strcpy(pInfo->current.categoryName, categoryInfo.categoryName); 407 strcpy(pInfo->current.categoryDescription, categoryInfo.categoryDescription); 408 strcpy(pInfo->current.imageUrl, categoryInfo.imageUrl); 409 pInfo->countOfProducts = categoryInfo.countOfProduct; 410 pInfo->countOfSubCategories = categoryInfo.countOfSubCategory; 411 412 if (categoryInfo.countOfSubCategory > 0) 413 { 414 // For each sub category, obtain information 415 for (int i = 0; i < result.rangeOfContents.count; i++) 416 { 417 418 ret = sceNpCommerce2GetContentInfo(&result, i, &contentInfo); 419 if (ret < 0) 420 { 421 sceNpCommerce2DestroyGetCategoryContentsResult(&result); 422 m_errorCode = ret; 423 return ret; 424 } 425 426 // Only process if it is a category 427 if (contentInfo.contentType == SCE_NP_COMMERCE2_CONTENT_TYPE_CATEGORY) 428 { 429 430 ret = sceNpCommerce2GetCategoryInfoFromContentInfo(&contentInfo, &categoryInfo); 431 if (ret < 0) 432 { 433 sceNpCommerce2DestroyGetCategoryContentsResult(&result); 434 m_errorCode = ret; 435 return ret; 436 } 437 438 strcpy(tempSubCatInfo.categoryId, categoryInfo.categoryId); 439 strcpy(tempSubCatInfo.categoryName, categoryInfo.categoryName); 440 strcpy(tempSubCatInfo.categoryDescription, categoryInfo.categoryDescription); 441 strcpy(tempSubCatInfo.imageUrl, categoryInfo.imageUrl); 442 443 // Add to the list 444 pInfo->subCategories.push_back(tempSubCatInfo); 445 } 446 } 447 } 448 449 // Set our result 450 //*info = tempCatInfo; 451 452 // Destroy the category contents result 453 ret = sceNpCommerce2DestroyGetCategoryContentsResult(&result); 454 if (ret < 0) { 455 return ret; 456 } 457 458 return ret; 459} 460 461 462int SonyCommerce::getDetailedProductInfo(ProductInfoDetailed *pInfo, const char *productId, char *categoryId) 463{ 464 int ret = 0; 465 uint32_t requestId; 466 size_t bufSize = sizeof(m_commercebuffer); 467 size_t fillSize = 0; 468 std::list<SceNpCommerce2ContentRatingDescriptor> ratingDescList; 469 SceNpCommerce2GetProductInfoResult result; 470 SceNpCommerce2ContentRatingInfo ratingInfo; 471 SceNpCommerce2GameProductInfo productInfo; 472 SceNpCommerce2GameSkuInfo skuInfo; 473 //ProductInfoDetailed tempInfo; 474 475 if (!m_contextCreated) { 476 ret = createContext(); 477 if (ret < 0) { 478 m_errorCode = ret; 479 return ret; 480 } 481 } 482 483 // Obtain product data 484 ret = sceNpCommerce2GetProductInfoCreateReq(m_contextId, &requestId); 485 if (ret < 0) { 486 m_errorCode = ret; 487 return ret; 488 } 489 490 if (categoryId && categoryId[0] != 0) { 491 ret = sceNpCommerce2GetProductInfoStart(requestId, categoryId, productId); 492 } else { 493 ret = sceNpCommerce2GetProductInfoStart(requestId, NULL, productId); 494 } 495 if (ret < 0) { 496 sceNpCommerce2DestroyReq(requestId); 497 m_errorCode = ret; 498 return ret; 499 } 500 501 ret = sceNpCommerce2GetProductInfoGetResult(requestId, m_commercebuffer, bufSize, &fillSize); 502 if (ret < 0) { 503 sceNpCommerce2DestroyReq(requestId); 504 m_errorCode = ret; 505 return ret; 506 } 507 508 ret = sceNpCommerce2DestroyReq(requestId); 509 if (ret < 0) 510 { 511 m_errorCode = ret; 512 return ret; 513 } 514 515 // Take Out Game Product Information 516 ret = sceNpCommerce2InitGetProductInfoResult(&result, m_commercebuffer, fillSize); 517 if (ret < 0) 518 { 519 m_errorCode = ret; 520 return ret; 521 } 522 523 ret = sceNpCommerce2GetGameProductInfo(&result, &productInfo); 524 if (ret < 0) 525 { 526 sceNpCommerce2DestroyGetProductInfoResult(&result); 527 m_errorCode = ret; 528 return ret; 529 } 530 531 // Get rating info 532 ret = sceNpCommerce2GetContentRatingInfoFromGameProductInfo(&productInfo, &ratingInfo); 533 if (ret < 0) 534 { 535 sceNpCommerce2DestroyGetProductInfoResult(&result); 536 m_errorCode = ret; 537 return ret; 538 } 539 540 for (int index = 0; index < ratingInfo.countOfContentRatingDescriptor; index++) 541 { 542 SceNpCommerce2ContentRatingDescriptor desc; 543 sceNpCommerce2GetContentRatingDescriptor(&ratingInfo, index, &desc); 544 ratingDescList.push_back(desc); 545 } 546 547 // populate our temp struct 548 pInfo->ratingDescriptors = ratingDescList; 549 strncpy(pInfo->productId, productInfo.productId, SCE_NP_COMMERCE2_PRODUCT_ID_LEN); 550 strncpy(pInfo->productName, productInfo.productName, SCE_NP_COMMERCE2_PRODUCT_NAME_LEN); 551 strncpy(pInfo->shortDescription, productInfo.productShortDescription, SCE_NP_COMMERCE2_PRODUCT_SHORT_DESCRIPTION_LEN); 552 strncpy(pInfo->longDescription, productInfo.productLongDescription, SCE_NP_COMMERCE2_PRODUCT_LONG_DESCRIPTION_LEN); 553 strncpy(pInfo->legalDescription, productInfo.legalDescription, SCE_NP_COMMERCE2_PRODUCT_LEGAL_DESCRIPTION_LEN); 554 strncpy(pInfo->spName, productInfo.spName, SCE_NP_COMMERCE2_SP_NAME_LEN); 555 strncpy(pInfo->imageUrl, productInfo.imageUrl, SCE_NP_COMMERCE2_URL_LEN); 556 pInfo->releaseDate = productInfo.releaseDate; 557 strncpy(pInfo->ratingSystemId, ratingInfo.ratingSystemId, SCE_NP_COMMERCE2_RATING_SYSTEM_ID_LEN); 558 strncpy(pInfo->ratingImageUrl, ratingInfo.imageUrl, SCE_NP_COMMERCE2_URL_LEN); 559 560 // Get SKU info 561 if (productInfo.countOfSku == 1) 562 { 563 ret = sceNpCommerce2GetGameSkuInfoFromGameProductInfo(&productInfo, 0, &skuInfo); 564 if (ret < 0) 565 { 566 sceNpCommerce2DestroyGetProductInfoResult(&result); 567 m_errorCode = ret; 568 return ret; 569 } 570 strncpy(pInfo->skuId, skuInfo.skuId, SCE_NP_COMMERCE2_SKU_ID_LEN); 571 pInfo->purchasabilityFlag = skuInfo.purchasabilityFlag; 572 573 // Take out the price. Nicely formatted 574 // but also keep the price as a value in case it's 0 - we need to show "free" for that 575 pInfo->ui32Price= skuInfo.price; 576 ret = sceNpCommerce2GetPrice(m_contextId, pInfo->price, sizeof(pInfo->price), skuInfo.price); 577 if (ret < 0) 578 { 579 sceNpCommerce2DestroyGetProductInfoResult(&result); 580 m_errorCode = ret; 581 return ret; 582 } 583 } 584 585 // Set our result 586 //*info = tempInfo; 587 588 ret = sceNpCommerce2DestroyGetProductInfoResult(&result); 589 if (ret < 0) 590 { 591 return ret; 592 } 593 594 return ret; 595} 596 597 598int SonyCommerce::addDetailedProductInfo(ProductInfo *info, const char *productId, char *categoryId) 599{ 600 int ret = 0; 601 uint32_t requestId; 602 size_t bufSize = sizeof(m_commercebuffer); 603 size_t fillSize = 0; 604 std::list<SceNpCommerce2ContentRatingDescriptor> ratingDescList; 605 SceNpCommerce2GetProductInfoResult result; 606 SceNpCommerce2ContentRatingInfo ratingInfo; 607 SceNpCommerce2GameProductInfo productInfo; 608 SceNpCommerce2GameSkuInfo skuInfo; 609 //ProductInfoDetailed tempInfo; 610 611 if (!m_contextCreated) 612 { 613 ret = createContext(); 614 if (ret < 0) 615 { 616 m_errorCode = ret; 617 return ret; 618 } 619 } 620 621 // Obtain product data 622 ret = sceNpCommerce2GetProductInfoCreateReq(m_contextId, &requestId); 623 if (ret < 0) 624 { 625 m_errorCode = ret; 626 return ret; 627 } 628 629 if (categoryId && categoryId[0] != 0) 630 { 631 ret = sceNpCommerce2GetProductInfoStart(requestId, categoryId, productId); 632 } 633 else 634 { 635 ret = sceNpCommerce2GetProductInfoStart(requestId, categoryId, productId); 636 } 637 if (ret < 0) { 638 sceNpCommerce2DestroyReq(requestId); 639 m_errorCode = ret; 640 return ret; 641 } 642 643 ret = sceNpCommerce2GetProductInfoGetResult(requestId, m_commercebuffer, bufSize, &fillSize); 644 if (ret < 0) 645 { 646 sceNpCommerce2DestroyReq(requestId); 647 m_errorCode = ret; 648 return ret; 649 } 650 651 ret = sceNpCommerce2DestroyReq(requestId); 652 if (ret < 0) 653 { 654 m_errorCode = ret; 655 return ret; 656 } 657 658 // Take Out Game Product Information 659 ret = sceNpCommerce2InitGetProductInfoResult(&result, m_commercebuffer, fillSize); 660 if (ret < 0) 661 { 662 m_errorCode = ret; 663 return ret; 664 } 665 666 ret = sceNpCommerce2GetGameProductInfo(&result, &productInfo); 667 if (ret < 0) 668 { 669 sceNpCommerce2DestroyGetProductInfoResult(&result); 670 m_errorCode = ret; 671 return ret; 672 } 673 674 // Get rating info 675 ret = sceNpCommerce2GetContentRatingInfoFromGameProductInfo(&productInfo, &ratingInfo); 676 if (ret < 0) 677 { 678 sceNpCommerce2DestroyGetProductInfoResult(&result); 679 m_errorCode = ret; 680 return ret; 681 } 682 683 for (int index = 0; index < ratingInfo.countOfContentRatingDescriptor; index++) 684 { 685 SceNpCommerce2ContentRatingDescriptor desc; 686 sceNpCommerce2GetContentRatingDescriptor(&ratingInfo, index, &desc); 687 ratingDescList.push_back(desc); 688 } 689 690 // populate our temp struct 691// tempInfo.ratingDescriptors = ratingDescList; 692// strncpy(tempInfo.productId, productInfo.productId, SCE_NP_COMMERCE2_PRODUCT_ID_LEN); 693// strncpy(tempInfo.productName, productInfo.productName, SCE_NP_COMMERCE2_PRODUCT_NAME_LEN); 694// strncpy(tempInfo.shortDescription, productInfo.productShortDescription, SCE_NP_COMMERCE2_PRODUCT_SHORT_DESCRIPTION_LEN); 695 strncpy(info->longDescription, productInfo.productLongDescription, SCE_NP_COMMERCE2_PRODUCT_LONG_DESCRIPTION_LEN); 696// strncpy(tempInfo.legalDescription, productInfo.legalDescription, SCE_NP_COMMERCE2_PRODUCT_LEGAL_DESCRIPTION_LEN); 697// strncpy(tempInfo.spName, productInfo.spName, SCE_NP_COMMERCE2_SP_NAME_LEN); 698// strncpy(tempInfo.imageUrl, productInfo.imageUrl, SCE_NP_COMMERCE2_URL_LEN); 699// tempInfo.releaseDate = productInfo.releaseDate; 700// strncpy(tempInfo.ratingSystemId, ratingInfo.ratingSystemId, SCE_NP_COMMERCE2_RATING_SYSTEM_ID_LEN); 701// strncpy(tempInfo.ratingImageUrl, ratingInfo.imageUrl, SCE_NP_COMMERCE2_URL_LEN); 702 703 // Get SKU info 704 if (productInfo.countOfSku == 1) 705 { 706 ret = sceNpCommerce2GetGameSkuInfoFromGameProductInfo(&productInfo, 0, &skuInfo); 707 if (ret < 0) 708 { 709 sceNpCommerce2DestroyGetProductInfoResult(&result); 710 m_errorCode = ret; 711 return ret; 712 } 713 strncpy(info->skuId, skuInfo.skuId, SCE_NP_COMMERCE2_SKU_ID_LEN); 714 info->purchasabilityFlag = skuInfo.purchasabilityFlag; 715 info->annotation = skuInfo.annotation; 716 717 // Take out the price. Nicely formatted 718 // but also keep the price as a value in case it's 0 - we need to show "free" for that 719 info->ui32Price= skuInfo.price; 720 ret = sceNpCommerce2GetPrice(m_contextId, info->price, sizeof(info->price), skuInfo.price); 721 if (ret < 0) 722 { 723 sceNpCommerce2DestroyGetProductInfoResult(&result); 724 m_errorCode = ret; 725 return ret; 726 } 727 } 728 else 729 { 730 // 4J-PB - more than one sku id! We have to be able to use the sku id returned for a product, so there is not supposed to be more than 1 731 app.DebugPrintf("MORE THAN 1 SKU ID FOR %s\n",info->productName); 732 } 733 734 // Set our result 735 //*info = tempInfo; 736 737 ret = sceNpCommerce2DestroyGetProductInfoResult(&result); 738 if (ret < 0) 739 { 740 return ret; 741 } 742 743 return ret; 744} 745 746 747int SonyCommerce::checkout(CheckoutInputParams &params) 748{ 749 int ret = 0; 750 const char *skuIdsTemp[SCE_NP_COMMERCE2_SKU_CHECKOUT_MAX]; 751 std::list<const char *>::iterator iter = params.skuIds.begin(); 752 std::list<const char *>::iterator iterEnd = params.skuIds.end(); 753 754 if (!m_contextCreated) { 755 ret = createContext(); 756 if (ret < 0) { 757 return ret; 758 } 759 } 760 761 for (int i = 0; i < params.skuIds.size(); i++) { 762 skuIdsTemp[i] = (const char *)(*iter); 763 iter++; 764 } 765 766 ret = sceNpCommerce2DoCheckoutStartAsync(m_contextId, skuIdsTemp, params.skuIds.size(), *params.memContainer); 767 if (ret < 0) { 768 return ret; 769 } 770 771 return CELL_OK; 772} 773 774 775int SonyCommerce::downloadList(DownloadListInputParams &params) 776{ 777 int ret = 0; 778 const char *skuIdsTemp[SCE_NP_COMMERCE2_SKU_CHECKOUT_MAX]; 779 std::list<const char *>::iterator iter = params.skuIds.begin(); 780 std::list<const char *>::iterator iterEnd = params.skuIds.end(); 781 782 if (!m_contextCreated) { 783 ret = createContext(); 784 if (ret < 0) { 785 return ret; 786 } 787 } 788 789 for (int i = 0; i < params.skuIds.size(); i++) { 790 skuIdsTemp[i] = (const char *)(*iter); 791 iter++; 792 } 793 ret = sceNpCommerce2DoDlListStartAsync(m_contextId, app.GetCommerceCategory(), skuIdsTemp, params.skuIds.size(), *params.memContainer); 794 if (ret < 0) { 795 return ret; 796 } 797 798 return CELL_OK; 799} 800 801void SonyCommerce::UpgradeTrialCallback2(LPVOID lpParam,int err) 802{ 803 app.DebugPrintf(4,"SonyCommerce_UpgradeTrialCallback2 : err : 0x%08x\n", err); 804 SonyCommerce::CheckForTrialUpgradeKey(); 805 if(err != CELL_OK) 806 { 807 UINT uiIDA[1]; 808 uiIDA[0]=IDS_CONFIRM_OK; 809 C4JStorage::EMessageResult result = ui.RequestMessageBox( IDS_PRO_UNLOCKGAME_TITLE, IDS_NO_DLCOFFERS, uiIDA,1,ProfileManager.GetPrimaryPad()); 810 } 811 m_trialUpgradeCallbackFunc(m_trialUpgradeCallbackParam, m_errorCode); 812} 813 814void SonyCommerce::UpgradeTrialCallback1(LPVOID lpParam,int err) 815{ 816 817 app.DebugPrintf(4,"SonyCommerce_UpgradeTrialCallback1 : err : 0x%08x\n", err); 818 if(err == CELL_OK) 819 { 820 const char* skuID = s_trialUpgradeProductInfoDetailed.skuId; 821 if(s_trialUpgradeProductInfoDetailed.purchasabilityFlag == SCE_NP_COMMERCE2_SKU_PURCHASABILITY_FLAG_OFF) 822 { 823 app.DebugPrintf(4,"UpgradeTrialCallback1 - DownloadAlreadyPurchased\n"); 824 SonyCommerce::DownloadAlreadyPurchased( UpgradeTrialCallback2, NULL, skuID); 825 } 826 else 827 { 828 app.DebugPrintf(4,"UpgradeTrialCallback1 - Checkout\n"); 829 SonyCommerce::Checkout( UpgradeTrialCallback2, NULL, skuID); 830 } 831 } 832 else 833 { 834 UINT uiIDA[1]; 835 uiIDA[0]=IDS_CONFIRM_OK; 836 C4JStorage::EMessageResult result = ui.RequestMessageBox( IDS_PRO_UNLOCKGAME_TITLE, IDS_NO_DLCOFFERS, uiIDA,1,ProfileManager.GetPrimaryPad()); 837 m_trialUpgradeCallbackFunc(m_trialUpgradeCallbackParam, m_errorCode); 838 } 839} 840 841 842 843// global func, so we can call from the profile lib 844void SonyCommerce_UpgradeTrial() 845{ 846 // we're now calling the app function here, which manages pending requests 847 app.UpgradeTrial(); 848} 849 850void SonyCommerce::UpgradeTrial(CallbackFunc cb, LPVOID lpParam) 851{ 852 m_trialUpgradeCallbackFunc = cb; 853 m_trialUpgradeCallbackParam = lpParam; 854 855// static char szTrialUpgradeSkuID[64]; 856// sprintf(szTrialUpgradeSkuID, "%s-TRIALUPGRADE0001", app.GetCommerceCategory());//, szSKUSuffix); 857 GetDetailedProductInfo(UpgradeTrialCallback1, NULL, &s_trialUpgradeProductInfoDetailed, app.GetUpgradeKey(), app.GetCommerceCategory()); 858} 859 860 861int SonyCommerce::createContext() 862{ 863 SceNpId npId; 864 int ret = sceNpManagerGetNpId(&npId); 865 if(ret < 0) 866 { 867 app.DebugPrintf(4,"createContext sceNpManagerGetNpId problem\n"); 868 return ret; 869 } 870 871 if (m_contextCreated) { 872 ret = sceNpCommerce2DestroyCtx(m_contextId); 873 if (ret < 0) 874 { 875 app.DebugPrintf(4,"createContext sceNpCommerce2DestroyCtx problem\n"); 876 return ret; 877 } 878 } 879 880 // Create commerce2 context 881 ret = sceNpCommerce2CreateCtx(SCE_NP_COMMERCE2_VERSION, &npId, commerce2Handler, NULL, &m_contextId); 882 if (ret < 0) 883 { 884 app.DebugPrintf(4,"createContext sceNpCommerce2CreateCtx problem\n"); 885 return ret; 886 } 887 888 m_contextCreated = true; 889 890 return CELL_OK; 891} 892 893int SonyCommerce::createSession() 894{ 895 int ret = createContext(); 896 if (ret < 0) { 897 return ret; 898 } 899 900 m_currentPhase = e_phase_creatingSessionPhase; 901 ret = sceNpCommerce2CreateSessionStart(m_contextId); 902 if (ret < 0) { 903 return ret; 904 } 905 return ret; 906} 907 908 909void SonyCommerce::commerce2Handler(uint32_t contextId, uint32_t subjectId, int event, int errorCode, void *arg) 910{ 911// Event reply; 912// reply.service = Toolkit::NP::commerce; 913// 914 EnterCriticalSection(&m_queueLock); 915 916 switch (event) { 917 case SCE_NP_COMMERCE2_EVENT_REQUEST_ERROR: 918 { 919 m_messageQueue.push(e_message_commerceEnd); 920 m_errorCode = errorCode; 921 break; 922 } 923 case SCE_NP_COMMERCE2_EVENT_CREATE_SESSION_DONE: 924 { 925 m_messageQueue.push(e_message_commerceEnd); 926 m_event = e_event_commerceSessionCreated; 927 break; 928 } 929 case SCE_NP_COMMERCE2_EVENT_CREATE_SESSION_ABORT: 930 { 931 m_messageQueue.push(e_message_commerceEnd); 932 m_event = e_event_commerceSessionAborted; 933 break; 934 } 935 case SCE_NP_COMMERCE2_EVENT_DO_CHECKOUT_STARTED: 936 { 937 m_currentPhase = e_phase_checkoutPhase; 938 m_event = e_event_commerceCheckoutStarted; 939 break; 940 } 941 case SCE_NP_COMMERCE2_EVENT_DO_CHECKOUT_SUCCESS: 942 { 943 m_messageQueue.push(e_message_commerceEnd); 944 m_event = e_event_commerceCheckoutSuccess; 945 break; 946 } 947 case SCE_NP_COMMERCE2_EVENT_DO_CHECKOUT_BACK: 948 { 949 m_messageQueue.push(e_message_commerceEnd); 950 m_event = e_event_commerceCheckoutAborted; 951 break; 952 } 953 case SCE_NP_COMMERCE2_EVENT_DO_CHECKOUT_FINISHED: 954 { 955 m_event = e_event_commerceCheckoutFinished; 956 break; 957 } 958 case SCE_NP_COMMERCE2_EVENT_DO_DL_LIST_STARTED: 959 { 960 m_currentPhase = e_phase_downloadListPhase; 961 m_event = e_event_commerceDownloadListStarted; 962 break; 963 } 964 case SCE_NP_COMMERCE2_EVENT_DO_DL_LIST_SUCCESS: 965 { 966 m_messageQueue.push(e_message_commerceEnd); 967 m_event = e_event_commerceDownloadListSuccess; 968 break; 969 } 970 case SCE_NP_COMMERCE2_EVENT_DO_DL_LIST_FINISHED: 971 { 972 m_event = e_event_commerceDownloadListFinished; 973 break; 974 } 975 case SCE_NP_COMMERCE2_EVENT_DO_PROD_BROWSE_STARTED: 976 m_currentPhase = e_phase_productBrowsePhase; 977 m_event = e_event_commerceProductBrowseStarted; 978 break; 979 case SCE_NP_COMMERCE2_EVENT_DO_PROD_BROWSE_SUCCESS: 980 { 981 m_messageQueue.push(e_message_commerceEnd); 982 m_event = e_event_commerceProductBrowseSuccess; 983 break; 984 } 985 case SCE_NP_COMMERCE2_EVENT_DO_PROD_BROWSE_BACK: 986 { 987 m_messageQueue.push(e_message_commerceEnd); 988 m_event = e_event_commerceProductBrowseAborted; 989 break; 990 } 991 case SCE_NP_COMMERCE2_EVENT_DO_PROD_BROWSE_FINISHED: 992 { 993 m_event = e_event_commerceProductBrowseFinished; 994 break; 995 } 996 case SCE_NP_COMMERCE2_EVENT_DO_PROD_BROWSE_OPENED: 997 break; 998 case SCE_NP_COMMERCE2_EVENT_DO_PRODUCT_CODE_STARTED: 999 { 1000 m_currentPhase = e_phase_voucherRedeemPhase; 1001 m_event = e_event_commerceVoucherInputStarted; 1002 break; 1003 } 1004 case SCE_NP_COMMERCE2_EVENT_DO_PRODUCT_CODE_SUCCESS: 1005 { 1006 m_messageQueue.push(e_message_commerceEnd); 1007 m_event = e_event_commerceVoucherInputSuccess; 1008 break; 1009 } 1010 case SCE_NP_COMMERCE2_EVENT_DO_PRODUCT_CODE_BACK: 1011 { 1012 m_messageQueue.push(e_message_commerceEnd); 1013 m_event = e_event_commerceVoucherInputAborted; 1014 break; 1015 } 1016 case SCE_NP_COMMERCE2_EVENT_DO_PRODUCT_CODE_FINISHED: 1017 { 1018 m_event = e_event_commerceVoucherInputFinished; 1019 break; 1020 } 1021 default: 1022 break; 1023 }; 1024 1025 LeaveCriticalSection(&m_queueLock); 1026} 1027 1028 1029 1030void SonyCommerce::processMessage() 1031{ 1032 EnterCriticalSection(&m_queueLock); 1033 int ret; 1034 if(m_messageQueue.empty()) 1035 { 1036 LeaveCriticalSection(&m_queueLock); 1037 return; 1038 } 1039 Message msg = m_messageQueue.front(); 1040 m_messageQueue.pop(); 1041 1042 switch (msg) 1043 { 1044 1045 case e_message_commerceCreateSession: 1046 ret = createSession(); 1047 if (ret < 0) 1048 { 1049 m_event = e_event_commerceError; 1050 m_errorCode = ret; 1051 } 1052 break; 1053 1054 case e_message_commerceGetCategoryInfo: 1055 { 1056 ret = getCategoryInfo(m_pCategoryInfo, m_pCategoryID); 1057 if (ret < 0) 1058 { 1059 m_event = e_event_commerceError; 1060 app.DebugPrintf(4,"ERROR - e_event_commerceGotCategoryInfo - %s\n",m_pCategoryID); 1061 m_errorCode = ret; 1062 } 1063 else 1064 { 1065 m_event = e_event_commerceGotCategoryInfo; 1066 app.DebugPrintf(4,"e_event_commerceGotCategoryInfo - %s\n",m_pCategoryID); 1067 } 1068 break; 1069 } 1070 1071 case e_message_commerceGetProductList: 1072 { 1073 ret = getProductList(m_pProductInfoList, m_pCategoryID); 1074 if (ret < 0) 1075 { 1076 m_event = e_event_commerceError; 1077 } 1078 else 1079 { 1080 m_event = e_event_commerceGotProductList; 1081 app.DebugPrintf(4,"e_event_commerceGotProductList - %s\n",m_pCategoryID); 1082 } 1083 break; 1084 } 1085 1086 case e_message_commerceGetDetailedProductInfo: 1087 { 1088 ret = getDetailedProductInfo(m_pProductInfoDetailed, m_pProductID, m_pCategoryID); 1089 if (ret < 0) 1090 { 1091 m_event = e_event_commerceError; 1092 m_errorCode = ret; 1093 } 1094 else 1095 { 1096 m_event = e_event_commerceGotDetailedProductInfo; 1097 app.DebugPrintf(4,"e_event_commerceGotDetailedProductInfo - %s\n",m_pCategoryID); 1098 } 1099 break; 1100 } 1101 case e_message_commerceAddDetailedProductInfo: 1102 { 1103 ret = addDetailedProductInfo(m_pProductInfo, m_pProductID, m_pCategoryID); 1104 if (ret < 0) 1105 { 1106 m_event = e_event_commerceError; 1107 m_errorCode = ret; 1108 } 1109 else 1110 { 1111 m_event = e_event_commerceAddedDetailedProductInfo; 1112 } 1113 break; 1114 } 1115 1116// 1117// case e_message_commerceStoreProductBrowse: 1118// { 1119// ret = productBrowse(*(ProductBrowseParams *)msg.inputArgs); 1120// if (ret < 0) { 1121// m_event = e_event_commerceError; 1122// m_errorCode = ret; 1123// } 1124// _TOOLKIT_NP_DEL (ProductBrowseParams *)msg.inputArgs; 1125// break; 1126// } 1127// 1128// case e_message_commerceUpgradeTrial: 1129// { 1130// ret = upgradeTrial(); 1131// if (ret < 0) { 1132// m_event = e_event_commerceError; 1133// m_errorCode = ret; 1134// } 1135// break; 1136// } 1137// 1138// case e_message_commerceRedeemVoucher: 1139// { 1140// ret = voucherCodeInput(*(VoucherInputParams *)msg.inputArgs); 1141// if (ret < 0) { 1142// m_event = e_event_commerceError; 1143// m_errorCode = ret; 1144// } 1145// _TOOLKIT_NP_DEL (VoucherInputParams *)msg.inputArgs; 1146// break; 1147// } 1148// 1149// case e_message_commerceGetEntitlementList: 1150// { 1151// Job<std::vector<SceNpEntitlement> > tmpJob(static_cast<Future<std::vector<SceNpEntitlement> > *>(msg.output)); 1152// 1153// int state = 0; 1154// int ret = sceNpManagerGetStatus(&state); 1155// 1156// // We don't want to process this if we are offline 1157// if (ret < 0 || state != SCE_NP_MANAGER_STATUS_ONLINE) { 1158// m_event = e_event_commerceError; 1159// reply.returnCode = SCE_TOOLKIT_NP_OFFLINE; 1160// tmpJob.setError(SCE_TOOLKIT_NP_OFFLINE); 1161// } else { 1162// getEntitlementList(&tmpJob); 1163// } 1164// break; 1165// } 1166// 1167// case e_message_commerceConsumeEntitlement: 1168// { 1169// int state = 0; 1170// int ret = sceNpManagerGetStatus(&state); 1171// 1172// // We don't want to process this if we are offline 1173// if (ret < 0 || state != SCE_NP_MANAGER_STATUS_ONLINE) { 1174// m_event = e_event_commerceError; 1175// reply.returnCode = SCE_TOOLKIT_NP_OFFLINE; 1176// } else { 1177// 1178// ret = consumeEntitlement(*(EntitlementToConsume *)msg.inputArgs); 1179// if (ret < 0) { 1180// m_event = e_event_commerceError; 1181// m_errorCode = ret; 1182// } else { 1183// m_event = e_event_commerceConsumedEntitlement; 1184// } 1185// } 1186// _TOOLKIT_NP_DEL (EntitlementToConsume *)msg.inputArgs; 1187// 1188// break; 1189// } 1190// 1191 case e_message_commerceCheckout: 1192 { 1193 ret = checkout(m_checkoutInputParams); 1194 if (ret < 0) { 1195 m_event = e_event_commerceError; 1196 m_errorCode = ret; 1197 } 1198 break; 1199 } 1200 1201 case e_message_commerceDownloadList: 1202 { 1203 ret = downloadList(m_downloadInputParams); 1204 if (ret < 0) { 1205 m_event = e_event_commerceError; 1206 m_errorCode = ret; 1207 } 1208 break; 1209 } 1210 1211 case e_message_commerceEnd: 1212 app.DebugPrintf("XXX - e_message_commerceEnd!\n"); 1213 ret = commerceEnd(); 1214 if (ret < 0) 1215 { 1216 m_event = e_event_commerceError; 1217 m_errorCode = ret; 1218 } 1219 // 4J-PB - we don't seem to handle the error code here 1220 else if(m_errorCode!=0) 1221 { 1222 m_event = e_event_commerceError; 1223 } 1224 break; 1225 1226 default: 1227 break; 1228 } 1229 1230 LeaveCriticalSection(&m_queueLock); 1231} 1232 1233 1234void SonyCommerce::processEvent() 1235{ 1236 int ret = 0; 1237 1238 switch (m_event) 1239 { 1240 case e_event_none: 1241 break; 1242 case e_event_commerceSessionCreated: 1243 app.DebugPrintf(4,"Commerce Session Created.\n"); 1244 runCallback(); 1245 break; 1246 case e_event_commerceSessionAborted: 1247 app.DebugPrintf(4,"Commerce Session aborted.\n"); 1248 runCallback(); 1249 break; 1250 case e_event_commerceGotProductList: 1251 app.DebugPrintf(4,"Got product list.\n"); 1252 runCallback(); 1253 break; 1254 case e_event_commerceGotCategoryInfo: 1255 app.DebugPrintf(4,"Got category info\n"); 1256 runCallback(); 1257 break; 1258 case e_event_commerceGotDetailedProductInfo: 1259 app.DebugPrintf(4,"Got detailed product info.\n"); 1260 runCallback(); 1261 break; 1262 case e_event_commerceAddedDetailedProductInfo: 1263 app.DebugPrintf(4,"Added detailed product info.\n"); 1264 runCallback(); 1265 break; 1266 case e_event_commerceProductBrowseStarted: 1267 break; 1268 case e_event_commerceProductBrowseSuccess: 1269 break; 1270 case e_event_commerceProductBrowseAborted: 1271 break; 1272 case e_event_commerceProductBrowseFinished: 1273 assert(0); 1274// ret = sys_memory_container_destroy(s_memContainer); 1275// if (ret < 0) { 1276// printf("Failed to destroy memory container"); 1277// } 1278// s_memContainer = SYS_MEMORY_CONTAINER_ID_INVALID; 1279 break; 1280 case e_event_commerceVoucherInputStarted: 1281 break; 1282 case e_event_commerceVoucherInputSuccess: 1283 break; 1284 case e_event_commerceVoucherInputAborted: 1285 break; 1286 case e_event_commerceVoucherInputFinished: 1287 assert(0); 1288// ret = sys_memory_container_destroy(s_memContainer); 1289// if (ret < 0) { 1290// printf("Failed to destroy memory container"); 1291// } 1292// s_memContainer = SYS_MEMORY_CONTAINER_ID_INVALID; 1293 break; 1294 case e_event_commerceGotEntitlementList: 1295 break; 1296 case e_event_commerceConsumedEntitlement: 1297 break; 1298 case e_event_commerceCheckoutStarted: 1299 app.DebugPrintf(4,"Checkout Started\n"); 1300 break; 1301 case e_event_commerceCheckoutSuccess: 1302 app.DebugPrintf(4,"Checkout succeeded: 0x%x\n", m_errorCode); 1303 // clear the DLC installed and check again 1304 app.ClearDLCInstalled(); 1305 ui.HandleDLCInstalled(0); 1306 break; 1307 case e_event_commerceCheckoutAborted: 1308 app.DebugPrintf(4,"Checkout aborted: 0x%x\n", m_errorCode); 1309 break; 1310 case e_event_commerceCheckoutFinished: 1311 app.DebugPrintf(4,"Checkout Finished: 0x%x\n", m_errorCode); 1312 ret = sys_memory_container_destroy(m_memContainer); 1313 if (ret < 0) { 1314 app.DebugPrintf(4,"Failed to destroy memory container"); 1315 } 1316 1317 m_memContainer = SYS_MEMORY_CONTAINER_ID_INVALID; 1318 // 4J-PB - if there's been an error - like dlc already purchased, the runcallback has already happened, and will crash this time 1319 if(m_callbackFunc!=NULL) 1320 { 1321 runCallback(); 1322 } 1323 break; 1324 case e_event_commerceDownloadListStarted: 1325 app.DebugPrintf(4,"Download List Started\n"); 1326 break; 1327 case e_event_commerceDownloadListSuccess: 1328 app.DebugPrintf(4,"Download succeeded: 0x%x\n", m_errorCode); 1329 break; 1330 case e_event_commerceDownloadListFinished: 1331 app.DebugPrintf(4,"Download Finished: 0x%x\n", m_errorCode); 1332 ret = sys_memory_container_destroy(m_memContainer); 1333 if (ret < 0) { 1334 app.DebugPrintf(4,"Failed to destroy memory container"); 1335 } 1336 1337 m_memContainer = SYS_MEMORY_CONTAINER_ID_INVALID; 1338 // 4J-PB - if there's been an error - like dlc already purchased, the runcallback has already happened, and will crash this time 1339 if(m_callbackFunc!=NULL) 1340 { 1341 runCallback(); 1342 } 1343 break; 1344 case e_event_commerceError: 1345 app.DebugPrintf(4,"Commerce Error 0x%x\n", m_errorCode); 1346 1347 if(m_memContainer != SYS_MEMORY_CONTAINER_ID_INVALID) 1348 { 1349 ret = sys_memory_container_destroy(m_memContainer); 1350 if (ret < 0) { 1351 app.DebugPrintf(4,"Failed to destroy memory container"); 1352 } 1353 1354 m_memContainer = SYS_MEMORY_CONTAINER_ID_INVALID; 1355 } 1356 1357 runCallback(); 1358 break; 1359 default: 1360 break; 1361 } 1362 m_event = e_event_none; 1363} 1364 1365 1366int SonyCommerce::commerceEnd() 1367{ 1368 int ret = 0; 1369 1370 if (m_currentPhase == e_phase_voucherRedeemPhase) 1371 ret = sceNpCommerce2DoProductCodeFinishAsync(m_contextId); 1372 else if (m_currentPhase == e_phase_productBrowsePhase) 1373 ret = sceNpCommerce2DoProductBrowseFinishAsync(m_contextId); 1374 else if (m_currentPhase == e_phase_creatingSessionPhase) 1375 ret = sceNpCommerce2CreateSessionFinish(m_contextId, &m_sessionInfo); 1376 else if (m_currentPhase == e_phase_checkoutPhase) 1377 ret = sceNpCommerce2DoCheckoutFinishAsync(m_contextId); 1378 else if (m_currentPhase == e_phase_downloadListPhase) 1379 ret = sceNpCommerce2DoDlListFinishAsync(m_contextId); 1380 1381 m_currentPhase = e_phase_idle; 1382 1383 return ret; 1384} 1385 1386void SonyCommerce::CreateSession( CallbackFunc cb, LPVOID lpParam ) 1387{ 1388 Init(); 1389 EnterCriticalSection(&m_queueLock); 1390 setCallback(cb,lpParam); 1391 m_messageQueue.push(e_message_commerceCreateSession); 1392 if(m_tickThread == NULL) 1393 m_tickThread = new C4JThread(TickLoop, NULL, "SonyCommerce tick"); 1394 if(m_tickThread->isRunning() == false) 1395 { 1396 m_currentPhase = e_phase_idle; 1397 m_tickThread->Run(); 1398 } 1399 LeaveCriticalSection(&m_queueLock); 1400} 1401 1402void SonyCommerce::CloseSession() 1403{ 1404 assert(m_currentPhase == e_phase_idle); 1405 m_currentPhase = e_phase_stopped; 1406 Shutdown(); 1407} 1408 1409void SonyCommerce::GetProductList( CallbackFunc cb, LPVOID lpParam, std::vector<ProductInfo>* productList, const char *categoryId) 1410{ 1411 EnterCriticalSection(&m_queueLock); 1412 setCallback(cb,lpParam); 1413 m_pProductInfoList = productList; 1414 strcpy(m_pCategoryID,categoryId); 1415 m_messageQueue.push(e_message_commerceGetProductList); 1416 LeaveCriticalSection(&m_queueLock); 1417} 1418 1419void SonyCommerce::GetDetailedProductInfo( CallbackFunc cb, LPVOID lpParam, ProductInfoDetailed* productInfo, const char *productId, const char *categoryId ) 1420{ 1421 EnterCriticalSection(&m_queueLock); 1422 setCallback(cb,lpParam); 1423 m_pProductInfoDetailed = productInfo; 1424 m_pProductID = productId; 1425 strcpy(m_pCategoryID,categoryId); 1426 m_messageQueue.push(e_message_commerceGetDetailedProductInfo); 1427 LeaveCriticalSection(&m_queueLock); 1428} 1429 1430// 4J-PB - fill out the long description and the price for the product 1431void SonyCommerce::AddDetailedProductInfo( CallbackFunc cb, LPVOID lpParam, ProductInfo* productInfo, const char *productId, const char *categoryId ) 1432{ 1433 EnterCriticalSection(&m_queueLock); 1434 setCallback(cb,lpParam); 1435 m_pProductInfo = productInfo; 1436 m_pProductID = productId; 1437 strcpy(m_pCategoryID,categoryId); 1438 m_messageQueue.push(e_message_commerceAddDetailedProductInfo); 1439 LeaveCriticalSection(&m_queueLock); 1440} 1441void SonyCommerce::GetCategoryInfo( CallbackFunc cb, LPVOID lpParam, CategoryInfo *info, const char *categoryId ) 1442{ 1443 EnterCriticalSection(&m_queueLock); 1444 setCallback(cb,lpParam); 1445 m_pCategoryInfo = info; 1446 strcpy(m_pCategoryID,categoryId); 1447 m_messageQueue.push(e_message_commerceGetCategoryInfo); 1448 LeaveCriticalSection(&m_queueLock); 1449} 1450 1451void SonyCommerce::Checkout( CallbackFunc cb, LPVOID lpParam, const char* skuID ) 1452{ 1453 if(m_memContainer != SYS_MEMORY_CONTAINER_ID_INVALID) 1454 { 1455 return; 1456 } 1457 EnterCriticalSection(&m_queueLock); 1458 setCallback(cb,lpParam); 1459 int ret = sys_memory_container_create(&m_memContainer, SCE_NP_COMMERCE2_DO_CHECKOUT_MEMORY_CONTAINER_SIZE); 1460 if (ret < 0) 1461 { 1462 app.DebugPrintf(4,"sys_memory_container_create() failed. ret = 0x%x\n", ret); 1463 } 1464 1465 m_checkoutInputParams.memContainer = &m_memContainer; 1466 m_checkoutInputParams.skuIds.clear(); 1467 m_checkoutInputParams.skuIds.push_back(skuID); 1468 m_messageQueue.push(e_message_commerceCheckout); 1469 LeaveCriticalSection(&m_queueLock); 1470} 1471 1472void SonyCommerce::DownloadAlreadyPurchased( CallbackFunc cb, LPVOID lpParam, const char* skuID ) 1473{ 1474 if(m_memContainer != SYS_MEMORY_CONTAINER_ID_INVALID) 1475 return; 1476 EnterCriticalSection(&m_queueLock); 1477 setCallback(cb,lpParam); 1478 int ret = sys_memory_container_create(&m_memContainer, SCE_NP_COMMERCE2_DO_CHECKOUT_MEMORY_CONTAINER_SIZE); 1479 if (ret < 0) 1480 { 1481 app.DebugPrintf(4,"sys_memory_container_create() failed. ret = 0x%x\n", ret); 1482 } 1483 1484 m_downloadInputParams.memContainer = &m_memContainer; 1485 m_downloadInputParams.skuIds.clear(); 1486 m_downloadInputParams.skuIds.push_back(skuID); 1487 m_messageQueue.push(e_message_commerceDownloadList); 1488 LeaveCriticalSection(&m_queueLock); 1489} 1490 1491 1492