void ShaderBrush::loadShaderVertex(const std::string &vert, const std::string &frag) { auto fileUtiles = FileUtils::getInstance(); // frag auto fragmentFilePath = fileUtiles->fullPathForFilename(frag); auto fragSource = fileUtiles->getStringFromFile(fragmentFilePath); // vert std::string vertSource; if (vert.empty()) { //vertSource = ccPositionTextureColor_vert; vertSource = ccPositionTextureColor_noMVP_vert; } else { std::string vertexFilePath = fileUtiles->fullPathForFilename(vert); vertSource = fileUtiles->getStringFromFile(vertexFilePath); } auto glprogram = GLProgram::createWithByteArrays(vertSource.c_str(), fragSource.c_str()); auto glprogramstate = GLProgramState::getOrCreateWithGLProgram(glprogram); // brush _brush = Sprite::create(NAME_BRUSH); _brush->retain(); _blend = { GL_ONE, GL_ZERO }; //_blend = BlendFunc{ GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA }; _brush->setBlendFunc(_blend); _brush->setGLProgramState(glprogramstate); auto s = Director::getInstance()->getVisibleSize(); GLProgramState* pGLPS = _brush->getGLProgramState(); pGLPS->setUniformVec2("resolution", Vec2(s)); }
void TestSearchPath::onEnter() { FileUtilsDemo::onEnter(); auto sharedFileUtils = FileUtils::getInstance(); std::string ret; sharedFileUtils->purgeCachedEntries(); _defaultSearchPathArray = sharedFileUtils->getSearchPaths(); std::vector<std::string> searchPaths = _defaultSearchPathArray; std::string writablePath = sharedFileUtils->getWritablePath(); std::string fileName = writablePath+"external.txt"; char szBuf[100] = "Hello Cocos2d-x!"; FILE* fp = fopen(fileName.c_str(), "wb"); if (fp) { size_t ret = fwrite(szBuf, 1, strlen(szBuf), fp); CCASSERT(ret != 0, "fwrite function returned zero value"); fclose(fp); if (ret != 0) log("Writing file to writable path succeed."); } searchPaths.insert(searchPaths.begin(), writablePath); searchPaths.insert(searchPaths.begin()+1, "Misc/searchpath1"); searchPaths.insert(searchPaths.begin()+2, "Misc/searchpath2"); sharedFileUtils->setSearchPaths(searchPaths); _defaultResolutionsOrderArray = sharedFileUtils->getSearchResolutionsOrder(); std::vector<std::string> resolutionsOrder = _defaultResolutionsOrderArray; resolutionsOrder.insert(resolutionsOrder.begin(), "resources-ipad"); sharedFileUtils->setSearchResolutionsOrder(resolutionsOrder); for( int i=1; i<3; i++) { auto filename = StringUtils::format("file%d.txt", i); ret = sharedFileUtils->fullPathForFilename(filename); log("%s -> %s", filename.c_str(), ret.c_str()); } // Gets external.txt from writable path std::string fullPath = sharedFileUtils->fullPathForFilename("external.txt"); log("external file path = %s", fullPath.c_str()); if (fullPath.length() > 0) { fp = fopen(fullPath.c_str(), "rb"); if (fp) { char szReadBuf[100] = {0}; size_t read = fread(szReadBuf, 1, strlen(szReadBuf), fp); if (read > 0) log("The content of file from writable path: %s", szReadBuf); fclose(fp); } } }
void DrawNode3D::loadShaderVertex(const std::string &vert, const std::string &frag) { auto fileUtiles = FileUtils::getInstance(); // frag auto fragmentFilePath = fileUtiles->fullPathForFilename(frag); auto fragSource = fileUtiles->getStringFromFile(fragmentFilePath); // vert auto vertexFilePath = fileUtiles->fullPathForFilename(vert); auto vertSource = fileUtiles->getStringFromFile(vertexFilePath); auto glprogram = GLProgram::createWithByteArrays(vertSource.c_str(), fragSource.c_str()); auto glprogramstate = GLProgramState::getOrCreateWithGLProgram(glprogram); setGLProgramState(glprogramstate); }
void FileUtils::getStringFromFile(char **ppDst, const std::string& filename) const { std::string ret; char* buffer = nullptr; size_t size = 0; size_t readsize; const char* mode = "rt"; do { if (filename.empty()) break; // Read the file from hardware std::string fullPath = fullPathForFilename(filename); FILE *fp = fopen(fullPath.c_str(), mode); if (!fp) break; fseek(fp,0,SEEK_END); size = ftell(fp); fseek(fp,0,SEEK_SET); buffer = (char*)malloc(sizeof(char) * (size + 1)); buffer[size] = '\0'; readsize = fread(buffer, sizeof(char), size, fp); fclose(fp); if (readsize < size) { buffer[readsize] = '\0'; } *ppDst = buffer; } while (0); }
void AppLang::readLocalizationFile() { if (!_hasInit) { _hasInit = true; auto fileUtils = FileUtils::getInstance(); if (!fileUtils->isFileExist(_localizationFileName)) { cocos2d::log("[WARNING]:not find %s", _localizationFileName.c_str()); return; } auto fullFilePath = fileUtils->fullPathForFilename(_localizationFileName); std::string fileContent = FileUtils::getInstance()->getStringFromFile(fullFilePath); if(fileContent.empty()) return; if (_docRootjson.Parse<0>(fileContent.c_str()).HasParseError()) { cocos2d::log("[WARNING]:read json file %s failed because of %d", _localizationFileName.c_str(), _docRootjson.GetParseError()); return; } } }
Node* CCBReader::readNodeGraphFromFile(const char *pCCBFileName, Ref *pOwner, const Size &parentSize) { if (nullptr == pCCBFileName || strlen(pCCBFileName) == 0) { return nullptr; } std::string strCCBFileName(pCCBFileName); std::string strSuffix(".ccbi"); // Add ccbi suffix if (!CCBReader::endsWith(strCCBFileName.c_str(), strSuffix.c_str())) { strCCBFileName += strSuffix; } auto fileUtils = FileUtils::getInstance(); std::string strPath = fileUtils->fullPathForFilename(strCCBFileName); if (strPath.empty()) { return nullptr; } auto dataPtr = std::make_shared<Data>(fileUtils->getDataFromFile(strPath)); if (dataPtr->isNull()) { return nullptr; } Node *ret = this->readNodeGraphFromData(dataPtr, pOwner, parentSize); return ret; }
void TestResolutionDirectories::onEnter() { FileUtilsDemo::onEnter(); auto sharedFileUtils = FileUtils::getInstance(); std::string ret; sharedFileUtils->purgeCachedEntries(); _defaultSearchPathArray = sharedFileUtils->getSearchPaths(); std::vector<std::string> searchPaths = _defaultSearchPathArray; searchPaths.insert(searchPaths.begin(), "Misc"); sharedFileUtils->setSearchPaths(searchPaths); _defaultResolutionsOrderArray = sharedFileUtils->getSearchResolutionsOrder(); std::vector<std::string> resolutionsOrder = _defaultResolutionsOrderArray; resolutionsOrder.insert(resolutionsOrder.begin(), "resources-ipadhd"); resolutionsOrder.insert(resolutionsOrder.begin()+1, "resources-ipad"); resolutionsOrder.insert(resolutionsOrder.begin()+2, "resources-widehd"); resolutionsOrder.insert(resolutionsOrder.begin()+3, "resources-wide"); resolutionsOrder.insert(resolutionsOrder.begin()+4, "resources-hd"); resolutionsOrder.insert(resolutionsOrder.begin()+5, "resources-iphone"); sharedFileUtils->setSearchResolutionsOrder(resolutionsOrder); for( int i=1; i<7; i++) { auto filename = StringUtils::format("test%d.txt", i); ret = sharedFileUtils->fullPathForFilename(filename); log("%s -> %s", filename.c_str(), ret.c_str()); } }
unsigned char* FileUtils::getFileData(const std::string& filename, const char* mode, ssize_t *size) { unsigned char * buffer = nullptr; CCASSERT(!filename.empty() && size != nullptr && mode != nullptr, "Invalid parameters."); *size = 0; do { // read the file from hardware const std::string fullPath = fullPathForFilename(filename); FILE *fp = fopen(fullPath.c_str(), mode); CC_BREAK_IF(!fp); fseek(fp,0,SEEK_END); *size = ftell(fp); fseek(fp,0,SEEK_SET); buffer = (unsigned char*)malloc(*size); *size = fread(buffer,sizeof(unsigned char), *size,fp); fclose(fp); } while (0); if (!buffer) { std::string msg = "Get data from file("; msg.append(filename).append(") failed!"); CCLOG("%s", msg.c_str()); } return buffer; }
string MyUtility::getUTF8Char(const string key) { auto sharedFileUtils = FileUtils::getInstance(); std::string fullPathForFilename = sharedFileUtils->fullPathForFilename("utf8_char.plist"); bool isExist = false; isExist = sharedFileUtils->isFileExist(fullPathForFilename); if (!isExist) { log("utf8_char.plist doesn't exist."); return ""; } ValueMap map = sharedFileUtils->getValueMapFromFile(fullPathForFilename); Value value = map[key]; //log("%s - %s",key.c_str(), value.asString().c_str()); if (value.isNull()) { log("%s doesn't exist.",key.c_str()); return ""; } return value.asString(); }
unsigned char* CCFileUtils::getFileData(const char* pszFileName, const char* pszMode, unsigned long * pSize) { unsigned char * pBuffer = NULL; CCAssert(pszFileName != NULL && pSize != NULL && pszMode != NULL, "Invalid parameters."); *pSize = 0; do { // read the file from hardware std::string fullPath = fullPathForFilename(pszFileName); FILE *fp = fopen(fullPath.c_str(), pszMode); CC_BREAK_IF(!fp); fseek(fp,0,SEEK_END); *pSize = ftell(fp); fseek(fp,0,SEEK_SET); pBuffer = new unsigned char[*pSize]; *pSize = fread(pBuffer,sizeof(unsigned char), *pSize,fp); fclose(fp); } while (0); if (! pBuffer) { std::string msg = "Get data from file("; msg.append(pszFileName).append(") failed!"); printf("%s\n", msg.c_str()); } return pBuffer; }
long FileUtils::getFileSize(const std::string &filepath) { CCASSERT(!filepath.empty(), "Invalid path"); std::string fullpath = filepath; if (!isAbsolutePath(filepath)) { fullpath = fullPathForFilename(filepath); if (fullpath.empty()) return 0; } struct stat info; // Get data associated with "crt_stat.c": int result = stat(fullpath.c_str(), &info); // Check if statistics are valid: if (result != 0) { // Failed return -1; } else { return (long)(info.st_size); } }
bool FileUtils::isDirectoryExist(const std::string& dirPath) const { CCASSERT(!dirPath.empty(), "Invalid path"); if (isAbsolutePath(dirPath)) { return isDirectoryExistInternal(dirPath); } // Already Cached ? auto cacheIter = _fullPathCache.find(dirPath); if( cacheIter != _fullPathCache.end() ) { return isDirectoryExistInternal(cacheIter->second); } std::string fullpath; for (const auto& searchIt : _searchPathArray) { for (const auto& resolutionIt : _searchResolutionsOrderArray) { // searchPath + file_path + resourceDirectory fullpath = fullPathForFilename(searchIt + dirPath + resolutionIt); if (isDirectoryExistInternal(fullpath)) { _fullPathCache.insert(std::make_pair(dirPath, fullpath)); return true; } } } return false; }
FileUtils::Status FileUtils::getContents(const std::string& filename, ResizableBuffer* buffer) { if (filename.empty()) return Status::NotExists; auto fs = FileUtils::getInstance(); std::string fullPath = fs->fullPathForFilename(filename); if (fullPath.empty()) return Status::NotExists; FILE *fp = fopen(fs->getSuitableFOpen(fullPath).c_str(), "rb"); if (!fp) return Status::OpenFailed; fseek(fp, 0, SEEK_END); size_t size = ftell(fp); fseek(fp, 0, SEEK_SET); buffer->resize(size); size_t readsize = fread(buffer->buffer(), 1, size, fp); fclose(fp); if (readsize < size) { buffer->resize(readsize); return Status::ReadFailed; } return Status::OK; }
static Data getData(const std::string& filename, bool forString) { if (filename.empty()) { return Data::Null; } Data ret; unsigned char* buffer = nullptr; size_t size = 0; size_t readsize; const char* mode = nullptr; if (forString) mode = "rt"; else mode = "rb"; auto fileutils = FileUtils::getInstance(); do { // Read the file from hardware std::string fullPath = fileutils->fullPathForFilename(filename); FILE *fp = fopen(fileutils->getSuitableFOpen(fullPath).c_str(), mode); CC_BREAK_IF(!fp); fseek(fp,0,SEEK_END); size = ftell(fp); fseek(fp,0,SEEK_SET); if (forString) { buffer = (unsigned char*)malloc(sizeof(unsigned char) * (size + 1)); buffer[size] = '\0'; } else { buffer = (unsigned char*)malloc(sizeof(unsigned char) * size); } readsize = fread(buffer, sizeof(unsigned char), size, fp); fclose(fp); if (forString && readsize < size) { buffer[readsize] = '\0'; } } while (0); if (nullptr == buffer || 0 == readsize) { CCLOG("Get data from file %s failed", filename.c_str()); } else { ret.fastSet(buffer, readsize); } return ret; }
void ScoreProgressStart::setStartSpriteShader(const std::string & shader_path) { auto fileUtiles = cocos2d::FileUtils::getInstance(); std::string fragmentFullPath = fileUtiles->fullPathForFilename(shader_path.c_str()); std::string fragSource = fileUtiles->getStringFromFile(fragmentFullPath); cocos2d::GLProgram* shader_program = cocos2d::GLProgram::createWithByteArrays(cocos2d::ccPositionTextureColor_noMVP_vert, fragSource.c_str()); cocos2d::GLProgramState* program_state = cocos2d::GLProgramState::getOrCreateWithGLProgram(shader_program); this->setGLProgramState(program_state); }
void BattleFieldUI::setGrayShader(Sprite * avatar) {//to turn the pictrue gray auto fileUtiles = FileUtils::getInstance(); auto fragmentFullPath = fileUtiles->fullPathForFilename("shader3D/greyScale.fsh"); auto fragSource = fileUtiles->getStringFromFile(fragmentFullPath); auto glprogram = cocos2d::GLProgram::createWithByteArrays(ccPositionTextureColor_noMVP_vert, fragSource.c_str()); auto glprogramstate = cocos2d::GLProgramState::getOrCreateWithGLProgram(glprogram); avatar->setGLProgramState(glprogramstate); }
void Joseki::Init(char *filenames) { if (m_JosekiSize > 0 && m_JosekiData != nullptr) { for (int i=0; i < m_JosekiSize; i++) { free(m_JosekiData[i]); } free(m_JosekiData); m_JosekiSize = 0; m_JosekiData = nullptr; } // filenamesは、,区切りとする。 // JosekiDataは1エントリー512バイトとする。 // JosekiSizeはファイルの長さ/512となる。 char *filename = filenames; char *nextfile = strchr(filenames, ','); if (nextfile != NULL) { *nextfile = '\0'; // ,を¥0で置き換え. nextfile++; // 次のファイル名の先頭へ. m_Child = new Joseki(); m_Child->Init(nextfile); } else { m_Child = NULL; } #if defined(WINDOWS) FILE *fp = fopen(filename, "rb"); #else // defined(WINDOWS). auto sharedFileUtils = FileUtils::getInstance(); std::string fullPath = sharedFileUtils->fullPathForFilename(filename); FILE *fp = fopen(fullPath.c_str(), "rb"); #endif // defined(WINDOWS). m_JosekiSize = 0; if (fp != NULL) { for (;;) { char buf[512]; if (fread(buf, 1, 512, fp) <= 0) { break; } // JosekiData[JosekiSize]=(unsigned char*)malloc(512); // memcpy(JosekiData[JosekiSize],buf,512); m_JosekiSize++; } if (m_JosekiSize > 0) { m_JosekiData = (unsigned char **)malloc(sizeof(unsigned char *) * m_JosekiSize); fseek(fp, 0, SEEK_SET); for (int i=0; i < m_JosekiSize; i++) { m_JosekiData[i] = (unsigned char*)malloc(512); if (fread(m_JosekiData[i], 1, 512, fp) <= 0) { break; } } } fclose(fp); } }
bool BaseEffect3D::initGLProgramState(const std::string& vertFilename, const std::string& fragFilename) { auto fileUtiles = FileUtils::getInstance(); auto vertexFullPath = fileUtiles->fullPathForFilename(vertFilename); auto fragmentFullPath = fileUtiles->fullPathForFilename(fragFilename); auto vertSource = fileUtiles->getStringFromFile(vertexFullPath); auto fragSource = fileUtiles->getStringFromFile(fragmentFullPath); auto glprogram = GLProgram::createWithByteArrays(vertSource.c_str(), fragSource.c_str()); #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) m_strVertSource = vertSource; m_strFragSource = fragSource; #endif m_pGLprogramstate = GLProgramState::getOrCreateWithGLProgram(glprogram); m_pGLprogramstate->retain(); return m_pGLprogramstate != nullptr; }
void FileUtilsLayer::OnClickMenu1(Ref* pSender) { auto sharedFileUtils = FileUtils::getInstance(); std::string fullPathForFilename = sharedFileUtils->fullPathForFilename("test.txt"); log("fullPathForFilename path = %s", fullPathForFilename.c_str()); bool isExist = false; isExist = sharedFileUtils->isFileExist("test.txt"); log("%s",isExist ? "test.txt exists" : "test.txt doesn't exist"); }
ValueMap FileUtils::getValueMapFromFile(const std::string& filename) { const std::string fullPath = fullPathForFilename(filename); if (fullPath.empty()) { ValueMap ret; return ret; } DictMaker tMaker; return tMaker.dictionaryWithContentsOfFile(fullPath); }
bool Effect::initGLProgramState(const std::string &fragmentFilename) { auto fileUtiles = FileUtils::getInstance(); auto fragmentFullPath = fileUtiles->fullPathForFilename(fragmentFilename); auto fragSource = fileUtiles->getStringFromFile(fragmentFullPath); auto glprogram = GLProgram::createWithByteArrays(ccPositionTextureColor_noMVP_vert, fragSource.c_str()); _glprogramstate = GLProgramState::getOrCreateWithGLProgram(glprogram); _glprogramstate->retain(); return _glprogramstate != nullptr; }
void ShaderNode::loadShaderVertex(const std::string &vert, const std::string &frag) { auto fileUtiles = FileUtils::getInstance(); // frag auto fragmentFilePath = fileUtiles->fullPathForFilename(frag); auto fragSource = fileUtiles->getStringFromFile(fragmentFilePath); // vert std::string vertSource; if (vert.empty()) { vertSource = ccPositionTextureColor_vert; } else { std::string vertexFilePath = fileUtiles->fullPathForFilename(vert); vertSource = fileUtiles->getStringFromFile(vertexFilePath); } auto glprogram = GLProgram::createWithByteArrays(vertSource.c_str(), fragSource.c_str()); auto glprogramstate = GLProgramState::getOrCreateWithGLProgram(glprogram); setGLProgramState(glprogramstate); }
GLProgramState* Mx::getGLProgramState(string fragmentFilename){ auto fileUtiles = FileUtils::getInstance(); auto fragmentFullPath = fileUtiles->fullPathForFilename(fragmentFilename); auto fragSource = fileUtiles->getStringFromFile(fragmentFullPath); auto glprogram = GLProgram::createWithByteArrays(ccPositionTextureColor_noMVP_vert, fragSource.c_str()); //#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) // _fragSource = fragSource; //#endif return GLProgramState::getOrCreateWithGLProgram(glprogram); }
void FileUtilsLayer::OnClickMenu3(Ref* pSender) { auto sharedFileUtils = FileUtils::getInstance(); std::string fullPathForFilename = sharedFileUtils->fullPathForFilename("test.txt"); log("test.txt path = %s", fullPathForFilename.c_str()); Data data = sharedFileUtils->getDataFromFile(fullPathForFilename); //Data构建string std::string content1 = std::string((const char*)data.getBytes(),0,data.getSize()); log("content1 : %s",content1.c_str()); std::string content2 = sharedFileUtils->getStringFromFile(fullPathForFilename); log("content2 : %s",content2.c_str()); }
GLProgram* CustomFilter::loadShader() { const GLchar* vertShader = nullptr; const GLchar* fragShader = nullptr; auto fileUtiles = FileUtils::getInstance(); if (0 == m_vertFile.size()) { vertShader = ccPositionTextureColor_noMVP_vert; } else { auto vertFullPath = fileUtiles->fullPathForFilename(m_vertFile); auto vertSource = fileUtiles->getStringFromFile(vertFullPath); vertShader = vertSource.c_str(); } auto fragmentFullPath = fileUtiles->fullPathForFilename(m_fragFile); auto fragSource = fileUtiles->getStringFromFile(fragmentFullPath); fragShader = fragSource.c_str(); GLProgram* p = GLProgram::createWithByteArrays(vertShader, fragShader); return p; }
bool ShaderEffect::initWithFragmentFile(const char* fragmentFilename) { auto fileUtiles = FileUtils::getInstance(); auto fragmentFullPath = fileUtiles->fullPathForFilename(fragmentFilename); std::string fragSource=""; if (fileUtiles->isFileExist(fragmentFullPath)) { fragSource = fileUtiles->getStringFromFile(fragmentFullPath); } else { fragSource=ccPositionTextureColor_noMVP_frag; } return initWithFragmentSource(fragSource.c_str()); }
bool FileUtils::isFileExist(const std::string& filename) const { if (isAbsolutePath(filename)) { return isFileExistInternal(filename); } else { std::string fullpath = fullPathForFilename(filename); if (fullpath.empty()) return false; else return true; } }
void FileUtils::loadFilenameLookupDictionaryFromFile(const std::string &filename) { const std::string fullPath = fullPathForFilename(filename); if (fullPath.length() > 0) { ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(fullPath); if (!dict.empty()) { ValueMap& metadata = dict["metadata"].asValueMap(); int version = metadata["version"].asInt(); if (version != 1) { CCLOG("cocos2d: ERROR: Invalid filenameLookup dictionary version: %d. Filename: %s", version, filename.c_str()); return; } setFilenameLookupDictionary( dict["filenames"].asValueMap()); } } }
__Array* CSVParser::parse(const char* fileName) { auto sharedFileUtils = FileUtils::getInstance(); string pathKey = sharedFileUtils->fullPathForFilename(fileName); string content = sharedFileUtils->getStringFromFile(pathKey); __String* contentStr = __String::create(content); __Array* rows = contentStr->componentsSeparatedByString("\n"); __Array* ret = __Array::createWithCapacity(rows->count()); Ref *obj = nullptr; CCARRAY_FOREACH(rows, obj) { auto fieldStr = static_cast<__String*>(obj); __Array* fields = fieldStr->componentsSeparatedByString(","); ret->addObject(fields); }
void FileUtilsLayer::OnClickMenu6(Ref* pSender) { auto sharedFileUtils = FileUtils::getInstance(); sharedFileUtils->purgeCachedEntries(); std::vector<std::string> searchPaths = sharedFileUtils->getSearchPaths(); std::string writablePath = sharedFileUtils->getWritablePath(); searchPaths.insert(searchPaths.begin(), "dir1"); searchPaths.insert(searchPaths.begin()+1, writablePath); sharedFileUtils->setSearchPaths(searchPaths); std::string fullPathForFilename = sharedFileUtils->fullPathForFilename("test.txt"); log("test.txt 's fullPathForFilename is : %s",fullPathForFilename.c_str()); Data data = sharedFileUtils->getDataFromFile(fullPathForFilename); std::string content = sharedFileUtils->getStringFromFile(fullPathForFilename); log("File content is : %s",content.c_str()); }