2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010-2013 cocos2d-x.org
|
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2022-04-26 16:20:11 +08:00
|
|
|
Copyright (c) 2021-2022 Bytedance Inc.
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-01-04 12:36:20 +08:00
|
|
|
https://adxeproject.github.io/
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
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>
|
2021-04-22 22:01:32 +08:00
|
|
|
#include <sstream>
|
2021-12-31 12:12:40 +08:00
|
|
|
#include <algorithm>
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
#include "base/CCData.h"
|
|
|
|
#include "base/ccMacros.h"
|
|
|
|
#include "base/CCDirector.h"
|
|
|
|
#include "platform/CCSAXParser.h"
|
|
|
|
//#include "base/ccUtils.h"
|
2021-04-22 22:01:32 +08:00
|
|
|
#include "platform/CCPosixFileStream.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
#ifdef MINIZIP_FROM_SYSTEM
|
2021-12-25 10:04:45 +08:00
|
|
|
# include <minizip/unzip.h>
|
|
|
|
#else // from our embedded sources
|
|
|
|
# include "unzip.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
#endif
|
|
|
|
#include <sys/stat.h>
|
2021-12-30 14:59:57 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
#if defined(_WIN32)
|
|
|
|
# include "ntcvt/ntcvt.hpp"
|
2022-04-26 15:16:02 +08:00
|
|
|
# include "yasio/cxx17/string_view.hpp"
|
2021-12-31 12:12:40 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CC_TARGET_PLATFORM == CC_PLATFORM_IOS
|
|
|
|
# include "tinydir/tinydir.h"
|
|
|
|
#endif
|
|
|
|
|
2020-10-21 12:28:34 +08:00
|
|
|
#include "pugixml/pugixml.hpp"
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-24 23:15:56 +08:00
|
|
|
#define DECLARE_GUARD (void)0
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-05-13 21:06:17 +08:00
|
|
|
#if CC_TARGET_PLATFORM != CC_PLATFORM_IOS && \
|
|
|
|
(CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID || (defined(__NDK_MAJOR__) && __NDK_MAJOR__ >= 22))
|
2021-12-31 12:12:40 +08:00
|
|
|
# define ADXE_HAVE_STDFS 1
|
|
|
|
# include <filesystem>
|
|
|
|
namespace stdfs = std::filesystem;
|
|
|
|
# if defined(_WIN32)
|
|
|
|
inline stdfs::path toFspath(const std::string_view& pathSV)
|
|
|
|
{
|
|
|
|
return stdfs::path{ntcvt::from_chars(pathSV)};
|
|
|
|
}
|
|
|
|
# else
|
|
|
|
inline stdfs::path toFspath(const std::string_view& pathSV)
|
|
|
|
{
|
|
|
|
return stdfs::path{pathSV};
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
#else
|
|
|
|
# include "tinydir/tinydir.h"
|
|
|
|
# define ADXE_HAVE_STDFS 0
|
|
|
|
#endif
|
|
|
|
|
2021-12-30 17:47:48 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Implement DictMaker
|
|
|
|
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
SAX_NONE = 0,
|
|
|
|
SAX_KEY,
|
|
|
|
SAX_DICT,
|
|
|
|
SAX_INT,
|
|
|
|
SAX_REAL,
|
|
|
|
SAX_STRING,
|
|
|
|
SAX_ARRAY
|
2021-12-25 10:04:45 +08:00
|
|
|
} SAXState;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
SAX_RESULT_NONE = 0,
|
|
|
|
SAX_RESULT_DICT,
|
|
|
|
SAX_RESULT_ARRAY
|
2021-12-25 10:04:45 +08:00
|
|
|
} SAXResult;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
class DictMaker : public SAXDelegator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
SAXResult _resultType;
|
|
|
|
ValueMap _rootDict;
|
|
|
|
ValueVector _rootArray;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
std::string _curKey; ///< parsed key
|
|
|
|
std::string _curValue; // parsed value
|
2019-11-23 20:27:39 +08:00
|
|
|
SAXState _state;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
ValueMap* _curDict;
|
2019-11-23 20:27:39 +08:00
|
|
|
ValueVector* _curArray;
|
|
|
|
|
|
|
|
std::stack<ValueMap*> _dictStack;
|
|
|
|
std::stack<ValueVector*> _arrayStack;
|
2021-12-25 10:04:45 +08:00
|
|
|
std::stack<SAXState> _stateStack;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
public:
|
2021-12-25 10:04:45 +08:00
|
|
|
DictMaker() : _resultType(SAX_RESULT_NONE), _state(SAX_NONE), _curDict(nullptr), _curArray(nullptr) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
~DictMaker() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
ValueMap dictionaryWithContentsOfFile(std::string_view fileName)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_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;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
ValueVector arrayWithContentsOfFile(std::string_view fileName)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_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;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void startElement(void* ctx, const char* name, const char** atts) override
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
const std::string sName(name);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (sName == "dict")
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_resultType == SAX_RESULT_DICT && _rootDict.empty())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_curDict = &_rootDict;
|
|
|
|
}
|
|
|
|
|
|
|
|
_state = SAX_DICT;
|
|
|
|
|
|
|
|
SAXState preState = SAX_NONE;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!_stateStack.empty())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
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
|
2021-12-25 10:04:45 +08:00
|
|
|
CCASSERT(!_dictStack.empty(), "The state is wrong!");
|
2021-12-31 12:12:40 +08:00
|
|
|
ValueMap* preDict = _dictStack.top();
|
|
|
|
auto& curVal = hlookup::set_item(*preDict, _curKey, Value(ValueMap()))->second;
|
|
|
|
_curDict = &curVal.asValueMap();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// record the dict state
|
|
|
|
_stateStack.push(_state);
|
|
|
|
_dictStack.push(_curDict);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (sName == "key")
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_state = SAX_KEY;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (sName == "integer")
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_state = SAX_INT;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (sName == "real")
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_state = SAX_REAL;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (sName == "string")
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_state = SAX_STRING;
|
|
|
|
}
|
|
|
|
else if (sName == "array")
|
|
|
|
{
|
|
|
|
_state = SAX_ARRAY;
|
|
|
|
|
|
|
|
if (_resultType == SAX_RESULT_ARRAY && _rootArray.empty())
|
|
|
|
{
|
|
|
|
_curArray = &_rootArray;
|
|
|
|
}
|
|
|
|
SAXState preState = SAX_NONE;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!_stateStack.empty())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
preState = _stateStack.top();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (preState == SAX_DICT)
|
|
|
|
{
|
|
|
|
(*_curDict)[_curKey] = Value(ValueVector());
|
2021-12-25 10:04:45 +08:00
|
|
|
_curArray = &(*_curDict)[_curKey].asValueVector();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
else if (preState == SAX_ARRAY)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
CCASSERT(!_arrayStack.empty(), "The state is wrong!");
|
2019-11-23 20:27:39 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void endElement(void* ctx, const char* name) override
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
SAXState curState = _stateStack.empty() ? SAX_DICT : _stateStack.top();
|
|
|
|
const std::string sName((char*)name);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (sName == "dict")
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_stateStack.pop();
|
|
|
|
_dictStack.pop();
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!_dictStack.empty())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_curDict = _dictStack.top();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (sName == "array")
|
|
|
|
{
|
|
|
|
_stateStack.pop();
|
|
|
|
_arrayStack.pop();
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!_arrayStack.empty())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_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;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void textHandler(void* ctx, const char* ch, size_t len) override
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (_state == SAX_NONE)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
SAXState curState = _stateStack.empty() ? SAX_DICT : _stateStack.top();
|
|
|
|
const std::string text = std::string((char*)ch, len);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
switch (_state)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
case SAX_KEY:
|
|
|
|
_curKey = text;
|
|
|
|
break;
|
|
|
|
case SAX_INT:
|
|
|
|
case SAX_REAL:
|
|
|
|
case SAX_STRING:
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
|
|
|
if (curState == SAX_DICT)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
CCASSERT(!_curKey.empty(), "key not found : <integer/real>");
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
_curValue.append(text);
|
|
|
|
}
|
|
|
|
break;
|
2019-11-23 20:27:39 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
ValueMap FileUtils::getValueMapFromFile(std::string_view filename) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
const std::string fullPath = fullPathForFilename(filename);
|
|
|
|
DictMaker tMaker;
|
|
|
|
return tMaker.dictionaryWithContentsOfFile(fullPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
ValueMap FileUtils::getValueMapFromData(const char* filedata, int filesize) const
|
|
|
|
{
|
|
|
|
DictMaker tMaker;
|
|
|
|
return tMaker.dictionaryWithDataOfFile(filedata, filesize);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
ValueVector FileUtils::getValueVectorFromFile(std::string_view filename) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
const std::string fullPath = fullPathForFilename(filename);
|
|
|
|
DictMaker tMaker;
|
|
|
|
return tMaker.arrayWithContentsOfFile(fullPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* forward statement
|
|
|
|
*/
|
2019-11-26 01:36:20 +08:00
|
|
|
static void generateElementForArray(const ValueVector& array, pugi::xml_node& parent);
|
2019-11-24 23:15:56 +08:00
|
|
|
static void generateElementForDict(const ValueMap& dict, pugi::xml_node& innerDict);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/*
|
2019-11-24 23:15:56 +08:00
|
|
|
* Use pugixml to write plist files
|
2019-11-23 20:27:39 +08:00
|
|
|
*/
|
2021-12-31 12:12:40 +08:00
|
|
|
bool FileUtils::writeToFile(const ValueMap& dict, std::string_view fullPath) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return writeValueMapToFile(dict, fullPath);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
bool FileUtils::writeValueMapToFile(const ValueMap& dict, std::string_view fullPath) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2019-11-24 23:15:56 +08:00
|
|
|
pugi::xml_document doc;
|
2021-12-25 10:04:45 +08:00
|
|
|
doc.load_string(R"(<?xml version="1.0" encoding="UTF-8" ?>
|
2019-11-24 23:15:56 +08:00
|
|
|
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
2021-12-25 10:04:45 +08:00
|
|
|
<plist />)",
|
|
|
|
pugi::parse_full);
|
|
|
|
|
|
|
|
auto rootEle = doc.document_element();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2019-11-26 01:36:20 +08:00
|
|
|
generateElementForDict(dict, rootEle);
|
2021-04-22 19:49:43 +08:00
|
|
|
std::stringstream ss;
|
|
|
|
doc.save(ss, " ");
|
|
|
|
return writeStringToFile(ss.str(), fullPath);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
bool FileUtils::writeValueVectorToFile(const ValueVector& vecData, std::string_view fullPath) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
pugi::xml_document doc;
|
|
|
|
doc.load_string(R"(<?xml version="1.0" encoding="UTF-8" ?>
|
2019-11-24 23:15:56 +08:00
|
|
|
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
2021-12-25 10:04:45 +08:00
|
|
|
<plist />)",
|
|
|
|
pugi::parse_full);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
auto rootEle = doc.document_element();
|
2019-11-26 01:36:20 +08:00
|
|
|
generateElementForArray(vecData, rootEle);
|
2021-04-22 19:49:43 +08:00
|
|
|
std::stringstream ss;
|
|
|
|
doc.save(ss, " ");
|
|
|
|
return writeStringToFile(ss.str(), fullPath);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2019-11-24 23:15:56 +08:00
|
|
|
static void generateElementForObject(const Value& value, pugi::xml_node& parent)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
// object is String
|
|
|
|
if (value.getType() == Value::Type::STRING)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto node = parent.append_child("string");
|
|
|
|
node.append_child(pugi::xml_node_type::node_pcdata).set_value(value.asString().c_str());
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
// object is integer
|
2019-11-24 23:15:56 +08:00
|
|
|
else if (value.getType() == Value::Type::INTEGER)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto node = parent.append_child("integer");
|
|
|
|
node.append_child(pugi::xml_node_type::node_pcdata).set_value(value.asString().c_str());
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
// object is real
|
2019-11-24 23:15:56 +08:00
|
|
|
else if (value.getType() == Value::Type::FLOAT || value.getType() == Value::Type::DOUBLE)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto node = parent.append_child("real");
|
|
|
|
node.append_child(pugi::xml_node_type::node_pcdata).set_value(value.asString().c_str());
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
// object is bool
|
|
|
|
else if (value.getType() == Value::Type::BOOLEAN)
|
|
|
|
{
|
|
|
|
parent.append_child(value.asString().c_str());
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
// object is Array
|
2019-11-24 23:15:56 +08:00
|
|
|
else if (value.getType() == Value::Type::VECTOR)
|
|
|
|
generateElementForArray(value.asValueVector(), parent);
|
2019-11-23 20:27:39 +08:00
|
|
|
// object is Dictionary
|
2019-11-24 23:15:56 +08:00
|
|
|
else if (value.getType() == Value::Type::MAP)
|
|
|
|
generateElementForDict(value.asValueMap(), parent);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2019-11-26 01:36:20 +08:00
|
|
|
static void generateElementForDict(const ValueMap& dict, pugi::xml_node& parent)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2019-11-26 01:36:20 +08:00
|
|
|
auto dictDS = parent.append_child("dict");
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const auto& iter : dict)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto key = dictDS.append_child("key");
|
|
|
|
key.append_child(pugi::xml_node_type::node_pcdata).set_value(iter.first.c_str());
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-26 01:36:20 +08:00
|
|
|
generateElementForObject(iter.second, dictDS);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-26 01:36:20 +08:00
|
|
|
static void generateElementForArray(const ValueVector& array, pugi::xml_node& parent)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2019-11-26 01:36:20 +08:00
|
|
|
auto arrayDS = parent.append_child("array");
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const auto& value : array)
|
|
|
|
{
|
2019-11-26 01:36:20 +08:00
|
|
|
generateElementForObject(value, arrayDS);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implement FileUtils
|
|
|
|
FileUtils* FileUtils::s_sharedFileUtils = nullptr;
|
|
|
|
|
|
|
|
void FileUtils::destroyInstance()
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(s_sharedFileUtils);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void FileUtils::setDelegate(FileUtils* delegate)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (s_sharedFileUtils)
|
|
|
|
delete s_sharedFileUtils;
|
|
|
|
|
|
|
|
s_sharedFileUtils = delegate;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
FileUtils::FileUtils() : _writablePath("") {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
FileUtils::~FileUtils() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
bool FileUtils::writeStringToFile(std::string_view dataStr, std::string_view fullPath) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
return FileUtils::writeBinaryToFile(dataStr.data(), dataStr.size(), fullPath);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void FileUtils::writeStringToFile(std::string dataStr,
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string_view fullPath,
|
2021-12-25 10:04:45 +08:00
|
|
|
std::function<void(bool)> callback) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
performOperationOffthread(
|
2021-12-31 12:12:40 +08:00
|
|
|
[path = std::string{fullPath}](std::string_view dataStrIn) -> bool {
|
|
|
|
return FileUtils::getInstance()->writeStringToFile(dataStrIn, path);
|
2021-12-25 10:04:45 +08:00
|
|
|
},
|
|
|
|
std::move(callback), std::move(dataStr));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
bool FileUtils::writeDataToFile(const Data& data, std::string_view fullPath) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2020-06-14 12:50:20 +08:00
|
|
|
return FileUtils::writeBinaryToFile(data.getBytes(), data.getSize(), fullPath);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void FileUtils::writeDataToFile(Data data, std::string_view fullPath, std::function<void(bool)> callback) const
|
2020-06-14 12:50:20 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
performOperationOffthread(
|
2021-12-31 12:12:40 +08:00
|
|
|
[path = std::string{fullPath}](const Data& dataIn) -> bool {
|
|
|
|
return FileUtils::getInstance()->writeDataToFile(dataIn, path);
|
|
|
|
},
|
2021-12-25 10:04:45 +08:00
|
|
|
std::move(callback), std::move(data));
|
2020-06-14 12:50:20 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
bool FileUtils::writeBinaryToFile(const void* data, size_t dataSize, std::string_view fullPath)
|
2020-06-14 12:50:20 +08:00
|
|
|
{
|
|
|
|
CCASSERT(!fullPath.empty() && dataSize > 0, "Invalid parameters.");
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-05-05 19:49:30 +08:00
|
|
|
auto* fileUtils = FileUtils::getInstance();
|
2019-11-23 20:27:39 +08:00
|
|
|
do
|
|
|
|
{
|
2021-05-05 19:49:30 +08:00
|
|
|
auto fileStream = fileUtils->openFileStream(fullPath, FileStream::Mode::WRITE);
|
2019-11-23 20:27:39 +08:00
|
|
|
// Read the file from hardware
|
2021-05-05 19:49:30 +08:00
|
|
|
CC_BREAK_IF(!fileStream);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-10-09 13:48:56 +08:00
|
|
|
fileStream->write(data, static_cast<unsigned int>(dataSize));
|
2019-11-23 20:27:39 +08:00
|
|
|
return true;
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileUtils::init()
|
|
|
|
{
|
|
|
|
DECLARE_GUARD;
|
|
|
|
_searchPathArray.push_back(_defaultResRootPath);
|
2019-11-24 23:15:56 +08:00
|
|
|
_searchResolutionsOrderArray.emplace_back("");
|
2019-11-23 20:27:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileUtils::purgeCachedEntries()
|
|
|
|
{
|
|
|
|
DECLARE_GUARD;
|
|
|
|
_fullPathCache.clear();
|
|
|
|
_fullPathCacheDir.clear();
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string FileUtils::getStringFromFile(std::string_view filename) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
std::string s;
|
|
|
|
getContents(filename, &s);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void FileUtils::getStringFromFile(std::string_view path, std::function<void(std::string)> callback) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
// Get the full path on the main thread, to avoid the issue that FileUtil's is not
|
|
|
|
// thread safe, and accessing the fullPath cache and searching the search paths is not thread safe
|
|
|
|
auto fullPath = fullPathForFilename(path);
|
2021-12-25 10:04:45 +08:00
|
|
|
performOperationOffthread(
|
2021-12-31 12:12:40 +08:00
|
|
|
[path = std::string{fullPath}]() -> std::string { return FileUtils::getInstance()->getStringFromFile(path); },
|
2021-12-25 10:04:45 +08:00
|
|
|
std::move(callback));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
Data FileUtils::getDataFromFile(std::string_view filename) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
Data d;
|
|
|
|
getContents(filename, &d);
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void FileUtils::getDataFromFile(std::string_view filename, std::function<void(Data)> callback) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto fullPath = fullPathForFilename(filename);
|
2021-12-31 12:12:40 +08:00
|
|
|
performOperationOffthread(
|
|
|
|
[path = std::string{fullPath}]() -> Data { return FileUtils::getInstance()->getDataFromFile(path); },
|
|
|
|
std::move(callback));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
FileUtils::Status FileUtils::getContents(std::string_view filename, ResizableBuffer* buffer) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (filename.empty())
|
|
|
|
return Status::NotExists;
|
|
|
|
|
2021-05-05 19:49:30 +08:00
|
|
|
auto fileUtils = FileUtils::getInstance();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-05-05 19:49:30 +08:00
|
|
|
const auto fullPath = fileUtils->fullPathForFilename(filename);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-05-05 19:49:30 +08:00
|
|
|
auto fileStream = fileUtils->openFileStream(fullPath, FileStream::Mode::READ);
|
|
|
|
if (!fileStream)
|
2020-08-05 13:10:37 +08:00
|
|
|
return Status::OpenFailed;
|
|
|
|
|
2021-05-31 15:08:44 +08:00
|
|
|
const auto size = fileStream->size();
|
2021-05-05 19:49:30 +08:00
|
|
|
if (size < 0)
|
|
|
|
{
|
|
|
|
return Status::ObtainSizeFailed;
|
|
|
|
}
|
2021-04-26 08:29:29 +08:00
|
|
|
|
2021-05-05 19:49:30 +08:00
|
|
|
if (size > ULONG_MAX)
|
|
|
|
{
|
|
|
|
return Status::TooLarge;
|
|
|
|
}
|
2021-04-25 16:36:20 +08:00
|
|
|
|
2021-05-31 15:08:44 +08:00
|
|
|
buffer->resize((size_t)size);
|
2021-04-25 23:05:06 +08:00
|
|
|
|
2021-05-05 19:49:30 +08:00
|
|
|
fileStream->seek(0, SEEK_SET);
|
|
|
|
|
2021-05-31 15:08:44 +08:00
|
|
|
const auto sizeRead = fileStream->read(buffer->buffer(), (unsigned)size);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (sizeRead < size)
|
|
|
|
{
|
2021-05-05 19:49:30 +08:00
|
|
|
buffer->resize(sizeRead);
|
2019-11-23 20:27:39 +08:00
|
|
|
return Status::ReadFailed;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void FileUtils::writeValueMapToFile(ValueMap dict, std::string_view fullPath, std::function<void(bool)> callback) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
performOperationOffthread(
|
2021-12-31 12:12:40 +08:00
|
|
|
[path = std::string{fullPath}](const ValueMap& dictIn) -> bool {
|
|
|
|
return FileUtils::getInstance()->writeValueMapToFile(dictIn, path);
|
2021-12-25 10:04:45 +08:00
|
|
|
},
|
|
|
|
std::move(callback), std::move(dict));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void FileUtils::writeValueVectorToFile(ValueVector vecData,
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string_view fullPath,
|
2021-12-25 10:04:45 +08:00
|
|
|
std::function<void(bool)> callback) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
performOperationOffthread(
|
2021-12-31 12:12:40 +08:00
|
|
|
[path = std::string{fullPath}](const ValueVector& vecDataIn) -> bool {
|
|
|
|
return FileUtils::getInstance()->writeValueVectorToFile(vecDataIn, path);
|
2021-12-25 10:04:45 +08:00
|
|
|
},
|
|
|
|
std::move(callback), std::move(vecData));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string FileUtils::getNewFilename(std::string_view filename) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
std::string newFileName;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
DECLARE_GUARD;
|
|
|
|
|
|
|
|
// in Lookup Filename dictionary ?
|
|
|
|
auto iter = _filenameLookupDict.find(filename);
|
|
|
|
|
|
|
|
if (iter == _filenameLookupDict.end())
|
|
|
|
{
|
|
|
|
newFileName = filename;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newFileName = iter->second.asString();
|
|
|
|
}
|
|
|
|
return newFileName;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string FileUtils::getPathForFilename(std::string_view filename,
|
|
|
|
std::string_view resolutionDirectory,
|
|
|
|
std::string_view searchPath) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
auto file = filename;
|
|
|
|
std::string_view file_path = hlookup::empty_sv;
|
|
|
|
size_t pos = filename.find_last_of('/');
|
2019-11-23 20:27:39 +08:00
|
|
|
if (pos != std::string::npos)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
file_path = filename.substr(0, pos + 1);
|
|
|
|
file = filename.substr(pos + 1);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// searchPath + file_path + resourceDirectory
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string path{searchPath};
|
2019-11-23 20:27:39 +08:00
|
|
|
path += file_path;
|
|
|
|
path += resolutionDirectory;
|
|
|
|
|
|
|
|
path = getFullPathForFilenameWithinDirectory(path, file);
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string FileUtils::getPathForDirectory(std::string_view dir,
|
|
|
|
std::string_view resolutionDiretory,
|
|
|
|
std::string_view searchPath) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
return std::string{searchPath}.append(resolutionDiretory).append(dir);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string FileUtils::fullPathForFilename(std::string_view filename) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
DECLARE_GUARD;
|
|
|
|
|
|
|
|
if (filename.empty())
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2020-08-18 14:29:09 +08:00
|
|
|
/*
|
2021-12-25 10:04:45 +08:00
|
|
|
* As we know, this function 'fullPathForFilename' will be calling when load any file, how to ensure this thread
|
|
|
|
* safe Step: a. call this function at main-thread always b. use the really fullPath to call
|
|
|
|
* getStringFromFile/getDataFromFile at sub-thread c. then this function will call again with really fullPath d.
|
|
|
|
* then isAbsolutePath avoid to access _fullPathCache _fullPathCache concurrent
|
|
|
|
*/
|
2019-11-23 20:27:39 +08:00
|
|
|
if (isAbsolutePath(filename))
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
return std::string{filename};
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Already Cached ?
|
|
|
|
auto cacheIter = _fullPathCache.find(filename);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (cacheIter != _fullPathCache.end())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return cacheIter->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the new file name.
|
2021-12-25 10:04:45 +08:00
|
|
|
const std::string newFilename(getNewFilename(filename));
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
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.emplace(filename, fullpath);
|
|
|
|
return fullpath;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (isPopupNotify())
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
CCLOG("cocos2d: fullPathForFilename: No file found at %s. Possible missing file.", filename.data());
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// The file wasn't found, return empty string.
|
2021-12-31 12:12:40 +08:00
|
|
|
return std::string{};
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string FileUtils::fullPathForDirectory(std::string_view dir) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
DECLARE_GUARD;
|
|
|
|
|
|
|
|
if (dir.empty())
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isAbsolutePath(dir))
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
return std::string{dir};
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Already Cached ?
|
|
|
|
auto cacheIter = _fullPathCacheDir.find(dir);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (cacheIter != _fullPathCacheDir.end())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return cacheIter->second;
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string longdir{dir};
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string fullpath;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (longdir[longdir.length() - 1] != '/')
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
longdir += "/";
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
const std::string newdirname(getNewFilename(longdir));
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
for (const auto& searchIt : _searchPathArray)
|
|
|
|
{
|
|
|
|
for (const auto& resolutionIt : _searchResolutionsOrderArray)
|
|
|
|
{
|
|
|
|
fullpath = this->getPathForDirectory(newdirname, resolutionIt, searchIt);
|
|
|
|
if (!fullpath.empty() && isDirectoryExistInternal(fullpath))
|
|
|
|
{
|
|
|
|
// Using the filename passed in as key.
|
|
|
|
_fullPathCacheDir.emplace(dir, fullpath);
|
|
|
|
return fullpath;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (isPopupNotify())
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
CCLOG("cocos2d: fullPathForDirectory: No directory found at %s. Possible missing directory.", dir.data());
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// The file wasn't found, return empty string.
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string FileUtils::fullPathFromRelativeFile(std::string_view filename, std::string_view relativeFile) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
return std::string{relativeFile.substr(0, relativeFile.rfind('/') + 1)}.append(getNewFilename(filename));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileUtils::setSearchResolutionsOrder(const std::vector<std::string>& searchResolutionsOrder)
|
|
|
|
{
|
|
|
|
DECLARE_GUARD;
|
|
|
|
|
|
|
|
if (_searchResolutionsOrderArray == searchResolutionsOrder)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool existDefault = false;
|
|
|
|
|
|
|
|
_fullPathCache.clear();
|
|
|
|
_fullPathCacheDir.clear();
|
|
|
|
_searchResolutionsOrderArray.clear();
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const auto& iter : searchResolutionsOrder)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
std::string resolutionDirectory = iter;
|
2021-12-31 12:12:40 +08:00
|
|
|
if (!existDefault && resolutionDirectory.empty())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
existDefault = true;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!resolutionDirectory.empty() && resolutionDirectory[resolutionDirectory.length() - 1] != '/')
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
resolutionDirectory += "/";
|
|
|
|
}
|
|
|
|
|
|
|
|
_searchResolutionsOrderArray.push_back(resolutionDirectory);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!existDefault)
|
|
|
|
{
|
|
|
|
_searchResolutionsOrderArray.push_back("");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void FileUtils::addSearchResolutionsOrder(std::string_view order, const bool front)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
DECLARE_GUARD;
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string resOrder{order};
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!resOrder.empty() && resOrder[resOrder.length() - 1] != '/')
|
2019-11-25 17:10:25 +08:00
|
|
|
resOrder.push_back('/');
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (front)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
_searchResolutionsOrderArray.insert(_searchResolutionsOrderArray.begin(), resOrder);
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
_searchResolutionsOrderArray.push_back(resOrder);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::vector<std::string> FileUtils::getSearchResolutionsOrder() const
|
|
|
|
{
|
|
|
|
DECLARE_GUARD;
|
|
|
|
return _searchResolutionsOrderArray;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::vector<std::string> FileUtils::getSearchPaths() const
|
|
|
|
{
|
|
|
|
DECLARE_GUARD;
|
|
|
|
return _searchPathArray;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::vector<std::string> FileUtils::getOriginalSearchPaths() const
|
|
|
|
{
|
|
|
|
DECLARE_GUARD;
|
|
|
|
return _originalSearchPaths;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void FileUtils::setWritablePath(std::string_view writablePath)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
DECLARE_GUARD;
|
|
|
|
_writablePath = writablePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string FileUtils::getDefaultResourceRootPath() const
|
|
|
|
{
|
|
|
|
DECLARE_GUARD;
|
|
|
|
return _defaultResRootPath;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void FileUtils::setDefaultResourceRootPath(std::string_view path)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
DECLARE_GUARD;
|
|
|
|
if (_defaultResRootPath != path)
|
|
|
|
{
|
|
|
|
_fullPathCache.clear();
|
|
|
|
_fullPathCacheDir.clear();
|
|
|
|
_defaultResRootPath = path;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!_defaultResRootPath.empty() && _defaultResRootPath[_defaultResRootPath.length() - 1] != '/')
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_defaultResRootPath += '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Updates search paths
|
|
|
|
setSearchPaths(_originalSearchPaths);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileUtils::setSearchPaths(const std::vector<std::string>& searchPaths)
|
|
|
|
{
|
|
|
|
DECLARE_GUARD;
|
|
|
|
bool existDefaultRootPath = false;
|
2021-12-25 10:04:45 +08:00
|
|
|
_originalSearchPaths = searchPaths;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
_fullPathCache.clear();
|
|
|
|
_fullPathCacheDir.clear();
|
|
|
|
_searchPathArray.clear();
|
|
|
|
|
|
|
|
for (const auto& path : _originalSearchPaths)
|
|
|
|
{
|
|
|
|
std::string prefix;
|
|
|
|
std::string fullPath;
|
|
|
|
|
|
|
|
if (!isAbsolutePath(path))
|
2021-12-25 10:04:45 +08:00
|
|
|
{ // Not an absolute path
|
2019-11-23 20:27:39 +08:00
|
|
|
prefix = _defaultResRootPath;
|
|
|
|
}
|
|
|
|
fullPath = prefix + path;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!path.empty() && path[path.length() - 1] != '/')
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
fullPath += "/";
|
|
|
|
}
|
|
|
|
if (!existDefaultRootPath && path == _defaultResRootPath)
|
|
|
|
{
|
|
|
|
existDefaultRootPath = true;
|
|
|
|
}
|
|
|
|
_searchPathArray.push_back(fullPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!existDefaultRootPath)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// CCLOG("Default root path doesn't exist, adding it.");
|
2019-11-23 20:27:39 +08:00
|
|
|
_searchPathArray.push_back(_defaultResRootPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void FileUtils::addSearchPath(std::string_view searchpath, const bool front)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
DECLARE_GUARD;
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string path;
|
2019-11-23 20:27:39 +08:00
|
|
|
if (!isAbsolutePath(searchpath))
|
2021-12-31 12:12:40 +08:00
|
|
|
path = _defaultResRootPath;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
path.append(searchpath);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!path.empty() && path[path.length() - 1] != '/')
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
path += "/";
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (front)
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
_originalSearchPaths.insert(_originalSearchPaths.begin(), std::string{searchpath});
|
|
|
|
_searchPathArray.insert(_searchPathArray.begin(), std::move(path));
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
_originalSearchPaths.push_back(std::string{searchpath});
|
|
|
|
_searchPathArray.push_back(std::move(path));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileUtils::setFilenameLookupDictionary(const ValueMap& filenameLookupDict)
|
|
|
|
{
|
|
|
|
DECLARE_GUARD;
|
|
|
|
_fullPathCache.clear();
|
|
|
|
_fullPathCacheDir.clear();
|
|
|
|
_filenameLookupDict = filenameLookupDict;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void FileUtils::loadFilenameLookupDictionaryFromFile(std::string_view filename)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
const std::string fullPath = fullPathForFilename(filename);
|
|
|
|
if (!fullPath.empty())
|
|
|
|
{
|
2022-05-13 21:06:17 +08:00
|
|
|
ValueMap dict = getValueMapFromFile(fullPath);
|
2019-11-23 20:27:39 +08:00
|
|
|
if (!dict.empty())
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
ValueMap& metadata = dict["metadata"].asValueMap();
|
|
|
|
int version = metadata["version"].asInt();
|
2019-11-23 20:27:39 +08:00
|
|
|
if (version != 1)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
CCLOG("cocos2d: ERROR: Invalid filenameLookup dictionary version: %d. Filename: %s", version,
|
2021-12-31 12:12:40 +08:00
|
|
|
filename.data());
|
2019-11-23 20:27:39 +08:00
|
|
|
return;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
setFilenameLookupDictionary(dict["filenames"].asValueMap());
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string FileUtils::getFullPathForFilenameWithinDirectory(std::string_view directory,
|
|
|
|
std::string_view filename) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
// get directory+filename, safely adding '/' as necessary
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string ret{directory};
|
|
|
|
if (!directory.empty() && directory[directory.size() - 1] != '/')
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
ret += '/';
|
|
|
|
}
|
|
|
|
ret += filename;
|
|
|
|
// if the file doesn't exist, return an empty string
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!isFileExistInternal(ret))
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
ret.clear();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
bool FileUtils::isFileExist(std::string_view filename) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (isAbsolutePath(filename))
|
|
|
|
{
|
|
|
|
return isFileExistInternal(filename);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::string fullpath = fullPathForFilename(filename);
|
2022-05-23 21:55:30 +08:00
|
|
|
return !fullpath.empty();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void FileUtils::isFileExist(std::string_view filename, std::function<void(bool)> callback) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto fullPath = fullPathForFilename(filename);
|
2021-12-31 12:12:40 +08:00
|
|
|
performOperationOffthread(
|
|
|
|
[path = std::string{fullPath}]() -> bool { return FileUtils::getInstance()->isFileExist(path); },
|
|
|
|
std::move(callback));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
bool FileUtils::isAbsolutePath(std::string_view path) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-11-21 22:11:48 +08:00
|
|
|
return isAbsolutePathInternal(path);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
bool FileUtils::isAbsolutePathInternal(std::string_view path)
|
2021-11-21 22:11:48 +08:00
|
|
|
{
|
2021-12-31 15:50:34 +08:00
|
|
|
const char* raw = path.data();
|
2021-11-21 22:11:48 +08:00
|
|
|
#if defined(_WIN32)
|
|
|
|
// see also: https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file?redirectedfrom=MSDN
|
2021-12-31 15:50:34 +08:00
|
|
|
return ((path.length() > 2 && ((raw[0] >= 'a' && raw[0] <= 'z') || (raw[0] >= 'A' && raw[0] <= 'Z')) &&
|
|
|
|
raw[1] == ':') // Normal absolute path
|
2021-11-21 22:11:48 +08:00
|
|
|
|| cxx20::starts_with(path, R"(\\?\)") // Win32 File Namespaces for Long Path
|
|
|
|
|| cxx20::starts_with(path, R"(\\.\)") // Win32 Device Namespaces for device
|
2021-12-31 15:50:34 +08:00
|
|
|
|| (raw[0] == '/' || raw[0] == '\\') // Current disk drive
|
2021-11-21 22:11:48 +08:00
|
|
|
);
|
|
|
|
#else
|
2021-12-31 15:50:34 +08:00
|
|
|
return (raw[0] == '/');
|
2021-11-21 22:11:48 +08:00
|
|
|
#endif
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
bool FileUtils::isDirectoryExist(std::string_view dirPath) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
CCASSERT(!dirPath.empty(), "Invalid path");
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
DECLARE_GUARD;
|
|
|
|
|
|
|
|
if (isAbsolutePath(dirPath))
|
|
|
|
{
|
|
|
|
return isDirectoryExistInternal(dirPath);
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
auto fullPath = fullPathForDirectory(dirPath);
|
|
|
|
return !fullPath.empty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void FileUtils::isDirectoryExist(std::string_view fullPath, std::function<void(bool)> callback) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
CCASSERT(isAbsolutePath(fullPath), "Async isDirectoryExist only accepts absolute file paths");
|
2021-12-31 12:12:40 +08:00
|
|
|
performOperationOffthread(
|
|
|
|
[path = std::string{fullPath}]() -> bool { return FileUtils::getInstance()->isDirectoryExist(path); },
|
|
|
|
std::move(callback));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void FileUtils::createDirectory(std::string_view dirPath, std::function<void(bool)> callback) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
performOperationOffthread(
|
|
|
|
[path = std::string{dirPath}]() -> bool { return FileUtils::getInstance()->createDirectory(path); },
|
|
|
|
std::move(callback));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void FileUtils::removeDirectory(std::string_view dirPath, std::function<void(bool)> callback) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
performOperationOffthread(
|
|
|
|
[path = std::string{dirPath}]() -> bool { return FileUtils::getInstance()->removeDirectory(path); },
|
|
|
|
std::move(callback));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void FileUtils::removeFile(std::string_view filepath, std::function<void(bool)> callback) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto fullPath = fullPathForFilename(filepath);
|
2021-12-31 12:12:40 +08:00
|
|
|
performOperationOffthread(
|
|
|
|
[path = std::string{fullPath}]() -> bool { return FileUtils::getInstance()->removeFile(path); },
|
|
|
|
std::move(callback));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void FileUtils::renameFile(std::string_view path,
|
|
|
|
std::string_view oldname,
|
|
|
|
std::string_view name,
|
2021-12-25 10:04:45 +08:00
|
|
|
std::function<void(bool)> callback) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
performOperationOffthread(
|
2021-12-31 12:12:40 +08:00
|
|
|
[path = std::string{path}, oldname = std::string{oldname}, name = std::string{name}]() -> bool {
|
|
|
|
return FileUtils::getInstance()->renameFile(path, oldname, name);
|
|
|
|
},
|
2021-12-25 10:04:45 +08:00
|
|
|
std::move(callback));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void FileUtils::renameFile(std::string_view oldfullpath,
|
|
|
|
std::string_view newfullpath,
|
2021-12-25 10:04:45 +08:00
|
|
|
std::function<void(bool)> callback) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
performOperationOffthread(
|
2021-12-31 12:12:40 +08:00
|
|
|
[oldpath = std::string{oldfullpath}, newpath = std::string{newfullpath}]() {
|
|
|
|
return FileUtils::getInstance()->renameFile(oldpath, newpath);
|
|
|
|
},
|
2021-12-25 10:04:45 +08:00
|
|
|
std::move(callback));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void FileUtils::getFileSize(std::string_view filepath, std::function<void(int64_t)> callback) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto fullPath = fullPathForFilename(filepath);
|
2021-12-31 12:12:40 +08:00
|
|
|
performOperationOffthread([path = std::string{fullPath}]() { return FileUtils::getInstance()->getFileSize(path); },
|
2021-12-25 10:04:45 +08:00
|
|
|
std::move(callback));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void FileUtils::listFilesAsync(std::string_view dirPath, std::function<void(std::vector<std::string>)> callback) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto fullPath = fullPathForDirectory(dirPath);
|
2021-12-31 12:12:40 +08:00
|
|
|
performOperationOffthread([path = std::string{fullPath}]() { return FileUtils::getInstance()->listFiles(path); },
|
2021-12-25 10:04:45 +08:00
|
|
|
std::move(callback));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void FileUtils::listFilesRecursivelyAsync(std::string_view dirPath,
|
2021-12-25 10:04:45 +08:00
|
|
|
std::function<void(std::vector<std::string>)> callback) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto fullPath = fullPathForDirectory(dirPath);
|
2021-12-25 10:04:45 +08:00
|
|
|
performOperationOffthread(
|
2021-12-31 12:12:40 +08:00
|
|
|
[path = std::string{fullPath}]() {
|
2021-12-25 10:04:45 +08:00
|
|
|
std::vector<std::string> retval;
|
2021-12-31 12:12:40 +08:00
|
|
|
FileUtils::getInstance()->listFilesRecursively(path, &retval);
|
2021-12-25 10:04:45 +08:00
|
|
|
return retval;
|
|
|
|
},
|
|
|
|
std::move(callback));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
std::unique_ptr<FileStream> FileUtils::openFileStream(std::string_view filePath, FileStream::Mode mode)
|
2021-04-22 22:01:32 +08:00
|
|
|
{
|
2021-04-24 07:22:27 +08:00
|
|
|
PosixFileStream fs;
|
|
|
|
|
|
|
|
if (fs.open(filePath, mode))
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
return std::make_unique<PosixFileStream>(std::move(fs)); // PosixFileStream is the default implementation
|
2021-04-24 07:22:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
2021-04-22 22:01:32 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
/* !!!Notes for c++fs
|
|
|
|
a. ios: require ios 13.0+
|
|
|
|
b. android: require ndk-r22+
|
|
|
|
*/
|
|
|
|
#if ADXE_HAVE_STDFS
|
|
|
|
std::vector<std::string> FileUtils::listFiles(std::string_view dirPath) const
|
|
|
|
{
|
|
|
|
const auto fullPath = fullPathForDirectory(dirPath);
|
|
|
|
auto fsPath = toFspath(fullPath);
|
|
|
|
if (!stdfs::is_directory(fsPath))
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
std::vector<std::string> files = {};
|
|
|
|
for (const auto& entry : stdfs::directory_iterator(fsPath))
|
|
|
|
{
|
|
|
|
const auto isDir = entry.is_directory();
|
|
|
|
if (isDir || entry.is_regular_file())
|
|
|
|
{
|
|
|
|
# if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
|
|
|
|
# if defined(__cpp_lib_char8_t)
|
|
|
|
std::u8string u8path = entry.path().u8string();
|
|
|
|
std::string pathStr = {u8path.begin(), u8path.end()};
|
|
|
|
# else
|
|
|
|
std::string pathStr = entry.path().u8string();
|
|
|
|
# endif
|
|
|
|
std::replace(pathStr.begin(), pathStr.end(), '\\', '/');
|
|
|
|
# else
|
|
|
|
std::string pathStr = entry.path().string();
|
|
|
|
# endif
|
|
|
|
if (isDir)
|
|
|
|
{
|
|
|
|
pathStr += '/';
|
|
|
|
}
|
|
|
|
files.emplace_back(std::move(pathStr));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return files;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileUtils::listFilesRecursively(std::string_view dirPath, std::vector<std::string>* files) const
|
|
|
|
{
|
|
|
|
const auto fullPath = fullPathForDirectory(dirPath);
|
|
|
|
auto fsPath = toFspath(fullPath);
|
|
|
|
if (!stdfs::is_directory(fsPath))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (const auto& entry : stdfs::recursive_directory_iterator(fsPath))
|
|
|
|
{
|
|
|
|
const auto isDir = entry.is_directory();
|
|
|
|
if (isDir || entry.is_regular_file())
|
|
|
|
{
|
|
|
|
# if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
|
|
|
|
# if defined(__cpp_lib_char8_t)
|
|
|
|
std::u8string u8path = entry.path().u8string();
|
|
|
|
std::string pathStr = {u8path.begin(), u8path.end()};
|
|
|
|
# else
|
|
|
|
std::string pathStr = entry.path().u8string();
|
|
|
|
# endif
|
|
|
|
std::replace(pathStr.begin(), pathStr.end(), '\\', '/');
|
|
|
|
# else
|
|
|
|
std::string pathStr = entry.path().string();
|
|
|
|
# endif
|
|
|
|
if (isDir)
|
|
|
|
{
|
|
|
|
pathStr += '/';
|
|
|
|
}
|
|
|
|
files->emplace_back(std::move(pathStr));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
std::vector<std::string> FileUtils::listFiles(std::string_view dirPath) const
|
|
|
|
{
|
|
|
|
std::vector<std::string> files;
|
|
|
|
std::string fullpath = fullPathForDirectory(dirPath);
|
|
|
|
if (!fullpath.empty() && isDirectoryExist(fullpath))
|
|
|
|
{
|
|
|
|
tinydir_dir dir;
|
|
|
|
std::string fullpathstr = fullpath;
|
|
|
|
|
|
|
|
if (tinydir_open(&dir, &fullpathstr[0]) != -1)
|
|
|
|
{
|
|
|
|
while (dir.has_next)
|
|
|
|
{
|
|
|
|
tinydir_file file;
|
|
|
|
if (tinydir_readfile(&dir, &file) == -1)
|
|
|
|
{
|
|
|
|
// Error getting file
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
std::string filepath = file.path;
|
|
|
|
|
|
|
|
if (strcmp(file.name, ".") != 0 && strcmp(file.name, "..") != 0)
|
|
|
|
{
|
|
|
|
if (file.is_dir)
|
|
|
|
filepath.push_back('/');
|
|
|
|
|
|
|
|
files.push_back(std::move(filepath));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tinydir_next(&dir) == -1)
|
|
|
|
{
|
|
|
|
// Error getting next file
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tinydir_close(&dir);
|
|
|
|
}
|
|
|
|
return files;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileUtils::listFilesRecursively(std::string_view dirPath, std::vector<std::string>* files) const
|
|
|
|
{
|
|
|
|
std::string fullpath = fullPathForDirectory(dirPath);
|
|
|
|
if (isDirectoryExist(fullpath))
|
|
|
|
{
|
|
|
|
tinydir_dir dir;
|
|
|
|
std::string fullpathstr = fullpath;
|
|
|
|
|
|
|
|
if (tinydir_open(&dir, &fullpathstr[0]) != -1)
|
|
|
|
{
|
|
|
|
while (dir.has_next)
|
|
|
|
{
|
|
|
|
tinydir_file file;
|
|
|
|
if (tinydir_readfile(&dir, &file) == -1)
|
|
|
|
{
|
|
|
|
// Error getting file
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(file.name, ".") != 0 && strcmp(file.name, "..") != 0)
|
|
|
|
{
|
|
|
|
std::string filepath = file.path;
|
|
|
|
if (file.is_dir)
|
|
|
|
{
|
|
|
|
filepath.push_back('/');
|
|
|
|
files->push_back(filepath);
|
|
|
|
listFilesRecursively(filepath, files);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
files->push_back(std::move(filepath));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tinydir_next(&dir) == -1)
|
|
|
|
{
|
|
|
|
// Error getting next file
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tinydir_close(&dir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
|
|
|
|
// windows os implement should override in platform specific FileUtiles class
|
2021-12-31 12:12:40 +08:00
|
|
|
bool FileUtils::isDirectoryExistInternal(std::string_view dirPath) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
CCASSERT(false, "FileUtils not support isDirectoryExistInternal");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
bool FileUtils::createDirectory(std::string_view path) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
CCASSERT(false, "FileUtils not support createDirectory");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
bool FileUtils::removeDirectory(std::string_view path) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
CCASSERT(false, "FileUtils not support removeDirectory");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
bool FileUtils::removeFile(std::string_view path) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
CCASSERT(false, "FileUtils not support removeFile");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
bool FileUtils::renameFile(std::string_view oldfullpath, std::string_view newfullpath) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
CCASSERT(false, "FileUtils not support renameFile");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
bool FileUtils::renameFile(std::string_view path, std::string_view oldname, std::string_view name) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
CCASSERT(false, "FileUtils not support renameFile");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
int64_t FileUtils::getFileSize(std::string_view filepath) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
CCASSERT(false, "getFileSize should be override by platform FileUtils");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
// default implements for unix like os
|
2021-12-25 10:04:45 +08:00
|
|
|
# include <sys/types.h>
|
|
|
|
# include <errno.h>
|
|
|
|
# include <dirent.h>
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// android doesn't have ftw.h
|
2021-12-25 10:04:45 +08:00
|
|
|
# if (CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID)
|
|
|
|
# include <ftw.h>
|
|
|
|
# endif
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
bool FileUtils::isDirectoryExistInternal(std::string_view dirPath) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
struct stat st;
|
2021-12-31 12:12:40 +08:00
|
|
|
if (stat(dirPath.data(), &st) == 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return S_ISDIR(st.st_mode);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
bool FileUtils::createDirectory(std::string_view path) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
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);
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string_view subpath;
|
2019-11-23 20:27:39 +08:00
|
|
|
std::vector<std::string> dirs;
|
|
|
|
|
|
|
|
if (found != std::string::npos)
|
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
subpath = path.substr(start, found - start + 1);
|
|
|
|
if (!subpath.empty())
|
2021-12-31 12:12:40 +08:00
|
|
|
dirs.push_back(std::string{subpath});
|
2021-12-25 10:04:45 +08:00
|
|
|
start = found + 1;
|
2019-11-23 20:27:39 +08:00
|
|
|
found = path.find_first_of("/\\", start);
|
|
|
|
if (found == std::string::npos)
|
|
|
|
{
|
|
|
|
if (start < path.length())
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
dirs.push_back(std::string{path.substr(start)});
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
DIR* dir = NULL;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// Create path recursively
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string strSubpath;
|
2019-11-23 20:27:39 +08:00
|
|
|
for (const auto& iter : dirs)
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
strSubpath += iter;
|
|
|
|
dir = opendir(strSubpath.c_str());
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
if (!dir)
|
|
|
|
{
|
|
|
|
// directory doesn't exist, should create a new one
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
int ret = mkdir(strSubpath.c_str(), S_IRWXU | S_IRWXG | S_IRWXO);
|
2019-11-23 20:27:39 +08:00
|
|
|
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
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
# 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;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
# endif
|
|
|
|
} // namespace
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
bool FileUtils::removeDirectory(std::string_view path) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
# if !defined(CC_TARGET_OS_TVOS)
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
# if (CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID)
|
2021-12-31 12:12:40 +08:00
|
|
|
if (nftw(path.data(), unlink_cb, 64, FTW_DEPTH | FTW_PHYS) == -1)
|
2019-11-23 20:27:39 +08:00
|
|
|
return false;
|
|
|
|
else
|
|
|
|
return true;
|
2021-12-25 10:04:45 +08:00
|
|
|
# else
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string command = "rm -r \""s;
|
2019-11-23 20:27:39 +08:00
|
|
|
// Path may include space.
|
2021-12-31 12:12:40 +08:00
|
|
|
command.append(path).append("\"", 1);
|
2019-11-23 20:27:39 +08:00
|
|
|
if (system(command.c_str()) >= 0)
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
2021-12-25 10:04:45 +08:00
|
|
|
# endif // (CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID)
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
# else
|
2019-11-23 20:27:39 +08:00
|
|
|
return false;
|
2021-12-25 10:04:45 +08:00
|
|
|
# endif // !defined(CC_TARGET_OS_TVOS)
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
bool FileUtils::removeFile(std::string_view path) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
if (remove(path.data()))
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
return false;
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
bool FileUtils::renameFile(std::string_view oldfullpath, std::string_view newfullpath) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
CCASSERT(!oldfullpath.empty(), "Invalid path");
|
|
|
|
CCASSERT(!newfullpath.empty(), "Invalid path");
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
int errorCode = rename(oldfullpath.data(), newfullpath.data());
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
if (0 != errorCode)
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
CCLOGERROR("Fail to rename file %s to %s !Error code is %d", oldfullpath.data(), newfullpath.data(), errorCode);
|
2019-11-23 20:27:39 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
bool FileUtils::renameFile(std::string_view path, std::string_view oldname, std::string_view name) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
CCASSERT(!path.empty(), "Invalid path");
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string oldPath{path};
|
|
|
|
oldPath += oldname;
|
|
|
|
std::string newPath{path};
|
|
|
|
newPath += name;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
return this->renameFile(oldPath, newPath);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
int64_t FileUtils::getFileSize(std::string_view filepath) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
CCASSERT(!filepath.empty(), "Invalid path");
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string_view path;
|
|
|
|
std::string fullpath;
|
2019-11-23 20:27:39 +08:00
|
|
|
if (!isAbsolutePath(filepath))
|
|
|
|
{
|
|
|
|
fullpath = fullPathForFilename(filepath);
|
|
|
|
if (fullpath.empty())
|
|
|
|
return 0;
|
2021-12-31 12:12:40 +08:00
|
|
|
path = fullpath;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
else
|
|
|
|
path = filepath;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2019-11-28 09:59:18 +08:00
|
|
|
struct stat info;
|
2019-11-23 20:27:39 +08:00
|
|
|
// Get data associated with "crt_stat.c":
|
2021-12-31 12:12:40 +08:00
|
|
|
int result = ::stat(path.data(), &info);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// Check if statistics are valid:
|
|
|
|
if (result != 0)
|
|
|
|
{
|
|
|
|
// Failed
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-11-27 15:09:45 +08:00
|
|
|
return info.st_size;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Notification support when getFileData from invalid file path.
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
static bool s_popupNotify = true;
|
|
|
|
|
|
|
|
void FileUtils::setPopupNotify(bool notify)
|
|
|
|
{
|
|
|
|
s_popupNotify = notify;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileUtils::isPopupNotify() const
|
|
|
|
{
|
|
|
|
return s_popupNotify;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string FileUtils::getFileExtension(std::string_view filePath) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string FileUtils::getFileShortName(std::string_view filePath)
|
2020-08-01 19:25:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
// std::string fileExtension;
|
2020-08-01 19:25:39 +08:00
|
|
|
size_t pos = filePath.find_last_of("/\\");
|
|
|
|
if (pos != std::string::npos)
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
return std::string{filePath.substr(pos + 1)};
|
2020-08-01 19:25:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
return std::string{filePath};
|
2020-08-01 19:25:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void FileUtils::valueMapCompact(ValueMap& /*valueMap*/) const {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void FileUtils::valueVectorCompact(ValueVector& /*valueVector*/) const {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
NS_CC_END
|