2012-12-10 12:08:54 +00:00
|
|
|
// Copyright (c) 2012- PPSSPP Project.
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, version 2.0 or later versions.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official git repository and contact information can be found at
|
|
|
|
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
|
|
|
|
|
2013-08-12 19:22:03 +00:00
|
|
|
|
|
|
|
#include "base/logging.h"
|
2013-12-29 22:28:31 +00:00
|
|
|
#include "Common/ChunkFile.h"
|
2013-05-31 06:32:13 +00:00
|
|
|
#include "Core/Reporting.h"
|
|
|
|
#include "Core/Dialog/SavedataParam.h"
|
|
|
|
#include "Core/Dialog/PSPSaveDialog.h"
|
2013-12-29 22:28:31 +00:00
|
|
|
#include "Core/FileSystems/MetaFileSystem.h"
|
2013-09-10 09:09:18 +00:00
|
|
|
#include "Core/HLE/sceIo.h"
|
2013-05-31 06:32:13 +00:00
|
|
|
#include "Core/HLE/sceKernelMemory.h"
|
|
|
|
#include "Core/HLE/sceChnnlsv.h"
|
|
|
|
#include "Core/ELF/ParamSFO.h"
|
2012-12-19 15:57:22 +00:00
|
|
|
#include "Core/HW/MemoryStick.h"
|
2013-05-31 06:32:13 +00:00
|
|
|
|
|
|
|
#include "image/png_load.h"
|
2012-12-10 12:08:54 +00:00
|
|
|
|
2013-09-17 04:48:14 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2013-06-24 11:22:05 +00:00
|
|
|
#ifdef BLACKBERRY
|
|
|
|
using std::strnlen;
|
|
|
|
#endif
|
|
|
|
|
2013-05-31 06:55:49 +00:00
|
|
|
static const std::string ICON0_FILENAME = "ICON0.PNG";
|
|
|
|
static const std::string ICON1_FILENAME = "ICON1.PMF";
|
|
|
|
static const std::string PIC1_FILENAME = "PIC1.PNG";
|
|
|
|
static const std::string SND0_FILENAME = "SND0.AT3";
|
|
|
|
static const std::string SFO_FILENAME = "PARAM.SFO";
|
2012-12-10 12:08:54 +00:00
|
|
|
|
2014-01-24 09:21:13 +00:00
|
|
|
static const std::string savePath = "ms0:/PSP/SAVEDATA/";
|
2012-12-10 12:08:54 +00:00
|
|
|
|
2012-12-19 15:57:22 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
int getSizeNormalized(int size)
|
|
|
|
{
|
2012-12-25 01:05:54 +00:00
|
|
|
int sizeCluster = (int)MemoryStick_SectorSize();
|
2012-12-19 17:15:02 +00:00
|
|
|
return ((int)((size + sizeCluster - 1) / sizeCluster)) * sizeCluster;
|
2012-12-19 15:57:22 +00:00
|
|
|
}
|
2012-12-25 01:18:59 +00:00
|
|
|
|
|
|
|
void SetStringFromSFO(ParamSFOData &sfoFile, const char *name, char *str, int strLength)
|
|
|
|
{
|
|
|
|
std::string value = sfoFile.GetValueString(name);
|
|
|
|
strncpy(str, value.c_str(), strLength - 1);
|
|
|
|
str[strLength - 1] = 0;
|
|
|
|
}
|
2012-12-25 01:32:14 +00:00
|
|
|
|
2013-01-28 23:11:02 +00:00
|
|
|
bool ReadPSPFile(std::string filename, u8 **data, s64 dataSize, s64 *readSize)
|
2012-12-25 01:32:14 +00:00
|
|
|
{
|
|
|
|
u32 handle = pspFileSystem.OpenFile(filename, FILEACCESS_READ);
|
|
|
|
if (handle == 0)
|
|
|
|
return false;
|
|
|
|
|
2013-01-28 23:11:02 +00:00
|
|
|
if(dataSize == -1)
|
|
|
|
{
|
|
|
|
dataSize = pspFileSystem.GetFileInfo(filename).size;
|
2013-02-06 18:01:10 +00:00
|
|
|
*data = new u8[(size_t)dataSize];
|
2013-01-28 23:11:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t result = pspFileSystem.ReadFile(handle, *data, dataSize);
|
2012-12-25 01:32:14 +00:00
|
|
|
pspFileSystem.CloseFile(handle);
|
2013-01-04 23:32:07 +00:00
|
|
|
if(readSize)
|
|
|
|
*readSize = result;
|
2012-12-25 01:32:14 +00:00
|
|
|
|
|
|
|
return result != 0;
|
|
|
|
}
|
|
|
|
|
2013-01-19 21:48:20 +00:00
|
|
|
bool WritePSPFile(std::string filename, u8 *data, SceSize dataSize)
|
2012-12-25 01:32:14 +00:00
|
|
|
{
|
|
|
|
u32 handle = pspFileSystem.OpenFile(filename, (FileAccess)(FILEACCESS_WRITE | FILEACCESS_CREATE));
|
|
|
|
if (handle == 0)
|
|
|
|
return false;
|
|
|
|
|
2013-01-19 21:48:20 +00:00
|
|
|
size_t result = pspFileSystem.WriteFile(handle, data, dataSize);
|
2012-12-25 01:32:14 +00:00
|
|
|
pspFileSystem.CloseFile(handle);
|
|
|
|
|
|
|
|
return result != 0;
|
|
|
|
}
|
2012-12-25 12:30:10 +00:00
|
|
|
|
2012-12-29 22:55:34 +00:00
|
|
|
bool PSPMatch(std::string text, std::string regexp)
|
|
|
|
{
|
|
|
|
if(text.empty() && regexp.empty())
|
|
|
|
return true;
|
|
|
|
else if(regexp == "*")
|
|
|
|
return true;
|
|
|
|
else if(text.empty())
|
|
|
|
return false;
|
|
|
|
else if(regexp.empty())
|
|
|
|
return false;
|
|
|
|
else if(regexp == "?" && text.length() == 1)
|
|
|
|
return true;
|
|
|
|
else if(text == regexp)
|
|
|
|
return true;
|
|
|
|
else if(regexp.data()[0] == '*')
|
|
|
|
{
|
|
|
|
bool res = PSPMatch(text.substr(1),regexp.substr(1));
|
|
|
|
if(!res)
|
|
|
|
res = PSPMatch(text.substr(1),regexp);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
else if(regexp.data()[0] == '?')
|
|
|
|
{
|
|
|
|
return PSPMatch(text.substr(1),regexp.substr(1));
|
|
|
|
}
|
|
|
|
else if(regexp.data()[0] == text.data()[0])
|
|
|
|
{
|
|
|
|
return PSPMatch(text.substr(1),regexp.substr(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2013-01-28 23:11:02 +00:00
|
|
|
|
|
|
|
int align16(int address)
|
|
|
|
{
|
|
|
|
return ((address + 0xF) >> 4) << 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
int GetSDKMainVersion(int sdkVersion)
|
|
|
|
{
|
2013-08-23 06:23:48 +00:00
|
|
|
if(sdkVersion > 0x0307FFFF)
|
2013-01-28 23:11:02 +00:00
|
|
|
return 6;
|
2013-08-23 06:23:48 +00:00
|
|
|
if(sdkVersion > 0x0300FFFF)
|
2013-01-28 23:11:02 +00:00
|
|
|
return 5;
|
2013-08-23 06:23:48 +00:00
|
|
|
if(sdkVersion > 0x0206FFFF)
|
2013-01-28 23:11:02 +00:00
|
|
|
return 4;
|
2013-08-23 06:23:48 +00:00
|
|
|
if(sdkVersion > 0x0205FFFF)
|
2013-01-28 23:11:02 +00:00
|
|
|
return 3;
|
2013-08-23 06:23:48 +00:00
|
|
|
if(sdkVersion >= 0x02000000)
|
2013-01-28 23:11:02 +00:00
|
|
|
return 2;
|
2013-08-23 06:23:48 +00:00
|
|
|
if(sdkVersion >= 0x01000000)
|
2013-01-28 23:11:02 +00:00
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
};
|
2012-12-19 15:57:22 +00:00
|
|
|
}
|
|
|
|
|
2013-12-29 22:44:35 +00:00
|
|
|
void SaveFileInfo::DoState(PointerWrap &p)
|
|
|
|
{
|
|
|
|
auto s = p.Section("SaveFileInfo", 1, 2);
|
|
|
|
if (!s)
|
|
|
|
return;
|
|
|
|
|
|
|
|
p.Do(size);
|
|
|
|
p.Do(saveName);
|
|
|
|
p.Do(idx);
|
|
|
|
|
|
|
|
p.DoArray(title, sizeof(title));
|
|
|
|
p.DoArray(saveTitle, sizeof(saveTitle));
|
|
|
|
p.DoArray(saveDetail, sizeof(saveDetail));
|
|
|
|
|
|
|
|
p.Do(modif_time);
|
|
|
|
|
|
|
|
if (s <= 1) {
|
|
|
|
u32 textureData;
|
|
|
|
int textureWidth;
|
|
|
|
int textureHeight;
|
|
|
|
p.Do(textureData);
|
|
|
|
p.Do(textureWidth);
|
|
|
|
p.Do(textureHeight);
|
|
|
|
|
|
|
|
if (textureData != 0) {
|
|
|
|
// Must be MODE_READ.
|
|
|
|
texture = new PPGeImage("");
|
|
|
|
texture->CompatLoad(textureData, textureWidth, textureHeight);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
bool hasTexture = texture != NULL;
|
|
|
|
p.Do(hasTexture);
|
|
|
|
if (hasTexture) {
|
|
|
|
if (p.mode == p.MODE_READ) {
|
|
|
|
texture = new PPGeImage("");
|
|
|
|
}
|
|
|
|
texture->DoState(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-10 12:08:54 +00:00
|
|
|
SavedataParam::SavedataParam()
|
|
|
|
: pspParam(0)
|
|
|
|
, selectedSave(0)
|
|
|
|
, saveDataList(0)
|
2013-01-29 21:46:20 +00:00
|
|
|
, noSaveIcon(0)
|
2012-12-28 21:36:37 +00:00
|
|
|
, saveDataListCount(0)
|
2013-01-02 20:11:24 +00:00
|
|
|
, saveNameListDataCount(0)
|
2012-12-10 12:08:54 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void SavedataParam::Init()
|
|
|
|
{
|
2012-12-19 20:23:52 +00:00
|
|
|
if (!pspFileSystem.GetFileInfo(savePath).exists)
|
2012-12-10 12:08:54 +00:00
|
|
|
{
|
|
|
|
pspFileSystem.MkDir(savePath);
|
|
|
|
}
|
2013-08-12 19:22:03 +00:00
|
|
|
// Create a nomedia file to hide save icons form Android image viewer
|
|
|
|
#ifdef ANDROID
|
|
|
|
int handle = pspFileSystem.OpenFile(savePath + ".nomedia", (FileAccess)(FILEACCESS_CREATE | FILEACCESS_WRITE), 0);
|
|
|
|
if (handle) {
|
|
|
|
pspFileSystem.CloseFile(handle);
|
|
|
|
} else {
|
2013-12-04 16:41:59 +00:00
|
|
|
ELOG("Failed to create .nomedia file");
|
2013-08-12 19:22:03 +00:00
|
|
|
}
|
|
|
|
#endif
|
2012-12-10 12:08:54 +00:00
|
|
|
}
|
|
|
|
|
2014-01-24 09:21:13 +00:00
|
|
|
std::string SavedataParam::GetSaveDirName(const SceUtilitySavedataParam *param, int saveId) const
|
2013-01-06 00:29:14 +00:00
|
|
|
{
|
|
|
|
if (!param) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (saveId >= 0 && saveNameListDataCount > 0) // if user selection, use it
|
2014-01-24 09:21:13 +00:00
|
|
|
return GetFilename(saveId);
|
|
|
|
else
|
|
|
|
return GetSaveName(param);
|
2013-01-06 00:29:14 +00:00
|
|
|
}
|
|
|
|
|
2014-01-24 09:21:13 +00:00
|
|
|
std::string SavedataParam::GetSaveDir(const SceUtilitySavedataParam *param, const std::string &saveDirName) const
|
2012-12-10 12:08:54 +00:00
|
|
|
{
|
|
|
|
if (!param) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2013-05-12 03:27:54 +00:00
|
|
|
return GetGameName(param) + saveDirName;
|
|
|
|
}
|
2012-12-10 12:08:54 +00:00
|
|
|
|
2014-01-24 09:21:13 +00:00
|
|
|
std::string SavedataParam::GetSaveDir(const SceUtilitySavedataParam *param, int saveId) const
|
2013-05-12 03:27:54 +00:00
|
|
|
{
|
|
|
|
return GetSaveDir(param, GetSaveDirName(param, saveId));
|
2012-12-14 19:55:57 +00:00
|
|
|
}
|
|
|
|
|
2014-01-24 09:21:13 +00:00
|
|
|
std::string SavedataParam::GetSaveFilePath(const SceUtilitySavedataParam *param, const std::string &saveDir) const
|
2012-12-14 19:55:57 +00:00
|
|
|
{
|
|
|
|
if (!param) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2013-05-12 03:27:54 +00:00
|
|
|
return savePath + saveDir;
|
|
|
|
}
|
|
|
|
|
2014-01-24 09:21:13 +00:00
|
|
|
std::string SavedataParam::GetSaveFilePath(const SceUtilitySavedataParam *param, int saveId) const
|
2013-05-12 03:27:54 +00:00
|
|
|
{
|
|
|
|
return GetSaveFilePath(param, GetSaveDir(param, saveId));
|
2012-12-10 12:08:54 +00:00
|
|
|
}
|
|
|
|
|
2014-01-24 09:21:13 +00:00
|
|
|
inline static std::string FixedToString(const char *str, size_t n)
|
|
|
|
{
|
|
|
|
return std::string(str, strnlen(str, n));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string SavedataParam::GetGameName(const SceUtilitySavedataParam *param) const
|
2012-12-11 02:09:52 +00:00
|
|
|
{
|
2014-01-24 09:21:13 +00:00
|
|
|
return FixedToString(param->gameName, ARRAY_SIZE(param->gameName));
|
2012-12-11 02:09:52 +00:00
|
|
|
}
|
|
|
|
|
2014-01-24 09:21:13 +00:00
|
|
|
std::string SavedataParam::GetSaveName(const SceUtilitySavedataParam *param) const
|
2012-12-11 02:09:52 +00:00
|
|
|
{
|
2014-01-24 09:21:13 +00:00
|
|
|
const std::string saveName = FixedToString(param->saveName, ARRAY_SIZE(param->saveName));
|
|
|
|
if (saveName == "<>")
|
2012-12-31 18:08:46 +00:00
|
|
|
return "";
|
2012-12-11 02:09:52 +00:00
|
|
|
return saveName;
|
|
|
|
}
|
|
|
|
|
2014-01-24 09:21:13 +00:00
|
|
|
std::string SavedataParam::GetFileName(const SceUtilitySavedataParam *param) const
|
2012-12-11 02:09:52 +00:00
|
|
|
{
|
2014-01-24 09:21:13 +00:00
|
|
|
return FixedToString(param->fileName, ARRAY_SIZE(param->fileName));
|
2012-12-11 02:09:52 +00:00
|
|
|
}
|
2012-12-10 12:08:54 +00:00
|
|
|
|
|
|
|
bool SavedataParam::Delete(SceUtilitySavedataParam* param, int saveId)
|
|
|
|
{
|
|
|
|
if (!param)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string dirPath = GetSaveFilePath(param,saveId);
|
2012-12-19 20:23:52 +00:00
|
|
|
if (saveId >= 0 && saveNameListDataCount > 0) // if user selection, use it
|
2012-12-10 12:08:54 +00:00
|
|
|
{
|
2012-12-19 20:23:52 +00:00
|
|
|
if (saveDataList[saveId].size == 0) // don't delete no existing file
|
2012-12-10 12:08:54 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pspFileSystem.RmDir(dirPath);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-11-02 11:48:25 +00:00
|
|
|
int SavedataParam::DeleteData(SceUtilitySavedataParam* param) {
|
|
|
|
if(!param)
|
|
|
|
return SCE_UTILITY_SAVEDATA_ERROR_DELETE_NO_DATA;
|
|
|
|
if (param->fileName == NULL)
|
|
|
|
return SCE_UTILITY_SAVEDATA_ERROR_DELETE_NO_DATA;
|
|
|
|
|
2014-01-24 09:21:13 +00:00
|
|
|
std::string filename = savePath + GetGameName(param) + GetSaveName(param) + "/" + GetFileName(param);
|
2013-11-02 11:48:25 +00:00
|
|
|
PSPFileInfo info = pspFileSystem.GetFileInfo(filename);
|
|
|
|
if (info.exists)
|
|
|
|
pspFileSystem.RemoveFile(filename);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-05-12 03:27:54 +00:00
|
|
|
bool SavedataParam::Save(SceUtilitySavedataParam* param, const std::string &saveDirName, bool secureMode)
|
2012-12-10 12:08:54 +00:00
|
|
|
{
|
|
|
|
if (!param) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-05-12 03:27:54 +00:00
|
|
|
std::string dirPath = GetSaveFilePath(param, GetSaveDir(param, saveDirName));
|
2012-12-10 12:08:54 +00:00
|
|
|
|
2012-12-19 20:23:52 +00:00
|
|
|
if (!pspFileSystem.GetFileInfo(dirPath).exists)
|
2012-12-10 12:08:54 +00:00
|
|
|
pspFileSystem.MkDir(dirPath);
|
|
|
|
|
2013-01-28 23:11:02 +00:00
|
|
|
u8* cryptedData = 0;
|
|
|
|
int cryptedSize = 0;
|
2013-01-30 20:27:46 +00:00
|
|
|
u8 cryptedHash[0x10];
|
|
|
|
memset(cryptedHash,0,0x10);
|
2013-01-28 23:11:02 +00:00
|
|
|
// Encrypt save.
|
2013-04-21 21:18:12 +00:00
|
|
|
// TODO: Is this the correct difference between MAKEDATA and MAKEDATASECURE?
|
2013-06-25 13:51:39 +00:00
|
|
|
if (param->dataBuf.IsValid() && g_Config.bEncryptSave && secureMode)
|
2012-12-10 12:08:54 +00:00
|
|
|
{
|
2013-01-28 23:11:02 +00:00
|
|
|
cryptedSize = param->dataSize;
|
2013-01-31 07:54:26 +00:00
|
|
|
if(cryptedSize == 0 || (SceSize)cryptedSize > param->dataBufSize)
|
2013-01-28 23:11:02 +00:00
|
|
|
cryptedSize = param->dataBufSize; // fallback, should never use this
|
2013-06-23 05:49:39 +00:00
|
|
|
u8 *data_ = param->dataBuf;
|
2012-12-24 21:28:10 +00:00
|
|
|
|
2013-01-28 23:11:02 +00:00
|
|
|
int aligned_len = align16(cryptedSize);
|
|
|
|
cryptedData = new u8[aligned_len + 0x10];
|
|
|
|
memcpy(cryptedData, data_, cryptedSize);
|
2013-01-06 00:29:14 +00:00
|
|
|
|
2013-01-28 23:11:02 +00:00
|
|
|
int decryptMode = 1;
|
|
|
|
if(param->key[0] != 0)
|
2012-12-10 12:08:54 +00:00
|
|
|
{
|
2013-01-28 23:11:02 +00:00
|
|
|
decryptMode = (GetSDKMainVersion(sceKernelGetCompiledSdkVersion()) >= 4 ? 5 : 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(EncryptData(decryptMode, cryptedData, &cryptedSize, &aligned_len, cryptedHash, ((param->key[0] != 0)?param->key:0)) == 0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-07 20:02:55 +00:00
|
|
|
ERROR_LOG(SCEUTILITY,"Save encryption failed. This save won't work on real PSP");
|
2013-01-28 23:11:02 +00:00
|
|
|
delete[] cryptedData;
|
|
|
|
cryptedData = 0;
|
2012-12-10 12:08:54 +00:00
|
|
|
}
|
2012-12-31 18:08:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SAVE PARAM.SFO
|
|
|
|
ParamSFOData sfoFile;
|
2013-05-31 06:55:49 +00:00
|
|
|
std::string sfopath = dirPath+"/" + SFO_FILENAME;
|
2012-12-31 18:08:46 +00:00
|
|
|
PSPFileInfo sfoInfo = pspFileSystem.GetFileInfo(sfopath);
|
2013-12-08 20:02:37 +00:00
|
|
|
if (sfoInfo.exists) // Read old sfo if exist
|
2012-12-31 18:08:46 +00:00
|
|
|
{
|
2013-12-08 20:02:37 +00:00
|
|
|
std::vector<u8> sfoData;
|
|
|
|
if (pspFileSystem.ReadEntireFile(sfopath, sfoData) >= 0)
|
|
|
|
sfoFile.ReadSFO(sfoData);
|
2012-12-31 18:08:46 +00:00
|
|
|
}
|
2012-12-10 12:08:54 +00:00
|
|
|
|
2012-12-31 18:08:46 +00:00
|
|
|
// Update values
|
|
|
|
sfoFile.SetValue("TITLE",param->sfoParam.title,128);
|
|
|
|
sfoFile.SetValue("SAVEDATA_TITLE",param->sfoParam.savedataTitle,128);
|
|
|
|
sfoFile.SetValue("SAVEDATA_DETAIL",param->sfoParam.detail,1024);
|
|
|
|
sfoFile.SetValue("PARENTAL_LEVEL",param->sfoParam.parentalLevel,4);
|
|
|
|
sfoFile.SetValue("CATEGORY","MS",4);
|
2013-05-12 03:27:54 +00:00
|
|
|
sfoFile.SetValue("SAVEDATA_DIRECTORY", GetSaveDir(param, saveDirName), 64);
|
2012-12-31 18:08:46 +00:00
|
|
|
|
|
|
|
// For each file, 13 bytes for filename, 16 bytes for file hash (0 in PPSSPP), 3 byte for padding
|
2013-05-31 06:32:13 +00:00
|
|
|
if (secureMode)
|
|
|
|
{
|
|
|
|
const int FILE_LIST_ITEM_SIZE = 13 + 16 + 3;
|
|
|
|
const int FILE_LIST_COUNT_MAX = 99;
|
2013-07-08 01:24:53 +00:00
|
|
|
const u32 FILE_LIST_TOTAL_SIZE = FILE_LIST_ITEM_SIZE * FILE_LIST_COUNT_MAX;
|
2013-05-31 06:32:13 +00:00
|
|
|
u32 tmpDataSize = 0;
|
|
|
|
u8 *tmpDataOrig = sfoFile.GetValueData("SAVEDATA_FILE_LIST", &tmpDataSize);
|
|
|
|
u8 *tmpData = new u8[FILE_LIST_TOTAL_SIZE];
|
|
|
|
|
|
|
|
if (tmpDataOrig != NULL)
|
|
|
|
memcpy(tmpData, tmpDataOrig, tmpDataSize > FILE_LIST_TOTAL_SIZE ? FILE_LIST_TOTAL_SIZE : tmpDataSize);
|
|
|
|
else
|
|
|
|
memset(tmpData, 0, FILE_LIST_TOTAL_SIZE);
|
|
|
|
|
2013-06-25 13:51:39 +00:00
|
|
|
if (param->dataBuf.IsValid())
|
2012-12-14 20:36:54 +00:00
|
|
|
{
|
2013-05-31 06:32:13 +00:00
|
|
|
char *fName = (char*)tmpData;
|
|
|
|
for(int i = 0; i < FILE_LIST_COUNT_MAX; i++)
|
|
|
|
{
|
|
|
|
if(fName[0] == 0)
|
|
|
|
break; // End of list
|
|
|
|
if(strncmp(fName,GetFileName(param).c_str(),20) == 0)
|
|
|
|
break;
|
|
|
|
fName += FILE_LIST_ITEM_SIZE;
|
|
|
|
}
|
2013-01-02 03:46:53 +00:00
|
|
|
|
2013-05-31 06:32:13 +00:00
|
|
|
if (fName + 13 <= (char*)tmpData + FILE_LIST_TOTAL_SIZE)
|
|
|
|
snprintf(fName, 13, "%s",GetFileName(param).c_str());
|
|
|
|
if (fName + 13 + 16 <= (char*)tmpData + FILE_LIST_TOTAL_SIZE)
|
|
|
|
memcpy(fName+13, cryptedHash, 16);
|
|
|
|
}
|
2013-07-08 01:24:53 +00:00
|
|
|
sfoFile.SetValue("SAVEDATA_FILE_LIST", tmpData, FILE_LIST_TOTAL_SIZE, (int)FILE_LIST_TOTAL_SIZE);
|
2013-05-31 06:32:13 +00:00
|
|
|
delete[] tmpData;
|
2012-12-31 18:08:46 +00:00
|
|
|
}
|
|
|
|
|
2013-01-28 23:11:02 +00:00
|
|
|
// Init param with 0. This will be used to detect crypted save or not on loading
|
2013-05-31 06:32:13 +00:00
|
|
|
u8 *tmpData = new u8[128];
|
2012-12-31 18:08:46 +00:00
|
|
|
memset(tmpData, 0, 128);
|
|
|
|
sfoFile.SetValue("SAVEDATA_PARAMS", tmpData, 128, 128);
|
|
|
|
delete[] tmpData;
|
|
|
|
|
|
|
|
u8 *sfoData;
|
|
|
|
size_t sfoSize;
|
|
|
|
sfoFile.WriteSFO(&sfoData,&sfoSize);
|
2013-01-28 23:11:02 +00:00
|
|
|
|
|
|
|
// Calc SFO hash for PSP.
|
|
|
|
if(cryptedData != 0)
|
|
|
|
{
|
|
|
|
int offset = sfoFile.GetDataOffset(sfoData,"SAVEDATA_PARAMS");
|
|
|
|
if(offset >= 0)
|
2013-06-09 11:02:16 +00:00
|
|
|
UpdateHash(sfoData, (int)sfoSize, offset, (param->key[0] ? 3 : 1));
|
2013-01-28 23:11:02 +00:00
|
|
|
}
|
2013-01-19 21:48:20 +00:00
|
|
|
WritePSPFile(sfopath, sfoData, (SceSize)sfoSize);
|
2012-12-31 18:08:46 +00:00
|
|
|
delete[] sfoData;
|
|
|
|
|
2013-06-25 13:51:39 +00:00
|
|
|
if(param->dataBuf.IsValid()) // Can launch save without save data in mode 13
|
2013-01-28 23:11:02 +00:00
|
|
|
{
|
|
|
|
std::string filePath = dirPath+"/"+GetFileName(param);
|
2013-06-23 05:49:39 +00:00
|
|
|
u8 *data_ = 0;
|
2013-01-28 23:11:02 +00:00
|
|
|
SceSize saveSize = 0;
|
|
|
|
if(cryptedData == 0) // Save decrypted data
|
|
|
|
{
|
|
|
|
saveSize = param->dataSize;
|
|
|
|
if(saveSize == 0 || saveSize > param->dataBufSize)
|
|
|
|
saveSize = param->dataBufSize; // fallback, should never use this
|
|
|
|
|
2013-06-23 05:49:39 +00:00
|
|
|
data_ = param->dataBuf;
|
2013-01-28 23:11:02 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
data_ = cryptedData;
|
|
|
|
saveSize = cryptedSize;
|
|
|
|
}
|
|
|
|
|
2013-09-07 20:02:55 +00:00
|
|
|
INFO_LOG(SCEUTILITY,"Saving file with size %u in %s",saveSize,filePath.c_str());
|
2013-01-28 23:11:02 +00:00
|
|
|
|
|
|
|
// copy back save name in request
|
2013-05-12 03:27:54 +00:00
|
|
|
strncpy(param->saveName, saveDirName.c_str(), 20);
|
2013-01-28 23:11:02 +00:00
|
|
|
|
|
|
|
if (!WritePSPFile(filePath, data_, saveSize))
|
|
|
|
{
|
2013-09-07 20:02:55 +00:00
|
|
|
ERROR_LOG(SCEUTILITY,"Error writing file %s",filePath.c_str());
|
2013-01-28 23:11:02 +00:00
|
|
|
if(cryptedData != 0)
|
|
|
|
{
|
|
|
|
delete[] cryptedData;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
delete[] cryptedData;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-31 18:08:46 +00:00
|
|
|
// SAVE ICON0
|
2013-06-25 13:51:39 +00:00
|
|
|
if (param->icon0FileData.buf.IsValid())
|
2012-12-31 18:08:46 +00:00
|
|
|
{
|
2013-05-31 06:55:49 +00:00
|
|
|
std::string icon0path = dirPath + "/" + ICON0_FILENAME;
|
2013-06-23 05:44:00 +00:00
|
|
|
WritePSPFile(icon0path, param->icon0FileData.buf, param->icon0FileData.bufSize);
|
2012-12-31 18:08:46 +00:00
|
|
|
}
|
|
|
|
// SAVE ICON1
|
2013-06-25 13:51:39 +00:00
|
|
|
if (param->icon1FileData.buf.IsValid())
|
2012-12-31 18:08:46 +00:00
|
|
|
{
|
2013-05-31 06:55:49 +00:00
|
|
|
std::string icon1path = dirPath + "/" + ICON1_FILENAME;
|
2013-06-23 05:44:00 +00:00
|
|
|
WritePSPFile(icon1path, param->icon1FileData.buf, param->icon1FileData.bufSize);
|
2012-12-31 18:08:46 +00:00
|
|
|
}
|
|
|
|
// SAVE PIC1
|
2013-06-25 13:51:39 +00:00
|
|
|
if (param->pic1FileData.buf.IsValid())
|
2012-12-31 18:08:46 +00:00
|
|
|
{
|
2013-05-31 06:55:49 +00:00
|
|
|
std::string pic1path = dirPath + "/" + PIC1_FILENAME;
|
2013-06-23 05:44:00 +00:00
|
|
|
WritePSPFile(pic1path, param->pic1FileData.buf, param->pic1FileData.bufSize);
|
2012-12-31 18:08:46 +00:00
|
|
|
}
|
2012-12-24 21:28:10 +00:00
|
|
|
|
2012-12-31 18:08:46 +00:00
|
|
|
// Save SND
|
2013-06-25 13:51:39 +00:00
|
|
|
if (param->snd0FileData.buf.IsValid())
|
2012-12-31 18:08:46 +00:00
|
|
|
{
|
2013-05-31 06:55:49 +00:00
|
|
|
std::string snd0path = dirPath + "/" + SND0_FILENAME;
|
2013-06-23 05:44:00 +00:00
|
|
|
WritePSPFile(snd0path, param->snd0FileData.buf, param->snd0FileData.bufSize);
|
2012-12-31 18:08:46 +00:00
|
|
|
}
|
2012-12-25 12:30:10 +00:00
|
|
|
|
2012-12-10 12:08:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-05-12 03:27:54 +00:00
|
|
|
bool SavedataParam::Load(SceUtilitySavedataParam *param, const std::string &saveDirName, int saveId, bool secureMode)
|
2012-12-10 12:08:54 +00:00
|
|
|
{
|
|
|
|
if (!param) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-05-12 03:27:54 +00:00
|
|
|
std::string dirPath = GetSaveFilePath(param, GetSaveDir(param, saveDirName));
|
2012-12-19 20:23:52 +00:00
|
|
|
if (saveId >= 0 && saveNameListDataCount > 0) // if user selection, use it
|
2012-12-10 12:08:54 +00:00
|
|
|
{
|
2012-12-19 20:23:52 +00:00
|
|
|
if (saveDataList[saveId].size == 0) // don't read no existing file
|
2012-12-10 12:08:54 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2013-11-03 15:52:16 +00:00
|
|
|
|
|
|
|
if(!LoadSaveData(param, saveDirName, dirPath, secureMode)) // Load main savedata
|
2013-11-03 15:10:05 +00:00
|
|
|
return false;
|
|
|
|
|
2013-11-03 15:52:16 +00:00
|
|
|
LoadSFO(param, dirPath); // Load sfo
|
2013-11-03 15:10:05 +00:00
|
|
|
|
|
|
|
// Don't know what it is, but PSP always respond this and this unlock some game
|
|
|
|
param->bind = 1021;
|
|
|
|
|
2013-11-03 16:13:00 +00:00
|
|
|
// Load another files,seems these are required by some games, e.g. Fushigi no Dungeon Fuurai no Shiren 4 Plus.
|
|
|
|
|
2013-11-03 15:52:16 +00:00
|
|
|
// Load ICON0.PNG
|
|
|
|
LoadFile(dirPath, ICON0_FILENAME, ¶m->icon0FileData);
|
|
|
|
// Load ICON1.PNG
|
|
|
|
LoadFile(dirPath, ICON1_FILENAME, ¶m->icon1FileData);
|
|
|
|
// Load PIC1.PNG
|
|
|
|
LoadFile(dirPath, PIC1_FILENAME, ¶m->pic1FileData);
|
|
|
|
// Load SND0.AT3
|
|
|
|
LoadFile(dirPath, SND0_FILENAME, ¶m->snd0FileData);
|
|
|
|
|
2013-11-03 15:10:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
2012-12-10 12:08:54 +00:00
|
|
|
|
2013-11-03 15:10:05 +00:00
|
|
|
bool SavedataParam::LoadSaveData(SceUtilitySavedataParam *param, const std::string &saveDirName, const std::string dirPath, bool secureMode) {
|
|
|
|
u8 *data_ = param->dataBuf;
|
2012-12-11 02:09:52 +00:00
|
|
|
std::string filePath = dirPath+"/"+GetFileName(param);
|
2013-01-04 23:32:07 +00:00
|
|
|
s64 readSize;
|
2013-09-07 20:02:55 +00:00
|
|
|
INFO_LOG(SCEUTILITY,"Loading file with size %u in %s",param->dataBufSize,filePath.c_str());
|
2013-01-28 23:11:02 +00:00
|
|
|
u8* saveData = 0;
|
|
|
|
int saveSize = -1;
|
2013-11-03 15:10:05 +00:00
|
|
|
if (!ReadPSPFile(filePath, &saveData, saveSize, &readSize)) {
|
2013-09-07 20:02:55 +00:00
|
|
|
ERROR_LOG(SCEUTILITY,"Error reading file %s",filePath.c_str());
|
2012-12-10 12:08:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-01-31 07:54:26 +00:00
|
|
|
saveSize = (int)readSize;
|
2013-01-06 00:29:14 +00:00
|
|
|
|
|
|
|
// copy back save name in request
|
2013-05-12 03:27:54 +00:00
|
|
|
strncpy(param->saveName, saveDirName.c_str(), 20);
|
2013-01-06 00:29:14 +00:00
|
|
|
|
2013-05-12 03:27:54 +00:00
|
|
|
bool isCrypted = IsSaveEncrypted(param, saveDirName) && secureMode;
|
2013-01-28 23:11:02 +00:00
|
|
|
bool saveDone = false;
|
2013-11-03 15:10:05 +00:00
|
|
|
if (isCrypted) {
|
|
|
|
LoadDecryptedSave(param, data_, saveData, saveSize, saveDone);
|
|
|
|
}
|
|
|
|
if (!saveDone) {
|
|
|
|
LoadNotCryptedSave(param, data_, saveData, saveSize);
|
|
|
|
}
|
|
|
|
param->dataSize = (SceSize)saveSize;
|
|
|
|
delete[] saveData;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-11-03 15:52:16 +00:00
|
|
|
void SavedataParam::LoadDecryptedSave(SceUtilitySavedataParam *param, u8 *data, u8 *saveData, int &saveSize, bool &saveDone) {
|
2013-11-03 15:10:05 +00:00
|
|
|
int align_len = align16(saveSize);
|
2013-01-28 23:11:02 +00:00
|
|
|
u8* data_base = new u8[align_len];
|
|
|
|
u8* cryptKey = new u8[0x10];
|
|
|
|
memset(cryptKey,0,0x10);
|
|
|
|
|
2013-11-03 15:10:05 +00:00
|
|
|
if(param->key[0] != 0) {
|
2013-01-28 23:11:02 +00:00
|
|
|
memcpy(cryptKey, param->key, 0x10);
|
|
|
|
}
|
|
|
|
memset(data_base + saveSize, 0, align_len - saveSize);
|
|
|
|
memcpy(data_base, saveData, saveSize);
|
|
|
|
|
|
|
|
int decryptMode = 1;
|
2013-11-03 15:10:05 +00:00
|
|
|
if(param->key[0] != 0) {
|
2013-01-28 23:11:02 +00:00
|
|
|
decryptMode = (GetSDKMainVersion(sceKernelGetCompiledSdkVersion()) >= 4 ? 5 : 3);
|
|
|
|
}
|
|
|
|
|
2013-11-03 15:10:05 +00:00
|
|
|
if(DecryptSave(decryptMode, data_base, &saveSize, &align_len, ((param->key[0] != 0)?cryptKey:0)) == 0) {
|
2013-09-04 15:14:39 +00:00
|
|
|
if (param->dataBuf.IsValid())
|
2013-11-03 15:10:05 +00:00
|
|
|
memcpy(data, data_base, std::min((u32)saveSize, (u32)param->dataBufSize));
|
2013-01-28 23:11:02 +00:00
|
|
|
saveDone = true;
|
|
|
|
}
|
|
|
|
delete[] data_base;
|
|
|
|
delete[] cryptKey;
|
2013-11-03 15:10:05 +00:00
|
|
|
}
|
2013-01-28 23:11:02 +00:00
|
|
|
|
2013-11-03 15:52:16 +00:00
|
|
|
void SavedataParam::LoadNotCryptedSave(SceUtilitySavedataParam *param, u8 *data, u8 *saveData, int &saveSize) {
|
2013-11-03 15:10:05 +00:00
|
|
|
if (param->dataBuf.IsValid())
|
|
|
|
memcpy(data, saveData, std::min((u32)saveSize, (u32)param->dataBufSize));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SavedataParam::LoadSFO(SceUtilitySavedataParam *param, const std::string dirPath) {
|
|
|
|
ParamSFOData sfoFile;
|
|
|
|
std::string sfopath = dirPath+"/" + SFO_FILENAME;
|
|
|
|
PSPFileInfo sfoInfo = pspFileSystem.GetFileInfo(sfopath);
|
2013-12-08 20:02:37 +00:00
|
|
|
if (sfoInfo.exists) {
|
|
|
|
// Read sfo
|
|
|
|
std::vector<u8> sfoData;
|
|
|
|
if (pspFileSystem.ReadEntireFile(sfopath, sfoData) >= 0) {
|
|
|
|
sfoFile.ReadSFO(sfoData);
|
2013-11-03 15:10:05 +00:00
|
|
|
|
|
|
|
// copy back info in request
|
|
|
|
strncpy(param->sfoParam.title,sfoFile.GetValueString("TITLE").c_str(),128);
|
|
|
|
strncpy(param->sfoParam.savedataTitle,sfoFile.GetValueString("SAVEDATA_TITLE").c_str(),128);
|
|
|
|
strncpy(param->sfoParam.detail,sfoFile.GetValueString("SAVEDATA_DETAIL").c_str(),1024);
|
|
|
|
param->sfoParam.parentalLevel = sfoFile.GetValueInt("PARENTAL_LEVEL");
|
|
|
|
}
|
|
|
|
}
|
2012-12-10 12:08:54 +00:00
|
|
|
}
|
|
|
|
|
2013-11-12 03:21:26 +00:00
|
|
|
std::set<std::string> SavedataParam::getSecureFileNames(std::string dirPath) {
|
2013-12-08 20:02:37 +00:00
|
|
|
PSPFileInfo sfoFileInfo = pspFileSystem.GetFileInfo(dirPath + "/" + SFO_FILENAME);
|
|
|
|
std::set<std::string> secureFileNames;
|
|
|
|
if (!sfoFileInfo.exists)
|
|
|
|
return secureFileNames;
|
2013-11-12 03:21:26 +00:00
|
|
|
|
2013-12-08 20:02:37 +00:00
|
|
|
ParamSFOData sfoFile;
|
|
|
|
std::vector<u8> sfoData;
|
|
|
|
if (pspFileSystem.ReadEntireFile(dirPath + "/" + SFO_FILENAME, sfoData) >= 0) {
|
|
|
|
sfoFile.ReadSFO(sfoData);
|
|
|
|
}
|
2013-11-12 03:21:26 +00:00
|
|
|
|
2013-12-08 20:02:37 +00:00
|
|
|
u32 sfoFileListSize = 0;
|
|
|
|
char *sfoFileList = (char *)sfoFile.GetValueData("SAVEDATA_FILE_LIST", &sfoFileListSize);
|
|
|
|
const int FILE_LIST_ITEM_SIZE = 13 + 16 + 3;
|
|
|
|
const u32 FILE_LIST_COUNT_MAX = 99;
|
2013-11-12 03:21:26 +00:00
|
|
|
|
2013-12-08 20:02:37 +00:00
|
|
|
// Filenames are 13 bytes long at most. Add a NULL so there's no surprises.
|
|
|
|
char temp[14];
|
|
|
|
temp[13] = '\0';
|
2013-11-12 03:21:26 +00:00
|
|
|
|
2013-12-08 20:02:37 +00:00
|
|
|
for (u32 i = 0; i < FILE_LIST_COUNT_MAX; ++i) {
|
|
|
|
// Ends at a NULL filename.
|
|
|
|
if (i * FILE_LIST_ITEM_SIZE >= sfoFileListSize || sfoFileList[i * FILE_LIST_ITEM_SIZE] == '\0') {
|
|
|
|
break;
|
2013-11-12 03:21:26 +00:00
|
|
|
}
|
2013-12-08 20:02:37 +00:00
|
|
|
|
|
|
|
strncpy(temp, &sfoFileList[i * FILE_LIST_ITEM_SIZE], 13);
|
|
|
|
secureFileNames.insert(temp);
|
|
|
|
}
|
|
|
|
return secureFileNames;
|
2013-11-12 03:21:26 +00:00
|
|
|
}
|
|
|
|
|
2013-11-03 15:52:16 +00:00
|
|
|
void SavedataParam::LoadFile(const std::string dirPath, const std::string filename, PspUtilitySavedataFileData *fileData) {
|
|
|
|
std::string filePath = dirPath + "/" + filename;
|
|
|
|
s64 readSize = -1;
|
2013-11-03 16:13:00 +00:00
|
|
|
if(!fileData->buf.IsValid())
|
|
|
|
return;
|
2013-11-03 15:52:16 +00:00
|
|
|
u8 *buf = fileData->buf;
|
|
|
|
if(ReadPSPFile(filePath, &buf, fileData->bufSize, &readSize))
|
|
|
|
fileData->size = readSize;
|
|
|
|
}
|
|
|
|
|
2013-01-28 23:11:02 +00:00
|
|
|
int SavedataParam::EncryptData(unsigned int mode,
|
|
|
|
unsigned char *data,
|
|
|
|
int *dataLen,
|
|
|
|
int *alignedLen,
|
|
|
|
unsigned char *hash,
|
|
|
|
unsigned char *cryptkey)
|
|
|
|
{
|
|
|
|
pspChnnlsvContext1 ctx1;
|
|
|
|
pspChnnlsvContext2 ctx2;
|
|
|
|
|
|
|
|
/* Make room for the IV in front of the data. */
|
|
|
|
memmove(data + 0x10, data, *alignedLen);
|
|
|
|
|
|
|
|
/* Set up buffers */
|
|
|
|
memset(&ctx1, 0, sizeof(pspChnnlsvContext1));
|
|
|
|
memset(&ctx2, 0, sizeof(pspChnnlsvContext2));
|
|
|
|
memset(hash, 0, 0x10);
|
|
|
|
memset(data, 0, 0x10);
|
|
|
|
|
|
|
|
/* Build the 0x10-byte IV and setup encryption */
|
|
|
|
if (sceSdCreateList_(ctx2, mode, 1, data, cryptkey) < 0)
|
|
|
|
return -1;
|
|
|
|
if (sceSdSetIndex_(ctx1, mode) < 0)
|
|
|
|
return -2;
|
|
|
|
if (sceSdRemoveValue_(ctx1, data, 0x10) < 0)
|
|
|
|
return -3;
|
|
|
|
if (sceSdSetMember_(ctx2, data + 0x10, *alignedLen) < 0)
|
|
|
|
return -4;
|
|
|
|
|
|
|
|
/* Clear any extra bytes left from the previous steps */
|
|
|
|
memset(data + 0x10 + *dataLen, 0, *alignedLen - *dataLen);
|
|
|
|
|
|
|
|
/* Encrypt the data */
|
|
|
|
if (sceSdRemoveValue_(ctx1, data + 0x10, *alignedLen) < 0)
|
|
|
|
return -5;
|
|
|
|
|
|
|
|
/* Verify encryption */
|
|
|
|
if (sceChnnlsv_21BE78B4_(ctx2) < 0)
|
|
|
|
return -6;
|
|
|
|
|
|
|
|
/* Build the file hash from this PSP */
|
|
|
|
if (sceSdGetLastIndex_(ctx1, hash, cryptkey) < 0)
|
|
|
|
return -7;
|
|
|
|
|
|
|
|
/* Adjust sizes to account for IV */
|
|
|
|
*alignedLen += 0x10;
|
|
|
|
*dataLen += 0x10;
|
|
|
|
|
|
|
|
/* All done */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SavedataParam::DecryptSave(unsigned int mode,
|
|
|
|
unsigned char *data,
|
|
|
|
int *dataLen,
|
|
|
|
int *alignedLen,
|
|
|
|
unsigned char *cryptkey)
|
|
|
|
{
|
|
|
|
|
|
|
|
pspChnnlsvContext1 ctx1;
|
|
|
|
pspChnnlsvContext2 ctx2;
|
|
|
|
|
|
|
|
/* Need a 16-byte IV plus some data */
|
|
|
|
if (*alignedLen <= 0x10)
|
|
|
|
return -1;
|
|
|
|
*dataLen -= 0x10;
|
|
|
|
*alignedLen -= 0x10;
|
|
|
|
|
|
|
|
/* Set up buffers */
|
|
|
|
memset(&ctx1, 0, sizeof(pspChnnlsvContext1));
|
|
|
|
memset(&ctx2, 0, sizeof(pspChnnlsvContext2));
|
|
|
|
|
|
|
|
/* Perform the magic */
|
|
|
|
if (sceSdSetIndex_(ctx1, mode) < 0)
|
|
|
|
return -2;
|
|
|
|
if (sceSdCreateList_(ctx2, mode, 2, data, cryptkey) < 0)
|
|
|
|
return -3;
|
|
|
|
if (sceSdRemoveValue_(ctx1, data, 0x10) < 0)
|
|
|
|
return -4;
|
|
|
|
if (sceSdRemoveValue_(ctx1, data + 0x10, *alignedLen) < 0)
|
|
|
|
return -5;
|
|
|
|
if (sceSdSetMember_(ctx2, data + 0x10, *alignedLen) < 0)
|
|
|
|
return -6;
|
|
|
|
|
|
|
|
/* Verify that it decrypted correctly */
|
|
|
|
if (sceChnnlsv_21BE78B4_(ctx2) < 0)
|
|
|
|
return -7;
|
|
|
|
|
|
|
|
/* The decrypted data starts at data + 0x10, so shift it back. */
|
|
|
|
memmove(data, data + 0x10, *dataLen);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SavedataParam::UpdateHash(u8* sfoData, int sfoSize, int sfoDataParamsOffset, int encryptmode)
|
|
|
|
{
|
|
|
|
int alignedLen = align16(sfoSize);
|
|
|
|
memset(sfoData+sfoDataParamsOffset, 0, 128);
|
|
|
|
u8 filehash[16];
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
/* Compute 11D0 hash over entire file */
|
|
|
|
if ((ret = BuildHash(filehash, sfoData, sfoSize, alignedLen, (encryptmode & 2) ? 4 : 2, NULL)) < 0)
|
|
|
|
{ // Not sure about "2"
|
|
|
|
return ret - 400;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy 11D0 hash to param.sfo and set flag indicating it's there */
|
|
|
|
memcpy(sfoData+sfoDataParamsOffset + 0x20, filehash, 0x10);
|
|
|
|
*(sfoData+sfoDataParamsOffset) |= 0x01;
|
|
|
|
|
|
|
|
/* If new encryption mode, compute and insert the 1220 hash. */
|
|
|
|
if (encryptmode & 2)
|
|
|
|
{
|
|
|
|
|
|
|
|
/* Enable the hash bit first */
|
|
|
|
*(sfoData+sfoDataParamsOffset) |= 0x20;
|
|
|
|
|
|
|
|
if ((ret = BuildHash(filehash, sfoData, sfoSize, alignedLen, 3, 0)) < 0)
|
|
|
|
{
|
|
|
|
return ret - 500;
|
|
|
|
}
|
|
|
|
memcpy(sfoData+sfoDataParamsOffset + 0x70, filehash, 0x10);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compute and insert the 11C0 hash. */
|
|
|
|
if ((ret = BuildHash(filehash, sfoData, sfoSize, alignedLen, 1, 0)) < 0)
|
|
|
|
{
|
|
|
|
return ret - 600;
|
|
|
|
}
|
|
|
|
memcpy(sfoData+sfoDataParamsOffset + 0x10, filehash, 0x10);
|
|
|
|
|
|
|
|
/* All done. */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SavedataParam::BuildHash(unsigned char *output,
|
|
|
|
unsigned char *data,
|
|
|
|
unsigned int len,
|
|
|
|
unsigned int alignedLen,
|
|
|
|
int mode,
|
|
|
|
unsigned char *cryptkey)
|
|
|
|
{
|
|
|
|
pspChnnlsvContext1 ctx1;
|
|
|
|
|
|
|
|
/* Set up buffers */
|
|
|
|
memset(&ctx1, 0, sizeof(pspChnnlsvContext1));
|
|
|
|
memset(output, 0, 0x10);
|
|
|
|
memset(data + len, 0, alignedLen - len);
|
|
|
|
|
|
|
|
/* Perform the magic */
|
|
|
|
if (sceSdSetIndex_(ctx1, mode & 0xFF) < 0)
|
|
|
|
return -1;
|
|
|
|
if (sceSdRemoveValue_(ctx1, data, alignedLen) < 0)
|
|
|
|
return -2;
|
|
|
|
if (sceSdGetLastIndex_(ctx1, output, cryptkey) < 0)
|
|
|
|
{
|
|
|
|
// Got here since Kirk CMD5 missing, return random value;
|
|
|
|
memset(output,0x1,0x10);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* All done. */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-12-19 15:57:22 +00:00
|
|
|
std::string SavedataParam::GetSpaceText(int size)
|
|
|
|
{
|
2012-12-19 17:15:02 +00:00
|
|
|
char text[50];
|
|
|
|
|
2012-12-19 15:57:22 +00:00
|
|
|
if(size < 1024)
|
2012-12-19 17:15:02 +00:00
|
|
|
{
|
|
|
|
sprintf(text,"%d B",size);
|
|
|
|
return std::string(text);
|
|
|
|
}
|
2012-12-19 15:57:22 +00:00
|
|
|
|
|
|
|
size /= 1024;
|
|
|
|
|
|
|
|
if(size < 1024)
|
2012-12-19 17:15:02 +00:00
|
|
|
{
|
|
|
|
sprintf(text,"%d KB",size);
|
|
|
|
return std::string(text);
|
|
|
|
}
|
2012-12-19 15:57:22 +00:00
|
|
|
|
|
|
|
size /= 1024;
|
|
|
|
|
|
|
|
if(size < 1024)
|
2012-12-19 17:15:02 +00:00
|
|
|
{
|
|
|
|
sprintf(text,"%d MB",size);
|
|
|
|
return std::string(text);
|
|
|
|
}
|
2012-12-19 15:57:22 +00:00
|
|
|
|
2012-12-19 17:15:02 +00:00
|
|
|
size /= 1024;
|
|
|
|
sprintf(text,"%d GB",size);
|
|
|
|
return std::string(text);
|
2012-12-19 15:57:22 +00:00
|
|
|
}
|
|
|
|
|
2013-06-23 18:14:13 +00:00
|
|
|
int SavedataParam::GetSizes(SceUtilitySavedataParam *param)
|
2012-12-11 02:09:52 +00:00
|
|
|
{
|
|
|
|
if (!param) {
|
2013-06-23 18:14:13 +00:00
|
|
|
return SCE_UTILITY_SAVEDATA_ERROR_SIZES_NO_DATA;
|
2012-12-11 02:09:52 +00:00
|
|
|
}
|
|
|
|
|
2013-06-23 18:14:13 +00:00
|
|
|
int ret = 0;
|
2012-12-24 21:28:10 +00:00
|
|
|
|
2013-06-25 13:51:39 +00:00
|
|
|
if (param->msFree.IsValid())
|
2012-12-11 02:09:52 +00:00
|
|
|
{
|
2013-06-23 18:06:54 +00:00
|
|
|
param->msFree->clusterSize = (u32)MemoryStick_SectorSize();
|
|
|
|
param->msFree->freeClusters = (u32)(MemoryStick_FreeSpace() / MemoryStick_SectorSize());
|
|
|
|
param->msFree->freeSpaceKB = (u32)(MemoryStick_FreeSpace() / 0x400);
|
2013-06-23 18:31:46 +00:00
|
|
|
const std::string spaceTxt = SavedataParam::GetSpaceText((int)MemoryStick_FreeSpace());
|
2013-06-23 18:06:54 +00:00
|
|
|
memset(param->msFree->freeSpaceStr, 0, sizeof(param->msFree->freeSpaceStr));
|
|
|
|
strncpy(param->msFree->freeSpaceStr, spaceTxt.c_str(), sizeof(param->msFree->freeSpaceStr));
|
2012-12-11 02:09:52 +00:00
|
|
|
}
|
2013-06-25 13:51:39 +00:00
|
|
|
if (param->msData.IsValid())
|
2012-12-11 02:09:52 +00:00
|
|
|
{
|
2013-06-23 18:31:46 +00:00
|
|
|
const std::string gameName(param->msData->gameName, strnlen(param->msData->gameName, sizeof(param->msData->gameName)));
|
|
|
|
const std::string saveName(param->msData->saveName, strnlen(param->msData->saveName, sizeof(param->msData->saveName)));
|
|
|
|
std::string path = GetSaveFilePath(param, gameName + saveName);
|
2012-12-19 15:57:22 +00:00
|
|
|
PSPFileInfo finfo = pspFileSystem.GetFileInfo(path);
|
|
|
|
if(finfo.exists)
|
|
|
|
{
|
2013-01-28 23:11:02 +00:00
|
|
|
// TODO : fill correctly with the total save size, be aware of crypted file size
|
2013-06-23 18:06:54 +00:00
|
|
|
param->msData->info.usedClusters = 1;
|
|
|
|
param->msData->info.usedSpaceKB = 0x20;
|
|
|
|
strncpy(param->msData->info.usedSpaceStr, "", sizeof(param->msData->info.usedSpaceStr)); // "32 KB" // 8 u8
|
|
|
|
param->msData->info.usedSpace32KB = 0x20;
|
|
|
|
strncpy(param->msData->info.usedSpace32Str, "", sizeof(param->msData->info.usedSpace32Str)); // "32 KB" // 8 u8
|
2012-12-19 15:57:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-23 18:06:54 +00:00
|
|
|
param->msData->info.usedClusters = 0;
|
|
|
|
param->msData->info.usedSpaceKB = 0;
|
|
|
|
strncpy(param->msData->info.usedSpaceStr, "", sizeof(param->msData->info.usedSpaceStr));
|
|
|
|
param->msData->info.usedSpace32KB = 0;
|
|
|
|
strncpy(param->msData->info.usedSpace32Str, "", sizeof(param->msData->info.usedSpace32Str));
|
2013-06-23 18:14:13 +00:00
|
|
|
ret = SCE_UTILITY_SAVEDATA_ERROR_SIZES_NO_DATA;
|
2012-12-19 15:57:22 +00:00
|
|
|
}
|
2012-12-11 02:09:52 +00:00
|
|
|
}
|
2013-06-25 13:51:39 +00:00
|
|
|
if (param->utilityData.IsValid())
|
2012-12-11 02:09:52 +00:00
|
|
|
{
|
2012-12-19 15:57:22 +00:00
|
|
|
int total_size = 0;
|
|
|
|
total_size += getSizeNormalized(1); // SFO;
|
|
|
|
total_size += getSizeNormalized(param->dataSize); // Save Data
|
|
|
|
total_size += getSizeNormalized(param->icon0FileData.size);
|
|
|
|
total_size += getSizeNormalized(param->icon1FileData.size);
|
|
|
|
total_size += getSizeNormalized(param->pic1FileData.size);
|
|
|
|
total_size += getSizeNormalized(param->snd0FileData.size);
|
|
|
|
|
2013-06-23 18:06:54 +00:00
|
|
|
param->utilityData->usedClusters = total_size / (u32)MemoryStick_SectorSize();
|
|
|
|
param->utilityData->usedSpaceKB = total_size / 0x400;
|
2012-12-19 15:57:22 +00:00
|
|
|
std::string spaceTxt = SavedataParam::GetSpaceText(total_size);
|
2013-06-23 18:06:54 +00:00
|
|
|
memset(param->utilityData->usedSpaceStr, 0, sizeof(param->utilityData->usedSpaceStr));
|
|
|
|
strncpy(param->utilityData->usedSpaceStr, spaceTxt.c_str(), sizeof(param->utilityData->usedSpaceStr));
|
|
|
|
|
|
|
|
// TODO: Maybe these are rounded to the nearest 32KB? Or something?
|
|
|
|
param->utilityData->usedSpace32KB = total_size / 0x400;
|
2012-12-19 15:57:22 +00:00
|
|
|
spaceTxt = SavedataParam::GetSpaceText(total_size);
|
2013-06-23 18:06:54 +00:00
|
|
|
memset(param->utilityData->usedSpace32Str, 0, sizeof(param->utilityData->usedSpace32Str));
|
|
|
|
strncpy(param->utilityData->usedSpace32Str, spaceTxt.c_str(), sizeof(param->utilityData->usedSpace32Str));
|
2012-12-11 02:09:52 +00:00
|
|
|
}
|
2012-12-24 21:28:10 +00:00
|
|
|
return ret;
|
2012-12-11 02:09:52 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-12-19 20:23:52 +00:00
|
|
|
bool SavedataParam::GetList(SceUtilitySavedataParam *param)
|
2012-12-11 02:09:52 +00:00
|
|
|
{
|
|
|
|
if (!param) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-25 13:51:39 +00:00
|
|
|
if (param->idList.IsValid())
|
2012-12-11 02:09:52 +00:00
|
|
|
{
|
2013-06-23 19:41:37 +00:00
|
|
|
u32 maxFile = param->idList->maxCount;
|
2012-12-29 22:55:34 +00:00
|
|
|
|
|
|
|
std::vector<PSPFileInfo> validDir;
|
2013-10-06 14:17:05 +00:00
|
|
|
std::vector<PSPFileInfo> sfoFiles;
|
2012-12-29 22:55:34 +00:00
|
|
|
std::vector<PSPFileInfo> allDir = pspFileSystem.GetDirListing(savePath);
|
|
|
|
|
2013-06-25 13:51:39 +00:00
|
|
|
if (param->idList.IsValid())
|
2012-12-29 22:55:34 +00:00
|
|
|
{
|
|
|
|
std::string searchString = GetGameName(param)+GetSaveName(param);
|
2013-04-16 07:47:47 +00:00
|
|
|
for (size_t i = 0; i < allDir.size() && validDir.size() < maxFile; i++)
|
2012-12-29 22:55:34 +00:00
|
|
|
{
|
|
|
|
std::string dirName = allDir[i].name;
|
|
|
|
if(PSPMatch(dirName, searchString))
|
|
|
|
{
|
|
|
|
validDir.push_back(allDir[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-06 14:17:05 +00:00
|
|
|
PSPFileInfo sfoFile;
|
|
|
|
for (size_t i = 0; i < validDir.size(); ++i) {
|
|
|
|
// GetFileName(param) == NUll here
|
|
|
|
// so use sfo files to set the date.
|
|
|
|
sfoFile = pspFileSystem.GetFileInfo(savePath + validDir[i].name + "/" + "PARAM.SFO");
|
|
|
|
sfoFiles.push_back(sfoFile);
|
|
|
|
}
|
|
|
|
|
2013-06-23 19:41:37 +00:00
|
|
|
SceUtilitySavedataIdListEntry *entries = param->idList->entries;
|
2013-01-19 21:48:20 +00:00
|
|
|
for (u32 i = 0; i < (u32)validDir.size(); i++)
|
2012-12-29 22:55:34 +00:00
|
|
|
{
|
2013-06-23 19:41:37 +00:00
|
|
|
entries[i].st_mode = 0x11FF;
|
2013-10-06 18:32:03 +00:00
|
|
|
if (sfoFiles[i].exists) {
|
|
|
|
__IoCopyDate(entries[i].st_ctime, sfoFiles[i].ctime);
|
|
|
|
__IoCopyDate(entries[i].st_atime, sfoFiles[i].atime);
|
|
|
|
__IoCopyDate(entries[i].st_mtime, sfoFiles[i].mtime);
|
|
|
|
} else {
|
|
|
|
__IoCopyDate(entries[i].st_ctime, validDir[i].ctime);
|
|
|
|
__IoCopyDate(entries[i].st_atime, validDir[i].atime);
|
|
|
|
__IoCopyDate(entries[i].st_mtime, validDir[i].mtime);
|
|
|
|
}
|
2012-12-29 22:55:34 +00:00
|
|
|
// folder name without gamename (max 20 u8)
|
|
|
|
std::string outName = validDir[i].name.substr(GetGameName(param).size());
|
2013-06-23 19:41:37 +00:00
|
|
|
memset(entries[i].name, 0, sizeof(entries[i].name));
|
|
|
|
strncpy(entries[i].name, outName.c_str(), sizeof(entries[i].name));
|
2012-12-29 22:55:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Save num of folder found
|
2013-06-23 19:41:37 +00:00
|
|
|
param->idList->resultCount = (u32)validDir.size();
|
2012-12-11 02:09:52 +00:00
|
|
|
}
|
2012-12-11 09:54:13 +00:00
|
|
|
return true;
|
2012-12-11 02:09:52 +00:00
|
|
|
}
|
|
|
|
|
2013-05-31 06:32:13 +00:00
|
|
|
int SavedataParam::GetFilesList(SceUtilitySavedataParam *param)
|
2012-12-31 18:08:46 +00:00
|
|
|
{
|
2013-05-25 15:57:59 +00:00
|
|
|
if (!param) {
|
2013-05-31 06:32:13 +00:00
|
|
|
return SCE_UTILITY_SAVEDATA_ERROR_RW_BAD_STATUS;
|
2012-12-31 18:08:46 +00:00
|
|
|
}
|
2013-05-31 06:32:13 +00:00
|
|
|
|
2013-06-25 13:51:39 +00:00
|
|
|
if (!param->fileList.IsValid()) {
|
2013-09-07 20:02:55 +00:00
|
|
|
ERROR_LOG_REPORT(SCEUTILITY, "SavedataParam::GetFilesList(): bad fileList address %08x", param->fileList.ptr);
|
2013-05-31 06:32:13 +00:00
|
|
|
// Should crash.
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-06-02 22:17:02 +00:00
|
|
|
auto &fileList = param->fileList;
|
2013-06-25 13:51:39 +00:00
|
|
|
if (fileList->secureEntries.IsValid() && fileList->maxSecureEntries > 99) {
|
2013-09-07 20:02:55 +00:00
|
|
|
ERROR_LOG_REPORT(SCEUTILITY, "SavedataParam::GetFilesList(): too many secure entries, %d", fileList->maxSecureEntries);
|
2013-05-31 06:32:13 +00:00
|
|
|
return SCE_UTILITY_SAVEDATA_ERROR_RW_BAD_PARAMS;
|
|
|
|
}
|
2013-06-25 13:51:39 +00:00
|
|
|
if (fileList->normalEntries.IsValid() && fileList->maxNormalEntries > 8192) {
|
2013-09-07 20:02:55 +00:00
|
|
|
ERROR_LOG_REPORT(SCEUTILITY, "SavedataParam::GetFilesList(): too many normal entries, %d", fileList->maxNormalEntries);
|
2013-05-31 06:32:13 +00:00
|
|
|
return SCE_UTILITY_SAVEDATA_ERROR_RW_BAD_PARAMS;
|
|
|
|
}
|
2013-06-23 20:21:41 +00:00
|
|
|
// TODO: This may depend on sdk version or something? Not returned by default.
|
2013-06-25 13:51:39 +00:00
|
|
|
if (false && fileList->systemEntries.IsValid() && fileList->maxSystemEntries > 5) {
|
2013-09-07 20:02:55 +00:00
|
|
|
ERROR_LOG_REPORT(SCEUTILITY, "SavedataParam::GetFilesList(): too many system entries, %d", fileList->maxSystemEntries);
|
2013-05-31 06:32:13 +00:00
|
|
|
return SCE_UTILITY_SAVEDATA_ERROR_RW_BAD_PARAMS;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string dirPath = savePath + GetGameName(param) + GetSaveName(param);
|
|
|
|
if (!pspFileSystem.GetFileInfo(dirPath).exists) {
|
2013-09-07 20:02:55 +00:00
|
|
|
DEBUG_LOG(SCEUTILITY, "SavedataParam::GetFilesList(): directory %s does not exist", dirPath.c_str());
|
2013-05-31 06:32:13 +00:00
|
|
|
return SCE_UTILITY_SAVEDATA_ERROR_RW_NO_DATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Even if there are no files, initialize to 0.
|
|
|
|
fileList->resultNumSecureEntries = 0;
|
|
|
|
fileList->resultNumNormalEntries = 0;
|
|
|
|
fileList->resultNumSystemEntries = 0;
|
|
|
|
|
2013-05-31 06:47:33 +00:00
|
|
|
// We need PARAMS.SFO's SAVEDATA_FILE_LIST to determine which entries are secure.
|
2013-05-31 06:55:49 +00:00
|
|
|
PSPFileInfo sfoFileInfo = pspFileSystem.GetFileInfo(dirPath + "/" + SFO_FILENAME);
|
2013-05-31 07:47:38 +00:00
|
|
|
std::set<std::string> secureFilenames;
|
2013-05-31 06:47:33 +00:00
|
|
|
// TODO: Error code if not?
|
|
|
|
if (sfoFileInfo.exists) {
|
2013-11-12 03:21:26 +00:00
|
|
|
secureFilenames = getSecureFileNames(dirPath);
|
2013-05-31 06:47:33 +00:00
|
|
|
}
|
|
|
|
|
2013-05-31 07:47:38 +00:00
|
|
|
// Does not list directories, nor recurse into them, and ignores files not ALL UPPERCASE.
|
|
|
|
auto files = pspFileSystem.GetDirListing(dirPath);
|
|
|
|
for (auto file = files.begin(), end = files.end(); file != end; ++file) {
|
|
|
|
if (file->type == FILETYPE_DIRECTORY) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// TODO: What are the exact rules? It definitely skips lowercase, and allows FILE or FILE.EXT.
|
|
|
|
if (file->name.find_first_of("abcdefghijklmnopqrstuvwxyz") != file->name.npos) {
|
2013-09-07 20:02:55 +00:00
|
|
|
DEBUG_LOG(SCEUTILITY, "SavedataParam::GetFilesList(): skipping file %s with lowercase", file->name.c_str());
|
2013-05-31 07:47:38 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isSystemFile = file->name == ICON0_FILENAME || file->name == ICON1_FILENAME || file->name == PIC1_FILENAME;
|
|
|
|
isSystemFile = isSystemFile || file->name == SND0_FILENAME || file->name == SFO_FILENAME;
|
|
|
|
|
|
|
|
SceUtilitySavedataFileListEntry *entry = NULL;
|
|
|
|
int sizeOffset = 0;
|
|
|
|
if (isSystemFile) {
|
2013-06-25 13:51:39 +00:00
|
|
|
if (fileList->systemEntries.IsValid() && fileList->resultNumSystemEntries < fileList->maxSystemEntries) {
|
2013-05-31 07:47:38 +00:00
|
|
|
entry = &fileList->systemEntries[fileList->resultNumSystemEntries++];
|
|
|
|
}
|
|
|
|
} else if (secureFilenames.find(file->name) != secureFilenames.end()) {
|
2013-06-25 13:51:39 +00:00
|
|
|
if (fileList->secureEntries.IsValid() && fileList->resultNumSecureEntries < fileList->maxSecureEntries) {
|
2013-05-31 07:47:38 +00:00
|
|
|
entry = &fileList->secureEntries[fileList->resultNumSecureEntries++];
|
|
|
|
}
|
|
|
|
// Secure files are slightly bigger.
|
|
|
|
bool isCrypted = IsSaveEncrypted(param, GetSaveDirName(param, 0));
|
|
|
|
if (isCrypted) {
|
|
|
|
sizeOffset = -0x10;
|
|
|
|
}
|
|
|
|
} else {
|
2013-06-25 13:51:39 +00:00
|
|
|
if (fileList->normalEntries.IsValid() && fileList->resultNumNormalEntries < fileList->maxNormalEntries) {
|
2013-05-31 07:47:38 +00:00
|
|
|
entry = &fileList->normalEntries[fileList->resultNumNormalEntries++];
|
2013-05-25 15:57:59 +00:00
|
|
|
}
|
2012-12-31 18:08:46 +00:00
|
|
|
}
|
2013-05-31 07:47:38 +00:00
|
|
|
|
|
|
|
// Out of space for this file in the list.
|
|
|
|
if (entry == NULL) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
entry->st_mode = 0x21FF;
|
|
|
|
entry->st_size = file->size + sizeOffset;
|
2013-09-10 09:09:18 +00:00
|
|
|
__IoCopyDate(entry->st_ctime, file->ctime);
|
|
|
|
__IoCopyDate(entry->st_atime, file->atime);
|
|
|
|
__IoCopyDate(entry->st_mtime, file->mtime);
|
2013-05-31 07:47:38 +00:00
|
|
|
// TODO: Probably actually 13 + 3 pad...
|
|
|
|
strncpy(entry->name, file->name.c_str(), 16);
|
|
|
|
entry->name[15] = '\0';
|
2012-12-31 18:08:46 +00:00
|
|
|
}
|
|
|
|
|
2013-06-23 20:21:41 +00:00
|
|
|
// TODO: Does this always happen?
|
|
|
|
// Don't know what it is, but PSP always respond this
|
|
|
|
param->bind = 1021;
|
|
|
|
|
2013-05-31 06:32:13 +00:00
|
|
|
return 0;
|
2012-12-31 18:08:46 +00:00
|
|
|
}
|
|
|
|
|
2013-01-10 06:33:05 +00:00
|
|
|
bool SavedataParam::GetSize(SceUtilitySavedataParam *param)
|
2012-12-31 18:08:46 +00:00
|
|
|
{
|
|
|
|
if (!param)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-03-18 00:47:37 +00:00
|
|
|
std::string saveDir = savePath + GetGameName(param) + GetSaveName(param);
|
|
|
|
PSPFileInfo info = pspFileSystem.GetFileInfo(saveDir);
|
|
|
|
bool exists = info.exists;
|
2012-12-31 18:08:46 +00:00
|
|
|
|
2013-06-25 13:51:39 +00:00
|
|
|
if (param->sizeInfo.IsValid())
|
2013-03-18 00:47:37 +00:00
|
|
|
{
|
|
|
|
// TODO: Read the entries and count up the size vs. existing size?
|
|
|
|
|
2013-06-23 20:56:18 +00:00
|
|
|
param->sizeInfo->sectorSize = (int)MemoryStick_SectorSize();
|
|
|
|
param->sizeInfo->freeSectors = (int)(MemoryStick_FreeSpace() / MemoryStick_SectorSize());
|
2013-03-18 00:47:37 +00:00
|
|
|
|
|
|
|
// TODO: Is this after the specified files? Before?
|
2013-06-23 20:56:18 +00:00
|
|
|
param->sizeInfo->freeKB = (int)(MemoryStick_FreeSpace() / 1024);
|
2013-03-18 00:47:37 +00:00
|
|
|
std::string spaceTxt = SavedataParam::GetSpaceText((int)MemoryStick_FreeSpace());
|
2013-06-23 20:56:18 +00:00
|
|
|
strncpy(param->sizeInfo->freeString, spaceTxt.c_str(), 8);
|
|
|
|
param->sizeInfo->freeString[7] = '\0';
|
2013-03-18 00:47:37 +00:00
|
|
|
|
|
|
|
// TODO.
|
2013-06-23 20:56:18 +00:00
|
|
|
param->sizeInfo->neededKB = 0;
|
|
|
|
strcpy(param->sizeInfo->neededString, "0 KB");
|
|
|
|
param->sizeInfo->overwriteKB = 0;
|
|
|
|
strcpy(param->sizeInfo->overwriteString, "0 KB");
|
2013-03-18 00:47:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return exists;
|
2012-12-31 18:08:46 +00:00
|
|
|
}
|
|
|
|
|
2012-12-13 21:06:45 +00:00
|
|
|
void SavedataParam::Clear()
|
|
|
|
{
|
2012-12-19 20:23:52 +00:00
|
|
|
if (saveDataList)
|
2012-12-13 21:06:45 +00:00
|
|
|
{
|
2012-12-19 20:23:52 +00:00
|
|
|
for (int i = 0; i < saveNameListDataCount; i++)
|
2012-12-13 21:06:45 +00:00
|
|
|
{
|
2013-12-08 22:46:05 +00:00
|
|
|
if (saveDataList[i].texture != NULL && (!noSaveIcon || saveDataList[i].texture != noSaveIcon->texture))
|
2013-12-08 19:06:18 +00:00
|
|
|
delete saveDataList[i].texture;
|
|
|
|
saveDataList[i].texture = NULL;
|
2012-12-13 21:06:45 +00:00
|
|
|
}
|
|
|
|
|
2013-12-08 19:06:18 +00:00
|
|
|
delete [] saveDataList;
|
2012-12-13 21:06:45 +00:00
|
|
|
saveDataList = 0;
|
2012-12-29 20:55:58 +00:00
|
|
|
saveDataListCount = 0;
|
2012-12-13 21:06:45 +00:00
|
|
|
}
|
2013-05-12 02:41:51 +00:00
|
|
|
if (noSaveIcon)
|
2013-01-29 21:46:20 +00:00
|
|
|
{
|
2013-12-08 19:06:18 +00:00
|
|
|
if (noSaveIcon->texture != NULL)
|
|
|
|
delete noSaveIcon->texture;
|
|
|
|
noSaveIcon->texture = NULL;
|
2013-01-29 21:46:20 +00:00
|
|
|
delete noSaveIcon;
|
|
|
|
noSaveIcon = 0;
|
|
|
|
}
|
2012-12-13 21:06:45 +00:00
|
|
|
}
|
|
|
|
|
2012-12-25 02:37:28 +00:00
|
|
|
int SavedataParam::SetPspParam(SceUtilitySavedataParam *param)
|
2012-12-10 12:08:54 +00:00
|
|
|
{
|
|
|
|
pspParam = param;
|
2012-12-19 20:23:52 +00:00
|
|
|
if (!pspParam)
|
2012-12-13 21:06:45 +00:00
|
|
|
{
|
|
|
|
Clear();
|
2012-12-19 20:23:52 +00:00
|
|
|
return 0;
|
2012-12-13 21:06:45 +00:00
|
|
|
}
|
2012-12-10 12:08:54 +00:00
|
|
|
|
|
|
|
bool listEmptyFile = true;
|
2012-12-19 20:23:52 +00:00
|
|
|
if (param->mode == SCE_UTILITY_SAVEDATA_TYPE_LISTLOAD ||
|
2012-12-10 12:08:54 +00:00
|
|
|
param->mode == SCE_UTILITY_SAVEDATA_TYPE_LISTDELETE)
|
|
|
|
{
|
|
|
|
listEmptyFile = false;
|
|
|
|
}
|
|
|
|
|
2013-06-23 05:49:39 +00:00
|
|
|
SceUtilitySavedataSaveName *saveNameListData;
|
2013-01-06 00:29:14 +00:00
|
|
|
bool hasMultipleFileName = false;
|
2013-06-25 13:51:39 +00:00
|
|
|
if (param->saveNameList.IsValid())
|
2012-12-10 12:08:54 +00:00
|
|
|
{
|
2012-12-31 23:39:49 +00:00
|
|
|
Clear();
|
|
|
|
|
2013-06-23 05:49:39 +00:00
|
|
|
saveNameListData = param->saveNameList;
|
2012-12-10 12:08:54 +00:00
|
|
|
|
|
|
|
// Get number of fileName in array
|
2012-12-28 21:36:37 +00:00
|
|
|
saveDataListCount = 0;
|
2013-05-19 13:11:54 +00:00
|
|
|
while (saveNameListData[saveDataListCount][0] != 0)
|
2012-12-10 12:08:54 +00:00
|
|
|
{
|
2012-12-28 21:36:37 +00:00
|
|
|
saveDataListCount++;
|
2013-01-06 00:29:14 +00:00
|
|
|
}
|
2012-12-10 12:08:54 +00:00
|
|
|
|
2013-05-19 13:11:54 +00:00
|
|
|
if (saveDataListCount > 0)
|
2012-12-10 12:08:54 +00:00
|
|
|
{
|
2013-01-06 00:29:14 +00:00
|
|
|
hasMultipleFileName = true;
|
|
|
|
saveDataList = new SaveFileInfo[saveDataListCount];
|
2013-10-27 11:03:14 +00:00
|
|
|
|
2013-01-06 00:29:14 +00:00
|
|
|
// get and stock file info for each file
|
|
|
|
int realCount = 0;
|
2013-10-27 11:03:14 +00:00
|
|
|
for (int i = 0; i < saveDataListCount; i++) {
|
|
|
|
// "<>" means saveName can be anything...
|
2014-01-24 09:21:13 +00:00
|
|
|
if (strncmp(saveNameListData[i], "<>", ARRAY_SIZE(saveNameListData[i])) == 0) {
|
2013-10-28 17:41:48 +00:00
|
|
|
std::string fileDataPath = "";
|
2013-10-28 18:14:39 +00:00
|
|
|
// TODO:Maybe we need a way to reorder the files?
|
2013-10-27 11:03:14 +00:00
|
|
|
auto allSaves = pspFileSystem.GetDirListing(savePath);
|
|
|
|
std::string gameName = GetGameName(param);
|
|
|
|
std::string saveName = "";
|
|
|
|
for(auto it = allSaves.begin(); it != allSaves.end(); ++it) {
|
|
|
|
if(strncmp(it->name.c_str(),gameName.c_str(),strlen(gameName.c_str())) == 0) {
|
2013-10-27 16:07:32 +00:00
|
|
|
saveName = it->name.substr(strlen(gameName.c_str()));
|
|
|
|
|
|
|
|
if(IsInSaveDataList(saveName, realCount)) // Already in SaveDataList, skip...
|
|
|
|
continue;
|
|
|
|
|
2014-01-24 09:21:13 +00:00
|
|
|
fileDataPath = savePath + it->name + "/" + GetFileName(param);
|
2013-10-27 11:03:14 +00:00
|
|
|
PSPFileInfo info = pspFileSystem.GetFileInfo(fileDataPath);
|
|
|
|
if (info.exists) {
|
|
|
|
SetFileInfo(realCount, info, saveName);
|
|
|
|
DEBUG_LOG(SCEUTILITY,"%s Exist",fileDataPath.c_str());
|
|
|
|
++realCount;
|
|
|
|
} else {
|
|
|
|
if (listEmptyFile) {
|
2013-10-28 17:41:48 +00:00
|
|
|
// If file doesn't exist,we only skip...
|
|
|
|
continue;
|
2013-10-27 11:03:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-05-31 07:57:25 +00:00
|
|
|
continue;
|
2013-10-27 11:03:14 +00:00
|
|
|
}
|
2013-05-31 07:57:25 +00:00
|
|
|
|
2014-01-24 09:21:13 +00:00
|
|
|
const std::string thisSaveName = FixedToString(saveNameListData[i], ARRAY_SIZE(saveNameListData[i]));
|
|
|
|
DEBUG_LOG(SCEUTILITY, "Name : %s", thisSaveName.c_str());
|
2012-12-14 22:08:56 +00:00
|
|
|
|
2014-01-24 09:21:13 +00:00
|
|
|
std::string fileDataPath = savePath + GetGameName(param) + thisSaveName + "/" + GetFileName(param);
|
2013-01-06 00:29:14 +00:00
|
|
|
PSPFileInfo info = pspFileSystem.GetFileInfo(fileDataPath);
|
|
|
|
if (info.exists)
|
2012-12-10 12:08:54 +00:00
|
|
|
{
|
2014-01-24 09:21:13 +00:00
|
|
|
SetFileInfo(realCount, info, thisSaveName);
|
2013-01-06 00:29:14 +00:00
|
|
|
|
2013-09-07 20:02:55 +00:00
|
|
|
DEBUG_LOG(SCEUTILITY,"%s Exist",fileDataPath.c_str());
|
2012-12-10 12:08:54 +00:00
|
|
|
realCount++;
|
|
|
|
}
|
2013-01-06 00:29:14 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (listEmptyFile)
|
|
|
|
{
|
2014-01-24 09:21:13 +00:00
|
|
|
ClearFileInfo(saveDataList[realCount], thisSaveName);
|
2013-09-07 20:02:55 +00:00
|
|
|
DEBUG_LOG(SCEUTILITY,"Don't Exist");
|
2013-01-06 00:29:14 +00:00
|
|
|
realCount++;
|
|
|
|
}
|
|
|
|
}
|
2012-12-10 12:08:54 +00:00
|
|
|
}
|
2013-01-06 00:29:14 +00:00
|
|
|
saveNameListDataCount = realCount;
|
2012-12-10 12:08:54 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-19 13:11:54 +00:00
|
|
|
if (!hasMultipleFileName) // Load info on only save
|
2012-12-16 11:51:02 +00:00
|
|
|
{
|
2012-12-18 21:27:59 +00:00
|
|
|
saveNameListData = 0;
|
2012-12-16 11:51:02 +00:00
|
|
|
|
|
|
|
Clear();
|
|
|
|
saveDataList = new SaveFileInfo[1];
|
2012-12-29 10:56:27 +00:00
|
|
|
saveDataListCount = 1;
|
2012-12-16 11:51:02 +00:00
|
|
|
|
|
|
|
// get and stock file info for each file
|
2013-09-07 20:02:55 +00:00
|
|
|
DEBUG_LOG(SCEUTILITY,"Name : %s",GetSaveName(param).c_str());
|
2012-12-16 11:51:02 +00:00
|
|
|
|
2014-01-24 09:21:13 +00:00
|
|
|
std::string fileDataPath = savePath + GetGameName(param) + GetSaveName(param) + "/" + GetFileName(param);
|
2012-12-16 11:51:02 +00:00
|
|
|
PSPFileInfo info = pspFileSystem.GetFileInfo(fileDataPath);
|
2012-12-19 20:23:52 +00:00
|
|
|
if (info.exists)
|
2012-12-16 11:51:02 +00:00
|
|
|
{
|
2013-05-12 02:41:51 +00:00
|
|
|
SetFileInfo(0, info, GetSaveName(param));
|
2012-12-16 11:51:02 +00:00
|
|
|
|
2013-09-07 20:02:55 +00:00
|
|
|
DEBUG_LOG(SCEUTILITY,"%s Exist",fileDataPath.c_str());
|
2012-12-16 11:51:02 +00:00
|
|
|
saveNameListDataCount = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-12-19 20:23:52 +00:00
|
|
|
if (listEmptyFile)
|
2012-12-16 11:51:02 +00:00
|
|
|
{
|
2013-05-12 02:41:51 +00:00
|
|
|
ClearFileInfo(saveDataList[0], GetSaveName(param));
|
2013-09-07 20:02:55 +00:00
|
|
|
DEBUG_LOG(SCEUTILITY,"Don't Exist");
|
2012-12-16 11:51:02 +00:00
|
|
|
}
|
|
|
|
saveNameListDataCount = 0;
|
2012-12-19 20:23:52 +00:00
|
|
|
return 0;
|
2012-12-16 11:51:02 +00:00
|
|
|
}
|
|
|
|
}
|
2012-12-19 20:23:52 +00:00
|
|
|
return 0;
|
2012-12-10 12:08:54 +00:00
|
|
|
}
|
|
|
|
|
2013-05-12 02:41:51 +00:00
|
|
|
void SavedataParam::SetFileInfo(SaveFileInfo &saveInfo, PSPFileInfo &info, std::string saveName)
|
2012-12-25 01:00:05 +00:00
|
|
|
{
|
2013-05-12 02:41:51 +00:00
|
|
|
saveInfo.size = info.size;
|
|
|
|
saveInfo.saveName = saveName;
|
|
|
|
saveInfo.idx = 0;
|
|
|
|
saveInfo.modif_time = info.mtime;
|
2012-12-25 01:00:05 +00:00
|
|
|
|
2012-12-25 09:36:51 +00:00
|
|
|
// Start with a blank slate.
|
2013-12-08 19:06:18 +00:00
|
|
|
if (saveInfo.texture != NULL) {
|
|
|
|
if (!noSaveIcon || saveInfo.texture != noSaveIcon->texture) {
|
|
|
|
delete saveInfo.texture;
|
|
|
|
}
|
|
|
|
saveInfo.texture = NULL;
|
|
|
|
}
|
2013-05-12 02:41:51 +00:00
|
|
|
saveInfo.title[0] = 0;
|
|
|
|
saveInfo.saveTitle[0] = 0;
|
|
|
|
saveInfo.saveDetail[0] = 0;
|
2012-12-25 09:36:51 +00:00
|
|
|
|
2012-12-25 01:00:05 +00:00
|
|
|
// Search save image icon0
|
|
|
|
// TODO : If icon0 don't exist, need to use icon1 which is a moving icon. Also play sound
|
2013-05-31 06:55:49 +00:00
|
|
|
std::string fileDataPath2 = savePath + GetGameName(pspParam) + saveName + "/" + ICON0_FILENAME;
|
2012-12-25 01:00:05 +00:00
|
|
|
PSPFileInfo info2 = pspFileSystem.GetFileInfo(fileDataPath2);
|
|
|
|
if (info2.exists)
|
2013-12-08 19:06:18 +00:00
|
|
|
saveInfo.texture = new PPGeImage(fileDataPath2);
|
2012-12-25 01:00:05 +00:00
|
|
|
|
|
|
|
// Load info in PARAM.SFO
|
2013-05-31 06:55:49 +00:00
|
|
|
fileDataPath2 = savePath + GetGameName(pspParam) + saveName + "/" + SFO_FILENAME;
|
2012-12-25 01:00:05 +00:00
|
|
|
info2 = pspFileSystem.GetFileInfo(fileDataPath2);
|
|
|
|
if (info2.exists)
|
|
|
|
{
|
2013-12-08 20:02:37 +00:00
|
|
|
std::vector<u8> sfoData;
|
|
|
|
pspFileSystem.ReadEntireFile(fileDataPath2, sfoData);
|
2012-12-25 01:00:05 +00:00
|
|
|
ParamSFOData sfoFile;
|
2013-12-08 20:02:37 +00:00
|
|
|
if (sfoFile.ReadSFO(sfoData))
|
2012-12-25 01:00:05 +00:00
|
|
|
{
|
2013-05-12 02:41:51 +00:00
|
|
|
SetStringFromSFO(sfoFile, "TITLE", saveInfo.title, sizeof(saveInfo.title));
|
|
|
|
SetStringFromSFO(sfoFile, "SAVEDATA_TITLE", saveInfo.saveTitle, sizeof(saveInfo.saveTitle));
|
|
|
|
SetStringFromSFO(sfoFile, "SAVEDATA_DETAIL", saveInfo.saveDetail, sizeof(saveInfo.saveDetail));
|
2012-12-25 01:00:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-12 02:41:51 +00:00
|
|
|
void SavedataParam::SetFileInfo(int idx, PSPFileInfo &info, std::string saveName)
|
|
|
|
{
|
|
|
|
SetFileInfo(saveDataList[idx], info, saveName);
|
|
|
|
saveDataList[idx].idx = idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SavedataParam::ClearFileInfo(SaveFileInfo &saveInfo, std::string saveName)
|
|
|
|
{
|
|
|
|
saveInfo.size = 0;
|
|
|
|
saveInfo.saveName = saveName;
|
|
|
|
saveInfo.idx = 0;
|
2013-12-08 19:06:18 +00:00
|
|
|
if (saveInfo.texture != NULL) {
|
|
|
|
if (!noSaveIcon || saveInfo.texture != noSaveIcon->texture) {
|
|
|
|
delete saveInfo.texture;
|
|
|
|
}
|
|
|
|
saveInfo.texture = NULL;
|
|
|
|
}
|
2013-05-12 02:41:51 +00:00
|
|
|
|
2013-06-25 13:51:39 +00:00
|
|
|
if (GetPspParam()->newData.IsValid() && GetPspParam()->newData->buf.IsValid())
|
2013-05-12 02:41:51 +00:00
|
|
|
{
|
|
|
|
// We have a png to show
|
|
|
|
if (!noSaveIcon)
|
|
|
|
{
|
|
|
|
noSaveIcon = new SaveFileInfo();
|
2013-06-23 05:40:48 +00:00
|
|
|
PspUtilitySavedataFileData *newData = GetPspParam()->newData;
|
2013-12-08 19:06:18 +00:00
|
|
|
noSaveIcon->texture = new PPGeImage(newData->buf.ptr, (SceSize)newData->size);
|
2013-05-12 02:41:51 +00:00
|
|
|
}
|
2013-12-08 19:06:18 +00:00
|
|
|
saveInfo.texture = noSaveIcon->texture;
|
2013-05-12 02:41:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-24 09:21:13 +00:00
|
|
|
SceUtilitySavedataParam *SavedataParam::GetPspParam()
|
|
|
|
{
|
|
|
|
return pspParam;
|
|
|
|
}
|
|
|
|
|
|
|
|
const SceUtilitySavedataParam *SavedataParam::GetPspParam() const
|
2012-12-10 12:08:54 +00:00
|
|
|
{
|
|
|
|
return pspParam;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SavedataParam::GetFilenameCount()
|
|
|
|
{
|
|
|
|
return saveNameListDataCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
const SaveFileInfo& SavedataParam::GetFileInfo(int idx)
|
|
|
|
{
|
|
|
|
return saveDataList[idx];
|
|
|
|
}
|
2014-01-24 09:21:13 +00:00
|
|
|
std::string SavedataParam::GetFilename(int idx) const
|
2012-12-10 12:08:54 +00:00
|
|
|
{
|
2012-12-16 11:51:02 +00:00
|
|
|
return saveDataList[idx].saveName;
|
2012-12-10 12:08:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int SavedataParam::GetSelectedSave()
|
|
|
|
{
|
2013-05-19 13:11:54 +00:00
|
|
|
// The slot # of the same save on LOAD/SAVE lists can dismatch so this isn't right anyhow
|
|
|
|
return selectedSave < saveNameListDataCount ? selectedSave : 0;
|
2012-12-10 12:08:54 +00:00
|
|
|
}
|
2012-12-19 20:23:52 +00:00
|
|
|
|
2012-12-10 12:08:54 +00:00
|
|
|
void SavedataParam::SetSelectedSave(int idx)
|
|
|
|
{
|
|
|
|
selectedSave = idx;
|
|
|
|
}
|
2012-12-28 21:36:37 +00:00
|
|
|
|
2013-05-20 11:03:58 +00:00
|
|
|
int SavedataParam::GetFirstListSave()
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SavedataParam::GetLastListSave()
|
|
|
|
{
|
2013-10-23 06:07:56 +00:00
|
|
|
return saveNameListDataCount - 1;
|
2013-05-20 11:03:58 +00:00
|
|
|
}
|
|
|
|
|
2013-05-19 13:11:54 +00:00
|
|
|
int SavedataParam::GetLatestSave()
|
|
|
|
{
|
|
|
|
int idx = 0;
|
2013-05-20 11:03:58 +00:00
|
|
|
time_t idxTime = 0;
|
2013-10-23 06:07:56 +00:00
|
|
|
for (int i = 0; i < saveNameListDataCount; ++i)
|
2013-05-20 11:03:58 +00:00
|
|
|
{
|
2013-10-23 13:25:25 +00:00
|
|
|
if (saveDataList[i].size == 0)
|
|
|
|
continue;
|
2013-05-20 11:03:58 +00:00
|
|
|
time_t thisTime = mktime(&saveDataList[i].modif_time);
|
2013-10-23 13:25:25 +00:00
|
|
|
if ((s64)idxTime < (s64)thisTime)
|
2013-05-20 11:03:58 +00:00
|
|
|
{
|
|
|
|
idx = i;
|
|
|
|
idxTime = thisTime;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SavedataParam::GetOldestSave()
|
|
|
|
{
|
|
|
|
int idx = 0;
|
|
|
|
time_t idxTime = 0;
|
2013-10-23 06:07:56 +00:00
|
|
|
for (int i = 0; i < saveNameListDataCount; ++i)
|
2013-05-19 13:11:54 +00:00
|
|
|
{
|
2013-10-23 13:25:25 +00:00
|
|
|
if (saveDataList[i].size == 0)
|
|
|
|
continue;
|
2013-05-19 13:11:54 +00:00
|
|
|
time_t thisTime = mktime(&saveDataList[i].modif_time);
|
2013-10-23 13:25:25 +00:00
|
|
|
if ((s64)idxTime > (s64)thisTime)
|
2013-05-20 11:03:58 +00:00
|
|
|
{
|
|
|
|
idx = i;
|
|
|
|
idxTime = thisTime;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SavedataParam::GetFirstDataSave()
|
|
|
|
{
|
|
|
|
int idx = 0;
|
2013-10-23 06:07:56 +00:00
|
|
|
for (int i = 0; i < saveNameListDataCount; ++i)
|
2013-05-20 11:03:58 +00:00
|
|
|
{
|
|
|
|
if (saveDataList[i].size != 0)
|
|
|
|
{
|
2013-06-10 05:48:35 +00:00
|
|
|
idx = i;
|
2013-05-20 11:03:58 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SavedataParam::GetLastDataSave()
|
|
|
|
{
|
|
|
|
int idx = 0;
|
2013-10-23 06:07:56 +00:00
|
|
|
for (int i = saveNameListDataCount; i > 0; )
|
2013-05-20 11:03:58 +00:00
|
|
|
{
|
|
|
|
--i;
|
|
|
|
if (saveDataList[i].size != 0)
|
|
|
|
{
|
|
|
|
idx = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SavedataParam::GetFirstEmptySave()
|
|
|
|
{
|
|
|
|
int idx = 0;
|
2013-10-23 06:07:56 +00:00
|
|
|
for (int i = 0; i < saveNameListDataCount; ++i)
|
2013-05-20 11:03:58 +00:00
|
|
|
{
|
|
|
|
if (saveDataList[i].size == 0)
|
|
|
|
{
|
|
|
|
idx = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SavedataParam::GetLastEmptySave()
|
|
|
|
{
|
|
|
|
int idx = 0;
|
2013-10-23 06:07:56 +00:00
|
|
|
for (int i = saveNameListDataCount; i > 0; )
|
2013-05-20 11:03:58 +00:00
|
|
|
{
|
|
|
|
--i;
|
|
|
|
if (saveDataList[i].size == 0)
|
2013-05-19 13:11:54 +00:00
|
|
|
{
|
|
|
|
idx = i;
|
2013-05-20 11:03:58 +00:00
|
|
|
break;
|
2013-05-19 13:11:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
2013-11-12 09:10:56 +00:00
|
|
|
int SavedataParam::GetSaveNameIndex(SceUtilitySavedataParam* param)
|
|
|
|
{
|
|
|
|
std::string saveName = GetSaveName(param);
|
|
|
|
for (int i = 0; i < saveNameListDataCount; i++)
|
|
|
|
{
|
|
|
|
// TODO: saveName may contain wildcards
|
|
|
|
if (saveDataList[i].saveName == saveName)
|
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-12-28 21:36:37 +00:00
|
|
|
void SavedataParam::DoState(PointerWrap &p)
|
|
|
|
{
|
2013-09-15 03:23:03 +00:00
|
|
|
auto s = p.Section("SavedataParam", 1);
|
|
|
|
if (!s)
|
|
|
|
return;
|
|
|
|
|
2012-12-28 21:36:37 +00:00
|
|
|
// pspParam is handled in PSPSaveDialog.
|
|
|
|
p.Do(selectedSave);
|
|
|
|
p.Do(saveDataListCount);
|
|
|
|
p.Do(saveNameListDataCount);
|
2012-12-29 10:56:27 +00:00
|
|
|
if (p.mode == p.MODE_READ)
|
|
|
|
{
|
|
|
|
if (saveDataList != NULL)
|
|
|
|
delete [] saveDataList;
|
2013-02-04 08:34:37 +00:00
|
|
|
if (saveDataListCount != 0)
|
|
|
|
{
|
|
|
|
saveDataList = new SaveFileInfo[saveDataListCount];
|
|
|
|
p.DoArray(saveDataList, saveDataListCount);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
saveDataList = NULL;
|
2012-12-29 10:56:27 +00:00
|
|
|
}
|
2013-02-04 08:34:37 +00:00
|
|
|
else
|
|
|
|
p.DoArray(saveDataList, saveDataListCount);
|
2012-12-28 21:36:37 +00:00
|
|
|
}
|
2013-01-28 23:11:02 +00:00
|
|
|
|
2013-05-12 03:27:54 +00:00
|
|
|
bool SavedataParam::IsSaveEncrypted(SceUtilitySavedataParam* param, const std::string &saveDirName)
|
2013-01-28 23:11:02 +00:00
|
|
|
{
|
|
|
|
bool isCrypted = false;
|
|
|
|
|
|
|
|
ParamSFOData sfoFile;
|
2013-05-12 03:27:54 +00:00
|
|
|
std::string dirPath = GetSaveFilePath(param, GetSaveDir(param, saveDirName));
|
2013-05-31 06:55:49 +00:00
|
|
|
std::string sfopath = dirPath + "/" + SFO_FILENAME;
|
2013-01-28 23:11:02 +00:00
|
|
|
PSPFileInfo sfoInfo = pspFileSystem.GetFileInfo(sfopath);
|
|
|
|
if(sfoInfo.exists) // Read sfo
|
|
|
|
{
|
2013-12-08 20:02:37 +00:00
|
|
|
std::vector<u8> sfoData;
|
|
|
|
if (pspFileSystem.ReadEntireFile(sfopath, sfoData) >= 0)
|
2013-01-28 23:11:02 +00:00
|
|
|
{
|
2013-12-08 20:02:37 +00:00
|
|
|
sfoFile.ReadSFO(sfoData);
|
2013-01-28 23:11:02 +00:00
|
|
|
|
|
|
|
// save created in PPSSPP and not encrypted has '0' in SAVEDATA_PARAMS
|
|
|
|
u32 tmpDataSize = 0;
|
|
|
|
u8* tmpDataOrig = sfoFile.GetValueData("SAVEDATA_PARAMS", &tmpDataSize);
|
2013-01-31 07:54:26 +00:00
|
|
|
for(u32 i = 0; i < tmpDataSize; i++)
|
2013-01-28 23:11:02 +00:00
|
|
|
{
|
|
|
|
if(tmpDataOrig[i] != 0)
|
|
|
|
{
|
|
|
|
isCrypted = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return isCrypted;
|
|
|
|
}
|
|
|
|
|
2013-10-27 16:07:32 +00:00
|
|
|
bool SavedataParam::IsInSaveDataList(std::string saveName, int count) {
|
|
|
|
for(int i = 0; i < count; ++i) {
|
|
|
|
if(strcmp(saveDataList[i].saveName.c_str(),saveName.c_str()) == 0)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2013-11-12 03:21:26 +00:00
|
|
|
|
2013-11-12 04:14:16 +00:00
|
|
|
bool SavedataParam::secureCanSkip(SceUtilitySavedataParam* param, bool secureMode) {
|
2013-11-14 14:49:26 +00:00
|
|
|
if (!secureMode) // Only check in secure mode.
|
2013-11-12 04:08:11 +00:00
|
|
|
return false;
|
2013-11-12 03:21:26 +00:00
|
|
|
std::string dirPath = savePath + GetGameName(param) + GetSaveName(param);
|
|
|
|
std::string sfoPath = dirPath + "/" + SFO_FILENAME;
|
|
|
|
std::string secureFileName = GetFileName(param);
|
|
|
|
std::set<std::string> secureFileNames;
|
|
|
|
PSPFileInfo sfoInfo = pspFileSystem.GetFileInfo(sfoPath);
|
2013-11-14 14:49:26 +00:00
|
|
|
// If sfo doesn't exist,shouldn't skip.
|
|
|
|
if (!sfoInfo.exists)
|
2013-11-12 03:21:26 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Get secure file names from PARAM.SFO.
|
|
|
|
secureFileNames = getSecureFileNames(dirPath);
|
|
|
|
// Secure file name should be saved in PARAM.SFO
|
2013-11-12 04:14:16 +00:00
|
|
|
// Cannot find name in PARAM.SFO, could skip.
|
2013-11-14 14:49:26 +00:00
|
|
|
if (secureFileNames.find(secureFileName) == secureFileNames.end())
|
2013-11-12 03:21:26 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
2013-11-14 22:02:21 +00:00
|
|
|
}
|