aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.Client/Common/Network/Sony/SonyRemoteStorage.cpp
blob: d745fb8524e92712cc0f5e3bd94b7291cde2caa9 (plain)
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
#include "stdafx.h"
#include "SonyRemoteStorage.h"


static const char sc_remoteSaveFilename[] = "/minecraft_save/gamedata.rs";
#ifdef __PSVITA__
static const char sc_localSaveFilename[] = "CloudSave_Vita.bin";
static const char sc_localSaveFullPath[] = "savedata0:CloudSave_Vita.bin";
#elif defined __PS3__
static const char sc_localSaveFilename[] = "CloudSave_PS3.bin";
static const char sc_localSaveFullPath[] = "NPEB01899--140720203552";
#else
static const char sc_localSaveFilename[] = "CloudSave_Orbis.bin";
static const char sc_localSaveFullPath[] = "/app0/CloudSave_Orbis.bin";
#endif

static SceRemoteStorageStatus statParams;




// void remoteStorageGetCallback(LPVOID lpParam, SonyRemoteStorage::Status s, int error_code)
// {
// 	app.DebugPrintf("remoteStorageGetCallback err : 0x%08x\n");
// }
//
// void remoteStorageCallback(LPVOID lpParam, SonyRemoteStorage::Status s, int error_code)
// {
// 	app.DebugPrintf("remoteStorageCallback err : 0x%08x\n");
//
// 	app.getRemoteStorage()->getRemoteFileInfo(&statParams, remoteStorageGetInfoCallback, NULL);
// }








void getSaveInfoReturnCallback(LPVOID lpParam, SonyRemoteStorage::Status s, int error_code)
{
	SonyRemoteStorage* pRemoteStorage = (SonyRemoteStorage*)lpParam;
	app.DebugPrintf("remoteStorageGetInfoCallback err : 0x%08x\n", error_code);
	if(error_code == 0)
	{
		for(int i=0;i<statParams.numFiles;i++)
		{
			if(strcmp(statParams.data[i].fileName, sc_remoteSaveFilename) == 0)
			{
				// found the file we need in the cloud
				pRemoteStorage->m_getInfoStatus = SonyRemoteStorage::e_infoFound;
				pRemoteStorage->m_remoteFileInfo = &statParams.data[i];
			}
		}
	}
	if(pRemoteStorage->m_getInfoStatus != SonyRemoteStorage::e_infoFound)
		pRemoteStorage->m_getInfoStatus = SonyRemoteStorage::e_noInfoFound;
}






static void getSaveInfoInitCallback(LPVOID lpParam, SonyRemoteStorage::Status s, int error_code)
{
	SonyRemoteStorage* pRemoteStorage = (SonyRemoteStorage*)lpParam;
	if(error_code != 0)
	{
		app.DebugPrintf("getSaveInfoInitCallback err : 0x%08x\n", error_code);
		pRemoteStorage->m_getInfoStatus = SonyRemoteStorage::e_noInfoFound;
	}
	else
	{
		app.DebugPrintf("getSaveInfoInitCallback calling getRemoteFileInfo\n");
		app.getRemoteStorage()->getRemoteFileInfo(&statParams, getSaveInfoReturnCallback, pRemoteStorage);
	}
}

void SonyRemoteStorage::getSaveInfo()
{
	if(m_getInfoStatus == e_gettingInfo)
	{
		app.DebugPrintf("SonyRemoteStorage::getSaveInfo already running!!!\n");
		return;
	}

	m_getInfoStatus = e_gettingInfo;
	if(!ProfileManager.IsSignedInLive(ProfileManager.GetPrimaryPad()))
	{
		m_getInfoStatus = e_noInfoFound;
		return;
	}
	app.DebugPrintf("SonyRemoteStorage::getSaveInfo calling init\n");

	bool bOK = init(getSaveInfoInitCallback, this);
	if(!bOK)
		m_getInfoStatus = e_noInfoFound;
}

