2020-12-05 13:08:24 +01:00
|
|
|
#include "stdafx.h"
|
2012-11-15 00:39:56 +01:00
|
|
|
#include "PSF.h"
|
|
|
|
|
|
2025-10-05 18:28:03 +02:00
|
|
|
#include "rx/align.hpp"
|
|
|
|
|
#include "rx/asm.hpp"
|
2024-11-11 20:54:44 +01:00
|
|
|
#include <span>
|
2020-12-18 15:43:34 +01:00
|
|
|
|
2020-02-01 08:43:43 +01:00
|
|
|
LOG_CHANNEL(psf_log, "PSF");
|
2020-02-01 05:36:53 +01:00
|
|
|
|
2021-09-16 08:09:30 +02:00
|
|
|
template <>
|
2016-08-03 22:51:05 +02:00
|
|
|
void fmt_class_string<psf::format>::format(std::string& out, u64 arg)
|
|
|
|
|
{
|
|
|
|
|
format_enum(out, arg, [](auto fmt)
|
|
|
|
|
{
|
2025-04-05 21:50:45 +02:00
|
|
|
switch (fmt)
|
|
|
|
|
{
|
|
|
|
|
STR_CASE(psf::format::array);
|
|
|
|
|
STR_CASE(psf::format::string);
|
|
|
|
|
STR_CASE(psf::format::integer);
|
|
|
|
|
}
|
2016-08-03 22:51:05 +02:00
|
|
|
|
2025-04-05 21:50:45 +02:00
|
|
|
return unknown;
|
|
|
|
|
});
|
2016-08-03 22:51:05 +02:00
|
|
|
}
|
|
|
|
|
|
2021-09-16 08:09:30 +02:00
|
|
|
template <>
|
2021-03-30 23:44:58 +02:00
|
|
|
void fmt_class_string<psf::error>::format(std::string& out, u64 arg)
|
|
|
|
|
{
|
|
|
|
|
format_enum(out, arg, [](auto fmt)
|
|
|
|
|
{
|
2025-04-05 21:50:45 +02:00
|
|
|
switch (fmt)
|
|
|
|
|
{
|
|
|
|
|
case psf::error::ok: return "OK";
|
|
|
|
|
case psf::error::stream: return "File doesn't exist";
|
|
|
|
|
case psf::error::not_psf: return "File is not of PSF format";
|
|
|
|
|
case psf::error::corrupt: return "PSF is truncated or corrupted";
|
|
|
|
|
}
|
2021-03-30 23:44:58 +02:00
|
|
|
|
2025-04-05 21:50:45 +02:00
|
|
|
return unknown;
|
|
|
|
|
});
|
2021-03-30 23:44:58 +02:00
|
|
|
}
|
|
|
|
|
|
2021-09-16 08:09:30 +02:00
|
|
|
template <>
|
|
|
|
|
void fmt_class_string<psf::registry>::format(std::string& out, u64 arg)
|
|
|
|
|
{
|
|
|
|
|
const psf::registry& psf = get_object(arg);
|
|
|
|
|
|
|
|
|
|
for (const auto& entry : psf)
|
|
|
|
|
{
|
|
|
|
|
if (entry.second.type() == psf::format::array)
|
|
|
|
|
{
|
|
|
|
|
// Format them last
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fmt::append(out, "%s: ", entry.first);
|
|
|
|
|
|
|
|
|
|
const psf::entry& data = entry.second;
|
|
|
|
|
|
|
|
|
|
if (data.type() == psf::format::integer)
|
|
|
|
|
{
|
|
|
|
|
fmt::append(out, "0x%x", data.as_integer());
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
fmt::append(out, "\"%s\"", data.as_string());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
out += '\n';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const auto& entry : psf)
|
|
|
|
|
{
|
|
|
|
|
if (entry.second.type() != psf::format::array)
|
|
|
|
|
{
|
|
|
|
|
// Formatted before
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-11 20:54:44 +01:00
|
|
|
fmt::append(out, "%s: %s\n", entry.first, std::span<const u8>(reinterpret_cast<const u8*>(entry.second.as_string().data()), entry.second.size()));
|
2021-09-16 08:09:30 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-07 23:12:33 +01:00
|
|
|
namespace psf
|
2012-11-15 00:39:56 +01:00
|
|
|
{
|
2016-01-26 19:13:36 +01:00
|
|
|
struct header_t
|
2013-12-08 14:47:54 +01:00
|
|
|
{
|
2016-01-26 19:13:36 +01:00
|
|
|
le_t<u32> magic;
|
|
|
|
|
le_t<u32> version;
|
|
|
|
|
le_t<u32> off_key_table;
|
|
|
|
|
le_t<u32> off_data_table;
|
|
|
|
|
le_t<u32> entries_num;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct def_table_t
|
2015-04-16 17:33:55 +02:00
|
|
|
{
|
2016-01-26 19:13:36 +01:00
|
|
|
le_t<u16> key_off;
|
|
|
|
|
le_t<format> param_fmt;
|
|
|
|
|
le_t<u32> param_len;
|
|
|
|
|
le_t<u32> param_max;
|
|
|
|
|
le_t<u32> data_off;
|
|
|
|
|
};
|
|
|
|
|
|
2023-02-04 10:46:34 +01:00
|
|
|
entry::entry(format type, u32 max_size, std::string_view value, bool allow_truncate) noexcept
|
2025-04-05 21:50:45 +02:00
|
|
|
: m_type(type), m_max_size(max_size), m_value_string(value)
|
2016-05-13 16:01:48 +02:00
|
|
|
{
|
2020-12-09 08:47:45 +01:00
|
|
|
ensure(type == format::string || type == format::array);
|
2023-06-06 04:29:38 +02:00
|
|
|
ensure(max_size > (type == format::string ? 1u : 0u));
|
2023-02-04 10:46:34 +01:00
|
|
|
|
|
|
|
|
if (allow_truncate && value.size() > max(false))
|
|
|
|
|
{
|
|
|
|
|
m_value_string.resize(max(false));
|
|
|
|
|
}
|
2016-05-13 16:01:48 +02:00
|
|
|
}
|
|
|
|
|
|
2023-02-04 10:46:34 +01:00
|
|
|
entry::entry(u32 value) noexcept
|
2025-04-05 21:50:45 +02:00
|
|
|
: m_type(format::integer), m_max_size(sizeof(u32)), m_value_integer(value)
|
2016-05-13 16:01:48 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-26 19:13:36 +01:00
|
|
|
const std::string& entry::as_string() const
|
2012-11-15 00:39:56 +01:00
|
|
|
{
|
2020-12-09 08:47:45 +01:00
|
|
|
ensure(m_type == format::string || m_type == format::array);
|
2016-01-07 23:12:33 +01:00
|
|
|
return m_value_string;
|
|
|
|
|
}
|
2012-11-15 00:39:56 +01:00
|
|
|
|
2016-01-07 23:12:33 +01:00
|
|
|
u32 entry::as_integer() const
|
2015-04-16 17:33:55 +02:00
|
|
|
{
|
2020-12-09 08:47:45 +01:00
|
|
|
ensure(m_type == format::integer);
|
2016-01-07 23:12:33 +01:00
|
|
|
return m_value_integer;
|
2012-11-15 00:39:56 +01:00
|
|
|
}
|
|
|
|
|
|
2025-04-05 21:50:45 +02:00
|
|
|
entry& entry::operator=(std::string_view value)
|
2015-04-16 17:33:55 +02:00
|
|
|
{
|
2020-12-09 08:47:45 +01:00
|
|
|
ensure(m_type == format::string || m_type == format::array);
|
2016-01-26 19:13:36 +01:00
|
|
|
m_value_string = value;
|
2016-01-07 23:12:33 +01:00
|
|
|
return *this;
|
|
|
|
|
}
|
2015-04-16 18:19:41 +02:00
|
|
|
|
2025-04-05 21:50:45 +02:00
|
|
|
entry& entry::operator=(u32 value)
|
2015-04-16 17:33:55 +02:00
|
|
|
{
|
2020-12-09 08:47:45 +01:00
|
|
|
ensure(m_type == format::integer);
|
2016-01-26 19:13:36 +01:00
|
|
|
m_value_integer = value;
|
2016-01-07 23:12:33 +01:00
|
|
|
return *this;
|
|
|
|
|
}
|
2015-04-16 17:33:55 +02:00
|
|
|
|
2016-01-26 19:13:36 +01:00
|
|
|
u32 entry::size() const
|
2016-01-07 23:12:33 +01:00
|
|
|
{
|
2016-01-26 19:13:36 +01:00
|
|
|
switch (m_type)
|
2012-11-15 00:39:56 +01:00
|
|
|
{
|
2016-01-26 19:13:36 +01:00
|
|
|
case format::string:
|
|
|
|
|
case format::array:
|
2023-02-04 10:46:34 +01:00
|
|
|
return std::min(m_max_size, ::narrow<u32>(m_value_string.size() + (m_type == format::string ? 1 : 0)));
|
2013-12-08 14:47:54 +01:00
|
|
|
|
2016-01-26 19:13:36 +01:00
|
|
|
case format::integer:
|
2018-09-03 17:46:14 +02:00
|
|
|
return sizeof(u32);
|
2012-11-15 00:39:56 +01:00
|
|
|
}
|
2015-04-16 01:17:42 +02:00
|
|
|
|
2020-12-09 16:04:52 +01:00
|
|
|
fmt::throw_exception("Invalid format (0x%x)", m_type);
|
2016-01-07 23:12:33 +01:00
|
|
|
}
|
2015-04-16 01:17:42 +02:00
|
|
|
|
2023-02-04 10:46:34 +01:00
|
|
|
bool entry::is_valid() const
|
|
|
|
|
{
|
|
|
|
|
switch (m_type)
|
|
|
|
|
{
|
|
|
|
|
case format::string:
|
|
|
|
|
case format::array:
|
|
|
|
|
return m_value_string.size() <= this->max(false);
|
|
|
|
|
|
|
|
|
|
case format::integer:
|
|
|
|
|
return true;
|
|
|
|
|
default: break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fmt::throw_exception("Invalid format (0x%x)", m_type);
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-27 08:59:57 +01:00
|
|
|
load_result_t load(const fs::file& stream, std::string_view filename)
|
2016-01-07 23:12:33 +01:00
|
|
|
{
|
2023-06-06 04:29:38 +02:00
|
|
|
load_result_t result{};
|
|
|
|
|
|
2025-04-05 21:50:45 +02:00
|
|
|
#define PSF_CHECK(cond, err) \
|
|
|
|
|
if (!static_cast<bool>(cond)) \
|
|
|
|
|
{ \
|
|
|
|
|
if (err != error::stream) \
|
|
|
|
|
psf_log.error("Error loading PSF '%s': %s%s", filename, err, std::source_location::current()); \
|
|
|
|
|
result.sfo.clear(); \
|
|
|
|
|
result.errc = err; \
|
|
|
|
|
return result; \
|
|
|
|
|
}
|
2021-03-30 23:44:58 +02:00
|
|
|
|
2023-06-06 04:29:38 +02:00
|
|
|
PSF_CHECK(stream, error::stream);
|
2015-04-16 17:33:55 +02:00
|
|
|
|
2021-02-21 20:55:07 +01:00
|
|
|
stream.seek(0);
|
|
|
|
|
|
2016-01-26 19:13:36 +01:00
|
|
|
// Get header
|
2016-05-13 16:01:48 +02:00
|
|
|
header_t header;
|
2023-06-06 04:29:38 +02:00
|
|
|
PSF_CHECK(stream.read(header), error::not_psf);
|
2015-04-16 18:19:41 +02:00
|
|
|
|
2016-01-26 19:13:36 +01:00
|
|
|
// Check magic and version
|
2023-06-06 04:29:38 +02:00
|
|
|
PSF_CHECK(header.magic == "\0PSF"_u32, error::not_psf);
|
|
|
|
|
PSF_CHECK(header.version == 0x101u, error::not_psf);
|
|
|
|
|
PSF_CHECK(header.off_key_table >= sizeof(header_t), error::corrupt);
|
|
|
|
|
PSF_CHECK(header.off_key_table <= header.off_data_table, error::corrupt);
|
|
|
|
|
PSF_CHECK(header.off_data_table <= stream.size(), error::corrupt);
|
2016-05-13 16:01:48 +02:00
|
|
|
|
|
|
|
|
// Get indices
|
|
|
|
|
std::vector<def_table_t> indices;
|
2023-06-06 04:29:38 +02:00
|
|
|
PSF_CHECK(stream.read(indices, header.entries_num), error::corrupt);
|
2015-04-16 01:17:42 +02:00
|
|
|
|
2016-05-13 16:01:48 +02:00
|
|
|
// Get keys
|
|
|
|
|
std::string keys;
|
2023-06-06 04:29:38 +02:00
|
|
|
PSF_CHECK(stream.seek(header.off_key_table) == header.off_key_table, error::corrupt);
|
|
|
|
|
PSF_CHECK(stream.read(keys, header.off_data_table - header.off_key_table), error::corrupt);
|
2015-04-16 18:19:41 +02:00
|
|
|
|
2016-01-26 19:13:36 +01:00
|
|
|
// Load entries
|
|
|
|
|
for (u32 i = 0; i < header.entries_num; ++i)
|
2016-01-07 23:12:33 +01:00
|
|
|
{
|
2023-06-06 04:29:38 +02:00
|
|
|
PSF_CHECK(indices[i].key_off < header.off_data_table - header.off_key_table, error::corrupt);
|
2015-04-16 18:19:41 +02:00
|
|
|
|
2016-05-13 16:01:48 +02:00
|
|
|
// Get key name (null-terminated string)
|
|
|
|
|
std::string key(keys.data() + indices[i].key_off);
|
2015-04-16 18:19:41 +02:00
|
|
|
|
2016-08-15 15:29:38 +02:00
|
|
|
// Check entry
|
2023-06-06 04:29:38 +02:00
|
|
|
PSF_CHECK(!result.sfo.contains(key), error::corrupt);
|
|
|
|
|
PSF_CHECK(indices[i].param_len <= indices[i].param_max, error::corrupt);
|
|
|
|
|
PSF_CHECK(indices[i].data_off < stream.size() - header.off_data_table, error::corrupt);
|
|
|
|
|
PSF_CHECK(indices[i].param_max < stream.size() - indices[i].data_off, error::corrupt);
|
2015-04-16 18:19:41 +02:00
|
|
|
|
2016-05-13 16:01:48 +02:00
|
|
|
// Seek data pointer
|
|
|
|
|
stream.seek(header.off_data_table + indices[i].data_off);
|
2015-04-16 18:19:41 +02:00
|
|
|
|
2016-01-26 19:13:36 +01:00
|
|
|
if (indices[i].param_fmt == format::integer && indices[i].param_max == sizeof(u32) && indices[i].param_len == sizeof(u32))
|
2016-01-07 20:22:36 +01:00
|
|
|
{
|
2016-01-26 19:13:36 +01:00
|
|
|
// Integer data
|
2016-05-13 16:01:48 +02:00
|
|
|
le_t<u32> value;
|
2023-06-06 04:29:38 +02:00
|
|
|
PSF_CHECK(stream.read(value), error::corrupt);
|
2016-05-13 16:01:48 +02:00
|
|
|
|
2023-06-06 04:29:38 +02:00
|
|
|
result.sfo.emplace(std::piecewise_construct,
|
2016-01-26 19:13:36 +01:00
|
|
|
std::forward_as_tuple(std::move(key)),
|
2016-05-13 16:01:48 +02:00
|
|
|
std::forward_as_tuple(value));
|
2016-01-07 20:22:36 +01:00
|
|
|
}
|
2016-01-26 19:13:36 +01:00
|
|
|
else if (indices[i].param_fmt == format::string || indices[i].param_fmt == format::array)
|
2015-04-16 18:19:41 +02:00
|
|
|
{
|
2016-01-26 19:13:36 +01:00
|
|
|
// String/array data
|
|
|
|
|
std::string value;
|
2023-06-06 04:29:38 +02:00
|
|
|
PSF_CHECK(stream.read(value, indices[i].param_len), error::corrupt);
|
2016-01-07 23:12:33 +01:00
|
|
|
|
2016-01-26 19:13:36 +01:00
|
|
|
if (indices[i].param_fmt == format::string)
|
2016-01-07 23:12:33 +01:00
|
|
|
{
|
2016-01-26 19:13:36 +01:00
|
|
|
// Find null terminator
|
2016-05-13 16:01:48 +02:00
|
|
|
value.resize(std::strlen(value.c_str()));
|
2016-01-07 23:12:33 +01:00
|
|
|
}
|
2016-01-26 19:13:36 +01:00
|
|
|
|
2023-06-06 04:29:38 +02:00
|
|
|
result.sfo.emplace(std::piecewise_construct,
|
2016-01-26 19:13:36 +01:00
|
|
|
std::forward_as_tuple(std::move(key)),
|
|
|
|
|
std::forward_as_tuple(indices[i].param_fmt, indices[i].param_max, std::move(value)));
|
2015-04-16 18:19:41 +02:00
|
|
|
}
|
2016-01-07 23:12:33 +01:00
|
|
|
else
|
2015-04-16 18:19:41 +02:00
|
|
|
{
|
2016-01-26 19:13:36 +01:00
|
|
|
// Possibly unsupported format, entry ignored
|
2020-02-01 05:36:53 +01:00
|
|
|
psf_log.error("Unknown entry format (key='%s', fmt=0x%x, len=0x%x, max=0x%x)", key, indices[i].param_fmt, indices[i].param_len, indices[i].param_max);
|
2015-04-16 18:19:41 +02:00
|
|
|
}
|
|
|
|
|
}
|
2015-04-16 17:33:55 +02:00
|
|
|
|
2023-06-06 04:29:38 +02:00
|
|
|
const auto cat = get_string(result.sfo, "CATEGORY", "");
|
2022-12-11 20:53:53 +01:00
|
|
|
constexpr std::string_view valid_cats[]{"GD", "DG", "HG", "AM", "AP", "AS", "AT", "AV", "BV", "WT", "HM", "CB", "SF", "2P", "2G", "1P", "PP", "MN", "PE", "2D", "SD", "MS"};
|
2022-04-09 12:13:08 +02:00
|
|
|
|
|
|
|
|
if (std::find(std::begin(valid_cats), std::end(valid_cats), cat) == std::end(valid_cats))
|
|
|
|
|
{
|
|
|
|
|
psf_log.error("Unknown category ('%s')", cat);
|
2023-06-06 04:29:38 +02:00
|
|
|
PSF_CHECK(false, error::corrupt);
|
2022-04-09 12:13:08 +02:00
|
|
|
}
|
|
|
|
|
|
2021-03-30 23:44:58 +02:00
|
|
|
#undef PSF_CHECK
|
2023-06-06 04:29:38 +02:00
|
|
|
return result;
|
2021-03-30 23:44:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
load_result_t load(const std::string& filename)
|
|
|
|
|
{
|
2022-11-27 08:59:57 +01:00
|
|
|
return load(fs::file(filename), filename);
|
2015-04-16 18:19:41 +02:00
|
|
|
}
|
|
|
|
|
|
2021-02-21 20:55:07 +01:00
|
|
|
std::vector<u8> save_object(const psf::registry& psf, std::vector<u8>&& init)
|
2015-04-16 18:19:41 +02:00
|
|
|
{
|
2021-02-21 20:55:07 +01:00
|
|
|
fs::file stream = fs::make_stream<std::vector<u8>>(std::move(init));
|
|
|
|
|
|
2025-04-05 21:50:45 +02:00
|
|
|
std::vector<def_table_t> indices;
|
|
|
|
|
indices.reserve(psf.size());
|
2016-01-07 23:12:33 +01:00
|
|
|
|
2016-01-26 19:13:36 +01:00
|
|
|
// Generate indices and calculate key table length
|
2020-12-18 08:39:54 +01:00
|
|
|
usz key_offset = 0, data_offset = 0;
|
2016-01-07 23:12:33 +01:00
|
|
|
|
2016-01-26 19:13:36 +01:00
|
|
|
for (const auto& entry : psf)
|
2016-01-07 23:12:33 +01:00
|
|
|
{
|
2023-11-16 00:18:35 +01:00
|
|
|
def_table_t index{};
|
2016-04-20 01:32:27 +02:00
|
|
|
index.key_off = ::narrow<u32>(key_offset);
|
2016-01-26 19:13:36 +01:00
|
|
|
index.param_fmt = entry.second.type();
|
|
|
|
|
index.param_len = entry.second.size();
|
2023-02-04 10:46:34 +01:00
|
|
|
index.param_max = entry.second.max(true);
|
2016-04-20 01:32:27 +02:00
|
|
|
index.data_off = ::narrow<u32>(data_offset);
|
2016-01-26 19:13:36 +01:00
|
|
|
|
|
|
|
|
// Update offsets:
|
2016-04-20 01:32:27 +02:00
|
|
|
key_offset += ::narrow<u32>(entry.first.size() + 1); // key size
|
2016-01-26 19:13:36 +01:00
|
|
|
data_offset += index.param_max;
|
|
|
|
|
|
|
|
|
|
indices.push_back(index);
|
|
|
|
|
}
|
2016-01-07 23:12:33 +01:00
|
|
|
|
2016-01-26 19:13:36 +01:00
|
|
|
// Align next section (data) offset
|
2025-10-05 18:28:03 +02:00
|
|
|
key_offset = rx::alignUp(key_offset, 4);
|
2016-01-07 23:12:33 +01:00
|
|
|
|
2016-01-26 19:13:36 +01:00
|
|
|
// Generate header
|
2023-11-16 00:18:35 +01:00
|
|
|
header_t header{};
|
2016-02-01 22:47:09 +01:00
|
|
|
header.magic = "\0PSF"_u32;
|
2016-01-26 19:13:36 +01:00
|
|
|
header.version = 0x101;
|
2016-04-20 01:32:27 +02:00
|
|
|
header.off_key_table = ::narrow<u32>(sizeof(header_t) + sizeof(def_table_t) * psf.size());
|
|
|
|
|
header.off_data_table = ::narrow<u32>(header.off_key_table + key_offset);
|
|
|
|
|
header.entries_num = ::narrow<u32>(psf.size());
|
2016-01-07 23:12:33 +01:00
|
|
|
|
2016-01-26 19:13:36 +01:00
|
|
|
// Save header and indices
|
2016-05-13 16:01:48 +02:00
|
|
|
stream.write(header);
|
|
|
|
|
stream.write(indices);
|
2016-01-07 23:12:33 +01:00
|
|
|
|
2016-01-26 19:13:36 +01:00
|
|
|
// Save key table
|
|
|
|
|
for (const auto& entry : psf)
|
2015-04-16 18:19:41 +02:00
|
|
|
{
|
2016-05-13 16:01:48 +02:00
|
|
|
stream.write(entry.first);
|
|
|
|
|
stream.write('\0');
|
2015-04-16 18:19:41 +02:00
|
|
|
}
|
|
|
|
|
|
2016-05-13 16:01:48 +02:00
|
|
|
// Skip padding
|
2016-05-24 00:59:39 +02:00
|
|
|
stream.trunc(stream.seek(header.off_data_table));
|
2016-01-07 23:12:33 +01:00
|
|
|
|
2016-01-26 19:13:36 +01:00
|
|
|
// Save data
|
|
|
|
|
for (const auto& entry : psf)
|
2015-04-16 18:19:41 +02:00
|
|
|
{
|
2016-01-26 19:13:36 +01:00
|
|
|
const auto fmt = entry.second.type();
|
2023-02-04 10:46:34 +01:00
|
|
|
const u32 max = entry.second.max(true);
|
2016-01-07 23:12:33 +01:00
|
|
|
|
2016-01-26 19:13:36 +01:00
|
|
|
if (fmt == format::integer && max == sizeof(u32))
|
2016-01-07 23:12:33 +01:00
|
|
|
{
|
2016-01-26 19:13:36 +01:00
|
|
|
const le_t<u32> value = entry.second.as_integer();
|
2016-05-13 16:01:48 +02:00
|
|
|
stream.write(value);
|
2016-01-07 20:22:36 +01:00
|
|
|
}
|
2016-01-26 19:13:36 +01:00
|
|
|
else if (fmt == format::string || fmt == format::array)
|
2015-04-16 18:19:41 +02:00
|
|
|
{
|
2023-02-04 10:46:34 +01:00
|
|
|
std::string_view value = entry.second.as_string();
|
2016-01-26 19:13:36 +01:00
|
|
|
|
2023-02-04 10:46:34 +01:00
|
|
|
if (!entry.second.is_valid())
|
2016-01-07 23:12:33 +01:00
|
|
|
{
|
2016-01-26 19:13:36 +01:00
|
|
|
// TODO: check real limitations of PSF format
|
2023-02-04 10:46:34 +01:00
|
|
|
psf_log.error("Entry value shrinkage (key='%s', value='%s', size=0x%zx, max=0x%x)", entry.first, value, value.size(), max);
|
|
|
|
|
value = value.substr(0, entry.second.max(false));
|
2016-01-07 23:12:33 +01:00
|
|
|
}
|
2016-01-26 19:13:36 +01:00
|
|
|
|
2023-02-04 10:46:34 +01:00
|
|
|
stream.write(value.data(), value.size());
|
|
|
|
|
stream.trunc(stream.seek(max - value.size(), fs::seek_cur)); // Skip up to max_size
|
2016-01-07 23:12:33 +01:00
|
|
|
}
|
2016-01-26 19:13:36 +01:00
|
|
|
else
|
2016-01-07 23:12:33 +01:00
|
|
|
{
|
2020-12-09 16:04:52 +01:00
|
|
|
fmt::throw_exception("Invalid entry format (key='%s', fmt=0x%x)", entry.first, fmt);
|
2015-04-16 18:19:41 +02:00
|
|
|
}
|
|
|
|
|
}
|
2021-02-21 20:55:07 +01:00
|
|
|
|
|
|
|
|
return std::move(static_cast<fs::container_stream<std::vector<u8>>*>(stream.release().get())->obj);
|
2012-11-15 00:39:56 +01:00
|
|
|
}
|
|
|
|
|
|
2022-04-09 12:13:08 +02:00
|
|
|
std::string_view get_string(const registry& psf, std::string_view key, std::string_view def)
|
2015-04-16 01:17:42 +02:00
|
|
|
{
|
2016-01-26 19:13:36 +01:00
|
|
|
const auto found = psf.find(key);
|
2016-01-08 00:07:55 +01:00
|
|
|
|
2016-01-26 19:13:36 +01:00
|
|
|
if (found == psf.end() || (found->second.type() != format::string && found->second.type() != format::array))
|
2016-01-08 00:07:55 +01:00
|
|
|
{
|
2016-01-26 19:13:36 +01:00
|
|
|
return def;
|
2016-01-08 00:07:55 +01:00
|
|
|
}
|
|
|
|
|
|
2016-01-26 19:13:36 +01:00
|
|
|
return found->second.as_string();
|
2016-01-08 00:07:55 +01:00
|
|
|
}
|
2016-01-08 00:53:03 +01:00
|
|
|
|
2022-04-09 12:13:08 +02:00
|
|
|
u32 get_integer(const registry& psf, std::string_view key, u32 def)
|
2016-01-08 00:53:03 +01:00
|
|
|
{
|
2016-01-26 19:13:36 +01:00
|
|
|
const auto found = psf.find(key);
|
2016-01-08 00:53:03 +01:00
|
|
|
|
2016-01-26 19:13:36 +01:00
|
|
|
if (found == psf.end() || found->second.type() != format::integer)
|
2016-01-08 00:53:03 +01:00
|
|
|
{
|
2016-01-26 19:13:36 +01:00
|
|
|
return def;
|
2016-01-08 00:53:03 +01:00
|
|
|
}
|
|
|
|
|
|
2016-01-26 19:13:36 +01:00
|
|
|
return found->second.as_integer();
|
2016-01-08 00:53:03 +01:00
|
|
|
}
|
2023-02-04 10:46:34 +01:00
|
|
|
|
2024-05-21 09:34:51 +02:00
|
|
|
bool check_registry(const registry& psf, std::function<bool(bool ok, const std::string& key, const entry& value)> validate, std::source_location src_loc)
|
2023-02-04 10:46:34 +01:00
|
|
|
{
|
|
|
|
|
bool psf_ok = true;
|
|
|
|
|
|
|
|
|
|
for (const auto& [key, value] : psf)
|
|
|
|
|
{
|
|
|
|
|
bool entry_ok = value.is_valid();
|
|
|
|
|
|
|
|
|
|
if (validate)
|
|
|
|
|
{
|
|
|
|
|
// Validate against a custom condition as well (forward error)
|
|
|
|
|
if (!validate(entry_ok, key, value))
|
|
|
|
|
{
|
|
|
|
|
entry_ok = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!entry_ok)
|
|
|
|
|
{
|
|
|
|
|
if (value.type() == format::string)
|
|
|
|
|
{
|
2024-05-21 09:34:51 +02:00
|
|
|
psf_log.error("Entry '%s' is invalid: string='%s'.%s", key, value.as_string(), src_loc);
|
2023-02-04 10:46:34 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// TODO: Better logging of other types
|
2024-05-21 09:34:51 +02:00
|
|
|
psf_log.error("Entry %s is invalid.%s", key, value.as_string(), src_loc);
|
2023-02-04 10:46:34 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!entry_ok)
|
|
|
|
|
{
|
|
|
|
|
// Do not break, run over all entries in order to report all errors
|
|
|
|
|
psf_ok = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return psf_ok;
|
|
|
|
|
}
|
2025-04-05 21:50:45 +02:00
|
|
|
} // namespace psf
|