// TODO: This method is soooo big!! We should refactor it! bool ComRender::serialize(void* r) { bool ret = false; do { CC_BREAK_IF(r == nullptr); SerData *serData = (SerData *)(r); const rapidjson::Value *v = serData->_rData; stExpCocoNode *cocoNode = serData->_cocoNode; CocoLoader *cocoLoader = serData->_cocoLoader; const char *className = nullptr; const char *comName = nullptr; const char *file = nullptr; const char *plist = nullptr; std::string filePath; std::string plistPath; int resType = 0; if (v != nullptr) { className = DICTOOL->getStringValue_json(*v, "classname"); CC_BREAK_IF(className == nullptr); comName = DICTOOL->getStringValue_json(*v, "name"); const rapidjson::Value &fileData = DICTOOL->getSubDictionary_json(*v, "fileData"); CC_BREAK_IF(!DICTOOL->checkObjectExist_json(fileData)); file = DICTOOL->getStringValue_json(fileData, "path"); plist = DICTOOL->getStringValue_json(fileData, "plistFile"); CC_BREAK_IF(file == nullptr && plist == nullptr); resType = DICTOOL->getIntValue_json(fileData, "resourceType", -1); } else if(cocoNode != nullptr) { className = cocoNode[1].GetValue(cocoLoader); CC_BREAK_IF(className == nullptr); comName = cocoNode[2].GetValue(cocoLoader); stExpCocoNode *pfileData = cocoNode[4].GetChildArray(cocoLoader); CC_BREAK_IF(!pfileData); file = pfileData[0].GetValue(cocoLoader); plist = pfileData[1].GetValue(cocoLoader); CC_BREAK_IF(file == nullptr && plist == nullptr); resType = atoi(pfileData[2].GetValue(cocoLoader)); } if (comName != nullptr) { setName(comName); } else { setName(className); } if (file != nullptr) { filePath.assign(cocos2d::FileUtils::getInstance()->fullPathForFilename(file)); } if (plist != nullptr) { plistPath.assign(cocos2d::FileUtils::getInstance()->fullPathForFilename(plist)); } if (resType == 0) { if (strcmp(className, "CCSprite") == 0 && (filePath.find(".png") != filePath.npos || filePath.find(".pvr.ccz") != filePath.npos)) { _render = Sprite::create(filePath.c_str()); _render->retain(); ret = true; } else if(strcmp(className, "CCTMXTiledMap") == 0 && filePath.find(".tmx") != filePath.npos) { _render = TMXTiledMap::create(filePath.c_str()); _render->retain(); ret = true; } else if(strcmp(className, "CCParticleSystemQuad") == 0 && filePath.find(".plist") != filePath.npos) { _render = ParticleSystemQuad::create(filePath.c_str()); _render->setPosition(0.0f, 0.0f); _render->retain(); ret = true; } else if(strcmp(className, "CCArmature") == 0) { std::string fileExtension = FileUtils::getInstance()->getFileExtension(filePath); if (fileExtension == ".json" || fileExtension == ".exportjson") { rapidjson::Document doc; if(!readJson(filePath.c_str(), doc)) { log("read json file[%s] error!\n", filePath.c_str()); continue; } const rapidjson::Value &subData = DICTOOL->getDictionaryFromArray_json(doc, "armature_data", 0); const char *name = DICTOOL->getStringValue_json(subData, "name"); ArmatureDataManager::getInstance()->addArmatureFileInfo(filePath.c_str()); Armature *pAr = Armature::create(name); _render = pAr; _render->retain(); const char *actionName = nullptr; if (cocoNode != nullptr) { actionName = cocoNode[6].GetValue(cocoLoader);//DICTOOL->getStringValue_json(*v, "selectedactionname"); } else { actionName = DICTOOL->getStringValue_json(*v, "selectedactionname"); } if (actionName != nullptr && pAr->getAnimation() != nullptr) { pAr->getAnimation()->play(actionName); } ret = true; } else if (fileExtension == ".csb") { std::string binaryFilePath = FileUtils::getInstance()->fullPathForFilename(filePath.c_str()); 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) { int count = tpRootCocoNode->GetChildNum(); stExpCocoNode *tpChildArray = tpRootCocoNode->GetChildArray(&tCocoLoader); for (int i = 0; i < count; ++i) { std::string key = tpChildArray[i].GetName(&tCocoLoader); if (key.compare("armature_data") == 0) { int length = tpChildArray[i].GetChildNum(); stExpCocoNode *armature_dataArray = tpChildArray[i].GetChildArray(&tCocoLoader); if (length < 1) { continue; } length = armature_dataArray[0].GetChildNum(); stExpCocoNode *armature_data = armature_dataArray[0].GetChildArray(&tCocoLoader); for (int j = 0; j < length; ++j) { std::string key1 = armature_data[j].GetName(&tCocoLoader); const char *str1 = armature_data[j].GetValue(&tCocoLoader); if (key.compare("name") == 0) { if (str1 != nullptr) { ArmatureDataManager::getInstance()->addArmatureFileInfo(filePath.c_str()); Armature *pAr = Armature::create(str1); _render = pAr; _render->retain(); const char *actionName = nullptr; if (cocoNode != nullptr) { actionName = cocoNode[6].GetValue(&tCocoLoader); } else { actionName = DICTOOL->getStringValue_json(*v, "selectedactionname"); } if (actionName != nullptr && pAr->getAnimation() != nullptr) { pAr->getAnimation()->play(actionName); } ret = true; } } } } } } } else { continue; } } else { continue; } } else if(strcmp(className, "GUIComponent") == 0) { std::string fileExtension = FileUtils::getInstance()->getFileExtension(filePath); if (fileExtension == ".json" || fileExtension == ".exportjson") { cocos2d::ui::Widget* widget = GUIReader::getInstance()->widgetFromJsonFile(filePath.c_str()); _render = widget; _render->retain(); ret = true; } else if (fileExtension == ".csb") { cocos2d::ui::Widget* widget = GUIReader::getInstance()->widgetFromBinaryFile(filePath.c_str()); _render = widget; _render->retain(); ret = true; } } else { CC_BREAK_IF(true); } } else if (resType == 1) { if (strcmp(className, "CCSprite") == 0) { std::string strPngFile = plistPath; std::string::size_type pos = strPngFile.find(".plist"); if (pos == strPngFile.npos) { continue; } strPngFile.replace(pos, strPngFile.length(), ".png"); SpriteFrameCache::getInstance()->addSpriteFramesWithFile(plistPath.c_str(), strPngFile.c_str()); _render = Sprite::createWithSpriteFrameName(filePath.c_str()); _render->retain(); ret = true; } else { CC_BREAK_IF(true); } } else { CC_BREAK_IF(true); } } while (0); return ret; }
bool CCComRender::serialize(void* r) { bool bRet = false; do { CC_BREAK_IF(r == NULL); SerData *pSerData = (SerData *)(r); const rapidjson::Value *v = pSerData->prData; stExpCocoNode *pCocoNode = pSerData->pCocoNode; const char *pClassName = NULL; const char *pComName = NULL; const char *pFile = NULL; const char *pPlist = NULL; std::string strFilePath; std::string strPlistPath; int nResType = 0; if (v != NULL) { pClassName = DICTOOL->getStringValue_json(*v, "classname"); CC_BREAK_IF(pClassName == NULL); pComName = DICTOOL->getStringValue_json(*v, "name"); const rapidjson::Value &fileData = DICTOOL->getSubDictionary_json(*v, "fileData"); CC_BREAK_IF(!DICTOOL->checkObjectExist_json(fileData)); pFile = DICTOOL->getStringValue_json(fileData, "path"); pPlist = DICTOOL->getStringValue_json(fileData, "plistFile"); CC_BREAK_IF(pFile == NULL && pPlist == NULL); nResType = DICTOOL->getIntValue_json(fileData, "resourceType", -1); } else if(pCocoNode != NULL) { pClassName = pCocoNode[1].GetValue(); CC_BREAK_IF(pClassName == NULL); pComName = pCocoNode[2].GetValue(); stExpCocoNode *pfileData = pCocoNode[4].GetChildArray(); CC_BREAK_IF(!pfileData); pFile = pfileData[0].GetValue(); pPlist = pfileData[1].GetValue(); CC_BREAK_IF(pFile == NULL && pPlist == NULL); nResType = atoi(pfileData[2].GetValue()); } if (pComName != NULL) { setName(pComName); } else { setName(pClassName); } if (pFile != NULL) { strFilePath.assign(cocos2d::CCFileUtils::sharedFileUtils()->fullPathForFilename(pFile)); } if (pPlist != NULL) { strPlistPath.assign(cocos2d::CCFileUtils::sharedFileUtils()->fullPathForFilename(pPlist)); } if (nResType == 0) { if (strcmp(pClassName, "CCSprite") == 0 && (strFilePath.find(".png") != strFilePath.npos || strFilePath.find(".pvr.ccz") != strFilePath.npos)) { m_pRender = CCSprite::create(strFilePath.c_str()); m_pRender->retain(); bRet = true; } else if(strcmp(pClassName, "CCTMXTiledMap") == 0 && strFilePath.find(".tmx") != strFilePath.npos) { m_pRender = CCTMXTiledMap::create(strFilePath.c_str()); m_pRender->retain(); bRet = true; } else if(strcmp(pClassName, "CCParticleSystemQuad") == 0 && strFilePath.find(".plist") != strFilePath.npos) { m_pRender = CCParticleSystemQuad::create(strFilePath.c_str()); m_pRender->setPosition(ccp(0.0f, 0.0f)); m_pRender->retain(); bRet = true; } else if(strcmp(pClassName, "CCArmature") == 0) { std::string file_extension = strFilePath; size_t pos = strFilePath.find_last_of('.'); if (pos != std::string::npos) { file_extension = strFilePath.substr(pos, strFilePath.length()); std::transform(file_extension.begin(),file_extension.end(), file_extension.begin(), (int(*)(int))toupper); } if (file_extension == ".JSON" || file_extension == ".EXPORTJSON") { rapidjson::Document doc; if(!readJson(strFilePath.c_str(), doc)) { CCLog("read json file[%s] error!\n", strFilePath.c_str()); continue; } const rapidjson::Value &subData = DICTOOL->getDictionaryFromArray_json(doc, "armature_data", 0); const char *name = DICTOOL->getStringValue_json(subData, "name"); CCArmatureDataManager::sharedArmatureDataManager()->addArmatureFileInfo(strFilePath.c_str()); CCArmature *pAr = CCArmature::create(name); m_pRender = pAr; m_pRender->retain(); const char *actionName = NULL; if (pCocoNode != NULL) { actionName = pCocoNode[6].GetValue();//DICTOOL->getStringValue_json(*v, "selectedactionname"); } else { actionName = DICTOOL->getStringValue_json(*v, "selectedactionname"); } if (actionName != NULL && pAr->getAnimation() != NULL) { pAr->getAnimation()->play(actionName); } bRet = true; } else if (file_extension == ".CSB") { unsigned long size = 0; unsigned char *pBytes = NULL; std::string binaryFilePath = CCFileUtils::sharedFileUtils()->fullPathForFilename(strFilePath.c_str()); pBytes = cocos2d::CCFileUtils::sharedFileUtils()->getFileData(binaryFilePath.c_str(), "rb", &size); CC_BREAK_IF(pBytes == NULL || strcmp((char*)pBytes, "") == 0); CocoLoader tCocoLoader; if (tCocoLoader.ReadCocoBinBuff((char*)pBytes)) { stExpCocoNode *tpRootCocoNode = tCocoLoader.GetRootCocoNode(); rapidjson::Type tType = tpRootCocoNode->GetType(&tCocoLoader); if (rapidjson::kObjectType == tType) { int count = tpRootCocoNode->GetChildNum(); stExpCocoNode *tpChildArray = tpRootCocoNode->GetChildArray(); for (int i = 0; i < count; ++i) { std::string key = tpChildArray[i].GetName(&tCocoLoader); const char *str = tpChildArray[i].GetValue(); if (key.compare("armature_data") == 0) { int length = tpChildArray[i].GetChildNum(); stExpCocoNode *armature_dataArray = tpChildArray[i].GetChildArray(); if (length < 1) { continue; } length = armature_dataArray[0].GetChildNum(); stExpCocoNode *armature_data = armature_dataArray[0].GetChildArray(); for (int i = 0; i < length; ++i) { std::string key = armature_data[i].GetName(&tCocoLoader); const char *str = armature_data[i].GetValue(); if (key.compare("name") == 0) { if (str != NULL) { CCArmatureDataManager::sharedArmatureDataManager()->addArmatureFileInfo(strFilePath.c_str()); CCArmature *pAr = CCArmature::create(str); m_pRender = pAr; m_pRender->retain(); const char *actionName = NULL; if (pCocoNode != NULL) { actionName = pCocoNode[6].GetValue();//DICTOOL->getStringValue_json(*v, "selectedactionname"); } else { actionName = DICTOOL->getStringValue_json(*v, "selectedactionname"); } if (actionName != NULL && pAr->getAnimation() != NULL) { pAr->getAnimation()->play(actionName); } bRet = true; } } } } } } } else { continue; } } else { continue; } } else if(strcmp(pClassName, "GUIComponent") == 0) { std::string file_extension = strFilePath; size_t pos = strFilePath.find_last_of('.'); if (pos != std::string::npos) { file_extension = strFilePath.substr(pos, strFilePath.length()); std::transform(file_extension.begin(),file_extension.end(), file_extension.begin(), (int(*)(int))toupper); } if (file_extension == ".JSON" || file_extension == ".EXPORTJSON") { cocos2d::ui::TouchGroup* tg = cocos2d::ui::TouchGroup::create(); cocos2d::ui::Widget* widget = cocos2d::extension::GUIReader::shareReader()->widgetFromJsonFile(strFilePath.c_str()); tg->addWidget(widget); m_pRender = tg; m_pRender->retain(); bRet = true; } else if (file_extension == ".CSB") { cocos2d::ui::TouchGroup* tg = cocos2d::ui::TouchGroup::create(); cocos2d::ui::Widget* widget = cocos2d::extension::GUIReader::shareReader()->widgetFromBinaryFile(strFilePath.c_str()); tg->addWidget(widget); m_pRender = tg; m_pRender->retain(); bRet = true; } } else { CC_BREAK_IF(true); } } else if (nResType == 1) { if (strcmp(pClassName, "CCSprite") == 0) { std::string strPngFile = strPlistPath; std::string::size_type pos = strPngFile.find(".plist"); if (pos == strPngFile.npos) { continue; } strPngFile.replace(pos, strPngFile.length(), ".png"); CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(strPlistPath.c_str(), strPngFile.c_str()); m_pRender = CCSprite::createWithSpriteFrameName(strFilePath.c_str()); m_pRender->retain(); bRet = true; } else { CC_BREAK_IF(true); } } else { CC_BREAK_IF(true); } } while (0); return bRet; }
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; }