rpcsx/rpcs3/Loader/TROPUSR.cpp

254 lines
4.9 KiB
C++
Raw Normal View History

#include "stdafx.h"
2017-01-28 13:32:45 +01:00
#include "restore_new.h"
2014-07-11 13:59:13 +02:00
#include "Utilities/rXml.h"
2017-01-28 13:32:45 +01:00
#include "define_new_memleakdetect.h"
#include "Emu/System.h"
#include "TROPUSR.h"
2014-03-21 15:07:05 +01:00
bool TROPUSRLoader::Load(const std::string& filepath, const std::string& configpath)
{
2016-02-01 22:47:09 +01:00
const std::string& path = vfs::get(filepath);
if (!m_file.open(path, fs::read))
2015-07-26 10:14:56 +02:00
{
if (!Generate(filepath, configpath))
{
return false;
}
2015-07-26 10:14:56 +02:00
}
2016-02-01 22:47:09 +01:00
if (!LoadHeader() || !LoadTableHeaders() || !LoadTables())
{
return false;
}
2016-02-01 22:47:09 +01:00
m_file.release();
return true;
}
bool TROPUSRLoader::LoadHeader()
{
2016-02-01 22:47:09 +01:00
if (!m_file)
2015-07-26 10:14:56 +02:00
{
return false;
2015-07-26 10:14:56 +02:00
}
2016-02-01 22:47:09 +01:00
m_file.seek(0);
2015-07-26 10:14:56 +02:00
2016-02-01 22:47:09 +01:00
if (!m_file.read(m_header))
2015-07-26 10:14:56 +02:00
{
return false;
2015-07-26 10:14:56 +02:00
}
return true;
}
bool TROPUSRLoader::LoadTableHeaders()
{
2016-02-01 22:47:09 +01:00
if (!m_file)
2015-07-26 10:14:56 +02:00
{
return false;
2015-07-26 10:14:56 +02:00
}
2016-02-01 22:47:09 +01:00
m_file.seek(0x30);
m_tableHeaders.clear();
m_tableHeaders.resize(m_header.tables_count);
2015-07-26 10:14:56 +02:00
for (TROPUSRTableHeader& tableHeader : m_tableHeaders)
{
2016-02-01 22:47:09 +01:00
if (!m_file.read(tableHeader))
return false;
}
2015-07-26 10:14:56 +02:00
return true;
}
bool TROPUSRLoader::LoadTables()
{
2016-02-01 22:47:09 +01:00
if (!m_file)
2015-07-26 10:14:56 +02:00
{
return false;
2015-07-26 10:14:56 +02:00
}
for (const TROPUSRTableHeader& tableHeader : m_tableHeaders)
{
2016-02-01 22:47:09 +01:00
m_file.seek(tableHeader.offset);
if (tableHeader.type == 4)
{
m_table4.clear();
m_table4.resize(tableHeader.entries_count);
2015-07-26 10:14:56 +02:00
for (auto& entry : m_table4)
{
2016-02-01 22:47:09 +01:00
if (!m_file.read(entry))
return false;
}
}
if (tableHeader.type == 6)
{
m_table6.clear();
m_table6.resize(tableHeader.entries_count);
2015-07-26 10:14:56 +02:00
for (auto& entry : m_table6)
{
2016-02-01 22:47:09 +01:00
if (!m_file.read(entry))
return false;
}
}
// TODO: Other tables
}
return true;
}
// TODO: TROPUSRLoader::Save deletes the TROPUSR and creates it again. This is probably very slow.
2014-03-21 15:07:05 +01:00
bool TROPUSRLoader::Save(const std::string& filepath)
{
2016-02-01 22:47:09 +01:00
if (!m_file.open(vfs::get(filepath), fs::rewrite))
2015-07-26 10:14:56 +02:00
{
2016-02-01 22:47:09 +01:00
return false;
2015-07-26 10:14:56 +02:00
}
2016-02-01 22:47:09 +01:00
m_file.write(m_header);
for (const TROPUSRTableHeader& tableHeader : m_tableHeaders)
2015-07-26 10:14:56 +02:00
{
2016-02-01 22:47:09 +01:00
m_file.write(tableHeader);
2015-07-26 10:14:56 +02:00
}
for (const auto& entry : m_table4)
2015-07-26 10:14:56 +02:00
{
2016-02-01 22:47:09 +01:00
m_file.write(entry);
2015-07-26 10:14:56 +02:00
}
for (const auto& entry : m_table6)
2015-07-26 10:14:56 +02:00
{
2016-02-01 22:47:09 +01:00
m_file.write(entry);
2015-07-26 10:14:56 +02:00
}
2016-02-01 22:47:09 +01:00
m_file.release();
return true;
}
2014-03-21 15:07:05 +01:00
bool TROPUSRLoader::Generate(const std::string& filepath, const std::string& configpath)
{
2017-09-03 21:29:20 +02:00
fs::file config(vfs::get(configpath));
if (!config)
{
return false;
}
2016-02-01 22:47:09 +01:00
rXmlDocument doc;
2017-09-03 21:29:20 +02:00
doc.Read(config.to_string());
m_table4.clear();
m_table6.clear();
2015-07-26 10:14:56 +02:00
auto trophy_base = doc.GetRoot();
if (trophy_base->GetChildren()->GetName() == "trophyconf")
{
trophy_base = trophy_base->GetChildren();
}
for (std::shared_ptr<rXmlNode> n = trophy_base->GetChildren(); n; n = n->GetNext())
{
if (n->GetName() == "trophy")
{
u32 trophy_id = std::atoi(n->GetAttribute("id").c_str());
u32 trophy_grade;
switch (((const char *)n->GetAttribute("ttype").c_str())[0])
{
case 'B': trophy_grade = 4; break;
case 'S': trophy_grade = 3; break;
case 'G': trophy_grade = 2; break;
case 'P': trophy_grade = 1; break;
default: trophy_grade = 0;
}
TROPUSREntry4 entry4 = { 4, SIZE_32(TROPUSREntry4) - 0x10, (u32)m_table4.size(), 0, trophy_id, trophy_grade, 0xFFFFFFFF };
TROPUSREntry6 entry6 = { 6, SIZE_32(TROPUSREntry6) - 0x10, (u32)m_table6.size(), 0, trophy_id };
m_table4.push_back(entry4);
m_table6.push_back(entry6);
}
}
u64 offset = sizeof(TROPUSRHeader) + 2 * sizeof(TROPUSRTableHeader);
TROPUSRTableHeader table4header = { 4, SIZE_32(TROPUSREntry4) - 0x10, 1, (u32)m_table4.size(), offset };
offset += m_table4.size() * sizeof(TROPUSREntry4);
TROPUSRTableHeader table6header = { 6, SIZE_32(TROPUSREntry6) - 0x10, 1, (u32)m_table6.size(), offset };
offset += m_table6.size() * sizeof(TROPUSREntry6);
m_tableHeaders.clear();
m_tableHeaders.push_back(table4header);
m_tableHeaders.push_back(table6header);
m_header.magic = 0x818F54AD;
m_header.unk1 = 0x00010000;
2014-08-30 22:41:01 +02:00
m_header.tables_count = (u32)m_tableHeaders.size();
m_header.unk2 = 0;
Save(filepath);
2015-07-26 10:14:56 +02:00
return true;
}
u32 TROPUSRLoader::GetTrophiesCount()
{
2014-08-30 22:41:01 +02:00
return (u32)m_table6.size();
}
u32 TROPUSRLoader::GetUnlockedTrophiesCount()
{
u32 count = 0;
for (const auto& trophy : m_table6)
{
if (trophy.trophy_state)
{
count++;
}
}
return count;
}
u32 TROPUSRLoader::GetTrophyUnlockState(u32 id)
{
2015-07-26 10:14:56 +02:00
if (id >= m_table6.size())
{
LOG_WARNING(LOADER, "TROPUSRLoader::GetUnlockState: Invalid id=%d", id);
return 0;
2015-07-26 10:14:56 +02:00
}
return m_table6[id].trophy_state; // Let's assume the trophies are stored ordered
}
2014-08-30 22:41:01 +02:00
u64 TROPUSRLoader::GetTrophyTimestamp(u32 id)
{
2014-08-30 22:41:01 +02:00
if (id >= m_table6.size())
2015-07-26 10:14:56 +02:00
{
LOG_WARNING(LOADER, "TROPUSRLoader::GetTrophyTimestamp: Invalid id=%d", id);
return 0;
2015-07-26 10:14:56 +02:00
}
// TODO: What timestamp does sceNpTrophyGetTrophyInfo want, timestamp1 or timestamp2?
return m_table6[id].timestamp2; // Let's assume the trophies are stored ordered
}
bool TROPUSRLoader::UnlockTrophy(u32 id, u64 timestamp1, u64 timestamp2)
{
2015-07-26 10:14:56 +02:00
if (id >= m_table6.size())
{
return false;
2015-07-26 10:14:56 +02:00
}
m_table6[id].trophy_state = 1;
m_table6[id].timestamp1 = timestamp1;
m_table6[id].timestamp2 = timestamp2;
2015-07-26 10:14:56 +02:00
return true;
}