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