bool SonyRemoteStorage::getSaveData( const char* localDirname, CallbackFunc cb, LPVOID lpParam )
{
	m_startTime = System::currentTimeMillis();
	m_dataProgress = 0;
	return getData(sc_remoteSaveFilename, localDirname, cb, lpParam);
}


static void setSaveDataInitCallback(LPVOID lpParam, SonyRemoteStorage::Status s, int error_code)
{
	SonyRemoteStorage* pRemoteStorage = (SonyRemoteStorage*)lpParam;
	if(error_code != 0)
	{
		app.DebugPrintf("setSaveDataInitCallback err : 0x%08x\n", error_code);
		pRemoteStorage->m_setDataStatus = SonyRemoteStorage::e_settingDataFailed;
		if(pRemoteStorage->m_initCallbackFunc)
			pRemoteStorage->m_initCallbackFunc(pRemoteStorage->m_initCallbackParam, s, error_code);
	}
	else
	{
		app.getRemoteStorage()->setData(pRemoteStorage->m_setSaveDataInfo, pRemoteStorage->m_initCallbackFunc, pRemoteStorage->m_initCallbackParam);
	}

}
bool SonyRemoteStorage::setSaveData(PSAVE_INFO info, CallbackFunc cb, void* lpParam)
{
	m_setSaveDataInfo = info;
	m_setDataStatus = e_settingData;
	m_initCallbackFunc = cb;
	m_initCallbackParam = lpParam;
	m_dataProgress = 0;
	bool bOK = init(setSaveDataInitCallback, this);
	if(!bOK)
		m_setDataStatus = e_settingDataFailed;

	return bOK;
}

const char* SonyRemoteStorage::getLocalFilename()
{
	return sc_localSaveFullPath;
}

const char* SonyRemoteStorage::getSaveNameUTF8()
{
	if(m_getInfoStatus != e_infoFound)
		return NULL;
	DescriptionData* pDescData = (DescriptionData*)m_remoteFileInfo->fileDescription;
	return pDescData->m_saveNameUTF8;
}

ESavePlatform SonyRemoteStorage::getSavePlatform()
{
	if(m_getInfoStatus != e_infoFound)
		return SAVE_FILE_PLATFORM_NONE;
	DescriptionData* pDescData = (DescriptionData*)m_remoteFileInfo->fileDescription;
	return (ESavePlatform)MAKE_FOURCC(pDescData->m_platform[0], pDescData->m_platform[1], pDescData->m_platform[2], pDescData->m_platform[3]);

}

int64_t SonyRemoteStorage::getSaveSeed()
{
	if(m_getInfoStatus != e_infoFound)
		return 0;
	DescriptionData* pDescData = (DescriptionData*)m_remoteFileInfo->fileDescription;

	char seedString[17];
	ZeroMemory(seedString,17);
	memcpy(seedString, pDescData->m_seed,16);

	uint64_t seed = 0;
	std::stringstream ss;
	ss << seedString;
	ss >> std::hex >> seed;
	return seed;
}

unsigned int SonyRemoteStorage::getSaveHostOptions()
{
	if(m_getInfoStatus != e_infoFound)
		return 0;
	DescriptionData* pDescData = (DescriptionData*)m_remoteFileInfo->fileDescription;

	char optionsString[9];
	ZeroMemory(optionsString,9);
	memcpy(optionsString, pDescData->m_hostOptions,8);

	unsigned int uiHostOptions = 0;
	std::stringstream ss;
	ss << optionsString;
	ss >> std::hex >> uiHostOptions;
	return uiHostOptions;
}

unsigned int SonyRemoteStorage::getSaveTexturePack()
{
	if(m_getInfoStatus != e_infoFound)
		return 0;
	DescriptionData* pDescData = (DescriptionData*)m_remoteFileInfo->fileDescription;

	char textureString[9];
	ZeroMemory(textureString,9);
	memcpy(textureString, pDescData->m_texturePack,8);

	unsigned int uiTexturePack = 0;
	std::stringstream ss;
	ss << textureString;
	ss >> std::hex >> uiTexturePack;
	return uiTexturePack;
}

