Fixes memory bug and adds support for loadConfigFile

This commit is contained in:
Ricardo Quesada 2013-05-22 17:03:00 -07:00
parent 836a61cd3e
commit ecf04e337c
2 changed files with 37 additions and 11 deletions

View File

@ -31,6 +31,7 @@ THE SOFTWARE.
#include "cocoa/CCInteger.h" #include "cocoa/CCInteger.h"
#include "cocoa/CCBool.h" #include "cocoa/CCBool.h"
#include "cocos2d.h" #include "cocos2d.h"
#include "CCFileUtils.h"
using namespace std; using namespace std;
@ -57,6 +58,7 @@ CCConfiguration::CCConfiguration(void)
bool CCConfiguration::init(void) bool CCConfiguration::init(void)
{ {
m_pDefaults = CCDictionary::create(); m_pDefaults = CCDictionary::create();
m_pDefaults->retain();
m_pDefaults->setObject( CCString::create( cocos2dVersion() ), "cocos2d.version"); m_pDefaults->setObject( CCString::create( cocos2dVersion() ), "cocos2d.version");
@ -221,11 +223,15 @@ bool CCConfiguration::supportsShareableVAO(void) const
const char *CCConfiguration::getCString( const char *key ) const const char *CCConfiguration::getCString( const char *key ) const
{ {
CCObject *ret = m_pDefaults->objectForKey(key); CCObject *ret = m_pDefaults->objectForKey(key);
if( ret ) if( ret ) {
if( CCString *str=dynamic_cast<CCString*>(ret) ) if( CCString *str=dynamic_cast<CCString*>(ret) )
return str->getCString(); return str->getCString();
CCAssert(false, "Key found, but from different type");
}
// XXX: Should it throw an exception ? // XXX: Should it throw an exception ?
CCLOG("Key not found: '%s'", key );
return NULL; return NULL;
} }
@ -233,11 +239,14 @@ const char *CCConfiguration::getCString( const char *key ) const
bool CCConfiguration::getBool( const char *key ) const bool CCConfiguration::getBool( const char *key ) const
{ {
CCObject *ret = m_pDefaults->objectForKey(key); CCObject *ret = m_pDefaults->objectForKey(key);
if( ret ) if( ret ) {
if( CCBool *obj=dynamic_cast<CCBool*>(ret) ) if( CCBool *obj=dynamic_cast<CCBool*>(ret) )
return obj->getValue(); return obj->getValue();
CCAssert(false, "Key found, but from different type");
}
// XXX: Should it throw an exception ? // XXX: Should it throw an exception ?
CCLOG("Key not found: '%s'", key );
return false; return false;
} }
@ -251,9 +260,12 @@ double CCConfiguration::getNumber( const char *key ) const
if( CCInteger *obj=dynamic_cast<CCInteger*>(ret) ) if( CCInteger *obj=dynamic_cast<CCInteger*>(ret) )
return obj->getValue(); return obj->getValue();
CCAssert(false, "Key found, but from different type");
} }
// XXX: Should it throw an exception ? // XXX: Should it throw an exception ?
CCLOG("Key not found: '%s'", key );
return 0.0; return 0.0;
} }
@ -262,9 +274,23 @@ CCObject * CCConfiguration::getObject( const char *key ) const
return m_pDefaults->objectForKey(key); return m_pDefaults->objectForKey(key);
} }
ccConfigurationType CCConfiguration::getType( const char *key ) const //
// load file
//
void CCConfiguration::loadConfigFile( const char *filename )
{ {
return ConfigurationError; CCDictionary *dict = CCDictionary::createWithContentsOfFile(filename);
CCAssert(dict, "cannot create dictionary");
// Add all keys in the existing dictionary
CCDictElement* element;
CCDICT_FOREACH(dict, element)
{
if( ! m_pDefaults->objectForKey( element->getStrKey() ) )
m_pDefaults->setObject(element->getObject(), element->getStrKey() );
else
CCLOG("Key already present. Ignoring '%s'", element->getStrKey() );
}
} }
NS_CC_END NS_CC_END

View File

@ -57,9 +57,11 @@ class CC_DLL CCConfiguration : public CCObject
public: public:
/** returns a shared instance of CCConfiguration */ /** returns a shared instance of CCConfiguration */
static CCConfiguration *sharedConfiguration(void); static CCConfiguration *sharedConfiguration(void);
/** purge the shared instance of CCConfiguration */ /** purge the shared instance of CCConfiguration */
static void purgeConfiguration(void); static void purgeConfiguration(void);
public:
public:
virtual ~CCConfiguration(void); virtual ~CCConfiguration(void);
@ -116,21 +118,19 @@ public:
/** returns the value of a given key as a double */ /** returns the value of a given key as a double */
CCObject * getObject( const char *key ) const; CCObject * getObject( const char *key ) const;
/** returns the type of a given key */
ccConfigurationType getType( const char *key ) const;
/** dumps the current configuration on the console */ /** dumps the current configuration on the console */
void dumpInfo(void) const; void dumpInfo(void) const;
/** loads JSON config file in the configuration */
void loadConfigFile( char *filename );
/** gathers OpenGL / GPU information */ /** gathers OpenGL / GPU information */
void gatherGPUInfo( void ); void gatherGPUInfo( void );
/** Loads a config file. If the keys are already present, then they are going to be replaced. Otherwise the new keys are added. */
void loadConfigFile( const char *filename );
private: private:
CCConfiguration(void); CCConfiguration(void);
static CCConfiguration *s_gSharedConfiguration; static CCConfiguration *s_gSharedConfiguration;
static std::string s_sConfigfile;
protected: protected:
GLint m_nMaxTextureSize; GLint m_nMaxTextureSize;