2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
2013-02-01 11:20:46 +08:00
|
|
|
Copyright (c) 2010-2013 cocos2d-x.org
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
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 "CCFileUtils.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
|
|
|
|
#include <stack>
|
|
|
|
|
2014-04-27 01:35:57 +08:00
|
|
|
#include "base/CCData.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/ccMacros.h"
|
|
|
|
#include "base/CCDirector.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "platform/CCSAXParser.h"
|
|
|
|
|
2013-10-14 14:01:00 +08:00
|
|
|
#include "tinyxml2.h"
|
|
|
|
#include "unzip.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
|
2013-02-01 11:20:46 +08:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
#if (CC_TARGET_PLATFORM != CC_PLATFORM_IOS) && (CC_TARGET_PLATFORM != CC_PLATFORM_MAC)
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
SAX_NONE = 0,
|
|
|
|
SAX_KEY,
|
|
|
|
SAX_DICT,
|
|
|
|
SAX_INT,
|
|
|
|
SAX_REAL,
|
|
|
|
SAX_STRING,
|
|
|
|
SAX_ARRAY
|
2013-06-20 14:13:12 +08:00
|
|
|
}SAXState;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
SAX_RESULT_NONE = 0,
|
|
|
|
SAX_RESULT_DICT,
|
|
|
|
SAX_RESULT_ARRAY
|
2013-06-20 14:13:12 +08:00
|
|
|
}SAXResult;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
class DictMaker : public SAXDelegator
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
public:
|
2013-06-20 14:13:12 +08:00
|
|
|
SAXResult _resultType;
|
2013-12-04 17:46:57 +08:00
|
|
|
ValueMap _rootDict;
|
|
|
|
ValueVector _rootArray;
|
2013-12-04 15:34:05 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
std::string _curKey; ///< parsed key
|
|
|
|
std::string _curValue; // parsed value
|
2013-06-20 14:13:12 +08:00
|
|
|
SAXState _state;
|
2013-06-15 14:03:30 +08:00
|
|
|
|
2013-12-04 17:46:57 +08:00
|
|
|
ValueMap* _curDict;
|
|
|
|
ValueVector* _curArray;
|
2013-12-04 15:34:05 +08:00
|
|
|
|
2013-12-04 17:46:57 +08:00
|
|
|
std::stack<ValueMap*> _dictStack;
|
|
|
|
std::stack<ValueVector*> _arrayStack;
|
2013-06-20 14:13:12 +08:00
|
|
|
std::stack<SAXState> _stateStack;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
public:
|
2013-06-20 14:13:12 +08:00
|
|
|
DictMaker()
|
2013-12-04 10:33:27 +08:00
|
|
|
: _resultType(SAX_RESULT_NONE)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
~DictMaker()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-12-24 10:51:47 +08:00
|
|
|
ValueMap dictionaryWithContentsOfFile(const std::string& fileName)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_resultType = SAX_RESULT_DICT;
|
2013-06-20 14:13:12 +08:00
|
|
|
SAXParser parser;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-12-04 10:33:27 +08:00
|
|
|
CCASSERT(parser.init("UTF-8"), "The file format isn't UTF-8");
|
2012-04-19 14:35:52 +08:00
|
|
|
parser.setDelegator(this);
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
parser.parse(fileName);
|
2013-12-10 16:06:05 +08:00
|
|
|
return _rootDict;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-12-24 10:51:47 +08:00
|
|
|
ValueVector arrayWithContentsOfFile(const std::string& fileName)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_resultType = SAX_RESULT_ARRAY;
|
2013-06-20 14:13:12 +08:00
|
|
|
SAXParser parser;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-12-04 10:33:27 +08:00
|
|
|
CCASSERT(parser.init("UTF-8"), "The file format isn't UTF-8");
|
2012-04-19 14:35:52 +08:00
|
|
|
parser.setDelegator(this);
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
parser.parse(fileName);
|
2013-12-10 16:06:05 +08:00
|
|
|
return _rootArray;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void startElement(void *ctx, const char *name, const char **atts)
|
|
|
|
{
|
|
|
|
CC_UNUSED_PARAM(ctx);
|
|
|
|
CC_UNUSED_PARAM(atts);
|
2014-01-08 05:02:31 +08:00
|
|
|
const std::string sName(name);
|
2012-04-19 14:35:52 +08:00
|
|
|
if( sName == "dict" )
|
|
|
|
{
|
2013-12-04 15:34:05 +08:00
|
|
|
if(_resultType == SAX_RESULT_DICT && _rootDict.empty())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-04 15:34:05 +08:00
|
|
|
_curDict = &_rootDict;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-12-04 15:34:05 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_state = SAX_DICT;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
SAXState preState = SAX_NONE;
|
2013-06-15 14:03:30 +08:00
|
|
|
if (! _stateStack.empty())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
preState = _stateStack.top();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (SAX_ARRAY == preState)
|
|
|
|
{
|
2013-12-04 15:34:05 +08:00
|
|
|
// add a new dictionary into the array
|
2013-12-04 17:46:57 +08:00
|
|
|
_curArray->push_back(Value(ValueMap()));
|
|
|
|
_curDict = &(_curArray->rbegin())->asValueMap();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else if (SAX_DICT == preState)
|
|
|
|
{
|
2013-12-04 15:34:05 +08:00
|
|
|
// add a new dictionary into the pre dictionary
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(! _dictStack.empty(), "The state is wrong!");
|
2013-12-04 17:46:57 +08:00
|
|
|
ValueMap* preDict = _dictStack.top();
|
|
|
|
(*preDict)[_curKey] = Value(ValueMap());
|
|
|
|
_curDict = &(*preDict)[_curKey].asValueMap();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-12-04 15:34:05 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// record the dict state
|
2013-06-15 14:03:30 +08:00
|
|
|
_stateStack.push(_state);
|
|
|
|
_dictStack.push(_curDict);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else if(sName == "key")
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_state = SAX_KEY;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else if(sName == "integer")
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_state = SAX_INT;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else if(sName == "real")
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_state = SAX_REAL;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else if(sName == "string")
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_state = SAX_STRING;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else if (sName == "array")
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_state = SAX_ARRAY;
|
2013-12-04 10:33:27 +08:00
|
|
|
|
2013-12-04 15:34:05 +08:00
|
|
|
if (_resultType == SAX_RESULT_ARRAY && _rootArray.empty())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-04 15:34:05 +08:00
|
|
|
_curArray = &_rootArray;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-06-20 14:13:12 +08:00
|
|
|
SAXState preState = SAX_NONE;
|
2013-06-15 14:03:30 +08:00
|
|
|
if (! _stateStack.empty())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
preState = _stateStack.top();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (preState == SAX_DICT)
|
|
|
|
{
|
2013-12-04 17:46:57 +08:00
|
|
|
(*_curDict)[_curKey] = Value(ValueVector());
|
|
|
|
_curArray = &(*_curDict)[_curKey].asValueVector();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else if (preState == SAX_ARRAY)
|
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(! _arrayStack.empty(), "The state is wrong!");
|
2013-12-04 17:46:57 +08:00
|
|
|
ValueVector* preArray = _arrayStack.top();
|
|
|
|
preArray->push_back(Value(ValueVector()));
|
|
|
|
_curArray = &(_curArray->rbegin())->asValueVector();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
// record the array state
|
2013-06-15 14:03:30 +08:00
|
|
|
_stateStack.push(_state);
|
2013-12-04 15:34:05 +08:00
|
|
|
_arrayStack.push(_curArray);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_state = SAX_NONE;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void endElement(void *ctx, const char *name)
|
|
|
|
{
|
|
|
|
CC_UNUSED_PARAM(ctx);
|
2013-06-20 14:13:12 +08:00
|
|
|
SAXState curState = _stateStack.empty() ? SAX_DICT : _stateStack.top();
|
2014-01-08 05:02:31 +08:00
|
|
|
const std::string sName((char*)name);
|
2012-04-19 14:35:52 +08:00
|
|
|
if( sName == "dict" )
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_stateStack.pop();
|
|
|
|
_dictStack.pop();
|
|
|
|
if ( !_dictStack.empty())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_curDict = _dictStack.top();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (sName == "array")
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_stateStack.pop();
|
|
|
|
_arrayStack.pop();
|
|
|
|
if (! _arrayStack.empty())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-04 15:34:05 +08:00
|
|
|
_curArray = _arrayStack.top();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (sName == "true")
|
|
|
|
{
|
|
|
|
if (SAX_ARRAY == curState)
|
|
|
|
{
|
2013-12-04 15:34:05 +08:00
|
|
|
_curArray->push_back(Value(true));
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else if (SAX_DICT == curState)
|
|
|
|
{
|
2013-12-04 15:34:05 +08:00
|
|
|
(*_curDict)[_curKey] = Value(true);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (sName == "false")
|
|
|
|
{
|
|
|
|
if (SAX_ARRAY == curState)
|
|
|
|
{
|
2013-12-04 15:34:05 +08:00
|
|
|
_curArray->push_back(Value(false));
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else if (SAX_DICT == curState)
|
|
|
|
{
|
2013-12-04 15:34:05 +08:00
|
|
|
(*_curDict)[_curKey] = Value(false);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
2012-04-25 10:55:59 +08:00
|
|
|
else if (sName == "string" || sName == "integer" || sName == "real")
|
|
|
|
{
|
|
|
|
if (SAX_ARRAY == curState)
|
|
|
|
{
|
2013-12-04 10:33:27 +08:00
|
|
|
if (sName == "string")
|
2013-12-04 15:34:05 +08:00
|
|
|
_curArray->push_back(Value(_curValue));
|
2013-12-04 10:33:27 +08:00
|
|
|
else if (sName == "integer")
|
2013-12-04 15:34:05 +08:00
|
|
|
_curArray->push_back(Value(atoi(_curValue.c_str())));
|
2013-12-04 10:33:27 +08:00
|
|
|
else
|
2013-12-04 15:34:05 +08:00
|
|
|
_curArray->push_back(Value(atof(_curValue.c_str())));
|
2012-04-25 10:55:59 +08:00
|
|
|
}
|
|
|
|
else if (SAX_DICT == curState)
|
|
|
|
{
|
2013-12-04 10:33:27 +08:00
|
|
|
if (sName == "string")
|
2013-12-04 15:34:05 +08:00
|
|
|
(*_curDict)[_curKey] = Value(_curValue);
|
2013-12-04 10:33:27 +08:00
|
|
|
else if (sName == "integer")
|
2013-12-04 15:34:05 +08:00
|
|
|
(*_curDict)[_curKey] = Value(atoi(_curValue.c_str()));
|
2013-12-04 10:33:27 +08:00
|
|
|
else
|
2013-12-04 15:34:05 +08:00
|
|
|
(*_curDict)[_curKey] = Value(atof(_curValue.c_str()));
|
2012-04-25 10:55:59 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_curValue.clear();
|
2012-04-25 10:55:59 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_state = SAX_NONE;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void textHandler(void *ctx, const char *ch, int len)
|
|
|
|
{
|
|
|
|
CC_UNUSED_PARAM(ctx);
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_state == SAX_NONE)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
SAXState curState = _stateStack.empty() ? SAX_DICT : _stateStack.top();
|
2014-01-08 05:02:31 +08:00
|
|
|
const std::string text = std::string((char*)ch,0,len);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
switch(_state)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
case SAX_KEY:
|
2013-12-04 10:33:27 +08:00
|
|
|
_curKey = text;
|
2012-04-19 14:35:52 +08:00
|
|
|
break;
|
|
|
|
case SAX_INT:
|
|
|
|
case SAX_REAL:
|
|
|
|
case SAX_STRING:
|
|
|
|
{
|
2012-04-25 10:55:59 +08:00
|
|
|
if (curState == SAX_DICT)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(!_curKey.empty(), "key not found : <integer/real>");
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-04-25 10:55:59 +08:00
|
|
|
|
2013-12-04 10:33:27 +08:00
|
|
|
_curValue.append(text);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-04-25 10:55:59 +08:00
|
|
|
break;
|
2012-04-19 14:35:52 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-12-04 17:50:57 +08:00
|
|
|
ValueMap FileUtils::getValueMapFromFile(const std::string& filename)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-01-08 05:02:31 +08:00
|
|
|
const std::string fullPath = fullPathForFilename(filename.c_str());
|
2013-06-20 14:13:12 +08:00
|
|
|
DictMaker tMaker;
|
2013-12-10 16:06:05 +08:00
|
|
|
return tMaker.dictionaryWithContentsOfFile(fullPath.c_str());
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-12-04 17:50:57 +08:00
|
|
|
ValueVector FileUtils::getValueVectorFromFile(const std::string& filename)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-01-08 05:02:31 +08:00
|
|
|
const std::string fullPath = fullPathForFilename(filename.c_str());
|
2013-06-20 14:13:12 +08:00
|
|
|
DictMaker tMaker;
|
2013-12-10 16:06:05 +08:00
|
|
|
return tMaker.arrayWithContentsOfFile(fullPath.c_str());
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
|
2013-04-16 12:34:09 +08:00
|
|
|
/*
|
|
|
|
* forward statement
|
|
|
|
*/
|
2013-12-20 10:11:41 +08:00
|
|
|
static tinyxml2::XMLElement* generateElementForArray(const ValueVector& array, tinyxml2::XMLDocument *doc);
|
|
|
|
static tinyxml2::XMLElement* generateElementForDict(const ValueMap& dict, tinyxml2::XMLDocument *doc);
|
2013-04-16 12:34:09 +08:00
|
|
|
|
2013-04-13 16:55:24 +08:00
|
|
|
/*
|
|
|
|
* Use tinyxml2 to write plist files
|
|
|
|
*/
|
2013-12-04 17:46:57 +08:00
|
|
|
bool FileUtils::writeToFile(ValueMap& dict, const std::string &fullPath)
|
2013-04-13 16:55:24 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
//CCLOG("tinyxml2 Dictionary %d writeToFile %s", dict->_ID, fullPath.c_str());
|
2013-12-18 17:47:20 +08:00
|
|
|
tinyxml2::XMLDocument *doc = new tinyxml2::XMLDocument();
|
|
|
|
if (nullptr == doc)
|
2013-04-13 16:55:24 +08:00
|
|
|
return false;
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
tinyxml2::XMLDeclaration *declaration = doc->NewDeclaration("xml version=\"1.0\" encoding=\"UTF-8\"");
|
|
|
|
if (nullptr == declaration)
|
2013-04-28 00:36:37 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
delete doc;
|
2013-04-16 12:34:09 +08:00
|
|
|
return false;
|
2013-04-28 00:36:37 +08:00
|
|
|
}
|
2013-04-13 16:55:24 +08:00
|
|
|
|
2013-12-18 22:44:03 +08:00
|
|
|
doc->LinkEndChild(declaration);
|
2013-12-18 17:47:20 +08:00
|
|
|
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);
|
2013-04-13 16:55:24 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
tinyxml2::XMLElement *rootEle = doc->NewElement("plist");
|
2013-12-18 22:44:03 +08:00
|
|
|
rootEle->SetAttribute("version", "1.0");
|
2013-12-18 17:47:20 +08:00
|
|
|
if (nullptr == rootEle)
|
2013-04-28 00:36:37 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
delete doc;
|
2013-04-16 12:34:09 +08:00
|
|
|
return false;
|
2013-04-28 00:36:37 +08:00
|
|
|
}
|
2013-12-18 22:44:03 +08:00
|
|
|
doc->LinkEndChild(rootEle);
|
2013-04-13 16:55:24 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
tinyxml2::XMLElement *innerDict = generateElementForDict(dict, doc);
|
2013-12-04 10:33:27 +08:00
|
|
|
if (nullptr == innerDict )
|
2013-04-28 00:36:37 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
delete doc;
|
2013-04-13 16:55:24 +08:00
|
|
|
return false;
|
2013-04-28 00:36:37 +08:00
|
|
|
}
|
2013-12-18 22:44:03 +08:00
|
|
|
rootEle->LinkEndChild(innerDict);
|
2013-04-13 16:55:24 +08:00
|
|
|
|
2013-12-18 22:44:03 +08:00
|
|
|
bool ret = tinyxml2::XML_SUCCESS == doc->SaveFile(fullPath.c_str());
|
2013-04-13 16:55:24 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
delete doc;
|
|
|
|
return ret;
|
2013-04-13 16:55:24 +08:00
|
|
|
}
|
|
|
|
|
2013-04-16 12:34:09 +08:00
|
|
|
/*
|
2013-06-20 14:13:12 +08:00
|
|
|
* Generate tinyxml2::XMLElement for Object through a tinyxml2::XMLDocument
|
2013-04-16 12:34:09 +08:00
|
|
|
*/
|
2013-12-20 10:11:41 +08:00
|
|
|
static tinyxml2::XMLElement* generateElementForObject(const Value& value, tinyxml2::XMLDocument *doc)
|
2013-04-16 12:34:09 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
// object is String
|
2013-12-04 10:33:27 +08:00
|
|
|
if (value.getType() == Value::Type::STRING)
|
2013-04-16 12:34:09 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
tinyxml2::XMLElement* node = doc->NewElement("string");
|
|
|
|
tinyxml2::XMLText* content = doc->NewText(value.asString().c_str());
|
2013-04-28 00:36:37 +08:00
|
|
|
node->LinkEndChild(content);
|
|
|
|
return node;
|
2013-04-16 12:34:09 +08:00
|
|
|
}
|
|
|
|
|
2013-12-04 10:33:27 +08:00
|
|
|
// object is integer
|
|
|
|
if (value.getType() == Value::Type::INTEGER)
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
tinyxml2::XMLElement* node = doc->NewElement("integer");
|
|
|
|
tinyxml2::XMLText* content = doc->NewText(value.asString().c_str());
|
2013-12-04 10:33:27 +08:00
|
|
|
node->LinkEndChild(content);
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
// object is real
|
|
|
|
if (value.getType() == Value::Type::FLOAT || value.getType() == Value::Type::DOUBLE)
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
tinyxml2::XMLElement* node = doc->NewElement("real");
|
|
|
|
tinyxml2::XMLText* content = doc->NewText(value.asString().c_str());
|
2013-12-04 10:33:27 +08:00
|
|
|
node->LinkEndChild(content);
|
|
|
|
return node;
|
|
|
|
}
|
2014-03-19 16:42:14 +08:00
|
|
|
|
|
|
|
//object is bool
|
|
|
|
if (value.getType() == Value::Type::BOOLEAN) {
|
|
|
|
tinyxml2::XMLElement* node = doc->NewElement(value.asString().c_str());
|
|
|
|
return node;
|
|
|
|
}
|
2013-12-04 10:33:27 +08:00
|
|
|
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// object is Array
|
2013-12-05 11:44:37 +08:00
|
|
|
if (value.getType() == Value::Type::VECTOR)
|
2013-12-18 17:47:20 +08:00
|
|
|
return generateElementForArray(value.asValueVector(), doc);
|
2013-04-16 12:34:09 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// object is Dictionary
|
2013-12-05 11:44:37 +08:00
|
|
|
if (value.getType() == Value::Type::MAP)
|
2013-12-18 17:47:20 +08:00
|
|
|
return generateElementForDict(value.asValueMap(), doc);
|
2013-04-16 12:34:09 +08:00
|
|
|
|
2013-04-28 00:36:37 +08:00
|
|
|
CCLOG("This type cannot appear in property list");
|
2013-12-04 10:33:27 +08:00
|
|
|
return nullptr;
|
2013-04-16 12:34:09 +08:00
|
|
|
}
|
2013-02-01 11:20:46 +08:00
|
|
|
|
2013-04-13 16:55:24 +08:00
|
|
|
/*
|
2013-06-20 14:13:12 +08:00
|
|
|
* Generate tinyxml2::XMLElement for Dictionary through a tinyxml2::XMLDocument
|
2013-04-13 16:55:24 +08:00
|
|
|
*/
|
2013-12-20 10:11:41 +08:00
|
|
|
static tinyxml2::XMLElement* generateElementForDict(const ValueMap& dict, tinyxml2::XMLDocument *doc)
|
2013-04-13 16:55:24 +08:00
|
|
|
{
|
2013-12-18 22:44:03 +08:00
|
|
|
tinyxml2::XMLElement* rootNode = doc->NewElement("dict");
|
2013-04-13 16:55:24 +08:00
|
|
|
|
2014-01-08 05:02:31 +08:00
|
|
|
for (auto iter = dict.cbegin(); iter != dict.cend(); ++iter)
|
2013-04-13 16:55:24 +08:00
|
|
|
{
|
2013-12-18 22:44:03 +08:00
|
|
|
tinyxml2::XMLElement* tmpNode = doc->NewElement("key");
|
2013-04-13 16:55:24 +08:00
|
|
|
rootNode->LinkEndChild(tmpNode);
|
2013-12-18 17:47:20 +08:00
|
|
|
tinyxml2::XMLText* content = doc->NewText(iter->first.c_str());
|
2013-04-13 16:55:24 +08:00
|
|
|
tmpNode->LinkEndChild(content);
|
|
|
|
|
2013-12-18 22:44:03 +08:00
|
|
|
tinyxml2::XMLElement *element = generateElementForObject(iter->second, doc);
|
2013-04-16 12:34:09 +08:00
|
|
|
if (element)
|
|
|
|
rootNode->LinkEndChild(element);
|
|
|
|
}
|
|
|
|
return rootNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-06-20 14:13:12 +08:00
|
|
|
* Generate tinyxml2::XMLElement for Array through a tinyxml2::XMLDocument
|
2013-04-16 12:34:09 +08:00
|
|
|
*/
|
2013-12-20 10:11:41 +08:00
|
|
|
static tinyxml2::XMLElement* generateElementForArray(const ValueVector& array, tinyxml2::XMLDocument *pDoc)
|
2013-04-16 12:34:09 +08:00
|
|
|
{
|
|
|
|
tinyxml2::XMLElement* rootNode = pDoc->NewElement("array");
|
2013-12-20 05:34:41 +08:00
|
|
|
|
|
|
|
for(const auto &value : array) {
|
2013-12-04 10:33:27 +08:00
|
|
|
tinyxml2::XMLElement *element = generateElementForObject(value, pDoc);
|
2013-04-16 12:34:09 +08:00
|
|
|
if (element)
|
|
|
|
rootNode->LinkEndChild(element);
|
2013-12-20 05:34:41 +08:00
|
|
|
}
|
2013-04-13 16:55:24 +08:00
|
|
|
return rootNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-16 12:34:09 +08:00
|
|
|
#else
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-09-07 06:33:28 +08:00
|
|
|
/* The subclass FileUtilsApple should override these two method. */
|
2013-12-04 17:50:57 +08:00
|
|
|
ValueMap FileUtils::getValueMapFromFile(const std::string& filename) {return ValueMap();}
|
|
|
|
ValueVector FileUtils::getValueVectorFromFile(const std::string& filename) {return ValueVector();}
|
2013-12-04 17:46:57 +08:00
|
|
|
bool FileUtils::writeToFile(ValueMap& dict, const std::string &fullPath) {return false;}
|
2013-04-16 12:34:09 +08:00
|
|
|
|
|
|
|
#endif /* (CC_TARGET_PLATFORM != CC_PLATFORM_IOS) && (CC_TARGET_PLATFORM != CC_PLATFORM_MAC) */
|
|
|
|
|
|
|
|
|
2013-12-04 10:33:27 +08:00
|
|
|
FileUtils* FileUtils::s_sharedFileUtils = nullptr;
|
2013-02-01 11:20:46 +08:00
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
|
|
|
|
void FileUtils::destroyInstance()
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(s_sharedFileUtils);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
FileUtils::FileUtils()
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
FileUtils::~FileUtils()
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool FileUtils::init()
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_searchPathArray.push_back(_defaultResRootPath);
|
|
|
|
_searchResolutionsOrderArray.push_back("");
|
2013-02-01 15:41:41 +08:00
|
|
|
return true;
|
2013-02-01 11:20:46 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void FileUtils::purgeCachedEntries()
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_fullPathCache.clear();
|
2013-02-01 11:20:46 +08:00
|
|
|
}
|
|
|
|
|
2013-12-18 14:58:17 +08:00
|
|
|
static Data getData(const std::string& filename, bool forString)
|
|
|
|
{
|
2014-04-10 11:07:00 +08:00
|
|
|
if (filename.empty())
|
|
|
|
{
|
|
|
|
return Data::Null;
|
|
|
|
}
|
2013-12-18 14:58:17 +08:00
|
|
|
|
|
|
|
Data ret;
|
|
|
|
unsigned char* buffer = nullptr;
|
|
|
|
ssize_t size = 0;
|
2014-05-31 06:06:56 +08:00
|
|
|
size_t readsize;
|
2013-12-18 14:58:17 +08:00
|
|
|
const char* mode = nullptr;
|
|
|
|
if (forString)
|
|
|
|
mode = "rt";
|
|
|
|
else
|
|
|
|
mode = "rb";
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
// Read the file from hardware
|
|
|
|
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(filename);
|
|
|
|
FILE *fp = fopen(fullPath.c_str(), mode);
|
|
|
|
CC_BREAK_IF(!fp);
|
|
|
|
fseek(fp,0,SEEK_END);
|
|
|
|
size = ftell(fp);
|
|
|
|
fseek(fp,0,SEEK_SET);
|
|
|
|
|
|
|
|
if (forString)
|
|
|
|
{
|
|
|
|
buffer = (unsigned char*)malloc(sizeof(unsigned char) * (size + 1));
|
|
|
|
buffer[size] = '\0';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buffer = (unsigned char*)malloc(sizeof(unsigned char) * size);
|
|
|
|
}
|
|
|
|
|
2014-05-31 06:06:56 +08:00
|
|
|
readsize = fread(buffer, sizeof(unsigned char), size, fp);
|
2013-12-18 14:58:17 +08:00
|
|
|
fclose(fp);
|
2014-05-31 06:06:56 +08:00
|
|
|
|
|
|
|
if (forString && readsize < size)
|
|
|
|
{
|
|
|
|
buffer[readsize] = '\0';
|
|
|
|
}
|
2013-12-18 14:58:17 +08:00
|
|
|
} while (0);
|
|
|
|
|
|
|
|
if (nullptr == buffer || 0 == size)
|
|
|
|
{
|
|
|
|
std::string msg = "Get data from file(";
|
|
|
|
msg.append(filename).append(") failed!");
|
|
|
|
CCLOG("%s", msg.c_str());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret.fastSet(buffer, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string FileUtils::getStringFromFile(const std::string& filename)
|
|
|
|
{
|
|
|
|
Data data = getData(filename, true);
|
2014-01-16 10:09:04 +08:00
|
|
|
if (data.isNull())
|
2014-01-15 12:39:56 +08:00
|
|
|
return "";
|
2014-04-10 11:07:00 +08:00
|
|
|
|
2013-12-18 14:58:17 +08:00
|
|
|
std::string ret((const char*)data.getBytes());
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
Data FileUtils::getDataFromFile(const std::string& filename)
|
|
|
|
{
|
|
|
|
return getData(filename, false);
|
|
|
|
}
|
|
|
|
|
2013-12-24 10:51:47 +08:00
|
|
|
unsigned char* FileUtils::getFileData(const std::string& filename, const char* mode, ssize_t *size)
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
2013-12-04 10:33:27 +08:00
|
|
|
unsigned char * buffer = nullptr;
|
2013-12-24 10:51:47 +08:00
|
|
|
CCASSERT(!filename.empty() && size != nullptr && mode != nullptr, "Invalid parameters.");
|
2013-09-07 09:46:33 +08:00
|
|
|
*size = 0;
|
2013-02-01 11:20:46 +08:00
|
|
|
do
|
|
|
|
{
|
|
|
|
// read the file from hardware
|
2014-01-08 05:02:31 +08:00
|
|
|
const std::string fullPath = fullPathForFilename(filename);
|
2013-09-07 09:46:33 +08:00
|
|
|
FILE *fp = fopen(fullPath.c_str(), mode);
|
2013-02-01 11:20:46 +08:00
|
|
|
CC_BREAK_IF(!fp);
|
|
|
|
|
|
|
|
fseek(fp,0,SEEK_END);
|
2013-09-07 09:46:33 +08:00
|
|
|
*size = ftell(fp);
|
2013-02-01 11:20:46 +08:00
|
|
|
fseek(fp,0,SEEK_SET);
|
2013-11-12 10:09:47 +08:00
|
|
|
buffer = (unsigned char*)malloc(*size);
|
2013-09-07 09:46:33 +08:00
|
|
|
*size = fread(buffer,sizeof(unsigned char), *size,fp);
|
2013-02-01 11:20:46 +08:00
|
|
|
fclose(fp);
|
|
|
|
} while (0);
|
|
|
|
|
2013-09-07 09:46:33 +08:00
|
|
|
if (! buffer)
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
|
|
|
std::string msg = "Get data from file(";
|
2013-07-26 06:53:24 +08:00
|
|
|
msg.append(filename).append(") failed!");
|
2013-02-01 11:20:46 +08:00
|
|
|
|
2013-05-07 14:36:01 +08:00
|
|
|
CCLOG("%s", msg.c_str());
|
2013-02-01 11:20:46 +08:00
|
|
|
}
|
2013-09-07 09:46:33 +08:00
|
|
|
return buffer;
|
2013-02-01 11:20:46 +08:00
|
|
|
}
|
|
|
|
|
2013-12-24 10:51:47 +08:00
|
|
|
unsigned char* FileUtils::getFileDataFromZip(const std::string& zipFilePath, const std::string& filename, ssize_t *size)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-04 10:33:27 +08:00
|
|
|
unsigned char * buffer = nullptr;
|
2013-12-18 17:47:20 +08:00
|
|
|
unzFile file = nullptr;
|
2013-09-07 09:46:33 +08:00
|
|
|
*size = 0;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2013-12-24 10:51:47 +08:00
|
|
|
CC_BREAK_IF(zipFilePath.empty());
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-12-24 10:51:47 +08:00
|
|
|
file = unzOpen(zipFilePath.c_str());
|
2013-12-18 17:47:20 +08:00
|
|
|
CC_BREAK_IF(!file);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-12-24 10:51:47 +08:00
|
|
|
int ret = unzLocateFile(file, filename.c_str(), 1);
|
2013-12-18 17:47:20 +08:00
|
|
|
CC_BREAK_IF(UNZ_OK != ret);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
char filePathA[260];
|
|
|
|
unz_file_info fileInfo;
|
|
|
|
ret = unzGetCurrentFileInfo(file, &fileInfo, filePathA, sizeof(filePathA), nullptr, 0, nullptr, 0);
|
|
|
|
CC_BREAK_IF(UNZ_OK != ret);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
ret = unzOpenCurrentFile(file);
|
|
|
|
CC_BREAK_IF(UNZ_OK != ret);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
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");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
*size = fileInfo.uncompressed_size;
|
|
|
|
unzCloseCurrentFile(file);
|
2012-04-19 14:35:52 +08:00
|
|
|
} while (0);
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
if (file)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
unzClose(file);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-09-07 09:46:33 +08:00
|
|
|
return buffer;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2014-04-02 15:35:09 +08:00
|
|
|
std::string FileUtils::getNewFilename(const std::string &filename) const
|
2013-01-24 00:32:22 +08:00
|
|
|
{
|
2013-09-07 13:54:08 +08:00
|
|
|
std::string newFileName;
|
2013-09-07 06:33:28 +08:00
|
|
|
|
2013-01-24 00:32:22 +08:00
|
|
|
// in Lookup Filename dictionary ?
|
2013-12-03 14:47:35 +08:00
|
|
|
auto iter = _filenameLookupDict.find(filename);
|
|
|
|
|
|
|
|
if (iter == _filenameLookupDict.end())
|
|
|
|
{
|
2013-09-07 06:33:28 +08:00
|
|
|
newFileName = filename;
|
2013-01-24 00:32:22 +08:00
|
|
|
}
|
2013-12-03 14:47:35 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
newFileName = iter->second.asString();
|
2013-01-24 00:32:22 +08:00
|
|
|
}
|
2013-09-07 06:33:28 +08:00
|
|
|
return newFileName;
|
2013-01-24 00:32:22 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
std::string FileUtils::getPathForFilename(const std::string& filename, const std::string& resolutionDirectory, const std::string& searchPath)
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
2013-02-01 11:53:05 +08:00
|
|
|
//CCLOG("getPathForFilename, fullPath = %s", path.c_str());
|
2013-02-01 11:20:46 +08:00
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-07 13:54:08 +08:00
|
|
|
std::string FileUtils::fullPathForFilename(const std::string &filename)
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
2014-04-28 14:53:30 +08:00
|
|
|
if (filename.empty())
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2013-07-26 06:53:24 +08:00
|
|
|
if (isAbsolutePath(filename))
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
2013-07-26 06:53:24 +08:00
|
|
|
return filename;
|
2013-02-01 11:20:46 +08:00
|
|
|
}
|
2014-04-02 16:33:05 +08:00
|
|
|
|
|
|
|
// 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) );
|
2013-02-01 11:20:46 +08:00
|
|
|
|
2014-01-08 05:02:31 +08:00
|
|
|
std::string fullpath;
|
2013-02-01 11:20:46 +08:00
|
|
|
|
2014-04-02 16:33:05 +08:00
|
|
|
for (auto searchIt = _searchPathArray.cbegin(); searchIt != _searchPathArray.cend(); ++searchIt)
|
2014-04-02 15:35:09 +08:00
|
|
|
{
|
2014-04-02 16:33:05 +08:00
|
|
|
for (auto resolutionIt = _searchResolutionsOrderArray.cbegin(); resolutionIt != _searchResolutionsOrderArray.cend(); ++resolutionIt)
|
|
|
|
{
|
|
|
|
fullpath = this->getPathForFilename(newFilename, *resolutionIt, *searchIt);
|
|
|
|
|
|
|
|
if (fullpath.length() > 0)
|
|
|
|
{
|
|
|
|
// Using the filename passed in as key.
|
|
|
|
_fullPathCache.insert(std::make_pair(filename, fullpath));
|
|
|
|
return fullpath;
|
|
|
|
}
|
|
|
|
}
|
2013-02-01 11:20:46 +08:00
|
|
|
}
|
|
|
|
|
2013-09-07 13:54:08 +08:00
|
|
|
CCLOG("cocos2d: fullPathForFilename: No file found at %s. Possible missing file.", filename.c_str());
|
2013-05-13 21:48:25 +08:00
|
|
|
|
2013-09-07 06:33:28 +08:00
|
|
|
// XXX: Should it return nullptr ? or an empty string ?
|
2013-02-01 11:20:46 +08:00
|
|
|
// The file wasn't found, return the file name passed in.
|
2013-07-26 06:53:24 +08:00
|
|
|
return filename;
|
2013-02-01 11:20:46 +08:00
|
|
|
}
|
|
|
|
|
2013-09-07 13:54:08 +08:00
|
|
|
std::string FileUtils::fullPathFromRelativeFile(const std::string &filename, const std::string &relativeFile)
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
2013-09-07 09:46:33 +08:00
|
|
|
return relativeFile.substr(0, relativeFile.rfind('/')+1) + getNewFilename(filename);
|
2013-02-01 11:20:46 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void FileUtils::setSearchResolutionsOrder(const std::vector<std::string>& searchResolutionsOrder)
|
2013-01-24 00:32:22 +08:00
|
|
|
{
|
2013-09-07 09:46:33 +08:00
|
|
|
bool existDefault = false;
|
2013-07-10 04:21:43 +08:00
|
|
|
_fullPathCache.clear();
|
2013-06-15 14:03:30 +08:00
|
|
|
_searchResolutionsOrderArray.clear();
|
2014-01-08 05:02:31 +08:00
|
|
|
for(auto iter = searchResolutionsOrder.cbegin(); iter != searchResolutionsOrder.cend(); ++iter)
|
2013-01-28 17:44:53 +08:00
|
|
|
{
|
2013-02-01 18:48:44 +08:00
|
|
|
std::string resolutionDirectory = *iter;
|
2013-09-07 09:46:33 +08:00
|
|
|
if (!existDefault && resolutionDirectory == "")
|
2013-01-28 17:44:53 +08:00
|
|
|
{
|
2013-09-07 09:46:33 +08:00
|
|
|
existDefault = true;
|
2013-01-28 17:44:53 +08:00
|
|
|
}
|
2013-02-01 18:48:44 +08:00
|
|
|
|
|
|
|
if (resolutionDirectory.length() > 0 && resolutionDirectory[resolutionDirectory.length()-1] != '/')
|
|
|
|
{
|
|
|
|
resolutionDirectory += "/";
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_searchResolutionsOrderArray.push_back(resolutionDirectory);
|
2013-01-28 17:44:53 +08:00
|
|
|
}
|
2013-09-07 09:46:33 +08:00
|
|
|
if (!existDefault)
|
2013-01-28 17:44:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_searchResolutionsOrderArray.push_back("");
|
2013-01-28 17:44:53 +08:00
|
|
|
}
|
2013-01-24 00:32:22 +08:00
|
|
|
}
|
|
|
|
|
2013-09-07 13:54:08 +08:00
|
|
|
void FileUtils::addSearchResolutionsOrder(const std::string &order)
|
2013-02-04 12:41:24 +08:00
|
|
|
{
|
2014-03-19 17:56:29 +08:00
|
|
|
std::string resOrder = order;
|
|
|
|
if (!resOrder.empty() && resOrder[resOrder.length()-1] != '/')
|
|
|
|
resOrder.append("/");
|
|
|
|
|
|
|
|
_searchResolutionsOrderArray.push_back(resOrder);
|
2013-02-04 12:41:24 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
const std::vector<std::string>& FileUtils::getSearchResolutionsOrder()
|
2013-01-24 00:32:22 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _searchResolutionsOrderArray;
|
2013-01-24 00:32:22 +08:00
|
|
|
}
|
|
|
|
|
2013-09-07 13:54:08 +08:00
|
|
|
const std::vector<std::string>& FileUtils::getSearchPaths() const
|
2013-01-24 00:32:22 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _searchPathArray;
|
2013-01-24 00:32:22 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void FileUtils::setSearchPaths(const std::vector<std::string>& searchPaths)
|
2012-08-09 12:49:33 +08:00
|
|
|
{
|
2013-09-07 09:46:33 +08:00
|
|
|
bool existDefaultRootPath = false;
|
2013-02-01 11:20:46 +08:00
|
|
|
|
2013-07-10 04:21:43 +08:00
|
|
|
_fullPathCache.clear();
|
2013-06-15 14:03:30 +08:00
|
|
|
_searchPathArray.clear();
|
2014-01-08 05:02:31 +08:00
|
|
|
for (auto iter = searchPaths.cbegin(); iter != searchPaths.cend(); ++iter)
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
std::string prefix;
|
2013-02-01 11:20:46 +08:00
|
|
|
std::string path;
|
2013-09-07 09:46:33 +08:00
|
|
|
|
2013-02-01 11:20:46 +08:00
|
|
|
if (!isAbsolutePath(*iter))
|
|
|
|
{ // Not an absolute path
|
2013-12-18 17:47:20 +08:00
|
|
|
prefix = _defaultResRootPath;
|
2013-02-01 11:20:46 +08:00
|
|
|
}
|
2013-12-18 17:47:20 +08:00
|
|
|
path = prefix + (*iter);
|
2013-02-01 11:20:46 +08:00
|
|
|
if (path.length() > 0 && path[path.length()-1] != '/')
|
|
|
|
{
|
|
|
|
path += "/";
|
|
|
|
}
|
2013-09-07 09:46:33 +08:00
|
|
|
if (!existDefaultRootPath && path == _defaultResRootPath)
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
2013-09-07 09:46:33 +08:00
|
|
|
existDefaultRootPath = true;
|
2013-02-01 11:20:46 +08:00
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
_searchPathArray.push_back(path);
|
2013-02-01 11:20:46 +08:00
|
|
|
}
|
|
|
|
|
2013-09-07 09:46:33 +08:00
|
|
|
if (!existDefaultRootPath)
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
2013-02-01 11:53:05 +08:00
|
|
|
//CCLOG("Default root path doesn't exist, adding it.");
|
2013-06-15 14:03:30 +08:00
|
|
|
_searchPathArray.push_back(_defaultResRootPath);
|
2013-02-01 11:20:46 +08:00
|
|
|
}
|
2012-08-09 12:49:33 +08:00
|
|
|
}
|
|
|
|
|
2013-09-07 13:54:08 +08:00
|
|
|
void FileUtils::addSearchPath(const std::string &searchpath)
|
2013-02-04 12:41:24 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
std::string prefix;
|
2013-09-07 13:54:08 +08:00
|
|
|
if (!isAbsolutePath(searchpath))
|
2013-12-18 17:47:20 +08:00
|
|
|
prefix = _defaultResRootPath;
|
2013-09-07 13:54:08 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
std::string path = prefix + searchpath;
|
2013-02-04 12:41:24 +08:00
|
|
|
if (path.length() > 0 && path[path.length()-1] != '/')
|
|
|
|
{
|
|
|
|
path += "/";
|
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
_searchPathArray.push_back(path);
|
2013-02-04 12:41:24 +08:00
|
|
|
}
|
|
|
|
|
2013-12-04 17:46:57 +08:00
|
|
|
void FileUtils::setFilenameLookupDictionary(const ValueMap& filenameLookupDict)
|
2013-01-18 18:05:32 +08:00
|
|
|
{
|
2013-07-10 04:21:43 +08:00
|
|
|
_fullPathCache.clear();
|
2013-12-03 14:47:35 +08:00
|
|
|
_filenameLookupDict = filenameLookupDict;
|
2013-01-18 18:05:32 +08:00
|
|
|
}
|
|
|
|
|
2013-09-07 13:54:08 +08:00
|
|
|
void FileUtils::loadFilenameLookupDictionaryFromFile(const std::string &filename)
|
2013-01-25 20:51:52 +08:00
|
|
|
{
|
2014-01-08 05:02:31 +08:00
|
|
|
const std::string fullPath = fullPathForFilename(filename);
|
2013-01-25 20:51:52 +08:00
|
|
|
if (fullPath.length() > 0)
|
|
|
|
{
|
2013-12-04 17:50:57 +08:00
|
|
|
ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(fullPath);
|
2013-12-03 14:47:35 +08:00
|
|
|
if (!dict.empty())
|
2013-01-25 20:51:52 +08:00
|
|
|
{
|
2013-12-04 17:46:57 +08:00
|
|
|
ValueMap& metadata = dict["metadata"].asValueMap();
|
2013-12-03 14:47:35 +08:00
|
|
|
int version = metadata["version"].asInt();
|
2013-01-25 20:51:52 +08:00
|
|
|
if (version != 1)
|
|
|
|
{
|
2013-12-05 17:19:01 +08:00
|
|
|
CCLOG("cocos2d: ERROR: Invalid filenameLookup dictionary version: %d. Filename: %s", version, filename.c_str());
|
2013-01-25 20:51:52 +08:00
|
|
|
return;
|
|
|
|
}
|
2013-12-04 17:46:57 +08:00
|
|
|
setFilenameLookupDictionary( dict["filenames"].asValueMap());
|
2013-01-25 20:51:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
std::string FileUtils::getFullPathForDirectoryAndFilename(const std::string& directory, const std::string& filename)
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
2013-08-21 01:22:44 +08:00
|
|
|
// get directory+filename, safely adding '/' as necessary
|
2013-12-18 17:47:20 +08:00
|
|
|
std::string ret = directory;
|
|
|
|
if (directory.size() && directory[directory.size()-1] != '/'){
|
2013-08-21 01:22:44 +08:00
|
|
|
ret += '/';
|
|
|
|
}
|
2013-12-18 17:47:20 +08:00
|
|
|
ret += filename;
|
2013-08-21 01:22:44 +08:00
|
|
|
|
|
|
|
// if the file doesn't exist, return an empty string
|
2014-04-02 15:35:09 +08:00
|
|
|
if (!isFileExistInternal(ret)) {
|
2013-02-01 11:20:46 +08:00
|
|
|
ret = "";
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-04-02 16:33:05 +08:00
|
|
|
bool FileUtils::isFileExist(const std::string& filename) const
|
2014-04-02 15:35:09 +08:00
|
|
|
{
|
|
|
|
// If filename is absolute path, we don't need to consider 'search paths' and 'resolution orders'.
|
|
|
|
if (isAbsolutePath(filename))
|
|
|
|
{
|
2014-04-02 16:33:05 +08:00
|
|
|
return isFileExistInternal(filename);
|
2014-04-02 15:35:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Already Cached ?
|
|
|
|
auto cacheIter = _fullPathCache.find(filename);
|
|
|
|
if( cacheIter != _fullPathCache.end() )
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the new file name.
|
|
|
|
const std::string newFilename( getNewFilename(filename) );
|
|
|
|
|
|
|
|
std::string fullpath;
|
|
|
|
|
2014-04-02 16:33:05 +08:00
|
|
|
for (auto searchIt = _searchPathArray.cbegin(); searchIt != _searchPathArray.cend(); ++searchIt)
|
|
|
|
{
|
|
|
|
for (auto resolutionIt = _searchResolutionsOrderArray.cbegin(); resolutionIt != _searchResolutionsOrderArray.cend(); ++resolutionIt)
|
|
|
|
{
|
2014-04-02 15:35:09 +08:00
|
|
|
fullpath = const_cast<FileUtils*>(this)->getPathForFilename(newFilename, *resolutionIt, *searchIt);
|
|
|
|
|
|
|
|
if (!fullpath.empty())
|
|
|
|
{
|
2014-04-02 16:33:05 +08:00
|
|
|
// Using the filename passed in as key.
|
|
|
|
const_cast<FileUtils*>(this)->_fullPathCache.insert(std::make_pair(filename, fullpath));
|
2014-04-02 15:35:09 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
bool FileUtils::isAbsolutePath(const std::string& path) const
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
return (path[0] == '/');
|
2013-02-01 11:20:46 +08:00
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Notification support when getFileData from invalid file path.
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
2013-09-07 09:46:33 +08:00
|
|
|
static bool s_popupNotify = true;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-09-07 09:46:33 +08:00
|
|
|
void FileUtils::setPopupNotify(bool notify)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-09-07 09:46:33 +08:00
|
|
|
s_popupNotify = notify;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool FileUtils::isPopupNotify()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-09-07 09:46:33 +08:00
|
|
|
return s_popupNotify;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|