Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions code/cfile/cfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
22 changes: 14 additions & 8 deletions code/cfile/cfilesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;

Expand Down
7 changes: 7 additions & 0 deletions code/cmdline/cmdline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions code/cmdline/cmdline.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
197 changes: 196 additions & 1 deletion code/localization/localize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
#include "playerman/player.h"
#include "mod_table/mod_table.h"
#include "options/Option.h"
#include "sound/audiostr.h"

#include <optional>
#include <stdexcept>

// ------------------------------------------------------------------------------------------------------------
// LOCALIZE DEFINES/VARS
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -156,6 +158,103 @@ auto LanguageOption = options::OptionBuilder<int>("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<int> 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<int> 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<int>("Audio.VoiceLanguage",
std::pair<const char*, int>{"Voice Language", 1942},
std::pair<const char*, int>{"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
Expand Down Expand Up @@ -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<int> 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
// "<name>-<ext>.<audio ext>" next to "<name>.<audio ext>"; the suffixed file is used only if it exists.
SCP_string lcl_get_voice_filename(const char *filename, int dir_type)
{
int lang = lcl_get_voice_lang_index();
if (!Lcl_languages.in_bounds(lang)) {
// the files as named (LCL_VOICE_ORIGINAL), or before lcl_init()
return filename;
}

const char *lang_ext = Lcl_languages[lang].lang_ext;
if (*lang_ext == '\0') {
return filename;
}

SCP_string localized = filename;

auto sep = localized.find_last_of("/\\");
auto name_start = (sep == SCP_string::npos) ? 0 : sep + 1;
cf_strip_extension(localized);
localized += "-";
localized += lang_ext;
// an extension for cf_find_file_location_ext() to strip, so that it keeps the "-<ext>" of "x_1.5-tw"
localized += audio_ext_list[0];

// the sound code copies file names into MAX_FILENAME_LEN buffers, and cfile asserts on the length
if (localized.length() >= MAX_FILENAME_LEN) {
mprintf(("Voice language: %s is too long for a file name, so %s plays\n", localized.c_str(), filename));
return filename;
}

auto res = cf_find_file_location_ext(localized.c_str(), NUM_AUDIO_EXT, audio_ext_list, dir_type);
if (!res.found) {
return filename;
}

// cfile looks a "dir/name" up in that directory but reports the name without it; keep the caller's directory
return localized.substr(0, name_start) + res.name_ext;
}

// whether a voice file exists: a file of the voice language counts, like the file as named
bool lcl_voice_file_exists(const char *filename, int dir_type)
{
return cf_exists_full_ext(lcl_get_voice_filename(filename, dir_type).c_str(), dir_type, NUM_AUDIO_EXT, audio_ext_list) != 0;
}

// initialize localization, if no language is passed - use the language specified in the registry
void lcl_init(int lang_init)
{
Expand Down Expand Up @@ -275,6 +451,22 @@ void lcl_init(int lang_init)

// set the language (this function takes care of setting up file pointers)
lcl_set_language(lang);

// show -voice_language in the options menu and disable the control; the lookup reads the flag itself
if (!Cmdline_voice_lang.empty()) {
auto voice_lang = voice_language_from_name(Cmdline_voice_lang);
if (voice_lang.has_value()) {
options::OptionsManager::instance()->setOverride("Audio.VoiceLanguage",
std::unique_ptr<json_t>(voice_language_serializer(*voice_lang)), "-voice_language");
} else {
Warning(LOCATION, "Unrecognized -voice_language value \"%s\"; ignoring.", Cmdline_voice_lang.c_str());
}
}

// a typo in $Voice language: can only be seen now that the languages are known
if (!Default_voice_language.empty() && !voice_language_from_name(Default_voice_language).has_value()) {
mprintf(("Game Settings Table: $Voice language: \"%s\" is not a language of this mod; voice files are used as named.\n", Default_voice_language.c_str()));
}
}

void lcl_close() {
Expand All @@ -296,6 +488,9 @@ void parse_stringstbl_quick(const char *filename)
while (required_string_either("#End","$Language:")) {
required_string("$Language:");
stuff_string(language.lang_name, F_RAW, LCL_LANG_NAME_LEN + 1);
if (!stricmp(language.lang_name, "original") || !stricmp(language.lang_name, "same-as-text")) {
Warning(LOCATION, "Language name '%s' is a reserved value of -voice_language and $Voice language:, so neither can select this language by name", language.lang_name);
}
if (optional_string("+XSTR:")) {
stuff_int(&language.xstr);
} else {
Expand Down
14 changes: 14 additions & 0 deletions code/localization/localize.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,20 @@ int lcl_find_lang_index_by_name(const SCP_string& lang);
// (translated, untranslated, and hybrid: internal translated, external untranslated)
int lcl_get_current_lang_index();

// values of the voice language besides an index into Lcl_languages
#define LCL_VOICE_ORIGINAL -1 // the voice files as named, with no language suffix (the default)
#define LCL_VOICE_TEXT_LANGUAGE -2 // the voice files of the text language

// get the index of the language whose voice files are used, or LCL_VOICE_ORIGINAL for the files as named
int lcl_get_voice_lang_index();

// get the file to load for a voice: "<name>-<ext>" if a file of the voice language exists in dir_type (a CF_TYPE_*),
// otherwise the name as given
SCP_string lcl_get_voice_filename(const char *filename, int dir_type);

// whether a voice file exists in dir_type, in the voice language or as named
bool lcl_voice_file_exists(const char *filename, int dir_type);

// initialize localization, if no language is passed - use the language specified in the registry
void lcl_init(int lang = -1);

Expand Down
7 changes: 4 additions & 3 deletions code/mission/missionmessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1292,11 +1292,11 @@ void message_load_wave(int index, const char *filename)
}

game_snd_entry tmp_gse;
strcpy_s(tmp_gse.filename, filename);
strcpy_s(tmp_gse.filename, lcl_get_voice_filename(filename, CF_TYPE_ANY).c_str());
Message_waves[index].num = snd_load(&tmp_gse, nullptr, 0);

if (!Message_waves[index].num.isValid())
nprintf(("messaging", "Cannot load message wave: %s. Will not play\n", Message_waves[index].name));
nprintf(("messaging", "Cannot load message wave: %s. Will not play\n", tmp_gse.filename));
}

// Goober5000
Expand Down Expand Up @@ -2042,7 +2042,8 @@ void message_queue_message( int message_num, int priority, int timing, const cha
if (message_filename_has_fs1_wingman_prefix(filename)) {
char converted[MAX_FILENAME_LEN];
message_filename_convert_to_command(converted, filename);
if (cf_exists_full_ext(converted, CF_TYPE_VOICE_SPECIAL, NUM_AUDIO_EXT, audio_ext_list)) {
// a Command file as named wins over the wingman file in the voice language, as for debriefing personas
if (lcl_voice_file_exists(converted, CF_TYPE_VOICE_SPECIAL)) {
convert_to_command = true;
}
}
Expand Down
Loading