const char* SonyRemoteStorage::getRemoteSaveFilename()
{
	return sc_remoteSaveFilename;
}

int SonyRemoteStorage::getSaveFilesize()
{
	if(m_getInfoStatus == e_infoFound)
	{
		return m_remoteFileInfo->fileSize;
	}
	return 0;
}


bool SonyRemoteStorage::setData( PSAVE_INFO info, CallbackFunc cb, LPVOID lpParam )
{
	m_setDataSaveInfo = info;
	m_callbackFunc = cb;
	m_callbackParam = lpParam;
	m_status = e_setDataInProgress;

	C4JStorage::ESaveGameState eLoadStatus=StorageManager.LoadSaveDataThumbnail(info,&LoadSaveDataThumbnailReturned,this);
	return true;
}

int SonyRemoteStorage::LoadSaveDataThumbnailReturned(LPVOID lpParam,PBYTE pbThumbnail,DWORD dwThumbnailBytes)
{
	SonyRemoteStorage *pClass= (SonyRemoteStorage *)lpParam;

	if(pClass->m_bAborting)
	{
		pClass->runCallback();
		return 0;
	}

	app.DebugPrintf("Received data for a thumbnail\n");

	if(pbThumbnail && dwThumbnailBytes)
	{
		pClass->m_thumbnailData = pbThumbnail;
		pClass->m_thumbnailDataSize = dwThumbnailBytes;
	}
	else
	{
		app.DebugPrintf("Thumbnail data is NULL, or has size 0\n");
		pClass->m_thumbnailData = NULL;
		pClass->m_thumbnailDataSize = 0;
	}

	if(pClass->m_SetDataThread != NULL)
		delete pClass->m_SetDataThread;

	pClass->m_SetDataThread = new C4JThread(setDataThread, pClass, "setDataThread");
	pClass->m_SetDataThread->Run();

	return 0;
}

int SonyRemoteStorage::setDataThread(void* lpParam)
{
	SonyRemoteStorage* pClass = (SonyRemoteStorage*)lpParam;
	pClass->m_startTime = System::currentTimeMillis();
	pClass->setDataInternal();
	return 0;
}

bool SonyRemoteStorage::saveIsAvailable()
{
	if(m_getInfoStatus != e_infoFound)
		return false;
#ifdef __PS3__
	return (getSavePlatform() == SAVE_FILE_PLATFORM_PSVITA);
#elif defined __PSVITA__
	return (getSavePlatform() == SAVE_FILE_PLATFORM_PS3);
#else // __ORBIS__
	return true;
#endif
}

int SonyRemoteStorage::getDataProgress()
{
	int64_t time = System::currentTimeMillis();
	int elapsedSecs = (time - m_startTime) / 1000;
	int progVal = m_dataProgress + (elapsedSecs/3);
	if(progVal > 95)
	{
		return m_dataProgress;
	}
	return progVal;
}


bool SonyRemoteStorage::shutdown()
{
	if(m_bInitialised)
	{
		int ret = sceRemoteStorageTerm();
		if(ret >= 0)
		{
			app.DebugPrintf("Term request done \n");
			m_bInitialised = false;
			free(m_memPoolBuffer);
			m_memPoolBuffer = NULL;
			return true;
		}
		else
		{
			app.DebugPrintf("Error in Term request: 0x%x \n", ret);
			return false;
		}
	}
	return true;
}


void SonyRemoteStorage::waitForStorageManagerIdle()
{
	C4JStorage::ESaveGameState storageState = StorageManager.GetSaveState();
	while(storageState != C4JStorage::ESaveGame_Idle)
	{
		Sleep(10);
// 		app.DebugPrintf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>           >>>>>     storageState = %d\n", storageState);
		storageState = StorageManager.GetSaveState();
	}
}