Play-/Source/saves/Save.cpp

125 lines
2.5 KiB
C++
Raw Permalink Normal View History

#include <exception>
#include "string_cast_sjis.h"
#include "Save.h"
#include "StdStream.h"
#include "StdStreamUtils.h"
#include "FilesystemUtils.h"
2019-10-16 20:51:11 -04:00
CSave::CSave(const fs::path& basePath)
2018-04-30 21:01:23 +01:00
: m_basePath(basePath)
{
2019-10-16 20:51:11 -04:00
auto iconSysPath = m_basePath / "icon.sys";
auto iconStream(Framework::CreateInputStdStream(iconSysPath.native()));
uint32 nMagic = iconStream.Read32();
if(nMagic != 0x44325350)
{
throw std::runtime_error("Invalid 'icon.sys' file.");
}
iconStream.Seek(6, Framework::STREAM_SEEK_SET);
m_nSecondLineStartPosition = iconStream.Read16();
iconStream.Seek(192, Framework::STREAM_SEEK_SET);
ReadName(iconStream);
char sBuffer[64];
iconStream.Read(sBuffer, 64);
m_sNormalIconFileName = sBuffer;
iconStream.Read(sBuffer, 64);
m_sCopyingIconFileName = sBuffer;
iconStream.Read(sBuffer, 64);
m_sDeletingIconFileName = sBuffer;
m_sId = m_basePath.filename().string();
m_nLastModificationTime = Framework::ConvertFsTimeToSystemTime(fs::last_write_time(iconSysPath));
}
const wchar_t* CSave::GetName() const
{
return m_sName.c_str();
}
const char* CSave::GetId() const
{
return m_sId.c_str();
}
unsigned int CSave::GetSize() const
{
2019-10-16 20:51:11 -04:00
fs::directory_iterator itEnd;
unsigned int nSize = 0;
2019-10-16 20:51:11 -04:00
for(fs::directory_iterator itElement(m_basePath);
2018-04-30 21:01:23 +01:00
itElement != itEnd;
itElement++)
{
2019-10-16 20:51:11 -04:00
if(!fs::is_directory(*itElement))
{
2019-10-16 20:51:11 -04:00
nSize += fs::file_size(*itElement);
}
}
return nSize;
}
2019-10-16 20:51:11 -04:00
fs::path CSave::GetPath() const
{
return m_basePath;
}
2019-10-16 20:51:11 -04:00
fs::path CSave::GetIconPath(const ICONTYPE& iconType) const
{
2019-10-16 20:51:11 -04:00
fs::path iconPath;
switch(iconType)
{
case ICON_NORMAL:
iconPath = GetNormalIconPath();
break;
case ICON_DELETING:
iconPath = GetDeletingIconPath();
break;
case ICON_COPYING:
iconPath = GetCopyingIconPath();
break;
}
return iconPath;
}
2019-10-16 20:51:11 -04:00
fs::path CSave::GetNormalIconPath() const
{
return m_basePath / m_sNormalIconFileName;
}
2019-10-16 20:51:11 -04:00
fs::path CSave::GetDeletingIconPath() const
{
return m_basePath / m_sDeletingIconFileName;
}
2019-10-16 20:51:11 -04:00
fs::path CSave::GetCopyingIconPath() const
{
return m_basePath / m_sCopyingIconFileName;
}
size_t CSave::GetSecondLineStartPosition() const
{
return std::min<size_t>(m_nSecondLineStartPosition / 2, m_sName.length());
}
time_t CSave::GetLastModificationTime() const
{
return m_nLastModificationTime;
}
void CSave::ReadName(Framework::CStream& inputStream)
{
char sNameSJIS[68];
inputStream.Read(sNameSJIS, 68);
m_sName = string_cast_sjis(sNameSJIS);
}