From ec273ae2cece76d0bcb7671099c9b6a59ceeadaa Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Mon, 23 Sep 2013 00:18:31 +0300 Subject: add support for gettext files extraction in SDL2 in Android. --- src/client.cpp | 86 ++++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 71 insertions(+), 15 deletions(-) (limited to 'src/client.cpp') diff --git a/src/client.cpp b/src/client.cpp index c4787d9a3..c81bf7dae 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -363,12 +363,6 @@ void Client::gameInit() #ifdef USE_FUZZER Fuzzer::init(); -#endif - -#ifdef ANDROID -#ifdef USE_SDL2 - extractAssets(); -#endif #endif initConfiguration(); paths.setDefaultValues(getPathsDefaults()); @@ -380,6 +374,20 @@ void Client::gameInit() storeSafeParameters(); + const ResourceManager *const resman = ResourceManager::getInstance(); + if (!resman->setWriteDir(mLocalDataDir)) + { + logger->error(strprintf("%s couldn't be set as home directory! " + "Exiting.", mLocalDataDir.c_str())); + } + +#ifdef ANDROID +#ifdef USE_SDL2 + extractAssets(); + extractLocale(); +#endif +#endif + #ifdef ENABLE_NLS std::string lang = config.getStringValue("lang"); #ifdef WIN32 @@ -398,8 +406,13 @@ void Client::gameInit() setEnv("LANGUAGE", lang.c_str()); } #ifdef ANDROID +#ifdef USE_SDL2 + bindTextDomain("manaplus", (std::string(getenv("APPDIR")).append( + "/locale")).c_str()); +#else bindTextDomain("manaplus", (std::string(PhysFs::getBaseDir()) .append("/locale")).c_str()); +#endif #else #ifdef ENABLE_PORTABLE bindTextDomain("manaplus", (std::string(PhysFs::getBaseDir()) @@ -493,13 +506,6 @@ void Client::gameInit() SMALL_VERSION); } - const ResourceManager *const resman = ResourceManager::getInstance(); - if (!resman->setWriteDir(mLocalDataDir)) - { - logger->error(strprintf("%s couldn't be set as home directory! " - "Exiting.", mLocalDataDir.c_str())); - } - resman->addToSearchPath(PKG_DATADIR "data/perserver/default", false); resman->addToSearchPath("data/perserver/default", false); @@ -3250,8 +3256,9 @@ void Client::extractAssets() const std::string fileName = std::string(getenv( "APPDIR")).append("/data.zip"); logger->log("Extracting asset into: " + fileName); - FILE *const file = fopen(fileName.c_str(), "w"); uint8_t *buf = new uint8_t[1000000]; + + FILE *const file = fopen(fileName.c_str(), "w"); for (int f = 0; f < 100; f ++) { std::string part = strprintf("manaplus-data.zip%u%u", @@ -3272,8 +3279,57 @@ void Client::extractAssets() break; } } - delete [] buf; fclose(file); + + const std::string fileName2 = std::string(getenv( + "APPDIR")).append("/locale.zip"); + FILE *const file2 = fopen(fileName2.c_str(), "w"); + SDL_RWops *const rw = SDL_RWFromFile("manaplus-locale.zip", "r"); + if (rw) + { + const int size = SDL_RWsize(rw); + int size2 = SDL_RWread(rw, buf, 1, size); + fwrite(buf, 1, size2, file2); + SDL_RWclose(rw); + } + fclose(file2); + + delete [] buf; +} + +void Client::extractLocale() +{ + // in future need also remove all locales in local dir + + const std::string fileName2 = std::string(getenv( + "APPDIR")).append("/locale.zip"); + const ResourceManager *const resman = ResourceManager::getInstance(); + resman->addToSearchPath(fileName2, false); + + const std::string localDir = std::string(getenv("APPDIR")).append("/"); + char **rootDirs = PhysFs::enumerateFiles("locale"); + for (char **i = rootDirs; *i; i++) + { + const std::string dir = std::string("locale/").append(*i); + if (PhysFs::isDirectory(dir.c_str())) + { + const std::string moFile = dir + "/LC_MESSAGES/manaplus.mo"; + if (PhysFs::exists((moFile).c_str())) + { + const std::string localFile = localDir + moFile; + const std::string localDir2 = localDir + dir + "/LC_MESSAGES"; + int size = 0; + mkdir_r(localDir2.c_str()); + void *const buf = ResourceManager::loadFile(moFile, size); + FILE *const file = fopen(localFile.c_str(), "w"); + fwrite(buf, 1, size, file); + fclose(file); + free(buf); + } + } + } + PhysFs::freeList(rootDirs); + resman->removeFromSearchPath(fileName2); } #endif #endif -- cgit v1.2.3-60-g2f50