2019-08-17 13:51:31 -04:00
|
|
|
#include <cstring>
|
2006-07-23 22:52:33 +00:00
|
|
|
#include "SaveExporter.h"
|
2011-12-30 22:49:52 +00:00
|
|
|
#include "StdStream.h"
|
|
|
|
#include "StdStreamUtils.h"
|
2006-07-23 22:52:33 +00:00
|
|
|
|
2019-10-16 20:51:11 -04:00
|
|
|
void CSaveExporter::ExportPSU(Framework::CStream& outputStream, const fs::path& savePath)
|
2006-07-23 22:52:33 +00:00
|
|
|
{
|
|
|
|
uint32 nSector = 0x18D;
|
|
|
|
|
2019-10-16 20:51:11 -04:00
|
|
|
auto baseTime = fs::last_write_time(savePath);
|
2006-07-23 22:52:33 +00:00
|
|
|
|
2011-12-30 22:49:52 +00:00
|
|
|
//Save base directory entry
|
2006-07-23 22:52:33 +00:00
|
|
|
{
|
2006-07-24 04:44:19 +00:00
|
|
|
PSUENTRY Entry;
|
|
|
|
memset(&Entry, 0, sizeof(PSUENTRY));
|
2006-07-23 22:52:33 +00:00
|
|
|
|
|
|
|
Entry.nSize = 2;
|
|
|
|
|
2019-10-16 20:51:11 -04:00
|
|
|
const fs::directory_iterator itEnd;
|
|
|
|
for(fs::directory_iterator itElement(savePath);
|
2018-04-30 21:01:23 +01:00
|
|
|
itElement != itEnd;
|
|
|
|
itElement++)
|
2006-07-23 22:52:33 +00:00
|
|
|
{
|
2019-10-16 20:51:11 -04:00
|
|
|
if(fs::is_directory(*itElement)) continue;
|
2006-07-23 22:52:33 +00:00
|
|
|
Entry.nSize++;
|
|
|
|
}
|
|
|
|
|
2018-04-30 21:01:23 +01:00
|
|
|
Entry.nFlags = 0x8427;
|
|
|
|
Entry.nSector = nSector;
|
2006-07-23 22:52:33 +00:00
|
|
|
|
2018-04-30 21:01:23 +01:00
|
|
|
PSU_CopyTime(&Entry.CreationTime, baseTime);
|
|
|
|
PSU_CopyTime(&Entry.ModificationTime, baseTime);
|
2006-07-23 22:52:33 +00:00
|
|
|
|
2011-12-30 22:49:52 +00:00
|
|
|
strncpy(reinterpret_cast<char*>(Entry.sName), savePath.filename().string().c_str(), 0x1C0);
|
2006-07-23 22:52:33 +00:00
|
|
|
|
2011-12-30 22:49:52 +00:00
|
|
|
outputStream.Write(&Entry, sizeof(PSUENTRY));
|
2006-07-23 22:52:33 +00:00
|
|
|
}
|
|
|
|
|
2018-04-30 21:01:23 +01:00
|
|
|
static const char* sRelDirName[2] =
|
|
|
|
{
|
|
|
|
".",
|
|
|
|
"..",
|
|
|
|
};
|
2006-07-23 22:52:33 +00:00
|
|
|
|
|
|
|
//Save "relative" directories
|
|
|
|
for(unsigned int i = 0; i < 2; i++)
|
|
|
|
{
|
2006-07-24 04:44:19 +00:00
|
|
|
PSUENTRY Entry;
|
|
|
|
memset(&Entry, 0, sizeof(PSUENTRY));
|
2006-07-23 22:52:33 +00:00
|
|
|
|
2018-04-30 21:01:23 +01:00
|
|
|
Entry.nFlags = 0x8427;
|
|
|
|
Entry.nSector = 0;
|
2006-07-23 22:52:33 +00:00
|
|
|
|
2018-04-30 21:01:23 +01:00
|
|
|
PSU_CopyTime(&Entry.CreationTime, baseTime);
|
|
|
|
PSU_CopyTime(&Entry.ModificationTime, baseTime);
|
2006-07-23 22:52:33 +00:00
|
|
|
|
|
|
|
strncpy(reinterpret_cast<char*>(Entry.sName), sRelDirName[i], 0x1C0);
|
|
|
|
|
2011-12-30 22:49:52 +00:00
|
|
|
outputStream.Write(&Entry, sizeof(PSUENTRY));
|
2006-07-23 22:52:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nSector += 2;
|
|
|
|
|
|
|
|
//Save individual file entries
|
2019-10-16 20:51:11 -04:00
|
|
|
const fs::directory_iterator itEnd;
|
|
|
|
for(fs::directory_iterator itElement(savePath);
|
2018-04-30 21:01:23 +01:00
|
|
|
itElement != itEnd;
|
|
|
|
itElement++)
|
2006-07-23 22:52:33 +00:00
|
|
|
{
|
2019-10-16 20:51:11 -04:00
|
|
|
if(fs::is_directory(*itElement)) continue;
|
2011-04-08 02:38:32 +00:00
|
|
|
|
2019-10-16 20:51:11 -04:00
|
|
|
fs::path saveItemPath(*itElement);
|
2006-07-24 04:44:19 +00:00
|
|
|
|
2019-10-16 20:51:11 -04:00
|
|
|
auto itemTime = fs::last_write_time(saveItemPath);
|
2006-07-23 22:52:33 +00:00
|
|
|
|
2011-12-30 22:49:52 +00:00
|
|
|
PSUENTRY Entry;
|
2006-07-24 04:44:19 +00:00
|
|
|
memset(&Entry, 0, sizeof(PSUENTRY));
|
2006-07-23 22:52:33 +00:00
|
|
|
|
2018-04-30 21:01:23 +01:00
|
|
|
Entry.nFlags = 0x8497;
|
2019-10-16 20:51:11 -04:00
|
|
|
Entry.nSize = static_cast<uint32>(fs::file_size(saveItemPath));
|
2018-04-30 21:01:23 +01:00
|
|
|
Entry.nSector = nSector;
|
2006-07-23 22:52:33 +00:00
|
|
|
|
2018-04-30 21:01:23 +01:00
|
|
|
PSU_CopyTime(&Entry.CreationTime, itemTime);
|
|
|
|
PSU_CopyTime(&Entry.ModificationTime, itemTime);
|
2006-07-23 22:52:33 +00:00
|
|
|
|
2011-12-30 22:49:52 +00:00
|
|
|
strncpy(reinterpret_cast<char*>(Entry.sName), saveItemPath.filename().string().c_str(), 0x1C0);
|
2006-07-23 22:52:33 +00:00
|
|
|
|
2011-12-30 22:49:52 +00:00
|
|
|
outputStream.Write(&Entry, sizeof(PSUENTRY));
|
2006-07-23 22:52:33 +00:00
|
|
|
|
|
|
|
//Write file contents
|
|
|
|
{
|
2012-09-06 06:32:15 +00:00
|
|
|
auto itemStream(Framework::CreateInputStdStream(saveItemPath.native()));
|
2011-12-30 22:49:52 +00:00
|
|
|
uint32 nSize = Entry.nSize;
|
2006-07-23 22:52:33 +00:00
|
|
|
|
|
|
|
while(nSize != 0)
|
|
|
|
{
|
2011-12-30 22:49:52 +00:00
|
|
|
const uint32 bufferSize = 0x1000;
|
|
|
|
char sBuffer[bufferSize];
|
2006-07-23 22:52:33 +00:00
|
|
|
|
2011-12-30 22:49:52 +00:00
|
|
|
uint32 nRead = std::min<uint32>(nSize, bufferSize);
|
2012-09-06 06:32:15 +00:00
|
|
|
itemStream.Read(sBuffer, nRead);
|
2011-12-30 22:49:52 +00:00
|
|
|
outputStream.Write(sBuffer, nRead);
|
2006-07-23 22:52:33 +00:00
|
|
|
nSize -= nRead;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Align on 0x400 boundary
|
|
|
|
{
|
|
|
|
char sBuffer[0x400];
|
|
|
|
|
2011-12-30 22:49:52 +00:00
|
|
|
unsigned int nPosition = Entry.nSize;
|
2006-07-23 22:52:33 +00:00
|
|
|
memset(sBuffer, 0, 0x400);
|
2018-04-30 21:01:23 +01:00
|
|
|
|
2011-12-30 22:49:52 +00:00
|
|
|
outputStream.Write(sBuffer, 0x400 - (nPosition & 0x3FF));
|
2006-07-23 22:52:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nSector += (Entry.nSize + 0x3FF) / 0x400;
|
|
|
|
}
|
|
|
|
}
|
2006-07-24 04:44:19 +00:00
|
|
|
|
2019-10-16 20:51:11 -04:00
|
|
|
void CSaveExporter::PSU_CopyTime(PSUENTRY::TIME* pDst, const fs::file_time_type& fileTime)
|
2006-07-24 04:44:19 +00:00
|
|
|
{
|
2019-10-16 20:51:11 -04:00
|
|
|
auto systemTime = std::chrono::system_clock::now() + (fileTime - fs::file_time_type::clock::now());
|
|
|
|
auto tm = std::chrono::system_clock::to_time_t(systemTime);
|
|
|
|
const auto pSrc = std::localtime(&tm);
|
|
|
|
pDst->nSecond = static_cast<uint8>(pSrc->tm_sec);
|
|
|
|
pDst->nMinute = static_cast<uint8>(pSrc->tm_min);
|
|
|
|
pDst->nHour = static_cast<uint8>(pSrc->tm_hour);
|
|
|
|
pDst->nDay = static_cast<uint8>(pSrc->tm_mday);
|
|
|
|
pDst->nMonth = static_cast<uint8>(pSrc->tm_mon);
|
|
|
|
pDst->nYear = pSrc->tm_year;
|
2006-07-24 04:44:19 +00:00
|
|
|
}
|