the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
at master 374 lines 9.9 kB view raw
1#include "stdafx.h" 2 3#include "SonyRemoteStorage_Vita.h" 4#include "SonyHttp_Vita.h" 5#include <stdio.h> 6#include <string> 7#include <stdlib.h> 8// #include <cell/sysmodule.h> 9// #include <cell/http.h> 10// #include <cell/ssl.h> 11// #include <netex/net.h> 12// #include <netex/libnetctl.h> 13// #include <np.h> 14// #include <sysutil/sysutil_common.h> 15// #include <sys/timer.h> 16// #include <sys/paths.h> 17// #include <sysutil\sysutil_savedata.h> 18 19 20 21 22#define AUTH_SCOPE "psn:s2s" 23#define CLIENT_ID "969e9d21-527c-4c22-b539-f8e479f690bc" 24static SceRemoteStorageData s_getDataOutput; 25 26 27void SonyRemoteStorage_Vita::staticInternalCallback(const SceRemoteStorageEvent event, int32_t retCode, void * userData) 28{ 29 ((SonyRemoteStorage_Vita*)userData)->internalCallback(event, retCode); 30} 31 32void SonyRemoteStorage_Vita::internalCallback(const SceRemoteStorageEvent event, int32_t retCode) 33{ 34 m_lastErrorCode = retCode; 35 36 switch(event) 37 { 38 case ERROR_OCCURRED: 39 app.DebugPrintf("An error occurred with retCode: 0x%x \n", retCode); 40 m_status = e_error; 41// shutdown(); // removed, as the remote storage lib now tries to reconnect if an error has occurred 42 runCallback(); 43 m_bTransferStarted = false; 44 break; 45 46 case GET_DATA_RESULT: 47 if(retCode >= 0) 48 { 49 app.DebugPrintf("Get Data success \n"); 50 m_status = e_getDataSucceeded; 51 } 52 else 53 { 54 app.DebugPrintf("An error occurred while Get Data was being processed. retCode: 0x%x \n", retCode); 55 m_status = e_error; 56 } 57 runCallback(); 58 m_bTransferStarted = false; 59 break; 60 61 case GET_DATA_PROGRESS: 62 app.DebugPrintf("Get data progress: %i%%\n", retCode); 63 m_status = e_getDataInProgress; 64 m_dataProgress = retCode; 65 m_startTime = System::currentTimeMillis(); 66 break; 67 68 case GET_STATUS_RESULT: 69 if(retCode >= 0) 70 { 71 app.DebugPrintf("Get Status success \n"); 72 app.DebugPrintf("Remaining Syncs for this user: %llu\n", outputGetStatus->remainingSyncs); 73 app.DebugPrintf("Number of files on the cloud: %d\n", outputGetStatus->numFiles); 74 for(int i = 0; i < outputGetStatus->numFiles; i++) 75 { 76 app.DebugPrintf("\n*** File %d information: ***\n", (i + 1)); 77 app.DebugPrintf("File name: %s \n", outputGetStatus->data[i].fileName); 78 app.DebugPrintf("File description: %s \n", outputGetStatus->data[i].fileDescription); 79 app.DebugPrintf("MD5 Checksum: %s \n", outputGetStatus->data[i].md5Checksum); 80 app.DebugPrintf("Size of the file: %u bytes \n", outputGetStatus->data[i].fileSize); 81 app.DebugPrintf("Timestamp: %s \n", outputGetStatus->data[i].timeStamp); 82 app.DebugPrintf("Visibility: \"%s\" \n", (outputGetStatus->data[i].visibility == 0)?"Private":((outputGetStatus->data[i].visibility == 1)?"Public read only":"Public read and write")); 83 } 84 m_status = e_getStatusSucceeded; 85 } 86 else 87 { 88 app.DebugPrintf("An error occurred while Get Status was being processed. retCode: 0x%x \n", retCode); 89 m_status = e_error; 90 } 91 runCallback(); 92 break; 93 94 case PSN_SIGN_IN_REQUIRED: 95 app.DebugPrintf("User's PSN sign-in through web browser is required \n"); 96 m_status = e_signInRequired; 97 runCallback(); 98 break; 99 100 case SET_DATA_RESULT: 101 if(retCode >= 0) 102 { 103 app.DebugPrintf("Set Data success \n"); 104 m_status = e_setDataSucceeded; 105 } 106 else 107 { 108 app.DebugPrintf("An error occurred while Set Data was being processed. retCode: 0x%x \n", retCode); 109 m_status = e_error; 110 } 111 runCallback(); 112 m_bTransferStarted = false; 113 break; 114 115 case SET_DATA_PROGRESS: 116 app.DebugPrintf("Set data progress: %i%%\n", retCode); 117 m_status = e_setDataInProgress; 118 m_dataProgress = retCode; 119 m_startTime = System::currentTimeMillis(); 120 break; 121 122 case USER_ACCOUNT_LINKED: 123 app.DebugPrintf("User's account has been linked with PSN \n"); 124 m_bInitialised = true; 125 m_status = e_accountLinked; 126 runCallback(); 127 break; 128 129 case WEB_BROWSER_RESULT: 130 app.DebugPrintf("This function is not used on PS Vita, as the account will be linked, it is not needed to open a browser to link it \n"); 131 assert(0); 132 break; 133 134 default: 135 app.DebugPrintf("This should never happen \n"); 136 assert(0); 137 break; 138 139 } 140} 141 142bool SonyRemoteStorage_Vita::init(CallbackFunc cb, LPVOID lpParam) 143{ 144 int ret = 0; 145 int reqId = 0; 146 147 m_callbackFunc = cb; 148 m_callbackParam = lpParam; 149 m_bTransferStarted = false; 150 m_bAborting = false; 151 152 m_lastErrorCode = SCE_OK; 153 154 155 if(m_bInitialised) 156 { 157 internalCallback(USER_ACCOUNT_LINKED, 0); 158 return true; 159 } 160 161 ret = sceNpAuthInit(); 162 if(ret < 0 && ret != SCE_NP_AUTH_ERROR_ALREADY_INITIALIZED) 163 { 164 app.DebugPrintf("sceNpAuthInit failed 0x%x\n", ret); 165 return false; 166 } 167 168 ret = sceNpAuthCreateOAuthRequest(); 169 if (ret < 0) 170 { 171 app.DebugPrintf("Couldn't create auth request 0x%x\n", ret); 172 return false; 173 } 174 175 reqId = ret; 176 177 SceNpClientId clientId; 178 memset(&clientId, 0x0, sizeof(clientId)); 179 180// SceNpAuthorizationCode authCode; 181// memset(&authCode, 0x0, sizeof(authCode)); 182 183 SceNpAuthGetAuthorizationCodeParameter authParams; 184 memset(&authParams, 0x0, sizeof(authParams)); 185 186 authParams.size = sizeof(authParams); 187 authParams.pScope = AUTH_SCOPE; 188 189 memcpy(clientId.id, CLIENT_ID, strlen(CLIENT_ID)); 190 authParams.pClientId = &clientId; 191 192 int issuerId = 0; 193// ret = sceNpAuthGetAuthorizationCode(reqId, &authParams, &authCode, &issuerId); 194// if (ret < 0) 195// { 196// app.DebugPrintf("Failed to get auth code 0x%x\n", ret); 197// sceNpAuthDeleteOAuthRequest(reqId); 198// return false; 199// } 200 201 ret = sceNpAuthDeleteOAuthRequest(reqId); 202 if (ret < 0) 203 { 204 app.DebugPrintf("Couldn't delete auth request 0x%x\n", ret); 205 return false; 206 } 207 208 SceRemoteStorageInitParams params; 209 210 params.callback = SonyRemoteStorage_Vita::staticInternalCallback; 211 params.userData = this; 212 params.thread.threadAffinity = SCE_KERNEL_THREAD_CPU_AFFINITY_MASK_DEFAULT; 213 params.thread.threadPriority = SCE_KERNEL_DEFAULT_PRIORITY_USER; 214// memcpy(params.authCode, authCode.code, SCE_NP_AUTHORIZATION_CODE_MAX_LEN); 215 strcpy(params.clientId, CLIENT_ID); 216 params.timeout.connectMs = 30 * 1000; //30 seconds is the default 217 params.timeout.resolveMs = 30 * 1000; //30 seconds is the default 218 params.timeout.receiveMs = 120 * 1000; //120 seconds is the default 219 params.timeout.sendMs = 120 * 1000; //120 seconds is the default 220 params.pool.memPoolSize = 7 * 1024 * 1024; 221 if(m_memPoolBuffer == NULL) 222 m_memPoolBuffer = malloc(params.pool.memPoolSize); 223 params.pool.memPoolBuffer = m_memPoolBuffer; 224 225 SceRemoteStorageAbortReqParams abortParams; 226 227 ret = sceRemoteStorageInit(params); 228 if(ret >= 0) 229 { 230 abortParams.requestId = ret; 231 app.DebugPrintf("Session will be created \n"); 232 } 233 else if(ret == SCE_REMOTE_STORAGE_ERROR_ALREADY_INITIALISED) 234 { 235 app.DebugPrintf("Session already created \n"); 236 runCallback(); 237 } 238 else 239 { 240 app.DebugPrintf("Error creating session: 0x%x \n", ret); 241 return false; 242 } 243 return true; 244} 245 246 247 248bool SonyRemoteStorage_Vita::getRemoteFileInfo(SceRemoteStorageStatus* pInfo, CallbackFunc cb, LPVOID lpParam) 249{ 250 m_callbackFunc = cb; 251 m_callbackParam = lpParam; 252 outputGetStatus = pInfo; 253 254 SceRemoteStorageStatusReqParams params; 255 reqId = sceRemoteStorageGetStatus(params, outputGetStatus); 256 m_status = e_getStatusInProgress; 257 258 if(reqId >= 0) 259 { 260 app.DebugPrintf("Get Status request sent \n"); 261 return true; 262 } 263 else 264 { 265 app.DebugPrintf("Error sending Get Status request: 0x%x \n", reqId); 266 return false; 267 } 268} 269 270void SonyRemoteStorage_Vita::abort() 271{ 272 m_bAborting = true; 273 app.DebugPrintf("Aborting...\n"); 274 if(m_bTransferStarted) 275 { 276 app.DebugPrintf("transfer has started so we'll call sceRemoteStorageAbort...\n"); 277 278 SceRemoteStorageAbortReqParams params; 279 params.requestId = reqId; 280 int ret = sceRemoteStorageAbort(params); 281 282 if(ret >= 0) 283 { 284 app.DebugPrintf("Abort request done \n"); 285 } 286 else 287 { 288 app.DebugPrintf("Error in Abort request: 0x%x \n", ret); 289 } 290 } 291} 292 293 294 295bool SonyRemoteStorage_Vita::setDataInternal() 296{ 297 // CompressSaveData(); // check if we need to re-save the file compressed first 298 299 snprintf(m_saveFilename, sizeof(m_saveFilename), "%s:%s/GAMEDATA.bin", "savedata0", m_setDataSaveInfo->UTF8SaveFilename); 300 301 SceFiosSize outSize = sceFiosFileGetSizeSync(NULL, m_saveFilename); 302 m_uploadSaveSize = (int)outSize; 303 304 strcpy(m_saveFileDesc, m_setDataSaveInfo->UTF8SaveTitle); 305 m_status = e_setDataInProgress; 306 307 308 SceRemoteStorageSetDataReqParams params; 309 params.visibility = PUBLIC_READ_WRITE; 310 strcpy(params.pathLocation, m_saveFilename); 311 sprintf(params.fileName, getRemoteSaveFilename()); 312 313 GetDescriptionData(params.fileDescription); 314 315 316 if(m_bAborting) 317 { 318 runCallback(); 319 return false; 320 } 321 reqId = sceRemoteStorageSetData(params); 322 323 app.DebugPrintf("\n*******************************\n"); 324 if(reqId >= 0) 325 { 326 app.DebugPrintf("Set Data request sent \n"); 327 m_bTransferStarted = true; 328 return true; 329 } 330 else 331 { 332 app.DebugPrintf("Error sending Set Data request: 0x%x \n", reqId); 333 return false; 334 } 335} 336 337 338bool SonyRemoteStorage_Vita::getData( const char* remotePath, const char* localPath, CallbackFunc cb, LPVOID lpParam ) 339{ 340 m_callbackFunc = cb; 341 m_callbackParam = lpParam; 342 343 SceRemoteStorageGetDataReqParams params; 344 sprintf(params.pathLocation, "savedata0:%s/GAMEDATA.bin", localPath); 345// strcpy(params.pathLocation, localPath); 346 // strcpy(params.fileName, "/test/small.txt"); 347 strcpy(params.fileName, remotePath); 348 memset(&params.psVitaSaveDataSlot, 0, sizeof(params.psVitaSaveDataSlot)); 349 SceRemoteStorageData s_getDataOutput; 350 reqId = sceRemoteStorageGetData(params, &s_getDataOutput); 351 352 app.DebugPrintf("\n*******************************\n"); 353 if(reqId >= 0) 354 { 355 app.DebugPrintf("Get Data request sent \n"); 356 m_bTransferStarted = true; 357 return true; 358 } 359 else 360 { 361 app.DebugPrintf("Error sending Get Data request: 0x%x \n", reqId); 362 return false; 363 } 364} 365 366void SonyRemoteStorage_Vita::runCallback() 367{ 368 assert(m_callbackFunc); 369 if(m_callbackFunc) 370 { 371 m_callbackFunc(m_callbackParam, m_status, m_lastErrorCode); 372 } 373 m_lastErrorCode = SCE_OK; 374}