void test_assets() { AAsset* a = AAssetManager_open( g_asset_mgr, "raw/test.jpg", AASSET_MODE_RANDOM ); assert( a != 0 ); off_t len = AAsset_getLength(a); const char *buf = (const char *)AAsset_getBuffer(a); LOGI( "asset: %p %d\n", a, len ); for( size_t i = 0; i < len; ++i ) { LOGI( "x: %c\n", buf[i] ); } // AAssetDir *dir = AAssetManager_openDir(g_asset_mgr, "assets" ); // // while( true ) { // const char *name = AAssetDir_getNextFileName(dir); // // if( name == 0 ) { // break; // } // // LOGI( "asset: %s\n", name ); // } }
tImage *LoadJPG(const char *strFileName) { tImage *pImageData = NULL; struct jpeg_decompress_struct cinfo; jpeg_error_mgr jerr; pImageData = (tImage*)malloc(sizeof(tImage)); cinfo.err = jpeg_std_error(&jerr); jpeg_create_decompress(&cinfo); AAsset* pAsset = AAssetManager_open(g_amgr, strFileName, AASSET_MODE_UNKNOWN); if (!pAsset) { free(pImageData); LOGE("Error opening jpeg %s", strFileName); return NULL; } unsigned char* ucharRawData = (unsigned char*)AAsset_getBuffer(pAsset); long myAssetLength = (long)AAsset_getLength(pAsset); // the jpeg_stdio_src alternative func, which is also included in IJG's lib. jpeg_mem_src(&cinfo, ucharRawData, myAssetLength); jpeg_read_header(&cinfo, TRUE); jpeg_start_decompress(&cinfo); pImageData->channels = cinfo.num_components; pImageData->sizeX = cinfo.image_width; pImageData->sizeY = cinfo.image_height; int rowSpan = cinfo.image_width * cinfo.num_components; pImageData->data = ((unsigned char*)malloc(sizeof(unsigned char)*rowSpan*pImageData->sizeY)); unsigned char** rowPtr = new unsigned char*[pImageData->sizeY]; for (int i = 0; i < pImageData->sizeY; i++) rowPtr[i] = &(pImageData->data[i * rowSpan]); int rowsRead = 0; while (cinfo.output_scanline < cinfo.output_height) rowsRead += jpeg_read_scanlines(&cinfo, &rowPtr[rowsRead], cinfo.output_height - rowsRead); delete [] rowPtr; jpeg_finish_decompress(&cinfo); jpeg_destroy_decompress(&cinfo); AAsset_close(pAsset); return pImageData; }
void withAssetData(const char* filename, const std::function<void(off64_t, const void*)>& callback) { auto asset = AAssetManager_open(g_assetManager, filename, AASSET_MODE_BUFFER); if (!asset) { throw std::runtime_error("Failed to open file"); } auto buffer = AAsset_getBuffer(asset); off64_t size = AAsset_getLength64(asset); callback(size, buffer); AAsset_close(asset); }
void init(AAssetManager *assetManager) { AAsset *vertexShaderAsset = AAssetManager_open(assetManager, "shader.glslv", AASSET_MODE_BUFFER); assert(vertexShaderAsset != NULL); const void *vertexShaderBuf = AAsset_getBuffer(vertexShaderAsset); assert(vertexShaderBuf != NULL); off_t vertexShaderLength = AAsset_getLength(vertexShaderAsset); vertexShaderSource = std::string((const char*)vertexShaderBuf, (size_t)vertexShaderLength); AAsset_close(vertexShaderAsset); AAsset *fragmentShaderAsset = AAssetManager_open(assetManager, "shader.glslf", AASSET_MODE_BUFFER); assert(fragmentShaderAsset != NULL); const void *fragmentShaderBuf = AAsset_getBuffer(fragmentShaderAsset); assert(fragmentShaderBuf != NULL); off_t fragmentShaderLength = AAsset_getLength(fragmentShaderAsset); fragmentShaderSource = std::string((const char*)fragmentShaderBuf, (size_t)fragmentShaderLength); AAsset_close(fragmentShaderAsset); sensorManager = AcquireASensorManagerInstance(); assert(sensorManager != NULL); accelerometer = ASensorManager_getDefaultSensor(sensorManager, ASENSOR_TYPE_ACCELEROMETER); assert(accelerometer != NULL); looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS); assert(looper != NULL); accelerometerEventQueue = ASensorManager_createEventQueue(sensorManager, looper, LOOPER_ID_USER, NULL, NULL); assert(accelerometerEventQueue != NULL); auto status = ASensorEventQueue_enableSensor(accelerometerEventQueue, accelerometer); assert(status >= 0); status = ASensorEventQueue_setEventRate(accelerometerEventQueue, accelerometer, SENSOR_REFRESH_PERIOD_US); assert(status >= 0); (void)status; //to silent unused compiler warning generateXPos(); }
void init(AAssetManager *assetManager) { AAsset *vertexShaderAsset = AAssetManager_open(assetManager, "shader.glslv", AASSET_MODE_BUFFER); assert(vertexShaderAsset != NULL); const void *vertexShaderBuf = AAsset_getBuffer(vertexShaderAsset); assert(vertexShaderBuf != NULL); off_t vertexShaderLength = AAsset_getLength(vertexShaderAsset); vertexShaderSource = std::string((const char*)vertexShaderBuf, (size_t)vertexShaderLength); AAsset_close(vertexShaderAsset); AAsset *fragmentShaderAsset = AAssetManager_open(assetManager, "shader.glslf", AASSET_MODE_BUFFER); assert(fragmentShaderAsset != NULL); const void *fragmentShaderBuf = AAsset_getBuffer(fragmentShaderAsset); assert(fragmentShaderBuf != NULL); off_t fragmentShaderLength = AAsset_getLength(fragmentShaderAsset); fragmentShaderSource = std::string((const char*)fragmentShaderBuf, (size_t)fragmentShaderLength); AAsset_close(fragmentShaderAsset); sensorManager = ASensorManager_getInstance(); assert(sensorManager != NULL); accelerometer = ASensorManager_getDefaultSensor(sensorManager, ASENSOR_TYPE_ACCELEROMETER); assert(accelerometer != NULL); looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS); assert(looper != NULL); accelerometerEventQueue = ASensorManager_createEventQueue(sensorManager, looper, LOOPER_ID_USER, NULL, NULL); assert(accelerometerEventQueue != NULL); int setEventRateResult = ASensorEventQueue_setEventRate(accelerometerEventQueue, accelerometer, int32_t(1000000 / SENSOR_REFRESH_RATE)); int enableSensorResult = ASensorEventQueue_enableSensor(accelerometerEventQueue, accelerometer); assert(enableSensorResult >= 0); generateXPos(); }
FileData get_asset_data(const char* relative_path) { assert(relative_path != NULL); AAsset *asset = AAssetManager_open(asset_manager, relative_path, AASSET_MODE_STREAMING); assert(asset != NULL); return (FileData) { AAsset_getLength(asset), AAsset_getBuffer(asset), asset }; }
AssetFile AndroidAssetLoader::loadAsset(const char* relativePath) { if(relativePath != NULL) { AAsset* asset = AAssetManager_open(assetManager_,relativePath, AASSET_MODE_STREAMING); if(asset != NULL) { return AssetFile(AAsset_getLength(asset), AAsset_getBuffer(asset), asset); } } return AssetFile(); }
const void* Asset::Bufferize() { if(!mAsset) { LOGE("Asset::Bufferize() error: asset is not opened"); return NULL; } int length=AAsset_getLength(mAsset); mBuffer=new char[length+1]; memcpy(mBuffer,AAsset_getBuffer(mAsset),length); mBuffer[length]=0; return mBuffer; }
static Ogre::DataStreamPtr openAPKFile(const Ogre::String& fileName){ Ogre::DataStreamPtr stream; AAsset* asset = AAssetManager_open(gAssetMgr, fileName.c_str(), AASSET_MODE_BUFFER); if(asset){ off_t length = AAsset_getLength(asset); void* membuf = OGRE_MALLOC(length, Ogre::MEMCATEGORY_GENERAL); memcpy(membuf, AAsset_getBuffer(asset), length); AAsset_close(asset); stream = Ogre::DataStreamPtr(new Ogre::MemoryDataStream(membuf, length, true, true)); } return stream; }
static int engine_init_shaders(struct engine* engine) { LOGI("from engine_init_shaders \n"); setupGraphics(engine->width, engine->height); AAssetDir* assetDir = AAssetManager_openDir(engine->assetManager, ""); AAssetDir_rewind(assetDir); const char* name=NULL; const char* vert=NULL, *frag=NULL; while ( (name = AAssetDir_getNextFileName(assetDir)) != NULL) { if (frag == NULL && NULL != strstr(name, gTagFrag)) //if we haven't found the fragment shader and 'name' is the fragment shader, save it. frag = name; if (vert == NULL && NULL != strstr(name, gTagVert)) //if we haven't found the vertex shader and the 'name' is the vertex shader, save it. vert = name; if (!vert && !frag) //if we found both files, we're done break; } //open the shader assets AAsset* fragAsset = AAssetManager_open(engine->assetManager, frag, AASSET_MODE_BUFFER); if (!fragAsset) { LOGE(" error opening %s\n", fragAsset); return; } AAsset* vertAsset = AAssetManager_open(engine->assetManager, vert, AASSET_MODE_BUFFER); if (!vertAsset) { LOGE(" error opening %s\n", vertAsset); return; } //access the shader asset buffer in preperation for reading const char* fragBuff = (const char*)AAsset_getBuffer(fragAsset); const char* vertBuff = (const char*)AAsset_getBuffer(vertAsset); setupGraphics(engine->width, engine->height); //minimaly initialize client graphics state LoadShaders(fragBuff, AAsset_getLength(fragAsset), vertBuff, AAsset_getLength(vertAsset)); //load the shaders AAssetDir_close(assetDir); }
Ogre::DataStreamPtr ApplicationContext::openAPKFile(const Ogre::String& fileName) { Ogre::MemoryDataStreamPtr stream; AAsset* asset = AAssetManager_open(mAAssetMgr, fileName.c_str(), AASSET_MODE_BUFFER); if(asset) { off_t length = AAsset_getLength(asset); stream.reset(new Ogre::MemoryDataStream(length, true, true)); memcpy(stream->getPtr(), AAsset_getBuffer(asset), length); AAsset_close(asset); } return stream; }
void FileSystem::createFileFromAsset(const char* path) { #ifdef __ANDROID__ static std::set<std::string> upToDateAssets; GP_ASSERT(path); std::string fullPath(__resourcePath); std::string resolvedPath = FileSystem::resolvePath(path); fullPath += resolvedPath; std::string directoryPath = fullPath.substr(0, fullPath.rfind('/')); struct stat s; if (stat(directoryPath.c_str(), &s) != 0) makepath(directoryPath, 0777); // To ensure that the files on the file system corresponding to the assets in the APK bundle // are always up to date (and in sync), we copy them from the APK to the file system once // for each time the process (game) runs. if (upToDateAssets.find(fullPath) == upToDateAssets.end()) { AAsset* asset = AAssetManager_open(__assetManager, resolvedPath.c_str(), AASSET_MODE_RANDOM); if (asset) { const void* data = AAsset_getBuffer(asset); int length = AAsset_getLength(asset); FILE* file = fopen(fullPath.c_str(), "wb"); if (file != NULL) { int ret = fwrite(data, sizeof(unsigned char), length, file); if (fclose(file) != 0) { GP_ERROR("Failed to close file on file system created from APK asset '%s'.", path); return; } if (ret != length) { GP_ERROR("Failed to write all data from APK asset '%s' to file on file system.", path); return; } } else { GP_ERROR("Failed to create file on file system from APK asset '%s'.", path); return; } upToDateAssets.insert(fullPath); } } #endif }
int ExtractAsset( AAssetManager* assetManager , const char* assetPath , const char* destPath ) { AAsset* asset = AAssetManager_open( assetManager, assetPath, AASSET_MODE_BUFFER ); if ( NULL == asset ) { FLOGE( "Unable to open asset for extraction: %s", assetPath ); return 0; } FLOGI( "Extracting asset: %s", assetPath ); const void* fileBuffer = AAsset_getBuffer( asset ); if ( NULL == fileBuffer ) { AAsset_close( asset ); FLOGE( "Unable to get buffer to asset for extraction: %s", assetPath ); return 0; } off_t bufferSize = AAsset_getLength( asset ); // Make sure the directories exist to put the file in if ( 0 == MakeFileDir( destPath, 00777 ) ) { AAsset_close( asset ); FLOGE( "Unable to make directories for asset extraction: %s", destPath ); return 0; } FILE* destFile = fopen( destPath, "wb" ); if ( NULL == destFile ) { AAsset_close( asset ); FLOGE( "Unable to open destination file for asset: %s", destPath ); return 0; } if ( 1 != fwrite( fileBuffer, bufferSize, 1, destFile ) ) { FLOGE( "Error extracting asset from %s to %s", assetPath, destPath ); fclose( destFile ); AAsset_close( asset ); return 0; } fclose( destFile ); AAsset_close( asset ); FLOGI( "Extracted asset from %s to %s", assetPath, destPath ); return 1; }
//----------------------------------------------------------------------- Object* ApkZipArchiveFactory::createInstance(const String& name, const String& guid = BLANK) { String apkName = name; if (apkName.size() > 0 && apkName[0] == '/') apkName.erase(apkName.begin()); AAsset* asset = AAssetManager_open(mAssetMgr, apkName.c_str(), AASSET_MODE_BUFFER); if (asset) { EmbeddedZipArchiveFactory::addEmbbeddedFile(apkName, (const u2::u2uint8*)AAsset_getBuffer(asset), AAsset_getLength(asset), 0); } return EmbeddedZipArchiveFactory::createInstance(apkName, guid); }
DataStreamPtr APKFileSystemArchive::open(const Ogre::String &filename, bool readOnly) const { DataStreamPtr stream; AAsset* asset = AAssetManager_open(mAssetMgr, (mPathPreFix + filename).c_str(), AASSET_MODE_BUFFER); if(asset) { off_t length = AAsset_getLength(asset); void* membuf = OGRE_MALLOC(length, Ogre::MEMCATEGORY_GENERAL); memcpy(membuf, AAsset_getBuffer(asset), length); AAsset_close(asset); stream = Ogre::DataStreamPtr(new Ogre::MemoryDataStream(membuf, length, true, true)); } return stream; }
Bitmap* engine_load_asset (Context* context, char* fpath) { char* buf; Bitmap* bitmap = (Bitmap*) malloc(sizeof(Bitmap)); AAsset* bitmap_asset = AAssetManager_open( context->app->activity->assetManager , fpath , AASSET_MODE_BUFFER ); buf = (char*) AAsset_getBuffer(bitmap_asset); bitmap_parse(bitmap, NULL, buf); AAsset_close(bitmap_asset); return bitmap; }
/** * static Bitmap nativeDecodeAsset(AssetManager am, String name, BitmapFactory.Options options) throws IOException; */ JNIEXPORT jobject JNICALL Java_android_backport_webp_WebPFactory_nativeDecodeAsset (JNIEnv *jniEnv, jclass, jobject assetManager, jstring name, jobject options) { if (!assetManager) { jniEnv->ThrowNew(jrefs::java::lang::NullPointerException->jclassRef, "assetManager is null"); return NULL; } if (!name) { jniEnv->ThrowNew(jrefs::java::lang::NullPointerException->jclassRef, "name is null"); return NULL; } AAssetManager *aam = AAssetManager_fromJava(jniEnv, assetManager); if (!aam) { jniEnv->ThrowNew(jrefs::java::lang::RuntimeException->jclassRef, "Failed to access AssetManager"); return NULL; } const char *namechars = jniEnv->GetStringUTFChars(name, NULL); AAsset *asset = AAssetManager_open(aam, namechars, AASSET_MODE_BUFFER); if (!asset) { jniEnv->ThrowNew(jrefs::java::io::FileNotFoundException->jclassRef, namechars); jniEnv->ReleaseStringUTFChars(name, namechars); return NULL; } const void *buffer = AAsset_getBuffer(asset); if (!buffer) { jniEnv->ReleaseStringUTFChars(name, namechars); AAsset_close(asset); jniEnv->ThrowNew(jrefs::java::lang::RuntimeException->jclassRef, "Failed to get asset buffer"); return NULL; } size_t length = AAsset_getLength(asset); __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, "Decoding asset '%s' (%d bytes)", namechars, length); jobject outputBitmap = WebPFactory_decodeBuffer(jniEnv, (const uint8_t *)buffer, length, options); jniEnv->ReleaseStringUTFChars(name, namechars); AAsset_close(asset); return outputBitmap; }
static int lopen(lua_State *L) { const char *fname = luaL_checkstring(L,1); char from[PATH_MAX]; snprintf(from, sizeof(from), "files/%s", fname); AAsset *asset = AAssetManager_open(__A, from, 2); if (asset == NULL) { lua_pushnil(L); lua_pushfstring(L, "asset open fail:%s", from); return 2; } off_t l = AAsset_getLength(asset); const void *p = AAsset_getBuffer(asset); lua_pushlightuserdata(L,(void*)p); lua_pushinteger(L,l); lua_pushlightuserdata(L,asset); return 3; }
my::buffer art::asset::retrieve(my::string request, my::string path) { DEBUG_SCOPE; my::buffer data; DEBUG_TRACE << "Attempting to find resource: " << request << my::endl; AAsset* asset = AAssetManager_open((AAssetManager*)manager_instance, request, AASSET_MODE_STREAMING); //m_ptr = asset; if(asset) { DEBUG_TRACE << "Found resource " << (int)AAsset_getLength(asset) << " bytes" << my::endl; // DEBUG_TRACE << "Contents: " << (char *)AAsset_getBuffer(asset) << my::endl; // DEBUG_TRACE << "Contents length: " << strlen((char *)AAsset_getBuffer(asset)) << my::endl; data.write((unsigned char *)AAsset_getBuffer(asset), AAsset_getLength(asset)); } else { DEBUG_TRACE << "Failure to load resource" << my::endl; } return(data); }
struct util_fp* util_file_open(const char* path) { struct util_fp* ret = NULL; AAssetManager* mgr = sl_get_asset_mgr(); AAsset* asset = AAssetManager_open(mgr, path, AASSET_MODE_UNKNOWN); if(asset){ ret = (struct util_fp*)malloc(sizeof(*ret)); if(!ret) goto ERROR; ret->cur_pos = 0; ret->asset = asset; ret->data = AAsset_getBuffer(asset); ret->length = AAsset_getLength(asset); return ret; } ERROR: if(ret) free(ret); return NULL; }
JSModulesUnbundle::Module JniJSModulesUnbundle::getModule(uint32_t moduleId) const { // can be nullptr for default constructor. FBASSERTMSGF(m_assetManager != nullptr, "Unbundle has not been initialized with an asset manager"); std::ostringstream sourceUrlBuilder; sourceUrlBuilder << moduleId << ".js"; auto sourceUrl = sourceUrlBuilder.str(); auto fileName = m_moduleDirectory + sourceUrl; auto asset = openAsset(m_assetManager, fileName, AASSET_MODE_BUFFER); const char *buffer = nullptr; if (asset != nullptr) { buffer = static_cast<const char *>(AAsset_getBuffer(asset.get())); } if (buffer == nullptr) { throw ModuleNotFound(moduleId); } return {sourceUrl, std::string(buffer, AAsset_getLength(asset.get()))}; }
//int AAsset_openFileDescriptor(AAsset* asset, off_t* outStart, off_t* outLength); //int AAsset_isAllocated(AAsset* asset); //const void* AAsset_getBuffer(AAsset* asset); CX_METHOD_DEF(cxMMapStream,Open,cxBool) { CX_ASSERT(this->cxStream.isOpen == false,"stream repeat open"); cxConstChars path = cxStringBody(this->cxStream.path); this->asset = AAssetManager_open(cxEngineGetAssetManager(), path, AASSET_MODE_UNKNOWN); if(this->asset == NULL){ CX_ERROR("open asset file %s failed",path); return false; } this->map = (cxAny)AAsset_getBuffer(this->asset); if(this->map == NULL){ CX_ERROR("asset get buffer error"); return false; } this->off = 0; this->cxStream.Length = (cxInt)AAsset_getLength(this->asset); this->cxStream.canRead = true; this->cxStream.canSeek = true; this->cxStream.canWrite = false; this->cxStream.isOpen = true; return true; }
Ogre::DataStreamPtr CFileManager::openDataStream(const std::string& fileName) { Ogre::DataStreamPtr stream; #if OGRE_PLATFORM == OGRE_PLATFORM_ANDROID AAsset* asset = AAssetManager_open(mNativeActivity->assetManager, fileName.c_str(), AASSET_MODE_BUFFER); if(asset) { off_t length = AAsset_getLength(asset); void* membuf = OGRE_MALLOC(length, Ogre::MEMCATEGORY_GENERAL); memcpy(membuf, AAsset_getBuffer(asset), length); AAsset_close(asset); stream = Ogre::DataStreamPtr(new Ogre::MemoryDataStream(membuf, length, true, true)); } #else // first one gives a memory leak //stream = Ogre::DataStreamPtr(new Ogre::FileStreamDataStream(new std::fstream(fileName))); // second one results in strange crash (segmentation fault on closing) //stream = Ogre::DataStreamPtr(new Ogre::FileStreamDataStream(OGRE_NEW_T( std::fstream, Ogre::MEMCATEGORY_GENERAL )( fileName.c_str(), std::fstream::in ) )); // this one works stream = Ogre::DataStreamPtr(new Ogre::FileHandleDataStream(fopen(fileName.c_str(), "r"))); #endif return stream; }
std::string ResourceLoader::loadLuaScript(const std::string &scriptname) { const std::string &filename = scriptname + ".lua"; AssetLoader *loader = AssetLoader::instance(); AAsset *asset = loader->open(filename); // the asset might not be found if (NULL == asset) { DEBUGLOG("Asset not found: %s", filename.c_str()); return NULL; } size_t length = AAsset_getLength(asset); const char *buffer = (char *)AAsset_getBuffer(asset); std::string script(buffer, length); if (buffer = NULL) { DEBUGLOG("Buffer is empty"); } AAsset_close(asset); return script; }
bool MemoryBuffer::lock(const fs::path &relativePath) { unlock(); #if defined(CHR_FS_RC) if (!locked) { auto basePath = fs::path("res") / relativePath; auto found = win::RESOURCES.find(basePath.generic_string()); if (found != win::RESOURCES.end()) { int resId = found->second; HRSRC infoHandle = ::FindResource(NULL, MAKEINTRESOURCE(resId), RT_RCDATA); if (infoHandle) { HGLOBAL handle = ::LoadResource(NULL, infoHandle); if (handle) { _size = ::SizeofResource(NULL, infoHandle); _data = ::LockResource(handle); locked = true; return true; } } } } #elif defined(CHR_FS_APK) asset = AAssetManager_open(android::assetManager, relativePath.c_str(), AASSET_MODE_BUFFER); if (asset) { _size = AAsset_getLength(asset); _data = AAsset_getBuffer(asset); locked = true; return true; } #elif defined(FS_JS_EMBED) || defined(FS_JS_PRELOAD) auto fd = open(relativePath.c_str(), O_RDONLY); if (fd != -1) { struct stat stats; if ((fstat(fd, &stats) != -1) && (stats.st_size > 0)) { _size = stats.st_size; _data = mmap(nullptr, size, PROT_READ, MAP_FILE | MAP_PRIVATE, fd, 0); close(fd); if (_data != MAP_FAILED) { locked = true; return true; } _size = 0; _data = nullptr; } } #endif return false; }
/* * sqlite3_vfs.xOpen - open database file. * Implemented using AAssetManager_open */ static int ndkOpen(sqlite3_vfs *pVfs, const char *zPath, sqlite3_file *pFile, int flags, int *pOutFlags) { const ndk_vfs* ndk = (ndk_vfs*) pVfs; ndk_file *ndkFile = (ndk_file*) pFile; // pMethod must be set to NULL, even if xOpen call fails. // // http://www.sqlite.org/c3ref/io_methods.html // "The only way to prevent a call to xClose following a failed sqlite3_vfs.xOpen // is for the sqlite3_vfs.xOpen to set the sqlite3_file.pMethods element to NULL." ndkFile->pMethod = NULL; // Allow only for opening main database file as read-only. // Opening JOURNAL/TEMP/WAL/etc. files will make call fails. // We don't need it, as DB opened from 'assets' .apk cannot // be modified if ( !zPath || (flags & SQLITE_OPEN_DELETEONCLOSE) || !(flags & SQLITE_OPEN_READONLY) || (flags & SQLITE_OPEN_READWRITE) || (flags & SQLITE_OPEN_CREATE) || !(flags & SQLITE_OPEN_MAIN_DB) ) { return SQLITE_PERM; } // Try top open database file AAsset* asset = AAssetManager_open(ndk->mgr, zPath, AASSET_MODE_RANDOM); if (!asset) { return SQLITE_CANTOPEN; } // Get pointer to database. This call can fail in case for example // out of memory. If file inside .apk is compressed, then whole // file must be allocated and read into memory. // If file is not compressed (inside .apk/zip), then this functions returns pointer // to memory-mapped address in .apk file, so doesn't need to allocate // explicit additional memory. // As for today there is no simple way to set if specific file // must be compressed or not. You can control it only by file extension. // Google for: android kNoCompressExt const void* buf = AAsset_getBuffer(asset); if (!buf) { AAsset_close(asset); return SQLITE_ERROR; } ndkFile->pMethod = ndk->pMethods; ndkFile->asset = asset; ndkFile->buf = buf; ndkFile->len = AAsset_getLength(asset); if (pOutFlags) { *pOutFlags = flags; } return SQLITE_OK; }
/** * Initialize an EGL context for the current display. */ static int engine_init_display(struct engine* engine) { // initialize OpenGL ES and EGL /* * Here specify the attributes of the desired configuration. * Below, we select an EGLConfig with at least 8 bits per color * component compatible with on-screen windows */ const EGLint attribs[] = { EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_BLUE_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_RED_SIZE, 8, EGL_DEPTH_SIZE, 8, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, EGL_NONE }; const EGLint context_attribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE }; EGLint w, h, dummy, format; EGLint numConfigs; EGLConfig config; EGLSurface surface; EGLContext context; EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY); eglInitialize(display, 0, 0); /* Here, the application chooses the configuration it desires. In this * sample, we have a very simplified selection process, where we pick * the first EGLConfig that matches our criteria */ eglChooseConfig(display, attribs, &config, 1, &numConfigs); /* EGL_NATIVE_VISUAL_ID is an attribute of the EGLConfig that is * guaranteed to be accepted by ANativeWindow_setBuffersGeometry(). * As soon as we picked a EGLConfig, we can safely reconfigure the * ANativeWindow buffers to match, using EGL_NATIVE_VISUAL_ID. */ eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format); ANativeWindow_setBuffersGeometry(engine->app->window, 0, 0, format); surface = eglCreateWindowSurface(display, config, engine->app->window, NULL); context = eglCreateContext(display, config, NULL, context_attribs); if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE) { LOGW("Unable to eglMakeCurrent"); return -1; } eglQuerySurface(display, surface, EGL_WIDTH, &w); eglQuerySurface(display, surface, EGL_HEIGHT, &h); engine->display = display; engine->context = context; engine->surface = surface; engine->width = w; engine->height = h; engine->state.angle = 0; engine->state.x0 = 0; engine->state.y0 = 0; engine->state.x1 = 0; engine->state.y1 = 0; engine->state.isTwoTouches = false; vesKiwiPointCloudApp::Ptr kiwiApp = vesKiwiPointCloudApp::Ptr(new vesKiwiPointCloudApp); kiwiApp->initGL(); engine->kiwiApp = kiwiApp; kiwiApp->resizeView(w, h); storageDir = "/mnt/sdcard"; assetManager = engine->app->activity->assetManager; // initialize shaders AAsset* vertex_asset = AAssetManager_open(assetManager, "vesShader_vert.glsl", AASSET_MODE_UNKNOWN); AAsset* fragment_asset = AAssetManager_open(assetManager, "vesShader_frag.glsl", AASSET_MODE_UNKNOWN); std::string vertex_source = std::string(static_cast<const char*>(AAsset_getBuffer(vertex_asset)), AAsset_getLength(vertex_asset)); std::string fragment_source = std::string(static_cast<const char*>(AAsset_getBuffer(fragment_asset)), AAsset_getLength(fragment_asset)); AAsset_close(vertex_asset); AAsset_close(fragment_asset); LOGI("vertex_source: %s\n", vertex_source.c_str()); LOGI("fragment_source: %s\n", fragment_source.c_str()); kiwiApp->initShader(vertex_source, fragment_source); std::string filename = "cturtle.vtp"; filename = copyAssetToExternalStorage(filename); kiwiApp->loadData(filename); kiwiApp->resetView(false); return 0; }
const void* Resource::bufferize() { return AAsset_getBuffer(mAsset); }
const void *File_getBuffer(FileRef file) { return AAsset_getBuffer((AAsset*)file); }
/* * ReadFile */ bool JNIHelper::ReadFile(const char *fileName, std::vector<uint8_t> *buffer_ref) { if (activity_ == NULL) { LOGI("JNIHelper has not been initialized.Call init() to initialize the " "helper"); return false; } // Lock mutex std::lock_guard<std::mutex> lock(mutex_); // First, try reading from externalFileDir; JNIEnv *env = AttachCurrentThread(); jstring str_path = GetExternalFilesDirJString(env); const char *path = env->GetStringUTFChars(str_path, NULL); std::string s(path); if (fileName[0] != '/') { s.append("/"); } s.append(fileName); std::ifstream f(s.c_str(), std::ios::binary); env->ReleaseStringUTFChars(str_path, path); env->DeleteLocalRef(str_path); activity_->vm->DetachCurrentThread(); if (f) { LOGI("reading:%s", s.c_str()); f.seekg(0, std::ifstream::end); int32_t fileSize = f.tellg(); f.seekg(0, std::ifstream::beg); buffer_ref->reserve(fileSize); buffer_ref->assign(std::istreambuf_iterator<char>(f), std::istreambuf_iterator<char>()); f.close(); return true; } else { //Fallback to assetManager AAssetManager *assetManager = activity_->assetManager; AAsset *assetFile = AAssetManager_open(assetManager, fileName, AASSET_MODE_BUFFER); if (!assetFile) { return false; } uint8_t *data = (uint8_t *)AAsset_getBuffer(assetFile); int32_t size = AAsset_getLength(assetFile); if (data == NULL) { AAsset_close(assetFile); LOGI("Failed to load:%s", fileName); return false; } buffer_ref->reserve(size); buffer_ref->assign(data, data + size); AAsset_close(assetFile); return true; } }