2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
2013-02-01 11:20:46 +08:00
|
|
|
Copyright (c) 2010-2013 cocos2d-x.org
|
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"
|
|
|
|
#include "CCDirector.h"
|
2013-10-14 14:01:00 +08:00
|
|
|
#include "CCDictionary.h"
|
|
|
|
#include "CCString.h"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "CCSAXParser.h"
|
2013-10-14 14:01:00 +08:00
|
|
|
#include "tinyxml2.h"
|
|
|
|
#include "unzip.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
#include <stack>
|
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;
|
|
|
|
Array* _rootArray;
|
|
|
|
Dictionary *_rootDict;
|
|
|
|
Dictionary *_curDict;
|
|
|
|
std::stack<Dictionary*> _dictStack;
|
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;
|
|
|
|
Array* _array;
|
2013-06-15 14:03:30 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
std::stack<Array*> _arrayStack;
|
|
|
|
std::stack<SAXState> _stateStack;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
public:
|
2013-06-20 14:13:12 +08:00
|
|
|
DictMaker()
|
2013-06-15 14:03:30 +08:00
|
|
|
: _resultType(SAX_RESULT_NONE),
|
|
|
|
_rootArray(NULL),
|
|
|
|
_rootDict(NULL),
|
|
|
|
_curDict(NULL),
|
|
|
|
_state(SAX_NONE),
|
|
|
|
_array(NULL)
|
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-06-20 14:13:12 +08:00
|
|
|
Dictionary* dictionaryWithContentsOfFile(const char *pFileName)
|
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
|
|
|
|
|
|
|
if (false == parser.init("UTF-8"))
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
parser.setDelegator(this);
|
|
|
|
|
|
|
|
parser.parse(pFileName);
|
2013-06-15 14:03:30 +08:00
|
|
|
return _rootDict;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Array* arrayWithContentsOfFile(const char* pFileName)
|
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
|
|
|
|
|
|
|
if (false == parser.init("UTF-8"))
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
parser.setDelegator(this);
|
|
|
|
|
|
|
|
parser.parse(pFileName);
|
2013-06-15 14:03:30 +08:00
|
|
|
return _array;
|
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);
|
|
|
|
std::string sName((char*)name);
|
|
|
|
if( sName == "dict" )
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
_curDict = new Dictionary();
|
2013-06-15 14:03:30 +08:00
|
|
|
if(_resultType == SAX_RESULT_DICT && _rootDict == NULL)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
// Because it will call _curDict->release() later, so retain here.
|
|
|
|
_rootDict = _curDict;
|
|
|
|
_rootDict->retain();
|
2012-04-19 14:35:52 +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)
|
|
|
|
{
|
|
|
|
// add the dictionary into the array
|
2013-06-15 14:03:30 +08:00
|
|
|
_array->addObject(_curDict);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else if (SAX_DICT == preState)
|
|
|
|
{
|
|
|
|
// add the dictionary into the pre dictionary
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(! _dictStack.empty(), "The state is wrong!");
|
2013-06-20 14:13:12 +08:00
|
|
|
Dictionary* pPreDict = _dictStack.top();
|
2013-06-15 14:03:30 +08:00
|
|
|
pPreDict->setObject(_curDict, _curKey.c_str());
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_curDict->release();
|
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-06-20 14:13:12 +08:00
|
|
|
_array = new Array();
|
2013-08-22 10:15:47 +08:00
|
|
|
_array->init();
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_resultType == SAX_RESULT_ARRAY && _rootArray == NULL)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_rootArray = _array;
|
|
|
|
_rootArray->retain();
|
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-06-15 14:03:30 +08:00
|
|
|
_curDict->setObject(_array, _curKey.c_str());
|
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-06-20 14:13:12 +08:00
|
|
|
Array* pPreArray = _arrayStack.top();
|
2013-06-15 14:03:30 +08:00
|
|
|
pPreArray->addObject(_array);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
_array->release();
|
2012-04-19 14:35:52 +08:00
|
|
|
// record the array state
|
2013-06-15 14:03:30 +08:00
|
|
|
_stateStack.push(_state);
|
|
|
|
_arrayStack.push(_array);
|
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();
|
2012-04-19 14:35:52 +08:00
|
|
|
std::string sName((char*)name);
|
|
|
|
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-06-15 14:03:30 +08:00
|
|
|
_array = _arrayStack.top();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (sName == "true")
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
String *str = new String("1");
|
2012-04-19 14:35:52 +08:00
|
|
|
if (SAX_ARRAY == curState)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_array->addObject(str);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else if (SAX_DICT == curState)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_curDict->setObject(str, _curKey.c_str());
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
str->release();
|
|
|
|
}
|
|
|
|
else if (sName == "false")
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
String *str = new String("0");
|
2012-04-19 14:35:52 +08:00
|
|
|
if (SAX_ARRAY == curState)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_array->addObject(str);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else if (SAX_DICT == curState)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_curDict->setObject(str, _curKey.c_str());
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
str->release();
|
|
|
|
}
|
2012-04-25 10:55:59 +08:00
|
|
|
else if (sName == "string" || sName == "integer" || sName == "real")
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
String* pStrValue = new String(_curValue);
|
2012-04-25 10:55:59 +08:00
|
|
|
|
|
|
|
if (SAX_ARRAY == curState)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_array->addObject(pStrValue);
|
2012-04-25 10:55:59 +08:00
|
|
|
}
|
|
|
|
else if (SAX_DICT == curState)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_curDict->setObject(pStrValue, _curKey.c_str());
|
2012-04-25 10:55:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pStrValue->release();
|
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();
|
|
|
|
String *pText = new String(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-06-15 14:03:30 +08:00
|
|
|
_curKey = pText->getCString();
|
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-06-15 14:03:30 +08:00
|
|
|
_curValue.append(pText->getCString());
|
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;
|
|
|
|
}
|
|
|
|
pText->release();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Dictionary* FileUtils::createDictionaryWithContentsOfFile(const std::string& filename)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-03-30 13:51:25 +08:00
|
|
|
std::string fullPath = fullPathForFilename(filename.c_str());
|
2013-06-20 14:13:12 +08:00
|
|
|
DictMaker tMaker;
|
2013-03-30 13:51:25 +08:00
|
|
|
return tMaker.dictionaryWithContentsOfFile(fullPath.c_str());
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Array* FileUtils::createArrayWithContentsOfFile(const std::string& filename)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-03-30 13:51:25 +08:00
|
|
|
std::string fullPath = fullPathForFilename(filename.c_str());
|
2013-06-20 14:13:12 +08:00
|
|
|
DictMaker tMaker;
|
2013-03-30 13:51:25 +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-06-20 14:13:12 +08:00
|
|
|
static tinyxml2::XMLElement* generateElementForArray(cocos2d::Array *array, tinyxml2::XMLDocument *pDoc);
|
|
|
|
static tinyxml2::XMLElement* generateElementForDict(cocos2d::Dictionary *dict, tinyxml2::XMLDocument *pDoc);
|
2013-04-16 12:34:09 +08:00
|
|
|
|
2013-04-13 16:55:24 +08:00
|
|
|
/*
|
|
|
|
* Use tinyxml2 to write plist files
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
bool FileUtils::writeToFile(cocos2d::Dictionary *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-04-13 16:55:24 +08:00
|
|
|
tinyxml2::XMLDocument *pDoc = new tinyxml2::XMLDocument();
|
|
|
|
if (NULL == pDoc)
|
|
|
|
return false;
|
|
|
|
|
2013-04-16 12:34:09 +08:00
|
|
|
tinyxml2::XMLDeclaration *pDeclaration = pDoc->NewDeclaration("xml version=\"1.0\" encoding=\"UTF-8\"");
|
|
|
|
if (NULL == pDeclaration)
|
2013-04-28 00:36:37 +08:00
|
|
|
{
|
|
|
|
delete pDoc;
|
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-04-16 12:34:09 +08:00
|
|
|
pDoc->LinkEndChild(pDeclaration);
|
2013-04-13 16:55:24 +08:00
|
|
|
tinyxml2::XMLElement *docType = pDoc->NewElement("!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\"");
|
|
|
|
pDoc->LinkEndChild(docType);
|
|
|
|
|
2013-04-16 12:34:09 +08:00
|
|
|
tinyxml2::XMLElement *pRootEle = pDoc->NewElement("plist");
|
2013-04-13 16:55:24 +08:00
|
|
|
pRootEle->SetAttribute("version", "1.0");
|
2013-04-16 12:34:09 +08:00
|
|
|
if (NULL == pRootEle)
|
2013-04-28 00:36:37 +08:00
|
|
|
{
|
|
|
|
delete pDoc;
|
2013-04-16 12:34:09 +08:00
|
|
|
return false;
|
2013-04-28 00:36:37 +08:00
|
|
|
}
|
2013-04-16 12:34:09 +08:00
|
|
|
pDoc->LinkEndChild(pRootEle);
|
2013-04-13 16:55:24 +08:00
|
|
|
|
|
|
|
tinyxml2::XMLElement *innerDict = generateElementForDict(dict, pDoc);
|
|
|
|
if (NULL == innerDict )
|
2013-04-28 00:36:37 +08:00
|
|
|
{
|
|
|
|
delete pDoc;
|
2013-04-13 16:55:24 +08:00
|
|
|
return false;
|
2013-04-28 00:36:37 +08:00
|
|
|
}
|
2013-04-13 16:55:24 +08:00
|
|
|
pRootEle->LinkEndChild(innerDict);
|
|
|
|
|
2013-04-28 00:36:37 +08:00
|
|
|
bool bRet = tinyxml2::XML_SUCCESS == pDoc->SaveFile(fullPath.c_str());
|
2013-04-13 16:55:24 +08:00
|
|
|
|
2013-04-28 00:36:37 +08:00
|
|
|
delete pDoc;
|
2013-04-13 16:55:24 +08:00
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
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-06-20 14:13:12 +08:00
|
|
|
static tinyxml2::XMLElement* generateElementForObject(cocos2d::Object *object, tinyxml2::XMLDocument *pDoc)
|
2013-04-16 12:34:09 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
// object is String
|
|
|
|
if (String *str = dynamic_cast<String *>(object))
|
2013-04-16 12:34:09 +08:00
|
|
|
{
|
2013-04-28 00:36:37 +08:00
|
|
|
tinyxml2::XMLElement* node = pDoc->NewElement("string");
|
2013-04-16 12:34:09 +08:00
|
|
|
tinyxml2::XMLText* content = pDoc->NewText(str->getCString());
|
2013-04-28 00:36:37 +08:00
|
|
|
node->LinkEndChild(content);
|
|
|
|
return node;
|
2013-04-16 12:34:09 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// object is Array
|
|
|
|
if (Array *array = dynamic_cast<Array *>(object))
|
2013-04-28 00:36:37 +08:00
|
|
|
return generateElementForArray(array, pDoc);
|
2013-04-16 12:34:09 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// object is Dictionary
|
|
|
|
if (Dictionary *innerDict = dynamic_cast<Dictionary *>(object))
|
2013-04-28 00:36:37 +08:00
|
|
|
return generateElementForDict(innerDict, pDoc);
|
2013-04-16 12:34:09 +08:00
|
|
|
|
2013-04-28 00:36:37 +08:00
|
|
|
CCLOG("This type cannot appear in property list");
|
|
|
|
return NULL;
|
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-06-20 14:13:12 +08:00
|
|
|
static tinyxml2::XMLElement* generateElementForDict(cocos2d::Dictionary *dict, tinyxml2::XMLDocument *pDoc)
|
2013-04-13 16:55:24 +08:00
|
|
|
{
|
|
|
|
tinyxml2::XMLElement* rootNode = pDoc->NewElement("dict");
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
DictElement *dictElement = NULL;
|
2013-04-16 12:34:09 +08:00
|
|
|
CCDICT_FOREACH(dict, dictElement)
|
2013-04-13 16:55:24 +08:00
|
|
|
{
|
|
|
|
tinyxml2::XMLElement* tmpNode = pDoc->NewElement("key");
|
|
|
|
rootNode->LinkEndChild(tmpNode);
|
2013-04-16 12:34:09 +08:00
|
|
|
tinyxml2::XMLText* content = pDoc->NewText(dictElement->getStrKey());
|
2013-04-13 16:55:24 +08:00
|
|
|
tmpNode->LinkEndChild(content);
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Object *object = dictElement->getObject();
|
2013-04-16 12:34:09 +08:00
|
|
|
tinyxml2::XMLElement *element = generateElementForObject(object, pDoc);
|
|
|
|
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-06-20 14:13:12 +08:00
|
|
|
static tinyxml2::XMLElement* generateElementForArray(cocos2d::Array *array, tinyxml2::XMLDocument *pDoc)
|
2013-04-16 12:34:09 +08:00
|
|
|
{
|
|
|
|
tinyxml2::XMLElement* rootNode = pDoc->NewElement("array");
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Object *object = NULL;
|
2013-04-16 12:34:09 +08:00
|
|
|
CCARRAY_FOREACH(array, object)
|
|
|
|
{
|
|
|
|
tinyxml2::XMLElement *element = generateElementForObject(object, pDoc);
|
|
|
|
if (element)
|
|
|
|
rootNode->LinkEndChild(element);
|
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-06-20 14:13:12 +08:00
|
|
|
Dictionary* FileUtils::createDictionaryWithContentsOfFile(const std::string& filename) {return NULL;}
|
2013-11-05 08:31:36 +08:00
|
|
|
bool FileUtils::writeToFile(cocos2d::Dictionary *dict, const std::string &fullPath) {return false;}
|
2013-06-20 14:13:12 +08:00
|
|
|
Array* FileUtils::createArrayWithContentsOfFile(const std::string& filename) {return NULL;}
|
2013-04-16 12:34:09 +08:00
|
|
|
|
|
|
|
#endif /* (CC_TARGET_PLATFORM != CC_PLATFORM_IOS) && (CC_TARGET_PLATFORM != CC_PLATFORM_MAC) */
|
|
|
|
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
FileUtils* FileUtils::s_sharedFileUtils = NULL;
|
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-06-15 14:03:30 +08:00
|
|
|
: _filenameLookupDict(NULL)
|
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-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_filenameLookupDict);
|
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-11-06 09:36:44 +08:00
|
|
|
unsigned char* FileUtils::getFileData(const char* filename, const char* mode, long *size)
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
2013-09-07 09:46:33 +08:00
|
|
|
unsigned char * buffer = NULL;
|
|
|
|
CCASSERT(filename != NULL && size != NULL && mode != NULL, "Invalid parameters.");
|
|
|
|
*size = 0;
|
2013-02-01 11:20:46 +08:00
|
|
|
do
|
|
|
|
{
|
|
|
|
// read the file from hardware
|
2013-07-26 06:53:24 +08:00
|
|
|
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-11-06 09:36:44 +08:00
|
|
|
unsigned char* FileUtils::getFileDataFromZip(const char* zipFilePath, const char* filename, long *size)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-09-07 09:46:33 +08:00
|
|
|
unsigned char * buffer = NULL;
|
2012-04-19 14:35:52 +08:00
|
|
|
unzFile pFile = NULL;
|
2013-09-07 09:46:33 +08:00
|
|
|
*size = 0;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2013-09-07 13:54:08 +08:00
|
|
|
CC_BREAK_IF(!zipFilePath || !filename);
|
|
|
|
CC_BREAK_IF(strlen(zipFilePath) == 0);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-09-07 13:54:08 +08:00
|
|
|
pFile = unzOpen(zipFilePath);
|
2012-04-19 14:35:52 +08:00
|
|
|
CC_BREAK_IF(!pFile);
|
|
|
|
|
2013-07-26 06:53:24 +08:00
|
|
|
int nRet = unzLocateFile(pFile, filename, 1);
|
2012-04-19 14:35:52 +08:00
|
|
|
CC_BREAK_IF(UNZ_OK != nRet);
|
|
|
|
|
|
|
|
char szFilePathA[260];
|
|
|
|
unz_file_info FileInfo;
|
|
|
|
nRet = unzGetCurrentFileInfo(pFile, &FileInfo, szFilePathA, sizeof(szFilePathA), NULL, 0, NULL, 0);
|
|
|
|
CC_BREAK_IF(UNZ_OK != nRet);
|
|
|
|
|
|
|
|
nRet = unzOpenCurrentFile(pFile);
|
|
|
|
CC_BREAK_IF(UNZ_OK != nRet);
|
|
|
|
|
2013-11-12 10:09:47 +08:00
|
|
|
buffer = (unsigned char*)malloc(FileInfo.uncompressed_size);
|
2013-09-07 09:46:33 +08:00
|
|
|
int CC_UNUSED nSize = unzReadCurrentFile(pFile, buffer, FileInfo.uncompressed_size);
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(nSize == 0 || nSize == (int)FileInfo.uncompressed_size, "the file size is wrong");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-09-07 09:46:33 +08:00
|
|
|
*size = FileInfo.uncompressed_size;
|
2012-04-19 14:35:52 +08:00
|
|
|
unzCloseCurrentFile(pFile);
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
if (pFile)
|
|
|
|
{
|
|
|
|
unzClose(pFile);
|
|
|
|
}
|
|
|
|
|
2013-09-07 09:46:33 +08:00
|
|
|
return buffer;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-09-07 13:54:08 +08:00
|
|
|
std::string FileUtils::getNewFilename(const std::string &filename)
|
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-07-26 06:53:24 +08:00
|
|
|
String* fileNameFound = _filenameLookupDict ? (String*)_filenameLookupDict->objectForKey(filename) : NULL;
|
2013-01-24 00:32:22 +08:00
|
|
|
if( NULL == fileNameFound || fileNameFound->length() == 0) {
|
2013-09-07 06:33:28 +08:00
|
|
|
newFileName = filename;
|
2013-01-24 00:32:22 +08:00
|
|
|
}
|
|
|
|
else {
|
2013-09-07 06:33:28 +08:00
|
|
|
newFileName = fileNameFound->getCString();
|
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
|
|
|
{
|
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
|
|
|
}
|
2013-09-07 09:46:33 +08:00
|
|
|
|
2013-02-01 11:20:46 +08:00
|
|
|
// Already Cached ?
|
2013-09-07 06:33:28 +08:00
|
|
|
auto cacheIter = _fullPathCache.find(filename);
|
2013-09-07 09:46:33 +08:00
|
|
|
if( cacheIter != _fullPathCache.end() )
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
|
|
|
return cacheIter->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the new file name.
|
2013-09-07 09:46:33 +08:00
|
|
|
std::string newFilename( getNewFilename(filename) );
|
2013-02-01 11:20:46 +08:00
|
|
|
|
|
|
|
string fullpath = "";
|
|
|
|
|
2013-09-07 06:33:28 +08:00
|
|
|
for (auto searchIt = _searchPathArray.begin(); searchIt != _searchPathArray.end(); ++searchIt) {
|
|
|
|
for (auto resolutionIt = _searchResolutionsOrderArray.begin(); resolutionIt != _searchResolutionsOrderArray.end(); ++resolutionIt) {
|
2013-02-01 11:20:46 +08:00
|
|
|
|
2013-09-07 06:33:28 +08:00
|
|
|
fullpath = this->getPathForFilename(newFilename, *resolutionIt, *searchIt);
|
2013-02-01 11:20:46 +08:00
|
|
|
|
|
|
|
if (fullpath.length() > 0)
|
|
|
|
{
|
|
|
|
// Using the filename passed in as key.
|
2013-07-26 06:53:24 +08:00
|
|
|
_fullPathCache.insert(std::pair<std::string, std::string>(filename, fullpath));
|
2013-02-01 11:20:46 +08:00
|
|
|
return fullpath;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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();
|
2013-09-07 09:46:33 +08:00
|
|
|
for(auto iter = searchResolutionsOrder.begin(); iter != searchResolutionsOrder.end(); ++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
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_searchResolutionsOrderArray.push_back(order);
|
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();
|
2013-09-07 09:46:33 +08:00
|
|
|
for (auto iter = searchPaths.begin(); iter != searchPaths.end(); ++iter)
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
|
|
|
std::string strPrefix;
|
|
|
|
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-06-15 14:03:30 +08:00
|
|
|
strPrefix = _defaultResRootPath;
|
2013-02-01 11:20:46 +08:00
|
|
|
}
|
2013-09-07 09:46:33 +08:00
|
|
|
path = strPrefix + (*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
|
|
|
{
|
|
|
|
std::string strPrefix;
|
2013-09-07 13:54:08 +08:00
|
|
|
if (!isAbsolutePath(searchpath))
|
2013-06-15 14:03:30 +08:00
|
|
|
strPrefix = _defaultResRootPath;
|
2013-09-07 13:54:08 +08:00
|
|
|
|
|
|
|
std::string path = strPrefix + 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-06-20 14:13:12 +08:00
|
|
|
void FileUtils::setFilenameLookupDictionary(Dictionary* pFilenameLookupDict)
|
2013-01-18 18:05:32 +08:00
|
|
|
{
|
2013-07-10 04:21:43 +08:00
|
|
|
_fullPathCache.clear();
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_filenameLookupDict);
|
|
|
|
_filenameLookupDict = pFilenameLookupDict;
|
|
|
|
CC_SAFE_RETAIN(_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
|
|
|
{
|
2013-09-07 13:54:08 +08:00
|
|
|
std::string fullPath = fullPathForFilename(filename);
|
2013-01-25 20:51:52 +08:00
|
|
|
if (fullPath.length() > 0)
|
|
|
|
{
|
2013-09-07 09:46:33 +08:00
|
|
|
Dictionary* dict = Dictionary::createWithContentsOfFile(fullPath.c_str());
|
|
|
|
if (dict)
|
2013-01-25 20:51:52 +08:00
|
|
|
{
|
2013-09-07 09:46:33 +08:00
|
|
|
Dictionary* metadata = static_cast<Dictionary*>( dict->objectForKey("metadata") );
|
|
|
|
int version = static_cast<String*>( metadata->objectForKey("version"))->intValue();
|
2013-01-25 20:51:52 +08:00
|
|
|
if (version != 1)
|
|
|
|
{
|
2013-09-07 13:54:08 +08:00
|
|
|
CCLOG("cocos2d: ERROR: Invalid filenameLookup dictionary version: %ld. Filename: %s", (long)version, filename.c_str());
|
2013-01-25 20:51:52 +08:00
|
|
|
return;
|
|
|
|
}
|
2013-09-07 09:46:33 +08:00
|
|
|
setFilenameLookupDictionary( static_cast<Dictionary*>( dict->objectForKey("filenames")) );
|
2013-01-25 20:51:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
std::string FileUtils::getFullPathForDirectoryAndFilename(const std::string& strDirectory, const std::string& strFilename)
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
2013-08-21 01:22:44 +08:00
|
|
|
// get directory+filename, safely adding '/' as necessary
|
|
|
|
std::string ret = strDirectory;
|
|
|
|
if (strDirectory.size() && strDirectory[strDirectory.size()-1] != '/'){
|
|
|
|
ret += '/';
|
|
|
|
}
|
|
|
|
ret += strFilename;
|
|
|
|
|
|
|
|
// if the file doesn't exist, return an empty string
|
2013-02-01 11:20:46 +08:00
|
|
|
if (!isFileExist(ret)) {
|
|
|
|
ret = "";
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-09-07 13:54:08 +08:00
|
|
|
bool FileUtils::isAbsolutePath(const std::string& strPath) const
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
2013-09-07 09:46:33 +08:00
|
|
|
return (strPath[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
|
|
|
|
|