Skip to content
Open
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
11 changes: 9 additions & 2 deletions code/cfile/cfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion code/cfile/cfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand Down
58 changes: 56 additions & 2 deletions code/cfile/cfilesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only the innermost folder is checked, and the localized lookup asks for exactly gr/<name>. Two results from my test mod:

  • voice/briefing/act1/x2.ogg with act1/gr/x2.ogg next to it: the copy is hidden from the bare lookup here, and cf_localize_filename() looks for gr/x2.ogg, which doesn't match the sub path act1/gr/. German plays the original and nothing is logged. So "next to the originals" works at the top of a folder like voice/briefing, but not for originals a mod keeps in subfolders.
  • voice/special/gr/act1/x4.ogg: the innermost folder is act1, so this still matches a bare x4.ogg for every player.

If both are intended, a line in the mod author notes would cover it.

}

// if we have search but no index then fail
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}
Expand Down
22 changes: 12 additions & 10 deletions code/globalincs/vmallocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,6 @@ bool SCP_vector_contains(const SCP_vector<T>& vector, const T& item) {
return std::find(vector.begin(), vector.end(), item) != vector.end();
}

template <typename T>
bool SCP_vector_contains_lcase(const SCP_vector<T>& 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 <typename T>
bool SCP_vector_contains_lcase(const SCP_vector<T>& 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 <typename T>
inline bool SCP_vector_inbounds(const SCP_vector<T>& vector, int idx) {
return ((idx >= 0) && (static_cast<size_t>(idx) < vector.size()));
Expand Down Expand Up @@ -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 <typename T>
bool SCP_vector_contains_lcase(const SCP_vector<T>& 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 <typename T>
bool SCP_vector_contains_lcase(const SCP_vector<T>& 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 <typename T, typename U, typename Less = std::less<T>>
class SCP_map : public std::map<T, U, Less, std::allocator<std::pair<const T, U>>>
Expand Down
17 changes: 2 additions & 15 deletions code/graphics/software/FontManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

#include "bmpman/bmpman.h"
#include "cfile/cfile.h"
#include "localization/localize.h"
#include "options/Option.h"

namespace font
Expand Down Expand Up @@ -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 )
{
Expand Down Expand Up @@ -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)
{
Expand Down
5 changes: 3 additions & 2 deletions code/mission/missionmessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
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)) {
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;
}
}
Expand Down
18 changes: 9 additions & 9 deletions code/mission/missiontraining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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));
Expand Down
3 changes: 2 additions & 1 deletion code/missionui/missiondebrief.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(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;
}

Expand Down
3 changes: 2 additions & 1 deletion code/scpui/RocketFileInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
39 changes: 29 additions & 10 deletions code/scpui/rocket_ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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<SCP_string> list_fonts(const char* ext)
{
SCP_vector<SCP_string> 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<SCP_string> 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());
}
}
}

Expand Down
5 changes: 3 additions & 2 deletions code/scripting/api/libs/audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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")
{
Expand Down
5 changes: 4 additions & 1 deletion code/sound/audiostr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
Loading
Loading