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"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "cocoa/CCDictionary.h"
|
|
|
|
#include "cocoa/CCString.h"
|
|
|
|
#include "CCSAXParser.h"
|
2013-04-13 16:55:24 +08:00
|
|
|
#include "support/tinyxml2/tinyxml2.h"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "support/zip_support/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-06-20 14:13:12 +08:00
|
|
|
/* The subclass FileUtilsIOS and FileUtilsMac should override these two method. */
|
|
|
|
Dictionary* FileUtils::createDictionaryWithContentsOfFile(const std::string& filename) {return NULL;}
|
|
|
|
bool FileUtils::writeToFile(cocos2d::Dictionary *dict, const std::string &fullPath) {return NULL;}
|
|
|
|
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
|
|
|
// XXX: deprecated
|
|
|
|
FileUtils* FileUtils::sharedFileUtils()
|
|
|
|
{
|
|
|
|
return FileUtils::getInstance();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileUtils::destroyInstance()
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(s_sharedFileUtils);
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX: deprecated
|
2013-06-20 14:13:12 +08:00
|
|
|
void FileUtils::purgeFileUtils()
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
2013-07-12 15:01:51 +08:00
|
|
|
FileUtils::destroyInstance();
|
2013-02-01 11:20:46 +08:00
|
|
|
}
|
|
|
|
|
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-07-26 06:53:24 +08:00
|
|
|
unsigned char* FileUtils::getFileData(const char* filename, const char* pszMode, unsigned long * pSize)
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
|
|
|
unsigned char * pBuffer = NULL;
|
2013-07-26 06:53:24 +08:00
|
|
|
CCASSERT(filename != NULL && pSize != NULL && pszMode != NULL, "Invalid parameters.");
|
2013-02-01 11:20:46 +08:00
|
|
|
*pSize = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
// read the file from hardware
|
2013-07-26 06:53:24 +08:00
|
|
|
std::string fullPath = fullPathForFilename(filename);
|
2013-02-01 11:20:46 +08:00
|
|
|
FILE *fp = fopen(fullPath.c_str(), pszMode);
|
|
|
|
CC_BREAK_IF(!fp);
|
|
|
|
|
|
|
|
fseek(fp,0,SEEK_END);
|
|
|
|
*pSize = ftell(fp);
|
|
|
|
fseek(fp,0,SEEK_SET);
|
|
|
|
pBuffer = new unsigned char[*pSize];
|
|
|
|
*pSize = fread(pBuffer,sizeof(unsigned char), *pSize,fp);
|
|
|
|
fclose(fp);
|
|
|
|
} while (0);
|
|
|
|
|
2013-02-22 07:50:57 +08:00
|
|
|
if (! pBuffer)
|
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
|
|
|
}
|
|
|
|
return pBuffer;
|
|
|
|
}
|
|
|
|
|
2013-07-26 06:53:24 +08:00
|
|
|
unsigned char* FileUtils::getFileDataFromZip(const char* pszZipFilePath, const char* filename, unsigned long * pSize)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
unsigned char * pBuffer = NULL;
|
|
|
|
unzFile pFile = NULL;
|
|
|
|
*pSize = 0;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2013-07-26 06:53:24 +08:00
|
|
|
CC_BREAK_IF(!pszZipFilePath || !filename);
|
2012-04-19 14:35:52 +08:00
|
|
|
CC_BREAK_IF(strlen(pszZipFilePath) == 0);
|
|
|
|
|
|
|
|
pFile = unzOpen(pszZipFilePath);
|
|
|
|
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);
|
|
|
|
|
|
|
|
pBuffer = new unsigned char[FileInfo.uncompressed_size];
|
2013-03-03 10:32:09 +08:00
|
|
|
int CC_UNUSED nSize = unzReadCurrentFile(pFile, pBuffer, 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
|
|
|
|
|
|
|
*pSize = FileInfo.uncompressed_size;
|
|
|
|
unzCloseCurrentFile(pFile);
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
if (pFile)
|
|
|
|
{
|
|
|
|
unzClose(pFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pBuffer;
|
|
|
|
}
|
|
|
|
|
2013-07-26 06:53:24 +08:00
|
|
|
std::string FileUtils::getNewFilename(const char* filename)
|
2013-01-24 00:32:22 +08:00
|
|
|
{
|
|
|
|
const char* pszNewFileName = NULL;
|
|
|
|
// 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-07-26 06:53:24 +08:00
|
|
|
pszNewFileName = filename;
|
2013-01-24 00:32:22 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
pszNewFileName = fileNameFound->getCString();
|
2013-02-01 11:53:05 +08:00
|
|
|
//CCLOG("FOUND NEW FILE NAME: %s.", pszNewFileName);
|
2013-01-24 00:32:22 +08:00
|
|
|
}
|
|
|
|
return pszNewFileName;
|
|
|
|
}
|
|
|
|
|
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-07-26 06:53:24 +08:00
|
|
|
std::string FileUtils::fullPathForFilename(const char* filename)
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
2013-07-26 06:53:24 +08:00
|
|
|
CCASSERT(filename != NULL, "CCFileUtils: Invalid path");
|
2013-02-01 11:20:46 +08:00
|
|
|
|
2013-07-26 06:53:24 +08:00
|
|
|
std::string strFileName = filename;
|
|
|
|
if (isAbsolutePath(filename))
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
2013-07-26 06:53:24 +08:00
|
|
|
//CCLOG("Return absolute path( %s ) directly.", filename);
|
|
|
|
return filename;
|
2013-02-01 11:20:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Already Cached ?
|
2013-07-26 06:53:24 +08:00
|
|
|
std::map<std::string, std::string>::iterator cacheIter = _fullPathCache.find(filename);
|
2013-06-15 14:03:30 +08:00
|
|
|
if (cacheIter != _fullPathCache.end())
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
2013-02-01 11:53:05 +08:00
|
|
|
//CCLOG("Return full path from cache: %s", cacheIter->second.c_str());
|
2013-02-01 11:20:46 +08:00
|
|
|
return cacheIter->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the new file name.
|
2013-07-26 06:53:24 +08:00
|
|
|
std::string newFilename = getNewFilename(filename);
|
2013-02-01 11:20:46 +08:00
|
|
|
|
|
|
|
string fullpath = "";
|
|
|
|
|
2013-07-04 16:30:35 +08:00
|
|
|
for (auto searchPathsIter = _searchPathArray.begin();
|
2013-06-15 14:03:30 +08:00
|
|
|
searchPathsIter != _searchPathArray.end(); ++searchPathsIter) {
|
2013-07-04 16:30:35 +08:00
|
|
|
for (auto resOrderIter = _searchResolutionsOrderArray.begin();
|
2013-06-15 14:03:30 +08:00
|
|
|
resOrderIter != _searchResolutionsOrderArray.end(); ++resOrderIter) {
|
2013-02-01 11:20:46 +08:00
|
|
|
|
2013-08-12 11:06:51 +08:00
|
|
|
// CCLOG("SEARCHING: %s\n", std::string(*searchPathsIter + *resOrderIter + newFilename).c_str() );
|
2013-02-01 11:20:46 +08:00
|
|
|
|
|
|
|
fullpath = this->getPathForFilename(newFilename, *resOrderIter, *searchPathsIter);
|
|
|
|
|
|
|
|
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-08-12 11:06:51 +08:00
|
|
|
// CCLOG("Returning path: %s\n", fullpath.c_str());
|
2013-02-01 11:20:46 +08:00
|
|
|
return fullpath;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-26 06:53:24 +08:00
|
|
|
// CCLOG("cocos2d: fullPathForFilename: No file found at %s. Possible missing file.", filename);
|
2013-05-13 21:48:25 +08:00
|
|
|
|
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-07-26 06:53:24 +08:00
|
|
|
const char* FileUtils::fullPathFromRelativeFile(const char *filename, const char *pszRelativeFile)
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
|
|
|
std::string relativeFile = pszRelativeFile;
|
2013-06-20 14:13:12 +08:00
|
|
|
String *pRet = String::create("");
|
2013-06-15 14:03:30 +08:00
|
|
|
pRet->_string = relativeFile.substr(0, relativeFile.rfind('/')+1);
|
2013-07-26 06:53:24 +08:00
|
|
|
pRet->_string += getNewFilename(filename);
|
2013-02-01 11:20:46 +08:00
|
|
|
return pRet->getCString();
|
|
|
|
}
|
|
|
|
|
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-01-28 17:44:53 +08:00
|
|
|
bool bExistDefault = false;
|
2013-07-10 04:21:43 +08:00
|
|
|
_fullPathCache.clear();
|
2013-06-15 14:03:30 +08:00
|
|
|
_searchResolutionsOrderArray.clear();
|
2013-01-28 17:44:53 +08:00
|
|
|
for (std::vector<std::string>::const_iterator iter = searchResolutionsOrder.begin(); iter != searchResolutionsOrder.end(); ++iter)
|
|
|
|
{
|
2013-02-01 18:48:44 +08:00
|
|
|
std::string resolutionDirectory = *iter;
|
|
|
|
if (!bExistDefault && resolutionDirectory == "")
|
2013-01-28 17:44:53 +08:00
|
|
|
{
|
|
|
|
bExistDefault = true;
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
if (!bExistDefault)
|
|
|
|
{
|
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-06-20 14:13:12 +08:00
|
|
|
void FileUtils::addSearchResolutionsOrder(const char* 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-06-20 14:13:12 +08:00
|
|
|
const std::vector<std::string>& FileUtils::getSearchPaths()
|
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-02-01 11:20:46 +08:00
|
|
|
bool bExistDefaultRootPath = false;
|
|
|
|
|
2013-07-10 04:21:43 +08:00
|
|
|
_fullPathCache.clear();
|
2013-06-15 14:03:30 +08:00
|
|
|
_searchPathArray.clear();
|
2013-02-01 11:20:46 +08:00
|
|
|
for (std::vector<std::string>::const_iterator iter = searchPaths.begin(); iter != searchPaths.end(); ++iter)
|
|
|
|
{
|
|
|
|
std::string strPrefix;
|
|
|
|
std::string path;
|
|
|
|
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
|
|
|
}
|
|
|
|
path = strPrefix+(*iter);
|
|
|
|
if (path.length() > 0 && path[path.length()-1] != '/')
|
|
|
|
{
|
|
|
|
path += "/";
|
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
if (!bExistDefaultRootPath && path == _defaultResRootPath)
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
|
|
|
bExistDefaultRootPath = true;
|
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
_searchPathArray.push_back(path);
|
2013-02-01 11:20:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!bExistDefaultRootPath)
|
|
|
|
{
|
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-06-20 14:13:12 +08:00
|
|
|
void FileUtils::addSearchPath(const char* path_)
|
2013-02-04 12:41:24 +08:00
|
|
|
{
|
|
|
|
std::string strPrefix;
|
|
|
|
std::string path(path_);
|
|
|
|
if (!isAbsolutePath(path))
|
|
|
|
{ // Not an absolute path
|
2013-06-15 14:03:30 +08:00
|
|
|
strPrefix = _defaultResRootPath;
|
2013-02-04 12:41:24 +08:00
|
|
|
}
|
|
|
|
path = strPrefix + path;
|
|
|
|
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-06-20 14:13:12 +08:00
|
|
|
void FileUtils::loadFilenameLookupDictionaryFromFile(const char* filename)
|
2013-01-25 20:51:52 +08:00
|
|
|
{
|
|
|
|
std::string fullPath = this->fullPathForFilename(filename);
|
|
|
|
if (fullPath.length() > 0)
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Dictionary* pDict = Dictionary::createWithContentsOfFile(fullPath.c_str());
|
2013-01-25 20:51:52 +08:00
|
|
|
if (pDict)
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Dictionary* pMetadata = (Dictionary*)pDict->objectForKey("metadata");
|
|
|
|
int version = ((String*)pMetadata->objectForKey("version"))->intValue();
|
2013-01-25 20:51:52 +08:00
|
|
|
if (version != 1)
|
|
|
|
{
|
|
|
|
CCLOG("cocos2d: ERROR: Invalid filenameLookup dictionary version: %ld. Filename: %s", (long)version, filename);
|
|
|
|
return;
|
|
|
|
}
|
2013-06-20 14:13:12 +08:00
|
|
|
setFilenameLookupDictionary((Dictionary*)pDict->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-06-20 14:13:12 +08:00
|
|
|
bool FileUtils::isAbsolutePath(const std::string& strPath)
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
|
|
|
return strPath[0] == '/' ? true : false;
|
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Notification support when getFileData from invalid file path.
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
static bool s_bPopupNotify = true;
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void FileUtils::setPopupNotify(bool bNotify)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
s_bPopupNotify = bNotify;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool FileUtils::isPopupNotify()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
return s_bPopupNotify;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|