2013-03-06 16:36:44 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2013-03-06 16:36:44 +08:00
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
#include "CCUserDefault.h"
|
2014-04-27 01:35:57 +08:00
|
|
|
#include "base/CCPlatformConfig.h"
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "2d/platform/CCCommon.h"
|
2013-10-14 14:01:00 +08:00
|
|
|
#include "base64.h"
|
2013-03-06 16:36:44 +08:00
|
|
|
|
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "2d/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h"
|
2013-03-06 16:36:44 +08:00
|
|
|
|
|
|
|
// root name of xml
|
|
|
|
#define USERDEFAULT_ROOT_NAME "userDefaultRoot"
|
|
|
|
|
2013-04-08 21:35:43 +08:00
|
|
|
#define KEEP_COMPATABILITY
|
|
|
|
|
2013-03-06 16:36:44 +08:00
|
|
|
#define XML_FILE_NAME "UserDefault.xml"
|
|
|
|
|
2013-04-08 21:35:43 +08:00
|
|
|
#ifdef KEEP_COMPATABILITY
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "2d/platform/CCFileUtils.h"
|
2013-04-08 21:35:43 +08:00
|
|
|
#include "../tinyxml2/tinyxml2.h"
|
|
|
|
#endif
|
|
|
|
|
2013-03-06 16:36:44 +08:00
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
/**
|
2013-06-20 14:13:12 +08:00
|
|
|
* implements of UserDefault
|
2013-03-06 16:36:44 +08:00
|
|
|
*/
|
|
|
|
|
2013-12-20 14:06:24 +08:00
|
|
|
UserDefault* UserDefault::_userDefault = nullptr;
|
2013-06-20 14:13:12 +08:00
|
|
|
string UserDefault::_filePath = string("");
|
2013-07-12 15:32:37 +08:00
|
|
|
bool UserDefault::_isFilePathInitialized = false;
|
2013-03-06 16:36:44 +08:00
|
|
|
|
2013-04-08 21:35:43 +08:00
|
|
|
#ifdef KEEP_COMPATABILITY
|
|
|
|
static tinyxml2::XMLElement* getXMLNodeForKey(const char* pKey, tinyxml2::XMLDocument **doc)
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
tinyxml2::XMLElement* curNode = nullptr;
|
|
|
|
tinyxml2::XMLElement* rootNode = nullptr;
|
2013-04-08 21:35:43 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
if (! UserDefault::isXMLFileExist())
|
2013-04-08 21:35:43 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
return nullptr;
|
2013-04-08 21:35:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// check the key value
|
|
|
|
if (! pKey)
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
return nullptr;
|
2013-04-08 21:35:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
tinyxml2::XMLDocument* xmlDoc = new tinyxml2::XMLDocument();
|
|
|
|
*doc = xmlDoc;
|
2013-12-05 17:19:01 +08:00
|
|
|
ssize_t size;
|
2013-12-24 18:08:40 +08:00
|
|
|
|
|
|
|
std::string xmlBuffer = FileUtils::getInstance()->getStringFromFile(UserDefault::getInstance()->getXMLFilePath().c_str());
|
|
|
|
|
|
|
|
if (xmlBuffer.empty())
|
2013-04-08 21:35:43 +08:00
|
|
|
{
|
|
|
|
CCLOG("can not read xml file");
|
|
|
|
break;
|
|
|
|
}
|
2013-12-24 18:08:40 +08:00
|
|
|
xmlDoc->Parse(xmlBuffer.c_str());
|
|
|
|
|
2013-04-08 21:35:43 +08:00
|
|
|
// get root node
|
|
|
|
rootNode = xmlDoc->RootElement();
|
2013-12-18 17:47:20 +08:00
|
|
|
if (nullptr == rootNode)
|
2013-04-08 21:35:43 +08:00
|
|
|
{
|
|
|
|
CCLOG("read root node error");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// find the node
|
|
|
|
curNode = rootNode->FirstChildElement();
|
|
|
|
if (!curNode)
|
|
|
|
{
|
|
|
|
// There is not xml node, delete xml file.
|
2013-07-12 06:24:23 +08:00
|
|
|
remove(UserDefault::getInstance()->getXMLFilePath().c_str());
|
2013-04-08 21:35:43 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
return nullptr;
|
2013-04-08 21:35:43 +08:00
|
|
|
}
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
while (nullptr != curNode)
|
2013-04-08 21:35:43 +08:00
|
|
|
{
|
|
|
|
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);
|
2013-07-12 06:24:23 +08:00
|
|
|
doc->SaveFile(UserDefault::getInstance()->getXMLFilePath().c_str());
|
2013-04-08 21:35:43 +08:00
|
|
|
delete doc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void deleteNodeByKey(const char *pKey)
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
tinyxml2::XMLDocument* doc = nullptr;
|
2013-04-08 21:35:43 +08:00
|
|
|
tinyxml2::XMLElement* node = getXMLNodeForKey(pKey, &doc);
|
|
|
|
deleteNode(doc, node);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
UserDefault::~UserDefault()
|
2013-03-06 16:36:44 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
UserDefault::UserDefault()
|
2013-03-06 16:36:44 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-07-12 14:34:57 +08:00
|
|
|
// XXX: deprecated
|
2013-06-20 14:13:12 +08:00
|
|
|
void UserDefault::purgeSharedUserDefault()
|
2013-07-12 14:34:57 +08:00
|
|
|
{
|
|
|
|
UserDefault::destroyInstance();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UserDefault::destroyInstance()
|
2013-03-06 16:36:44 +08:00
|
|
|
{
|
2013-12-20 14:06:24 +08:00
|
|
|
CC_SAFE_DELETE(_userDefault);
|
2013-03-06 16:36:44 +08:00
|
|
|
}
|
|
|
|
|
2013-07-12 14:34:57 +08:00
|
|
|
bool UserDefault::getBoolForKey(const char* pKey)
|
|
|
|
{
|
|
|
|
return getBoolForKey(pKey, false);
|
|
|
|
}
|
2013-03-06 16:36:44 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool UserDefault::getBoolForKey(const char* pKey, bool defaultValue)
|
2013-03-06 16:36:44 +08:00
|
|
|
{
|
2013-04-08 21:35:43 +08:00
|
|
|
#ifdef KEEP_COMPATABILITY
|
2013-12-18 17:47:20 +08:00
|
|
|
tinyxml2::XMLDocument* doc = nullptr;
|
2013-04-08 21:35:43 +08:00
|
|
|
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
|
|
|
|
|
2013-03-11 16:45:20 +08:00
|
|
|
return getBoolForKeyJNI(pKey, defaultValue);
|
2013-03-06 16:36:44 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
int UserDefault::getIntegerForKey(const char* pKey)
|
2013-03-06 16:36:44 +08:00
|
|
|
{
|
|
|
|
return getIntegerForKey(pKey, 0);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
int UserDefault::getIntegerForKey(const char* pKey, int defaultValue)
|
2013-03-06 16:36:44 +08:00
|
|
|
{
|
2013-04-08 21:35:43 +08:00
|
|
|
#ifdef KEEP_COMPATABILITY
|
2013-12-18 17:47:20 +08:00
|
|
|
tinyxml2::XMLDocument* doc = nullptr;
|
2013-04-08 21:35:43 +08:00
|
|
|
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
|
|
|
|
|
2013-03-11 16:45:20 +08:00
|
|
|
return getIntegerForKeyJNI(pKey, defaultValue);
|
2013-03-06 16:36:44 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
float UserDefault::getFloatForKey(const char* pKey)
|
2013-03-06 16:36:44 +08:00
|
|
|
{
|
|
|
|
return getFloatForKey(pKey, 0.0f);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
float UserDefault::getFloatForKey(const char* pKey, float defaultValue)
|
2013-03-06 16:36:44 +08:00
|
|
|
{
|
2013-04-08 21:35:43 +08:00
|
|
|
#ifdef KEEP_COMPATABILITY
|
2013-12-18 17:47:20 +08:00
|
|
|
tinyxml2::XMLDocument* doc = nullptr;
|
2013-04-08 21:35:43 +08:00
|
|
|
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
|
|
|
|
|
2013-03-11 16:45:20 +08:00
|
|
|
return getFloatForKeyJNI(pKey, defaultValue);
|
2013-03-06 16:36:44 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
double UserDefault::getDoubleForKey(const char* pKey)
|
2013-03-06 16:36:44 +08:00
|
|
|
{
|
|
|
|
return getDoubleForKey(pKey, 0.0);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
double UserDefault::getDoubleForKey(const char* pKey, double defaultValue)
|
2013-03-06 16:36:44 +08:00
|
|
|
{
|
2013-04-08 21:35:43 +08:00
|
|
|
#ifdef KEEP_COMPATABILITY
|
2013-12-18 17:47:20 +08:00
|
|
|
tinyxml2::XMLDocument* doc = nullptr;
|
2013-04-08 21:35:43 +08:00
|
|
|
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
|
|
|
|
|
2013-03-11 16:45:20 +08:00
|
|
|
return getDoubleForKeyJNI(pKey, defaultValue);
|
2013-03-06 16:36:44 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
std::string UserDefault::getStringForKey(const char* pKey)
|
2013-03-06 16:36:44 +08:00
|
|
|
{
|
|
|
|
return getStringForKey(pKey, "");
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
string UserDefault::getStringForKey(const char* pKey, const std::string & defaultValue)
|
2013-03-06 16:36:44 +08:00
|
|
|
{
|
2013-04-08 21:35:43 +08:00
|
|
|
#ifdef KEEP_COMPATABILITY
|
2013-12-18 17:47:20 +08:00
|
|
|
tinyxml2::XMLDocument* doc = nullptr;
|
2013-04-08 21:35:43 +08:00
|
|
|
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
|
|
|
|
|
2013-03-11 16:45:20 +08:00
|
|
|
return getStringForKeyJNI(pKey, defaultValue.c_str());
|
2013-03-06 16:36:44 +08:00
|
|
|
}
|
|
|
|
|
2013-12-18 14:58:17 +08:00
|
|
|
Data UserDefault::getDataForKey(const char* pKey)
|
2013-05-29 08:06:41 +08:00
|
|
|
{
|
2013-12-18 14:58:17 +08:00
|
|
|
return getDataForKey(pKey, Data::Null);
|
2013-05-29 08:06:41 +08:00
|
|
|
}
|
|
|
|
|
2013-12-18 14:58:17 +08:00
|
|
|
Data UserDefault::getDataForKey(const char* pKey, const Data& defaultValue)
|
2013-05-29 08:06:41 +08:00
|
|
|
{
|
|
|
|
#ifdef KEEP_COMPATABILITY
|
2013-12-18 17:47:20 +08:00
|
|
|
tinyxml2::XMLDocument* doc = nullptr;
|
2013-05-29 08:06:41 +08:00
|
|
|
tinyxml2::XMLElement* node = getXMLNodeForKey(pKey, &doc);
|
|
|
|
if (node)
|
|
|
|
{
|
|
|
|
if (node->FirstChild())
|
|
|
|
{
|
|
|
|
const char * encodedData = node->FirstChild()->Value();
|
|
|
|
|
|
|
|
unsigned char * decodedData;
|
|
|
|
int decodedDataLen = base64Decode((unsigned char*)encodedData, (unsigned int)strlen(encodedData), &decodedData);
|
|
|
|
|
|
|
|
if (decodedData) {
|
2013-12-18 14:58:17 +08:00
|
|
|
Data ret;
|
|
|
|
ret.fastSet(decodedData, decodedDataLen);
|
2013-05-29 08:06:41 +08:00
|
|
|
|
|
|
|
// set value in NSUserDefaults
|
|
|
|
setDataForKey(pKey, ret);
|
|
|
|
|
|
|
|
flush();
|
|
|
|
|
|
|
|
// delete xmle node
|
|
|
|
deleteNode(doc, node);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// delete xmle node
|
|
|
|
deleteNode(doc, node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
char * encodedDefaultData = NULL;
|
2013-12-20 21:51:53 +08:00
|
|
|
unsigned int encodedDefaultDataLen = !defaultValue.isNull() ? base64Encode(defaultValue.getBytes(), defaultValue.getSize(), &encodedDefaultData) : 0;
|
2013-05-29 08:06:41 +08:00
|
|
|
|
|
|
|
string encodedStr = getStringForKeyJNI(pKey, encodedDefaultData);
|
|
|
|
|
2013-11-12 10:09:47 +08:00
|
|
|
if (encodedDefaultData)
|
|
|
|
free(encodedDefaultData);
|
2013-05-29 08:06:41 +08:00
|
|
|
|
|
|
|
CCLOG("ENCODED STRING: --%s--%d", encodedStr.c_str(), encodedStr.length());
|
2013-12-18 14:58:17 +08:00
|
|
|
|
2013-05-29 08:06:41 +08:00
|
|
|
unsigned char * decodedData = NULL;
|
|
|
|
int decodedDataLen = base64Decode((unsigned char*)encodedStr.c_str(), (unsigned int)encodedStr.length(), &decodedData);
|
|
|
|
|
2013-12-18 14:58:17 +08:00
|
|
|
CCLOG("DECODED DATA: %s %d", decodedData, decodedDataLen);
|
2013-05-29 08:06:41 +08:00
|
|
|
|
|
|
|
if (decodedData && decodedDataLen) {
|
2013-12-18 14:58:17 +08:00
|
|
|
Data ret;
|
|
|
|
ret.fastSet(decodedData, decodedDataLen);
|
|
|
|
return ret;
|
2013-05-29 08:06:41 +08:00
|
|
|
}
|
|
|
|
|
2013-12-18 14:58:17 +08:00
|
|
|
return defaultValue;
|
2013-05-29 08:06:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void UserDefault::setBoolForKey(const char* pKey, bool value)
|
2013-03-06 16:36:44 +08:00
|
|
|
{
|
2013-04-08 21:35:43 +08:00
|
|
|
#ifdef KEEP_COMPATABILITY
|
|
|
|
deleteNodeByKey(pKey);
|
|
|
|
#endif
|
|
|
|
|
2013-03-11 16:45:20 +08:00
|
|
|
return setBoolForKeyJNI(pKey, value);
|
2013-03-06 16:36:44 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void UserDefault::setIntegerForKey(const char* pKey, int value)
|
2013-03-06 16:36:44 +08:00
|
|
|
{
|
2013-04-08 21:35:43 +08:00
|
|
|
#ifdef KEEP_COMPATABILITY
|
|
|
|
deleteNodeByKey(pKey);
|
|
|
|
#endif
|
|
|
|
|
2013-03-11 16:45:20 +08:00
|
|
|
return setIntegerForKeyJNI(pKey, value);
|
2013-03-06 16:36:44 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void UserDefault::setFloatForKey(const char* pKey, float value)
|
2013-03-06 16:36:44 +08:00
|
|
|
{
|
2013-04-08 21:35:43 +08:00
|
|
|
#ifdef KEEP_COMPATABILITY
|
|
|
|
deleteNodeByKey(pKey);
|
|
|
|
#endif
|
|
|
|
|
2013-03-11 16:45:20 +08:00
|
|
|
return setFloatForKeyJNI(pKey, value);
|
2013-03-06 16:36:44 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void UserDefault::setDoubleForKey(const char* pKey, double value)
|
2013-03-06 16:36:44 +08:00
|
|
|
{
|
2013-04-08 21:35:43 +08:00
|
|
|
#ifdef KEEP_COMPATABILITY
|
|
|
|
deleteNodeByKey(pKey);
|
|
|
|
#endif
|
|
|
|
|
2013-03-11 16:45:20 +08:00
|
|
|
return setDoubleForKeyJNI(pKey, value);
|
2013-03-06 16:36:44 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void UserDefault::setStringForKey(const char* pKey, const std::string & value)
|
2013-03-06 16:36:44 +08:00
|
|
|
{
|
2013-04-08 21:35:43 +08:00
|
|
|
#ifdef KEEP_COMPATABILITY
|
|
|
|
deleteNodeByKey(pKey);
|
|
|
|
#endif
|
|
|
|
|
2013-03-11 16:45:20 +08:00
|
|
|
return setStringForKeyJNI(pKey, value.c_str());
|
2013-03-06 16:36:44 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void UserDefault::setDataForKey(const char* pKey, const Data& value)
|
2013-05-29 08:06:41 +08:00
|
|
|
{
|
|
|
|
#ifdef KEEP_COMPATABILITY
|
|
|
|
deleteNodeByKey(pKey);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
CCLOG("SET DATA FOR KEY: --%s--%d", value.getBytes(), value.getSize());
|
2013-12-18 17:47:20 +08:00
|
|
|
char * encodedData = nullptr;
|
2013-05-29 08:06:41 +08:00
|
|
|
unsigned int encodedDataLen = base64Encode(value.getBytes(), value.getSize(), &encodedData);
|
|
|
|
|
|
|
|
CCLOG("SET DATA ENCODED: --%s", encodedData);
|
|
|
|
|
2013-12-06 11:53:43 +08:00
|
|
|
setStringForKeyJNI(pKey, encodedData);
|
2013-05-29 08:06:41 +08:00
|
|
|
|
2013-11-12 10:09:47 +08:00
|
|
|
if (encodedData)
|
|
|
|
free(encodedData);
|
2013-05-29 08:06:41 +08:00
|
|
|
}
|
|
|
|
|
2013-07-12 14:34:57 +08:00
|
|
|
// XXX: deprecated
|
2013-06-20 14:13:12 +08:00
|
|
|
UserDefault* UserDefault::sharedUserDefault()
|
2013-07-12 14:34:57 +08:00
|
|
|
{
|
|
|
|
return UserDefault::getInstance();
|
|
|
|
}
|
|
|
|
|
|
|
|
UserDefault* UserDefault::getInstance()
|
2013-03-06 16:36:44 +08:00
|
|
|
{
|
2013-04-08 21:35:43 +08:00
|
|
|
#ifdef KEEP_COMPATABILITY
|
|
|
|
initXMLFilePath();
|
|
|
|
#endif
|
|
|
|
|
2013-07-12 15:21:21 +08:00
|
|
|
if (! _userDefault)
|
2013-03-06 16:36:44 +08:00
|
|
|
{
|
2013-07-12 15:21:21 +08:00
|
|
|
_userDefault = new UserDefault();
|
2013-03-06 16:36:44 +08:00
|
|
|
}
|
|
|
|
|
2013-07-12 15:21:21 +08:00
|
|
|
return _userDefault;
|
2013-03-06 16:36:44 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool UserDefault::isXMLFileExist()
|
2013-03-06 16:36:44 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
FILE *fp = fopen(_filePath.c_str(), "r");
|
2013-04-08 21:35:43 +08:00
|
|
|
bool bRet = false;
|
|
|
|
|
|
|
|
if (fp)
|
|
|
|
{
|
|
|
|
bRet = true;
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
return bRet;
|
2013-03-06 16:36:44 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void UserDefault::initXMLFilePath()
|
2013-04-08 21:35:43 +08:00
|
|
|
{
|
|
|
|
#ifdef KEEP_COMPATABILITY
|
2013-07-12 15:32:37 +08:00
|
|
|
if (! _isFilePathInitialized)
|
2013-04-08 21:35:43 +08:00
|
|
|
{
|
2013-04-10 11:39:01 +08:00
|
|
|
// UserDefault.xml is stored in /data/data/<package-path>/ before v2.1.2
|
2013-06-15 14:03:30 +08:00
|
|
|
_filePath += "/data/data/" + getPackageNameJNI() + "/" + XML_FILE_NAME;
|
2013-07-12 15:32:37 +08:00
|
|
|
_isFilePathInitialized = true;
|
2013-04-08 21:35:43 +08:00
|
|
|
}
|
|
|
|
#endif
|
2013-03-06 16:36:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// create new xml file
|
2013-06-20 14:13:12 +08:00
|
|
|
bool UserDefault::createXMLFile()
|
2013-03-06 16:36:44 +08:00
|
|
|
{
|
2013-04-08 21:35:43 +08:00
|
|
|
return false;
|
2013-03-06 16:36:44 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
const string& UserDefault::getXMLFilePath()
|
2013-03-06 16:36:44 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _filePath;
|
2013-03-06 16:36:44 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void UserDefault::flush()
|
2013-03-06 16:36:44 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|
|
|
|
#endif // (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
|