diff --git a/cocos2dx/include/CCArray.h b/cocos2dx/include/CCArray.h index ec2400c8aa..24333ccaaf 100755 --- a/cocos2dx/include/CCArray.h +++ b/cocos2dx/include/CCArray.h @@ -74,6 +74,8 @@ class CC_DLL CCArray : public CCObject { public: ~CCArray(); + + /* static functions */ /** Create an array */ static CCArray* array(); /** Create an array with one object */ @@ -84,6 +86,18 @@ public: static CCArray* arrayWithCapacity(unsigned int capacity); /** Create an array with an existing array */ static CCArray* arrayWithArray(CCArray* otherArray); + /** + @brief Generate a CCArray pointer by file + @param pFileName The file name of *.plist file + @return The CCArray pointer generated from the file + */ + static CCArray* arrayWithContentsOfFile(const char* pFileName); + + /* + @brief The same meaning as arrayWithContentsOfFile(), but it doesn't call autorelease, so the + invoker should call release(). + */ + static CCArray* arrayWithContentsOfFileThreadSafe(const char* pFileName); /** Initializes an array */ bool init(); @@ -152,7 +166,9 @@ public: void replaceObjectAtIndex(unsigned int uIndex, CCObject* pObject, bool bReleaseObject = true); + /* override functions */ virtual CCObject* copyWithZone(CCZone* pZone); + public: ccArray* data; CCArray(); diff --git a/cocos2dx/platform/CCFileUtils.cpp b/cocos2dx/platform/CCFileUtils.cpp index 8df50de18c..371e692df7 100644 --- a/cocos2dx/platform/CCFileUtils.cpp +++ b/cocos2dx/platform/CCFileUtils.cpp @@ -54,9 +54,18 @@ typedef enum SAX_ARRAY }CCSAXState; +typedef enum +{ + SAX_RESULT_NONE = 0, + SAX_RESULT_DICT, + SAX_RESULT_ARRAY +}CCSAXResult; + class CCDictMaker : public CCSAXDelegator { public: + CCSAXResult m_eResultType; + CCArray* m_pRootArray; CCDictionary *m_pRootDict; CCDictionary *m_pCurDict; std::stack m_tDictStack; @@ -68,8 +77,10 @@ public: std::stack m_tStateStack; public: - CCDictMaker() - : m_pRootDict(NULL), + CCDictMaker() + : m_eResultType(SAX_RESULT_NONE), + m_pRootArray(NULL), + m_pRootDict(NULL), m_pCurDict(NULL), m_tState(SAX_NONE), m_pArray(NULL) @@ -82,6 +93,7 @@ public: CCDictionary* dictionaryWithContentsOfFile(const char *pFileName) { + m_eResultType = SAX_RESULT_DICT; CCSAXParser parser; if (false == parser.init("UTF-8")) @@ -94,6 +106,21 @@ public: return m_pRootDict; } + CCArray* arrayWithContentsOfFile(const char* pFileName) + { + m_eResultType = SAX_RESULT_ARRAY; + CCSAXParser parser; + + if (false == parser.init("UTF-8")) + { + return NULL; + } + parser.setDelegator(this); + + parser.parse(pFileName); + return m_pArray; + } + void startElement(void *ctx, const char *name, const char **atts) { CC_UNUSED_PARAM(ctx); @@ -154,10 +181,18 @@ public: else if (sName == "array") { m_tState = SAX_ARRAY; - m_pArray = CCArray::array(); - m_pArray->retain(); + m_pArray = new CCArray(); + if (m_eResultType == SAX_RESULT_ARRAY && m_pRootArray == NULL) + { + m_pRootArray = m_pArray; + m_pRootArray->retain(); + } + CCSAXState preState = SAX_NONE; + if (! m_tStateStack.empty()) + { + preState = m_tStateStack.top(); + } - CCSAXState preState = m_tStateStack.empty() ? SAX_DICT : m_tStateStack.top(); if (preState == SAX_DICT) { m_pCurDict->setObject(m_pArray, m_sCurKey.c_str()); @@ -297,6 +332,12 @@ CCDictionary* ccFileUtils_dictionaryWithContentsOfFileThreadSafe(const char *pFi return tMaker.dictionaryWithContentsOfFile(pFileName); } +CCArray* ccFileUtils_arrayWithContentsOfFileThreadSafe(const char* pFileName) +{ + CCDictMaker tMaker; + return tMaker.arrayWithContentsOfFile(pFileName); +} + unsigned char* CCFileUtils::getFileDataFromZip(const char* pszZipFilePath, const char* pszFileName, unsigned long * pSize) { unsigned char * pBuffer = NULL; diff --git a/cocos2dx/platform/ios/CCFileUtils_ios.mm b/cocos2dx/platform/ios/CCFileUtils_ios.mm index f6af5df46c..2116b0fdc2 100644 --- a/cocos2dx/platform/ios/CCFileUtils_ios.mm +++ b/cocos2dx/platform/ios/CCFileUtils_ios.mm @@ -431,6 +431,22 @@ namespace cocos2d { return pRet; } + CCArray* ccFileUtils_arrayWithContentsOfFileThreadSafe(const char* pFileName) + { + NSString* pPath = [NSString stringWithUTF8String:pFileName]; + NSString* pathExtension= [pPath pathExtension]; + pPath = [pPath stringByDeletingPathExtension]; + pPath = [[NSBundle mainBundle] pathForResource:pPath ofType:pathExtension]; + NSArray* pArray = [NSArray arrayWithContentsOfFile:pPath]; + + CCArray* pRet = new CCArray(); + for (id value in pArray) { + static_addItemToCCArray(value, pRet); + } + + return pRet; + } + unsigned char* CCFileUtils::getFileData(const char* pszFileName, const char* pszMode, unsigned long * pSize) { unsigned char * pBuffer = NULL; diff --git a/cocos2dx/support/CCArray.cpp b/cocos2dx/support/CCArray.cpp index 320cfb7126..2a614b1319 100644 --- a/cocos2dx/support/CCArray.cpp +++ b/cocos2dx/support/CCArray.cpp @@ -116,9 +116,26 @@ CCArray* CCArray::arrayWithCapacity(unsigned int capacity) CCArray* CCArray::arrayWithArray(CCArray* otherArray) { - CCArray* pArray = (CCArray*)otherArray->copyWithZone(NULL); - pArray->autorelease(); - return pArray; + CCArray* pRet = (CCArray*)otherArray->copy(); + pRet->autorelease(); + return pRet; +} + +CCArray* CCArray::arrayWithContentsOfFile(const char* pFileName) +{ + CCArray* pRet = arrayWithContentsOfFileThreadSafe(pFileName); + if (pRet != NULL) + { + pRet->autorelease(); + } + return pRet; +} + +extern CCArray* ccFileUtils_arrayWithContentsOfFileThreadSafe(const char* pFileName); + +CCArray* CCArray::arrayWithContentsOfFileThreadSafe(const char* pFileName) +{ + return ccFileUtils_arrayWithContentsOfFileThreadSafe(pFileName); } bool CCArray::init()