mirror of
https://github.com/open-goal/jak-project
synced 2026-05-23 15:02:01 -04:00
006d24b29a
Resolves #3075 TODO before merge: - [x] Properly draw non-korean strings while in korean mode (language selection) - [x] Check jak 3 - [x] Translation scaffolding (allow korean characters, add to Crowdin, fix japanese locale, etc) - [x] Check translation of text lines - [x] Check translation of subtitle lines - [x] Cleanup PR / some performance optimization (it's take a bit too long to build the text and it shouldn't since the information is in a giant lookup table) - [x] Wait until release is cut I confirmed the font textures are identical between Jak 2 and Jak 3, so thank god for that. Some examples of converting the korean encoding to utf-8. These show off all scenarios, pure korean / korean with ascii and japanese / korean with replacements (flags): <img width="316" height="611" alt="Screenshot 2025-07-26 191511" src="https://github.com/user-attachments/assets/614383ba-8049-4bf4-937e-24ad3e605d41" /> <img width="254" height="220" alt="Screenshot 2025-07-26 191529" src="https://github.com/user-attachments/assets/1f6e5a6c-8527-4f98-a988-925ec66e437d" /> And it working in game. `Input Options` is a custom not-yet-translated string. It now shows up properly instead of a disgusting block of glyphs, and all the original strings are hopefully the same semantically!: <img width="550" height="493" alt="Screenshot 2025-07-26 202838" src="https://github.com/user-attachments/assets/9ebdf6c0-f5a3-4a30-84a1-e5840809a1a2" /> Quite the challenge. The crux of the problem is -- Naughty Dog came up with their own encoding for representing korean syllable blocks, and that source information is lost so it has to be reverse engineered. Instead of trying to figure out their encoding from the text -- I went at it from the angle of just "how do i draw every single korean character using their glyph set". One might think this is way too time consuming but it's important to remember: - Korean letters are designed to be composable from a relatively small number of glyphs (more on this later) - Someone at naughty dog did basically this exact process - There is no other way! While there are loose patterns, there isn't an overarching rhyme or reason, they just picked the right glyph for the writing context (more on this later). And there are even situations where there IS NO good looking glyph, or the one ND chose looks awful and unreadable (we could technically fix this by adjusting the positioning of the glyphs but....no more)! Information on their encoding that gets passed to `convert-korean-text`: - It's a raw stream of bytes - It can contain normal font letters - Every syllable block begins with: `0x04 <num_glyphs> <...the glyph bytes...>` - DO NOT confuse `num_glyphs` with num jamo, because some glyphs can have multiple jamo! - Every section of normal text starts with `0x03`. For example a space would be `0x03 0x20` - There are a very select few number of jamo glyphs on a secondary texture page, these glyph bytes are preceeded with a `0x05`. These jamo are a variant of some of the final vowels, moving them as low down as possible. Crash course on korean writing: - Nice resource as this is basically what we are doing - https://glyphsapp.com/learn/creating-a-hangeul-font - Korean syllable blocks have either 2 or 3 jamo. Jamo are basically letters and are the individual pieces that make up the syllable blocks. - The jamo are split up into "initial", "medial" and "final" categories. Within the "medial" category there are obvious visual variants: - Horizontal - Vertical - Combination (horizontal + a vertical) - These jamo are laid out in 6 main pre-defined "orientations": - initial + vertical medial - initial + horizontal medial - initial + combination - initial + vertical medial + final - initial + horizontal medial + final - initial + combination + final - Sometimes, for stylistic reasons, jamo will be written in different ways (ie. if there is nothing below a vertical vowel will be extended). - Annoying, and ND's glyph set supports this stylistic choice! - There are some combination of jamo that are never used, and some that are only used for a single word in the entire language! With all that in mind, my basic process was: - Scan the game's entire corpus of korean text, that includes subtitles. It's very easy to look at the font texture's glyphs and assign them to their respective jamo - This let me construct a mapping and see which glyphs were used under which context - I then shoved this information into a 2-D matrix in excel, and created an in-game tool to check every single jamo permutation to fill in the gaps / change them if naughty dogs was bad. Most of the time, ND's encoding was fine. - https://docs.google.com/spreadsheets/d/e/2PACX-1vTtyMeb5-mL5rXseS9YllVj32BGCISOGZFic6nkRV5Er5aLZ9CLq1Hj_rTY7pRCn-wrQDH1rvTqUHwB/pubhtml?gid=886895534&single=true anything in red is an addition / modification on my part. - This was the most lengthy part but not as long as you may think, you can do a lot of pruning. For example if you are checking a 3-jamo variant (the ones with the most permutations) and you've verified that the medial jamo is as far up vertically as it can be, and you are using the lowest final jamo that are available -- there is nothing to check or improve -- for better or worse! So those end up being the permutations between the initial and medial instead of a three-way permutation nightmare. - Also, while it is a 2d matrix, there's a lot of pruning even within that. For example, for the first 3 orientations, you dont have to care about final vowels at all. - At the end, I'm left with a lookup table that I can use the encode the best looking korean syllable blocks possible given the context of the jamo combination.
273 lines
11 KiB
C++
273 lines
11 KiB
C++
/*!
|
|
* @file game_text.cpp
|
|
* Builds the XCOMMON.TXT text files. Each file contains all the strings that appear in the game
|
|
* translated into a language.
|
|
*
|
|
* The decompiler/data/game_text.cpp file extracts text from the game and creates a file that
|
|
* can be read with these functions.
|
|
*
|
|
* Also builds the XSUBTIT.TXT text files. Each file contains all the strings used as subtitles for
|
|
* cutscenes, hints and ambient speech, along with the timings.
|
|
* This kind of file is completely custom.
|
|
*/
|
|
|
|
#include "game_text_common.h"
|
|
|
|
#include <algorithm>
|
|
#include <queue>
|
|
|
|
#include "DataObjectGenerator.h"
|
|
|
|
#include "common/goos/ParseHelpers.h"
|
|
#include "common/goos/Reader.h"
|
|
#include "common/util/FileUtil.h"
|
|
#include "common/util/font/font_utils.h"
|
|
#include "common/util/json_util.h"
|
|
#include "common/util/string_util.h"
|
|
|
|
#include "game/runtime.h"
|
|
|
|
#include "fmt/format.h"
|
|
|
|
namespace {
|
|
|
|
/*
|
|
(deftype game-text (structure)
|
|
((id uint32 :offset-assert 0)
|
|
(text basic :offset-assert 4)
|
|
)
|
|
)
|
|
|
|
(deftype game-text-info (basic)
|
|
((length int32 :offset-assert 4)
|
|
(language-id int32 :offset-assert 8)
|
|
(group-name basic :offset-assert 12)
|
|
(data game-text :dynamic :offset-assert 16)
|
|
)
|
|
)
|
|
*/
|
|
|
|
/*!
|
|
* Write game text data to a file. Uses the V2 object format which is identical between GOAL and
|
|
* OpenGOAL, so this should produce exactly identical files to what is found in the game.
|
|
*/
|
|
void compile_text(GameTextDB& db, const std::string& output_prefix) {
|
|
for (const auto& [group_name, banks] : db.groups()) {
|
|
for (const auto& [lang, bank] : banks) {
|
|
DataObjectGenerator gen;
|
|
gen.add_type_tag("game-text-info"); // type
|
|
gen.add_word(bank->lines().size()); // length
|
|
gen.add_word(lang); // language-id
|
|
// this string is found in the string pool.
|
|
gen.add_ref_to_string_in_pool(group_name); // group-name
|
|
|
|
// now add all the datas: (the lines are already sorted by id)
|
|
for (auto& [id, line] : bank->lines()) {
|
|
gen.add_word(id); // id
|
|
// these strings must be in the string pool, as sometimes there are duplicate
|
|
// strings in a single language, and these strings should be stored once and have multiple
|
|
// references to them.
|
|
gen.add_ref_to_string_in_pool(line); // text
|
|
}
|
|
|
|
auto data = gen.generate_v2();
|
|
|
|
file_util::create_dir_if_needed(file_util::get_file_path({"out", output_prefix, "iso"}));
|
|
file_util::write_binary_file(
|
|
file_util::get_file_path({"out", output_prefix, "iso",
|
|
fmt::format("{}{}.TXT", lang, str_util::to_upper(group_name))}),
|
|
data.data(), data.size());
|
|
}
|
|
}
|
|
}
|
|
|
|
/*!
|
|
* Write game subtitle data to a file. Uses the V2 object format which is identical between GOAL and
|
|
* OpenGOAL.
|
|
*/
|
|
void compile_subtitles_v1(GameSubtitleDB& db, const std::string& output_prefix) {
|
|
for (const auto& [lang, bank] : db.m_banks) {
|
|
// get font encoding information
|
|
auto font = get_font_bank(bank->m_text_version);
|
|
|
|
// convert speakers once.
|
|
auto speakers_converted = bank->m_speakers;
|
|
for (auto& [id, name] : speakers_converted) {
|
|
// convert name in-place. we copied the map earlier so this is safe.
|
|
// the subtitle lines have the speaker "id" stored in them, which is the map key here.
|
|
name = font->convert_utf8_to_game(name);
|
|
}
|
|
|
|
DataObjectGenerator gen;
|
|
gen.add_type_tag("subtitle-text-info"); // type
|
|
gen.add_word(bank->m_scenes.size()); // length
|
|
gen.add_word(lang); // lang
|
|
gen.add_word(0); // dummy
|
|
|
|
// fifo queue for scene data arrays
|
|
std::queue<int> array_link_sources;
|
|
// now add all the scene infos
|
|
for (auto& [name, scene] : bank->m_scenes) {
|
|
gen.add_word((u16)(scene.is_cutscene ? 0 : 2) |
|
|
(scene.m_lines.size() << 16)); // kind (lower 16 bits), length (upper 16 bits)
|
|
|
|
array_link_sources.push(gen.words());
|
|
gen.add_word(0); // keyframes (linked later)
|
|
|
|
gen.add_ref_to_string_in_pool(name);
|
|
gen.add_word(scene.m_hint_id);
|
|
}
|
|
// now add all the scene *data!* (keyframes)
|
|
for (auto& [name, scene] : bank->m_scenes) {
|
|
// link inline-array with reference from earlier
|
|
gen.link_word_to_word(array_link_sources.front(), gen.words());
|
|
array_link_sources.pop();
|
|
|
|
for (auto& subtitle : scene.m_lines) {
|
|
gen.add_word(subtitle.metadata.frame_start); // frame
|
|
gen.add_ref_to_string_in_pool(font->convert_utf8_to_game(subtitle.text)); // line
|
|
// speaker
|
|
if (subtitle.metadata.speaker.empty()) {
|
|
gen.add_ref_to_string_in_pool("");
|
|
} else {
|
|
auto it = speakers_converted.find(subtitle.metadata.speaker);
|
|
if (it == speakers_converted.end()) {
|
|
throw std::runtime_error(fmt::format("in file `{}`: could not find speaker {}",
|
|
bank->m_file_path, subtitle.metadata.speaker));
|
|
}
|
|
gen.add_ref_to_string_in_pool(it->second);
|
|
}
|
|
gen.add_word(subtitle.metadata.offscreen); // offscreen
|
|
}
|
|
}
|
|
|
|
auto data = gen.generate_v2();
|
|
|
|
file_util::create_dir_if_needed(file_util::get_file_path({"out", output_prefix, "iso"}));
|
|
file_util::write_binary_file(
|
|
file_util::get_file_path({"out", output_prefix, "iso",
|
|
fmt::format("{}{}.TXT", lang, str_util::to_upper("subtit"))}),
|
|
data.data(), data.size());
|
|
}
|
|
}
|
|
|
|
/*!
|
|
* Write game subtitle2 data to a file. Uses the V2 object format which is identical between GOAL
|
|
* and OpenGOAL.
|
|
*/
|
|
void compile_subtitles_v2(GameSubtitleDB& db, const std::string& output_prefix) {
|
|
for (const auto& [lang, bank] : db.m_banks) {
|
|
auto font = get_font_bank(bank->m_text_version);
|
|
DataObjectGenerator gen;
|
|
if (get_text_version_name(bank->m_text_version) == "jak3") {
|
|
gen.add_type_tag("subtitle3-text-info"); // type
|
|
} else {
|
|
gen.add_type_tag("subtitle2-text-info"); // type
|
|
}
|
|
gen.add_word((bank->m_scenes.size() & 0xffff) | (1 << 16)); // length (lo) + version (hi)
|
|
// note: we add 1 because "none" isn't included
|
|
gen.add_word((lang & 0xffff) | ((bank->m_speakers.size() + 1) << 16)); // lang + speaker-length
|
|
int speaker_array_link = gen.add_word(0); // speaker array (dummy for now)
|
|
|
|
// fifo queue for scene data arrays
|
|
std::queue<int> array_link_sources;
|
|
// now add all the scenes inline
|
|
for (auto& [name, scene] : bank->m_scenes) {
|
|
gen.add_ref_to_string_in_pool(name); // scene name
|
|
gen.add_word(scene.m_lines.size()); // line amount
|
|
array_link_sources.push(gen.words());
|
|
gen.add_word(0); // line array (linked later)
|
|
}
|
|
// now add all the line arrays and link them to their scene
|
|
for (auto& [name, scene] : bank->m_scenes) {
|
|
// link inline-array with reference from earlier
|
|
gen.link_word_to_word(array_link_sources.front(), gen.words());
|
|
array_link_sources.pop();
|
|
|
|
for (auto& line : scene.m_lines) {
|
|
gen.add_word_float(static_cast<float>(line.metadata.frame_start)); // start frame
|
|
gen.add_word_float(static_cast<float>(line.metadata.frame_end)); // end frame
|
|
if (line.metadata.merge) {
|
|
gen.add_symbol_link("#f");
|
|
} else {
|
|
if (font->is_language_id_korean(lang)) {
|
|
gen.add_ref_to_string_in_pool(
|
|
font->convert_utf8_to_game_korean(line.text)); // line text
|
|
} else {
|
|
gen.add_ref_to_string_in_pool(font->convert_utf8_to_game(line.text)); // line text
|
|
}
|
|
}
|
|
u16 speaker = bank->speaker_enum_value_from_name(line.metadata.speaker);
|
|
u16 flags = 0;
|
|
flags |= line.metadata.offscreen << 0;
|
|
flags |= line.metadata.merge << 1;
|
|
gen.add_word(speaker | (flags << 16)); // speaker (lo) + flags (hi)
|
|
}
|
|
}
|
|
// now write the array of strings for the speakers
|
|
// key word array -- it has to be in the right order.
|
|
gen.link_word_to_word(speaker_array_link, gen.words());
|
|
const auto localized_speakers = bank->speaker_names_ordered_by_enum_value();
|
|
for (auto& speaker_localized : localized_speakers) {
|
|
// No need to check for invalid speakers here, they are checked at the scene line level above
|
|
// and throw an error
|
|
if (font->is_language_id_korean(lang)) {
|
|
gen.add_ref_to_string_in_pool(font->convert_utf8_to_game_korean(speaker_localized));
|
|
} else {
|
|
gen.add_ref_to_string_in_pool(font->convert_utf8_to_game(speaker_localized));
|
|
}
|
|
}
|
|
|
|
auto data = gen.generate_v2();
|
|
|
|
file_util::create_dir_if_needed(file_util::get_file_path({"out", output_prefix, "iso"}));
|
|
auto file_name = get_text_version_name(bank->m_text_version) == "jak3" ? "subti3" : "subti2";
|
|
file_util::write_binary_file(
|
|
file_util::get_file_path({"out", output_prefix, "iso",
|
|
fmt::format("{}{}.TXT", lang, str_util::to_upper(file_name))}),
|
|
data.data(), data.size());
|
|
}
|
|
}
|
|
} // namespace
|
|
|
|
/*!
|
|
* Read a game text description file and generate GOAL objects.
|
|
*/
|
|
void compile_game_text(const std::vector<GameTextDefinitionFile>& files,
|
|
GameTextDB& db,
|
|
const std::string& output_prefix) {
|
|
goos::Reader reader;
|
|
for (auto& file : files) {
|
|
if (file.format == GameTextDefinitionFile::Format::GOAL) {
|
|
lg::print("[Build Game Text] GOAL {}\n", file.file_path);
|
|
auto code = reader.read_from_file({file.file_path});
|
|
parse_text_goal(code, db, file);
|
|
} else if (file.format == GameTextDefinitionFile::Format::JSON) {
|
|
lg::print("[Build Game Text] JSON {}\n", file.file_path);
|
|
auto file_path = file_util::get_jak_project_dir() / file.file_path;
|
|
auto json = parse_commented_json(file_util::read_text_file(file_path), file.file_path);
|
|
parse_text_json(json, db, file);
|
|
}
|
|
}
|
|
compile_text(db, output_prefix);
|
|
}
|
|
|
|
void compile_game_subtitles(const std::vector<GameSubtitleDefinitionFile>& files,
|
|
GameSubtitleDB& db,
|
|
const std::string& output_prefix) {
|
|
goos::Reader reader;
|
|
if (db.m_subtitle_version == GameSubtitleDB::SubtitleFormat::V1) {
|
|
for (auto& file : files) {
|
|
lg::print("[Build Game Subtitle V1] {}:{}\n", file.lines_path, file.meta_path);
|
|
db.init_banks_from_file(file);
|
|
}
|
|
compile_subtitles_v1(db, output_prefix);
|
|
} else {
|
|
for (auto& file : files) {
|
|
lg::print("[Build Game Subtitle V2] {}:{}\n", file.lines_path, file.meta_path);
|
|
db.init_banks_from_file(file);
|
|
}
|
|
compile_subtitles_v2(db, output_prefix);
|
|
}
|
|
}
|