From 6e5600a9a5bd2fa2aafa577fd0b113d99ba5b2b4 Mon Sep 17 00:00:00 2001 From: Daniel-Genkin Date: Thu, 28 Oct 2021 10:26:14 -0400 Subject: [PATCH 1/3] Added another constructor to avoid requiring a full ANativeActivity --- include/assimp/port/AndroidJNI/AndroidJNIIOSystem.h | 2 ++ port/AndroidJNI/AndroidJNIIOSystem.cpp | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/include/assimp/port/AndroidJNI/AndroidJNIIOSystem.h b/include/assimp/port/AndroidJNI/AndroidJNIIOSystem.h index 01505d571..370327542 100644 --- a/include/assimp/port/AndroidJNI/AndroidJNIIOSystem.h +++ b/include/assimp/port/AndroidJNI/AndroidJNIIOSystem.h @@ -65,6 +65,8 @@ public: /** Constructor. */ AndroidJNIIOSystem(ANativeActivity* activity); + AndroidJNIIOSystem(const char *internalPath, AAssetManager assetManager); + /** Destructor. */ ~AndroidJNIIOSystem(); diff --git a/port/AndroidJNI/AndroidJNIIOSystem.cpp b/port/AndroidJNI/AndroidJNIIOSystem.cpp index db499a20b..bed40ce51 100644 --- a/port/AndroidJNI/AndroidJNIIOSystem.cpp +++ b/port/AndroidJNI/AndroidJNIIOSystem.cpp @@ -67,6 +67,12 @@ AndroidJNIIOSystem::AndroidJNIIOSystem(ANativeActivity* activity) AndroidActivityInit(activity); } +AndroidJNIIOSystem::AndroidJNIIOSystem(const char *internalPath, AAssetManager assetManager) +{ + mApkWorkspacePath = internalDataPath; + mApkAssetManager = assetManager; +} + // ------------------------------------------------------------------------------------------------ // Destructor. AndroidJNIIOSystem::~AndroidJNIIOSystem() From e5cd5733e1c2e5aae93382dae0b2524657a96c7a Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Thu, 28 Oct 2021 17:50:25 +0200 Subject: [PATCH 2/3] Update AndroidJNIIOSystem.cpp --- port/AndroidJNI/AndroidJNIIOSystem.cpp | 169 ++++++++++++------------- 1 file changed, 83 insertions(+), 86 deletions(-) diff --git a/port/AndroidJNI/AndroidJNIIOSystem.cpp b/port/AndroidJNI/AndroidJNIIOSystem.cpp index bed40ce51..00cf3af9c 100644 --- a/port/AndroidJNI/AndroidJNIIOSystem.cpp +++ b/port/AndroidJNI/AndroidJNIIOSystem.cpp @@ -3,7 +3,7 @@ Open Asset Import Library (assimp) --------------------------------------------------------------------------- -Copyright (c) 2006-2020, assimp team +Copyright (c) 2006-2021, assimp team All rights reserved. @@ -67,51 +67,50 @@ AndroidJNIIOSystem::AndroidJNIIOSystem(ANativeActivity* activity) AndroidActivityInit(activity); } -AndroidJNIIOSystem::AndroidJNIIOSystem(const char *internalPath, AAssetManager assetManager) -{ - mApkWorkspacePath = internalDataPath; - mApkAssetManager = assetManager; +AndroidJNIIOSystem::AndroidJNIIOSystem(const char *internalPath, AAssetManager assetManager) : + mApkWorkspacePath(internalDataPath), + mApkAssetManager(assetManager) { + // empty } // ------------------------------------------------------------------------------------------------ // Destructor. -AndroidJNIIOSystem::~AndroidJNIIOSystem() -{ - // nothing to do here +AndroidJNIIOSystem::~AndroidJNIIOSystem() { + // nothing to do here } // ------------------------------------------------------------------------------------------------ // Tests for the existence of a file at the given path. -bool AndroidJNIIOSystem::Exists( const char* pFile) const -{ - AAsset* asset = AAssetManager_open(mApkAssetManager, pFile, - AASSET_MODE_UNKNOWN); - FILE* file = ::fopen( (mApkWorkspacePath + getOsSeparator() + std::string(pFile)).c_str(), "rb"); +bool AndroidJNIIOSystem::Exists( const char* pFile) const { + AAsset* asset = AAssetManager_open(mApkAssetManager, pFile, AASSET_MODE_UNKNOWN); + FILE* file = ::fopen( (mApkWorkspacePath + getOsSeparator() + std::string(pFile)).c_str(), "rb"); + + if (!asset && !file) { + __android_log_print(ANDROID_LOG_ERROR, "Assimp", "Asset manager can not find: %s", pFile); + return false; + } - if (!asset && !file) - { - __android_log_print(ANDROID_LOG_ERROR, "Assimp", "Asset manager can not find: %s", pFile); - return false; - } - - __android_log_print(ANDROID_LOG_ERROR, "Assimp", "Asset exists"); - if (file) - ::fclose( file); - return true; + __android_log_print(ANDROID_LOG_ERROR, "Assimp", "Asset exists"); + if (file) { + ::fclose( file); + } + + return true; } // ------------------------------------------------------------------------------------------------ // Inits Android extractor -void AndroidJNIIOSystem::AndroidActivityInit(ANativeActivity* activity) -{ - mApkWorkspacePath = activity->internalDataPath; - mApkAssetManager = activity->assetManager; +void AndroidJNIIOSystem::AndroidActivityInit(ANativeActivity* activity) { + if (activity == nullptr) { + return; + } + mApkWorkspacePath = activity->internalDataPath; + mApkAssetManager = activity->assetManager; } // ------------------------------------------------------------------------------------------------ // Create the directory for the extracted resource -static int mkpath(std::string path, mode_t mode) -{ +static int mkpath(std::string path, mode_t mode) { if (mkdir(path.c_str(), mode) == -1) { switch(errno) { case ENOENT: @@ -131,82 +130,80 @@ static int mkpath(std::string path, mode_t mode) // ------------------------------------------------------------------------------------------------ // Extracts android asset -bool AndroidJNIIOSystem::AndroidExtractAsset(std::string name) -{ - std::string newPath = mApkWorkspacePath + getOsSeparator() + name; +bool AndroidJNIIOSystem::AndroidExtractAsset(std::string name) { + std::string newPath = mApkWorkspacePath + getOsSeparator() + name; - DefaultIOSystem io; + DefaultIOSystem io; - // Do not extract if extracted already - if ( io.Exists(newPath.c_str()) ) { - __android_log_print(ANDROID_LOG_DEFAULT, "Assimp", "Asset already extracted"); - return true; - } - // Open file - AAsset* asset = AAssetManager_open(mApkAssetManager, name.c_str(), + // Do not extract if extracted already + if ( io.Exists(newPath.c_str()) ) { + __android_log_print(ANDROID_LOG_DEFAULT, "Assimp", "Asset already extracted"); + return true; + } + + // Open file + AAsset* asset = AAssetManager_open(mApkAssetManager, name.c_str(), AASSET_MODE_UNKNOWN); - std::vector assetContent; + std::vector assetContent; - if (asset != NULL) { - // Find size - off_t assetSize = AAsset_getLength(asset); + if (asset != NULL) { + // Find size + off_t assetSize = AAsset_getLength(asset); - // Prepare input buffer - assetContent.resize(assetSize); + // Prepare input buffer + assetContent.resize(assetSize); - // Store input buffer - AAsset_read(asset, &assetContent[0], assetSize); + // Store input buffer + AAsset_read(asset, &assetContent[0], assetSize); - // Close - AAsset_close(asset); + // Close + AAsset_close(asset); - // Prepare directory for output buffer - std::string directoryNewPath = newPath; - directoryNewPath = dirname(&directoryNewPath[0]); + // Prepare directory for output buffer + std::string directoryNewPath = newPath; + directoryNewPath = dirname(&directoryNewPath[0]); - if (mkpath(directoryNewPath, S_IRUSR | S_IWUSR | S_IXUSR) == -1) { - __android_log_print(ANDROID_LOG_ERROR, "assimp", - "Can not create the directory for the output file"); - } + if (mkpath(directoryNewPath, S_IRUSR | S_IWUSR | S_IXUSR) == -1) { + __android_log_print(ANDROID_LOG_ERROR, "assimp", "Can not create the directory for the output file"); + } - // Prepare output buffer - std::ofstream assetExtracted(newPath.c_str(), - std::ios::out | std::ios::binary); - if (!assetExtracted) { - __android_log_print(ANDROID_LOG_ERROR, "assimp", - "Can not open output file"); - } + // Prepare output buffer + std::ofstream assetExtracted(newPath.c_str(), std::ios::out | std::ios::binary); + if (!assetExtracted) { + __android_log_print(ANDROID_LOG_ERROR, "assimp", "Can not open output file"); + } - // Write output buffer into a file - assetExtracted.write(&assetContent[0], assetContent.size()); - assetExtracted.close(); + // Write output buffer into a file + assetExtracted.write(&assetContent[0], assetContent.size()); + assetExtracted.close(); - __android_log_print(ANDROID_LOG_DEFAULT, "Assimp", "Asset extracted"); - } else { - __android_log_print(ANDROID_LOG_ERROR, "assimp", "Asset not found: %s", name.c_str()); - return false; - } - return true; + __android_log_print(ANDROID_LOG_DEFAULT, "Assimp", "Asset extracted"); + } else { + __android_log_print(ANDROID_LOG_ERROR, "assimp", "Asset not found: %s", name.c_str()); + return false; + } + + return true; } // ------------------------------------------------------------------------------------------------ // Open a new file with a given path. -IOStream* AndroidJNIIOSystem::Open( const char* strFile, const char* strMode) -{ - ai_assert(NULL != strFile); - ai_assert(NULL != strMode); +IOStream* AndroidJNIIOSystem::Open( const char* strFile, const char* strMode) { + ai_assert(nullptr != strFile); + ai_assert(nullptr != strMode); - std::string fullPath(mApkWorkspacePath + getOsSeparator() + std::string(strFile)); - if (Exists(strFile)) - AndroidExtractAsset(std::string(strFile)); + std::string fullPath(mApkWorkspacePath + getOsSeparator() + std::string(strFile)); + if (Exists(strFile)) { + AndroidExtractAsset(std::string(strFile)); + } - FILE* file = ::fopen( fullPath.c_str(), strMode); + FILE* file = ::fopen( fullPath.c_str(), strMode); + if (nullptr == file) { + return nullptr; + } - if( NULL == file) - return NULL; - - __android_log_print(ANDROID_LOG_ERROR, "assimp", "AndroidIOSystem: file %s opened", fullPath.c_str()); - return new DefaultIOStream(file, fullPath); + __android_log_print(ANDROID_LOG_ERROR, "assimp", "AndroidIOSystem: file %s opened", fullPath.c_str()); + return new DefaultIOStream(file, fullPath); } #undef PATHLIMIT From 5333e41607f07046c3a15c55bc74e1eecfad0ab7 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Thu, 28 Oct 2021 17:52:01 +0200 Subject: [PATCH 3/3] Update AndroidJNIIOSystem.h --- .../port/AndroidJNI/AndroidJNIIOSystem.h | 24 +++++++------------ 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/include/assimp/port/AndroidJNI/AndroidJNIIOSystem.h b/include/assimp/port/AndroidJNI/AndroidJNIIOSystem.h index 370327542..bb52d3065 100644 --- a/include/assimp/port/AndroidJNI/AndroidJNIIOSystem.h +++ b/include/assimp/port/AndroidJNI/AndroidJNIIOSystem.h @@ -2,7 +2,7 @@ Open Asset Import Library (assimp) ---------------------------------------------------------------------- -Copyright (c) 2006-2020, assimp team +Copyright (c) 2006-2021, assimp team All rights reserved. Redistribution and use of this software in source and binary forms, @@ -54,38 +54,32 @@ namespace Assimp { // --------------------------------------------------------------------------- /** Android extension to DefaultIOSystem using the standard C file functions */ -class ASSIMP_API AndroidJNIIOSystem : public DefaultIOSystem -{ +class ASSIMP_API AndroidJNIIOSystem : public DefaultIOSystem { public: - /** Initialize android activity data */ std::string mApkWorkspacePath; AAssetManager* mApkAssetManager; - /** Constructor. */ + /// Constructor. AndroidJNIIOSystem(ANativeActivity* activity); + /// Class constructor with past and asset manager. AndroidJNIIOSystem(const char *internalPath, AAssetManager assetManager); - /** Destructor. */ + /// Destructor. ~AndroidJNIIOSystem(); - // ------------------------------------------------------------------- - /** Tests for the existence of a file at the given path. */ + /// Tests for the existence of a file at the given path. bool Exists( const char* pFile) const; - // ------------------------------------------------------------------- - /** Opens a file at the given path, with given mode */ + /// Opens a file at the given path, with given mode IOStream* Open( const char* strFile, const char* strMode); - // ------------------------------------------------------------------------------------------------ - // Inits Android extractor + /// Inits Android extractor void AndroidActivityInit(ANativeActivity* activity); - // ------------------------------------------------------------------------------------------------ - // Extracts android asset + /// Extracts android asset bool AndroidExtractAsset(std::string name); - }; } //!ns Assimp