Node* SceneReader::createObject(const rapidjson::Value &dict, cocos2d::Node* parent)
{
    const char *className = DICTOOL->getStringValue_json(dict, "classname");
    if(strcmp(className, "CCNode") == 0)
    {
        Node* gb = nullptr;
        if(nullptr == parent)
        {
            gb = Node::create();
        }
        else
        {
            gb = Node::create();
            parent->addChild(gb);
        }
        
        setPropertyFromJsonDict(dict, gb);

        int count = DICTOOL->getArrayCount_json(dict, "components");
        for (int i = 0; i < count; i++)
        {
            const rapidjson::Value &subDict = DICTOOL->getSubDictionary_json(dict, "components", i);
            if (!DICTOOL->checkObjectExist_json(subDict))
            {
                break;
            }
            const char *comName = DICTOOL->getStringValue_json(subDict, "classname");
            Component *com = ObjectFactory::getInstance()->createComponent(comName);
			if (com != NULL)
			{
				if (com->serialize((void*)(&subDict)))
				{
					gb->addComponent(com);
				}
                else
                {
                    com = nullptr;
                }
			}
            if(_fnSelector != nullptr)
            {
                _fnSelector(com, (void*)(&subDict));
            }
        }

        int length = DICTOOL->getArrayCount_json(dict, "gameobjects");
        for (int i = 0; i < length; ++i)
        {
            const rapidjson::Value &subDict = DICTOOL->getSubDictionary_json(dict, "gameobjects", i);
            if (!DICTOOL->checkObjectExist_json(subDict))
            {
                break;
            }
            createObject(subDict, gb);
        }
        
        return gb;
    }
    
    return nullptr;
}
cocos2d::Node* SceneReader::createNodeWithSceneFile(const std::string &fileName, AttachComponentType attachComponent /*= AttachComponentType::EMPTY_NODE*/)
{
    std::string reDir = fileName;
    std::string file_extension = "";
    size_t pos = reDir.find_last_of('.');
    if (pos != std::string::npos)
    {
        file_extension = reDir.substr(pos, reDir.length());
        std::transform(file_extension.begin(),file_extension.end(), file_extension.begin(), (int(*)(int))toupper);
    }
    if (file_extension == ".JSON")
    {
        _node = nullptr;
        rapidjson::Document jsonDict;
        do {
            CC_BREAK_IF(!readJson(fileName, jsonDict));
            _node = createObject(jsonDict, nullptr, attachComponent);
            TriggerMng::getInstance()->parse(jsonDict);
        } while (0);
        
        return _node;
    }
    else if(file_extension == ".CSB")
    {
        do {
            std::string binaryFilePath = CCFileUtils::getInstance()->fullPathForFilename(fileName);
            auto fileData = FileUtils::getInstance()->getDataFromFile(binaryFilePath);
            auto fileDataBytes = fileData.getBytes();
            CC_BREAK_IF(fileData.isNull());
            CocoLoader tCocoLoader;
            if (tCocoLoader.ReadCocoBinBuff((char*)fileDataBytes))
            {
                stExpCocoNode *tpRootCocoNode = tCocoLoader.GetRootCocoNode();
                rapidjson::Type tType = tpRootCocoNode->GetType(&tCocoLoader);
                if (rapidjson::kObjectType  == tType)
                {
                    stExpCocoNode *tpChildArray = tpRootCocoNode->GetChildArray(&tCocoLoader);
                    CC_BREAK_IF(tpRootCocoNode->GetChildNum() == 0);
                    _node = Node::create();
                    int  nCount = 0;
                    std::vector<Component*> _vecComs;
                    ComRender *pRender = nullptr;
                    std::string key = tpChildArray[15].GetName(&tCocoLoader);
                    if (key == "components")
                    {
                        nCount = tpChildArray[15].GetChildNum();
                    }
                    stExpCocoNode *pComponents = tpChildArray[15].GetChildArray(&tCocoLoader);
                    SerData *data = new (std::nothrow) SerData();
                    for (int i = 0; i < nCount; i++)
                    {
                        stExpCocoNode *subDict = pComponents[i].GetChildArray(&tCocoLoader);
                        if (subDict == nullptr)
                        {
                            continue;
                        }
                        std::string key1 = subDict[1].GetName(&tCocoLoader);
                        const char *comName = subDict[1].GetValue(&tCocoLoader);
                        Component *pCom = nullptr;
                        if (key1 == "classname" && comName != nullptr)
                        {
                            pCom = createComponent(comName);
                        }
                        CCLOG("classname = %s", comName);
                        if (pCom != nullptr)
                        {
                            data->_rData = nullptr;
                            data->_cocoNode = subDict;
                            data->_cocoLoader = &tCocoLoader;
                            if (pCom->serialize(data))
                            {
                                ComRender *pTRender = dynamic_cast<ComRender*>(pCom);
                                if (pTRender != nullptr)
                                {
                                    pRender = pTRender;
                                }
                                else
                                {
                                    _vecComs.push_back(pCom);
                                }
                            }
                            else
                            {
                                CC_SAFE_RELEASE_NULL(pCom);
                            }
                        }
                        if(_fnSelector != nullptr)
                        {
                            _fnSelector(pCom, (void*)(data));
                        }
                    }
                    
                    setPropertyFromJsonDict(&tCocoLoader, tpRootCocoNode, _node);
                    for (std::vector<Component*>::iterator iter = _vecComs.begin(); iter != _vecComs.end(); ++iter)
                    {
                        _node->addComponent(*iter);
                    }
                    
                    stExpCocoNode *pGameObjects = tpChildArray[11].GetChildArray(&tCocoLoader);
                    int length = tpChildArray[11].GetChildNum();
                    for (int i = 0; i < length; ++i)
                    {
                        createObject(&tCocoLoader, &pGameObjects[i], _node, attachComponent);
                    }
                    TriggerMng::getInstance()->parse(&tCocoLoader, tpChildArray);
                }
                
            }
        }while (0);
        return _node;
    }
    else
    {
        log("read file [%s] error!\n", fileName.c_str());
    }
    return nullptr;
}
Example #3
0
Node* SceneReader::createObject(const rapidjson::Value &dict, cocos2d::Node* parent, AttachComponentType attachComponent)
{
    const char *className = DICTOOL->getStringValue_json(dict, "classname");
    if(strcmp(className, "CCNode") == 0)
    {
        Node* gb = nullptr;
        if(nullptr == parent)
        {
            gb = Node::create();
        }

        std::vector<Component*> vecComs;
        ComRender *render = nullptr;
        int count = DICTOOL->getArrayCount_json(dict, "components");
        for (int i = 0; i < count; i++)
        {
            const rapidjson::Value &subDict = DICTOOL->getSubDictionary_json(dict, "components", i);
            if (!DICTOOL->checkObjectExist_json(subDict))
            {
                break;
            }
            const char *comName = DICTOOL->getStringValue_json(subDict, "classname");
            Component *com = ObjectFactory::getInstance()->createComponent(comName);
            if (com != nullptr)
            {
                if (com->serialize((void*)(&subDict)))
                {
                    ComRender *tRender = dynamic_cast<ComRender*>(com);
                    if (tRender == nullptr)
                    {
                        vecComs.push_back(com);
                    }
                    else
                    {
                        render = tRender;
                    }
                }
            }
            if(_fnSelector != nullptr)
            {
                _fnSelector(com, (void*)(&subDict));
            }
        }

        if (parent != nullptr)
        {
            if (render == nullptr || attachComponent == AttachComponentType::EMPTY_NODE)
            {
                gb = Node::create();
                if (render != nullptr)
                {
                    vecComs.push_back(render);
                }
            }
            else
            {
                gb = render->getNode();
                gb->retain();
                render->setNode(nullptr);
            }
            parent->addChild(gb);
        }

        setPropertyFromJsonDict(dict, gb);
        for (std::vector<Component*>::iterator iter = vecComs.begin(); iter != vecComs.end(); ++iter)
        {
              gb->addComponent(*iter);
        }

        int length = DICTOOL->getArrayCount_json(dict, "gameobjects");
        for (int i = 0; i < length; ++i)
        {
            const rapidjson::Value &subDict = DICTOOL->getSubDictionary_json(dict, "gameobjects", i);
            if (!DICTOOL->checkObjectExist_json(subDict))
            {
                break;
            }
            createObject(subDict, gb, attachComponent);
        }
        
        return gb;
    }
    
    return nullptr;
}
cocos2d::Node* SceneReader::createObject(CocoLoader *cocoLoader, stExpCocoNode *cocoNode, cocos2d::Node* parent, AttachComponentType attachComponent)
{
    const char *className = nullptr;
    stExpCocoNode *pNodeArray = cocoNode->GetChildArray(cocoLoader);
    std::string Key = pNodeArray[1].GetName(cocoLoader);
    if (Key == "classname")
    {
        className = pNodeArray[1].GetValue(cocoLoader);
    }
    if(strcmp(className, "CCNode") == 0)
    {
        Node* gb = nullptr;
        std::vector<Component*> _vecComs;
        ComRender *pRender = nullptr;
        int count = 0;
        std::string key = pNodeArray[13].GetName(cocoLoader);
        if (key == "components")
        {
            count = pNodeArray[13].GetChildNum();
        }
        stExpCocoNode *pComponents = pNodeArray[13].GetChildArray(cocoLoader);
        SerData *data = new (std::nothrow) SerData();
        for (int i = 0; i < count; ++i)
        {
            stExpCocoNode *subDict = pComponents[i].GetChildArray(cocoLoader);
            if (subDict == nullptr)
            {
                continue;
            }
            std::string key1 = subDict[1].GetName(cocoLoader);
            const char *comName = subDict[1].GetValue(cocoLoader);
            Component *pCom = nullptr;
            if (key1 == "classname" && comName != nullptr)
            {
                pCom = createComponent(comName);
            }
            CCLOG("classname = %s", comName);
            if (pCom != nullptr)
            {
                data->_rData = nullptr;
                data->_cocoNode = subDict;
                data->_cocoLoader = cocoLoader;
                if (pCom->serialize(data))
                {
                    ComRender *pTRender = dynamic_cast<ComRender*>(pCom);
                    if (pTRender != nullptr)
                    {
                        pRender = pTRender;
                    }
                    else
                    {
                        _vecComs.push_back(pCom);
                    }
                }
                else
                {
                    CC_SAFE_RELEASE_NULL(pCom);
                }
            }
            if(_fnSelector != nullptr)
            {
                _fnSelector(pCom, (void*)(data));
            }
        }
        CC_SAFE_DELETE(data);
        
        if (parent != nullptr)
        {
            if (pRender == nullptr || attachComponent == AttachComponentType::EMPTY_NODE)
            {
                gb = CCNode::create();
                if (pRender != nullptr)
                {
                    _vecComs.push_back(pRender);
                }
            }
            else
            {
                gb = pRender->getNode();
                gb->retain();
                pRender->setNode(nullptr);
                CC_SAFE_RELEASE_NULL(pRender);
            }
            parent->addChild(gb);
        }
        setPropertyFromJsonDict(cocoLoader, cocoNode, gb);
        for (std::vector<Component*>::iterator iter = _vecComs.begin(); iter != _vecComs.end(); ++iter)
        {
            gb->addComponent(*iter);
        }
        
        stExpCocoNode *pGameObjects = pNodeArray[12].GetChildArray(cocoLoader);
        if (pGameObjects != nullptr)
        {
            int length = pNodeArray[12].GetChildNum();
            for (int i = 0; i < length; ++i)
            {
                createObject(cocoLoader, &pGameObjects[i], gb, attachComponent);
            }
        }
        return gb;
    }
    return nullptr;
}
Node* SceneReader::createObject(const rapidjson::Value &dict, cocos2d::Node* parent, AttachComponentType attachComponent)
{
    const char *className = DICTOOL->getStringValue_json(dict, "classname");
    if(strcmp(className, "CCNode") == 0)
    {
        Node* gb = nullptr;
        if(nullptr == parent)
        {
            gb = Node::create();
        }

        std::vector<Component*> vecComs;
        ComRender *render = nullptr;
        int count = DICTOOL->getArrayCount_json(dict, "components");
        for (int i = 0; i < count; i++)
        {
            const rapidjson::Value &subDict = DICTOOL->getSubDictionary_json(dict, "components", i);
            if (!DICTOOL->checkObjectExist_json(subDict))
            {
                break;
            }
            const char *comName = DICTOOL->getStringValue_json(subDict, "classname");
            Component *com = this->createComponent(comName);
            CCLOG("classname = %s", comName);
            SerData *data = new (std::nothrow) SerData();
            if (com != nullptr)
            {
                data->_rData = &subDict;
                data->_cocoNode = nullptr;
                data->_cocoLoader = nullptr;
                if (com->serialize(data))
                {
                    ComRender *tRender = dynamic_cast<ComRender*>(com);
                    if (tRender == nullptr)
                    {
                        vecComs.push_back(com);
                    }
                    else
                    {
                        render = tRender;
                    }
                }
            }
            CC_SAFE_DELETE(data);
            if(_fnSelector != nullptr)
            {
                _fnSelector(com, data);
            }
        }

        if (parent != nullptr)
        {
            if (render == nullptr || attachComponent == AttachComponentType::EMPTY_NODE)
            {
                gb = Node::create();
                if (render != nullptr)
                {
                    vecComs.push_back(render);
                }
            }
            else
            {
                gb = render->getNode();
                gb->retain();
                render->setNode(nullptr);
            }
            parent->addChild(gb);
        }

        setPropertyFromJsonDict(dict, gb);
        for (std::vector<Component*>::iterator iter = vecComs.begin(); iter != vecComs.end(); ++iter)
        {
              gb->addComponent(*iter);
        }

        int length = DICTOOL->getArrayCount_json(dict, "gameobjects");
        for (int i = 0; i < length; ++i)
        {
            const rapidjson::Value &subDict = DICTOOL->getSubDictionary_json(dict, "gameobjects", i);
            if (!DICTOOL->checkObjectExist_json(subDict))
            {
                break;
            }
            createObject(subDict, gb, attachComponent);
        }
        
        if(dict.HasMember("CanvasSize"))
        {
            const rapidjson::Value &canvasSizeDict = DICTOOL->getSubDictionary_json(dict, "CanvasSize");
            if (DICTOOL->checkObjectExist_json(canvasSizeDict))
            {
                int width = DICTOOL->getIntValue_json(canvasSizeDict, "_width");
                int height = DICTOOL->getIntValue_json(canvasSizeDict, "_height");
                gb->setContentSize(Size(width, height));
            }

        }
        
        return gb;
    }
    
    return nullptr;
}
    CCNode* CCJsonReader::createObject(cs::CSJsonDictionary * inputFiles, CCNode* parenet)
    {
        const char* className = inputFiles->getItemStringValue("classname"); 

        if(strcmp(className, "CCNode") == 0)
        {
            CCNode* gb = NULL;
            if(NULL == parenet)
            {
                gb = CCNode::create();
            }
            else
            {
                gb = CCNode::create();
                parenet->addChild(gb);
            }
            
            setPropertyFromJsonDict(gb, inputFiles);
    
            int count = inputFiles->getArrayItemCount("components");
            for (int i = 0; i < count; i++)
            {
                cs::CSJsonDictionary * subDict = inputFiles->getSubItemFromArray("components", i);
                if (!subDict)
                   break;
                const char *comName = subDict->getItemStringValue("classname");
                const char *file = subDict->getItemStringValue("file");
                const char *pComName = subDict->getItemStringValue("name");
                if (file == NULL || strcmp(file, "") == 0)
                {
                    continue;
                }
                CCAssert(file != NULL, "file must be not NULL!");
                std::string pPath = cocos2d::CCFileUtils::sharedFileUtils()->fullPathForFilename(file);

                if (comName != NULL && strcmp(comName, "CCSprite") == 0)
                {
                    cocos2d::CCSprite *pSprite = CCSprite::create(pPath.c_str());
					
                    CCComRender *pRender = CCComRender::create(pSprite, "CCSprite");
                    if (pComName != NULL)
                    {
                        pRender->setName(pComName);
                    }
                    
                    gb->addComponent(pRender);
                }
                else if(comName != NULL && strcmp(comName, "CCTMXTiledMap") == 0)
                {
                    cocos2d::CCTMXTiledMap *pTmx = CCTMXTiledMap::create(pPath.c_str());
                    CCComRender *pRender = CCComRender::create(pTmx, "CCTMXTiledMap");
                    if (pComName != NULL)
                    {
                        pRender->setName(pComName);
                    }
                    gb->addComponent(pRender);
                }
                else if(comName != NULL && strcmp(comName, "CCParticleSystemQuad") == 0)
                {
                    std::string::size_type pos =  pPath.find(".plist");
                    if (pos  == pPath.npos)
                    {
                        continue;
                    }
                    cocos2d::CCParticleSystemQuad *pParticle = CCParticleSystemQuad::create(pPath.c_str());

					pParticle->setPosition(0, 0);
                    CCComRender *pRender = CCComRender::create(pParticle, "CCParticleSystemQuad");
                    if (pComName != NULL)
                    {
                        pRender->setName(pComName);
                    }
                    gb->addComponent(pRender);
                }
                else if(comName != NULL && strcmp(comName, "CCArmature") == 0)
                {
                    std::string reDir = pPath;
                    std::string file_path = "";
                    size_t pos = reDir.find_last_of('/');
                    if (pos != std::string::npos)
                    {
                        file_path = reDir.substr(0, pos+1);
                    }
                    unsigned long size = 0;
                    const char *des = (char*)(cocos2d::CCFileUtils::sharedFileUtils()->getFileData(pPath.c_str(),"r" , &size));
			        cs::CSJsonDictionary *jsonDict = new cs::CSJsonDictionary();
			        jsonDict->initWithDescription(des);
                    if(NULL == des || strcmp(des, "") == 0)
                    {
                        CCLog("read json file[%s] error!\n", pPath.c_str());
                    }
                    
                    int childrenCount = DICTOOL->getArrayCount_json(jsonDict, "armature_data");
                    cs::CSJsonDictionary* subData = DICTOOL->getDictionaryFromArray_json(jsonDict, "armature_data", 0);
                    const char *name = DICTOOL->getStringValue_json(subData, "name");

                    childrenCount = DICTOOL->getArrayCount_json(jsonDict, "config_file_path");
                    for (int i = 0; i < childrenCount; ++i)
                    {
                        const char* plist = DICTOOL->getStringValueFromArray_json(jsonDict, "config_file_path", i);
                        std::string plistpath;
                        plistpath += file_path;
                        plistpath.append(plist);
                        cocos2d::CCDictionary *root = CCDictionary::createWithContentsOfFile(plistpath.c_str());
                        CCDictionary* metadata = DICTOOL->getSubDictionary(root, "metadata");
                        const char* textureFileName = DICTOOL->getStringValue(metadata, "textureFileName");

                        std::string textupath;
                        textupath += file_path;
                        textupath.append(textureFileName);

                        CCArmatureDataManager::sharedArmatureDataManager()->addArmatureFileInfo(textupath.c_str(), plistpath.c_str(), pPath.c_str());
                        
                    }
                    
                    CCArmature *pAr = CCArmature::create(name);
                    CCComRender *pRender = CCComRender::create(pAr, "CCArmature");
                    if (pComName != NULL)
                    {
                        pRender->setName(pComName);
                    }
                    gb->addComponent(pRender);

                    CC_SAFE_DELETE(jsonDict);
                }
                else if(comName != NULL && strcmp(comName, "CCComAudio") == 0)
                {
                    CCComAudio *pAudio = CCComAudio::create();
                    pAudio->preloadEffect(pPath.c_str());
                    gb->addComponent(pAudio);
                }
                else if(comName != NULL && strcmp(comName, "CCComAttribute") == 0)
                {
                    CCComAttribute *pAttribute = CCComAttribute::create();
                    gb->addComponent(pAttribute);
                }
                else if (comName != NULL && strcmp(comName, "CCBackgroundAudio") == 0)
                {
                    CCComAudio *pAudio = CCComAudio::create();
                    pAudio->preloadBackgroundMusic(pPath.c_str());
					pAudio->setFile(pPath.c_str());
					bool bLoop = subDict->getItemIntValue("loop", 0);
					pAudio->setIsLoop(bLoop);
                    gb->addComponent(pAudio);
                }
				else if(comName != NULL && strcmp(comName, "GUIComponent") == 0)
				{
					cocos2d::extension::UILayer *pLayer = cocos2d::extension::UILayer::create();
					UIWidget* widget=cocos2d::extension::UIHelper::instance()->createWidgetFromJsonFile(pPath.c_str());
					pLayer->addWidget(widget);
					CCComRender *pRender = CCComRender::create(pLayer, "GUIComponent");
					if (pComName != NULL)
					{
						pRender->setName(pComName);
					}
					gb->addComponent(pRender);
				}
                
                CC_SAFE_DELETE(subDict);
            }

            for (int i = 0; i < inputFiles->getArrayItemCount("gameobjects"); i++)
            {
                cs::CSJsonDictionary * subDict = inputFiles->getSubItemFromArray("gameobjects", i);
                if (!subDict)
                {
                    break;
                }
                createObject(subDict, gb);
                CC_SAFE_DELETE(subDict);
            }
            
            return gb;
        }
        
        return NULL;
    }
