offer a thread safe function to read contents of xml file, and the engine use it internal

This commit is contained in:
minggo 2011-11-15 10:41:18 +08:00
parent 47b7419a1e
commit 5273819e83
4 changed files with 34 additions and 7 deletions

View File

@ -141,11 +141,15 @@ CCParticleSystem * CCParticleSystem::particleWithFile(const char *plistFile)
} }
bool CCParticleSystem::initWithFile(const char *plistFile) bool CCParticleSystem::initWithFile(const char *plistFile)
{ {
bool bRet = false;
m_sPlistFile = CCFileUtils::fullPathFromRelativePath(plistFile); m_sPlistFile = CCFileUtils::fullPathFromRelativePath(plistFile);
CCDictionary<std::string, CCObject*> *dict = CCFileUtils::dictionaryWithContentsOfFile(m_sPlistFile.c_str()); CCDictionary<std::string, CCObject*> *dict = CCFileUtils::dictionaryWithContentsOfFileThreadSafe(m_sPlistFile.c_str());
CCAssert( dict != NULL, "Particles: file not found"); CCAssert( dict != NULL, "Particles: file not found");
return this->initWithDictionary(dict); bRet = this->initWithDictionary(dict);
dict->release();
return bRet;
} }
bool CCParticleSystem::initWithDictionary(CCDictionary<std::string, CCObject*> *dictionary) bool CCParticleSystem::initWithDictionary(CCDictionary<std::string, CCObject*> *dictionary)

View File

@ -98,7 +98,9 @@ public:
m_pCurDict = new CCDictionary<std::string, CCObject*>(); m_pCurDict = new CCDictionary<std::string, CCObject*>();
if(! m_pRootDict) if(! m_pRootDict)
{ {
// Because it will call m_pCurDict->release() later, so retain here.
m_pRootDict = m_pCurDict; m_pRootDict = m_pCurDict;
m_pRootDict->retain();
} }
m_tState = SAX_DICT; m_tState = SAX_DICT;
@ -120,7 +122,8 @@ public:
CCDictionary<std::string, CCObject*>* pPreDict = m_tDictStack.top(); CCDictionary<std::string, CCObject*>* pPreDict = m_tDictStack.top();
pPreDict->setObject(m_pCurDict, m_sCurKey); pPreDict->setObject(m_pCurDict, m_sCurKey);
} }
m_pCurDict->autorelease();
m_pCurDict->release();
// record the dict state // record the dict state
m_tStateStack.push(m_tState); m_tStateStack.push(m_tState);
@ -285,7 +288,15 @@ std::string& CCFileUtils::ccRemoveHDSuffixFromFile(std::string& path)
CCDictionary<std::string, CCObject*> *CCFileUtils::dictionaryWithContentsOfFile(const char *pFileName) CCDictionary<std::string, CCObject*> *CCFileUtils::dictionaryWithContentsOfFile(const char *pFileName)
{ {
CCDictMaker tMaker; CCDictionary<std::string, CCObject*> *ret = dictionaryWithContentsOfFileThreadSafe(pFileName);
ret->autorelease();
return ret;
}
CCDictionary<std::string, CCObject*> *CCFileUtils::dictionaryWithContentsOfFileThreadSafe(const char *pFileName)
{
CCDictMaker tMaker;
return tMaker.dictionaryWithContentsOfFile(pFileName); return tMaker.dictionaryWithContentsOfFile(pFileName);
} }

View File

