2021-01-02 22:26:46 -05:00
|
|
|
#include "HardDiskDevice.h"
|
|
|
|
#include <cassert>
|
2021-01-05 12:35:28 -05:00
|
|
|
#include "AppConfig.h"
|
2021-01-08 11:46:10 -05:00
|
|
|
#include "PathUtils.h"
|
2021-01-05 17:25:38 -05:00
|
|
|
#include "PS2VM_Preferences.h"
|
2021-01-02 22:26:46 -05:00
|
|
|
#include "StringUtils.h"
|
2023-01-03 21:18:18 -05:00
|
|
|
#include "PathDirectoryDevice.h"
|
2022-02-25 17:32:56 -05:00
|
|
|
#include "PathDirectoryIterator.h"
|
2021-01-02 22:26:46 -05:00
|
|
|
|
|
|
|
using namespace Iop::Ioman;
|
|
|
|
|
2021-01-05 12:35:28 -05:00
|
|
|
CHardDiskDevice::CHardDiskDevice()
|
|
|
|
{
|
|
|
|
m_basePath = CAppConfig::GetInstance().GetPreferencePath(PREF_PS2_HDD_DIRECTORY);
|
2021-01-08 11:46:10 -05:00
|
|
|
//Seems that FFXI needs the "__common" partition to exist and also needs the "Your Saves" directory
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Framework::PathUtils::EnsurePathExists(m_basePath / "__common/Your Saves");
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
//We failed to create it, let's not crash for nothing
|
|
|
|
}
|
2021-01-05 12:35:28 -05:00
|
|
|
}
|
|
|
|
|
2021-01-02 22:26:46 -05:00
|
|
|
Framework::CStream* CHardDiskDevice::GetFile(uint32 accessType, const char* devicePath)
|
|
|
|
{
|
2021-01-05 17:19:49 -05:00
|
|
|
if(accessType & OPEN_FLAG_CREAT)
|
2021-01-02 22:26:46 -05:00
|
|
|
{
|
|
|
|
//Create a new partition
|
|
|
|
auto createParams = StringUtils::Split(devicePath, ',', true);
|
|
|
|
assert(createParams.size() == 5);
|
2021-01-05 17:19:49 -05:00
|
|
|
CreatePartition(createParams);
|
2021-01-02 22:26:46 -05:00
|
|
|
return new CHardDiskPartition();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-05 17:19:49 -05:00
|
|
|
auto openParams = StringUtils::Split(devicePath, ',', true);
|
|
|
|
assert(openParams.size() >= 2);
|
|
|
|
auto partitionPath = m_basePath / openParams[0];
|
|
|
|
if(fs::exists(partitionPath))
|
|
|
|
{
|
|
|
|
return new CHardDiskPartition();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
2021-01-02 22:26:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-25 17:32:56 -05:00
|
|
|
DirectoryIteratorPtr CHardDiskDevice::GetDirectory(const char* devicePath)
|
2021-01-02 22:26:46 -05:00
|
|
|
{
|
2022-02-25 17:32:56 -05:00
|
|
|
return std::make_unique<CPathDirectoryIterator>(m_basePath);
|
2021-01-02 22:26:46 -05:00
|
|
|
}
|
|
|
|
|
2023-01-03 21:18:18 -05:00
|
|
|
DevicePtr CHardDiskDevice::Mount(const char* devicePath)
|
2021-01-05 17:19:49 -05:00
|
|
|
{
|
|
|
|
auto mountParams = StringUtils::Split(devicePath, ',', true);
|
2021-01-06 13:33:36 -05:00
|
|
|
assert(!mountParams.empty());
|
2021-01-05 17:19:49 -05:00
|
|
|
auto partitionPath = m_basePath / mountParams[0];
|
|
|
|
if(!fs::exists(partitionPath))
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Partition doesn't exist.");
|
|
|
|
}
|
2023-01-03 21:18:18 -05:00
|
|
|
auto mountPath = m_basePath / mountParams[0];
|
|
|
|
return std::make_shared<CPathDirectoryDevice>(mountPath);
|
2021-01-05 17:19:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void CHardDiskDevice::CreatePartition(const std::vector<std::string>& createParams)
|
|
|
|
{
|
|
|
|
auto partitionName = createParams[0];
|
|
|
|
if(partitionName.empty())
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Invalid partition name.");
|
|
|
|
}
|
|
|
|
auto partitionPath = m_basePath / partitionName;
|
|
|
|
fs::create_directory(partitionPath);
|
|
|
|
}
|
|
|
|
|
2021-01-02 22:26:46 -05:00
|
|
|
//-----------------------------------------------
|
|
|
|
//CHardDiskPartition
|
|
|
|
|
|
|
|
void CHardDiskPartition::Seek(int64, Framework::STREAM_SEEK_DIRECTION)
|
|
|
|
{
|
2021-01-05 17:20:54 -05:00
|
|
|
//Used by FFX
|
2021-01-02 22:26:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64 CHardDiskPartition::Read(void*, uint64)
|
|
|
|
{
|
|
|
|
throw new std::runtime_error("Not supported.");
|
|
|
|
}
|
|
|
|
|
2021-01-05 17:20:54 -05:00
|
|
|
uint64 CHardDiskPartition::Write(const void*, uint64 size)
|
2021-01-02 22:26:46 -05:00
|
|
|
{
|
2021-01-05 17:20:54 -05:00
|
|
|
//FFX writes to partition (PS2ICON3D?)
|
|
|
|
return size;
|
2021-01-02 22:26:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64 CHardDiskPartition::Tell()
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CHardDiskPartition::IsEOF()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|