Example #7
0
	CCNode* SceneReader::createObject(const rapidjson::Value &root, cocos2d::CCNode* parent)
    {
        const char *className = DICTOOL->getStringValue_json(root, "classname"); //inputFiles->getItemStringValue("classname");
        if(strcmp(className, "CCNode") == 0)
        {
            CCNode* gb = NULL;
            if(NULL == parent)
            {
                gb = CCNode::create();
            }
            else
            {
                gb = CCNode::create();
                parent->addChild(gb);
            }
            
            setPropertyFromJsonDict(root, gb);
    
            int count = DICTOOL->getArrayCount_json(root, "components");
            for (int i = 0; i < count; i++)
            {
                const rapidjson::Value &subDict = DICTOOL->getSubDictionary_json(root, "components", i);
                if (!DICTOOL->checkObjectExist_json(subDict))
                {
                    break;
                }

                const char *comName = DICTOOL->getStringValue_json(subDict, "classname");
				const char *pComName = DICTOOL->getStringValue_json(subDict, "name");
				const rapidjson::Value &fileData = DICTOOL->getSubDictionary_json(subDict, "fileData");
                std::string pPath;
                std::string pPlistFile;
				int nResType = 0;
				//if (fileData != NULL)
                if (DICTOOL->checkObjectExist_json(fileData))
                {
					const char *file = DICTOOL->getStringValue_json(fileData, "path");
					nResType = DICTOOL->getIntValue_json(fileData, "resourceType", - 1);
					const char *plistFile = DICTOOL->getStringValue_json(fileData, "plistFile");
					if (file != NULL)
					{
						pPath.append(cocos2d::CCFileUtils::sharedFileUtils()->fullPathForFilename(file));
					}

					if (plistFile != NULL)
					{
						pPlistFile.append(cocos2d::CCFileUtils::sharedFileUtils()->fullPathForFilename(plistFile));
					}
                }

                if (comName != NULL && strcmp(comName, "CCSprite") == 0)
                {
					cocos2d::CCSprite *pSprite = NULL;

					if (nResType == 0)
					{
						if (pPath.find(".png") == pPath.npos)
						{
							continue;
						}
						pSprite = CCSprite::create(pPath.c_str());
					}
					else if (nResType == 1)
					{
						std::string pngFile = pPlistFile;
						std::string::size_type pos = pngFile.find(".plist");
						if (pos  == pPath.npos)
						{
							continue;
						}
						pngFile.replace(pos, pngFile.length(), ".png");
						CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(pPlistFile.c_str(), pngFile.c_str());
						pSprite = CCSprite::createWithSpriteFrameName(pPath.c_str());
					}
					else
					{
						continue;
					}
					
                    CCComRender *pRender = CCComRender::create(pSprite, "CCSprite");
                    if (pComName != NULL)
                    {
                        pRender->setName(pComName);
                    }
                    
                    gb->addComponent(pRender);
					if (_pListener && _pfnSelector)
					{
						(_pListener->*_pfnSelector)(pSprite, (void*)(&subDict));
					}
                }
                else if(comName != NULL && strcmp(comName, "CCTMXTiledMap") == 0)
                {
					cocos2d::CCTMXTiledMap *pTmx = NULL;
					if (nResType == 0)
					{
						if (pPath.find(".tmx") == pPath.npos)
						{
							continue;
						}
						pTmx = CCTMXTiledMap::create(pPath.c_str());
					}
					else
					{
						continue;
					}

                    CCComRender *pRender = CCComRender::create(pTmx, "CCTMXTiledMap");
                    if (pComName != NULL)
                    {
                        pRender->setName(pComName);
                    }
                    gb->addComponent(pRender);
					if (_pListener && _pfnSelector)
					{
						(_pListener->*_pfnSelector)(pTmx, (void*)(&subDict));
					}
                }
                else if(comName != NULL && strcmp(comName, "CCParticleSystemQuad") == 0)
                {
                    std::string::size_type pos =  pPath.find(".plist");
                    if (pos  == pPath.npos)
                    {
                        continue;
                    }

					cocos2d::CCParticleSystemQuad *pParticle = NULL;
					if (nResType == 0)
					{
						pParticle = CCParticleSystemQuad::create(pPath.c_str());
					}
					else
					{
						CCLog("unknown resourcetype on CCParticleSystemQuad!");
					}

					pParticle->setPosition(0, 0);
                    CCComRender *pRender = CCComRender::create(pParticle, "CCParticleSystemQuad");
                    if (pComName != NULL)
                    {
                        pRender->setName(pComName);
                    }
                    gb->addComponent(pRender);
					if (_pListener && _pfnSelector)
					{
						(_pListener->*_pfnSelector)(pParticle, (void*)(&subDict));
					}
                }
                else if(comName != NULL && strcmp(comName, "CCArmature") == 0)
                {
					if (nResType != 0)
					{
						continue;
					}
                    std::string reDir = pPath;
                    std::string file_path = "";
                    size_t pos = reDir.find_last_of('/');
                    if (pos != std::string::npos)
                    {
                        file_path = reDir.substr(0, pos+1);
                    }
                    
                    rapidjson::Document jsonDict;
                    if(!readJson(pPath.c_str(), jsonDict))
                    {
                        CCLog("read json file[%s] error!\n", pPath.c_str());
                        continue;
                    }
                    
                    
                    int childrenCount = DICTOOL->getArrayCount_json(jsonDict, "armature_data");
                    const rapidjson::Value &subData = DICTOOL->getDictionaryFromArray_json(jsonDict, "armature_data", 0);
                    const char *name = DICTOOL->getStringValue_json(subData, "name");

                    childrenCount = DICTOOL->getArrayCount_json(jsonDict, "config_file_path");
                    for (int i = 0; i < childrenCount; ++i)
                    {
                        const char* plist = DICTOOL->getStringValueFromArray_json(jsonDict, "config_file_path", i);
                        if (plist == NULL)
                        {
                            continue;
                        }
                        std::string plistpath;
                        plistpath += file_path;
                        plistpath.append(plist);
                        cocos2d::CCDictionary *root = CCDictionary::createWithContentsOfFile(plistpath.c_str());
                        CCDictionary* metadata = DICTOOL->getSubDictionary(root, "metadata");
                        const char* textureFileName = DICTOOL->getStringValue(metadata, "textureFileName");

                        std::string textupath;
                        textupath += file_path;
                        textupath.append(textureFileName);

                        CCArmatureDataManager::sharedArmatureDataManager()->addArmatureFileInfo(textupath.c_str(), plistpath.c_str(), pPath.c_str());
                        
                    }
                    
                    CCArmature *pAr = CCArmature::create(name);
                    CCComRender *pRender = CCComRender::create(pAr, "CCArmature");
                    if (pComName != NULL)
                    {
                        pRender->setName(pComName);
                    }
                    gb->addComponent(pRender);
					const char *actionName = DICTOOL->getStringValue_json(subDict, "selectedactionname"); 
					if (actionName != NULL && pAr->getAnimation() != NULL)
					{
						pAr->getAnimation()->play(actionName);
					}
					if (_pListener && _pfnSelector)
					{
						(_pListener->*_pfnSelector)(pAr, (void*)(&subDict));
					}
                }
                else if(comName != NULL && strcmp(comName, "CCComAudio") == 0)
                {
					CCComAudio *pAudio = NULL;
					if (nResType == 0)
					{
						pAudio = CCComAudio::create();
					}
					else
					{
						continue;
					}
                    pAudio->preloadEffect(pPath.c_str());
                    gb->addComponent(pAudio);
					if (_pListener && _pfnSelector)
					{
						(_pListener->*_pfnSelector)(pAudio, (void*)(&subDict));
					}
                }
                else if(comName != NULL && strcmp(comName, "CCComAttribute") == 0)
                {
                    CCComAttribute *pAttribute = NULL;
					if (nResType == 0)
					{
						pAttribute = CCComAttribute::create();
						unsigned long size = 0;
						const char* pData = 0;
						pData = (char*)(cocos2d::CCFileUtils::sharedFileUtils()->getFileData(pPath.c_str(), "r", &size));
						if(pData != NULL && strcmp(pData, "") != 0)
						{
                            pAttribute->parse(pData);
						}
					}
					else
					{
						CCLog("unknown resourcetype on CCComAttribute!");
						continue;
					}
                    gb->addComponent(pAttribute);
					if (_pListener && _pfnSelector)
					{
						(_pListener->*_pfnSelector)(pAttribute, (void*)(&subDict));
					}
                }
                else if (comName != NULL && strcmp(comName, "CCBackgroundAudio") == 0)
                {
					CCComAudio *pAudio = NULL;
					if (nResType == 0)
					{
						pAudio = CCComAudio::create();
					}
					else
					{
						continue;
					}
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
                    // no MP3 support for CC_PLATFORM_WP8
                    std::string::size_type pos = pPath.find(".mp3");
					if (pos  == pPath.npos)
					{
						continue;
					}
					pPath.replace(pos, pPath.length(), ".wav");
#endif
                    pAudio->preloadBackgroundMusic(pPath.c_str());
					pAudio->setFile(pPath.c_str());
					bool bLoop = (bool)(DICTOOL->getIntValue_json(subDict, "loop"));
					pAudio->setLoop(bLoop);
                    gb->addComponent(pAudio);
					if (_pListener && _pfnSelector)
					{
						(_pListener->*_pfnSelector)(pAudio, (void*)(&subDict));
					}
					pAudio->playBackgroundMusic(pPath.c_str(), bLoop);
                }
				else if(comName != NULL && strcmp(comName, "GUIComponent") == 0)
				{
					cocos2d::extension::UILayer *pLayer = cocos2d::extension::UILayer::create();
                    pLayer->scheduleUpdate();
                    UIWidget* widget = cocos2d::extension::GUIReader::shareReader()->widgetFromJsonFile(pPath.c_str());
					pLayer->addWidget(widget);
					CCComRender *pRender = CCComRender::create(pLayer, "GUIComponent");
					if (pComName != NULL)
					{
					pRender->setName(pComName);
					}
					gb->addComponent(pRender);
					if (_pListener && _pfnSelector)
					{
						(_pListener->*_pfnSelector)(pLayer, (void*)(&subDict));
					}
				}
            }

            int length = DICTOOL->getArrayCount_json(root, "gameobjects");
            for (int i = 0; i < length; ++i)
            {
                const rapidjson::Value &subDict = DICTOOL->getSubDictionary_json(root, "gameobjects", i);
                if (!DICTOOL->checkObjectExist_json(subDict))
                {
                    break;
                }
                createObject(subDict, gb);
            }
            
            return gb;
        }
        
        return NULL;
    }
