diff --git a/code/cfile/cfile.h b/code/cfile/cfile.h index f5e99d8960b..69d4b425858 100644 --- a/code/cfile/cfile.h +++ b/code/cfile/cfile.h @@ -420,6 +420,10 @@ struct CFileLocationExt : public CFileLocation { // (NOTE: This function is exponentially slow, so don't use it unless truely needed!!) CFileLocationExt cf_find_file_location_ext(const char* filename, const int ext_num, const char** ext_list, int pathtype); +// Strip a file extension the way cf_find_file_location_ext() does: after the last directory separator (either kind), +// a dot starts an extension only when at least two characters follow it, so that "x_1.5" keeps its ".5". +void cf_strip_extension(SCP_string& filename); + // Functions to change directories int cfile_chdir(const char *dir); diff --git a/code/cfile/cfilesystem.cpp b/code/cfile/cfilesystem.cpp index 545ab62d105..288d1a18995 100644 --- a/code/cfile/cfilesystem.cpp +++ b/code/cfile/cfilesystem.cpp @@ -1436,6 +1436,19 @@ CFileLocation cf_find_file_location(const char* filespec, int pathtype, uint32_t // -- from parselo.cpp -- extern char *stristr(char *str, const char *substr); +// (NOTE: to be fully retail compatible, we need to support multiple periods for something like *_1.5.wav, +// which means that we need to strip a length of >2 only, assuming that all valid ext are at least 2 chars) +void cf_strip_extension(SCP_string& filename) +{ + auto sep = filename.find_last_of("/\\"); + auto name_start = (sep == SCP_string::npos) ? 0 : sep + 1; + auto dot = filename.rfind('.'); + + if ( (dot != SCP_string::npos) && (dot >= name_start) && ((filename.length() - dot) > 2) ) { + filename.erase(dot); + } +} + /** * Searches for a file. * @@ -1493,14 +1506,7 @@ CFileLocationExt cf_find_file_location_ext(const char *filename, const int ext_n filespec.erase(0, seperator+1); } - // strip any existing extension - // (NOTE: to be fully retail compatible, we need to support multiple periods for something like *_1.5.wav, - // which means that we need to strip a length of >2 only, assuming that all valid ext are at least 2 chars) - auto dot = filespec.rfind('.'); - - if ( (dot != SCP_string::npos) && ((filespec.length() - dot) > 2) ) { - filespec.erase(dot); - } + cf_strip_extension(filespec); SCP_string filespec_ext; 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..46825a67f8c 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 1946 // 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,103 @@ 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 "same-as-text", the words of -voice_language and $Voice language:. +static json_t* voice_language_serializer(int lang) +{ + if (lang == LCL_VOICE_TEXT_LANGUAGE) { + return json_string("same-as-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, "same-as-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", 1944); + } + if (value < 0) { + return XSTR("Original voice", 1945); + } + return language_display(value); +} + +// -voice_language and $Voice language: take a language name, "same-as-text" or "original"; the two words are +// reserved, and a language with one of these names gets a warning when the language table is read +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", 1942}, + std::pair{"The language of the voice files, when the mod has them in more than one language. Voice files that are already loaded keep their language.", 1943}) + .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)) + .importance(-1) // below the speech (TTS) options, which share importance 0 and would otherwise mix with it + .default_func(voice_language_mod_default) + .finish(); + // ------------------------------------------------------------------------------------------------------------ // LOCALIZE FUNCTIONS @@ -184,6 +283,83 @@ int lcl_get_current_lang_index() return LCL_DEFAULT; } +// the language whose voice files are used, or LCL_VOICE_ORIGINAL. Precedence: -voice_language, the player's +// choice, $Voice language:, the files as named; "same as text" is never the default, so nothing set changes nothing +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 +// "-.