use phosg namespace

This commit is contained in:
Martin Michelsen
2024-07-28 11:54:41 -07:00
parent 5523388ad4
commit 1bfbf09891
116 changed files with 2969 additions and 3088 deletions
+52 -58
View File
@@ -14,7 +14,7 @@
using namespace std;
TextSet::TextSet(const JSON& json) {
TextSet::TextSet(const phosg::JSON& json) {
for (const auto& coll_json : json.as_list()) {
auto& collection = this->collections.emplace_back();
for (const auto& s_json : coll_json->as_list()) {
@@ -23,7 +23,7 @@ TextSet::TextSet(const JSON& json) {
}
}
TextSet::TextSet(JSON&& json) {
TextSet::TextSet(phosg::JSON&& json) {
for (const auto& coll_json : json.as_list()) {
auto& collection = this->collections.emplace_back();
for (const auto& s_json : coll_json->as_list()) {
@@ -32,10 +32,10 @@ TextSet::TextSet(JSON&& json) {
}
}
JSON TextSet::json() const {
JSON j = JSON::list();
phosg::JSON TextSet::json() const {
phosg::JSON j = phosg::JSON::list();
for (const auto& collection : this->collections) {
JSON& coll_j = j.emplace_back(JSON::list());
phosg::JSON& coll_j = j.emplace_back(phosg::JSON::list());
for (const auto& s : collection) {
coll_j.emplace_back(s);
}
@@ -106,7 +106,7 @@ void TextSet::ensure_collection_exists(size_t collection_index) {
UnicodeTextSet::UnicodeTextSet(const string& prs_data) {
string data = prs_decompress(prs_data);
StringReader r(data);
phosg::StringReader r(data);
uint32_t num_collections = r.get_u32l();
deque<uint32_t> collection_sizes;
@@ -122,8 +122,8 @@ UnicodeTextSet::UnicodeTextSet(const string& prs_data) {
auto& strings = this->collections.emplace_back();
strings.reserve(num_strings);
while (strings.size() < num_strings) {
StringReader sub_r = r.sub(r.get_u32l());
StringWriter w;
phosg::StringReader sub_r = r.sub(r.get_u32l());
phosg::StringWriter w;
for (uint16_t ch = sub_r.get_u16l(); ch != 0; ch = sub_r.get_u16l()) {
w.put_u16l(ch);
}
@@ -133,8 +133,8 @@ UnicodeTextSet::UnicodeTextSet(const string& prs_data) {
}
string UnicodeTextSet::serialize() const {
StringWriter header_w;
StringWriter data_w;
phosg::StringWriter header_w;
phosg::StringWriter data_w;
size_t total_num_strings = 0;
header_w.put_u32l(this->collections.size());
@@ -170,7 +170,7 @@ string UnicodeTextSet::serialize() const {
BinaryTextSet::BinaryTextSet(const std::string& pr2_data, size_t collection_count, bool has_rel_footer, bool is_sjis) {
auto pr2_decrypted = decrypt_pr2_data<false>(pr2_data);
auto decompressed = prs_decompress(pr2_decrypted.compressed_data);
StringReader r(decompressed);
phosg::StringReader r(decompressed);
// Annoyingly, there doesn't appear to be any bounds-checking on the language
// functions, so there are no counts of strings in each collection. We have to
@@ -181,7 +181,7 @@ BinaryTextSet::BinaryTextSet(const std::string& pr2_data, size_t collection_coun
? r.pget_u32l(r.size() - 0x10)
: (r.size() - collection_count * sizeof(le_uint32_t));
StringReader collection_offsets_r = r.sub(root_offset, collection_count * sizeof(le_uint32_t));
phosg::StringReader collection_offsets_r = r.sub(root_offset, collection_count * sizeof(le_uint32_t));
while (!collection_offsets_r.eof()) {
used_offsets.emplace(collection_offsets_r.get_u32l());
}
@@ -214,7 +214,7 @@ BinaryTextAndKeyboardsSet::BinaryTextAndKeyboardsSet(const string& pr2_data, boo
}
}
BinaryTextAndKeyboardsSet::BinaryTextAndKeyboardsSet(const JSON& json) {
BinaryTextAndKeyboardsSet::BinaryTextAndKeyboardsSet(const phosg::JSON& json) {
for (const auto& collection_json : json.at("collections").as_list()) {
auto& collection = this->collections.emplace_back();
for (const auto& string_json : collection_json->as_list()) {
@@ -236,14 +236,14 @@ BinaryTextAndKeyboardsSet::BinaryTextAndKeyboardsSet(const JSON& json) {
this->keyboard_selector_width = json.at("keyboard_selector_width").as_int();
}
JSON BinaryTextAndKeyboardsSet::json() const {
phosg::JSON BinaryTextAndKeyboardsSet::json() const {
auto collections_json = this->TextSet::json();
auto keyboards_json = JSON::list();
auto keyboards_json = phosg::JSON::list();
for (const auto& kb : this->keyboards) {
JSON keyboard_json = JSON::list();
phosg::JSON keyboard_json = phosg::JSON::list();
for (size_t y = 0; y < kb->size(); y++) {
const auto& row = kb->at(y);
JSON row_json = JSON::list();
phosg::JSON row_json = phosg::JSON::list();
for (size_t x = 0; x < row.size(); x++) {
row_json.emplace_back(row[x]);
}
@@ -251,7 +251,7 @@ JSON BinaryTextAndKeyboardsSet::json() const {
}
keyboards_json.emplace_back(std::move(keyboard_json));
}
return JSON::dict({
return phosg::JSON::dict({
{"collections", std::move(collections_json)},
{"keyboards", std::move(keyboards_json)},
{"keyboard_selector_width", this->keyboard_selector_width},
@@ -281,11 +281,8 @@ pair<string, string> BinaryTextAndKeyboardsSet::serialize(bool big_endian, bool
}
}
template <bool IsBigEndian>
template <bool BE>
void BinaryTextAndKeyboardsSet::parse_t(const string& pr2_data, bool is_sjis) {
using U32T = std::conditional_t<IsBigEndian, be_uint32_t, le_uint32_t>;
using U16T = std::conditional_t<IsBigEndian, be_uint16_t, le_uint16_t>;
auto& tt = is_sjis ? tt_sega_sjis_to_utf8 : tt_8859_to_utf8;
// The structure is as follows:
@@ -303,9 +300,9 @@ void BinaryTextAndKeyboardsSet::parse_t(const string& pr2_data, bool is_sjis) {
// char string[...\0]
// <EOF>
auto pr2_decrypted = decrypt_pr2_data<IsBigEndian>(pr2_data);
auto pr2_decrypted = decrypt_pr2_data<BE>(pr2_data);
auto decompressed = prs_decompress(pr2_decrypted.compressed_data);
StringReader r(decompressed);
phosg::StringReader r(decompressed);
// Annoyingly, there doesn't appear to be any bounds-checking on the language
// functions, so there are no counts of strings in each collection. We have to
@@ -314,54 +311,51 @@ void BinaryTextAndKeyboardsSet::parse_t(const string& pr2_data, bool is_sjis) {
::set<uint32_t> used_offsets;
used_offsets.emplace(r.size() - 8);
uint32_t keyboard_index_offset = r.pget<U32T>(r.size() - 8);
uint32_t keyboard_index_offset = r.pget<U32T<BE>>(r.size() - 8);
used_offsets.emplace(keyboard_index_offset);
size_t num_keyboards = r.pget_u8(keyboard_index_offset);
this->keyboard_selector_width = r.pget_u8(keyboard_index_offset + 1);
uint32_t keyboards_offset = r.pget<U32T>(keyboard_index_offset + 4);
uint32_t keyboards_offset = r.pget<U32T<BE>>(keyboard_index_offset + 4);
used_offsets.emplace(keyboards_offset);
while (this->keyboards.size() < num_keyboards) {
uint32_t keyboard_offset = r.pget<U32T>(keyboards_offset + 4 * this->keyboards.size());
uint32_t keyboard_offset = r.pget<U32T<BE>>(keyboards_offset + 4 * this->keyboards.size());
used_offsets.emplace(keyboard_offset);
auto& kb = this->keyboards.emplace_back(make_unique<Keyboard>());
auto key_r = r.sub(keyboard_offset, sizeof(Keyboard));
for (size_t y = 0; y < kb->size(); y++) {
auto& row = kb->at(y);
for (size_t x = 0; x < row.size(); x++) {
row[x] = key_r.get<U16T>();
row[x] = key_r.get<U16T<BE>>();
}
}
}
uint32_t collections_offset = r.pget<U32T>(r.size() - 4);
uint32_t collections_offset = r.pget<U32T<BE>>(r.size() - 4);
for (uint32_t offset = collections_offset; !used_offsets.count(offset); offset += 4) {
used_offsets.emplace(r.pget<U32T>(offset));
used_offsets.emplace(r.pget<U32T<BE>>(offset));
}
used_offsets.emplace(collections_offset);
for (uint32_t offset = collections_offset; (offset == collections_offset) || !used_offsets.count(offset); offset += 4) {
auto& collection = this->collections.emplace_back();
uint32_t first_string_offset_offset = r.pget<U32T>(offset);
uint32_t first_string_offset_offset = r.pget<U32T<BE>>(offset);
for (uint32_t string_offset_offset = first_string_offset_offset;
(string_offset_offset == first_string_offset_offset) || !used_offsets.count(string_offset_offset);
string_offset_offset += 4) {
collection.emplace_back(tt(r.pget_cstr(r.pget<U32T>(string_offset_offset))));
collection.emplace_back(tt(r.pget_cstr(r.pget<U32T<BE>>(string_offset_offset))));
}
}
}
template <bool IsBigEndian>
template <bool BE>
pair<string, string> BinaryTextAndKeyboardsSet::serialize_t(bool is_sjis) const {
using U32T = std::conditional_t<IsBigEndian, be_uint32_t, le_uint32_t>;
using U16T = std::conditional_t<IsBigEndian, be_uint16_t, le_uint16_t>;
auto& tt = is_sjis ? tt_utf8_to_sega_sjis : tt_utf8_to_8859;
StringWriter w;
phosg::StringWriter w;
::set<size_t> relocation_offsets;
auto put_offset_u32 = [&](uint32_t v) {
relocation_offsets.emplace(w.size());
w.put<U32T>(v);
w.put<U32T<BE>>(v);
};
uint32_t collections_offset;
@@ -401,7 +395,7 @@ pair<string, string> BinaryTextAndKeyboardsSet::serialize_t(bool is_sjis) const
for (size_t y = 0; y < keyboard->size(); y++) {
const auto& row = keyboard->at(y);
for (size_t x = 0; x < row.size(); x++) {
w.put<U16T>(row[x]);
w.put<U16T<BE>>(row[x]);
}
}
}
@@ -421,11 +415,11 @@ pair<string, string> BinaryTextAndKeyboardsSet::serialize_t(bool is_sjis) const
put_offset_u32(keyboard_index_offset);
put_offset_u32(collections_offset);
StringWriter reloc_w;
phosg::StringWriter reloc_w;
reloc_w.put_u32(0);
reloc_w.put<U32T>(relocation_offsets.size());
reloc_w.put<U32T<BE>>(relocation_offsets.size());
reloc_w.put_u64(0);
reloc_w.put<U32T>(w.size() - 8);
reloc_w.put<U32T<BE>>(w.size() - 8);
reloc_w.put_u32(0);
reloc_w.put_u64(0);
{
@@ -438,7 +432,7 @@ pair<string, string> BinaryTextAndKeyboardsSet::serialize_t(bool is_sjis) const
if (num_words > 0xFFFF) {
throw runtime_error("relocation offset too far away");
}
reloc_w.put<U16T>(num_words);
reloc_w.put<U16T<BE>>(num_words);
offset = reloc_offset;
}
}
@@ -447,8 +441,8 @@ pair<string, string> BinaryTextAndKeyboardsSet::serialize_t(bool is_sjis) const
const string& pr3_data = reloc_w.str();
string pr2_compressed = prs_compress_optimal(pr2_data.data(), pr2_data.size());
string pr3_compressed = prs_compress_optimal(pr3_data.data(), pr3_data.size());
string pr2_ret = encrypt_pr2_data<IsBigEndian>(pr2_compressed, pr2_data.size(), random_object<uint32_t>());
string pr3_ret = encrypt_pr2_data<IsBigEndian>(pr3_compressed, pr3_data.size(), random_object<uint32_t>());
string pr2_ret = encrypt_pr2_data<BE>(pr2_compressed, pr2_data.size(), phosg::random_object<uint32_t>());
string pr3_ret = encrypt_pr2_data<BE>(pr3_compressed, pr3_data.size(), phosg::random_object<uint32_t>());
return make_pair(std::move(pr2_ret), std::move(pr3_ret));
}
@@ -482,30 +476,30 @@ TextIndex::TextIndex(
for (const auto& it : bintext_filenames) {
string file_path = directory + "/" + subdirectory + "/" + it.first;
string json_path = file_path + ".json";
if (isfile(json_path)) {
this->log.info("Loading %s %c JSON text set from %s", name_for_enum(version), char_for_language_code(it.second), json_path.c_str());
this->add_set(version, it.second, make_shared<BinaryTextSet>(JSON::parse(load_file(json_path))));
} else if (isfile(file_path)) {
this->log.info("Loading %s %c binary text set from %s", name_for_enum(version), char_for_language_code(it.second), file_path.c_str());
this->add_set(version, it.second, make_set(load_file(file_path), it.second == 0));
if (phosg::isfile(json_path)) {
this->log.info("Loading %s %c JSON text set from %s", phosg::name_for_enum(version), char_for_language_code(it.second), json_path.c_str());
this->add_set(version, it.second, make_shared<BinaryTextSet>(phosg::JSON::parse(phosg::load_file(json_path))));
} else if (phosg::isfile(file_path)) {
this->log.info("Loading %s %c binary text set from %s", phosg::name_for_enum(version), char_for_language_code(it.second), file_path.c_str());
this->add_set(version, it.second, make_set(phosg::load_file(file_path), it.second == 0));
}
}
} else {
for (const auto& it : unitext_filenames) {
string file_path = directory + "/" + subdirectory + "/" + it.first;
string json_path = file_path + ".json";
if (isfile(json_path)) {
this->log.info("Loading %s %c JSON text set from %s", name_for_enum(version), char_for_language_code(it.second), json_path.c_str());
this->add_set(version, it.second, make_shared<UnicodeTextSet>(JSON::parse(load_file(json_path))));
if (phosg::isfile(json_path)) {
this->log.info("Loading %s %c JSON text set from %s", phosg::name_for_enum(version), char_for_language_code(it.second), json_path.c_str());
this->add_set(version, it.second, make_shared<UnicodeTextSet>(phosg::JSON::parse(phosg::load_file(json_path))));
} else {
auto patch_file = get_patch_file ? get_patch_file(version, it.first) : nullptr;
if (patch_file) {
this->log.info("Loading %s %c Unicode text set from %s in patch tree", name_for_enum(version), char_for_language_code(it.second), it.first.c_str());
this->log.info("Loading %s %c Unicode text set from %s in patch tree", phosg::name_for_enum(version), char_for_language_code(it.second), it.first.c_str());
this->add_set(version, it.second, make_set(*patch_file, it.second == 0));
} else {
if (isfile(file_path)) {
this->log.info("Loading %s %c Unicode text set from %s", name_for_enum(version), char_for_language_code(it.second), file_path.c_str());
this->add_set(version, it.second, make_set(load_file(file_path), it.second == 0));
if (phosg::isfile(file_path)) {
this->log.info("Loading %s %c Unicode text set from %s", phosg::name_for_enum(version), char_for_language_code(it.second), file_path.c_str());
this->add_set(version, it.second, make_set(phosg::load_file(file_path), it.second == 0));
}
}
}