the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
1
2
3#include "stdafx.h"
4#include "SonyRemoteStorage.h"
5
6
7static const char sc_remoteSaveFilename[] = "/minecraft_save/gamedata.rs";
8#ifdef __PSVITA__
9static const char sc_localSaveFilename[] = "CloudSave_Vita.bin";
10static const char sc_localSaveFullPath[] = "savedata0:CloudSave_Vita.bin";
11#elif defined __PS3__
12static const char sc_localSaveFilename[] = "CloudSave_PS3.bin";
13static const char sc_localSaveFullPath[] = "NPEB01899--140720203552";
14#else
15static const char sc_localSaveFilename[] = "CloudSave_Orbis.bin";
16static const char sc_localSaveFullPath[] = "/app0/CloudSave_Orbis.bin";
17#endif
18
19static SceRemoteStorageStatus statParams;
20
21
22
23
24// void remoteStorageGetCallback(LPVOID lpParam, SonyRemoteStorage::Status s, int error_code)
25// {
26// app.DebugPrintf("remoteStorageGetCallback err : 0x%08x\n");
27// }
28//
29// void remoteStorageCallback(LPVOID lpParam, SonyRemoteStorage::Status s, int error_code)
30// {
31// app.DebugPrintf("remoteStorageCallback err : 0x%08x\n");
32//
33// app.getRemoteStorage()->getRemoteFileInfo(&statParams, remoteStorageGetInfoCallback, NULL);
34// }
35
36
37
38
39void SonyRemoteStorage::SetRetrievedDescData()
40{
41 DescriptionData* pDescDataTest = (DescriptionData*)m_remoteFileInfo->fileDescription;
42 ESavePlatform testPlatform = (ESavePlatform)MAKE_FOURCC(pDescDataTest->m_platform[0], pDescDataTest->m_platform[1], pDescDataTest->m_platform[2], pDescDataTest->m_platform[3]);
43 if(testPlatform == SAVE_FILE_PLATFORM_NONE)
44 {
45 // new version of the descData
46 DescriptionData_V2* pDescData2 = (DescriptionData_V2*)m_remoteFileInfo->fileDescription;
47 m_retrievedDescData.m_descDataVersion = GetU32FromHexBytes(pDescData2->m_descDataVersion);
48 m_retrievedDescData.m_savePlatform = (ESavePlatform)MAKE_FOURCC(pDescData2->m_platform[0], pDescData2->m_platform[1], pDescData2->m_platform[2], pDescData2->m_platform[3]);
49 m_retrievedDescData.m_seed = GetU64FromHexBytes(pDescData2->m_seed);
50 m_retrievedDescData.m_hostOptions = GetU32FromHexBytes(pDescData2->m_hostOptions);
51 m_retrievedDescData.m_texturePack = GetU32FromHexBytes(pDescData2->m_texturePack);
52 m_retrievedDescData.m_saveVersion = GetU32FromHexBytes(pDescData2->m_saveVersion);
53 memcpy(m_retrievedDescData.m_saveNameUTF8, pDescData2->m_saveNameUTF8, sizeof(pDescData2->m_saveNameUTF8));
54 assert(m_retrievedDescData.m_descDataVersion > 1 && m_retrievedDescData.m_descDataVersion <= sc_CurrentDescDataVersion);
55 }
56 else
57 {
58 // old version,copy the data across to the new version
59 DescriptionData* pDescData = (DescriptionData*)m_remoteFileInfo->fileDescription;
60 m_retrievedDescData.m_descDataVersion = 1;
61 m_retrievedDescData.m_savePlatform = (ESavePlatform)MAKE_FOURCC(pDescData->m_platform[0], pDescData->m_platform[1], pDescData->m_platform[2], pDescData->m_platform[3]);
62 m_retrievedDescData.m_seed = GetU64FromHexBytes(pDescData->m_seed);
63 m_retrievedDescData.m_hostOptions = GetU32FromHexBytes(pDescData->m_hostOptions);
64 m_retrievedDescData.m_texturePack = GetU32FromHexBytes(pDescData->m_texturePack);
65 m_retrievedDescData.m_saveVersion = SAVE_FILE_VERSION_COMPRESSED_CHUNK_STORAGE; // the last save version before we added it to this data
66 memcpy(m_retrievedDescData.m_saveNameUTF8, pDescData->m_saveNameUTF8, sizeof(pDescData->m_saveNameUTF8));
67 }
68
69}
70
71
72
73
74void getSaveInfoReturnCallback(LPVOID lpParam, SonyRemoteStorage::Status s, int error_code)
75{
76 SonyRemoteStorage* pRemoteStorage = (SonyRemoteStorage*)lpParam;
77 app.DebugPrintf("remoteStorageGetInfoCallback err : 0x%08x\n", error_code);
78 if(error_code == 0)
79 {
80 for(int i=0;i<statParams.numFiles;i++)
81 {
82 if(strcmp(statParams.data[i].fileName, sc_remoteSaveFilename) == 0)
83 {
84 // found the file we need in the cloud
85 pRemoteStorage->m_remoteFileInfo = &statParams.data[i];
86 pRemoteStorage->SetRetrievedDescData();
87 pRemoteStorage->m_getInfoStatus = SonyRemoteStorage::e_infoFound;
88 }
89 }
90 }
91 if(pRemoteStorage->m_getInfoStatus != SonyRemoteStorage::e_infoFound)
92 pRemoteStorage->m_getInfoStatus = SonyRemoteStorage::e_noInfoFound;
93}
94
95
96
97
98
99
100static void getSaveInfoInitCallback(LPVOID lpParam, SonyRemoteStorage::Status s, int error_code)
101{
102 SonyRemoteStorage* pRemoteStorage = (SonyRemoteStorage*)lpParam;
103 if(error_code != 0)
104 {
105 app.DebugPrintf("getSaveInfoInitCallback err : 0x%08x\n", error_code);
106 pRemoteStorage->m_getInfoStatus = SonyRemoteStorage::e_noInfoFound;
107 }
108 else
109 {
110 app.DebugPrintf("getSaveInfoInitCallback calling getRemoteFileInfo\n");
111 app.getRemoteStorage()->getRemoteFileInfo(&statParams, getSaveInfoReturnCallback, pRemoteStorage);
112 }
113}
114
115void SonyRemoteStorage::getSaveInfo()
116{
117 if(m_getInfoStatus == e_gettingInfo)
118 {
119 app.DebugPrintf("SonyRemoteStorage::getSaveInfo already running!!!\n");
120 return;
121 }
122
123 m_getInfoStatus = e_gettingInfo;
124 if(!ProfileManager.IsSignedInLive(ProfileManager.GetPrimaryPad()))
125 {
126 m_getInfoStatus = e_noInfoFound;
127 return;
128 }
129 app.DebugPrintf("SonyRemoteStorage::getSaveInfo calling init\n");
130
131 bool bOK = init(getSaveInfoInitCallback, this);
132 if(!bOK)
133 m_getInfoStatus = e_noInfoFound;
134}
135
136bool SonyRemoteStorage::getSaveData( const char* localDirname, CallbackFunc cb, LPVOID lpParam )
137{
138 m_startTime = System::currentTimeMillis();
139 m_dataProgress = -1;
140 return getData(sc_remoteSaveFilename, localDirname, cb, lpParam);
141}
142
143
144static void setSaveDataInitCallback(LPVOID lpParam, SonyRemoteStorage::Status s, int error_code)
145{
146 SonyRemoteStorage* pRemoteStorage = (SonyRemoteStorage*)lpParam;
147 if(error_code != 0)
148 {
149 app.DebugPrintf("setSaveDataInitCallback err : 0x%08x\n", error_code);
150 pRemoteStorage->m_setDataStatus = SonyRemoteStorage::e_settingDataFailed;
151 if(pRemoteStorage->m_initCallbackFunc)
152 pRemoteStorage->m_initCallbackFunc(pRemoteStorage->m_initCallbackParam, s, error_code);
153 }
154 else
155 {
156 app.getRemoteStorage()->setData(pRemoteStorage->m_setSaveDataInfo, pRemoteStorage->m_initCallbackFunc, pRemoteStorage->m_initCallbackParam);
157 }
158
159}
160bool SonyRemoteStorage::setSaveData(PSAVE_INFO info, CallbackFunc cb, void* lpParam)
161{
162 m_setSaveDataInfo = info;
163 m_setDataStatus = e_settingData;
164 m_initCallbackFunc = cb;
165 m_initCallbackParam = lpParam;
166 m_dataProgress = -1;
167 m_uploadSaveSize = 0;
168 m_startTime = System::currentTimeMillis();
169 bool bOK = init(setSaveDataInitCallback, this);
170 if(!bOK)
171 m_setDataStatus = e_settingDataFailed;
172
173 return bOK;
174}
175
176const char* SonyRemoteStorage::getLocalFilename()
177{
178 return sc_localSaveFullPath;
179}
180
181const char* SonyRemoteStorage::getSaveNameUTF8()
182{
183 if(m_getInfoStatus != e_infoFound)
184 return NULL;
185 return m_retrievedDescData.m_saveNameUTF8;
186}
187
188ESavePlatform SonyRemoteStorage::getSavePlatform()
189{
190 if(m_getInfoStatus != e_infoFound)
191 return SAVE_FILE_PLATFORM_NONE;
192 return m_retrievedDescData.m_savePlatform;
193
194}
195
196__int64 SonyRemoteStorage::getSaveSeed()
197{
198 if(m_getInfoStatus != e_infoFound)
199 return 0;
200
201 return m_retrievedDescData.m_seed;
202}
203
204unsigned int SonyRemoteStorage::getSaveHostOptions()
205{
206 if(m_getInfoStatus != e_infoFound)
207 return 0;
208 return m_retrievedDescData.m_hostOptions;
209}
210
211unsigned int SonyRemoteStorage::getSaveTexturePack()
212{
213 if(m_getInfoStatus != e_infoFound)
214 return 0;
215
216 return m_retrievedDescData.m_texturePack;
217}
218
219const char* SonyRemoteStorage::getRemoteSaveFilename()
220{
221 return sc_remoteSaveFilename;
222}
223
224int SonyRemoteStorage::getSaveFilesize()
225{
226 if(m_getInfoStatus == e_infoFound)
227 {
228 return m_remoteFileInfo->fileSize;
229 }
230 return 0;
231}
232
233
234bool SonyRemoteStorage::setData( PSAVE_INFO info, CallbackFunc cb, LPVOID lpParam )
235{
236 m_setDataSaveInfo = info;
237 m_callbackFunc = cb;
238 m_callbackParam = lpParam;
239 m_status = e_setDataInProgress;
240
241 C4JStorage::ESaveGameState eLoadStatus=StorageManager.LoadSaveDataThumbnail(info,&LoadSaveDataThumbnailReturned,this);
242 return true;
243}
244
245int SonyRemoteStorage::LoadSaveDataThumbnailReturned(LPVOID lpParam,PBYTE pbThumbnail,DWORD dwThumbnailBytes)
246{
247 SonyRemoteStorage *pClass= (SonyRemoteStorage *)lpParam;
248
249 if(pClass->m_bAborting)
250 {
251 pClass->runCallback();
252 return 0;
253 }
254
255 app.DebugPrintf("Received data for a thumbnail\n");
256
257 if(pbThumbnail && dwThumbnailBytes)
258 {
259 pClass->m_thumbnailData = pbThumbnail;
260 pClass->m_thumbnailDataSize = dwThumbnailBytes;
261 }
262 else
263 {
264 app.DebugPrintf("Thumbnail data is NULL, or has size 0\n");
265 pClass->m_thumbnailData = NULL;
266 pClass->m_thumbnailDataSize = 0;
267 }
268
269 if(pClass->m_SetDataThread != NULL)
270 delete pClass->m_SetDataThread;
271
272 pClass->m_SetDataThread = new C4JThread(setDataThread, pClass, "setDataThread");
273 pClass->m_SetDataThread->Run();
274
275 return 0;
276}
277
278int SonyRemoteStorage::setDataThread(void* lpParam)
279{
280 SonyRemoteStorage* pClass = (SonyRemoteStorage*)lpParam;
281 pClass->m_startTime = System::currentTimeMillis();
282 pClass->setDataInternal();
283 return 0;
284}
285
286bool SonyRemoteStorage::saveIsAvailable()
287{
288 if(m_getInfoStatus != e_infoFound)
289 return false;
290#ifdef __PS3__
291 return (getSavePlatform() == SAVE_FILE_PLATFORM_PSVITA);
292#elif defined __PSVITA__
293 return (getSavePlatform() == SAVE_FILE_PLATFORM_PS3);
294#else // __ORBIS__
295 return true;
296#endif
297}
298
299bool SonyRemoteStorage::saveVersionSupported()
300{
301 return (m_retrievedDescData.m_saveVersion <= SAVE_FILE_VERSION_NUMBER);
302}
303
304
305
306int SonyRemoteStorage::getDataProgress()
307{
308 if(m_dataProgress < 0)
309 return 0;
310 int chunkSize = 1024*1024; // 1mb chunks when downloading
311 int totalSize = getSaveFilesize();
312 int transferRatePerSec = 300*1024; // a pessimistic download transfer rate
313 if(getStatus() == e_setDataInProgress)
314 {
315 chunkSize = 5 * 1024 * 1024; // 5mb chunks when uploading
316 totalSize = m_uploadSaveSize;
317 transferRatePerSec = 20*1024; // a pessimistic upload transfer rate
318 }
319 int sizeTransferred = (totalSize * m_dataProgress) / 100;
320 int nextChunk = ((sizeTransferred + chunkSize) * 100) / totalSize;
321
322
323 __int64 time = System::currentTimeMillis();
324 int elapsedSecs = (time - m_startTime) / 1000;
325 float estimatedTransfered = float(elapsedSecs * transferRatePerSec);
326 int progVal = m_dataProgress + (estimatedTransfered / float(totalSize)) * 100;
327 if(progVal > nextChunk)
328 return nextChunk;
329 if(progVal > 99)
330 {
331 if(m_dataProgress > 99)
332 return m_dataProgress;
333 return 99;
334 }
335 return progVal;
336}
337
338
339bool SonyRemoteStorage::shutdown()
340{
341 if(m_bInitialised)
342 {
343 int ret = sceRemoteStorageTerm();
344 if(ret >= 0)
345 {
346 app.DebugPrintf("Term request done \n");
347 m_bInitialised = false;
348 free(m_memPoolBuffer);
349 m_memPoolBuffer = NULL;
350 return true;
351 }
352 else
353 {
354 app.DebugPrintf("Error in Term request: 0x%x \n", ret);
355 return false;
356 }
357 }
358 return true;
359}
360
361
362void SonyRemoteStorage::waitForStorageManagerIdle()
363{
364 C4JStorage::ESaveGameState storageState = StorageManager.GetSaveState();
365 while(storageState != C4JStorage::ESaveGame_Idle)
366 {
367 Sleep(10);
368// app.DebugPrintf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>> storageState = %d\n", storageState);
369 storageState = StorageManager.GetSaveState();
370 }
371}
372void SonyRemoteStorage::GetDescriptionData(char* descData)
373{
374 switch(sc_CurrentDescDataVersion)
375 {
376 case 1:
377 {
378 DescriptionData descData_V1;
379 GetDescriptionData(descData_V1);
380 memcpy(descData, &descData_V1, sizeof(descData_V1));
381 }
382 break;
383 case 2:
384 {
385 DescriptionData_V2 descData_V2;
386 GetDescriptionData(descData_V2);
387 memcpy(descData, &descData_V2, sizeof(descData_V2));
388 }
389 break;
390 default:
391 assert(0);
392 break;
393 }
394}
395
396void SonyRemoteStorage::GetDescriptionData( DescriptionData& descData)
397{
398 ZeroMemory(&descData, sizeof(DescriptionData));
399 descData.m_platform[0] = SAVE_FILE_PLATFORM_LOCAL & 0xff;
400 descData.m_platform[1] = (SAVE_FILE_PLATFORM_LOCAL >> 8) & 0xff;
401 descData.m_platform[2] = (SAVE_FILE_PLATFORM_LOCAL >> 16) & 0xff;
402 descData.m_platform[3] = (SAVE_FILE_PLATFORM_LOCAL >> 24)& 0xff;
403
404 if(m_thumbnailData)
405 {
406 unsigned int uiHostOptions;
407 bool bHostOptionsRead;
408 DWORD uiTexturePack;
409 char seed[22];
410 app.GetImageTextData(m_thumbnailData, m_thumbnailDataSize,(unsigned char *)seed, uiHostOptions, bHostOptionsRead, uiTexturePack);
411
412 __int64 iSeed = strtoll(seed,NULL,10);
413 SetU64HexBytes(descData.m_seed, iSeed);
414 // Save the host options that this world was last played with
415 SetU32HexBytes(descData.m_hostOptions, uiHostOptions);
416 // Save the texture pack id
417 SetU32HexBytes(descData.m_texturePack, uiTexturePack);
418 }
419
420 memcpy(descData.m_saveNameUTF8, m_saveFileDesc, strlen(m_saveFileDesc));
421
422}
423
424void SonyRemoteStorage::GetDescriptionData( DescriptionData_V2& descData)
425{
426 ZeroMemory(&descData, sizeof(DescriptionData_V2));
427 descData.m_platformNone[0] = SAVE_FILE_PLATFORM_NONE & 0xff;
428 descData.m_platformNone[1] = (SAVE_FILE_PLATFORM_NONE >> 8) & 0xff;
429 descData.m_platformNone[2] = (SAVE_FILE_PLATFORM_NONE >> 16) & 0xff;
430 descData.m_platformNone[3] = (SAVE_FILE_PLATFORM_NONE >> 24)& 0xff;
431
432 // Save descData version
433 char descDataVersion[9];
434 sprintf(descDataVersion,"%08x",sc_CurrentDescDataVersion);
435 memcpy(descData.m_descDataVersion,descDataVersion,8); // Don't copy null
436
437
438 descData.m_platform[0] = SAVE_FILE_PLATFORM_LOCAL & 0xff;
439 descData.m_platform[1] = (SAVE_FILE_PLATFORM_LOCAL >> 8) & 0xff;
440 descData.m_platform[2] = (SAVE_FILE_PLATFORM_LOCAL >> 16) & 0xff;
441 descData.m_platform[3] = (SAVE_FILE_PLATFORM_LOCAL >> 24)& 0xff;
442
443 if(m_thumbnailData)
444 {
445 unsigned int uiHostOptions;
446 bool bHostOptionsRead;
447 DWORD uiTexturePack;
448 char seed[22];
449 app.GetImageTextData(m_thumbnailData, m_thumbnailDataSize,(unsigned char *)seed, uiHostOptions, bHostOptionsRead, uiTexturePack);
450
451 __int64 iSeed = strtoll(seed,NULL,10);
452 SetU64HexBytes(descData.m_seed, iSeed);
453 // Save the host options that this world was last played with
454 SetU32HexBytes(descData.m_hostOptions, uiHostOptions);
455 // Save the texture pack id
456 SetU32HexBytes(descData.m_texturePack, uiTexturePack);
457 // Save the savefile version
458 SetU32HexBytes(descData.m_saveVersion, SAVE_FILE_VERSION_NUMBER);
459 // clear out the future data with underscores
460 memset(descData.m_futureData, '_', sizeof(descData.m_futureData));
461 }
462
463 memcpy(descData.m_saveNameUTF8, m_saveFileDesc, strlen(m_saveFileDesc));
464
465}
466
467
468uint32_t SonyRemoteStorage::GetU32FromHexBytes(char* hexBytes)
469{
470 char hexString[9];
471 ZeroMemory(hexString,9);
472 memcpy(hexString, hexBytes,8);
473
474 uint32_t u32Val = 0;
475 std::stringstream ss;
476 ss << hexString;
477 ss >> std::hex >> u32Val;
478 return u32Val;
479}
480
481uint64_t SonyRemoteStorage::GetU64FromHexBytes(char* hexBytes)
482{
483 char hexString[17];
484 ZeroMemory(hexString,17);
485 memcpy(hexString, hexBytes,16);
486
487 uint64_t u64Val = 0;
488 std::stringstream ss;
489 ss << hexString;
490 ss >> std::hex >> u64Val;
491 return u64Val;
492
493}
494
495void SonyRemoteStorage::SetU32HexBytes(char* hexBytes, uint32_t u32)
496{
497 char hexString[9];
498 sprintf(hexString,"%08x",u32);
499 memcpy(hexBytes,hexString,8); // Don't copy null
500}
501
502void SonyRemoteStorage::SetU64HexBytes(char* hexBytes, uint64_t u64)
503{
504 char hexString[17];
505 sprintf(hexString,"%016llx",u64);
506 memcpy(hexBytes,hexString,16); // Don't copy null
507}