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