diff --git a/cocos2dx/CCConfiguration.cpp b/cocos2dx/CCConfiguration.cpp index ccbb5afe77..154efb8ceb 100644 --- a/cocos2dx/CCConfiguration.cpp +++ b/cocos2dx/CCConfiguration.cpp @@ -282,9 +282,38 @@ void CCConfiguration::loadConfigFile( const char *filename ) CCDictionary *dict = CCDictionary::createWithContentsOfFile(filename); CCAssert(dict, "cannot create dictionary"); + // search for metadata + bool metadata_ok = false; + CCObject *metadata = dict->objectForKey("metadata"); + if( metadata && dynamic_cast(metadata) ) { + CCObject *format_o = static_cast(metadata)->objectForKey("format"); + + // XXX: cocos2d-x returns CCStrings when importing from .plist. This bug will be addressed in cocos2d-x v3.x + if( format_o && dynamic_cast(format_o) ) { + int format = static_cast(format_o)->intValue(); + + // Support format: 1 + if( format == 1 ) { + metadata_ok = true; + } + } + } + + if( ! metadata_ok ) { + CCLOG("Invalid config format for file: %s", filename); + return; + } + + CCObject *data = dict->objectForKey("data"); + if( !data || !dynamic_cast(data) ) { + CCLOG("Expected 'data' dict, but not found. Config file: %s", filename); + return; + } + // Add all keys in the existing dictionary + CCDictionary *data_dict = static_cast(data); CCDictElement* element; - CCDICT_FOREACH(dict, element) + CCDICT_FOREACH(data_dict, element) { if( ! m_pDefaults->objectForKey( element->getStrKey() ) ) m_pDefaults->setObject(element->getObject(), element->getStrKey() ); diff --git a/samples/Cpp/TestCpp/Classes/ConfigurationTest/ConfigurationTest.cpp b/samples/Cpp/TestCpp/Classes/ConfigurationTest/ConfigurationTest.cpp index 94d8bd1c2d..80ab32d74a 100644 --- a/samples/Cpp/TestCpp/Classes/ConfigurationTest/ConfigurationTest.cpp +++ b/samples/Cpp/TestCpp/Classes/ConfigurationTest/ConfigurationTest.cpp @@ -5,10 +5,12 @@ TESTLAYER_CREATE_FUNC(ConfigurationLoadConfig); TESTLAYER_CREATE_FUNC(ConfigurationQuery); +TESTLAYER_CREATE_FUNC(ConfigurationInvalid); static NEWTESTFUNC createFunctions[] = { CF(ConfigurationLoadConfig), - CF(ConfigurationQuery) + CF(ConfigurationQuery), + CF(ConfigurationInvalid) }; static int sceneIdx=-1; @@ -140,7 +142,7 @@ void ConfigurationLoadConfig::onEnter() { ConfigurationBase::onEnter(); - CCConfiguration::sharedConfiguration()->loadConfigFile("animations/animations.plist"); + CCConfiguration::sharedConfiguration()->loadConfigFile("configs/config-test-ok.plist"); CCConfiguration::sharedConfiguration()->dumpInfo(); } @@ -167,3 +169,20 @@ std::string ConfigurationQuery::subtitle() { return "Using getCString(). Check the console"; } + +//------------------------------------------------------------------ +// +// ConfigurationInvalid +// +//------------------------------------------------------------------ +void ConfigurationInvalid::onEnter() +{ + ConfigurationBase::onEnter(); + + CCConfiguration::sharedConfiguration()->loadConfigFile("configs/config-test-invalid.plist"); +} + +std::string ConfigurationInvalid::subtitle() +{ + return "Loading an invalid config file"; +} diff --git a/samples/Cpp/TestCpp/Classes/ConfigurationTest/ConfigurationTest.h b/samples/Cpp/TestCpp/Classes/ConfigurationTest/ConfigurationTest.h index 656aa9c9ab..7f2cbc1e93 100644 --- a/samples/Cpp/TestCpp/Classes/ConfigurationTest/ConfigurationTest.h +++ b/samples/Cpp/TestCpp/Classes/ConfigurationTest/ConfigurationTest.h @@ -45,4 +45,11 @@ public: virtual std::string subtitle(); }; +class ConfigurationInvalid : public ConfigurationBase +{ +public: + virtual void onEnter(); + virtual std::string subtitle(); +}; + #endif // __CONFIGURATIONTEST_H__