2016-01-28 03:02:05 +00:00
|
|
|
#include "stdafx.h"
|
2017-07-30 16:32:50 +00:00
|
|
|
#include "Mempak.h"
|
2016-02-04 19:16:46 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
2016-01-28 03:02:05 +00:00
|
|
|
#include <Common/path.h>
|
|
|
|
|
2017-01-20 23:11:14 +00:00
|
|
|
CMempak::CMempak()
|
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < sizeof(m_Formatted) / sizeof(m_Formatted[0]); i++)
|
|
|
|
{
|
|
|
|
m_Formatted[i] = 0;
|
2017-01-22 08:59:26 +00:00
|
|
|
m_SaveExists[i] = true;
|
|
|
|
}
|
2017-01-20 23:11:14 +00:00
|
|
|
memset(m_Mempaks, 0, sizeof(m_Mempaks));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMempak::LoadMempak(int32_t Control, bool Create)
|
2016-01-28 03:02:05 +00:00
|
|
|
{
|
|
|
|
stdstr MempakName;
|
2016-02-09 07:57:30 +00:00
|
|
|
MempakName.Format("%s_Cont_%d", g_Settings->LoadStringVal(Game_GameName).c_str(), Control + 1);
|
2016-01-28 03:02:05 +00:00
|
|
|
|
2016-07-07 11:51:06 +00:00
|
|
|
CPath MempakPath(g_Settings->LoadStringVal(Directory_NativeSave).c_str(), stdstr_f("%s.mpk", MempakName.c_str()).c_str());
|
2016-04-21 20:29:55 +00:00
|
|
|
if (g_Settings->LoadBool(Setting_UniqueSaveDir))
|
|
|
|
{
|
2016-05-16 08:23:31 +00:00
|
|
|
MempakPath.AppendDirectory(g_Settings->LoadStringVal(Game_UniqueSaveDir).c_str());
|
2016-04-21 20:29:55 +00:00
|
|
|
}
|
2018-11-20 03:41:32 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
MempakPath.NormalizePath(CPath(CPath::MODULE_DIRECTORY));
|
|
|
|
#endif
|
2017-01-20 23:11:14 +00:00
|
|
|
|
|
|
|
if (!Create && !MempakPath.Exists())
|
|
|
|
{
|
|
|
|
if (!m_Formatted[Control])
|
|
|
|
{
|
|
|
|
CMempak::Format(Control);
|
|
|
|
m_Formatted[Control] = true;
|
|
|
|
}
|
2017-01-22 08:59:26 +00:00
|
|
|
m_SaveExists[Control] = false;
|
2017-01-20 23:11:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-16 08:23:31 +00:00
|
|
|
if (!MempakPath.DirectoryExists())
|
2016-02-09 07:57:30 +00:00
|
|
|
{
|
2016-05-16 08:23:31 +00:00
|
|
|
MempakPath.DirectoryCreate();
|
2016-02-09 07:57:30 +00:00
|
|
|
}
|
2016-01-28 03:02:05 +00:00
|
|
|
|
2016-05-16 08:23:31 +00:00
|
|
|
bool formatMempak = !MempakPath.Exists();
|
|
|
|
|
2016-07-07 11:51:06 +00:00
|
|
|
m_MempakHandle[Control].Open(MempakPath, CFileBase::modeReadWrite | CFileBase::modeNoTruncate | CFileBase::modeCreate);
|
|
|
|
m_MempakHandle[Control].SeekToBegin();
|
2016-05-16 08:23:31 +00:00
|
|
|
|
|
|
|
if (formatMempak)
|
2016-02-09 07:57:30 +00:00
|
|
|
{
|
2017-01-20 23:11:14 +00:00
|
|
|
if (!m_Formatted[Control])
|
|
|
|
{
|
|
|
|
CMempak::Format(Control);
|
|
|
|
m_Formatted[Control] = true;
|
|
|
|
}
|
2016-07-07 11:51:06 +00:00
|
|
|
m_MempakHandle[Control].Write(m_Mempaks[Control], 0x8000);
|
2016-02-09 07:57:30 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-07-07 11:51:06 +00:00
|
|
|
m_MempakHandle[Control].Read(m_Mempaks[Control], 0x8000);
|
2017-01-22 08:59:26 +00:00
|
|
|
m_Formatted[Control] = true;
|
2016-01-28 03:02:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-07 11:51:06 +00:00
|
|
|
void CMempak::Format(int32_t Control)
|
2016-01-28 03:02:05 +00:00
|
|
|
{
|
2016-02-04 19:34:07 +00:00
|
|
|
static const uint8_t Initialize[] = {
|
2020-05-09 07:00:06 +00:00
|
|
|
0x81, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,
|
|
|
|
0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
|
|
|
|
0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0xFF, 0xFF, 0xFF, 0xFF,
|
|
|
|
0x05, 0x1A, 0x5F, 0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0xFF,
|
|
|
|
0x66, 0x25, 0x99, 0xCD, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0xFF, 0xFF, 0xFF, 0xFF, 0x05, 0x1A, 0x5F, 0x13, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
|
|
|
0xFF, 0xFF, 0x01, 0xFF, 0x66, 0x25, 0x99, 0xCD, 0xFF, 0xFF, 0xFF, 0xFF,
|
|
|
|
0x05, 0x1A, 0x5F, 0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0xFF,
|
|
|
|
0x66, 0x25, 0x99, 0xCD, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0xFF, 0xFF, 0xFF, 0xFF, 0x05, 0x1A, 0x5F, 0x13, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
|
|
|
0xFF, 0xFF, 0x01, 0xFF, 0x66, 0x25, 0x99, 0xCD, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x71, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03,
|
|
|
|
0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03,
|
|
|
|
0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03,
|
|
|
|
0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03,
|
|
|
|
0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03,
|
|
|
|
0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03,
|
|
|
|
0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03,
|
|
|
|
0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03,
|
|
|
|
0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03,
|
|
|
|
0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03,
|
|
|
|
0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03,
|
|
|
|
0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03,
|
|
|
|
0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03,
|
|
|
|
0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03,
|
|
|
|
0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03,
|
|
|
|
0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03,
|
|
|
|
0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03,
|
|
|
|
0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03
|
2016-01-28 03:02:05 +00:00
|
|
|
};
|
|
|
|
|
2016-07-07 11:51:06 +00:00
|
|
|
memcpy(&m_Mempaks[Control][0], &Initialize[0], sizeof(Initialize));
|
2016-01-28 03:02:05 +00:00
|
|
|
|
2020-05-09 07:00:06 +00:00
|
|
|
for (size_t count = sizeof(Initialize); count < 128 * 256; count++)
|
2016-01-28 03:02:05 +00:00
|
|
|
{
|
2020-05-09 07:00:06 +00:00
|
|
|
m_Mempaks[Control][count] = 0x00;
|
2016-01-28 03:02:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-07 11:51:06 +00:00
|
|
|
uint8_t CMempak::CalculateCrc(uint8_t * DataToCrc)
|
2016-01-28 03:02:05 +00:00
|
|
|
{
|
|
|
|
uint32_t Count;
|
|
|
|
uint32_t XorTap;
|
|
|
|
|
|
|
|
int32_t Length;
|
|
|
|
uint8_t CRC = 0;
|
|
|
|
|
|
|
|
for (Count = 0; Count < 0x21; Count++)
|
|
|
|
{
|
|
|
|
for (Length = 0x80; Length >= 1; Length >>= 1)
|
|
|
|
{
|
|
|
|
XorTap = (CRC & 0x80) ? 0x85 : 0x00;
|
|
|
|
CRC <<= 1;
|
|
|
|
if (Count == 0x20)
|
|
|
|
{
|
|
|
|
CRC &= 0xFF;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ((*DataToCrc & Length) != 0)
|
|
|
|
{
|
|
|
|
CRC |= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CRC ^= XorTap;
|
|
|
|
}
|
|
|
|
DataToCrc++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CRC;
|
|
|
|
}
|
|
|
|
|
2016-07-07 11:51:06 +00:00
|
|
|
void CMempak::ReadFrom(int32_t Control, uint32_t address, uint8_t * data)
|
2016-01-28 03:02:05 +00:00
|
|
|
{
|
|
|
|
if (address < 0x8000)
|
|
|
|
{
|
2017-01-22 08:59:26 +00:00
|
|
|
if (m_SaveExists[Control] && !m_MempakHandle[Control].IsOpen())
|
2016-05-16 08:23:31 +00:00
|
|
|
{
|
2017-01-20 23:11:14 +00:00
|
|
|
LoadMempak(Control, false);
|
2016-05-16 08:23:31 +00:00
|
|
|
}
|
|
|
|
|
2016-07-07 11:51:06 +00:00
|
|
|
memcpy(data, &m_Mempaks[Control][address], 0x20);
|
2016-01-28 03:02:05 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-01-28 11:51:48 +00:00
|
|
|
memset(data, 0x00, 0x20);
|
2021-05-18 11:51:36 +00:00
|
|
|
// Rumble pack area
|
2016-01-28 03:02:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-07 11:51:06 +00:00
|
|
|
void CMempak::WriteTo(int32_t Control, uint32_t address, uint8_t * data)
|
2016-01-28 03:02:05 +00:00
|
|
|
{
|
|
|
|
if (address < 0x8000)
|
|
|
|
{
|
2017-01-20 23:11:14 +00:00
|
|
|
if (!m_Formatted[Control])
|
2016-05-16 08:23:31 +00:00
|
|
|
{
|
2017-01-20 23:11:14 +00:00
|
|
|
CMempak::Format(Control);
|
|
|
|
m_Formatted[Control] = true;
|
2016-05-16 08:23:31 +00:00
|
|
|
}
|
2017-01-20 23:11:14 +00:00
|
|
|
if (memcmp(&m_Mempaks[Control][address], data, 0x20) != 0)
|
|
|
|
{
|
|
|
|
if (!m_MempakHandle[Control].IsOpen())
|
|
|
|
{
|
|
|
|
LoadMempak(Control, true);
|
|
|
|
}
|
|
|
|
memcpy(&m_Mempaks[Control][address], data, 0x20);
|
2016-05-16 08:23:31 +00:00
|
|
|
|
2017-01-20 23:11:14 +00:00
|
|
|
m_MempakHandle[Control].Seek(address, CFile::begin);
|
|
|
|
m_MempakHandle[Control].Write(data, 0x20);
|
|
|
|
}
|
2016-01-28 03:02:05 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-05-18 11:51:36 +00:00
|
|
|
// Rumble pack area
|
2016-01-28 03:02:05 +00:00
|
|
|
}
|
2016-07-07 11:51:06 +00:00
|
|
|
}
|