mirror of https://github.com/axmolengine/axmol.git
add UserDefault api deleteValueForKey
This commit is contained in:
parent
faab927f1b
commit
6f3b977c1e
|
@ -60,24 +60,24 @@ static tinyxml2::XMLElement* getXMLNodeForKey(const char* pKey, tinyxml2::XMLDoc
|
||||||
{
|
{
|
||||||
tinyxml2::XMLElement* curNode = nullptr;
|
tinyxml2::XMLElement* curNode = nullptr;
|
||||||
tinyxml2::XMLElement* rootNode = nullptr;
|
tinyxml2::XMLElement* rootNode = nullptr;
|
||||||
|
|
||||||
if (! UserDefault::isXMLFileExist())
|
if (! UserDefault::isXMLFileExist())
|
||||||
{
|
{
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
// check the key value
|
// check the key value
|
||||||
if (! pKey)
|
if (! pKey)
|
||||||
{
|
{
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
tinyxml2::XMLDocument* xmlDoc = new tinyxml2::XMLDocument();
|
tinyxml2::XMLDocument* xmlDoc = new tinyxml2::XMLDocument();
|
||||||
*doc = xmlDoc;
|
*doc = xmlDoc;
|
||||||
ssize_t size;
|
ssize_t size;
|
||||||
|
|
||||||
std::string xmlBuffer = FileUtils::getInstance()->getStringFromFile(UserDefault::getInstance()->getXMLFilePath().c_str());
|
std::string xmlBuffer = FileUtils::getInstance()->getStringFromFile(UserDefault::getInstance()->getXMLFilePath().c_str());
|
||||||
|
|
||||||
if (xmlBuffer.empty())
|
if (xmlBuffer.empty())
|
||||||
|
@ -100,10 +100,10 @@ static tinyxml2::XMLElement* getXMLNodeForKey(const char* pKey, tinyxml2::XMLDoc
|
||||||
{
|
{
|
||||||
// There is not xml node, delete xml file.
|
// There is not xml node, delete xml file.
|
||||||
remove(UserDefault::getInstance()->getXMLFilePath().c_str());
|
remove(UserDefault::getInstance()->getXMLFilePath().c_str());
|
||||||
|
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (nullptr != curNode)
|
while (nullptr != curNode)
|
||||||
{
|
{
|
||||||
const char* nodeName = curNode->Value();
|
const char* nodeName = curNode->Value();
|
||||||
|
@ -112,11 +112,11 @@ static tinyxml2::XMLElement* getXMLNodeForKey(const char* pKey, tinyxml2::XMLDoc
|
||||||
// delete the node
|
// delete the node
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
curNode = curNode->NextSiblingElement();
|
curNode = curNode->NextSiblingElement();
|
||||||
}
|
}
|
||||||
} while (0);
|
} while (0);
|
||||||
|
|
||||||
return curNode;
|
return curNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -173,14 +173,14 @@ bool UserDefault::getBoolForKey(const char* pKey, bool defaultValue)
|
||||||
{
|
{
|
||||||
const char* value = (const char*)node->FirstChild()->Value();
|
const char* value = (const char*)node->FirstChild()->Value();
|
||||||
bool ret = (! strcmp(value, "true"));
|
bool ret = (! strcmp(value, "true"));
|
||||||
|
|
||||||
// set value in NSUserDefaults
|
// set value in NSUserDefaults
|
||||||
setBoolForKey(pKey, ret);
|
setBoolForKey(pKey, ret);
|
||||||
flush();
|
flush();
|
||||||
|
|
||||||
// delete xmle node
|
// delete xmle node
|
||||||
deleteNode(doc, node);
|
deleteNode(doc, node);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -209,14 +209,14 @@ int UserDefault::getIntegerForKey(const char* pKey, int defaultValue)
|
||||||
if (node->FirstChild())
|
if (node->FirstChild())
|
||||||
{
|
{
|
||||||
int ret = atoi((const char*)node->FirstChild()->Value());
|
int ret = atoi((const char*)node->FirstChild()->Value());
|
||||||
|
|
||||||
// set value in NSUserDefaults
|
// set value in NSUserDefaults
|
||||||
setIntegerForKey(pKey, ret);
|
setIntegerForKey(pKey, ret);
|
||||||
flush();
|
flush();
|
||||||
|
|
||||||
// delete xmle node
|
// delete xmle node
|
||||||
deleteNode(doc, node);
|
deleteNode(doc, node);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -226,7 +226,7 @@ int UserDefault::getIntegerForKey(const char* pKey, int defaultValue)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return getIntegerForKeyJNI(pKey, defaultValue);
|
return getIntegerForKeyJNI(pKey, defaultValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -245,14 +245,14 @@ float UserDefault::getFloatForKey(const char* pKey, float defaultValue)
|
||||||
if (node->FirstChild())
|
if (node->FirstChild())
|
||||||
{
|
{
|
||||||
float ret = utils::atof((const char*)node->FirstChild()->Value());
|
float ret = utils::atof((const char*)node->FirstChild()->Value());
|
||||||
|
|
||||||
// set value in NSUserDefaults
|
// set value in NSUserDefaults
|
||||||
setFloatForKey(pKey, ret);
|
setFloatForKey(pKey, ret);
|
||||||
flush();
|
flush();
|
||||||
|
|
||||||
// delete xmle node
|
// delete xmle node
|
||||||
deleteNode(doc, node);
|
deleteNode(doc, node);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -281,14 +281,14 @@ double UserDefault::getDoubleForKey(const char* pKey, double defaultValue)
|
||||||
if (node->FirstChild())
|
if (node->FirstChild())
|
||||||
{
|
{
|
||||||
double ret = utils::atof((const char*)node->FirstChild()->Value());
|
double ret = utils::atof((const char*)node->FirstChild()->Value());
|
||||||
|
|
||||||
// set value in NSUserDefaults
|
// set value in NSUserDefaults
|
||||||
setDoubleForKey(pKey, ret);
|
setDoubleForKey(pKey, ret);
|
||||||
flush();
|
flush();
|
||||||
|
|
||||||
// delete xmle node
|
// delete xmle node
|
||||||
deleteNode(doc, node);
|
deleteNode(doc, node);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -317,14 +317,14 @@ string UserDefault::getStringForKey(const char* pKey, const std::string & defaul
|
||||||
if (node->FirstChild())
|
if (node->FirstChild())
|
||||||
{
|
{
|
||||||
string ret = (const char*)node->FirstChild()->Value();
|
string ret = (const char*)node->FirstChild()->Value();
|
||||||
|
|
||||||
// set value in NSUserDefaults
|
// set value in NSUserDefaults
|
||||||
setStringForKey(pKey, ret);
|
setStringForKey(pKey, ret);
|
||||||
flush();
|
flush();
|
||||||
|
|
||||||
// delete xmle node
|
// delete xmle node
|
||||||
deleteNode(doc, node);
|
deleteNode(doc, node);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -353,22 +353,22 @@ Data UserDefault::getDataForKey(const char* pKey, const Data& defaultValue)
|
||||||
if (node->FirstChild())
|
if (node->FirstChild())
|
||||||
{
|
{
|
||||||
const char * encodedData = node->FirstChild()->Value();
|
const char * encodedData = node->FirstChild()->Value();
|
||||||
|
|
||||||
unsigned char * decodedData;
|
unsigned char * decodedData;
|
||||||
int decodedDataLen = base64Decode((unsigned char*)encodedData, (unsigned int)strlen(encodedData), &decodedData);
|
int decodedDataLen = base64Decode((unsigned char*)encodedData, (unsigned int)strlen(encodedData), &decodedData);
|
||||||
|
|
||||||
if (decodedData) {
|
if (decodedData) {
|
||||||
Data ret;
|
Data ret;
|
||||||
ret.fastSet(decodedData, decodedDataLen);
|
ret.fastSet(decodedData, decodedDataLen);
|
||||||
|
|
||||||
// set value in NSUserDefaults
|
// set value in NSUserDefaults
|
||||||
setDataForKey(pKey, ret);
|
setDataForKey(pKey, ret);
|
||||||
|
|
||||||
flush();
|
flush();
|
||||||
|
|
||||||
// delete xmle node
|
// delete xmle node
|
||||||
deleteNode(doc, node);
|
deleteNode(doc, node);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -379,22 +379,22 @@ Data UserDefault::getDataForKey(const char* pKey, const Data& defaultValue)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
char * encodedDefaultData = NULL;
|
char * encodedDefaultData = NULL;
|
||||||
unsigned int encodedDefaultDataLen = !defaultValue.isNull() ? base64Encode(defaultValue.getBytes(), defaultValue.getSize(), &encodedDefaultData) : 0;
|
unsigned int encodedDefaultDataLen = !defaultValue.isNull() ? base64Encode(defaultValue.getBytes(), defaultValue.getSize(), &encodedDefaultData) : 0;
|
||||||
|
|
||||||
string encodedStr = getStringForKeyJNI(pKey, encodedDefaultData);
|
string encodedStr = getStringForKeyJNI(pKey, encodedDefaultData);
|
||||||
|
|
||||||
if (encodedDefaultData)
|
if (encodedDefaultData)
|
||||||
free(encodedDefaultData);
|
free(encodedDefaultData);
|
||||||
|
|
||||||
CCLOG("ENCODED STRING: --%s--%d", encodedStr.c_str(), encodedStr.length());
|
CCLOG("ENCODED STRING: --%s--%d", encodedStr.c_str(), encodedStr.length());
|
||||||
|
|
||||||
unsigned char * decodedData = NULL;
|
unsigned char * decodedData = NULL;
|
||||||
int decodedDataLen = base64Decode((unsigned char*)encodedStr.c_str(), (unsigned int)encodedStr.length(), &decodedData);
|
int decodedDataLen = base64Decode((unsigned char*)encodedStr.c_str(), (unsigned int)encodedStr.length(), &decodedData);
|
||||||
|
|
||||||
CCLOG("DECODED DATA: %s %d", decodedData, decodedDataLen);
|
CCLOG("DECODED DATA: %s %d", decodedData, decodedDataLen);
|
||||||
|
|
||||||
if (decodedData && decodedDataLen) {
|
if (decodedData && decodedDataLen) {
|
||||||
Data ret;
|
Data ret;
|
||||||
ret.fastSet(decodedData, decodedDataLen);
|
ret.fastSet(decodedData, decodedDataLen);
|
||||||
|
@ -455,15 +455,15 @@ void UserDefault::setDataForKey(const char* pKey, const Data& value)
|
||||||
#ifdef KEEP_COMPATABILITY
|
#ifdef KEEP_COMPATABILITY
|
||||||
deleteNodeByKey(pKey);
|
deleteNodeByKey(pKey);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
CCLOG("SET DATA FOR KEY: --%s--%d", value.getBytes(), (int)(value.getSize()));
|
CCLOG("SET DATA FOR KEY: --%s--%d", value.getBytes(), (int)(value.getSize()));
|
||||||
char * encodedData = nullptr;
|
char * encodedData = nullptr;
|
||||||
unsigned int encodedDataLen = base64Encode(value.getBytes(), value.getSize(), &encodedData);
|
unsigned int encodedDataLen = base64Encode(value.getBytes(), value.getSize(), &encodedData);
|
||||||
|
|
||||||
CCLOG("SET DATA ENCODED: --%s", encodedData);
|
CCLOG("SET DATA ENCODED: --%s", encodedData);
|
||||||
|
|
||||||
setStringForKeyJNI(pKey, encodedData);
|
setStringForKeyJNI(pKey, encodedData);
|
||||||
|
|
||||||
if (encodedData)
|
if (encodedData)
|
||||||
free(encodedData);
|
free(encodedData);
|
||||||
}
|
}
|
||||||
|
@ -475,7 +475,7 @@ UserDefault* UserDefault::sharedUserDefault()
|
||||||
}
|
}
|
||||||
|
|
||||||
UserDefault* UserDefault::getInstance()
|
UserDefault* UserDefault::getInstance()
|
||||||
{
|
{
|
||||||
if (! _userDefault)
|
if (! _userDefault)
|
||||||
{
|
{
|
||||||
#ifdef KEEP_COMPATABILITY
|
#ifdef KEEP_COMPATABILITY
|
||||||
|
@ -519,6 +519,18 @@ void UserDefault::flush()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void UserDefault::deleteValueForKey(const char* key)
|
||||||
|
{
|
||||||
|
// check the params
|
||||||
|
if (!key)
|
||||||
|
{
|
||||||
|
CCLOG("the key is invalid");
|
||||||
|
}
|
||||||
|
|
||||||
|
deleteValueForKeyJNI(key);
|
||||||
|
|
||||||
|
flush();
|
||||||
|
}
|
||||||
NS_CC_END
|
NS_CC_END
|
||||||
|
|
||||||
#endif // (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
|
#endif // (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
|
||||||
|
|
|
@ -1,19 +1,19 @@
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
Copyright (c) 2010-2012 cocos2d-x.org
|
Copyright (c) 2010-2012 cocos2d-x.org
|
||||||
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
||||||
|
|
||||||
http://www.cocos2d-x.org
|
http://www.cocos2d-x.org
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
in the Software without restriction, including without limitation the rights
|
in the Software without restriction, including without limitation the rights
|
||||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
copies of the Software, and to permit persons to whom the Software is
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
furnished to do so, subject to the following conditions:
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be included in
|
The above copyright notice and this permission notice shall be included in
|
||||||
all copies or substantial portions of the Software.
|
all copies or substantial portions of the Software.
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
@ -61,18 +61,18 @@ static tinyxml2::XMLElement* getXMLNodeForKey(const char* pKey, tinyxml2::XMLDoc
|
||||||
{
|
{
|
||||||
tinyxml2::XMLElement* curNode = nullptr;
|
tinyxml2::XMLElement* curNode = nullptr;
|
||||||
tinyxml2::XMLElement* rootNode = nullptr;
|
tinyxml2::XMLElement* rootNode = nullptr;
|
||||||
|
|
||||||
if (! UserDefault::isXMLFileExist())
|
if (! UserDefault::isXMLFileExist())
|
||||||
{
|
{
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
// check the key value
|
// check the key value
|
||||||
if (! pKey)
|
if (! pKey)
|
||||||
{
|
{
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
tinyxml2::XMLDocument* xmlDoc = new tinyxml2::XMLDocument();
|
tinyxml2::XMLDocument* xmlDoc = new tinyxml2::XMLDocument();
|
||||||
|
@ -100,10 +100,10 @@ static tinyxml2::XMLElement* getXMLNodeForKey(const char* pKey, tinyxml2::XMLDoc
|
||||||
{
|
{
|
||||||
// There is not xml node, delete xml file.
|
// There is not xml node, delete xml file.
|
||||||
remove(UserDefault::getInstance()->getXMLFilePath().c_str());
|
remove(UserDefault::getInstance()->getXMLFilePath().c_str());
|
||||||
|
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (nullptr != curNode)
|
while (nullptr != curNode)
|
||||||
{
|
{
|
||||||
const char* nodeName = curNode->Value();
|
const char* nodeName = curNode->Value();
|
||||||
|
@ -112,11 +112,11 @@ static tinyxml2::XMLElement* getXMLNodeForKey(const char* pKey, tinyxml2::XMLDoc
|
||||||
// delete the node
|
// delete the node
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
curNode = curNode->NextSiblingElement();
|
curNode = curNode->NextSiblingElement();
|
||||||
}
|
}
|
||||||
} while (0);
|
} while (0);
|
||||||
|
|
||||||
return curNode;
|
return curNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -162,14 +162,14 @@ bool UserDefault::getBoolForKey(const char* pKey, bool defaultValue)
|
||||||
{
|
{
|
||||||
const char* value = (const char*)node->FirstChild()->Value();
|
const char* value = (const char*)node->FirstChild()->Value();
|
||||||
bool ret = (! strcmp(value, "true"));
|
bool ret = (! strcmp(value, "true"));
|
||||||
|
|
||||||
// set value in NSUserDefaults
|
// set value in NSUserDefaults
|
||||||
setBoolForKey(pKey, ret);
|
setBoolForKey(pKey, ret);
|
||||||
flush();
|
flush();
|
||||||
|
|
||||||
// delete xmle node
|
// delete xmle node
|
||||||
deleteNode(doc, node);
|
deleteNode(doc, node);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -179,15 +179,15 @@ bool UserDefault::getBoolForKey(const char* pKey, bool defaultValue)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
bool ret = defaultValue;
|
bool ret = defaultValue;
|
||||||
|
|
||||||
NSNumber *value = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithUTF8String:pKey]];
|
NSNumber *value = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithUTF8String:pKey]];
|
||||||
if (value)
|
if (value)
|
||||||
{
|
{
|
||||||
ret = [value boolValue];
|
ret = [value boolValue];
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -206,14 +206,14 @@ int UserDefault::getIntegerForKey(const char* pKey, int defaultValue)
|
||||||
if (node->FirstChild())
|
if (node->FirstChild())
|
||||||
{
|
{
|
||||||
int ret = atoi((const char*)node->FirstChild()->Value());
|
int ret = atoi((const char*)node->FirstChild()->Value());
|
||||||
|
|
||||||
// set value in NSUserDefaults
|
// set value in NSUserDefaults
|
||||||
setIntegerForKey(pKey, ret);
|
setIntegerForKey(pKey, ret);
|
||||||
flush();
|
flush();
|
||||||
|
|
||||||
// delete xmle node
|
// delete xmle node
|
||||||
deleteNode(doc, node);
|
deleteNode(doc, node);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -223,15 +223,15 @@ int UserDefault::getIntegerForKey(const char* pKey, int defaultValue)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
int ret = defaultValue;
|
int ret = defaultValue;
|
||||||
|
|
||||||
NSNumber *value = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithUTF8String:pKey]];
|
NSNumber *value = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithUTF8String:pKey]];
|
||||||
if (value)
|
if (value)
|
||||||
{
|
{
|
||||||
ret = [value intValue];
|
ret = [value intValue];
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -250,14 +250,14 @@ float UserDefault::getFloatForKey(const char* pKey, float defaultValue)
|
||||||
if (node->FirstChild())
|
if (node->FirstChild())
|
||||||
{
|
{
|
||||||
float ret = atof((const char*)node->FirstChild()->Value());
|
float ret = atof((const char*)node->FirstChild()->Value());
|
||||||
|
|
||||||
// set value in NSUserDefaults
|
// set value in NSUserDefaults
|
||||||
setFloatForKey(pKey, ret);
|
setFloatForKey(pKey, ret);
|
||||||
flush();
|
flush();
|
||||||
|
|
||||||
// delete xmle node
|
// delete xmle node
|
||||||
deleteNode(doc, node);
|
deleteNode(doc, node);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -267,15 +267,15 @@ float UserDefault::getFloatForKey(const char* pKey, float defaultValue)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
float ret = defaultValue;
|
float ret = defaultValue;
|
||||||
|
|
||||||
NSNumber *value = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithUTF8String:pKey]];
|
NSNumber *value = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithUTF8String:pKey]];
|
||||||
if (value)
|
if (value)
|
||||||
{
|
{
|
||||||
ret = [value floatValue];
|
ret = [value floatValue];
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -294,14 +294,14 @@ double UserDefault::getDoubleForKey(const char* pKey, double defaultValue)
|
||||||
if (node->FirstChild())
|
if (node->FirstChild())
|
||||||
{
|
{
|
||||||
double ret = atof((const char*)node->FirstChild()->Value());
|
double ret = atof((const char*)node->FirstChild()->Value());
|
||||||
|
|
||||||
// set value in NSUserDefaults
|
// set value in NSUserDefaults
|
||||||
setDoubleForKey(pKey, ret);
|
setDoubleForKey(pKey, ret);
|
||||||
flush();
|
flush();
|
||||||
|
|
||||||
// delete xmle node
|
// delete xmle node
|
||||||
deleteNode(doc, node);
|
deleteNode(doc, node);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -311,15 +311,15 @@ double UserDefault::getDoubleForKey(const char* pKey, double defaultValue)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
double ret = defaultValue;
|
double ret = defaultValue;
|
||||||
|
|
||||||
NSNumber *value = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithUTF8String:pKey]];
|
NSNumber *value = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithUTF8String:pKey]];
|
||||||
if (value)
|
if (value)
|
||||||
{
|
{
|
||||||
ret = [value doubleValue];
|
ret = [value doubleValue];
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -338,14 +338,14 @@ string UserDefault::getStringForKey(const char* pKey, const std::string & defaul
|
||||||
if (node->FirstChild())
|
if (node->FirstChild())
|
||||||
{
|
{
|
||||||
string ret = (const char*)node->FirstChild()->Value();
|
string ret = (const char*)node->FirstChild()->Value();
|
||||||
|
|
||||||
// set value in NSUserDefaults
|
// set value in NSUserDefaults
|
||||||
setStringForKey(pKey, ret);
|
setStringForKey(pKey, ret);
|
||||||
flush();
|
flush();
|
||||||
|
|
||||||
// delete xmle node
|
// delete xmle node
|
||||||
deleteNode(doc, node);
|
deleteNode(doc, node);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -355,7 +355,7 @@ string UserDefault::getStringForKey(const char* pKey, const std::string & defaul
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
NSString *str = [[NSUserDefaults standardUserDefaults] stringForKey:[NSString stringWithUTF8String:pKey]];
|
NSString *str = [[NSUserDefaults standardUserDefaults] stringForKey:[NSString stringWithUTF8String:pKey]];
|
||||||
if (! str)
|
if (! str)
|
||||||
{
|
{
|
||||||
|
@ -388,15 +388,15 @@ Data UserDefault::getDataForKey(const char* pKey, const Data& defaultValue)
|
||||||
if (decodedData) {
|
if (decodedData) {
|
||||||
Data ret;
|
Data ret;
|
||||||
ret.fastSet(decodedData, decodedDataLen);
|
ret.fastSet(decodedData, decodedDataLen);
|
||||||
|
|
||||||
// set value in NSUserDefaults
|
// set value in NSUserDefaults
|
||||||
setDataForKey(pKey, ret);
|
setDataForKey(pKey, ret);
|
||||||
|
|
||||||
flush();
|
flush();
|
||||||
|
|
||||||
// delete xmle node
|
// delete xmle node
|
||||||
deleteNode(doc, node);
|
deleteNode(doc, node);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -407,7 +407,7 @@ Data UserDefault::getDataForKey(const char* pKey, const Data& defaultValue)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
NSData *data = [[NSUserDefaults standardUserDefaults] dataForKey:[NSString stringWithUTF8String:pKey]];
|
NSData *data = [[NSUserDefaults standardUserDefaults] dataForKey:[NSString stringWithUTF8String:pKey]];
|
||||||
if (! data)
|
if (! data)
|
||||||
{
|
{
|
||||||
|
@ -426,7 +426,7 @@ void UserDefault::setBoolForKey(const char* pKey, bool value)
|
||||||
#ifdef KEEP_COMPATABILITY
|
#ifdef KEEP_COMPATABILITY
|
||||||
deleteNodeByKey(pKey);
|
deleteNodeByKey(pKey);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:value] forKey:[NSString stringWithUTF8String:pKey]];
|
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:value] forKey:[NSString stringWithUTF8String:pKey]];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -435,7 +435,7 @@ void UserDefault::setIntegerForKey(const char* pKey, int value)
|
||||||
#ifdef KEEP_COMPATABILITY
|
#ifdef KEEP_COMPATABILITY
|
||||||
deleteNodeByKey(pKey);
|
deleteNodeByKey(pKey);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:value] forKey:[NSString stringWithUTF8String:pKey]];
|
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:value] forKey:[NSString stringWithUTF8String:pKey]];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -444,7 +444,7 @@ void UserDefault::setFloatForKey(const char* pKey, float value)
|
||||||
#ifdef KEEP_COMPATABILITY
|
#ifdef KEEP_COMPATABILITY
|
||||||
deleteNodeByKey(pKey);
|
deleteNodeByKey(pKey);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithFloat:value] forKey:[NSString stringWithUTF8String:pKey]];
|
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithFloat:value] forKey:[NSString stringWithUTF8String:pKey]];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -453,7 +453,7 @@ void UserDefault::setDoubleForKey(const char* pKey, double value)
|
||||||
#ifdef KEEP_COMPATABILITY
|
#ifdef KEEP_COMPATABILITY
|
||||||
deleteNodeByKey(pKey);
|
deleteNodeByKey(pKey);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithDouble:value] forKey:[NSString stringWithUTF8String:pKey]];
|
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithDouble:value] forKey:[NSString stringWithUTF8String:pKey]];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -462,7 +462,7 @@ void UserDefault::setStringForKey(const char* pKey, const std::string & value)
|
||||||
#ifdef KEEP_COMPATABILITY
|
#ifdef KEEP_COMPATABILITY
|
||||||
deleteNodeByKey(pKey);
|
deleteNodeByKey(pKey);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
[[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithUTF8String:value.c_str()] forKey:[NSString stringWithUTF8String:pKey]];
|
[[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithUTF8String:value.c_str()] forKey:[NSString stringWithUTF8String:pKey]];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -470,12 +470,12 @@ void UserDefault::setDataForKey(const char* pKey, const Data& value) {
|
||||||
#ifdef KEEP_COMPATABILITY
|
#ifdef KEEP_COMPATABILITY
|
||||||
deleteNodeByKey(pKey);
|
deleteNodeByKey(pKey);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
[[NSUserDefaults standardUserDefaults] setObject:[NSData dataWithBytes: value.getBytes() length: value.getSize()] forKey:[NSString stringWithUTF8String:pKey]];
|
[[NSUserDefaults standardUserDefaults] setObject:[NSData dataWithBytes: value.getBytes() length: value.getSize()] forKey:[NSString stringWithUTF8String:pKey]];
|
||||||
}
|
}
|
||||||
|
|
||||||
UserDefault* UserDefault::getInstance()
|
UserDefault* UserDefault::getInstance()
|
||||||
{
|
{
|
||||||
if (! _userDefault)
|
if (! _userDefault)
|
||||||
{
|
{
|
||||||
#ifdef KEEP_COMPATABILITY
|
#ifdef KEEP_COMPATABILITY
|
||||||
|
@ -483,7 +483,7 @@ UserDefault* UserDefault::getInstance()
|
||||||
#endif
|
#endif
|
||||||
_userDefault = new (std::nothrow) UserDefault();
|
_userDefault = new (std::nothrow) UserDefault();
|
||||||
}
|
}
|
||||||
|
|
||||||
return _userDefault;
|
return _userDefault;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -519,7 +519,7 @@ void UserDefault::initXMLFilePath()
|
||||||
NSString *documentsDirectory = [paths objectAtIndex:0];
|
NSString *documentsDirectory = [paths objectAtIndex:0];
|
||||||
_filePath = [documentsDirectory UTF8String];
|
_filePath = [documentsDirectory UTF8String];
|
||||||
_filePath.append("/");
|
_filePath.append("/");
|
||||||
|
|
||||||
_filePath += XML_FILE_NAME;
|
_filePath += XML_FILE_NAME;
|
||||||
_isFilePathInitialized = true;
|
_isFilePathInitialized = true;
|
||||||
}
|
}
|
||||||
|
@ -542,6 +542,18 @@ void UserDefault::flush()
|
||||||
[[NSUserDefaults standardUserDefaults] synchronize];
|
[[NSUserDefaults standardUserDefaults] synchronize];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void UserDefault::deleteValueForKey(const char* key)
|
||||||
|
{
|
||||||
|
// check the params
|
||||||
|
if (!key)
|
||||||
|
{
|
||||||
|
CCLOG("the key is invalid");
|
||||||
|
}
|
||||||
|
|
||||||
|
[[NSUserDefaults standardUserDefaults] removeObjectForKey:[NSString stringWithUTF8String:key]];
|
||||||
|
|
||||||
|
flush();
|
||||||
|
}
|
||||||
|
|
||||||
NS_CC_END
|
NS_CC_END
|
||||||
|
|
||||||
|
|
|
@ -128,7 +128,7 @@ float UserDefault::getFloatForKey(const char* pKey)
|
||||||
float UserDefault::getFloatForKey(const char* pKey, float defaultValue)
|
float UserDefault::getFloatForKey(const char* pKey, float defaultValue)
|
||||||
{
|
{
|
||||||
float ret = (float)getDoubleForKey(pKey, (double)defaultValue);
|
float ret = (float)getDoubleForKey(pKey, (double)defaultValue);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -180,7 +180,7 @@ Data UserDefault::getDataForKey(const char* pKey, const Data& defaultValue)
|
||||||
{
|
{
|
||||||
unsigned char* decodedData = nullptr;
|
unsigned char* decodedData = nullptr;
|
||||||
int decodedDataLen = base64Decode((unsigned char*) encodedData.c_str(), (unsigned int) encodedData.length(), &decodedData);
|
int decodedDataLen = base64Decode((unsigned char*) encodedData.c_str(), (unsigned int) encodedData.length(), &decodedData);
|
||||||
if (decodedData && decodedDataLen > 0)
|
if (decodedData && decodedDataLen > 0)
|
||||||
{
|
{
|
||||||
ret.fastSet(decodedData, decodedDataLen);
|
ret.fastSet(decodedData, decodedDataLen);
|
||||||
}
|
}
|
||||||
|
@ -247,7 +247,7 @@ void UserDefault::setDataForKey(const char* pKey, const Data& value) {
|
||||||
|
|
||||||
char *encodedData = 0;
|
char *encodedData = 0;
|
||||||
base64Encode(value.getBytes(), static_cast<unsigned int>(value.getSize()), &encodedData);
|
base64Encode(value.getBytes(), static_cast<unsigned int>(value.getSize()), &encodedData);
|
||||||
|
|
||||||
setPlatformKeyValue(pKey, dynamic_cast<PropertyValue^>(PropertyValue::CreateString(PlatformStringFromString(encodedData))));
|
setPlatformKeyValue(pKey, dynamic_cast<PropertyValue^>(PropertyValue::CreateString(PlatformStringFromString(encodedData))));
|
||||||
|
|
||||||
if (encodedData)
|
if (encodedData)
|
||||||
|
@ -311,7 +311,7 @@ void UserDefault::initXMLFilePath()
|
||||||
{
|
{
|
||||||
_filePath += FileUtils::getInstance()->getWritablePath() + XML_FILE_NAME;
|
_filePath += FileUtils::getInstance()->getWritablePath() + XML_FILE_NAME;
|
||||||
_isFilePathInitialized = true;
|
_isFilePathInitialized = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// create new xml file
|
// create new xml file
|
||||||
|
@ -329,6 +329,21 @@ void UserDefault::flush()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void UserDefault::deleteValueForKey(const char* key)
|
||||||
|
{
|
||||||
|
// check the params
|
||||||
|
if (!key)
|
||||||
|
{
|
||||||
|
CCLOG("the key is invalid");
|
||||||
|
}
|
||||||
|
|
||||||
|
ApplicationDataContainer^ localSettings = ApplicationData::Current->LocalSettings;
|
||||||
|
auto values = localSettings->Values;
|
||||||
|
values->Remove(PlatformStringFromString(key));
|
||||||
|
|
||||||
|
flush();
|
||||||
|
}
|
||||||
|
|
||||||
NS_CC_END
|
NS_CC_END
|
||||||
|
|
||||||
#endif // (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
|
#endif // (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
|
||||||
|
|
|
@ -57,57 +57,57 @@ static tinyxml2::XMLElement* getXMLNodeForKey(const char* pKey, tinyxml2::XMLEle
|
||||||
|
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
tinyxml2::XMLDocument* xmlDoc = new tinyxml2::XMLDocument();
|
tinyxml2::XMLDocument* xmlDoc = new tinyxml2::XMLDocument();
|
||||||
*doc = xmlDoc;
|
*doc = xmlDoc;
|
||||||
|
|
||||||
std::string xmlBuffer = FileUtils::getInstance()->getStringFromFile(UserDefault::getInstance()->getXMLFilePath());
|
std::string xmlBuffer = FileUtils::getInstance()->getStringFromFile(UserDefault::getInstance()->getXMLFilePath());
|
||||||
|
|
||||||
if (xmlBuffer.empty())
|
if (xmlBuffer.empty())
|
||||||
{
|
{
|
||||||
CCLOG("can not read xml file");
|
CCLOG("can not read xml file");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
xmlDoc->Parse(xmlBuffer.c_str(), xmlBuffer.size());
|
xmlDoc->Parse(xmlBuffer.c_str(), xmlBuffer.size());
|
||||||
|
|
||||||
// get root node
|
// get root node
|
||||||
*rootNode = xmlDoc->RootElement();
|
*rootNode = xmlDoc->RootElement();
|
||||||
if (nullptr == *rootNode)
|
if (nullptr == *rootNode)
|
||||||
{
|
{
|
||||||
CCLOG("read root node error");
|
CCLOG("read root node error");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// find the node
|
// find the node
|
||||||
curNode = (*rootNode)->FirstChildElement();
|
curNode = (*rootNode)->FirstChildElement();
|
||||||
while (nullptr != curNode)
|
while (nullptr != curNode)
|
||||||
{
|
{
|
||||||
const char* nodeName = curNode->Value();
|
const char* nodeName = curNode->Value();
|
||||||
if (!strcmp(nodeName, pKey))
|
if (!strcmp(nodeName, pKey))
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
curNode = curNode->NextSiblingElement();
|
curNode = curNode->NextSiblingElement();
|
||||||
}
|
}
|
||||||
} while (0);
|
} while (0);
|
||||||
|
|
||||||
return curNode;
|
return curNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void setValueForKey(const char* pKey, const char* pValue)
|
static void setValueForKey(const char* pKey, const char* pValue)
|
||||||
{
|
{
|
||||||
tinyxml2::XMLElement* rootNode;
|
tinyxml2::XMLElement* rootNode;
|
||||||
tinyxml2::XMLDocument* doc;
|
tinyxml2::XMLDocument* doc;
|
||||||
tinyxml2::XMLElement* node;
|
tinyxml2::XMLElement* node;
|
||||||
// check the params
|
// check the params
|
||||||
if (! pKey || ! pValue)
|
if (! pKey || ! pValue)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// find the node
|
// find the node
|
||||||
node = getXMLNodeForKey(pKey, &rootNode, &doc);
|
node = getXMLNodeForKey(pKey, &rootNode, &doc);
|
||||||
// if node exist, change the content
|
// if node exist, change the content
|
||||||
if (node)
|
if (node)
|
||||||
{
|
{
|
||||||
if (node->FirstChild())
|
if (node->FirstChild())
|
||||||
{
|
{
|
||||||
node->FirstChild()->SetValue(pValue);
|
node->FirstChild()->SetValue(pValue);
|
||||||
|
@ -117,24 +117,24 @@ static void setValueForKey(const char* pKey, const char* pValue)
|
||||||
tinyxml2::XMLText* content = doc->NewText(pValue);
|
tinyxml2::XMLText* content = doc->NewText(pValue);
|
||||||
node->LinkEndChild(content);
|
node->LinkEndChild(content);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (rootNode)
|
if (rootNode)
|
||||||
{
|
{
|
||||||
tinyxml2::XMLElement* tmpNode = doc->NewElement(pKey);//new tinyxml2::XMLElement(pKey);
|
tinyxml2::XMLElement* tmpNode = doc->NewElement(pKey);//new tinyxml2::XMLElement(pKey);
|
||||||
rootNode->LinkEndChild(tmpNode);
|
rootNode->LinkEndChild(tmpNode);
|
||||||
tinyxml2::XMLText* content = doc->NewText(pValue);//new tinyxml2::XMLText(pValue);
|
tinyxml2::XMLText* content = doc->NewText(pValue);//new tinyxml2::XMLText(pValue);
|
||||||
tmpNode->LinkEndChild(content);
|
tmpNode->LinkEndChild(content);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// save file and free doc
|
// save file and free doc
|
||||||
if (doc)
|
if (doc)
|
||||||
{
|
{
|
||||||
doc->SaveFile(FileUtils::getInstance()->getSuitableFOpen(UserDefault::getInstance()->getXMLFilePath()).c_str());
|
doc->SaveFile(FileUtils::getInstance()->getSuitableFOpen(UserDefault::getInstance()->getXMLFilePath()).c_str());
|
||||||
delete doc;
|
delete doc;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -161,26 +161,26 @@ bool UserDefault::getBoolForKey(const char* pKey)
|
||||||
bool UserDefault::getBoolForKey(const char* pKey, bool defaultValue)
|
bool UserDefault::getBoolForKey(const char* pKey, bool defaultValue)
|
||||||
{
|
{
|
||||||
const char* value = nullptr;
|
const char* value = nullptr;
|
||||||
tinyxml2::XMLElement* rootNode;
|
tinyxml2::XMLElement* rootNode;
|
||||||
tinyxml2::XMLDocument* doc;
|
tinyxml2::XMLDocument* doc;
|
||||||
tinyxml2::XMLElement* node;
|
tinyxml2::XMLElement* node;
|
||||||
node = getXMLNodeForKey(pKey, &rootNode, &doc);
|
node = getXMLNodeForKey(pKey, &rootNode, &doc);
|
||||||
// find the node
|
// find the node
|
||||||
if (node && node->FirstChild())
|
if (node && node->FirstChild())
|
||||||
{
|
{
|
||||||
value = (const char*)(node->FirstChild()->Value());
|
value = (const char*)(node->FirstChild()->Value());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ret = defaultValue;
|
bool ret = defaultValue;
|
||||||
|
|
||||||
if (value)
|
if (value)
|
||||||
{
|
{
|
||||||
ret = (! strcmp(value, "true"));
|
ret = (! strcmp(value, "true"));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (doc) delete doc;
|
if (doc) delete doc;
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int UserDefault::getIntegerForKey(const char* pKey)
|
int UserDefault::getIntegerForKey(const char* pKey)
|
||||||
|
@ -190,31 +190,31 @@ int UserDefault::getIntegerForKey(const char* pKey)
|
||||||
|
|
||||||
int UserDefault::getIntegerForKey(const char* pKey, int defaultValue)
|
int UserDefault::getIntegerForKey(const char* pKey, int defaultValue)
|
||||||
{
|
{
|
||||||
const char* value = nullptr;
|
const char* value = nullptr;
|
||||||
tinyxml2::XMLElement* rootNode;
|
tinyxml2::XMLElement* rootNode;
|
||||||
tinyxml2::XMLDocument* doc;
|
tinyxml2::XMLDocument* doc;
|
||||||
tinyxml2::XMLElement* node;
|
tinyxml2::XMLElement* node;
|
||||||
node = getXMLNodeForKey(pKey, &rootNode, &doc);
|
node = getXMLNodeForKey(pKey, &rootNode, &doc);
|
||||||
// find the node
|
// find the node
|
||||||
if (node && node->FirstChild())
|
if (node && node->FirstChild())
|
||||||
{
|
{
|
||||||
value = (const char*)(node->FirstChild()->Value());
|
value = (const char*)(node->FirstChild()->Value());
|
||||||
}
|
}
|
||||||
|
|
||||||
int ret = defaultValue;
|
int ret = defaultValue;
|
||||||
|
|
||||||
if (value)
|
if (value)
|
||||||
{
|
{
|
||||||
ret = atoi(value);
|
ret = atoi(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(doc)
|
if(doc)
|
||||||
{
|
{
|
||||||
delete doc;
|
delete doc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
float UserDefault::getFloatForKey(const char* pKey)
|
float UserDefault::getFloatForKey(const char* pKey)
|
||||||
|
@ -236,27 +236,27 @@ double UserDefault::getDoubleForKey(const char* pKey)
|
||||||
|
|
||||||
double UserDefault::getDoubleForKey(const char* pKey, double defaultValue)
|
double UserDefault::getDoubleForKey(const char* pKey, double defaultValue)
|
||||||
{
|
{
|
||||||
const char* value = nullptr;
|
const char* value = nullptr;
|
||||||
tinyxml2::XMLElement* rootNode;
|
tinyxml2::XMLElement* rootNode;
|
||||||
tinyxml2::XMLDocument* doc;
|
tinyxml2::XMLDocument* doc;
|
||||||
tinyxml2::XMLElement* node;
|
tinyxml2::XMLElement* node;
|
||||||
node = getXMLNodeForKey(pKey, &rootNode, &doc);
|
node = getXMLNodeForKey(pKey, &rootNode, &doc);
|
||||||
// find the node
|
// find the node
|
||||||
if (node && node->FirstChild())
|
if (node && node->FirstChild())
|
||||||
{
|
{
|
||||||
value = (const char*)(node->FirstChild()->Value());
|
value = (const char*)(node->FirstChild()->Value());
|
||||||
}
|
}
|
||||||
|
|
||||||
double ret = defaultValue;
|
double ret = defaultValue;
|
||||||
|
|
||||||
if (value)
|
if (value)
|
||||||
{
|
{
|
||||||
ret = utils::atof(value);
|
ret = utils::atof(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (doc) delete doc;
|
if (doc) delete doc;
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string UserDefault::getStringForKey(const char* pKey)
|
std::string UserDefault::getStringForKey(const char* pKey)
|
||||||
|
@ -267,26 +267,26 @@ std::string UserDefault::getStringForKey(const char* pKey)
|
||||||
string UserDefault::getStringForKey(const char* pKey, const std::string & defaultValue)
|
string UserDefault::getStringForKey(const char* pKey, const std::string & defaultValue)
|
||||||
{
|
{
|
||||||
const char* value = nullptr;
|
const char* value = nullptr;
|
||||||
tinyxml2::XMLElement* rootNode;
|
tinyxml2::XMLElement* rootNode;
|
||||||
tinyxml2::XMLDocument* doc;
|
tinyxml2::XMLDocument* doc;
|
||||||
tinyxml2::XMLElement* node;
|
tinyxml2::XMLElement* node;
|
||||||
node = getXMLNodeForKey(pKey, &rootNode, &doc);
|
node = getXMLNodeForKey(pKey, &rootNode, &doc);
|
||||||
// find the node
|
// find the node
|
||||||
if (node && node->FirstChild())
|
if (node && node->FirstChild())
|
||||||
{
|
{
|
||||||
value = (const char*)(node->FirstChild()->Value());
|
value = (const char*)(node->FirstChild()->Value());
|
||||||
}
|
}
|
||||||
|
|
||||||
string ret = defaultValue;
|
string ret = defaultValue;
|
||||||
|
|
||||||
if (value)
|
if (value)
|
||||||
{
|
{
|
||||||
ret = string(value);
|
ret = string(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (doc) delete doc;
|
if (doc) delete doc;
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
Data UserDefault::getDataForKey(const char* pKey)
|
Data UserDefault::getDataForKey(const char* pKey)
|
||||||
|
@ -297,31 +297,31 @@ Data UserDefault::getDataForKey(const char* pKey)
|
||||||
Data UserDefault::getDataForKey(const char* pKey, const Data& defaultValue)
|
Data UserDefault::getDataForKey(const char* pKey, const Data& defaultValue)
|
||||||
{
|
{
|
||||||
const char* encodedData = nullptr;
|
const char* encodedData = nullptr;
|
||||||
tinyxml2::XMLElement* rootNode;
|
tinyxml2::XMLElement* rootNode;
|
||||||
tinyxml2::XMLDocument* doc;
|
tinyxml2::XMLDocument* doc;
|
||||||
tinyxml2::XMLElement* node;
|
tinyxml2::XMLElement* node;
|
||||||
node = getXMLNodeForKey(pKey, &rootNode, &doc);
|
node = getXMLNodeForKey(pKey, &rootNode, &doc);
|
||||||
// find the node
|
// find the node
|
||||||
if (node && node->FirstChild())
|
if (node && node->FirstChild())
|
||||||
{
|
{
|
||||||
encodedData = (const char*)(node->FirstChild()->Value());
|
encodedData = (const char*)(node->FirstChild()->Value());
|
||||||
}
|
}
|
||||||
|
|
||||||
Data ret = defaultValue;
|
Data ret = defaultValue;
|
||||||
|
|
||||||
if (encodedData)
|
if (encodedData)
|
||||||
{
|
{
|
||||||
unsigned char * decodedData = nullptr;
|
unsigned char * decodedData = nullptr;
|
||||||
int decodedDataLen = base64Decode((unsigned char*)encodedData, (unsigned int)strlen(encodedData), &decodedData);
|
int decodedDataLen = base64Decode((unsigned char*)encodedData, (unsigned int)strlen(encodedData), &decodedData);
|
||||||
|
|
||||||
if (decodedData) {
|
if (decodedData) {
|
||||||
ret.fastSet(decodedData, decodedDataLen);
|
ret.fastSet(decodedData, decodedDataLen);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (doc) delete doc;
|
if (doc) delete doc;
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -466,32 +466,32 @@ void UserDefault::initXMLFilePath()
|
||||||
// create new xml file
|
// create new xml file
|
||||||
bool UserDefault::createXMLFile()
|
bool UserDefault::createXMLFile()
|
||||||
{
|
{
|
||||||
bool bRet = false;
|
bool bRet = false;
|
||||||
tinyxml2::XMLDocument *pDoc = new tinyxml2::XMLDocument();
|
tinyxml2::XMLDocument *pDoc = new tinyxml2::XMLDocument();
|
||||||
if (nullptr==pDoc)
|
if (nullptr==pDoc)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
tinyxml2::XMLDeclaration *pDeclaration = pDoc->NewDeclaration(nullptr);
|
tinyxml2::XMLDeclaration *pDeclaration = pDoc->NewDeclaration(nullptr);
|
||||||
if (nullptr==pDeclaration)
|
if (nullptr==pDeclaration)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
pDoc->LinkEndChild(pDeclaration);
|
pDoc->LinkEndChild(pDeclaration);
|
||||||
tinyxml2::XMLElement *pRootEle = pDoc->NewElement(USERDEFAULT_ROOT_NAME);
|
tinyxml2::XMLElement *pRootEle = pDoc->NewElement(USERDEFAULT_ROOT_NAME);
|
||||||
if (nullptr==pRootEle)
|
if (nullptr==pRootEle)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
pDoc->LinkEndChild(pRootEle);
|
pDoc->LinkEndChild(pRootEle);
|
||||||
bRet = tinyxml2::XML_SUCCESS == pDoc->SaveFile(FileUtils::getInstance()->getSuitableFOpen(_filePath).c_str());
|
bRet = tinyxml2::XML_SUCCESS == pDoc->SaveFile(FileUtils::getInstance()->getSuitableFOpen(_filePath).c_str());
|
||||||
|
|
||||||
if(pDoc)
|
if(pDoc)
|
||||||
{
|
{
|
||||||
delete pDoc;
|
delete pDoc;
|
||||||
}
|
}
|
||||||
|
|
||||||
return bRet;
|
return bRet;
|
||||||
}
|
}
|
||||||
|
|
||||||
const string& UserDefault::getXMLFilePath()
|
const string& UserDefault::getXMLFilePath()
|
||||||
|
@ -503,6 +503,39 @@ void UserDefault::flush()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void UserDefault::deleteValueForKey(const char* key)
|
||||||
|
{
|
||||||
|
tinyxml2::XMLElement* rootNode;
|
||||||
|
tinyxml2::XMLDocument* doc;
|
||||||
|
tinyxml2::XMLElement* node;
|
||||||
|
|
||||||
|
// check the params
|
||||||
|
if (!key)
|
||||||
|
{
|
||||||
|
CCLOG("the key is invalid");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// find the node
|
||||||
|
node = getXMLNodeForKey(key, &rootNode, &doc);
|
||||||
|
|
||||||
|
// if node not exist, don't need to delete
|
||||||
|
if (!node)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// save file and free doc
|
||||||
|
if (doc)
|
||||||
|
{
|
||||||
|
doc->DeleteNode(node);
|
||||||
|
doc->SaveFile(FileUtils::getInstance()->getSuitableFOpen(UserDefault::getInstance()->getXMLFilePath()).c_str());
|
||||||
|
delete doc;
|
||||||
|
}
|
||||||
|
|
||||||
|
flush();
|
||||||
|
}
|
||||||
|
|
||||||
NS_CC_END
|
NS_CC_END
|
||||||
|
|
||||||
#endif // (CC_TARGET_PLATFORM != CC_PLATFORM_IOS && CC_PLATFORM != CC_PLATFORM_ANDROID)
|
#endif // (CC_TARGET_PLATFORM != CC_PLATFORM_IOS && CC_PLATFORM != CC_PLATFORM_ANDROID)
|
||||||
|
|
|
@ -202,6 +202,13 @@ public:
|
||||||
*/
|
*/
|
||||||
virtual void flush();
|
virtual void flush();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* delete any value by key,
|
||||||
|
* @param key The key to delete value.
|
||||||
|
* @js NA
|
||||||
|
*/
|
||||||
|
virtual void deleteValueForKey(const char* key);
|
||||||
|
|
||||||
/** Returns the singleton.
|
/** Returns the singleton.
|
||||||
* @js NA
|
* @js NA
|
||||||
* @lua NA
|
* @lua NA
|
||||||
|
@ -238,7 +245,7 @@ public:
|
||||||
*/
|
*/
|
||||||
static const std::string& getXMLFilePath();
|
static const std::string& getXMLFilePath();
|
||||||
/** All supported platforms other iOS & Android and CC_PLATFORM_WINRT use xml file to save values. This function checks whether the xml file exists or not.
|
/** All supported platforms other iOS & Android and CC_PLATFORM_WINRT use xml file to save values. This function checks whether the xml file exists or not.
|
||||||
* @return True if the xml file exists, flase if not.
|
* @return True if the xml file exists, false if not.
|
||||||
* @js NA
|
* @js NA
|
||||||
*/
|
*/
|
||||||
static bool isXMLFileExist();
|
static bool isXMLFileExist();
|
||||||
|
|
|
@ -526,6 +526,13 @@ public class Cocos2dxHelper {
|
||||||
editor.commit();
|
editor.commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void deleteValueForKey(String key) {
|
||||||
|
SharedPreferences settings = sActivity.getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0);
|
||||||
|
SharedPreferences.Editor editor = settings.edit();
|
||||||
|
editor.remove(key);
|
||||||
|
editor.commit();
|
||||||
|
}
|
||||||
|
|
||||||
// ===========================================================
|
// ===========================================================
|
||||||
// Inner and Anonymous Classes
|
// Inner and Anonymous Classes
|
||||||
// ===========================================================
|
// ===========================================================
|
||||||
|
|
|
@ -400,3 +400,16 @@ void setStringForKeyJNI(const char* key, const char* value)
|
||||||
t.env->DeleteLocalRef(stringArg2);
|
t.env->DeleteLocalRef(stringArg2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void deleteValueForKeyJNI(const char* key)
|
||||||
|
{
|
||||||
|
JniMethodInfo t;
|
||||||
|
|
||||||
|
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "deleteValueForKey", "(Ljava/lang/String;)V")) {
|
||||||
|
jstring stringArg1 = t.env->NewStringUTF(key);
|
||||||
|
t.env->CallStaticVoidMethod(t.classID, t.methodID, stringArg1);
|
||||||
|
|
||||||
|
t.env->DeleteLocalRef(t.classID);
|
||||||
|
t.env->DeleteLocalRef(stringArg1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -53,5 +53,5 @@ extern void setIntegerForKeyJNI(const char* key, int value);
|
||||||
extern void setFloatForKeyJNI(const char* key, float value);
|
extern void setFloatForKeyJNI(const char* key, float value);
|
||||||
extern void setDoubleForKeyJNI(const char* key, double value);
|
extern void setDoubleForKeyJNI(const char* key, double value);
|
||||||
extern void setStringForKeyJNI(const char* key, const char* value);
|
extern void setStringForKeyJNI(const char* key, const char* value);
|
||||||
|
extern void deleteValueForKeyJNI(const char* key);
|
||||||
#endif /* __Java_org_cocos2dx_lib_Cocos2dxHelper_H__ */
|
#endif /* __Java_org_cocos2dx_lib_Cocos2dxHelper_H__ */
|
||||||
|
|
|
@ -21,10 +21,16 @@ UserDefaultTests::UserDefaultTests()
|
||||||
UserDefaultTest::UserDefaultTest()
|
UserDefaultTest::UserDefaultTest()
|
||||||
{
|
{
|
||||||
auto s = Director::getInstance()->getWinSize();
|
auto s = Director::getInstance()->getWinSize();
|
||||||
auto label = Label::createWithTTF("CCUserDefault test see log", "fonts/arial.ttf", 28);
|
auto label = Label::createWithTTF("CCUserDefault test Log data see console", "fonts/arial.ttf", 22);
|
||||||
addChild(label, 0);
|
addChild(label, 0);
|
||||||
label->setPosition( Vec2(s.width/2, s.height-50) );
|
label->setPosition( Vec2(s.width/2, s.height-50) );
|
||||||
|
|
||||||
|
this->_label = Label::createWithTTF("result", "fonts/arial.ttf", 12);
|
||||||
|
addChild(this->_label, 0);
|
||||||
|
|
||||||
|
label->setPosition(Vec2(s.width / 2, s.height - 50));
|
||||||
|
this->_label->setPosition(Vec2(s.width / 2, s.height / 2));
|
||||||
|
|
||||||
doTest();
|
doTest();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,7 +81,7 @@ void setData2(const char* key)
|
||||||
|
|
||||||
void UserDefaultTest::doTest()
|
void UserDefaultTest::doTest()
|
||||||
{
|
{
|
||||||
CCLOG("********************** init value ***********************");
|
this->_label->setString(this->_label->getString() + "\n" + "********************** init value ***********************");
|
||||||
|
|
||||||
// set default value
|
// set default value
|
||||||
|
|
||||||
|
@ -90,29 +96,7 @@ void UserDefaultTest::doTest()
|
||||||
setData<float>("float_data");
|
setData<float>("float_data");
|
||||||
setData<double>("double_data");
|
setData<double>("double_data");
|
||||||
|
|
||||||
// print value
|
printValue();
|
||||||
|
|
||||||
std::string ret = UserDefault::getInstance()->getStringForKey("string");
|
|
||||||
CCLOG("string is %s", ret.c_str());
|
|
||||||
|
|
||||||
double d = UserDefault::getInstance()->getDoubleForKey("double");
|
|
||||||
CCLOG("double is %f", d);
|
|
||||||
|
|
||||||
int i = UserDefault::getInstance()->getIntegerForKey("integer");
|
|
||||||
CCLOG("integer is %d", i);
|
|
||||||
|
|
||||||
float f = UserDefault::getInstance()->getFloatForKey("float");
|
|
||||||
CCLOG("float is %f", f);
|
|
||||||
|
|
||||||
bool b = UserDefault::getInstance()->getBoolForKey("bool");
|
|
||||||
if (b)
|
|
||||||
{
|
|
||||||
CCLOG("bool is true");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
CCLOG("bool is false");
|
|
||||||
}
|
|
||||||
|
|
||||||
logData<int>("int_data");
|
logData<int>("int_data");
|
||||||
logData<float>("float_data");
|
logData<float>("float_data");
|
||||||
|
@ -120,7 +104,7 @@ void UserDefaultTest::doTest()
|
||||||
|
|
||||||
//CCUserDefault::getInstance()->flush();
|
//CCUserDefault::getInstance()->flush();
|
||||||
|
|
||||||
CCLOG("********************** after change value ***********************");
|
this->_label->setString(this->_label->getString() + "\n" + "********************** after change value ***********************");
|
||||||
|
|
||||||
// change the value
|
// change the value
|
||||||
|
|
||||||
|
@ -137,35 +121,59 @@ void UserDefaultTest::doTest()
|
||||||
UserDefault::getInstance()->flush();
|
UserDefault::getInstance()->flush();
|
||||||
|
|
||||||
// print value
|
// print value
|
||||||
|
printValue();
|
||||||
ret = UserDefault::getInstance()->getStringForKey("string");
|
|
||||||
CCLOG("string is %s", ret.c_str());
|
|
||||||
|
|
||||||
d = UserDefault::getInstance()->getDoubleForKey("double");
|
|
||||||
CCLOG("double is %f", d);
|
|
||||||
|
|
||||||
i = UserDefault::getInstance()->getIntegerForKey("integer");
|
|
||||||
CCLOG("integer is %d", i);
|
|
||||||
|
|
||||||
f = UserDefault::getInstance()->getFloatForKey("float");
|
|
||||||
CCLOG("float is %f", f);
|
|
||||||
|
|
||||||
b = UserDefault::getInstance()->getBoolForKey("bool");
|
|
||||||
if (b)
|
|
||||||
{
|
|
||||||
CCLOG("bool is true");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
CCLOG("bool is false");
|
|
||||||
}
|
|
||||||
|
|
||||||
logData<int>("int_data");
|
logData<int>("int_data");
|
||||||
logData<float>("float_data");
|
logData<float>("float_data");
|
||||||
logData<double>("double_data");
|
logData<double>("double_data");
|
||||||
|
|
||||||
|
this->_label->setString(this->_label->getString() + "\n" + "********************** after delete value ***********************");
|
||||||
|
|
||||||
|
UserDefault::getInstance()->deleteValueForKey("string");
|
||||||
|
UserDefault::getInstance()->deleteValueForKey("integer");
|
||||||
|
UserDefault::getInstance()->deleteValueForKey("float");
|
||||||
|
UserDefault::getInstance()->deleteValueForKey("double");
|
||||||
|
UserDefault::getInstance()->deleteValueForKey("bool");
|
||||||
|
|
||||||
|
// print value
|
||||||
|
printValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void UserDefaultTest::printValue()
|
||||||
|
{
|
||||||
|
char strTemp[256] = "";
|
||||||
|
// print value
|
||||||
|
std::string ret = UserDefault::getInstance()->getStringForKey("string");
|
||||||
|
sprintf(strTemp, "string is %s", ret.c_str());
|
||||||
|
this->_label->setString(this->_label->getString() + "\n" + strTemp);
|
||||||
|
|
||||||
|
double d = UserDefault::getInstance()->getDoubleForKey("double");
|
||||||
|
sprintf(strTemp, "double is %f", d);
|
||||||
|
this->_label->setString(this->_label->getString() + "\n" + strTemp);
|
||||||
|
|
||||||
|
int i = UserDefault::getInstance()->getIntegerForKey("integer");
|
||||||
|
sprintf(strTemp, "integer is %d", i);
|
||||||
|
this->_label->setString(this->_label->getString() + "\n" + strTemp);
|
||||||
|
|
||||||
|
float f = UserDefault::getInstance()->getFloatForKey("float");
|
||||||
|
sprintf(strTemp, "float is %f", f);
|
||||||
|
this->_label->setString(this->_label->getString() + "\n" + strTemp);
|
||||||
|
|
||||||
|
bool b = UserDefault::getInstance()->getBoolForKey("bool");
|
||||||
|
if (b)
|
||||||
|
{
|
||||||
|
sprintf(strTemp, "bool is true");
|
||||||
|
this->_label->setString(this->_label->getString() + "\n" + strTemp);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
sprintf(strTemp, "bool is false");
|
||||||
|
this->_label->setString(this->_label->getString() + "\n" + strTemp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
UserDefaultTest::~UserDefaultTest()
|
UserDefaultTest::~UserDefaultTest()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
#include "cocos2d.h"
|
#include "cocos2d.h"
|
||||||
#include "../BaseTest.h"
|
#include "../BaseTest.h"
|
||||||
|
#include "2d/CCLabel.h"
|
||||||
|
|
||||||
DEFINE_TEST_SUITE(UserDefaultTests);
|
DEFINE_TEST_SUITE(UserDefaultTests);
|
||||||
|
|
||||||
|
@ -15,6 +16,8 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void doTest();
|
void doTest();
|
||||||
|
void printValue();
|
||||||
|
cocos2d::Label* _label;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // _USERDEFAULT_TEST_H_
|
#endif // _USERDEFAULT_TEST_H_
|
||||||
|
|
Loading…
Reference in New Issue