2
0

AndroidJNIIOSystem.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2021, assimp team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /** @file Android extension of DefaultIOSystem using the standard C file functions */
  35. #include <assimp/config.h>
  36. #include <android/api-level.h>
  37. #if __ANDROID__ and __ANDROID_API__ > 9 and defined(AI_CONFIG_ANDROID_JNI_ASSIMP_MANAGER_SUPPORT)
  38. #include <libgen.h>
  39. #include <stdlib.h>
  40. #include <sys/stat.h>
  41. #include <android/log.h>
  42. #include <android/asset_manager.h>
  43. #include <android/asset_manager_jni.h>
  44. #include <android/native_activity.h>
  45. #include <assimp/ai_assert.h>
  46. #include <assimp/port/AndroidJNI/AndroidJNIIOSystem.h>
  47. #include <assimp/DefaultIOStream.h>
  48. #include <fstream>
  49. using namespace Assimp;
  50. // ------------------------------------------------------------------------------------------------
  51. // Constructor.
  52. AndroidJNIIOSystem::AndroidJNIIOSystem(ANativeActivity* activity)
  53. {
  54. AndroidActivityInit(activity);
  55. }
  56. AndroidJNIIOSystem::AndroidJNIIOSystem(const char *internalPath, AAssetManager* assetManager) :
  57. mApkWorkspacePath(internalPath),
  58. mApkAssetManager(assetManager) {
  59. // empty
  60. }
  61. // ------------------------------------------------------------------------------------------------
  62. // Destructor.
  63. AndroidJNIIOSystem::~AndroidJNIIOSystem() {
  64. // nothing to do here
  65. }
  66. // ------------------------------------------------------------------------------------------------
  67. // Tests for the existence of a file at the given path.
  68. bool AndroidJNIIOSystem::Exists( const char* pFile) const {
  69. AAsset* asset = AAssetManager_open(mApkAssetManager, pFile, AASSET_MODE_UNKNOWN);
  70. FILE* file = ::fopen( (mApkWorkspacePath + getOsSeparator() + std::string(pFile)).c_str(), "rb");
  71. if (!asset && !file) {
  72. __android_log_print(ANDROID_LOG_ERROR, "Assimp", "Asset manager can not find: %s", pFile);
  73. return false;
  74. }
  75. __android_log_print(ANDROID_LOG_ERROR, "Assimp", "Asset exists");
  76. if (file) {
  77. ::fclose( file);
  78. }
  79. return true;
  80. }
  81. // ------------------------------------------------------------------------------------------------
  82. // Inits Android extractor
  83. void AndroidJNIIOSystem::AndroidActivityInit(ANativeActivity* activity) {
  84. if (activity == nullptr) {
  85. return;
  86. }
  87. mApkWorkspacePath = activity->internalDataPath;
  88. mApkAssetManager = activity->assetManager;
  89. }
  90. // ------------------------------------------------------------------------------------------------
  91. // Create the directory for the extracted resource
  92. static int mkpath(std::string path, mode_t mode) {
  93. if (mkdir(path.c_str(), mode) == -1) {
  94. switch(errno) {
  95. case ENOENT:
  96. if (mkpath(path.substr(0, path.find_last_of('/')), mode) == -1)
  97. return -1;
  98. else
  99. return mkdir(path.c_str(), mode);
  100. case EEXIST:
  101. return 0;
  102. default:
  103. return -1;
  104. }
  105. }
  106. return 0;
  107. }
  108. // ------------------------------------------------------------------------------------------------
  109. // Extracts android asset
  110. bool AndroidJNIIOSystem::AndroidExtractAsset(std::string name) {
  111. std::string newPath = mApkWorkspacePath + getOsSeparator() + name;
  112. DefaultIOSystem io;
  113. // Do not extract if extracted already
  114. if ( io.Exists(newPath.c_str()) ) {
  115. __android_log_print(ANDROID_LOG_DEFAULT, "Assimp", "Asset already extracted");
  116. return true;
  117. }
  118. // Open file
  119. AAsset* asset = AAssetManager_open(mApkAssetManager, name.c_str(),
  120. AASSET_MODE_UNKNOWN);
  121. std::vector<char> assetContent;
  122. if (asset != NULL) {
  123. // Find size
  124. off_t assetSize = AAsset_getLength(asset);
  125. // Prepare input buffer
  126. assetContent.resize(assetSize);
  127. // Store input buffer
  128. AAsset_read(asset, &assetContent[0], assetSize);
  129. // Close
  130. AAsset_close(asset);
  131. // Prepare directory for output buffer
  132. std::string directoryNewPath = newPath;
  133. directoryNewPath = dirname(&directoryNewPath[0]);
  134. if (mkpath(directoryNewPath, S_IRUSR | S_IWUSR | S_IXUSR) == -1) {
  135. __android_log_print(ANDROID_LOG_ERROR, "assimp", "Can not create the directory for the output file");
  136. }
  137. // Prepare output buffer
  138. std::ofstream assetExtracted(newPath.c_str(), std::ios::out | std::ios::binary);
  139. if (!assetExtracted) {
  140. __android_log_print(ANDROID_LOG_ERROR, "assimp", "Can not open output file");
  141. }
  142. // Write output buffer into a file
  143. assetExtracted.write(&assetContent[0], assetContent.size());
  144. assetExtracted.close();
  145. __android_log_print(ANDROID_LOG_DEFAULT, "Assimp", "Asset extracted");
  146. } else {
  147. __android_log_print(ANDROID_LOG_ERROR, "assimp", "Asset not found: %s", name.c_str());
  148. return false;
  149. }
  150. return true;
  151. }
  152. // ------------------------------------------------------------------------------------------------
  153. // Open a new file with a given path.
  154. IOStream* AndroidJNIIOSystem::Open( const char* strFile, const char* strMode) {
  155. ai_assert(nullptr != strFile);
  156. ai_assert(nullptr != strMode);
  157. std::string fullPath(mApkWorkspacePath + getOsSeparator() + std::string(strFile));
  158. if (Exists(strFile)) {
  159. AndroidExtractAsset(std::string(strFile));
  160. }
  161. FILE* file = ::fopen( fullPath.c_str(), strMode);
  162. if (nullptr == file) {
  163. return nullptr;
  164. }
  165. __android_log_print(ANDROID_LOG_ERROR, "assimp", "AndroidIOSystem: file %s opened", fullPath.c_str());
  166. return new DefaultIOStream(file, fullPath);
  167. }
  168. #undef PATHLIMIT
  169. #endif // __ANDROID__ and __ANDROID_API__ > 9 and defined(AI_CONFIG_ANDROID_JNI_ASSIMP_MANAGER_SUPPORT)