@ -89,6 +89,12 @@ public:
*/ */
static CCDictionary<std::string, CCObject*> *dictionaryWithContentsOfFile(const char *pFileName); static CCDictionary<std::string, CCObject*> *dictionaryWithContentsOfFile(const char *pFileName);
/*
@brief The same meaning as dictionaryWithContentsOfFile(), but it doesn't call autorelease, so the
invoker should call release().
*/
static CCDictionary<std::string, CCObject*> *dictionaryWithContentsOfFileThreadSafe(const char *pFileName);
/** /**
@brief Get the writeable path @brief Get the writeable path
@return The path that can write/read file @return The path that can write/read file

View File

@ -197,9 +197,11 @@ void CCSpriteFrameCache::addSpriteFramesWithDictionary(CCDictionary<std::string,
void CCSpriteFrameCache::addSpriteFramesWithFile(const char *pszPlist, CCTexture2D *pobTexture) void CCSpriteFrameCache::addSpriteFramesWithFile(const char *pszPlist, CCTexture2D *pobTexture)
{ {
const char *pszPath = CCFileUtils::fullPathFromRelativePath(pszPlist); const char *pszPath = CCFileUtils::fullPathFromRelativePath(pszPlist);
CCDictionary<std::string, CCObject*> *dict = CCFileUtils::dictionaryWithContentsOfFile(pszPath); CCDictionary<std::string, CCObject*> *dict = CCFileUtils::dictionaryWithContentsOfFileThreadSafe(pszPath);
addSpriteFramesWithDictionary(dict, pobTexture); addSpriteFramesWithDictionary(dict, pobTexture);
dict->release();
} }
void CCSpriteFrameCache::addSpriteFramesWithFile(const char* plist, const char* textureFileName) void CCSpriteFrameCache::addSpriteFramesWithFile(const char* plist, const char* textureFileName)
@ -220,7 +222,7 @@ void CCSpriteFrameCache::addSpriteFramesWithFile(const char* plist, const char*
void CCSpriteFrameCache::addSpriteFramesWithFile(const char *pszPlist) void CCSpriteFrameCache::addSpriteFramesWithFile(const char *pszPlist)
{ {
const char *pszPath = CCFileUtils::fullPathFromRelativePath(pszPlist); const char *pszPath = CCFileUtils::fullPathFromRelativePath(pszPlist);
CCDictionary<std::string, CCObject*> *dict = CCFileUtils::dictionaryWithContentsOfFile(pszPath); CCDictionary<std::string, CCObject*> *dict = CCFileUtils::dictionaryWithContentsOfFileThreadSafe(pszPath);
string texturePath(""); string texturePath("");
@ -261,6 +263,8 @@ void CCSpriteFrameCache::addSpriteFramesWithFile(const char *pszPlist)
{ {
CCLOG("cocos2d: CCSpriteFrameCache: Couldn't load texture"); CCLOG("cocos2d: CCSpriteFrameCache: Couldn't load texture");
} }
dict->release();
} }
void CCSpriteFrameCache::addSpriteFrame(CCSpriteFrame *pobFrame, const char *pszFrameName) void CCSpriteFrameCache::addSpriteFrame(CCSpriteFrame *pobFrame, const char *pszFrameName)
@ -316,9 +320,11 @@ void CCSpriteFrameCache::removeSpriteFrameByName(const char *pszName)
void CCSpriteFrameCache::removeSpriteFramesFromFile(const char* plist) void CCSpriteFrameCache::removeSpriteFramesFromFile(const char* plist)
{ {
const char* path = CCFileUtils::fullPathFromRelativePath(plist); const char* path = CCFileUtils::fullPathFromRelativePath(plist);
CCDictionary<std::string, CCObject*>* dict = CCFileUtils::dictionaryWithContentsOfFile(path); CCDictionary<std::string, CCObject*>* dict = CCFileUtils::dictionaryWithContentsOfFileThreadSafe(path);
removeSpriteFramesFromDictionary((CCDictionary<std::string, CCSpriteFrame*>*)dict); removeSpriteFramesFromDictionary((CCDictionary<std::string, CCSpriteFrame*>*)dict);
dict->release();
} }
void CCSpriteFrameCache::removeSpriteFramesFromDictionary(CCDictionary<std::string, CCSpriteFrame*> *dictionary) void CCSpriteFrameCache::removeSpriteFramesFromDictionary(CCDictionary<std::string, CCSpriteFrame*> *dictionary)