rpcsx/rpcs3/Loader/TRP.cpp

221 lines
4.3 KiB
C++
Raw Permalink Normal View History

2020-12-05 13:08:24 +01:00
#include "stdafx.h"
#include "Emu/VFS.h"
#include "TRP.h"
#include "Crypto/sha1.h"
#include "util/StrUtil.h"
LOG_CHANNEL(trp_log, "Trophy");
2020-02-01 05:36:53 +01:00
2016-02-01 22:47:09 +01:00
TRPLoader::TRPLoader(const fs::file& f)
: trp_f(f)
{
}
2022-04-25 08:39:58 +02:00
bool TRPLoader::Install(std::string_view dest, bool /*show*/)
{
2016-02-01 22:47:09 +01:00
if (!trp_f)
2015-07-26 10:14:56 +02:00
{
fs::g_tls_error = fs::error::noent;
return false;
2015-07-26 10:14:56 +02:00
}
fs::g_tls_error = {};
const std::string local_path = vfs::get(dest);
const std::string temp = fmt::format(u8"%s.temp%u", local_path, utils::get_unique_tsc());
if (!fs::create_dir(temp))
2015-07-15 15:21:41 +02:00
{
trp_log.error("Failed to create temp dir: '%s' (error=%s)", temp, fs::g_tls_error);
2016-02-01 22:47:09 +01:00
return false;
2015-07-15 15:21:41 +02:00
}
2020-09-22 20:29:08 +02:00
// Save TROPUSR.DAT
if (!fs::copy_file(local_path + "/TROPUSR.DAT", temp + "/TROPUSR.DAT", false))
{
trp_log.error("Failed to copy TROPUSR.DAT from '%s' to '%s' (error=%s)", local_path, temp, fs::g_tls_error);
}
2020-09-22 20:29:08 +02:00
std::vector<char> buffer(65536);
2016-02-01 22:47:09 +01:00
bool success = true;
for (const TRPEntry& entry : m_entries)
{
2016-02-01 22:47:09 +01:00
trp_f.seek(entry.offset);
if (!trp_f.read(buffer, entry.size))
{
trp_log.error("Failed to read TRPEntry at: offset=0x%x, size=0x%x", entry.offset, entry.size);
continue; // ???
}
// Create the file in the temporary directory
const std::string filename = temp + '/' + vfs::escape(entry.name);
success = fs::write_file<true>(filename, fs::create + fs::excl, buffer);
if (!success)
{
trp_log.error("Failed to write file '%s' (error=%s)", filename, fs::g_tls_error);
break;
}
}
if (success)
{
success = fs::remove_all(local_path, true, true);
if (success)
{
// Atomically create trophy data (overwrite existing data)
success = fs::rename(temp, local_path, false);
if (!success)
{
trp_log.error("Failed to move directory '%s' to '%s' (error=%s)", temp, local_path, fs::g_tls_error);
}
}
else
{
trp_log.error("Failed to remove directory '%s' (error=%s)", local_path, fs::g_tls_error);
}
}
if (!success)
{
// Remove temporary directory manually on failure (removed automatically on success)
auto old_error = fs::g_tls_error;
fs::remove_all(temp);
fs::g_tls_error = old_error;
}
return success;
}
bool TRPLoader::LoadHeader(bool show)
{
2016-02-01 22:47:09 +01:00
if (!trp_f)
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
trp_f.seek(0);
2015-07-26 10:14:56 +02:00
2016-02-01 22:47:09 +01:00
if (!trp_f.read(m_header))
2015-07-26 10:14:56 +02:00
{
return false;
2015-07-26 10:14:56 +02:00
}
if (m_header.trp_magic != 0xDCA24D00)
2015-07-26 10:14:56 +02:00
{
return false;
2015-07-26 10:14:56 +02:00
}
if (show)
2015-07-26 10:14:56 +02:00
{
2020-02-01 05:36:53 +01:00
trp_log.notice("TRP version: 0x%x", m_header.trp_version);
2015-07-26 10:14:56 +02:00
}
if (m_header.trp_version >= 2)
{
unsigned char hash[20];
std::vector<u8> file_contents;
trp_f.seek(0);
if (!trp_f.read(file_contents, m_header.trp_file_size))
{
2020-02-01 05:36:53 +01:00
trp_log.notice("Failed verifying checksum");
}
else
{
memset(&(reinterpret_cast<TRPHeader*>(file_contents.data()))->sha1, 0, 20);
sha1(reinterpret_cast<const unsigned char*>(file_contents.data()), m_header.trp_file_size, hash);
if (memcmp(hash, m_header.sha1, 20) != 0)
{
2020-02-01 05:36:53 +01:00
trp_log.error("Invalid checksum of TROPHY.TRP file");
return false;
}
}
trp_f.seek(sizeof(m_header));
}
m_entries.clear();
if (!trp_f.read(m_entries, m_header.trp_files_count))
{
return false;
}
if (show)
{
for (const auto& entry : m_entries)
2015-07-26 10:14:56 +02:00
{
trp_log.notice("TRP entry #%u: %s", &entry - m_entries.data(), entry.name);
2015-07-26 10:14:56 +02:00
}
}
return true;
}
u64 TRPLoader::GetRequiredSpace() const
{
const u64 file_size = m_header.trp_file_size;
const u64 file_element_size = u64{1} * m_header.trp_files_count * m_header.trp_element_size;
return file_size - sizeof(m_header) - file_element_size;
}
2022-04-25 08:39:58 +02:00
bool TRPLoader::ContainsEntry(std::string_view filename)
{
2022-04-25 08:39:58 +02:00
if (filename.size() >= sizeof(TRPEntry::name))
{
return false;
}
2015-07-26 10:14:56 +02:00
for (const TRPEntry& entry : m_entries)
{
2022-04-25 08:39:58 +02:00
if (entry.name == filename)
2015-07-26 10:14:56 +02:00
{
return true;
2015-07-26 10:14:56 +02:00
}
}
return false;
}
2022-04-25 08:39:58 +02:00
void TRPLoader::RemoveEntry(std::string_view filename)
{
2022-04-25 08:39:58 +02:00
if (filename.size() >= sizeof(TRPEntry::name))
{
return;
}
std::vector<TRPEntry>::iterator i = m_entries.begin();
2015-07-26 10:14:56 +02:00
while (i != m_entries.end())
{
2022-04-25 08:39:58 +02:00
if (i->name == filename)
2015-07-26 10:14:56 +02:00
{
i = m_entries.erase(i);
2015-07-26 10:14:56 +02:00
}
else
2015-07-26 10:14:56 +02:00
{
i++;
2015-07-26 10:14:56 +02:00
}
}
}
2022-04-25 08:39:58 +02:00
void TRPLoader::RenameEntry(std::string_view oldname, std::string_view newname)
{
2022-04-25 08:39:58 +02:00
if (oldname.size() >= sizeof(TRPEntry::name) || newname.size() >= sizeof(TRPEntry::name))
{
return;
}
2018-10-10 06:59:33 +02:00
for (TRPEntry& entry : m_entries)
2015-07-26 10:14:56 +02:00
{
2022-04-25 08:39:58 +02:00
if (entry.name == oldname)
2015-07-26 10:14:56 +02:00
{
2022-04-25 08:39:58 +02:00
strcpy_trunc(entry.name, newname);
2015-07-26 10:14:56 +02:00
}
}
}