From b605b4a7b210363ac2f1441b9771c103cd154d42 Mon Sep 17 00:00:00 2001 From: Isaac Huang Date: Wed, 11 Nov 2026 03:37:52 +0800 Subject: [PATCH 1/5] feat(localize): add a voice language with its own file lookup Players can now hear voice files in a language other than the text language, for example Chinese text with English voice. A mod ships a voice in another language as "-." next to "", the same naming that movie subtitles and fiction files use. The voice language comes from, in this order: the -voice_language flag, the player's stored choice of the in-game option Audio.VoiceLanguage (only when in-game options are on), $Voice language: in game_settings.tbl, and else the original voice. Each source takes a language name, "same-as-text" or "original". With nothing set, voice files load as named, with no extra file-system access, for every text language, so existing installs do not change. A stored player choice, including "Same as text language" and "Original voice", wins over the mod default; while the player has not chosen, the menu shows the mod default. A stored value that the mod does not know counts as not chosen. lcl_get_voice_filename() resolves a name: it follows the cfile extension rule (a dot starts an extension only when at least two characters follow it, and only after the last directory separator), keeps the directory prefix, falls back to the name as given when the suffixed file does not exist or the name would not fit in MAX_FILENAME_LEN, and leaves the name alone before lcl_init(). lcl_voice_file_exists() is the same check for a given folder type. An unknown -voice_language value is reported by a Warning; an unknown $Voice language: value is logged and gives the original voice. The tests save and restore the global language list, so they do not depend on the order in which tests run. Co-Authored-By: Claude Fable 5.1 Co-Authored-By: Claude Opus 5.5 --- code/cmdline/cmdline.cpp | 7 + code/cmdline/cmdline.h | 1 + code/localization/localize.cpp | 201 ++++++++- code/localization/localize.h | 14 + code/mod_table/mod_table.cpp | 8 + code/mod_table/mod_table.h | 1 + test/src/localization/test_voice_language.cpp | 420 ++++++++++++++++++ test/src/source_groups.cmake | 4 + .../data/tables/game_settings.tbl | 6 + .../data/tables/game_settings.tbl | 6 + .../ModDefault/data/tables/game_settings.tbl | 6 + .../data/tables/game_settings.tbl | 6 + .../data/tables/game_settings.tbl | 6 + .../data/tables/game_settings.tbl | 6 + .../data/tables/game_settings.tbl | 6 + .../voice_language/data/tables/zh-lcl.tbm | 5 + .../data/voice/briefing/bar.ogg | Bin 0 -> 2669 bytes .../data/voice/briefing/dot_1-tw.ogg | Bin 0 -> 2669 bytes .../data/voice/briefing/dot_1.5-tw.ogg | Bin 0 -> 2669 bytes .../data/voice/briefing/dot_1.5.ogg | Bin 0 -> 2669 bytes .../data/voice/briefing/dot_1.ogg | Bin 0 -> 2669 bytes .../data/voice/briefing/foo-tw.ogg | Bin 0 -> 2669 bytes .../data/voice/briefing/foo.ogg | Bin 0 -> 2669 bytes .../briefing/long_name_1234567890abcde-tw.ogg | Bin 0 -> 2669 bytes .../data/voice/briefing/none-tw.ogg | Bin 0 -> 2669 bytes .../data/voice/briefing/sub.d/foo-tw.ogg | Bin 0 -> 2669 bytes .../data/voice/briefing/sub/foo-tw.ogg | Bin 0 -> 2669 bytes .../data/voice/briefing/sub/foo.ogg | Bin 0 -> 2669 bytes .../data/voice/debriefing/9_onlytw-tw.ogg | Bin 0 -> 2669 bytes .../data/voice/special/command_onlytw-tw.ogg | Bin 0 -> 2669 bytes 30 files changed, 702 insertions(+), 1 deletion(-) create mode 100644 test/src/localization/test_voice_language.cpp create mode 100644 test/test_data/voice_language/BogusStoredStringFallsBackToTheModDefault/data/tables/game_settings.tbl create mode 100644 test/test_data/voice_language/FlagBeatsModDefault/data/tables/game_settings.tbl create mode 100644 test/test_data/voice_language/ModDefault/data/tables/game_settings.tbl create mode 100644 test/test_data/voice_language/ModDefaultSameAsText/data/tables/game_settings.tbl create mode 100644 test/test_data/voice_language/ModDefaultUnknown/data/tables/game_settings.tbl create mode 100644 test/test_data/voice_language/PlayerChoiceBeatsTheModDefault/data/tables/game_settings.tbl create mode 100644 test/test_data/voice_language/StaleStoredLanguageFallsBackToTheModDefault/data/tables/game_settings.tbl create mode 100644 test/test_data/voice_language/data/tables/zh-lcl.tbm create mode 100644 test/test_data/voice_language/data/voice/briefing/bar.ogg create mode 100644 test/test_data/voice_language/data/voice/briefing/dot_1-tw.ogg create mode 100644 test/test_data/voice_language/data/voice/briefing/dot_1.5-tw.ogg create mode 100644 test/test_data/voice_language/data/voice/briefing/dot_1.5.ogg create mode 100644 test/test_data/voice_language/data/voice/briefing/dot_1.ogg create mode 100644 test/test_data/voice_language/data/voice/briefing/foo-tw.ogg create mode 100644 test/test_data/voice_language/data/voice/briefing/foo.ogg create mode 100644 test/test_data/voice_language/data/voice/briefing/long_name_1234567890abcde-tw.ogg create mode 100644 test/test_data/voice_language/data/voice/briefing/none-tw.ogg create mode 100644 test/test_data/voice_language/data/voice/briefing/sub.d/foo-tw.ogg create mode 100644 test/test_data/voice_language/data/voice/briefing/sub/foo-tw.ogg create mode 100644 test/test_data/voice_language/data/voice/briefing/sub/foo.ogg create mode 100644 test/test_data/voice_language/data/voice/debriefing/9_onlytw-tw.ogg create mode 100644 test/test_data/voice_language/data/voice/special/command_onlytw-tw.ogg diff --git a/code/cmdline/cmdline.cpp b/code/cmdline/cmdline.cpp index 4042cea4477..10a37cc1655 100644 --- a/code/cmdline/cmdline.cpp +++ b/code/cmdline/cmdline.cpp @@ -444,9 +444,11 @@ char *Cmdline_gateway_ip = nullptr; cmdline_parm portable_mode("-portable_mode", NULL, AT_NONE); cmdline_parm joy_info("-joy_info", "Outputs SDL joystick info", AT_NONE); cmdline_parm lang_arg("-language", "Language name as defined in strings.tbl", AT_STRING); +cmdline_parm voice_lang_arg("-voice_language", "Language name as defined in strings.tbl, for voice files only; or same-as-text, or original", AT_STRING); bool Cmdline_portable_mode = false; SCP_string Cmdline_lang; +SCP_string Cmdline_voice_lang; // Troubleshooting cmdline_parm loadallweapons_arg("-loadallweps", NULL, AT_NONE); // Cmdline_load_all_weapons @@ -2227,6 +2229,11 @@ bool SetCmdlineParams() Cmdline_lang = lang_arg.str(); } + if (voice_lang_arg.found()) + { + Cmdline_voice_lang = voice_lang_arg.str(); + } + if (no_bsp_align_arg.found()) { Cmdline_no_bsp_align = true; } diff --git a/code/cmdline/cmdline.h b/code/cmdline/cmdline.h index 43a698b6d6c..214f44de41d 100644 --- a/code/cmdline/cmdline.h +++ b/code/cmdline/cmdline.h @@ -104,6 +104,7 @@ extern char *Cmdline_gateway_ip; // Launcher related options extern bool Cmdline_portable_mode; extern SCP_string Cmdline_lang; +extern SCP_string Cmdline_voice_lang; // Troubleshooting extern int Cmdline_load_all_weapons; diff --git a/code/localization/localize.cpp b/code/localization/localize.cpp index d7559d3dba5..33ee01fe9dc 100644 --- a/code/localization/localize.cpp +++ b/code/localization/localize.cpp @@ -21,8 +21,10 @@ #include "playerman/player.h" #include "mod_table/mod_table.h" #include "options/Option.h" +#include "sound/audiostr.h" #include +#include // ------------------------------------------------------------------------------------------------------------ // LOCALIZE DEFINES/VARS @@ -65,7 +67,7 @@ bool *Lcl_unexpected_tstring_check = nullptr; // NOTE: with map storage of XSTR strings, the indexes no longer need to be contiguous, // but internal strings should still increment XSTR_SIZE to avoid collisions. // retail XSTR_SIZE = 1570 -// #define XSTR_SIZE 1942 // This is the next available ID +// #define XSTR_SIZE 1944 // This is the next available ID // struct to allow for strings.tbl-determined x offset // offset is 0 for english, by default @@ -156,6 +158,101 @@ auto LanguageOption = options::OptionBuilder("Game.Language", .default_val(0) .finish(); +// Besides a language, the voice language can be the files as named (LCL_VOICE_ORIGINAL) or the files of the text +// language (LCL_VOICE_TEXT_LANGUAGE). A language is stored like Game.Language, as {name, ext}; the two other +// values as the strings "original" and "text". +static json_t* voice_language_serializer(int lang) +{ + if (lang == LCL_VOICE_TEXT_LANGUAGE) { + return json_string("text"); + } + if (lang < 0) { + return json_string("original"); + } + return language_serializer(lang); +} + +static int voice_language_deserializer(const json_t* value) +{ + if (json_is_string(value)) { + const char* name = json_string_value(value); + if (!stricmp(name, "text")) { + return LCL_VOICE_TEXT_LANGUAGE; + } + if (!stricmp(name, "original")) { + return LCL_VOICE_ORIGINAL; + } + // a hand-edited value: as if the player had not chosen + throw std::runtime_error("unknown voice language value"); + } + + int lang = language_deserializer(value); + if (lang < 0) { + // a language this mod does not have: as if the player had not chosen, so that the default applies + throw std::runtime_error("unknown voice language"); + } + return lang; +} + +static SCP_vector voice_language_enumerator() +{ + auto vals = language_enumerator(); + vals.insert(vals.begin(), {LCL_VOICE_ORIGINAL, LCL_VOICE_TEXT_LANGUAGE}); + return vals; +} + +static SCP_string voice_language_display(int value) +{ + if (value == LCL_VOICE_TEXT_LANGUAGE) { + return XSTR("Same as text language", 1942); + } + if (value < 0) { + return XSTR("Original voice", 1943); + } + return language_display(value); +} + +// -voice_language and $Voice language: take a language name, "same-as-text" or "original" +static std::optional voice_language_from_name(const SCP_string& name) +{ + if (!stricmp(name.c_str(), "original")) { + return LCL_VOICE_ORIGINAL; + } + if (!stricmp(name.c_str(), "same-as-text")) { + return LCL_VOICE_TEXT_LANGUAGE; + } + + int lang = lcl_find_lang_index_by_name(name); + if (lang < 0) { + return std::nullopt; + } + return lang; +} + +// the mod default from $Voice language:, which the option also shows and uses while the player has not chosen +static int voice_language_mod_default() +{ + if (Default_voice_language.empty()) { + return LCL_VOICE_ORIGINAL; + } + return voice_language_from_name(Default_voice_language).value_or(LCL_VOICE_ORIGINAL); +} + +auto VoiceLanguageOption = options::OptionBuilder("Audio.VoiceLanguage", + std::pair{"Voice Language", 1940}, + std::pair{"The language of the voice files, when the mod has them in more than one language", 1941}) + .deserializer(voice_language_deserializer) + .serializer(voice_language_serializer) + .enumerator(voice_language_enumerator) + .display(voice_language_display) + .change_listener([](int, bool){ + return true; // voice files are looked up when they are loaded, so no restart is needed + }) + .flags({options::OptionFlags::ForceMultiValueSelection}) + .category(std::make_pair("Audio", 1826)) + .default_func(voice_language_mod_default) + .finish(); + // ------------------------------------------------------------------------------------------------------------ // LOCALIZE FUNCTIONS @@ -184,6 +281,91 @@ int lcl_get_current_lang_index() return LCL_DEFAULT; } +// get the index of the language whose voice files are used, or LCL_VOICE_ORIGINAL for the files as named. This is +// the one place that decides the precedence: the -voice_language flag, then the player's choice, then the mod +// default from $Voice language:, then the files as named. "Same as text language" is a value each of them can +// give; it is not the default, so that nothing changes while nothing is set. +int lcl_get_voice_lang_index() +{ + std::optional lang; + + if (!Cmdline_voice_lang.empty()) { + // an unknown name was reported once by lcl_init() and does not count + lang = voice_language_from_name(Cmdline_voice_lang); + } + + if (!lang.has_value() && Using_in_game_options) { + // the player's choice; while the player has not chosen, the option reports the mod default + lang = VoiceLanguageOption->getValue(); + } + + if (!lang.has_value()) { + lang = voice_language_mod_default(); + } + + if (*lang == LCL_VOICE_TEXT_LANGUAGE) { + lang = lcl_get_current_lang_index(); + } + + if (!Lcl_languages.in_bounds(*lang)) { + return LCL_VOICE_ORIGINAL; + } + + return *lang; +} + +// get the file to load for a voice. Like movie subtitles and fiction files, a voice in another language is +// "-.