Merge pull request from ricardoquesada/configuration_format_2

CCConfiguration has format
This commit is contained in:
minggo 2013-05-29 01:59:34 -07:00
commit 05db67e8ae
3 changed files with 58 additions and 3 deletions
cocos2dx
samples/Cpp/TestCpp/Classes/ConfigurationTest

View File

@ -282,9 +282,38 @@ void CCConfiguration::loadConfigFile( const char *filename )
CCDictionary *dict = CCDictionary::createWithContentsOfFile(filename); CCDictionary *dict = CCDictionary::createWithContentsOfFile(filename);
CCAssert(dict, "cannot create dictionary"); CCAssert(dict, "cannot create dictionary");
// search for metadata
bool metadata_ok = false;
CCObject *metadata = dict->objectForKey("metadata");
if( metadata && dynamic_cast<CCDictionary*>(metadata) ) {
CCObject *format_o = static_cast<CCDictionary*>(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<CCString*>(format_o) ) {
int format = static_cast<CCString*>(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<CCDictionary*>(data) ) {
CCLOG("Expected 'data' dict, but not found. Config file: %s", filename);
return;
}
// Add all keys in the existing dictionary // Add all keys in the existing dictionary
CCDictionary *data_dict = static_cast<CCDictionary*>(data);
CCDictElement* element; CCDictElement* element;
CCDICT_FOREACH(dict, element) CCDICT_FOREACH(data_dict, element)
{ {
if( ! m_pDefaults->objectForKey( element->getStrKey() ) ) if( ! m_pDefaults->objectForKey( element->getStrKey() ) )
m_pDefaults->setObject(element->getObject(), element->getStrKey() ); m_pDefaults->setObject(element->getObject(), element->getStrKey() );

View File

@ -5,10 +5,12 @@
TESTLAYER_CREATE_FUNC(ConfigurationLoadConfig); TESTLAYER_CREATE_FUNC(ConfigurationLoadConfig);
TESTLAYER_CREATE_FUNC(ConfigurationQuery); TESTLAYER_CREATE_FUNC(ConfigurationQuery);
TESTLAYER_CREATE_FUNC(ConfigurationInvalid);
static NEWTESTFUNC createFunctions[] = { static NEWTESTFUNC createFunctions[] = {
CF(ConfigurationLoadConfig), CF(ConfigurationLoadConfig),
CF(ConfigurationQuery) CF(ConfigurationQuery),
CF(ConfigurationInvalid)
}; };
static int sceneIdx=-1; static int sceneIdx=-1;
@ -140,7 +142,7 @@ void ConfigurationLoadConfig::onEnter()
{ {
ConfigurationBase::onEnter(); ConfigurationBase::onEnter();
CCConfiguration::sharedConfiguration()->loadConfigFile("animations/animations.plist"); CCConfiguration::sharedConfiguration()->loadConfigFile("configs/config-test-ok.plist");
CCConfiguration::sharedConfiguration()->dumpInfo(); CCConfiguration::sharedConfiguration()->dumpInfo();
} }
@ -167,3 +169,20 @@ std::string ConfigurationQuery::subtitle()
{ {
return "Using getCString(). Check the console"; 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";
}

View File

@ -45,4 +45,11 @@ public:
virtual std::string subtitle(); virtual std::string subtitle();
}; };
class ConfigurationInvalid : public ConfigurationBase
{
public:
virtual void onEnter();
virtual std::string subtitle();
};
#endif // __CONFIGURATIONTEST_H__ #endif // __CONFIGURATIONTEST_H__