From 7f2484d82112851dbeba83afd18790a58b03599d Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Tue, 29 Oct 2013 23:36:53 +0300 Subject: Initial porting to NACL by Vasily_Makarov. --- src/utils/files.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'src/utils/files.h') diff --git a/src/utils/files.h b/src/utils/files.h index bb9a6a11f..519b7f520 100644 --- a/src/utils/files.h +++ b/src/utils/files.h @@ -21,13 +21,14 @@ #ifndef UTILS_FILES_H #define UTILS_FILES_H -#ifdef ANDROID +#if defined(ANDROID) || defined(__native_client__) #include namespace Files { +#ifdef ANDROID void extractLocale(); - +#endif void copyPhysFsFile(const std::string &inFile, const std::string &outFile); void copyPhysFsDir(const std::string &inDir, const std::string &outDir); @@ -36,5 +37,5 @@ namespace Files const std::string &outDir); } // namespace Files -#endif // ANDROID +#endif // ANDROID __native_client__ #endif // UTILS_FILES_H -- cgit v1.2.3-70-g09d2 From c2f7ae98d3c321a615cda6946443f5098d9d08bd Mon Sep 17 00:00:00 2001 From: Dan Sagunov Date: Sat, 2 Nov 2013 00:01:15 +0400 Subject: Adding renameFile function for NaCl --- src/net/download.cpp | 11 ++++++++++- src/utils/files.cpp | 29 +++++++++++++++++++++++++++++ src/utils/files.h | 2 ++ 3 files changed, 41 insertions(+), 1 deletion(-) (limited to 'src/utils/files.h') diff --git a/src/net/download.cpp b/src/net/download.cpp index 3ccff63c8..38b8d2873 100644 --- a/src/net/download.cpp +++ b/src/net/download.cpp @@ -39,6 +39,10 @@ ((LIBCURL_VERSION_MAJOR == (a)) && (LIBCURL_VERSION_MINOR == (b)) && \ (LIBCURL_VERSION_PATCH >= (c)))) +#if defined __native_client__ +#include "utils/files.h" +#endif + #include "debug.h" const char *DOWNLOAD_ERROR_MESSAGE_THREAD @@ -360,7 +364,12 @@ int Download::downloadThread(void *ptr) if (!d->mOptions.cancel) { ::remove(d->mFileName.c_str()); - ::rename(outFilename.c_str(), d->mFileName.c_str()); +#if defined __native_client__ + Files::renameFile( +#else + ::rename( +#endif + outFilename.c_str(), d->mFileName.c_str()); // Check if we can open it and no errors were encountered // during renaming diff --git a/src/utils/files.cpp b/src/utils/files.cpp index c06b3d892..fa69cd6af 100644 --- a/src/utils/files.cpp +++ b/src/utils/files.cpp @@ -100,4 +100,33 @@ void Files::extractZip(const std::string &zipName, const std::string &inDir, remove(zipName.c_str()); } +int Files::renameFile(const std::string &pFrom, const std::string &pTo) +{ + FILE *file = fopen(pFrom.c_str(), "rb"); + if (file == NULL) + return -1; + + fseek(file, 0, SEEK_END); + size_t sz = ftell(file); + fseek(file, 0, SEEK_SET); + + char *buf = (char *)malloc(sz + 1); + if (fread(buf, 1, sz, file) != sz) + return -1; + fclose(file); + buf[sz] = 0; + + file = fopen(pTo.c_str(), "w+b"); + if (file == NULL) + return -1; + + if (fwrite(buf, 1, sz, file) != sz) + return -1; + fclose(file); + + free(buf); + + return 0; +} + #endif // ANDROID __native_client__ diff --git a/src/utils/files.h b/src/utils/files.h index 519b7f520..4a44631cf 100644 --- a/src/utils/files.h +++ b/src/utils/files.h @@ -35,6 +35,8 @@ namespace Files void extractZip(const std::string &zipName, const std::string &inDir, const std::string &outDir); + + int renameFile(const std::string &pFrom, const std::string &pTo); } // namespace Files #endif // ANDROID __native_client__ -- cgit v1.2.3-70-g09d2 From 57cb5102b2fd47fed46bd85ab07e0135e19dd484 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Mon, 4 Nov 2013 21:50:15 +0300 Subject: improve renameFile function. add test for this function. --- src/Makefile.am | 1 + src/utils/files.cpp | 52 ++++++++++++++++++-------------- src/utils/files.h | 5 ++-- src/utils/files_unittest.cpp | 71 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 104 insertions(+), 25 deletions(-) create mode 100644 src/utils/files_unittest.cpp (limited to 'src/utils/files.h') diff --git a/src/Makefile.am b/src/Makefile.am index a05e4bc41..b0f6348c5 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -656,6 +656,7 @@ manaplus_SOURCES += gui/widgets/avatarlistbox.cpp \ utils/dtor.h \ utils/files.cpp \ utils/files.h \ + utils/files_unittest.cpp \ utils/fuzzer.cpp \ utils/fuzzer.h \ utils/gettext.h \ diff --git a/src/utils/files.cpp b/src/utils/files.cpp index fa69cd6af..6f57d3062 100644 --- a/src/utils/files.cpp +++ b/src/utils/files.cpp @@ -18,7 +18,6 @@ * along with this program. If not, see . */ -#if defined(ANDROID) || defined(__native_client__) #include "utils/files.h" #include "logger.h" @@ -63,6 +62,7 @@ void Files::extractLocale() } #endif // ANDROID +#if defined(ANDROID) || defined(__native_client__) void Files::copyPhysFsFile(const std::string &inFile, const std::string &outFile) { @@ -100,33 +100,39 @@ void Files::extractZip(const std::string &zipName, const std::string &inDir, remove(zipName.c_str()); } -int Files::renameFile(const std::string &pFrom, const std::string &pTo) -{ - FILE *file = fopen(pFrom.c_str(), "rb"); - if (file == NULL) - return -1; - - fseek(file, 0, SEEK_END); - size_t sz = ftell(file); - fseek(file, 0, SEEK_SET); +#endif // ANDROID __native_client__ - char *buf = (char *)malloc(sz + 1); - if (fread(buf, 1, sz, file) != sz) +int Files::renameFile(const std::string &srcName, const std::string &dstName) +{ + FILE *srcFile = fopen(srcName.c_str(), "rb"); + if (srcFile == nullptr) return -1; - fclose(file); - buf[sz] = 0; - - file = fopen(pTo.c_str(), "w+b"); - if (file == NULL) + FILE *dstFile = fopen(dstName.c_str(), "w+b"); + if (dstFile == nullptr) + { + fclose(srcFile); return -1; + } - if (fwrite(buf, 1, sz, file) != sz) - return -1; - fclose(file); + const int chunkSize = 500000; + char *buf = new char[chunkSize]; + size_t sz = 0; + while ((sz = fread(buf, 1, chunkSize, srcFile))) + { + if (fwrite(buf, 1, sz, dstFile) != sz) + { + delete [] buf; + fclose(srcFile); + fclose(dstFile); + ::remove(dstName.c_str()); + return -1; + } + } - free(buf); + delete [] buf; + fclose(srcFile); + fclose(dstFile); + ::remove(srcName.c_str()); return 0; } - -#endif // ANDROID __native_client__ diff --git a/src/utils/files.h b/src/utils/files.h index 4a44631cf..735812563 100644 --- a/src/utils/files.h +++ b/src/utils/files.h @@ -21,7 +21,6 @@ #ifndef UTILS_FILES_H #define UTILS_FILES_H -#if defined(ANDROID) || defined(__native_client__) #include namespace Files @@ -29,15 +28,17 @@ namespace Files #ifdef ANDROID void extractLocale(); #endif + +#if defined(ANDROID) || defined(__native_client__) void copyPhysFsFile(const std::string &inFile, const std::string &outFile); void copyPhysFsDir(const std::string &inDir, const std::string &outDir); void extractZip(const std::string &zipName, const std::string &inDir, const std::string &outDir); +#endif // ANDROID __native_client__ int renameFile(const std::string &pFrom, const std::string &pTo); } // namespace Files -#endif // ANDROID __native_client__ #endif // UTILS_FILES_H diff --git a/src/utils/files_unittest.cpp b/src/utils/files_unittest.cpp new file mode 100644 index 000000000..63e1fc271 --- /dev/null +++ b/src/utils/files_unittest.cpp @@ -0,0 +1,71 @@ +/* + * The ManaPlus Client + * Copyright (C) 2013 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "utils/files.h" + +#include "logger.h" + +#include "utils/physfstools.h" + +#include "gtest/gtest.h" + +#include "resources/resourcemanager.h" + +#include "debug.h" + +static void init() +{ + PHYSFS_init("manaplus"); + dirSeparator = "/"; + logger = new Logger(); + ResourceManager *resman = ResourceManager::getInstance(); + resman->addToSearchPath("data", false); + resman->addToSearchPath("../data", false); +} + +TEST(Files, renameFile) +{ + init(); + const int sz = 1234567; + char *buf = new char[sz]; + for (int f = 0; f < sz; f ++) + buf[f] = f; + + const std::string name1 = "file1.test"; + const std::string name2 = "file2.test"; + FILE *file = fopen(name1.c_str(), "w+b"); + fwrite(buf, 1, sz, file); + fclose(file); + + EXPECT_EQ(0, Files::renameFile(name1, name2)); + char *buf2 = new char[sz]; + FILE *file2 = fopen(name2.c_str(), "rb"); + EXPECT_NE(nullptr, file2); + fread(buf2, 1, sz, file2); + fclose(file2); + ::remove(name1.c_str()); + ::remove(name2.c_str()); + + for (int f = 0; f < sz; f ++) + EXPECT_EQ(buf[f], buf2[f]); + + delete [] buf; + delete [] buf2; +} -- cgit v1.2.3-70-g09d2 From 64535f9763c432bbb46be2769560bfd6a2202d72 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Thu, 7 Nov 2013 16:32:18 +0300 Subject: add progress bar while loading in SDL2 in Android. --- src/client.cpp | 35 +++++++++++++++++++++++++++++++++-- src/utils/files.cpp | 19 +++++++++++++++++++ src/utils/files.h | 4 ++++ 3 files changed, 56 insertions(+), 2 deletions(-) (limited to 'src/utils/files.h') diff --git a/src/client.cpp b/src/client.cpp index 461ce03c8..bcaba2a40 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -152,6 +152,12 @@ #define _nacl_dir std::string("/persistent/manaplus") #endif +#ifdef ANDROID +#ifdef USE_SDL2 +int loadingProgressCounter = 1; +#endif +#endif + std::string errorMessage; ErrorListener errorListener; LoginData loginData; @@ -309,7 +315,6 @@ void Client::gameInit() "Exiting.", mLocalDataDir.c_str())); } - extractDataDir(); initLang(); chatLogger = new ChatLogger; @@ -348,6 +353,8 @@ void Client::gameInit() SDL_EventState(SDL_SYSWMEVENT, SDL_IGNORE); SDL_EventState(SDL_USEREVENT, SDL_IGNORE); + initGraphics(); + extractDataDir(); mountDataDir(); if (mOptions.dataPath.empty() @@ -373,7 +380,6 @@ void Client::gameInit() resman->addToSearchPath(mLocalDataDir, false); TranslationManager::loadCurrentLang(); - initGraphics(); initTitle(); Theme::selectSkin(); @@ -580,10 +586,33 @@ void Client::initTitle() setIcon(); } +#ifdef ANDROID +#ifdef USE_SDL2 +static void updateProgress(int cnt) +{ + const int progress = cnt + loadingProgressCounter; + const int h = mainGraphics->mHeight; + mainGraphics->setColor(gcn::Color(255, 255, 255)); + const int maxSize = mainGraphics->mWidth - 100; + const int width = maxSize * progress / 450; + mainGraphics->fillRectangle(gcn::Rectangle(50, h - 100, width, 50)); + mainGraphics->updateScreen(); +} + +static void setProgress(const int val) +{ + loadingProgressCounter = val; + updateProgress(loadingProgressCounter); +} +#endif +#endif + void Client::extractDataDir() { #ifdef ANDROID #ifdef USE_SDL2 + Files::setCopyCallBack(&updateProgress); + setProgress(0); extractAssets(); const std::string zipName = std::string(getenv( @@ -2965,6 +2994,7 @@ void Client::extractAssets() logger->log("asset size: %d", size2); fwrite(buf, 1, size2, file); SDL_RWclose(rw); + setProgress(loadingProgressCounter + 1); } else { @@ -2983,6 +3013,7 @@ void Client::extractAssets() int size2 = SDL_RWread(rw, buf, 1, size); fwrite(buf, 1, size2, file2); SDL_RWclose(rw); + setProgress(loadingProgressCounter + 1); } fclose(file2); diff --git a/src/utils/files.cpp b/src/utils/files.cpp index 6f57d3062..bfaed472a 100644 --- a/src/utils/files.cpp +++ b/src/utils/files.cpp @@ -63,6 +63,18 @@ void Files::extractLocale() #endif // ANDROID #if defined(ANDROID) || defined(__native_client__) + +namespace +{ + int mFilesCount = 0; + Files::CopyFileCallbackPtr mCallbackPtr = nullptr; +} // namespace + +void Files::setCopyCallBack(Files::CopyFileCallbackPtr callback) +{ + mCallbackPtr = callback; +} + void Files::copyPhysFsFile(const std::string &inFile, const std::string &outFile) { @@ -72,6 +84,13 @@ void Files::copyPhysFsFile(const std::string &inFile, fwrite(buf, 1, size, file); fclose(file); free(buf); +#ifdef ANDROID + if (mCallbackPtr) + { + mCallbackPtr(mFilesCount); + mFilesCount ++; + } +#endif } void Files::copyPhysFsDir(const std::string &inDir, const std::string &outDir) diff --git a/src/utils/files.h b/src/utils/files.h index 735812563..f356d7688 100644 --- a/src/utils/files.h +++ b/src/utils/files.h @@ -30,6 +30,10 @@ namespace Files #endif #if defined(ANDROID) || defined(__native_client__) + typedef void (*CopyFileCallbackPtr) (int cnt); + + void setCopyCallBack(CopyFileCallbackPtr callback); + void copyPhysFsFile(const std::string &inFile, const std::string &outFile); void copyPhysFsDir(const std::string &inDir, const std::string &outDir); -- cgit v1.2.3-70-g09d2