Example #8
0
	Node* SceneReader::createObject(cs::JsonDictionary * inputFiles, Node* parenet, ISceneReaderListener* listener)
    {
        const char *className = inputFiles->getItemStringValue("classname"); 
        if(strcmp(className, "CCNode") == 0)
        {
            Node* gb = NULL;
            if(NULL == parenet)
            {
                gb = Node::create();
            }
            else
            {
                gb = Node::create();
                parenet->addChild(gb);
            }
            
            setPropertyFromJsonDict(gb, inputFiles);
    
            int count = inputFiles->getArrayItemCount("components");
            for (int i = 0; i < count; i++)
            {
                cs::JsonDictionary * subDict = inputFiles->getSubItemFromArray("components", i);
                if (!subDict)
				{
				   CC_SAFE_DELETE(subDict);
                   break;
				}
                const char *comName = subDict->getItemStringValue("classname");
				const char *pComName = subDict->getItemStringValue("name");
                
				cs::JsonDictionary *fileData = subDict->getSubDictionary("fileData");
				std::string pPath;
                std::string pPlistFile;
				int nResType = 0;
				if (fileData != NULL)
                {
					const char *file = fileData->getItemStringValue("path");
					nResType = fileData->getItemIntValue("resourceType", -1);
					const char *plistFile = fileData->getItemStringValue("plistFile");
					if (file != NULL)
					{
						pPath.append(cocos2d::FileUtils::getInstance()->fullPathForFilename(file));
					}

					if (plistFile != NULL)
					{
						pPlistFile.append(cocos2d::FileUtils::getInstance()->fullPathForFilename(plistFile));
					}
					CC_SAFE_DELETE(fileData);
                }

                if (comName != NULL && strcmp(comName, "CCSprite") == 0)
                {
					cocos2d::Sprite *pSprite = NULL;

					if (nResType == 0)
					{
						if (pPath.find(".png") == pPath.npos)
						{
							continue;
						}
						pSprite = Sprite::create(pPath.c_str());
					}
					else if (nResType == 1)
					{
						std::string pngFile = pPlistFile;
						std::string::size_type pos = pngFile.find(".plist");
						if (pos  == pPath.npos)
						{
							continue;
						}
						pngFile.replace(pos, pngFile.length(), ".png");
						CCSpriteFrameCache::getInstance()->addSpriteFramesWithFile(pPlistFile.c_str(), pngFile.c_str());
						pSprite = Sprite::createWithSpriteFrameName(pPath.c_str());
					}
					else
					{
						continue;
					}
					
                    ComRender *pRender = ComRender::create(pSprite, "CCSprite");
                    if (pComName != NULL)
                    {
                        pRender->setName(pComName);
                    }
                    
                    gb->addComponent(pRender);
                }
                else if(comName != NULL && strcmp(comName, "CCTMXTiledMap") == 0)
                {
					cocos2d::TMXTiledMap *pTmx = NULL;
					if (nResType == 0)
					{
						if (pPath.find(".tmx") == pPath.npos)
						{
							continue;
						}
						pTmx = TMXTiledMap::create(pPath.c_str());
					}
					else
					{
						continue;
					}

                    ComRender *pRender = ComRender::create(pTmx, "CCTMXTiledMap");
                    if (pComName != NULL)
                    {
                        pRender->setName(pComName);
                    }
                    gb->addComponent(pRender);
                }
                else if(comName != NULL && strcmp(comName, "CCParticleSystemQuad") == 0)
                {
                    std::string::size_type pos =  pPath.find(".plist");
                    if (pos  == pPath.npos)
                    {
                        continue;
                    }

					cocos2d::ParticleSystemQuad *pParticle = NULL;
					if (nResType == 0)
					{
						pParticle = ParticleSystemQuad::create(pPath.c_str());
					}
					else
					{
						CCLOG("unknown resourcetype on CCParticleSystemQuad!");
					}

					pParticle->setPosition(0, 0);
                    ComRender *pRender = ComRender::create(pParticle, "CCParticleSystemQuad");
                    if (pComName != NULL)
                    {
                        pRender->setName(pComName);
                    }
                    gb->addComponent(pRender);
                }
                else if(comName != NULL && strcmp(comName, "CCArmature") == 0)
                {
					if (nResType != 0)
					{
						continue;
					}
					std::string reDir = pPath;
					std::string file_path = "";
					size_t pos = reDir.find_last_of('/');
					if (pos != std::string::npos)
					{
						file_path = reDir.substr(0, pos+1);
					}
					unsigned long size = 0;
					const char *des = (char*)(cocos2d::FileUtils::getInstance()->getFileData(pPath.c_str(),"r" , &size));
					cs::JsonDictionary *jsonDict = new cs::JsonDictionary();
					jsonDict->initWithDescription(des);
					if(NULL == des || strcmp(des, "") == 0)
					{
						CCLOG("read json file[%s] error!\n", pPath.c_str());
					}

					int childrenCount = DICTOOL->getArrayCount_json(jsonDict, "armature_data");
					cs::JsonDictionary* subData = DICTOOL->getDictionaryFromArray_json(jsonDict, "armature_data", 0);
					const char *name = DICTOOL->getStringValue_json(subData, "name");

					childrenCount = DICTOOL->getArrayCount_json(jsonDict, "config_file_path");
					for (int i = 0; i < childrenCount; ++i)
					{
						const char* plist = DICTOOL->getStringValueFromArray_json(jsonDict, "config_file_path", i);
						std::string plistpath;
						plistpath += file_path;
						plistpath.append(plist);
						cocos2d::Dictionary *root = Dictionary::createWithContentsOfFile(plistpath.c_str());
						Dictionary* metadata = DICTOOL->getSubDictionary(root, "metadata");
						const char* textureFileName = DICTOOL->getStringValue(metadata, "textureFileName");

						std::string textupath;
						textupath += file_path;
						textupath.append(textureFileName);

						cocos2d::extension::armature::ArmatureDataManager::getInstance()->addArmatureFileInfo(textupath.c_str(), plistpath.c_str(), pPath.c_str());

					}

					cocos2d::extension::armature::Armature *pAr = cocos2d::extension::armature::Armature::create(name);
					ComRender *pRender = ComRender::create(pAr, "CCArmature");
					if (pComName != NULL)
					{
						pRender->setName(pComName);
					}
					gb->addComponent(pRender);

					const char *actionName = subDict->getItemStringValue("selectedactionname");
					if (actionName != NULL && pAr->getAnimation() != NULL)
					{
						pAr->getAnimation()->play(actionName);
					}

					CC_SAFE_DELETE(jsonDict);
					CC_SAFE_DELETE(subData);
					CC_SAFE_DELETE_ARRAY(des);
                }
                else if(comName != NULL && strcmp(comName, "CCComAudio") == 0)
                {
					ComAudio *pAudio = NULL;
					if (nResType == 0)
					{
						pAudio = ComAudio::create();
					}
					else
					{
						continue;
					}
                    pAudio->preloadEffect(pPath.c_str());
                    gb->addComponent(pAudio);
                }
                else if(comName != NULL && strcmp(comName, "CCComAttribute") == 0)
                {
                    ComAttribute *pAttribute = NULL;
					if (nResType == 0)
					{
						pAttribute = ComAttribute::create();
						unsigned long size = 0;
						const char* pData = 0;
						pData = (char*)(cocos2d::FileUtils::getInstance()->getFileData(pPath.c_str(), "r", &size));
						if(pData != NULL && strcmp(pData, "") != 0)
						{
							pAttribute->getDict()->initWithDescription(pData);
						}
					}
					else
					{
						CCLOG("unknown resourcetype on CCComAttribute!");
						continue;
					}
                    gb->addComponent(pAttribute);
                }
                else if (comName != NULL && strcmp(comName, "CCBackgroundAudio") == 0)
                {
					ComAudio *pAudio = NULL;
					if (nResType == 0)
					{
						pAudio = ComAudio::create();
					}
					else
					{
						continue;
					}
                    pAudio->preloadBackgroundMusic(pPath.c_str());
					pAudio->setFile(pPath.c_str());
					bool bLoop = subDict->getItemIntValue("loop", 0);
					pAudio->setLoop(bLoop);
                    gb->addComponent(pAudio);
					pAudio->playBackgroundMusic(pPath.c_str(), bLoop);
                }
				else if(comName != NULL && strcmp(comName, "GUIComponent") == 0)
				{
                    cocos2d::extension::UILayer *pLayer = cocos2d::extension::UILayer::create();
					pLayer->scheduleUpdate();
					UIWidget* widget = cocos2d::extension::UIHelper::instance()->createWidgetFromJsonFile(pPath.c_str(), listener);
					pLayer->addWidget(widget);
					ComRender *pRender = ComRender::create(pLayer, "GUIComponent");
					if (pComName != NULL)
					{
					pRender->setName(pComName);
					}
					gb->addComponent(pRender);
				}
                
                CC_SAFE_DELETE(subDict);
            }

            for (int i = 0; i < inputFiles->getArrayItemCount("gameobjects"); i++)
            {
                cs::JsonDictionary * subDict = inputFiles->getSubItemFromArray("gameobjects", i);
                if (!subDict)
                {
                    break;
                }
                createObject(subDict, gb,listener);
                CC_SAFE_DELETE(subDict);
            }
			if (listener)
				listener->OnNodeLoaded(gb);
            return gb;
        }
        
        return NULL;
    }