rpcsx/rpcs3/Loader/PSF.cpp

202 lines
3.7 KiB
C++
Raw Normal View History

#include "stdafx.h"
#include "Utilities/Log.h"
#include "Emu/FS/vfsStream.h"
#include "PSF.h"
2015-04-16 01:17:42 +02:00
bool PSFLoader::Load(vfsStream& stream)
{
2015-04-16 17:33:55 +02:00
Clear();
2015-04-16 17:33:55 +02:00
// load header
if (!stream.SRead(m_header))
{
return false;
2015-04-16 01:17:42 +02:00
}
2015-04-16 17:33:55 +02:00
// check magic
if (m_header.magic != *(u32*)"\0PSF")
{
LOG_ERROR(LOADER, "PSFLoader::Load() failed: unknown magic (0x%x)", m_header.magic);
return false;
}
2015-04-16 17:33:55 +02:00
// check version
if (m_header.version != 0x101)
{
2015-04-16 17:33:55 +02:00
LOG_ERROR(LOADER, "PSFLoader::Load() failed: unknown version (0x%x)", m_header.version);
return false;
}
// load indices
std::vector<PSFDefTable> indices;
indices.resize(m_header.entries_num);
2015-04-16 17:33:55 +02:00
if (!stream.SRead(indices[0], sizeof(PSFDefTable) * m_header.entries_num))
{
return false;
}
2015-04-16 17:33:55 +02:00
// load key table
if (m_header.off_key_table > m_header.off_data_table)
{
2015-04-16 17:33:55 +02:00
LOG_ERROR(LOADER, "PSFLoader::Load() failed: off_key_table=0x%x, off_data_table=0x%x", m_header.off_key_table, m_header.off_data_table);
return false;
}
const u32 key_table_size = m_header.off_data_table - m_header.off_key_table;
2015-04-16 17:33:55 +02:00
std::unique_ptr<char> keys(new char[key_table_size + 1]);
stream.Seek(m_header.off_key_table);
if (stream.Read(keys.get(), key_table_size) != key_table_size)
{
return false;
}
2015-04-16 17:33:55 +02:00
keys.get()[key_table_size] = 0;
// fill entries
m_entries.resize(m_header.entries_num);
for (u32 i = 0; i < m_header.entries_num; ++i)
{
m_entries[i].fmt = indices[i].param_fmt;
if (indices[i].key_off >= key_table_size)
{
return false;
}
m_entries[i].name = keys.get() + indices[i].key_off;
// load data
stream.Seek(m_header.off_data_table + indices[i].data_off);
if (indices[i].param_fmt == PSF_PARAM_INT && indices[i].param_len == 4 && indices[i].param_max == 4)
{
2015-04-16 17:33:55 +02:00
// load int data
2015-04-16 17:33:55 +02:00
if (!stream.SRead(m_entries[i].vint))
2015-04-16 01:17:42 +02:00
{
2015-04-16 17:33:55 +02:00
return false;
2015-04-16 01:17:42 +02:00
}
}
2015-04-16 17:33:55 +02:00
else if (indices[i].param_fmt == PSF_PARAM_STR && indices[i].param_max >= indices[i].param_len)
{
// load str data
2015-04-16 01:17:42 +02:00
2015-04-16 17:33:55 +02:00
const u32 size = indices[i].param_len;
2015-04-16 01:17:42 +02:00
2015-04-16 17:33:55 +02:00
std::unique_ptr<char> str(new char[size + 1]);
if (stream.Read(str.get(), size) != size)
{
return false;
}
str.get()[size] = 0;
m_entries[i].vstr = str.get();
}
else
{
LOG_ERROR(LOADER, "PSFLoader::Load() failed: (i=%d) fmt=0x%x, len=0x%x, max=0x%x", i, indices[i].param_fmt, indices[i].param_len, indices[i].param_max);
return false;
}
}
2015-04-16 01:17:42 +02:00
return (m_loaded = true);
}
2015-04-16 17:33:55 +02:00
bool PSFLoader::Save(vfsStream& stream)
{
// TODO: Construct m_header
m_loaded = true;
// TODO: Save data
return true;
}
void PSFLoader::Clear()
2015-04-16 01:17:42 +02:00
{
m_loaded = false;
m_header = {};
m_entries.clear();
}
2015-04-16 01:17:42 +02:00
const PSFEntry* PSFLoader::SearchEntry(const std::string& key) const
{
2015-04-16 01:17:42 +02:00
for (auto& entry : m_entries)
{
2015-04-16 01:17:42 +02:00
if (key == entry.name)
{
return &entry;
}
}
2015-04-16 01:17:42 +02:00
return nullptr;
}
2015-04-16 17:33:55 +02:00
PSFEntry& PSFLoader::AddEntry(const std::string& key, u16 fmt)
{
2015-04-16 17:33:55 +02:00
for (auto& entry : m_entries)
2015-04-16 01:17:42 +02:00
{
2015-04-16 17:33:55 +02:00
if (key == entry.name)
{
entry.fmt = fmt;
return entry;
}
2015-04-16 01:17:42 +02:00
}
2015-04-16 17:33:55 +02:00
PSFEntry new_entry = {};
new_entry.fmt = fmt;
new_entry.name = key;
m_entries.push_back(new_entry);
return m_entries.back();
}
2015-04-16 17:33:55 +02:00
std::string PSFLoader::GetString(const std::string& key, std::string def) const
{
2015-04-16 01:17:42 +02:00
if (const auto entry = SearchEntry(key))
{
2015-04-16 17:33:55 +02:00
if (entry->fmt == PSF_PARAM_STR)
{
return entry->vstr;
}
2015-04-16 01:17:42 +02:00
}
2015-04-16 17:33:55 +02:00
return def;
}
s32 PSFLoader::GetInteger(const std::string& key, s32 def) const
{
if (const auto entry = SearchEntry(key))
2015-04-16 01:17:42 +02:00
{
2015-04-16 17:33:55 +02:00
if (entry->fmt == PSF_PARAM_INT)
{
return entry->vint;
}
2015-04-16 01:17:42 +02:00
}
2015-04-16 17:33:55 +02:00
return def;
}
void PSFLoader::SetString(const std::string& key, std::string value)
{
auto& entry = AddEntry(key, PSF_PARAM_STR);
entry.vstr = value;
}
void PSFLoader::SetInteger(const std::string& key, s32 value)
{
auto& entry = AddEntry(key, PSF_PARAM_INT);
entry.vint = value;
2013-11-19 11:30:58 +01:00
}