From 7b2fa39428e0ee7e0e36e20b50914f585e39ed60 Mon Sep 17 00:00:00 2001 From: fgenesis Date: Tue, 10 Jul 2012 22:16:48 +0200 Subject: [PATCH] Move localisation code to BBGE, and allow textures to be localised. --- Aquaria/Continuity.cpp | 12 +-- Aquaria/DSQ.cpp | 4 +- Aquaria/Game.cpp | 14 ++-- Aquaria/StatsAndAchievements.cpp | 2 +- Aquaria/StringBank.cpp | 4 +- Aquaria/SubtitlePlayer.cpp | 4 +- Aquaria/UserSettings.cpp | 125 +++---------------------------- Aquaria/UserSettings.h | 4 - BBGE/Core.h | 1 + BBGE/Localization.cpp | 120 +++++++++++++++++++++++++++++ BBGE/Localization.h | 10 +++ BBGE/Texture.cpp | 27 ++++--- 12 files changed, 174 insertions(+), 153 deletions(-) create mode 100644 BBGE/Localization.cpp create mode 100644 BBGE/Localization.h diff --git a/Aquaria/Continuity.cpp b/Aquaria/Continuity.cpp index 0a3fc2e..cc88b79 100644 --- a/Aquaria/Continuity.cpp +++ b/Aquaria/Continuity.cpp @@ -1209,11 +1209,11 @@ void Continuity::loadSongBank() songSlotNames.clear(); songBank.clear(); - loadIntoSongBank(dsq->user.localisePath("data/songs.xml")); + loadIntoSongBank(localisePath("data/songs.xml")); if (dsq->mod.isActive()) { - loadIntoSongBank(dsq->user.localisePath(dsq->mod.getPath() + "scripts/songs.xml", dsq->mod.getPath())); + loadIntoSongBank(localisePath(dsq->mod.getPath() + "scripts/songs.xml", dsq->mod.getPath())); } } @@ -3244,12 +3244,12 @@ void Continuity::reset() loadIngredientDisplayNames("data/ingredientnames.txt"); - std::string fname = dsq->user.localisePath("data/ingredientnames.txt"); + std::string fname = localisePath("data/ingredientnames.txt"); loadIngredientDisplayNames(fname); if(dsq->mod.isActive()) { - fname = dsq->user.localisePath(dsq->mod.getPath() + "ingredientnames.txt", dsq->mod.getPath()); + fname = localisePath(dsq->mod.getPath() + "ingredientnames.txt", dsq->mod.getPath()); loadIngredientDisplayNames(fname); } @@ -3260,13 +3260,13 @@ void Continuity::reset() if(dsq->mod.isActive()) { //load mod ingredients - fname = dsq->user.localisePath(dsq->mod.getPath() + "ingredients.txt", dsq->mod.getPath()); + fname = localisePath(dsq->mod.getPath() + "ingredients.txt", dsq->mod.getPath()); loadIngredientData(fname); } //load ingredients for the main game if(ingredientData.empty() && recipes.empty()) { - fname = dsq->user.localisePath("data/ingredients.txt"); + fname = localisePath("data/ingredients.txt"); loadIngredientData(fname); } diff --git a/Aquaria/DSQ.cpp b/Aquaria/DSQ.cpp index dcf57a6..61a8bbf 100644 --- a/Aquaria/DSQ.cpp +++ b/Aquaria/DSQ.cpp @@ -485,7 +485,7 @@ void DSQ::loadFonts() destroyFonts(); - std::string file = user.localisePath("data/font-small.glf"); + std::string file = localisePath("data/font-small.glf"); font.load(file, 1, false); font.fontTopColor = Vector(0.9,0.9,1); @@ -513,7 +513,7 @@ void DSQ::loadFonts() goldFont.overrideTexture = core->addTexture("font"); - file = user.localisePath("data/font.ttf"); + file = localisePath("data/font.ttf"); debugLog("ttf..."); arialFontData = (unsigned char *)readFile(file, &arialFontDataSize); diff --git a/Aquaria/Game.cpp b/Aquaria/Game.cpp index 42d9421..03a732c 100644 --- a/Aquaria/Game.cpp +++ b/Aquaria/Game.cpp @@ -7919,29 +7919,29 @@ void Game::toggleHelpScreen(bool on, const std::string &label) // These say "Mac" but we use them on Linux, too. #if defined(BBGE_BUILD_UNIX) - std::string fname = dsq->user.localisePath("data/help_header_mac.txt"); + std::string fname = localisePath("data/help_header_mac.txt"); appendFileToString(data, fname); #else - std::string fname = dsq->user.localisePath("data/help_header.txt"); + std::string fname = localisePath("data/help_header.txt"); appendFileToString(data, fname); #endif if (dsq->continuity.hasSong(SONG_BIND)) { - fname = dsq->user.localisePath("data/help_bindsong.txt"); + fname = localisePath("data/help_bindsong.txt"); appendFileToString(data, fname); } if (dsq->continuity.hasSong(SONG_ENERGYFORM)) { - fname = dsq->user.localisePath("data/help_energyform.txt"); + fname = localisePath("data/help_energyform.txt"); appendFileToString(data, fname); } - fname = dsq->user.localisePath("data/help_start.txt"); + fname = localisePath("data/help_start.txt"); appendFileToString(data, fname); // These say "Mac" but we use them on Linux, too. #if defined(BBGE_BUILD_UNIX) - fname = dsq->user.localisePath("data/help_end_mac.txt"); + fname = localisePath("data/help_end_mac.txt"); appendFileToString(data, fname); #else - fname = dsq->user.localisePath("data/help_end.txt"); + fname = localisePath("data/help_end.txt"); appendFileToString(data, fname); #endif diff --git a/Aquaria/StatsAndAchievements.cpp b/Aquaria/StatsAndAchievements.cpp index cb39565..67bb01e 100644 --- a/Aquaria/StatsAndAchievements.cpp +++ b/Aquaria/StatsAndAchievements.cpp @@ -174,7 +174,7 @@ void StatsAndAchievements::RunFrame() VFILE *io = NULL; // Get generic achievement data... - std::string fname = dsq->user.localisePath("data/achievements.txt"); + std::string fname = localisePath("data/achievements.txt"); io = vfopen(fname.c_str(), "r"); char line[1024]; for (size_t i = 0; i < max_achievements; i++) diff --git a/Aquaria/StringBank.cpp b/Aquaria/StringBank.cpp index 7afbaa0..7fe20d9 100644 --- a/Aquaria/StringBank.cpp +++ b/Aquaria/StringBank.cpp @@ -34,11 +34,11 @@ void StringBank::load() _load(dsq->mod.getPath() + "stringbank.txt"); // Then, load localized ones. If some entries in these are missing, the default for each is taken. - std::string fname = dsq->user.localisePath("data/stringbank.txt"); + std::string fname = localisePath("data/stringbank.txt"); _load(fname); if (dsq->mod.isActive()) { - fname = dsq->user.localisePath(dsq->mod.getPath() + "stringbank.txt", dsq->mod.getPath()); + fname = localisePath(dsq->mod.getPath() + "stringbank.txt", dsq->mod.getPath()); _load(fname); } } diff --git a/Aquaria/SubtitlePlayer.cpp b/Aquaria/SubtitlePlayer.cpp index c6469b6..6a5f89e 100644 --- a/Aquaria/SubtitlePlayer.cpp +++ b/Aquaria/SubtitlePlayer.cpp @@ -46,7 +46,7 @@ void SubtitlePlayer::go(const std::string &subs) if (dsq->mod.isActive()) { f = dsq->mod.getPath() + "audio/" + subs + ".txt"; - f = dsq->user.localisePath(f, dsq->mod.getPath()); + f = localisePath(f, dsq->mod.getPath()); f = core->adjustFilenameCase(f); if (exists(f)) checkAfter = false; @@ -55,7 +55,7 @@ void SubtitlePlayer::go(const std::string &subs) if (checkAfter) { f = "scripts/vox/" + subs + ".txt"; - f = dsq->user.localisePath(f); + f = localisePath(f); f = core->adjustFilenameCase(f); if (!exists(f)) { diff --git a/Aquaria/UserSettings.cpp b/Aquaria/UserSettings.cpp index 4887e16..487abd7 100644 --- a/Aquaria/UserSettings.cpp +++ b/Aquaria/UserSettings.cpp @@ -28,31 +28,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "../ExternalLibs/tinyxml.h" #endif -#ifdef BBGE_BUILD_WINDOWS - #define WIN32_LEAN_AND_MEAN - #include -#endif - -#ifdef BBGE_BUILD_UNIX - #include - #include - #include -#endif - -#ifdef BBGE_BUILD_MACOSX - #include - #include - #include - -// veeery clunky. -static std::string _CFToStdString(CFStringRef cs) -{ - char buf[1024]; - CFStringGetCString(cs, &buf[0], 2048, kCFStringEncodingUTF8); - return &buf[0]; -} - -#endif void UserSettings::save() { @@ -560,11 +535,6 @@ void UserSettings::load(bool doApply, const std::string &overrideFile) network.masterServer = serv; } - if (system.locale.empty()) - getSystemLocale(); - else - debugLog("use user config locale: " + system.locale); - //clearInputCodeMap(); if (doApply) @@ -606,93 +576,18 @@ void UserSettings::apply() core->settings.prebufferSounds = audio.prebuffer; -#endif -} - -std::string UserSettings::localisePath(const std::string &path, const std::string &modpath) -{ if (system.locale.empty()) - return path; - - const std::string fname = path.substr(modpath.length()); - - /* we first try with complete locale name, i.e "locales/en_US/" */ - std::string localisedPath = modpath + "locales/" + system.locale + "/" + fname; - - if (exists(localisedPath.c_str())) - return localisedPath; - - /* ok didn't work, let's retry with only language part of locale name, i.e "locales/en/" */ - const size_t found = system.locale.find('_'); - - /* hmm, seems like we didn't have a full locale name anyway, use original path */ - if (found == string::npos) - return path; - - localisedPath = modpath + "locales/" + system.locale.substr(0,found) + "/" + fname; - - /* hooray we found a file! */ - if (exists(localisedPath.c_str())) - return localisedPath; - - /* seems like we don't have a localized version of the file available, use original path */ - return path; -} - -void UserSettings::getSystemLocale() -{ - system.isSystemLocale = true; - -#ifdef BBGE_BUILD_WINDOWS - LCID lcid = GetThreadLocale(); - - char buf[100]; - char ctry[100]; - - if (GetLocaleInfo(lcid, LOCALE_SISO639LANGNAME, buf, sizeof buf) != 0) { - system.locale = buf; - - if (GetLocaleInfo(lcid, LOCALE_SISO3166CTRYNAME, ctry, sizeof ctry) != 0) - { - system.locale += "_"; - system.locale += ctry; - } + std::string loc = getSystemLocale(); + debugLog("Using autodetected system locale: " + loc); + setUsedLocale(loc); } -#elif BBGE_BUILD_MACOSX - CFLocaleRef locale = CFLocaleCopyCurrent(); - CFStringRef buf; - - if ((buf = (CFStringRef)CFLocaleGetValue(locale, kCFLocaleLanguageCode)) != NULL) - { - system.locale = _CFToStdString(buf); - CFRelease(buf); - - if ((buf = (CFStringRef)CFLocaleGetValue(locale, kCFLocaleCountryCode)) != NULL) - { - system.locale += "_"; - system.locale += _CFToStdString(buf); - CFRelease(buf); - } - } - - CFRelease(locale); - -#else - const char *lang = (const char *)getenv("LANG"); - - if (lang && *lang) - { - system.locale = lang; - - size_t found = system.locale.find('.'); - - if (found != string::npos) - system.locale.resize(found); - } -#endif - if (system.locale.empty()) - debugLog("could not establish system locale"); else - debugLog("use system locale: " + system.locale); + { + debugLog("Using user config locale: " + system.locale); + setUsedLocale(system.locale); + } + +#endif } + diff --git a/Aquaria/UserSettings.h b/Aquaria/UserSettings.h index 6be8e60..6088f88 100644 --- a/Aquaria/UserSettings.h +++ b/Aquaria/UserSettings.h @@ -182,8 +182,4 @@ public: void load(bool doApply=true, const std::string &overrideFile=""); void save(); void apply(); - std::string localisePath(const std::string &path, const std::string &modpath=""); - -private: - void getSystemLocale(); }; diff --git a/BBGE/Core.h b/BBGE/Core.h index 79e0d1a..2d62bda 100644 --- a/BBGE/Core.h +++ b/BBGE/Core.h @@ -39,6 +39,7 @@ BUILD_LINUX #include "Flags.h" //#include "Scripting.h" #include "Effects.h" +#include "Localization.h" #include "DarkLayer.h" diff --git a/BBGE/Localization.cpp b/BBGE/Localization.cpp new file mode 100644 index 0000000..0e8e494 --- /dev/null +++ b/BBGE/Localization.cpp @@ -0,0 +1,120 @@ +#include "Localization.h" + +#ifdef BBGE_BUILD_WINDOWS +#define WIN32_LEAN_AND_MEAN +#include +#endif + +#ifdef BBGE_BUILD_UNIX +#include +#include +#include +#endif + +#ifdef BBGE_BUILD_MACOSX +#include +#include +#include + +// veeery clunky. +static std::string _CFToStdString(CFStringRef cs) +{ + char buf[1024]; + CFStringGetCString(cs, &buf[0], 2048, kCFStringEncodingUTF8); + return &buf[0]; +} +#endif + +static std::string s_locale; + +void setUsedLocale(const std::string& s) +{ + s_locale = s; +} + +std::string localisePath(const std::string &path, const std::string &modpath /* = "" */) +{ + if (s_locale.empty()) + return path; + + const std::string fname = path.substr(modpath.length()); + + /* we first try with complete locale name, i.e "locales/en_US/" */ + std::string localisedPath = modpath + "locales/" + s_locale + "/" + fname; + + if (exists(localisedPath.c_str())) + return localisedPath; + + /* ok didn't work, let's retry with only language part of locale name, i.e "locales/en/" */ + const size_t found = s_locale.find('_'); + + /* hmm, seems like we didn't have a full locale name anyway, use original path */ + if (found == std::string::npos) + return path; + + localisedPath = modpath + "locales/" + s_locale.substr(0,found) + "/" + fname; + + /* hooray we found a file! */ + if (exists(localisedPath.c_str())) + return localisedPath; + + /* seems like we don't have a localized version of the file available, use original path */ + return path; +} + +std::string getSystemLocale() +{ + std::string localeStr; + +#ifdef BBGE_BUILD_WINDOWS + LCID lcid = GetThreadLocale(); + + char buf[100]; + char ctry[100]; + + if (GetLocaleInfo(lcid, LOCALE_SISO639LANGNAME, buf, sizeof buf) != 0) + { + localeStr = buf; + + if (GetLocaleInfo(lcid, LOCALE_SISO3166CTRYNAME, ctry, sizeof ctry) != 0) + { + localeStr += "_"; + localeStr += ctry; + } + } +#elif BBGE_BUILD_MACOSX + CFLocaleRef locale = CFLocaleCopyCurrent(); + CFStringRef buf; + + if ((buf = (CFStringRef)CFLocaleGetValue(locale, kCFLocaleLanguageCode)) != NULL) + { + localeStr = _CFToStdString(buf); + CFRelease(buf); + + if ((buf = (CFStringRef)CFLocaleGetValue(locale, kCFLocaleCountryCode)) != NULL) + { + system.locale += "_"; + system.locale += _CFToStdString(buf); + CFRelease(buf); + } + } + + CFRelease(locale); + +#else + const char *lang = (const char *)getenv("LANG"); + + if (lang && *lang) + { + localeStr = lang; + + size_t found = system.locale.find('.'); + + if (found != string::npos) + localeStr.resize(found); + } +#endif + + return localeStr; +} + diff --git a/BBGE/Localization.h b/BBGE/Localization.h new file mode 100644 index 0000000..0b67301 --- /dev/null +++ b/BBGE/Localization.h @@ -0,0 +1,10 @@ +#ifndef LOCALIZATION_H +#define LOCALIZATION_H + +#include "Base.h" + +void setUsedLocale(const std::string& s); +std::string localisePath(const std::string &path, const std::string &modpath = ""); +std::string getSystemLocale(); + +#endif diff --git a/BBGE/Texture.cpp b/BBGE/Texture.cpp index 5c7d030..bb74f46 100644 --- a/BBGE/Texture.cpp +++ b/BBGE/Texture.cpp @@ -281,9 +281,6 @@ void Texture::load(std::string file) return; } - stringToLowerUserData(file); - file = core->adjustFilenameCase(file); - loadName = file; repeating = false; @@ -298,26 +295,28 @@ void Texture::load(std::string file) pos = std::string::npos; } - if (core->debugLogTextures) + /*if (core->debugLogTextures) { std::ostringstream os; os << "pos [" << pos << "], file :" << file; debugLog(os.str()); + }*/ + + bool found = exists(file); + + if(!found && exists(file + ".png")) + { + found = true; + file += ".png"; } - bool found = exists(file); - - if(!found && exists(file + ".png")) - { - found = true; - file += ".png"; - } - - // .tga/.zga are never used as game graphics anywhere except save slot thumbnails. - // if so, their file names are passed exact, not with a missing extension + // .tga/.zga are never used as game graphics anywhere except save slot thumbnails. + // if so, their file names are passed exact, not with a missing extension if (found) { + file = localisePath(file); + /* std::ostringstream os; os << "Loading texture [" << file << "]";