12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559 |
- /****************************************************************************
- Copyright (c) 2010-2013 cocos2d-x.org
- Copyright (c) 2013-2016 Chukong Technologies Inc.
- Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
- http://www.cocos2d-x.org
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
- ****************************************************************************/
- #include "platform/CCFileUtils.h"
- #include <stack>
- #include "base/CCData.h"
- #include "base/ccMacros.h"
- #include "platform/CCSAXParser.h"
- #include "tinyxml2/tinyxml2.h"
- #include "tinydir/tinydir.h"
- #ifdef MINIZIP_FROM_SYSTEM
- #include <minizip/unzip.h>
- #else // from our embedded sources
- #include "unzip/unzip.h"
- #endif
- #include <sys/stat.h>
- #include <regex>
- NS_CC_BEGIN
- // Implement DictMaker
- #if (CC_TARGET_PLATFORM != CC_PLATFORM_IOS) && (CC_TARGET_PLATFORM != CC_PLATFORM_MAC)
- typedef enum
- {
- SAX_NONE = 0,
- SAX_KEY,
- SAX_DICT,
- SAX_INT,
- SAX_REAL,
- SAX_STRING,
- SAX_ARRAY
- }SAXState;
- typedef enum
- {
- SAX_RESULT_NONE = 0,
- SAX_RESULT_DICT,
- SAX_RESULT_ARRAY
- }SAXResult;
- class DictMaker : public SAXDelegator
- {
- public:
- SAXResult _resultType;
- ValueMap _rootDict;
- ValueVector _rootArray;
- std::string _curKey; ///< parsed key
- std::string _curValue; // parsed value
- SAXState _state;
- ValueMap* _curDict;
- ValueVector* _curArray;
- std::stack<ValueMap*> _dictStack;
- std::stack<ValueVector*> _arrayStack;
- std::stack<SAXState> _stateStack;
- public:
- DictMaker()
- : _resultType(SAX_RESULT_NONE)
- , _state(SAX_NONE)
- {
- }
- ~DictMaker()
- {
- }
- ValueMap dictionaryWithContentsOfFile(const std::string& fileName)
- {
- _resultType = SAX_RESULT_DICT;
- SAXParser parser;
- CCASSERT(parser.init("UTF-8"), "The file format isn't UTF-8");
- parser.setDelegator(this);
- parser.parse(fileName);
- return _rootDict;
- }
- ValueMap dictionaryWithDataOfFile(const char* filedata, int filesize)
- {
- _resultType = SAX_RESULT_DICT;
- SAXParser parser;
- CCASSERT(parser.init("UTF-8"), "The file format isn't UTF-8");
- parser.setDelegator(this);
- parser.parse(filedata, filesize);
- return _rootDict;
- }
- ValueVector arrayWithContentsOfFile(const std::string& fileName)
- {
- _resultType = SAX_RESULT_ARRAY;
- SAXParser parser;
- CCASSERT(parser.init("UTF-8"), "The file format isn't UTF-8");
- parser.setDelegator(this);
- parser.parse(fileName);
- return _rootArray;
- }
- virtual void startElement(void *ctx, const char *name, const char **atts) override
- {
- CC_UNUSED_PARAM(ctx);
- CC_UNUSED_PARAM(atts);
- const std::string sName(name);
- if( sName == "dict" )
- {
- if(_resultType == SAX_RESULT_DICT && _rootDict.empty())
- {
- _curDict = &_rootDict;
- }
- _state = SAX_DICT;
- SAXState preState = SAX_NONE;
- if (! _stateStack.empty())
- {
- preState = _stateStack.top();
- }
- if (SAX_ARRAY == preState)
- {
- // add a new dictionary into the array
- _curArray->push_back(Value(ValueMap()));
- _curDict = &(_curArray->rbegin())->asValueMap();
- }
- else if (SAX_DICT == preState)
- {
- // add a new dictionary into the pre dictionary
- CCASSERT(! _dictStack.empty(), "The state is wrong!");
- ValueMap* preDict = _dictStack.top();
- (*preDict)[_curKey] = Value(ValueMap());
- _curDict = &(*preDict)[_curKey].asValueMap();
- }
- // record the dict state
- _stateStack.push(_state);
- _dictStack.push(_curDict);
- }
- else if(sName == "key")
- {
- _state = SAX_KEY;
- }
- else if(sName == "integer")
- {
- _state = SAX_INT;
- }
- else if(sName == "real")
- {
- _state = SAX_REAL;
- }
- else if(sName == "string")
- {
- _state = SAX_STRING;
- }
- else if (sName == "array")
- {
- _state = SAX_ARRAY;
- if (_resultType == SAX_RESULT_ARRAY && _rootArray.empty())
- {
- _curArray = &_rootArray;
- }
- SAXState preState = SAX_NONE;
- if (! _stateStack.empty())
- {
- preState = _stateStack.top();
- }
- if (preState == SAX_DICT)
- {
- (*_curDict)[_curKey] = Value(ValueVector());
- _curArray = &(*_curDict)[_curKey].asValueVector();
- }
- else if (preState == SAX_ARRAY)
- {
- CCASSERT(! _arrayStack.empty(), "The state is wrong!");
- ValueVector* preArray = _arrayStack.top();
- preArray->push_back(Value(ValueVector()));
- _curArray = &(_curArray->rbegin())->asValueVector();
- }
- // record the array state
- _stateStack.push(_state);
- _arrayStack.push(_curArray);
- }
- else
- {
- _state = SAX_NONE;
- }
- }
- virtual void endElement(void *ctx, const char *name) override
- {
- CC_UNUSED_PARAM(ctx);
- SAXState curState = _stateStack.empty() ? SAX_DICT : _stateStack.top();
- const std::string sName((char*)name);
- if( sName == "dict" )
- {
- _stateStack.pop();
- _dictStack.pop();
- if ( !_dictStack.empty())
- {
- _curDict = _dictStack.top();
- }
- }
- else if (sName == "array")
- {
- _stateStack.pop();
- _arrayStack.pop();
- if (! _arrayStack.empty())
- {
- _curArray = _arrayStack.top();
- }
- }
- else if (sName == "true")
- {
- if (SAX_ARRAY == curState)
- {
- _curArray->push_back(Value(true));
- }
- else if (SAX_DICT == curState)
- {
- (*_curDict)[_curKey] = Value(true);
- }
- }
- else if (sName == "false")
- {
- if (SAX_ARRAY == curState)
- {
- _curArray->push_back(Value(false));
- }
- else if (SAX_DICT == curState)
- {
- (*_curDict)[_curKey] = Value(false);
- }
- }
- else if (sName == "string" || sName == "integer" || sName == "real")
- {
- if (SAX_ARRAY == curState)
- {
- if (sName == "string")
- _curArray->push_back(Value(_curValue));
- else if (sName == "integer")
- _curArray->push_back(Value(atoi(_curValue.c_str())));
- else
- _curArray->push_back(Value(std::atof(_curValue.c_str())));
- }
- else if (SAX_DICT == curState)
- {
- if (sName == "string")
- (*_curDict)[_curKey] = Value(_curValue);
- else if (sName == "integer")
- (*_curDict)[_curKey] = Value(atoi(_curValue.c_str()));
- else
- (*_curDict)[_curKey] = Value(std::atof(_curValue.c_str()));
- }
- _curValue.clear();
- }
- _state = SAX_NONE;
- }
- virtual void textHandler(void *ctx, const char *ch, int len) override
- {
- CC_UNUSED_PARAM(ctx);
- if (_state == SAX_NONE)
- {
- return;
- }
- SAXState curState = _stateStack.empty() ? SAX_DICT : _stateStack.top();
- const std::string text = std::string((char*)ch,len);
- switch(_state)
- {
- case SAX_KEY:
- _curKey = text;
- break;
- case SAX_INT:
- case SAX_REAL:
- case SAX_STRING:
- {
- if (curState == SAX_DICT)
- {
- CCASSERT(!_curKey.empty(), "key not found : <integer/real>");
- }
- _curValue.append(text);
- }
- break;
- default:
- break;
- }
- }
- };
- 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);
- }
- ValueMap FileUtils::getValueMapFromData(const char* filedata, int filesize)
- {
- DictMaker tMaker;
- return tMaker.dictionaryWithDataOfFile(filedata, filesize);
- }
- ValueVector FileUtils::getValueVectorFromFile(const std::string& filename)
- {
- const std::string fullPath = fullPathForFilename(filename);
- DictMaker tMaker;
- return tMaker.arrayWithContentsOfFile(fullPath);
- }
- /*
- * forward statement
- */
- static tinyxml2::XMLElement* generateElementForArray(const ValueVector& array, tinyxml2::XMLDocument *doc);
- static tinyxml2::XMLElement* generateElementForDict(const ValueMap& dict, tinyxml2::XMLDocument *doc);
- /*
- * Use tinyxml2 to write plist files
- */
- bool FileUtils::writeToFile(const ValueMap& dict, const std::string &fullPath)
- {
- return writeValueMapToFile(dict, fullPath);
- }
- bool FileUtils::writeValueMapToFile(const ValueMap& dict, const std::string& fullPath)
- {
- tinyxml2::XMLDocument *doc = new (std::nothrow)tinyxml2::XMLDocument();
- if (nullptr == doc)
- return false;
- tinyxml2::XMLDeclaration *declaration = doc->NewDeclaration("xml version=\"1.0\" encoding=\"UTF-8\"");
- if (nullptr == declaration)
- {
- delete doc;
- return false;
- }
- doc->LinkEndChild(declaration);
- tinyxml2::XMLElement *docType = doc->NewElement("!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\"");
- doc->LinkEndChild(docType);
- tinyxml2::XMLElement *rootEle = doc->NewElement("plist");
- if (nullptr == rootEle)
- {
- delete doc;
- return false;
- }
- rootEle->SetAttribute("version", "1.0");
- doc->LinkEndChild(rootEle);
- tinyxml2::XMLElement *innerDict = generateElementForDict(dict, doc);
- if (nullptr == innerDict)
- {
- delete doc;
- return false;
- }
- rootEle->LinkEndChild(innerDict);
- bool ret = tinyxml2::XML_SUCCESS == doc->SaveFile(getSuitableFOpen(fullPath).c_str());
- delete doc;
- return ret;
- }
- bool FileUtils::writeValueVectorToFile(const ValueVector& vecData, const std::string& fullPath)
- {
- tinyxml2::XMLDocument *doc = new (std::nothrow)tinyxml2::XMLDocument();
- if (nullptr == doc)
- return false;
- tinyxml2::XMLDeclaration *declaration = doc->NewDeclaration("xml version=\"1.0\" encoding=\"UTF-8\"");
- if (nullptr == declaration)
- {
- delete doc;
- return false;
- }
- doc->LinkEndChild(declaration);
- tinyxml2::XMLElement *docType = doc->NewElement("!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\"");
- doc->LinkEndChild(docType);
- tinyxml2::XMLElement *rootEle = doc->NewElement("plist");
- if (nullptr == rootEle)
- {
- delete doc;
- return false;
- }
- rootEle->SetAttribute("version", "1.0");
- doc->LinkEndChild(rootEle);
- tinyxml2::XMLElement *innerDict = generateElementForArray(vecData, doc);
- if (nullptr == innerDict)
- {
- delete doc;
- return false;
- }
- rootEle->LinkEndChild(innerDict);
- bool ret = tinyxml2::XML_SUCCESS == doc->SaveFile(getSuitableFOpen(fullPath).c_str());
- delete doc;
- return ret;
- }
- /*
- * Generate tinyxml2::XMLElement for Object through a tinyxml2::XMLDocument
- */
- static tinyxml2::XMLElement* generateElementForObject(const Value& value, tinyxml2::XMLDocument *doc)
- {
- // object is String
- if (value.getType() == Value::Type::STRING)
- {
- tinyxml2::XMLElement* node = doc->NewElement("string");
- tinyxml2::XMLText* content = doc->NewText(value.asString().c_str());
- node->LinkEndChild(content);
- return node;
- }
- // object is integer
- if (value.getType() == Value::Type::INTEGER)
- {
- tinyxml2::XMLElement* node = doc->NewElement("integer");
- tinyxml2::XMLText* content = doc->NewText(value.asString().c_str());
- node->LinkEndChild(content);
- return node;
- }
- // object is real
- if (value.getType() == Value::Type::FLOAT || value.getType() == Value::Type::DOUBLE)
- {
- tinyxml2::XMLElement* node = doc->NewElement("real");
- tinyxml2::XMLText* content = doc->NewText(value.asString().c_str());
- node->LinkEndChild(content);
- return node;
- }
- //object is bool
- if (value.getType() == Value::Type::BOOLEAN) {
- tinyxml2::XMLElement* node = doc->NewElement(value.asString().c_str());
- return node;
- }
- // object is Array
- if (value.getType() == Value::Type::VECTOR)
- return generateElementForArray(value.asValueVector(), doc);
- // object is Dictionary
- if (value.getType() == Value::Type::MAP)
- return generateElementForDict(value.asValueMap(), doc);
- CCLOG("This type cannot appear in property list");
- return nullptr;
- }
- /*
- * Generate tinyxml2::XMLElement for Dictionary through a tinyxml2::XMLDocument
- */
- static tinyxml2::XMLElement* generateElementForDict(const ValueMap& dict, tinyxml2::XMLDocument *doc)
- {
- tinyxml2::XMLElement* rootNode = doc->NewElement("dict");
- for (const auto &iter : dict)
- {
- tinyxml2::XMLElement* tmpNode = doc->NewElement("key");
- rootNode->LinkEndChild(tmpNode);
- tinyxml2::XMLText* content = doc->NewText(iter.first.c_str());
- tmpNode->LinkEndChild(content);
- tinyxml2::XMLElement *element = generateElementForObject(iter.second, doc);
- if (element)
- rootNode->LinkEndChild(element);
- }
- return rootNode;
- }
- /*
- * Generate tinyxml2::XMLElement for Array through a tinyxml2::XMLDocument
- */
- static tinyxml2::XMLElement* generateElementForArray(const ValueVector& array, tinyxml2::XMLDocument *pDoc)
- {
- tinyxml2::XMLElement* rootNode = pDoc->NewElement("array");
- for(const auto &value : array) {
- tinyxml2::XMLElement *element = generateElementForObject(value, pDoc);
- if (element)
- rootNode->LinkEndChild(element);
- }
- return rootNode;
- }
- #else
- /* The subclass FileUtilsApple should override these two method. */
- ValueMap FileUtils::getValueMapFromFile(const std::string& filename) {return ValueMap();}
- ValueMap FileUtils::getValueMapFromData(const char* filedata, int filesize) {return ValueMap();}
- ValueVector FileUtils::getValueVectorFromFile(const std::string& filename) {return ValueVector();}
- bool FileUtils::writeToFile(const ValueMap& dict, const std::string &fullPath) {return false;}
- #endif /* (CC_TARGET_PLATFORM != CC_PLATFORM_IOS) && (CC_TARGET_PLATFORM != CC_PLATFORM_MAC) */
- // Implement FileUtils
- FileUtils* FileUtils::s_sharedFileUtils = nullptr;
- void FileUtils::destroyInstance()
- {
- CC_SAFE_DELETE(s_sharedFileUtils);
- }
- void FileUtils::setDelegate(FileUtils *delegate)
- {
- if (s_sharedFileUtils)
- delete s_sharedFileUtils;
- s_sharedFileUtils = delegate;
- }
- FileUtils::FileUtils()
- : _writablePath("")
- {
- }
- FileUtils::~FileUtils()
- {
- }
- bool FileUtils::writeStringToFile(const std::string& dataStr, const std::string& fullPath)
- {
- Data data;
- data.fastSet((unsigned char*)dataStr.c_str(), dataStr.size());
- bool rv = writeDataToFile(data, fullPath);
- // need to give up buffer ownership for temp using, or double free will occur
- data.takeBuffer();
- return rv;
- }
- bool FileUtils::writeDataToFile(const Data& data, const std::string& fullPath)
- {
- size_t size = 0;
- const char* mode = "wb";
- CCASSERT(!fullPath.empty() && data.getSize() != 0, "Invalid parameters.");
- auto fileutils = FileUtils::getInstance();
- do
- {
- // Read the file from hardware
- FILE *fp = fopen(fileutils->getSuitableFOpen(fullPath).c_str(), mode);
- CC_BREAK_IF(!fp);
- size = data.getSize();
- fwrite(data.getBytes(), size, 1, fp);
- fclose(fp);
- return true;
- } while (0);
- return false;
- }
- bool FileUtils::init()
- {
- _searchPathArray.push_back(_defaultResRootPath);
- _searchResolutionsOrderArray.push_back("");
- return true;
- }
- void FileUtils::purgeCachedEntries()
- {
- _fullPathCache.clear();
- }
- // 加密数据
- char ENCRYPT_SIGNATURE[] = {0x6a, 0x69, 0x61, 0x6d, 0x69}; // jiami
- ssize_t ENCRYPT_SIGNATURE_LEN = sizeof(ENCRYPT_SIGNATURE);
- std::string ENCRYPT_KEY = "7911ea3ff0ba6ea6d475da6418f254017911ea3ff0ba6ea6d475da6418f25401";
- unsigned long ENCRYPT_KEY_LEN = ENCRYPT_KEY.length();
- // 是否加密过
- bool FileUtils::isEncrypted(const unsigned char *data)
- {
- return memcmp(ENCRYPT_SIGNATURE, data, ENCRYPT_SIGNATURE_LEN) == 0;
- }
- // 解密
- unsigned char * FileUtils::decrypt(const unsigned char * data, ssize_t dataLen, bool isMalloc)
- {
- unsigned char * out;
- if (isMalloc) {
- out = (unsigned char*)malloc(sizeof(unsigned char) * (dataLen-ENCRYPT_SIGNATURE_LEN));
- } else {
- out = const_cast<unsigned char*>(&data[ENCRYPT_SIGNATURE_LEN]);
- }
- int kIndex = 0;
- for (ssize_t i = ENCRYPT_SIGNATURE_LEN; i < dataLen; i++) {
- if (kIndex >= ENCRYPT_KEY_LEN) {
- kIndex = 0;
- }
- ssize_t pos = i - ENCRYPT_SIGNATURE_LEN;
- out[pos] = data[i] ^ ENCRYPT_KEY[kIndex];
- kIndex++;
- }
- return out;
- }
- std::string FileUtils::getStringFromFile(const std::string& filename)
- {
- std::string s;
- getContents(filename, &s);
- // 开始解密逻辑
- unsigned char *original = (unsigned char*)s.c_str();
- if (isEncrypted(original)) {
- size_t originalLen = s.size();
- s = (char *)decrypt(original, originalLen, false);
- }
- // 结束解密逻辑
- return s;
- }
- Data FileUtils::getDataFromFile(const std::string& filename)
- {
- Data d;
- getContents(filename, &d);
- // 开始解密逻辑
- unsigned char *original = d.getBytes();
- if (isEncrypted(original)) {
- size_t originalLen = d.getSize();
- unsigned char * dest = decrypt(original, originalLen, true);
- d.copy(dest, originalLen-ENCRYPT_SIGNATURE_LEN);
- free(dest);
- }
- // 结束解密逻辑
- return d;
- }
- 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;
- #if defined(_MSC_VER)
- auto descriptor = _fileno(fp);
- #else
- auto descriptor = fileno(fp);
- #endif
- struct stat statBuf;
- if (fstat(descriptor, &statBuf) == -1) {
- fclose(fp);
- return Status::ReadFailed;
- }
- size_t size = statBuf.st_size;
- 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;
- }
- unsigned char* FileUtils::getFileData(const std::string& filename, const char* mode, ssize_t *size)
- {
- CCASSERT(!filename.empty() && size != nullptr && mode != nullptr, "Invalid parameters.");
- (void)(mode); // mode is unused, as we do not support text mode any more...
- Data d;
- if (getContents(filename, &d) != Status::OK) {
- *size = 0;
- return nullptr;
- }
- return d.takeBuffer(size);
- }
- unsigned char* FileUtils::getFileDataFromZip(const std::string& zipFilePath, const std::string& filename, ssize_t *size)
- {
- unsigned char * buffer = nullptr;
- unzFile file = nullptr;
- *size = 0;
- do
- {
- CC_BREAK_IF(zipFilePath.empty());
- file = unzOpen(FileUtils::getInstance()->getSuitableFOpen(zipFilePath).c_str());
- CC_BREAK_IF(!file);
- // IDEA: Other platforms should use upstream minizip like mingw-w64
- #ifdef MINIZIP_FROM_SYSTEM
- int ret = unzLocateFile(file, filename.c_str(), NULL);
- #else
- int ret = unzLocateFile(file, filename.c_str(), 1);
- #endif
- CC_BREAK_IF(UNZ_OK != ret);
- char filePathA[260];
- unz_file_info fileInfo;
- ret = unzGetCurrentFileInfo(file, &fileInfo, filePathA, sizeof(filePathA), nullptr, 0, nullptr, 0);
- CC_BREAK_IF(UNZ_OK != ret);
- ret = unzOpenCurrentFile(file);
- CC_BREAK_IF(UNZ_OK != ret);
- buffer = (unsigned char*)malloc(fileInfo.uncompressed_size);
- int CC_UNUSED readedSize = unzReadCurrentFile(file, buffer, static_cast<unsigned>(fileInfo.uncompressed_size));
- CCASSERT(readedSize == 0 || readedSize == (int)fileInfo.uncompressed_size, "the file size is wrong");
- *size = fileInfo.uncompressed_size;
- unzCloseCurrentFile(file);
- } while (0);
- if (file)
- {
- unzClose(file);
- }
- return buffer;
- }
- std::string FileUtils::getNewFilename(const std::string &filename) const
- {
- std::string newFileName;
- // in Lookup Filename dictionary ?
- auto iter = _filenameLookupDict.find(filename);
- if (iter == _filenameLookupDict.end())
- {
- newFileName = filename;
- }
- else
- {
- newFileName = iter->second.asString();
- }
- return newFileName;
- }
- std::string FileUtils::getPathForFilename(const std::string& filename, const std::string& resolutionDirectory, const std::string& searchPath) const
- {
- std::string file = filename;
- std::string file_path = "";
- size_t pos = filename.find_last_of("/");
- if (pos != std::string::npos)
- {
- file_path = filename.substr(0, pos+1);
- file = filename.substr(pos+1);
- }
- // searchPath + file_path + resourceDirectory
- std::string path = searchPath;
- path += file_path;
- path += resolutionDirectory;
- path = getFullPathForDirectoryAndFilename(path, file);
- return path;
- }
- std::string FileUtils::fullPathForFilename(const std::string &filename) const
- {
- if (filename.empty())
- {
- return "";
- }
- if (isAbsolutePath(filename))
- {
- return normalizePath(filename);
- }
- // Already Cached ?
- auto cacheIter = _fullPathCache.find(filename);
- if(cacheIter != _fullPathCache.end())
- {
- return cacheIter->second;
- }
- // Get the new file name.
- const std::string newFilename( getNewFilename(filename) );
- std::string fullpath;
- for (const auto& searchIt : _searchPathArray)
- {
- for (const auto& resolutionIt : _searchResolutionsOrderArray)
- {
- fullpath = this->getPathForFilename(newFilename, resolutionIt, searchIt);
- if (!fullpath.empty())
- {
- // Using the filename passed in as key.
- _fullPathCache.insert(std::make_pair(filename, fullpath));
- return fullpath;
- }
- }
- }
- if(isPopupNotify()){
- CCLOG("fullPathForFilename: No file found at %s. Possible missing file.", filename.c_str());
- }
- // The file wasn't found, return empty string.
- return "";
- }
- std::string FileUtils::fullPathFromRelativeFile(const std::string &filename, const std::string &relativeFile)
- {
- return relativeFile.substr(0, relativeFile.rfind('/')+1) + getNewFilename(filename);
- }
- void FileUtils::setSearchResolutionsOrder(const std::vector<std::string>& searchResolutionsOrder)
- {
- if (_searchResolutionsOrderArray == searchResolutionsOrder)
- {
- return;
- }
- bool existDefault = false;
- _fullPathCache.clear();
- _searchResolutionsOrderArray.clear();
- for(const auto& iter : searchResolutionsOrder)
- {
- std::string resolutionDirectory = iter;
- if (!existDefault && resolutionDirectory == "")
- {
- existDefault = true;
- }
- if (resolutionDirectory.length() > 0 && resolutionDirectory[resolutionDirectory.length()-1] != '/')
- {
- resolutionDirectory += "/";
- }
- _searchResolutionsOrderArray.push_back(resolutionDirectory);
- }
- if (!existDefault)
- {
- _searchResolutionsOrderArray.push_back("");
- }
- }
- void FileUtils::addSearchResolutionsOrder(const std::string &order,const bool front)
- {
- std::string resOrder = order;
- if (!resOrder.empty() && resOrder[resOrder.length()-1] != '/')
- resOrder.append("/");
- if (front) {
- _searchResolutionsOrderArray.insert(_searchResolutionsOrderArray.begin(), resOrder);
- } else {
- _searchResolutionsOrderArray.push_back(resOrder);
- }
- }
- const std::vector<std::string>& FileUtils::getSearchResolutionsOrder() const
- {
- return _searchResolutionsOrderArray;
- }
- const std::vector<std::string>& FileUtils::getSearchPaths() const
- {
- return _searchPathArray;
- }
- const std::vector<std::string>& FileUtils::getOriginalSearchPaths() const
- {
- return _originalSearchPaths;
- }
- void FileUtils::setWritablePath(const std::string& writablePath)
- {
- _writablePath = writablePath;
- }
- const std::string& FileUtils::getDefaultResourceRootPath() const
- {
- return _defaultResRootPath;
- }
- void FileUtils::setDefaultResourceRootPath(const std::string& path)
- {
- if (_defaultResRootPath != path)
- {
- _fullPathCache.clear();
- _defaultResRootPath = path;
- if (!_defaultResRootPath.empty() && _defaultResRootPath[_defaultResRootPath.length()-1] != '/')
- {
- _defaultResRootPath += '/';
- }
- // Updates search paths
- setSearchPaths(_originalSearchPaths);
- }
- }
- void FileUtils::setSearchPaths(const std::vector<std::string>& searchPaths)
- {
- bool existDefaultRootPath = false;
- _originalSearchPaths = searchPaths;
- _fullPathCache.clear();
- _searchPathArray.clear();
- for (const auto& path : _originalSearchPaths)
- {
- std::string prefix;
- std::string fullPath;
- if (!isAbsolutePath(path))
- { // Not an absolute path
- prefix = _defaultResRootPath;
- }
- fullPath = prefix + path;
- if (!path.empty() && path[path.length()-1] != '/')
- {
- fullPath += "/";
- }
- if (!existDefaultRootPath && path == _defaultResRootPath)
- {
- existDefaultRootPath = true;
- }
- _searchPathArray.push_back(fullPath);
- }
- if (!existDefaultRootPath)
- {
- //CCLOG("Default root path doesn't exist, adding it.");
- _searchPathArray.push_back(_defaultResRootPath);
- }
- }
- void FileUtils::addSearchPath(const std::string &searchpath,const bool front)
- {
- std::string prefix;
- if (!isAbsolutePath(searchpath))
- prefix = _defaultResRootPath;
- std::string path = prefix + searchpath;
- if (!path.empty() && path[path.length()-1] != '/')
- {
- path += "/";
- }
- if (front) {
- _originalSearchPaths.insert(_originalSearchPaths.begin(), searchpath);
- _searchPathArray.insert(_searchPathArray.begin(), path);
- } else {
- _originalSearchPaths.push_back(searchpath);
- _searchPathArray.push_back(path);
- }
- }
- void FileUtils::setFilenameLookupDictionary(const ValueMap& filenameLookupDict)
- {
- _fullPathCache.clear();
- _filenameLookupDict = filenameLookupDict;
- }
- void FileUtils::loadFilenameLookupDictionaryFromFile(const std::string &filename)
- {
- const std::string fullPath = fullPathForFilename(filename);
- if (!fullPath.empty())
- {
- ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(fullPath);
- if (!dict.empty())
- {
- ValueMap& metadata = dict["metadata"].asValueMap();
- int version = metadata["version"].asInt();
- if (version != 1)
- {
- CCLOG("ERROR: Invalid filenameLookup dictionary version: %d. Filename: %s", version, filename.c_str());
- return;
- }
- setFilenameLookupDictionary( dict["filenames"].asValueMap());
- }
- }
- }
- std::string FileUtils::getFullPathForDirectoryAndFilename(const std::string& directory, const std::string& filename) const
- {
- // get directory+filename, safely adding '/' as necessary
- std::string ret = directory;
- if (directory.size() && directory[directory.size()-1] != '/'){
- ret += '/';
- }
- ret += filename;
- ret = normalizePath(ret);
- // if the file doesn't exist, return an empty string
- if (!isFileExistInternal(ret)) {
- ret = "";
- }
- return ret;
- }
- bool FileUtils::isFileExist(const std::string& filename) const
- {
- if (isAbsolutePath(filename))
- {
- return isFileExistInternal(normalizePath(filename));
- }
- else
- {
- std::string fullpath = fullPathForFilename(filename);
- if (fullpath.empty())
- return false;
- else
- return true;
- }
- }
- bool FileUtils::isAbsolutePath(const std::string& path) const
- {
- return (path[0] == '/');
- }
- bool FileUtils::isDirectoryExist(const std::string& dirPath) const
- {
- CCASSERT(!dirPath.empty(), "Invalid path");
- if (isAbsolutePath(dirPath))
- {
- return isDirectoryExistInternal(normalizePath(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;
- }
- std::vector<std::string> FileUtils::listFiles(const std::string& dirPath) const
- {
- std::string fullpath = fullPathForFilename(dirPath);
- std::vector<std::string> files;
- if (isDirectoryExist(fullpath))
- {
- tinydir_dir dir;
- #ifdef UNICODE
- unsigned int length = MultiByteToWideChar(CP_UTF8, 0, &fullpath[0], (int)fullpath.size(), NULL, 0);
- if (length != fullpath.size())
- {
- return files;
- }
- std::wstring fullpathstr(length, 0);
- MultiByteToWideChar(CP_UTF8, 0, &fullpath[0], (int)fullpath.size(), &fullpathstr[0], length);
- #else
- std::string fullpathstr = fullpath;
- #endif
- if (tinydir_open(&dir, &fullpathstr[0]) != -1)
- {
- while (dir.has_next)
- {
- tinydir_file file;
- if (tinydir_readfile(&dir, &file) == -1)
- {
- // Error getting file
- break;
- }
- #ifdef UNICODE
- std::wstring path = file.path;
- length = WideCharToMultiByte(CP_UTF8, 0, &path[0], (int)path.size(), NULL, 0, NULL, NULL);
- std::string filepath;
- if (length > 0)
- {
- filepath.resize(length);
- WideCharToMultiByte(CP_UTF8, 0, &path[0], (int)path.size(), &filepath[0], length, NULL, NULL);
- }
- #else
- std::string filepath = file.path;
- #endif
- if (file.is_dir)
- {
- filepath.append("/");
- }
- files.push_back(filepath);
- if (tinydir_next(&dir) == -1)
- {
- // Error getting next file
- break;
- }
- }
- }
- tinydir_close(&dir);
- }
- return files;
- }
- void FileUtils::listFilesRecursively(const std::string& dirPath, std::vector<std::string> *files) const
- {
- std::string fullpath = fullPathForFilename(dirPath);
- if (isDirectoryExist(fullpath))
- {
- tinydir_dir dir;
- #ifdef UNICODE
- unsigned int length = MultiByteToWideChar(CP_UTF8, 0, &fullpath[0], (int)fullpath.size(), NULL, 0);
- if (length != fullpath.size())
- {
- return;
- }
- std::wstring fullpathstr(length, 0);
- MultiByteToWideChar(CP_UTF8, 0, &fullpath[0], (int)fullpath.size(), &fullpathstr[0], length);
- #else
- std::string fullpathstr = fullpath;
- #endif
- if (tinydir_open(&dir, &fullpathstr[0]) != -1)
- {
- while (dir.has_next)
- {
- tinydir_file file;
- if (tinydir_readfile(&dir, &file) == -1)
- {
- // Error getting file
- break;
- }
- #ifdef UNICODE
- std::wstring path = file.path;
- length = WideCharToMultiByte(CP_UTF8, 0, &path[0], (int)path.size(), NULL, 0, NULL, NULL);
- std::string filepath;
- if (length > 0)
- {
- filepath.resize(length);
- WideCharToMultiByte(CP_UTF8, 0, &path[0], (int)path.size(), &filepath[0], length, NULL, NULL);
- }
- #else
- std::string filepath = file.path;
- #endif
- if (file.name[0] != '.')
- {
- if (file.is_dir)
- {
- filepath.append("/");
- files->push_back(filepath);
- listFilesRecursively(filepath, files);
- }
- else
- {
- files->push_back(filepath);
- }
- }
- if (tinydir_next(&dir) == -1)
- {
- // Error getting next file
- break;
- }
- }
- }
- tinydir_close(&dir);
- }
- }
- #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
- // windows os implement should override in platform specific FileUtiles class
- bool FileUtils::isDirectoryExistInternal(const std::string& dirPath) const
- {
- CCASSERT(false, "FileUtils not support isDirectoryExistInternal");
- return false;
- }
- bool FileUtils::createDirectory(const std::string& path)
- {
- CCASSERT(false, "FileUtils not support createDirectory");
- return false;
- }
- bool FileUtils::removeDirectory(const std::string& path)
- {
- CCASSERT(false, "FileUtils not support removeDirectory");
- return false;
- }
- bool FileUtils::removeFile(const std::string &path)
- {
- CCASSERT(false, "FileUtils not support removeFile");
- return false;
- }
- bool FileUtils::renameFile(const std::string &oldfullpath, const std::string& newfullpath)
- {
- CCASSERT(false, "FileUtils not support renameFile");
- return false;
- }
- bool FileUtils::renameFile(const std::string &path, const std::string &oldname, const std::string &name)
- {
- CCASSERT(false, "FileUtils not support renameFile");
- return false;
- }
- std::string FileUtils::getSuitableFOpen(const std::string& filenameUtf8) const
- {
- CCASSERT(false, "getSuitableFOpen should be override by platform FileUtils");
- return filenameUtf8;
- }
- long FileUtils::getFileSize(const std::string &filepath)
- {
- CCASSERT(false, "getFileSize should be override by platform FileUtils");
- return 0;
- }
- #else
- // default implements for unix like os
- #include <sys/types.h>
- #include <errno.h>
- #include <dirent.h>
- // android doesn't have ftw.h
- #if (CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID)
- #include <ftw.h>
- #endif
- bool FileUtils::isDirectoryExistInternal(const std::string& dirPath) const
- {
- struct stat st;
- if (stat(dirPath.c_str(), &st) == 0)
- {
- return S_ISDIR(st.st_mode);
- }
- return false;
- }
- bool FileUtils::createDirectory(const std::string& path)
- {
- CCASSERT(!path.empty(), "Invalid path");
- if (isDirectoryExist(path))
- return true;
- // Split the path
- size_t start = 0;
- size_t found = path.find_first_of("/\\", start);
- std::string subpath;
- std::vector<std::string> dirs;
- if (found != std::string::npos)
- {
- while (true)
- {
- subpath = path.substr(start, found - start + 1);
- if (!subpath.empty())
- dirs.push_back(subpath);
- start = found+1;
- found = path.find_first_of("/\\", start);
- if (found == std::string::npos)
- {
- if (start < path.length())
- {
- dirs.push_back(path.substr(start));
- }
- break;
- }
- }
- }
- DIR *dir = NULL;
- // Create path recursively
- subpath = "";
- for (const auto& iter : dirs)
- {
- subpath += iter;
- dir = opendir(subpath.c_str());
- if (!dir)
- {
- // directory doesn't exist, should create a new one
- int ret = mkdir(subpath.c_str(), S_IRWXU | S_IRWXG | S_IRWXO);
- if (ret != 0 && (errno != EEXIST))
- {
- // current directory can not be created, sub directories can not be created too
- // should return
- return false;
- }
- }
- else
- {
- // directory exists, should close opened dir
- closedir(dir);
- }
- }
- return true;
- }
- namespace
- {
- #if (CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID)
- int unlink_cb(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf)
- {
- int rv = remove(fpath);
- if (rv)
- perror(fpath);
- return rv;
- }
- #endif
- }
- bool FileUtils::removeDirectory(const std::string& path)
- {
- #if !defined(CC_TARGET_OS_TVOS)
- #if (CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID)
- if (nftw(path.c_str(), unlink_cb, 64, FTW_DEPTH | FTW_PHYS) == -1)
- return false;
- else
- return true;
- #else
- std::string command = "rm -r ";
- // Path may include space.
- command += "\"" + path + "\"";
- if (system(command.c_str()) >= 0)
- return true;
- else
- return false;
- #endif // (CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID)
- #else
- return false;
- #endif // !defined(CC_TARGET_OS_TVOS)
- }
- bool FileUtils::removeFile(const std::string &path)
- {
- if (remove(path.c_str())) {
- return false;
- } else {
- return true;
- }
- }
- bool FileUtils::renameFile(const std::string &oldfullpath, const std::string &newfullpath)
- {
- CCASSERT(!oldfullpath.empty(), "Invalid path");
- CCASSERT(!newfullpath.empty(), "Invalid path");
- int errorCode = rename(oldfullpath.c_str(), newfullpath.c_str());
- if (0 != errorCode)
- {
- CCLOGERROR("Fail to rename file %s to %s !Error code is %d", oldfullpath.c_str(), newfullpath.c_str(), errorCode);
- return false;
- }
- return true;
- }
- bool FileUtils::renameFile(const std::string &path, const std::string &oldname, const std::string &name)
- {
- CCASSERT(!path.empty(), "Invalid path");
- std::string oldPath = path + oldname;
- std::string newPath = path + name;
- return this->renameFile(oldPath, newPath);
- }
- std::string FileUtils::getSuitableFOpen(const std::string& filenameUtf8) const
- {
- return filenameUtf8;
- }
- 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);
- }
- }
- #endif
- //////////////////////////////////////////////////////////////////////////
- // Notification support when getFileData from invalid file path.
- //////////////////////////////////////////////////////////////////////////
- /* Default to false, enable it by setPopupNotify if needed */
- static bool s_popupNotify = false;
- void FileUtils::setPopupNotify(bool notify)
- {
- s_popupNotify = notify;
- }
- bool FileUtils::isPopupNotify() const
- {
- return s_popupNotify;
- }
- std::string FileUtils::getFileExtension(const std::string& filePath) const
- {
- std::string fileExtension;
- size_t pos = filePath.find_last_of('.');
- if (pos != std::string::npos)
- {
- fileExtension = filePath.substr(pos, filePath.length());
- std::transform(fileExtension.begin(), fileExtension.end(), fileExtension.begin(), ::tolower);
- }
- return fileExtension;
- }
- void FileUtils::valueMapCompact(ValueMap &valueMap)
- {
- }
- void FileUtils::valueVectorCompact(ValueVector &valueVector)
- {
- }
- std::string FileUtils::getFileDir(const std::string& path) const
- {
- std::string ret;
- size_t pos = path.rfind("/");
- if (pos != std::string::npos)
- {
- ret = path.substr(0, pos);
- }
- normalizePath(ret);
- return ret;
- }
- std::string FileUtils::normalizePath(const std::string& path) const
- {
- std::string ret;
- // Normalize: remove . and ..
- ret = std::regex_replace(path, std::regex("/\\./"), "/");
- ret = std::regex_replace(ret, std::regex("/\\.$"), "");
- size_t pos;
- while ((pos = ret.find("..")) != std::string::npos && pos > 2)
- {
- size_t prevSlash = ret.rfind("/", pos-2);
- if (prevSlash == std::string::npos)
- break;
- ret = ret.replace(prevSlash, pos - prevSlash + 2 , "");
- }
- return ret;
- }
- NS_CC_END
|