diff --git a/code/cfile/cfile.cpp b/code/cfile/cfile.cpp index 6fac45be3c0..7d7a21a3c04 100644 --- a/code/cfile/cfile.cpp +++ b/code/cfile/cfile.cpp @@ -637,7 +637,7 @@ void cf_create_directory(int dir_type, uint32_t location_flags) // CFILE* _cfopen(const char* source, int line, const char* file_path, const char* mode, int dir_type, - bool /* localize */, uint32_t location_flags) + bool localize, uint32_t location_flags) { /* Bobboau, what is this doing here? 31 is way too short... - Goober5000 if( strlen(file_path) > 31 ) @@ -723,7 +723,14 @@ CFILE* _cfopen(const char* source, int line, const char* file_path, const char* //================================================ // Search for file on disk, on cdrom, or in a packfile - auto find_res = cf_find_file_location(file_path, dir_type, location_flags); + CFileLocation find_res; + + // the localized copy has to satisfy location_flags too, or the original is opened + if (localize) + find_res = cf_find_file_location(cf_localize_filename(file_path, dir_type).c_str(), dir_type, location_flags); + + if (!find_res.found) + find_res = cf_find_file_location(file_path, dir_type, location_flags); if ( find_res.found ) { // Fount it, now create a cfile out of it diff --git a/code/cfile/cfile.h b/code/cfile/cfile.h index f5e99d8960b..63b4b1d1a74 100644 --- a/code/cfile/cfile.h +++ b/code/cfile/cfile.h @@ -176,7 +176,8 @@ const char *cf_get_filename(const CFILE *cfile); int cf_get_dir_type(const CFILE *cfile); // Opens the file. If no path is given, use the extension to look into the -// default path. If mode is NULL, delete the file. +// default path. If mode is NULL, delete the file. When reading, localize +// opens the copy in the current language's subfolder if there is one. CFILE* _cfopen(const char* source_file, int line, const char* filename, const char* mode, int dir_type = CF_TYPE_ANY, bool localize = false, uint32_t location_flags = CF_LOCATION_ALL); #define cfopen(...) _cfopen(LOCATION, __VA_ARGS__) // Pass source location to the function @@ -420,6 +421,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); +// the localized version of a file (in the current language's subfolder) if it exists, otherwise the file as given +// the language folder goes directly before the filename, so for a bare name it must be directly inside the path type's folder +SCP_string cf_localize_filename(const char* filename, int pathtype, int num_ext = 0, const char** ext_list = nullptr); + // Functions to change directories int cfile_chdir(const char *dir); diff --git a/code/cfile/cfilesystem.cpp b/code/cfile/cfilesystem.cpp index 545ab62d105..a2a41a5f503 100644 --- a/code/cfile/cfilesystem.cpp +++ b/code/cfile/cfilesystem.cpp @@ -39,6 +39,7 @@ #include "cmdline/cmdline.h" #include "globalincs/pstypes.h" #include "def_files/def_files.h" +#include "localization/localize.h" #include "osapi/osapi.h" #include "parse/parselo.h" @@ -1223,11 +1224,32 @@ static bool is_absolute_path(const char *path) #endif } +// whether the innermost folder of a sub path (which ends with a separator) is named after a language extension +static bool is_language_sub_path(const SCP_string &sub_path) +{ + if (sub_path.size() < 2) + return false; + + size_t end = sub_path.size() - 1; + auto sep = sub_path.rfind(DIR_SEPARATOR_CHAR, end - 1); + size_t start = (sep == SCP_string::npos) ? 0 : sep + 1; + size_t len = end - start; + + for (const auto& language : Lcl_languages) + { + if (strlen(language.lang_ext) == len && !strnicmp(sub_path.c_str() + start, language.lang_ext, len)) + return true; + } + + return false; +} + static bool sub_path_match(const SCP_string &search, const SCP_string &index) { - // if search path is empty then we only care about the file name + // if search path is empty then we only care about the file name, + // except that localized files are only found by naming their language subfolder if (search.empty()) { - return true; + return index.empty() || !is_language_sub_path(index); } // if we have search but no index then fail @@ -1662,6 +1684,33 @@ CFileLocationExt cf_find_file_location_ext(const char *filename, const int ext_n return CFileLocationExt(); } +SCP_string cf_localize_filename(const char *filename, int pathtype, int num_ext, const char **ext_list) +{ + Assertion(filename != nullptr, "filename must not be null!"); + + int lang = lcl_get_current_lang_index(); + if (!Lcl_languages.in_bounds(lang) || Lcl_languages[lang].lang_ext[0] == '\0') + return filename; + + SCP_string localized = filename; + lcl_add_dir_to_path_with_filename(localized); + + // cf_find_file_location_ext() and the sound code's buffers are limited to MAX_FILENAME_LEN + if (localized.length() >= MAX_FILENAME_LEN) + { + mprintf(("CFILE: localized file name %s is too long, so %s will be used\n", localized.c_str(), filename)); + return filename; + } + + bool found; + if (num_ext > 0) + found = cf_find_file_location_ext(localized.c_str(), num_ext, ext_list, pathtype).found; + else + found = cf_find_file_location(localized.c_str(), pathtype).found; + + return found ? localized : SCP_string(filename); +} + // Returns true if filename matches filespec, else zero if not int cf_matches_spec(const char *filespec, const char *filename) @@ -2312,6 +2361,11 @@ int cf_get_file_list_preallocated(int max, char arr[][MAX_FILENAME_LEN], char** continue; } + // sub paths aren't supported here, so this only skips language subfolders + if ( !sub_path_match("", f->sub_path) ) { + continue; + } + if ( cf_file_already_in_list_preallocated(num_files, arr, f->name_ext.c_str()) ) { continue; } diff --git a/code/globalincs/vmallocator.h b/code/globalincs/vmallocator.h index 2c8f792f88e..31bf633e68d 100644 --- a/code/globalincs/vmallocator.h +++ b/code/globalincs/vmallocator.h @@ -60,16 +60,6 @@ bool SCP_vector_contains(const SCP_vector& vector, const T& item) { return std::find(vector.begin(), vector.end(), item) != vector.end(); } -template -bool SCP_vector_contains_lcase(const SCP_vector& vector, const T& item) { - return std::find_if(vector.begin(), vector.end(), [&item](const T& iterator_item) { return lcase_equal(iterator_item, item); }) != vector.end(); -} - -template -bool SCP_vector_contains_lcase(const SCP_vector& vector, const char* item) { - return std::find_if(vector.begin(), vector.end(), [&item](const T& iterator_item) { return !stricmp(iterator_item.c_str(), item); }) != vector.end(); -} - template inline bool SCP_vector_inbounds(const SCP_vector& vector, int idx) { return ((idx >= 0) && (static_cast(idx) < vector.size())); @@ -118,6 +108,18 @@ extern bool SCP_trim(SCP_string& str); extern bool lcase_equal(const SCP_string& _Left, const SCP_string& _Right); extern bool lcase_lessthan(const SCP_string& _Left, const SCP_string& _Right); +template +bool SCP_vector_contains_lcase(const SCP_vector& vector, const T& item) +{ + return std::find_if(vector.begin(), vector.end(), [&item](const T& iterator_item) { return lcase_equal(iterator_item, item); }) != vector.end(); +} + +template +bool SCP_vector_contains_lcase(const SCP_vector& vector, const char* item) +{ + return std::find_if(vector.begin(), vector.end(), [&item](const T& iterator_item) { return !stricmp(iterator_item.c_str(), item); }) != vector.end(); +} + template > class SCP_map : public std::map>> diff --git a/code/graphics/software/FontManager.cpp b/code/graphics/software/FontManager.cpp index 34296b3c050..60ce9ccce68 100644 --- a/code/graphics/software/FontManager.cpp +++ b/code/graphics/software/FontManager.cpp @@ -10,7 +10,6 @@ #include "bmpman/bmpman.h" #include "cfile/cfile.h" -#include "localization/localize.h" #include "options/Option.h" namespace font @@ -156,19 +155,7 @@ namespace font return data; } - // try localized version first - CFILE* fp = nullptr; - SCP_string typeface_lcl = typeface; - - lcl_add_dir_to_path_with_filename(typeface_lcl); - - fp = cfopen(typeface_lcl.c_str(), "rb", CF_TYPE_ANY); - - // fallback if not found - if ( !fp ) - { - fp = cfopen(typeface.c_str(), "rb", CF_TYPE_ANY); - } + CFILE* fp = cfopen(typeface.c_str(), "rb", CF_TYPE_ANY, true); if ( !fp ) { @@ -479,7 +466,7 @@ namespace font auto iter = allocatedData.find(fileName); if (iter == allocatedData.end()) { - CFILE *fontFile = cfopen(fileName.c_str(), "rb", CF_TYPE_ANY); + CFILE *fontFile = cfopen(fileName.c_str(), "rb", CF_TYPE_ANY, true); if (fontFile == nullptr) { diff --git a/code/mission/missionmessage.cpp b/code/mission/missionmessage.cpp index 5bd686668ab..e83824f0951 100644 --- a/code/mission/missionmessage.cpp +++ b/code/mission/missionmessage.cpp @@ -1292,7 +1292,7 @@ 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, cf_localize_filename(filename, CF_TYPE_ANY, NUM_AUDIO_EXT, audio_ext_list).c_str()); Message_waves[index].num = snd_load(&tmp_gse, nullptr, 0); if (!Message_waves[index].num.isValid()) @@ -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)) { + auto localized = cf_localize_filename(converted, CF_TYPE_VOICE_SPECIAL, NUM_AUDIO_EXT, audio_ext_list); + if (cf_exists_full_ext(localized.c_str(), CF_TYPE_VOICE_SPECIAL, NUM_AUDIO_EXT, audio_ext_list)) { convert_to_command = true; } } diff --git a/code/mission/missiontraining.cpp b/code/mission/missiontraining.cpp index 9a507244fca..1fff85b2986 100644 --- a/code/mission/missiontraining.cpp +++ b/code/mission/missiontraining.cpp @@ -826,9 +826,6 @@ SCP_string message_translate_tokens(const char *text) */ int message_play_training_voice(int index) { - int len; - CFILE *fp; - if (index < 0) { if (Training_voice >= 0) { if (Training_voice_type) { @@ -845,13 +842,16 @@ int message_play_training_voice(int index) } if (!Message_waves[index].num.isValid()) { - fp = cfopen(Message_waves[index].name, "rb"); - if (!fp) + // even though we localize the filename here, if we use the audiostream branch, we pass the original filename; + // because audiostream_open() performs its own localization + auto localized = cf_localize_filename(Message_waves[index].name, CF_TYPE_ANY, NUM_AUDIO_EXT, audio_ext_list); + + // check the size of the localized file: large files get streamed, small files get played as sounds + auto res = cf_find_file_location_ext(localized.c_str(), NUM_AUDIO_EXT, audio_ext_list, CF_TYPE_ANY); + if (!res.found) return -1; - len = cfilelength(fp); - cfclose(fp); - if (len > 100000) { + if (res.size > 100000) { if ((Training_voice < 0) || !Training_voice_type || (Training_voice != index)) { if (Training_voice >= 0) { if (Training_voice_type) { @@ -880,7 +880,7 @@ int message_play_training_voice(int index) } else { game_snd_entry tmp_gse; - strcpy_s(tmp_gse.filename, Message_waves[index].name); + strcpy_s(tmp_gse.filename, localized.c_str()); Message_waves[index].num = snd_load(&tmp_gse, nullptr, 0); if (!Message_waves[index].num.isValid()) { nprintf(("Warning", "Cannot load message wave: %s. Will not play\n", Message_waves[index].name)); diff --git a/code/missionui/missiondebrief.cpp b/code/missionui/missiondebrief.cpp index acd4a06937b..06f53ea115b 100644 --- a/code/missionui/missiondebrief.cpp +++ b/code/missionui/missiondebrief.cpp @@ -925,7 +925,8 @@ void debrief_choose_voice(char *voice_dest, size_t buf_size, char *voice_base, i if (snprintf(voice_dest, buf_size, NOX("%d_%s"), persona_index, voice_base) < static_cast(buf_size)) { // if it exists, we're done - if (cf_exists_full_ext(voice_dest, CF_TYPE_VOICE_DEBRIEFINGS, NUM_AUDIO_EXT, audio_ext_list)) + auto localized = cf_localize_filename(voice_dest, CF_TYPE_VOICE_DEBRIEFINGS, NUM_AUDIO_EXT, audio_ext_list); + if (cf_exists_full_ext(localized.c_str(), CF_TYPE_VOICE_DEBRIEFINGS, NUM_AUDIO_EXT, audio_ext_list)) return; } diff --git a/code/scpui/RocketFileInterface.cpp b/code/scpui/RocketFileInterface.cpp index 2abf6a63071..e1088a8327f 100644 --- a/code/scpui/RocketFileInterface.cpp +++ b/code/scpui/RocketFileInterface.cpp @@ -35,7 +35,8 @@ FileHandle RocketFileInterface::Open(const String& path) return 0; } - return (FileHandle)cfopen(name.c_str(), "rb", dir_type); + // open the copy in the current language's subfolder, if there is one + return (FileHandle)cfopen(name.c_str(), "rb", dir_type, true); } void RocketFileInterface::Close(FileHandle file) { cfclose((CFILE*)file); } size_t RocketFileInterface::Read(void* buffer, size_t size, FileHandle file) diff --git a/code/scpui/rocket_ui.cpp b/code/scpui/rocket_ui.cpp index c1405068431..73d169ad6d0 100644 --- a/code/scpui/rocket_ui.cpp +++ b/code/scpui/rocket_ui.cpp @@ -14,6 +14,7 @@ #include "cfile/cfile.h" #include "io/mouse.h" +#include "localization/localize.h" #include "mod_table/mod_table.h" #include "osapi/osapi.h" #include "scpui/IncludeNodeHandler.h" @@ -134,22 +135,40 @@ int get_modifier_state() return mods; } -void load_fonts() +// the fonts with this extension, including those that only exist in the current language's subfolder +SCP_vector list_fonts(const char* ext) { SCP_vector files; - cf_get_file_list(files, CF_TYPE_FONT, "*.ttf"); + SCP_string filter = SCP_string("*") + ext; + cf_get_file_list(files, CF_TYPE_FONT, filter.c_str()); - for (auto& name : files) { - // The file extension gets removed by CFile - FontDatabase::LoadFontFace((name + ".ttf").c_str()); + int lang = lcl_get_current_lang_index(); + if (Lcl_languages.in_bounds(lang) && Lcl_languages[lang].lang_ext[0] != '\0') + { + SCP_vector localized; + cf_get_file_list(localized, CF_TYPE_FONT, (SCP_string(Lcl_languages[lang].lang_ext) + DIR_SEPARATOR_STR + filter).c_str()); + + for (auto& name : localized) + { + // a listing of a subfolder includes the subfolder in each name + auto bare = name.substr(name.rfind(DIR_SEPARATOR_CHAR) + 1); + if (!SCP_vector_contains_lcase(files, bare)) + files.push_back(std::move(bare)); + } } - files.clear(); - cf_get_file_list(files, CF_TYPE_FONT, "*.otf"); + return files; +} - for (auto& name : files) { - // The file extension gets removed by CFile - FontDatabase::LoadFontFace((name + ".otf").c_str()); +void load_fonts() +{ + for (auto ext : { ".ttf", ".otf" }) + { + for (auto& name : list_fonts(ext)) + { + // The file extension gets removed by CFile + FontDatabase::LoadFontFace((name + ext).c_str()); + } } } diff --git a/code/scripting/api/libs/audio.cpp b/code/scripting/api/libs/audio.cpp index b40a63a7f47..683f10f396d 100644 --- a/code/scripting/api/libs/audio.cpp +++ b/code/scripting/api/libs/audio.cpp @@ -342,8 +342,9 @@ ADE_FUNC(pauseMusic, ADE_FUNC(openAudioStream, l_Audio, "string fileName, enumeration stream_type /* AUDIOSTREAM_* */", - "Opens an audio stream of the specified file and type. An audio stream is meant for more long time sounds since " - "they are streamed from the file instead of loaded in its entirety.", + "Opens an audio stream of the specified file and type. An audio stream is meant for longer sounds since " + "it is streamed from the file instead of loaded in its entirety. A voice stream (AUDIOSTREAM_VOICE) will open " + "a localized file (i.e. a copy in the current language's subfolder) if one exists.", "audio_stream", "A handle to the opened stream or invalid on error") { diff --git a/code/sound/audiostr.cpp b/code/sound/audiostr.cpp index 5e43ea16e1e..a51589fcc26 100644 --- a/code/sound/audiostr.cpp +++ b/code/sound/audiostr.cpp @@ -1023,7 +1023,10 @@ int audiostream_open( const char *filename, int type ) char fname[MAX_FILENAME_LEN]; // copy filename, since we might modify it - strcpy_s(fname, filename); + if (type == ASF_VOICE) + strcpy_s(fname, cf_localize_filename(filename, CF_TYPE_ANY, NUM_AUDIO_EXT, audio_ext_list).c_str()); + else + strcpy_s(fname, filename); switch (type) { diff --git a/test/src/cfile/cfile.cpp b/test/src/cfile/cfile.cpp index 597a4a1ef5e..9e081fec055 100644 --- a/test/src/cfile/cfile.cpp +++ b/test/src/cfile/cfile.cpp @@ -1,10 +1,13 @@ #include #include +#include #include #include #include +#include #include +#include #include #include "util/FSTestFixture.h" @@ -217,6 +220,177 @@ TEST_F(CFileTest, subfolder_list) ASSERT_TRUE(table_files.back().substr(0, 6) == "folder"); } +// Stacks two mods, cfile//pack over cfile//base, whose tables define the test languages. +// In the test data, a file of 4 bytes ("base") is the original and a file of 9 bytes ("localized") is in a +// language subfolder. +class CFileLanguageSubdirTest : public test::FSTestFixture +{ + public: + CFileLanguageSubdirTest() : test::FSTestFixture(INIT_NONE) {} + + protected: + void SetUp() override + { + test::FSTestFixture::SetUp(); + + SCP_string test_dir = "cfile"; + test_dir += DIR_SEPARATOR_CHAR; + test_dir += ::testing::UnitTest::GetInstance()->current_test_info()->name(); + test_dir += DIR_SEPARATOR_CHAR; + + // Cmdline_mod is a list of null-terminated names, ended by an empty name + SCP_string modlist = test_dir + "pack"; + modlist += '\0'; + modlist += test_dir + "base"; + modlist += '\0'; + + delete[] Cmdline_mod; + Cmdline_mod = new char[modlist.size() + 1]; + memcpy(Cmdline_mod, modlist.c_str(), modlist.size() + 1); + + SCP_string cfile_dir(TEST_DATA_PATH); + cfile_dir += DIR_SEPARATOR_CHAR; + cfile_dir += "test"; // Cfile expects something after the path + + ASSERT_FALSE(cfile_init(cfile_dir.c_str())); + + lcl_init(-1); + } + + void TearDown() override + { + test::FSTestFixture::TearDown(); + + cfile_close(); + } +}; + +TEST_F(CFileLanguageSubdirTest, language_subdir_other_root) +{ + // the localized files can be found by naming the subfolder + auto loc = cf_find_file_location("gr/font01.vf", CF_TYPE_ANY); + ASSERT_TRUE(loc.found); + EXPECT_EQ(9u, loc.size); + + loc = cf_find_file_location("tw/brief1.ogg", CF_TYPE_ANY); + ASSERT_TRUE(loc.found); + EXPECT_EQ(9u, loc.size); + + // a bare name should find the original in the lower-priority root, not the localized file in the pack + loc = cf_find_file_location("font01.vf", CF_TYPE_ANY); + ASSERT_TRUE(loc.found); + EXPECT_EQ(4u, loc.size) << "found " << loc.full_name; + + loc = cf_find_file_location("font01.vf", CF_TYPE_FONT); + ASSERT_TRUE(loc.found); + EXPECT_EQ(4u, loc.size) << "found " << loc.full_name; + + loc = cf_find_file_location("brief1.ogg", CF_TYPE_ANY); + ASSERT_TRUE(loc.found); + EXPECT_EQ(4u, loc.size) << "found " << loc.full_name; + + loc = cf_find_file_location("brief1.ogg", CF_TYPE_VOICE_BRIEFINGS); + ASSERT_TRUE(loc.found); + EXPECT_EQ(4u, loc.size) << "found " << loc.full_name; +} + +TEST_F(CFileLanguageSubdirTest, language_subdir_only_localized) +{ + // only gr/only_gr.vf exists, so the bare name finds nothing + EXPECT_FALSE(cf_find_file_location("only_gr.vf", CF_TYPE_ANY).found); + EXPECT_FALSE(cf_find_file_location("only_gr.vf", CF_TYPE_FONT).found); + + auto loc = cf_find_file_location("gr/only_gr.vf", CF_TYPE_FONT); + ASSERT_TRUE(loc.found); + EXPECT_EQ(9u, loc.size); + + SCP_vector fonts; + cf_get_file_list(fonts, CF_TYPE_FONT, "*.vf", CF_SORT_NAME); + EXPECT_FALSE(fonts.empty()); + EXPECT_EQ(std::find(fonts.begin(), fonts.end(), "only_gr"), fonts.end()); + + // a subfolder glob still lists the localized files + fonts.clear(); + cf_get_file_list(fonts, CF_TYPE_FONT, "*/*.vf", CF_SORT_NAME); + EXPECT_NE(std::find(fonts.begin(), fonts.end(), "gr" DIR_SEPARATOR_STR "only_gr"), fonts.end()); + + constexpr int max_fonts = 64; + char font_arr[max_fonts][MAX_FILENAME_LEN]; + int num_fonts = cf_get_file_list_preallocated(max_fonts, font_arr, nullptr, CF_TYPE_FONT, "*.vf"); + EXPECT_GT(num_fonts, 0); + for (int i = 0; i < num_fonts; ++i) + EXPECT_STRNE("only_gr", font_arr[i]); +} + +TEST_F(CFileLanguageSubdirTest, language_subdir_same_root) +{ + // font01.vf sorts before "gr", zfont.vf sorts after it + auto loc = cf_find_file_location("font01.vf", CF_TYPE_FONT); + ASSERT_TRUE(loc.found); + EXPECT_EQ(4u, loc.size) << "found " << loc.full_name; + + loc = cf_find_file_location("zfont.vf", CF_TYPE_FONT); + ASSERT_TRUE(loc.found); + EXPECT_EQ(4u, loc.size) << "found " << loc.full_name; +} + +TEST_F(CFileLanguageSubdirTest, localize_filename) +{ + auto restore_language = util::finally([] { lcl_set_language(LCL_DEFAULT); }); + + int german = lcl_find_lang_index_by_name("German"); + ASSERT_GE(german, 0); + lcl_set_language(german); + + auto localize = [](const char* filename, int pathtype = CF_TYPE_ANY) { + return cf_localize_filename(filename, pathtype, NUM_AUDIO_EXT, audio_ext_list); + }; + const SCP_string gr = "gr" DIR_SEPARATOR_STR; + + EXPECT_EQ(gr + "same.ogg", localize("same.ogg")); + + // a mod's override of the original doesn't hide the base game's localized copy + EXPECT_EQ(gr + "override.ogg", localize("override.ogg")); + + EXPECT_EQ(gr + "pack_over_base.ogg", localize("pack_over_base.ogg")); + EXPECT_EQ(gr + "only_localized.ogg", localize("only_localized.ogg")); + EXPECT_EQ("not_localized.ogg", localize("not_localized.ogg")); + + // any audio extension counts, and the name keeps the one asked for + EXPECT_EQ(gr + "mismatch.wav", localize("mismatch.wav")); + + // the localized name would be MAX_FILENAME_LEN characters + EXPECT_EQ("long_name_0123456789abcde.ogg", localize("long_name_0123456789abcde.ogg")); + + EXPECT_EQ(gr + "debrief_only.ogg", localize("debrief_only.ogg", CF_TYPE_VOICE_DEBRIEFINGS)); + + // without an extension list, the name must match exactly + EXPECT_EQ(gr + "same.ogg", cf_localize_filename("same.ogg", CF_TYPE_ANY)); + EXPECT_EQ("mismatch.wav", cf_localize_filename("mismatch.wav", CF_TYPE_ANY)); + + // cfopen() with localize; 9 bytes is the localized copy, 4 the original + auto opened_size = [](const char* filename, bool localize_param, uint32_t location_flags = CF_LOCATION_ALL) { + auto fp = cfopen(filename, "rb", CF_TYPE_ANY, localize_param, location_flags); + if (fp == nullptr) + return -1; + int size = cfilelength(fp); + cfclose(fp); + return size; + }; + EXPECT_EQ(9, opened_size("same.ogg", true)); + EXPECT_EQ(4, opened_size("same.ogg", false)); + EXPECT_EQ(9, opened_size("only_localized.ogg", true)); + EXPECT_EQ(4, opened_size("not_localized.ogg", true)); + + // the localized copy is only in base, which the location flags rule out, so the pack's original opens + EXPECT_EQ(9, opened_size("override.ogg", true)); + EXPECT_EQ(4, opened_size("override.ogg", true, CF_LOCATION_TYPE_PRIMARY_MOD)); + + lcl_set_language(LCL_DEFAULT); + EXPECT_EQ("same.ogg", localize("same.ogg")); + EXPECT_EQ(4, opened_size("same.ogg", true)); +} + namespace { // Builds the bytes cfwrite_string_len writes (a little-endian length, then the characters) as an in-memory file. // The file reads from this object's buffer, so the object has to outlive the CFILE. diff --git a/test/test_data/cfile/language_subdir_only_localized/pack/data/fonts/gr/only_gr.vf b/test/test_data/cfile/language_subdir_only_localized/pack/data/fonts/gr/only_gr.vf new file mode 100644 index 00000000000..18220c157df --- /dev/null +++ b/test/test_data/cfile/language_subdir_only_localized/pack/data/fonts/gr/only_gr.vf @@ -0,0 +1 @@ +localized \ No newline at end of file diff --git a/test/test_data/cfile/language_subdir_only_localized/pack/data/tables/test-lcl.tbm b/test/test_data/cfile/language_subdir_only_localized/pack/data/tables/test-lcl.tbm new file mode 100644 index 00000000000..92571ecb974 --- /dev/null +++ b/test/test_data/cfile/language_subdir_only_localized/pack/data/tables/test-lcl.tbm @@ -0,0 +1,11 @@ +#Supported Languages + +$Language: German ++Extension: gr ++Special Character Index: 164 + +$Language: Chinese (TW) ++Extension: tw ++Special Character Index: 0 + +#End diff --git a/test/test_data/cfile/language_subdir_other_root/base/data/fonts/font01.vf b/test/test_data/cfile/language_subdir_other_root/base/data/fonts/font01.vf new file mode 100644 index 00000000000..8681f8b8f32 --- /dev/null +++ b/test/test_data/cfile/language_subdir_other_root/base/data/fonts/font01.vf @@ -0,0 +1 @@ +base \ No newline at end of file diff --git a/test/test_data/cfile/language_subdir_other_root/base/data/tables/test-lcl.tbm b/test/test_data/cfile/language_subdir_other_root/base/data/tables/test-lcl.tbm new file mode 100644 index 00000000000..92571ecb974 --- /dev/null +++ b/test/test_data/cfile/language_subdir_other_root/base/data/tables/test-lcl.tbm @@ -0,0 +1,11 @@ +#Supported Languages + +$Language: German ++Extension: gr ++Special Character Index: 164 + +$Language: Chinese (TW) ++Extension: tw ++Special Character Index: 0 + +#End diff --git a/test/test_data/cfile/language_subdir_other_root/base/data/voice/briefing/brief1.ogg b/test/test_data/cfile/language_subdir_other_root/base/data/voice/briefing/brief1.ogg new file mode 100644 index 00000000000..8681f8b8f32 --- /dev/null +++ b/test/test_data/cfile/language_subdir_other_root/base/data/voice/briefing/brief1.ogg @@ -0,0 +1 @@ +base \ No newline at end of file diff --git a/test/test_data/cfile/language_subdir_other_root/pack/data/fonts/gr/font01.vf b/test/test_data/cfile/language_subdir_other_root/pack/data/fonts/gr/font01.vf new file mode 100644 index 00000000000..18220c157df --- /dev/null +++ b/test/test_data/cfile/language_subdir_other_root/pack/data/fonts/gr/font01.vf @@ -0,0 +1 @@ +localized \ No newline at end of file diff --git a/test/test_data/cfile/language_subdir_other_root/pack/data/voice/briefing/tw/brief1.ogg b/test/test_data/cfile/language_subdir_other_root/pack/data/voice/briefing/tw/brief1.ogg new file mode 100644 index 00000000000..18220c157df --- /dev/null +++ b/test/test_data/cfile/language_subdir_other_root/pack/data/voice/briefing/tw/brief1.ogg @@ -0,0 +1 @@ +localized \ No newline at end of file diff --git a/test/test_data/cfile/language_subdir_same_root/pack/data/fonts/font01.vf b/test/test_data/cfile/language_subdir_same_root/pack/data/fonts/font01.vf new file mode 100644 index 00000000000..8681f8b8f32 --- /dev/null +++ b/test/test_data/cfile/language_subdir_same_root/pack/data/fonts/font01.vf @@ -0,0 +1 @@ +base \ No newline at end of file diff --git a/test/test_data/cfile/language_subdir_same_root/pack/data/fonts/gr/font01.vf b/test/test_data/cfile/language_subdir_same_root/pack/data/fonts/gr/font01.vf new file mode 100644 index 00000000000..18220c157df --- /dev/null +++ b/test/test_data/cfile/language_subdir_same_root/pack/data/fonts/gr/font01.vf @@ -0,0 +1 @@ +localized \ No newline at end of file diff --git a/test/test_data/cfile/language_subdir_same_root/pack/data/fonts/gr/zfont.vf b/test/test_data/cfile/language_subdir_same_root/pack/data/fonts/gr/zfont.vf new file mode 100644 index 00000000000..18220c157df --- /dev/null +++ b/test/test_data/cfile/language_subdir_same_root/pack/data/fonts/gr/zfont.vf @@ -0,0 +1 @@ +localized \ No newline at end of file diff --git a/test/test_data/cfile/language_subdir_same_root/pack/data/fonts/zfont.vf b/test/test_data/cfile/language_subdir_same_root/pack/data/fonts/zfont.vf new file mode 100644 index 00000000000..8681f8b8f32 --- /dev/null +++ b/test/test_data/cfile/language_subdir_same_root/pack/data/fonts/zfont.vf @@ -0,0 +1 @@ +base \ No newline at end of file diff --git a/test/test_data/cfile/language_subdir_same_root/pack/data/tables/test-lcl.tbm b/test/test_data/cfile/language_subdir_same_root/pack/data/tables/test-lcl.tbm new file mode 100644 index 00000000000..92571ecb974 --- /dev/null +++ b/test/test_data/cfile/language_subdir_same_root/pack/data/tables/test-lcl.tbm @@ -0,0 +1,11 @@ +#Supported Languages + +$Language: German ++Extension: gr ++Special Character Index: 164 + +$Language: Chinese (TW) ++Extension: tw ++Special Character Index: 0 + +#End diff --git a/test/test_data/cfile/localize_filename/base/data/voice/briefing/gr/override.ogg b/test/test_data/cfile/localize_filename/base/data/voice/briefing/gr/override.ogg new file mode 100644 index 00000000000..18220c157df --- /dev/null +++ b/test/test_data/cfile/localize_filename/base/data/voice/briefing/gr/override.ogg @@ -0,0 +1 @@ +localized \ No newline at end of file diff --git a/test/test_data/cfile/localize_filename/base/data/voice/briefing/mismatch.wav b/test/test_data/cfile/localize_filename/base/data/voice/briefing/mismatch.wav new file mode 100644 index 00000000000..8681f8b8f32 --- /dev/null +++ b/test/test_data/cfile/localize_filename/base/data/voice/briefing/mismatch.wav @@ -0,0 +1 @@ +base \ No newline at end of file diff --git a/test/test_data/cfile/localize_filename/base/data/voice/briefing/pack_over_base.ogg b/test/test_data/cfile/localize_filename/base/data/voice/briefing/pack_over_base.ogg new file mode 100644 index 00000000000..8681f8b8f32 --- /dev/null +++ b/test/test_data/cfile/localize_filename/base/data/voice/briefing/pack_over_base.ogg @@ -0,0 +1 @@ +base \ No newline at end of file diff --git a/test/test_data/cfile/localize_filename/pack/data/tables/test-lcl.tbm b/test/test_data/cfile/localize_filename/pack/data/tables/test-lcl.tbm new file mode 100644 index 00000000000..159c1090564 --- /dev/null +++ b/test/test_data/cfile/localize_filename/pack/data/tables/test-lcl.tbm @@ -0,0 +1,7 @@ +#Supported Languages + +$Language: German ++Extension: gr ++Special Character Index: 164 + +#End diff --git a/test/test_data/cfile/localize_filename/pack/data/voice/briefing/gr/long_name_0123456789abcde.ogg b/test/test_data/cfile/localize_filename/pack/data/voice/briefing/gr/long_name_0123456789abcde.ogg new file mode 100644 index 00000000000..18220c157df --- /dev/null +++ b/test/test_data/cfile/localize_filename/pack/data/voice/briefing/gr/long_name_0123456789abcde.ogg @@ -0,0 +1 @@ +localized \ No newline at end of file diff --git a/test/test_data/cfile/localize_filename/pack/data/voice/briefing/gr/mismatch.ogg b/test/test_data/cfile/localize_filename/pack/data/voice/briefing/gr/mismatch.ogg new file mode 100644 index 00000000000..18220c157df --- /dev/null +++ b/test/test_data/cfile/localize_filename/pack/data/voice/briefing/gr/mismatch.ogg @@ -0,0 +1 @@ +localized \ No newline at end of file diff --git a/test/test_data/cfile/localize_filename/pack/data/voice/briefing/gr/only_localized.ogg b/test/test_data/cfile/localize_filename/pack/data/voice/briefing/gr/only_localized.ogg new file mode 100644 index 00000000000..18220c157df --- /dev/null +++ b/test/test_data/cfile/localize_filename/pack/data/voice/briefing/gr/only_localized.ogg @@ -0,0 +1 @@ +localized \ No newline at end of file diff --git a/test/test_data/cfile/localize_filename/pack/data/voice/briefing/gr/pack_over_base.ogg b/test/test_data/cfile/localize_filename/pack/data/voice/briefing/gr/pack_over_base.ogg new file mode 100644 index 00000000000..18220c157df --- /dev/null +++ b/test/test_data/cfile/localize_filename/pack/data/voice/briefing/gr/pack_over_base.ogg @@ -0,0 +1 @@ +localized \ No newline at end of file diff --git a/test/test_data/cfile/localize_filename/pack/data/voice/briefing/gr/same.ogg b/test/test_data/cfile/localize_filename/pack/data/voice/briefing/gr/same.ogg new file mode 100644 index 00000000000..18220c157df --- /dev/null +++ b/test/test_data/cfile/localize_filename/pack/data/voice/briefing/gr/same.ogg @@ -0,0 +1 @@ +localized \ No newline at end of file diff --git a/test/test_data/cfile/localize_filename/pack/data/voice/briefing/long_name_0123456789abcde.ogg b/test/test_data/cfile/localize_filename/pack/data/voice/briefing/long_name_0123456789abcde.ogg new file mode 100644 index 00000000000..8681f8b8f32 --- /dev/null +++ b/test/test_data/cfile/localize_filename/pack/data/voice/briefing/long_name_0123456789abcde.ogg @@ -0,0 +1 @@ +base \ No newline at end of file diff --git a/test/test_data/cfile/localize_filename/pack/data/voice/briefing/not_localized.ogg b/test/test_data/cfile/localize_filename/pack/data/voice/briefing/not_localized.ogg new file mode 100644 index 00000000000..8681f8b8f32 --- /dev/null +++ b/test/test_data/cfile/localize_filename/pack/data/voice/briefing/not_localized.ogg @@ -0,0 +1 @@ +base \ No newline at end of file diff --git a/test/test_data/cfile/localize_filename/pack/data/voice/briefing/override.ogg b/test/test_data/cfile/localize_filename/pack/data/voice/briefing/override.ogg new file mode 100644 index 00000000000..8681f8b8f32 --- /dev/null +++ b/test/test_data/cfile/localize_filename/pack/data/voice/briefing/override.ogg @@ -0,0 +1 @@ +base \ No newline at end of file diff --git a/test/test_data/cfile/localize_filename/pack/data/voice/briefing/same.ogg b/test/test_data/cfile/localize_filename/pack/data/voice/briefing/same.ogg new file mode 100644 index 00000000000..8681f8b8f32 --- /dev/null +++ b/test/test_data/cfile/localize_filename/pack/data/voice/briefing/same.ogg @@ -0,0 +1 @@ +base \ No newline at end of file diff --git a/test/test_data/cfile/localize_filename/pack/data/voice/debriefing/gr/debrief_only.ogg b/test/test_data/cfile/localize_filename/pack/data/voice/debriefing/gr/debrief_only.ogg new file mode 100644 index 00000000000..18220c157df --- /dev/null +++ b/test/test_data/cfile/localize_filename/pack/data/voice/debriefing/gr/debrief_only.ogg @@ -0,0 +1 @@ +localized \ No newline at end of file