fixed #2034:make CCUserDefault compatible with previous version on Android and iOS

This commit is contained in:
minggo 2013-04-08 21:35:43 +08:00
parent 1e0cdee9d4
commit d3aa718b22
3 changed files with 532 additions and 8 deletions

View File

@ -110,11 +110,11 @@ public:
static CCUserDefault* sharedUserDefault();
static void purgeSharedUserDefault();
const static std::string& getXMLFilePath();
static bool isXMLFileExist();
private:
CCUserDefault();
static bool createXMLFile();
static bool isXMLFileExist();
static void initXMLFilePath();
static CCUserDefault* m_spUserDefault;

View File

@ -24,13 +24,25 @@
#import "CCUserDefault.h"
#import <string>
#import "platform/CCFileUtils.h"
#import "../tinyxml2/tinyxml2.h"
#import "platform/CCPlatformConfig.h"
#import "platform/CCPlatformMacros.h"
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
NS_CC_BEGIN
#define XML_FILE_NAME "UserDefault.xml"
// root name of xml
#define USERDEFAULT_ROOT_NAME "userDefaultRoot"
#define KEEP_COMPATABILITY
using namespace std;
NS_CC_BEGIN
/**
* implements of CCUserDefault
*/
@ -39,6 +51,87 @@ CCUserDefault* CCUserDefault::m_spUserDefault = 0;
string CCUserDefault::m_sFilePath = string("");
bool CCUserDefault::m_sbIsFilePathInitialized = false;
#ifdef KEEP_COMPATABILITY
static tinyxml2::XMLElement* getXMLNodeForKey(const char* pKey, tinyxml2::XMLDocument **doc)
{
tinyxml2::XMLElement* curNode = NULL;
tinyxml2::XMLElement* rootNode = NULL;
if (! CCUserDefault::isXMLFileExist())
{
return NULL;
}
// check the key value
if (! pKey)
{
return NULL;
}
do
{
tinyxml2::XMLDocument* xmlDoc = new tinyxml2::XMLDocument();
*doc = xmlDoc;
unsigned long nSize;
const char* pXmlBuffer = (const char*)CCFileUtils::sharedFileUtils()->getFileData(CCUserDefault::sharedUserDefault()->getXMLFilePath().c_str(), "rb", &nSize);
//const char* pXmlBuffer = (const char*)data.getBuffer();
if(NULL == pXmlBuffer)
{
NSLog(@"can not read xml file");
break;
}
xmlDoc->Parse(pXmlBuffer);
// get root node
rootNode = xmlDoc->RootElement();
if (NULL == rootNode)
{
NSLog(@"read root node error");
break;
}
// find the node
curNode = rootNode->FirstChildElement();
if (!curNode)
{
// There is not xml node, delete xml file.
remove(CCUserDefault::sharedUserDefault()->getXMLFilePath().c_str());
return NULL;
}
while (NULL != curNode)
{
const char* nodeName = curNode->Value();
if (!strcmp(nodeName, pKey))
{
// delete the node
break;
}
curNode = curNode->NextSiblingElement();
}
} while (0);
return curNode;
}
static void deleteNode(tinyxml2::XMLDocument* doc, tinyxml2::XMLElement* node)
{
if (node)
{
doc->DeleteNode(node);
doc->SaveFile(CCUserDefault::sharedUserDefault()->getXMLFilePath().c_str());
delete doc;
}
}
static void deleteNodeByKey(const char *pKey)
{
tinyxml2::XMLDocument* doc = NULL;
tinyxml2::XMLElement* node = getXMLNodeForKey(pKey, &doc);
deleteNode(doc, node);
}
#endif
/**
* If the user invoke delete CCUserDefault::sharedUserDefault(), should set m_spUserDefault
* to null to avoid error when he invoke CCUserDefault::sharedUserDefault() later.
@ -66,6 +159,33 @@ bool CCUserDefault::getBoolForKey(const char* pKey)
bool CCUserDefault::getBoolForKey(const char* pKey, bool defaultValue)
{
#ifdef KEEP_COMPATABILITY
tinyxml2::XMLDocument* doc = NULL;
tinyxml2::XMLElement* node = getXMLNodeForKey(pKey, &doc);
if (node)
{
if (node->FirstChild())
{
const char* value = (const char*)node->FirstChild()->Value();
bool ret = (! strcmp(value, "true"));
// set value in NSUserDefaults
setBoolForKey(pKey, ret);
flush();
// delete xmle node
deleteNode(doc, node);
return ret;
}
else
{
// delete xmle node
deleteNode(doc, node);
}
}
#endif
bool ret = defaultValue;
NSNumber *value = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithUTF8String:pKey]];
@ -84,6 +204,32 @@ int CCUserDefault::getIntegerForKey(const char* pKey)
int CCUserDefault::getIntegerForKey(const char* pKey, int defaultValue)
{
#ifdef KEEP_COMPATABILITY
tinyxml2::XMLDocument* doc = NULL;
tinyxml2::XMLElement* node = getXMLNodeForKey(pKey, &doc);
if (node)
{
if (node->FirstChild())
{
int ret = atoi((const char*)node->FirstChild()->Value());
// set value in NSUserDefaults
setIntegerForKey(pKey, ret);
flush();
// delete xmle node
deleteNode(doc, node);
return ret;
}
else
{
// delete xmle node
deleteNode(doc, node);
}
}
#endif
int ret = defaultValue;
NSNumber *value = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithUTF8String:pKey]];
@ -102,6 +248,32 @@ float CCUserDefault::getFloatForKey(const char* pKey)
float CCUserDefault::getFloatForKey(const char* pKey, float defaultValue)
{
#ifdef KEEP_COMPATABILITY
tinyxml2::XMLDocument* doc = NULL;
tinyxml2::XMLElement* node = getXMLNodeForKey(pKey, &doc);
if (node)
{
if (node->FirstChild())
{
float ret = atof((const char*)node->FirstChild()->Value());
// set value in NSUserDefaults
setFloatForKey(pKey, ret);
flush();
// delete xmle node
deleteNode(doc, node);
return ret;
}
else
{
// delete xmle node
deleteNode(doc, node);
}
}
#endif
float ret = defaultValue;
NSNumber *value = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithUTF8String:pKey]];
@ -120,6 +292,32 @@ double CCUserDefault::getDoubleForKey(const char* pKey)
double CCUserDefault::getDoubleForKey(const char* pKey, double defaultValue)
{
#ifdef KEEP_COMPATABILITY
tinyxml2::XMLDocument* doc = NULL;
tinyxml2::XMLElement* node = getXMLNodeForKey(pKey, &doc);
if (node)
{
if (node->FirstChild())
{
double ret = atof((const char*)node->FirstChild()->Value());
// set value in NSUserDefaults
setDoubleForKey(pKey, ret);
flush();
// delete xmle node
deleteNode(doc, node);
return ret;
}
else
{
// delete xmle node
deleteNode(doc, node);
}
}
#endif
double ret = defaultValue;
NSNumber *value = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithUTF8String:pKey]];
@ -138,6 +336,32 @@ std::string CCUserDefault::getStringForKey(const char* pKey)
string CCUserDefault::getStringForKey(const char* pKey, const std::string & defaultValue)
{
#ifdef KEEP_COMPATABILITY
tinyxml2::XMLDocument* doc = NULL;
tinyxml2::XMLElement* node = getXMLNodeForKey(pKey, &doc);
if (node)
{
if (node->FirstChild())
{
string ret = (const char*)node->FirstChild()->Value();
// set value in NSUserDefaults
setStringForKey(pKey, ret);
flush();
// delete xmle node
deleteNode(doc, node);
return ret;
}
else
{
// delete xmle node
deleteNode(doc, node);
}
}
#endif
NSString *str = [[NSUserDefaults standardUserDefaults] stringForKey:[NSString stringWithUTF8String:pKey]];
if (! str)
{
@ -151,31 +375,55 @@ string CCUserDefault::getStringForKey(const char* pKey, const std::string & defa
void CCUserDefault::setBoolForKey(const char* pKey, bool value)
{
#ifdef KEEP_COMPATABILITY
deleteNodeByKey(pKey);
#endif
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:value] forKey:[NSString stringWithUTF8String:pKey]];
}
void CCUserDefault::setIntegerForKey(const char* pKey, int value)
{
#ifdef KEEP_COMPATABILITY
deleteNodeByKey(pKey);
#endif
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:value] forKey:[NSString stringWithUTF8String:pKey]];
}
void CCUserDefault::setFloatForKey(const char* pKey, float value)
{
#ifdef KEEP_COMPATABILITY
deleteNodeByKey(pKey);
#endif
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithFloat:value] forKey:[NSString stringWithUTF8String:pKey]];
}
void CCUserDefault::setDoubleForKey(const char* pKey, double value)
{
#ifdef KEEP_COMPATABILITY
deleteNodeByKey(pKey);
#endif
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithDouble:value] forKey:[NSString stringWithUTF8String:pKey]];
}
void CCUserDefault::setStringForKey(const char* pKey, const std::string & value)
{
#ifdef KEEP_COMPATABILITY
deleteNodeByKey(pKey);
#endif
[[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithUTF8String:value.c_str()] forKey:[NSString stringWithUTF8String:pKey]];
}
CCUserDefault* CCUserDefault::sharedUserDefault()
{
#ifdef KEEP_COMPATABILITY
initXMLFilePath();
#endif
if (! m_spUserDefault)
{
m_spUserDefault = new CCUserDefault();
@ -186,18 +434,33 @@ CCUserDefault* CCUserDefault::sharedUserDefault()
bool CCUserDefault::isXMLFileExist()
{
return false;
FILE *fp = fopen(m_sFilePath.c_str(), "r");
bool bRet = false;
if (fp)
{
bRet = true;
fclose(fp);
}
return bRet;
}
void CCUserDefault::initXMLFilePath()
{
#ifdef KEEP_COMPATABILITY
if (! m_sbIsFilePathInitialized)
{
m_sFilePath += CCFileUtils::sharedFileUtils()->getWritablePath() + XML_FILE_NAME;
m_sbIsFilePathInitialized = true;
}
#endif
}
// create new xml file
bool CCUserDefault::createXMLFile()
{
return false;
return false;
}
const string& CCUserDefault::getXMLFilePath()
@ -207,6 +470,7 @@ const string& CCUserDefault::getXMLFilePath()
void CCUserDefault::flush()
{
[[NSUserDefaults standardUserDefaults] synchronize];
}

View File

@ -23,6 +23,7 @@ THE SOFTWARE.
****************************************************************************/
#include "CCUserDefault.h"
#include "platform/CCPlatformConfig.h"
#include "platform/CCCommon.h"
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include "platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h"
@ -30,8 +31,15 @@ THE SOFTWARE.
// root name of xml
#define USERDEFAULT_ROOT_NAME "userDefaultRoot"
#define KEEP_COMPATABILITY
#define XML_FILE_NAME "UserDefault.xml"
#ifdef KEEP_COMPATABILITY
#include "platform/CCFileUtils.h"
#include "../tinyxml2/tinyxml2.h"
#endif
using namespace std;
NS_CC_BEGIN
@ -44,6 +52,87 @@ CCUserDefault* CCUserDefault::m_spUserDefault = 0;
string CCUserDefault::m_sFilePath = string("");
bool CCUserDefault::m_sbIsFilePathInitialized = false;
#ifdef KEEP_COMPATABILITY
static tinyxml2::XMLElement* getXMLNodeForKey(const char* pKey, tinyxml2::XMLDocument **doc)
{
tinyxml2::XMLElement* curNode = NULL;
tinyxml2::XMLElement* rootNode = NULL;
if (! CCUserDefault::isXMLFileExist())
{
return NULL;
}
// check the key value
if (! pKey)
{
return NULL;
}
do
{
tinyxml2::XMLDocument* xmlDoc = new tinyxml2::XMLDocument();
*doc = xmlDoc;
unsigned long nSize;
const char* pXmlBuffer = (const char*)CCFileUtils::sharedFileUtils()->getFileData(CCUserDefault::sharedUserDefault()->getXMLFilePath().c_str(), "rb", &nSize);
//const char* pXmlBuffer = (const char*)data.getBuffer();
if(NULL == pXmlBuffer)
{
CCLOG("can not read xml file");
break;
}
xmlDoc->Parse(pXmlBuffer);
// get root node
rootNode = xmlDoc->RootElement();
if (NULL == rootNode)
{
CCLOG("read root node error");
break;
}
// find the node
curNode = rootNode->FirstChildElement();
if (!curNode)
{
// There is not xml node, delete xml file.
remove(CCUserDefault::sharedUserDefault()->getXMLFilePath().c_str());
return NULL;
}
while (NULL != curNode)
{
const char* nodeName = curNode->Value();
if (!strcmp(nodeName, pKey))
{
// delete the node
break;
}
curNode = curNode->NextSiblingElement();
}
} while (0);
return curNode;
}
static void deleteNode(tinyxml2::XMLDocument* doc, tinyxml2::XMLElement* node)
{
if (node)
{
doc->DeleteNode(node);
doc->SaveFile(CCUserDefault::sharedUserDefault()->getXMLFilePath().c_str());
delete doc;
}
}
static void deleteNodeByKey(const char *pKey)
{
tinyxml2::XMLDocument* doc = NULL;
tinyxml2::XMLElement* node = getXMLNodeForKey(pKey, &doc);
deleteNode(doc, node);
}
#endif
/**
* If the user invoke delete CCUserDefault::sharedUserDefault(), should set m_spUserDefault
* to null to avoid error when he invoke CCUserDefault::sharedUserDefault() later.
@ -71,6 +160,33 @@ void CCUserDefault::purgeSharedUserDefault()
bool CCUserDefault::getBoolForKey(const char* pKey, bool defaultValue)
{
#ifdef KEEP_COMPATABILITY
tinyxml2::XMLDocument* doc = NULL;
tinyxml2::XMLElement* node = getXMLNodeForKey(pKey, &doc);
if (node)
{
if (node->FirstChild())
{
const char* value = (const char*)node->FirstChild()->Value();
bool ret = (! strcmp(value, "true"));
// set value in NSUserDefaults
setBoolForKey(pKey, ret);
flush();
// delete xmle node
deleteNode(doc, node);
return ret;
}
else
{
// delete xmle node
deleteNode(doc, node);
}
}
#endif
return getBoolForKeyJNI(pKey, defaultValue);
}
@ -81,6 +197,32 @@ int CCUserDefault::getIntegerForKey(const char* pKey)
int CCUserDefault::getIntegerForKey(const char* pKey, int defaultValue)
{
#ifdef KEEP_COMPATABILITY
tinyxml2::XMLDocument* doc = NULL;
tinyxml2::XMLElement* node = getXMLNodeForKey(pKey, &doc);
if (node)
{
if (node->FirstChild())
{
int ret = atoi((const char*)node->FirstChild()->Value());
// set value in NSUserDefaults
setIntegerForKey(pKey, ret);
flush();
// delete xmle node
deleteNode(doc, node);
return ret;
}
else
{
// delete xmle node
deleteNode(doc, node);
}
}
#endif
return getIntegerForKeyJNI(pKey, defaultValue);
}
@ -91,6 +233,32 @@ float CCUserDefault::getFloatForKey(const char* pKey)
float CCUserDefault::getFloatForKey(const char* pKey, float defaultValue)
{
#ifdef KEEP_COMPATABILITY
tinyxml2::XMLDocument* doc = NULL;
tinyxml2::XMLElement* node = getXMLNodeForKey(pKey, &doc);
if (node)
{
if (node->FirstChild())
{
float ret = atof((const char*)node->FirstChild()->Value());
// set value in NSUserDefaults
setFloatForKey(pKey, ret);
flush();
// delete xmle node
deleteNode(doc, node);
return ret;
}
else
{
// delete xmle node
deleteNode(doc, node);
}
}
#endif
return getFloatForKeyJNI(pKey, defaultValue);
}
@ -101,6 +269,32 @@ double CCUserDefault::getDoubleForKey(const char* pKey)
double CCUserDefault::getDoubleForKey(const char* pKey, double defaultValue)
{
#ifdef KEEP_COMPATABILITY
tinyxml2::XMLDocument* doc = NULL;
tinyxml2::XMLElement* node = getXMLNodeForKey(pKey, &doc);
if (node)
{
if (node->FirstChild())
{
double ret = atof((const char*)node->FirstChild()->Value());
// set value in NSUserDefaults
setDoubleForKey(pKey, ret);
flush();
// delete xmle node
deleteNode(doc, node);
return ret;
}
else
{
// delete xmle node
deleteNode(doc, node);
}
}
#endif
return getDoubleForKeyJNI(pKey, defaultValue);
}
@ -111,36 +305,86 @@ std::string CCUserDefault::getStringForKey(const char* pKey)
string CCUserDefault::getStringForKey(const char* pKey, const std::string & defaultValue)
{
#ifdef KEEP_COMPATABILITY
tinyxml2::XMLDocument* doc = NULL;
tinyxml2::XMLElement* node = getXMLNodeForKey(pKey, &doc);
if (node)
{
if (node->FirstChild())
{
string ret = (const char*)node->FirstChild()->Value();
// set value in NSUserDefaults
setStringForKey(pKey, ret);
flush();
// delete xmle node
deleteNode(doc, node);
return ret;
}
else
{
// delete xmle node
deleteNode(doc, node);
}
}
#endif
return getStringForKeyJNI(pKey, defaultValue.c_str());
}
void CCUserDefault::setBoolForKey(const char* pKey, bool value)
{
#ifdef KEEP_COMPATABILITY
deleteNodeByKey(pKey);
#endif
return setBoolForKeyJNI(pKey, value);
}
void CCUserDefault::setIntegerForKey(const char* pKey, int value)
{
#ifdef KEEP_COMPATABILITY
deleteNodeByKey(pKey);
#endif
return setIntegerForKeyJNI(pKey, value);
}
void CCUserDefault::setFloatForKey(const char* pKey, float value)
{
#ifdef KEEP_COMPATABILITY
deleteNodeByKey(pKey);
#endif
return setFloatForKeyJNI(pKey, value);
}
void CCUserDefault::setDoubleForKey(const char* pKey, double value)
{
#ifdef KEEP_COMPATABILITY
deleteNodeByKey(pKey);
#endif
return setDoubleForKeyJNI(pKey, value);
}
void CCUserDefault::setStringForKey(const char* pKey, const std::string & value)
{
#ifdef KEEP_COMPATABILITY
deleteNodeByKey(pKey);
#endif
return setStringForKeyJNI(pKey, value.c_str());
}
CCUserDefault* CCUserDefault::sharedUserDefault()
{
#ifdef KEEP_COMPATABILITY
initXMLFilePath();
#endif
if (! m_spUserDefault)
{
m_spUserDefault = new CCUserDefault();
@ -151,17 +395,33 @@ CCUserDefault* CCUserDefault::sharedUserDefault()
bool CCUserDefault::isXMLFileExist()
{
return false;
FILE *fp = fopen(m_sFilePath.c_str(), "r");
bool bRet = false;
if (fp)
{
bRet = true;
fclose(fp);
}
return bRet;
}
void CCUserDefault::initXMLFilePath()
{
{
#ifdef KEEP_COMPATABILITY
if (! m_sbIsFilePathInitialized)
{
m_sFilePath += CCFileUtils::sharedFileUtils()->getWritablePath() + XML_FILE_NAME;
m_sbIsFilePathInitialized = true;
}
#endif
}
// create new xml file
bool CCUserDefault::createXMLFile()
{
return false;
return false;
}
const string& CCUserDefault::getXMLFilePath()