bool do_file_exists(const std::string& fname) { //LOG("do_file_exists(): " << fname); std::ifstream file(fname.c_str(), std::ios_base::binary); if(file.rdstate() == 0) { file.close(); //LOG("do_file_exists(): exists(1)"); return true; } //LOG("do_file_exists(): check assets1"); AAssetManager* assetManager = GetJavaAssetManager(); //LOG("do_file_exists(): check assets2"); AAsset* asset; if(fname[0] == '.' && fname[1] == '/') { asset = AAssetManager_open(assetManager, fname.substr(2).c_str(), AASSET_MODE_UNKNOWN); } else { asset = AAssetManager_open(assetManager, fname.c_str(), AASSET_MODE_UNKNOWN); } if(asset) { AAsset_close(asset); //LOG("do_file_exists(): exists(2)"); return true; } //LOG("do_file_exists(): fail"); return false; }
std::string read_file(const std::string& fname) { AAssetManager* assetManager = GetJavaAssetManager(); AAsset* asset; std::string name(fname); if(name[0] == '.' && name[1] == '/') { name = name.substr(2); } asset = AAssetManager_open(assetManager, name.c_str(), AASSET_MODE_RANDOM); if(asset != 0) { int len = AAsset_getLength(asset); std::vector<char> v; v.resize(len); if(AAsset_read(asset,&v[0],len)>0) { std::string s(v.begin(),v.end()); AAsset_close(asset); return s; } AAsset_close(asset); return 0; } // Couldn't find the file as an asset, try the standard filesystem std::string filename = find_file(fname); std::ifstream file(filename.c_str(),std::ios_base::binary); std::stringstream ss; ss << file.rdbuf(); return ss.str(); }
void print_assets() { AAssetManager* assetManager = GetJavaAssetManager(); AAssetDir* assetDir = AAssetManager_openDir(assetManager, "images"); const char* filename; __android_log_print(ANDROID_LOG_INFO,"Frogatto","print_assets()"); while((filename = AAssetDir_getNextFileName(assetDir)) != 0) { __android_log_print(ANDROID_LOG_INFO,"Frogatto","File: %s",filename); } AAssetDir_close(assetDir); }
void get_files_in_dir(const std::string& sdirectory, std::vector<std::string>* files, std::vector<std::string>* dirs, FILE_NAME_MODE mode) { AAssetManager* assetManager = GetJavaAssetManager(); AAssetDir* assetDir; std::string directory(sdirectory); int len = directory.length()-1; if(directory[len] == '/') { directory = directory.substr(0,len); } if(directory[0] == '.' && directory[1] == '/') { directory = directory.substr(2); } //LOG("get_files_in_dir() : " << directory << " : " << sdirectory); assetDir = AAssetManager_openDir(assetManager, directory.c_str()); const char* filename; bool read_dirs_txt = false; while((filename = AAssetDir_getNextFileName(assetDir)) != 0) { // __android_log_print(ANDROID_LOG_INFO,"Frogatto:get_files_in_dir","File: %s",filename); //LOG("get_files_in_dir() : " << filename); if(filename[0] != '.') { std::string s(filename); if(dirs != 0 && s.compare("dirs.txt") == 0) { read_dirs_txt = true; } else { if(files) { files->push_back(s); } } } } AAssetDir_close(assetDir); //LOG("get_files_in_dir() : after close"); if(read_dirs_txt) { //LOG("get_files_in_dir() : read_files : " << directory << "/dirs.txt"); std::string dir_list = read_file(directory + "/dirs.txt"); while(dir_list[dir_list.length()-1] == '\n') { dir_list = dir_list.substr(0,dir_list.length()-1); } boost::split(*dirs, dir_list, std::bind2nd(std::equal_to<char>(), '\n')); //LOG("get_files_in_dir() : after split"); } if(files != NULL) std::sort(files->begin(),files->end()); if (dirs != NULL) std::sort(dirs->begin(),dirs->end()); //LOG("get_files_in_dir : after sorts"); }
SDL_RWops* read_sdl_rw_from_asset(const std::string& name) { AAssetManager* assetManager = GetJavaAssetManager(); AAsset* asset; if(name[0] == '.' && name[1] == '/') { asset = AAssetManager_open(assetManager, name.substr(2).c_str(), AASSET_MODE_RANDOM); } else { asset = AAssetManager_open(assetManager, name.c_str(), AASSET_MODE_RANDOM); } if(!asset) { return 0; } SDL_RWops* ops = SDL_AllocRW(); if(!ops) { AAsset_close(asset); return 0; } ops->hidden.unknown.data1 = asset; ops->read = aa_rw_read; ops->write = NULL; ops->seek = aa_rw_seek; ops->close = aa_rw_close; return ops; }