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 "SonyRemoteStorage_PS3.h"
4#include <stdio.h>
5#include <string>
6#include <stdlib.h>
7// #include <cell/sysmodule.h>
8// #include <cell/http.h>
9// #include <cell/ssl.h>
10// #include <netex/net.h>
11// #include <netex/libnetctl.h>
12// #include <np.h>
13// #include <sysutil/sysutil_common.h>
14// #include <sys/timer.h>
15// #include <sys/paths.h>
16// #include <sysutil\sysutil_savedata.h>
17
18
19
20// 4J-PB - ticketing changed to our SCEE product id
21#define TICKETING_SERVICE_ID "EP4433-NPEB01899_00" //"EP9009-NPWA00114_00"
22#define CLIENT_ID "969e9d21-527c-4c22-b539-f8e479f690bc"
23
24
25
26
27void SonyRemoteStorage_PS3::npauthhandler(int event, int result, void *arg)
28{
29#ifdef __PS3__
30 if (event != SCE_NP_MANAGER_EVENT_GOT_TICKET || result <= 0)
31 {
32 app.DebugPrintf("Could not retrieve ticket: 0x%x\n", result);
33 }
34 else
35 {
36 psnTicketSize = result;
37 psnTicket = malloc(psnTicketSize);
38 if (psnTicket == NULL)
39 {
40 app.DebugPrintf("Failed to allocate for ticket\n");
41 }
42
43 int ret = sceNpManagerGetTicket(psnTicket, &psnTicketSize);
44 if (ret < 0)
45 {
46 app.DebugPrintf("Could not retrieve ticket: 0x%x\n", ret);
47 free(psnTicket);
48 psnTicket = 0;
49 return;
50 }
51 }
52 m_waitingForTicket = false;
53#endif
54}
55
56int SonyRemoteStorage_PS3::initPreconditions()
57{
58 int ret = 0;
59
60 SceNpId npId;
61
62 ret = sceNpManagerGetNpId(&npId);
63 if(ret < 0)
64 {
65 return ret;
66 }
67 SceNpTicketVersion ticketVersion;
68 ticketVersion.major = 3;
69 ticketVersion.minor = 0;
70 ret = sceNpManagerRequestTicket2(&npId, &ticketVersion, TICKETING_SERVICE_ID, NULL, 0, NULL, 0);
71 if(ret < 0)
72 {
73 return ret;
74 }
75 m_waitingForTicket = true;
76 while(m_waitingForTicket)
77 {
78 cellSysutilCheckCallback();
79 sys_timer_usleep(50000); //50 milliseconds.
80 }
81 if(psnTicket == NULL)
82 return -1;
83
84 return 0;
85}
86
87void SonyRemoteStorage_PS3::staticInternalCallback(const SceRemoteStorageEvent event, int32_t retCode, void * userData)
88{
89 ((SonyRemoteStorage_PS3*)userData)->internalCallback(event, retCode);
90}
91
92void SonyRemoteStorage_PS3::internalCallback(const SceRemoteStorageEvent event, int32_t retCode)
93{
94 m_lastErrorCode = retCode;
95
96 switch(event)
97 {
98 case ERROR_OCCURRED:
99 app.DebugPrintf("An error occurred with retCode: 0x%x \n", retCode);
100// shutdown(); // removed, as the remote storage lib now tries to reconnect if an error has occurred
101 m_status = e_error;
102 runCallback();
103 m_bTransferStarted = false;
104 break;
105
106 case GET_DATA_RESULT:
107 if(retCode >= 0)
108 {
109 app.DebugPrintf("Get Data success \n");
110 m_status = e_getDataSucceeded;
111 }
112 else
113 {
114 app.DebugPrintf("An error occurred while Get Data was being processed. retCode: 0x%x \n", retCode);
115 m_status = e_error;
116 }
117 runCallback();
118 m_bTransferStarted = false;
119 break;
120
121 case GET_DATA_PROGRESS:
122 app.DebugPrintf("Get data progress: %i%%\n", retCode);
123 m_status = e_getDataInProgress;
124 m_dataProgress = retCode;
125 m_startTime = System::currentTimeMillis();
126 break;
127
128 case GET_STATUS_RESULT:
129 if(retCode >= 0)
130 {
131 app.DebugPrintf("Get Status success \n");
132 app.DebugPrintf("Remaining Syncs for this user: %llu\n", outputGetStatus->remainingSyncs);
133 app.DebugPrintf("Number of files on the cloud: %d\n", outputGetStatus->numFiles);
134 for(int i = 0; i < outputGetStatus->numFiles; i++)
135 {
136 app.DebugPrintf("\n*** File %d information: ***\n", (i + 1));
137 app.DebugPrintf("File name: %s \n", outputGetStatus->data[i].fileName);
138 app.DebugPrintf("File description: %s \n", outputGetStatus->data[i].fileDescription);
139 app.DebugPrintf("MD5 Checksum: %s \n", outputGetStatus->data[i].md5Checksum);
140 app.DebugPrintf("Size of the file: %u bytes \n", outputGetStatus->data[i].fileSize);
141 app.DebugPrintf("Timestamp: %s \n", outputGetStatus->data[i].timeStamp);
142 app.DebugPrintf("Visibility: \"%s\" \n", (outputGetStatus->data[i].visibility == 0)?"Private":((outputGetStatus->data[i].visibility == 1)?"Public read only":"Public read and write"));
143 }
144 m_status = e_getStatusSucceeded;
145 }
146 else
147 {
148 app.DebugPrintf("An error occurred while Get Status was being processed. retCode: 0x%x \n", retCode);
149 m_status = e_error;
150 }
151 runCallback();
152 break;
153
154 case PSN_SIGN_IN_REQUIRED:
155 app.DebugPrintf("User's PSN sign-in through web browser is required \n");
156 m_status = e_signInRequired;
157 runCallback();
158 break;
159
160 case SET_DATA_RESULT:
161 if(retCode >= 0)
162 {
163 app.DebugPrintf("Set Data success \n");
164 m_status = e_setDataSucceeded;
165 }
166 else
167 {
168 app.DebugPrintf("An error occurred while Set Data was being processed. retCode: 0x%x \n", retCode);
169 m_status = e_error;
170 }
171 runCallback();
172 m_bTransferStarted = false;
173 break;
174
175 case SET_DATA_PROGRESS:
176 app.DebugPrintf("Set data progress: %i%%\n", retCode);
177 m_status = e_setDataInProgress;
178 m_dataProgress = retCode;
179
180 break;
181
182 case USER_ACCOUNT_LINKED:
183 app.DebugPrintf("User's account has been linked with PSN \n");
184 m_bInitialised = true;
185 m_status = e_accountLinked;
186 runCallback();
187 break;
188
189 case WEB_BROWSER_RESULT:
190 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");
191 assert(0);
192 break;
193
194 default:
195 app.DebugPrintf("This should never happen \n");
196 assert(0);
197 break;
198
199 }
200}
201
202bool SonyRemoteStorage_PS3::init(CallbackFunc cb, LPVOID lpParam)
203{
204 m_callbackFunc = cb;
205 m_callbackParam = lpParam;
206 m_bTransferStarted = false;
207 m_bAborting = false;
208
209 if(m_bInitialised)
210 {
211 runCallback();
212 return true;
213 }
214
215
216 if(m_bInitialised)
217 {
218 internalCallback(USER_ACCOUNT_LINKED, 0);
219 return true;
220 }
221
222 int ret = initPreconditions();
223 if(ret < 0)
224 {
225 return false;
226 }
227
228 SceRemoteStorageInitParams params;
229
230 params.callback = staticInternalCallback;
231 params.userData = this;
232 params.thread.threadAffinity = 0; //Not used in PS3
233 params.thread.threadPriority = 1000; //Must be between [0-3071], being 0 the highest.
234 params.psnTicket = psnTicket;
235 params.psnTicketSize = psnTicketSize;
236 strcpy(params.clientId, CLIENT_ID);
237 params.timeout.connectMs = 30 * 1000; //30 seconds is the default
238 params.timeout.resolveMs = 30 * 1000; //30 seconds is the default
239 params.timeout.receiveMs = 120 * 1000; //120 seconds is the default
240 params.timeout.sendMs = 120 * 1000; //120 seconds is the default
241 params.pool.memPoolSize = 7 * 1024 * 1024;
242 if(m_memPoolBuffer == NULL)
243 m_memPoolBuffer = malloc(params.pool.memPoolSize);
244 params.pool.memPoolBuffer = m_memPoolBuffer;
245
246// SceRemoteStorageAbortReqParams abortParams;
247
248 ret = sceRemoteStorageInit(params);
249 if(ret >= 0 || ret == SCE_REMOTE_STORAGE_ERROR_ALREADY_INITIALISED)
250 {
251// abortParams.requestId = ret;
252 //ret = sceRemoteStorageAbort(abortParams);
253 app.DebugPrintf("Session will be created \n");
254 //if(ret >= 0)
255 //{
256 // printf("Session aborted \n");
257 //} else
258 //{
259 // printf("Error aborting session: 0x%x \n", ret);
260 //}
261 }
262 else
263 {
264 app.DebugPrintf("Error creating session: 0x%x \n", ret);
265 return false;
266 }
267 return true;
268}
269
270
271
272bool SonyRemoteStorage_PS3::getRemoteFileInfo(SceRemoteStorageStatus* pInfo, CallbackFunc cb, LPVOID lpParam)
273{
274 m_callbackFunc = cb;
275 m_callbackParam = lpParam;
276 outputGetStatus = pInfo;
277
278 SceRemoteStorageStatusReqParams params;
279 reqId = sceRemoteStorageGetStatus(params, outputGetStatus);
280 m_status = e_getStatusInProgress;
281
282 if(reqId >= 0)
283 {
284 app.DebugPrintf("Get Status request sent \n");
285 return true;
286 }
287 else
288 {
289 app.DebugPrintf("Error sending Get Status request: 0x%x \n", reqId);
290 return false;
291 }
292}
293
294void SonyRemoteStorage_PS3::abort()
295{
296 m_bAborting = true;
297 app.DebugPrintf("Aborting...\n");
298 if(m_bTransferStarted)
299 {
300 app.DebugPrintf("transfer has started so we'll call sceRemoteStorageAbort...\n");
301
302 SceRemoteStorageAbortReqParams params;
303 params.requestId = reqId;
304 int ret = sceRemoteStorageAbort(params);
305
306 if(ret >= 0)
307 {
308 app.DebugPrintf("Abort request done \n");
309 }
310 else
311 {
312 app.DebugPrintf("Error in Abort request: 0x%x \n", ret);
313 }
314 }
315}
316
317
318
319bool SonyRemoteStorage_PS3::setDataInternal()
320{
321 CompressSaveData(); // check if we need to re-save the file compressed first
322
323
324 strcpy(m_saveFilename, m_setDataSaveInfo->UTF8SaveFilename);
325 strcpy(m_saveFileDesc, m_setDataSaveInfo->UTF8SaveTitle);
326
327
328 SceRemoteStorageSetDataReqParams params;
329 params.visibility = PUBLIC_READ_WRITE;
330 strcpy(params.pathLocation, m_saveFilename);
331 sprintf(params.fileName, getRemoteSaveFilename());
332
333 DescriptionData descData;
334 ZeroMemory(&descData, sizeof(DescriptionData));
335 descData.m_platform[0] = SAVE_FILE_PLATFORM_LOCAL & 0xff;
336 descData.m_platform[1] = (SAVE_FILE_PLATFORM_LOCAL >> 8) & 0xff;
337 descData.m_platform[2] = (SAVE_FILE_PLATFORM_LOCAL >> 16) & 0xff;
338 descData.m_platform[3] = (SAVE_FILE_PLATFORM_LOCAL >> 24)& 0xff;
339
340 if(m_thumbnailData)
341 {
342 unsigned int uiHostOptions;
343 bool bHostOptionsRead;
344 DWORD uiTexturePack;
345 char seed[22];
346 app.GetImageTextData(m_thumbnailData, m_thumbnailDataSize,(unsigned char *)seed, uiHostOptions, bHostOptionsRead, uiTexturePack);
347
348 __int64 iSeed = strtoll(seed,NULL,10);
349 char seedHex[17];
350 sprintf(seedHex,"%016llx",iSeed);
351 memcpy(descData.m_seed,seedHex,16); // Don't copy null
352
353 // Save the host options that this world was last played with
354 char hostOptions[9];
355 sprintf(hostOptions,"%08x",uiHostOptions);
356 memcpy(descData.m_hostOptions,hostOptions,8); // Don't copy null
357
358 // Save the texture pack id
359 char texturePack[9];
360 sprintf(texturePack,"%08x",uiTexturePack);
361 memcpy(descData.m_texturePack,texturePack,8); // Don't copy null
362 }
363
364 memcpy(descData.m_saveNameUTF8, m_saveFileDesc, strlen(m_saveFileDesc)+1); // plus null
365 memcpy(params.fileDescription, &descData, sizeof(descData));
366 strcpy(params.ps3DataFilename, "GAMEDATA");
367
368 params.ps3FileType = CELL_SAVEDATA_FILETYPE_NORMALFILE;
369 memcpy(params.secureFileId, m_secureFileId, CELL_SAVEDATA_SECUREFILEID_SIZE);
370
371
372 if(m_bAborting)
373 {
374 runCallback();
375 return false;
376 }
377 reqId = sceRemoteStorageSetData(params);
378
379 app.DebugPrintf("\n*******************************\n");
380 if(reqId >= 0)
381 {
382 app.DebugPrintf("Set Data request sent \n");
383 m_bTransferStarted = true;
384 m_status = e_setDataInProgress;
385 return true;
386 }
387 else
388 {
389 app.DebugPrintf("Error sending Set Data request: 0x%x \n", reqId);
390 return false;
391 }
392}
393
394bool SonyRemoteStorage_PS3::getData( const char* remotePath, const char* localPath, CallbackFunc cb, LPVOID lpParam )
395{
396 m_callbackFunc = cb;
397 m_callbackParam = lpParam;
398
399 SceRemoteStorageGetDataReqParams params;
400 strcpy(params.pathLocation, localPath);//"ABCD12345-RS-DATA");
401 strcpy(params.fileName, remotePath);//"/test/small.txt");
402 strcpy(params.ps3DataFilename, "GAMEDATA");
403 params.ps3FileType = CELL_SAVEDATA_FILETYPE_NORMALFILE;
404 memcpy(params.secureFileId, m_secureFileId, CELL_SAVEDATA_SECUREFILEID_SIZE);
405
406 reqId = sceRemoteStorageGetData(params, &outputGetData);
407
408 app.DebugPrintf("\n*******************************\n");
409 if(reqId >= 0)
410 {
411 app.DebugPrintf("Get Data request sent \n");
412 m_bTransferStarted = true;
413 m_status = e_getDataInProgress;
414 } else
415 {
416 app.DebugPrintf("Error sending Get Data request: 0x%x \n", reqId);
417 }
418
419}
420
421void SonyRemoteStorage_PS3::runCallback()
422{
423 assert(m_callbackFunc);
424 if(m_callbackFunc)
425 {
426 m_callbackFunc(m_callbackParam, m_status, m_lastErrorCode);
427 }
428 m_lastErrorCode = ERROR_SUCCESS;
429}
430
431int SonyRemoteStorage_PS3::SaveCompressCallback(LPVOID lpParam,bool bRes)
432{
433 SonyRemoteStorage_PS3* pRS = (SonyRemoteStorage_PS3*)lpParam;
434 pRS->m_compressedSaveState = e_state_Idle;
435 return 0;
436}
437
438int SonyRemoteStorage_PS3::LoadCompressCallback(void *pParam,bool bIsCorrupt, bool bIsOwner)
439{
440 SonyRemoteStorage_PS3* pRS = (SonyRemoteStorage_PS3*)pParam;
441 int origFilesize = StorageManager.GetSaveSize();
442 void* pOrigSaveData = malloc(origFilesize);
443 unsigned int retFilesize;
444 StorageManager.GetSaveData( pOrigSaveData, &retFilesize );
445 // check if this save file is already compressed
446 if(*((int*)pOrigSaveData) != 0)
447 {
448 app.DebugPrintf("compressing save data\n");
449
450 // Assume that the compression will make it smaller so initially attempt to allocate the current file size
451 // We add 4 bytes to the start so that we can signal compressed data
452 // And another 4 bytes to store the decompressed data size
453 unsigned int compLength = origFilesize+8;
454 byte *compData = (byte *)malloc( compLength );
455 Compression::UseDefaultThreadStorage();
456 Compression::getCompression()->Compress(compData+8,&compLength,pOrigSaveData,origFilesize);
457 ZeroMemory(compData,8);
458 int saveVer = 0;
459 memcpy( compData, &saveVer, sizeof(int) );
460 memcpy( compData+4, &origFilesize, sizeof(int) );
461
462 StorageManager.FreeSaveData();
463 StorageManager.SetSaveData(compData,compLength+8);
464 pRS->m_compressedSaveState = e_state_CompressedSave;
465 }
466 else
467 {
468 // already compressed, do nothing
469 pRS->m_compressedSaveState = e_state_Idle;
470 }
471
472 return 0;
473}
474
475void SonyRemoteStorage_PS3::CompressSaveData()
476{
477 app.DebugPrintf("CompressSaveData\n");
478 m_compressedSaveState = e_state_LoadingSave;
479 app.DebugPrintf("Loading save\n");
480 waitForStorageManagerIdle();
481 C4JStorage::ESaveGameState eLoadStatus=StorageManager.LoadSaveData(m_setDataSaveInfo, &LoadCompressCallback, this);
482 if(eLoadStatus != C4JStorage::ESaveGame_Load)
483 {
484 app.DebugPrintf("Failed to load savegame for compression!!!!!!\n");
485 m_compressedSaveState = e_state_Idle;
486 return;
487 }
488 while(m_compressedSaveState == e_state_LoadingSave)
489 {
490 Sleep(10);
491 }
492 if(m_compressedSaveState == e_state_CompressedSave)
493 {
494
495 waitForStorageManagerIdle();
496 app.DebugPrintf("Saving compressed save\n");
497 waitForStorageManagerIdle();
498 StorageManager.SetDefaultSaveImage(); // we can't get the save image back to overwrite, so set it to the default
499 C4JStorage::ESaveGameState storageState = StorageManager.SaveSaveData( &SaveCompressCallback, this, true ); // only save the data file, so we don't overwrite the icon
500 if(storageState == C4JStorage::ESaveGame_Save)
501 {
502 m_compressedSaveState = e_state_SavingSave;
503 while(m_compressedSaveState == e_state_SavingSave)
504 {
505 Sleep(10);
506 }
507 }
508 else
509 {
510 app.DebugPrintf("StorageManager.SaveSaveData failed, save is left uncompressed\n");
511 }
512 }
513 waitForStorageManagerIdle();
514
515 app.DebugPrintf("done\n");
516 assert(m_compressedSaveState == e_state_Idle);
517}
518