axmol/cocos/base/CCUserDefault.cpp

542 lines
12 KiB
C++
Raw Normal View History

/****************************************************************************
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2013-2014 Chukong Technologies Inc.
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.
****************************************************************************/
Squashed commit of the following: commit a794d107ad85667e3d754f0b6251fc864dfbf288 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 14:33:49 2014 -0700 Yeah... everything compiles on win32 and wp8 commit 4740be6e4a0d16f742c27996e7ab2c100adc76af Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:58:38 2014 -0700 CCIME moved to base and compiles on Android commit ff3e1bf1eb27a01019f4e1b56d1aebbe2d385f72 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:02:57 2014 -0700 compiles Ok for Windows Phone 8 commit 8160a4eb2ecdc61b5bd1cf56b90d2da6f11e3ebd Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 12:25:31 2014 -0700 fixes for Windows Phone 8 commit 418197649efc93032aee0adc205e502101cdb53d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 11:15:13 2014 -0700 Compiles on Win32 commit 08813ed7cf8ac1079ffadeb1ce78ea9e833e1a33 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 10:08:31 2014 -0700 Compiles on linux! commit 118896521e5b335a5257090b6863f1fb2a2002fe Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 09:30:42 2014 -0700 moves cocos/2d/platform -> cocos/platform commit 4fe9319d7717b0c1bccb2db0156eeb86255a89e0 Merge: bd68ec2 511295e Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 08:24:41 2014 -0700 Merge remote-tracking branch 'cocos2d/v3' into files commit bd68ec2f0e3a826d8b2f4b60564ba65ce766bc56 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu May 15 19:36:23 2014 -0700 files in the correct directory
2014-05-17 05:36:00 +08:00
#include "base/CCUserDefault.h"
#include "platform/CCCommon.h"
#include "platform/CCFileUtils.h"
#include "tinyxml2.h"
2014-04-30 08:37:36 +08:00
#include "base/base64.h"
2014-07-14 23:05:16 +08:00
#include "base/ccUtils.h"
#if (CC_TARGET_PLATFORM != CC_PLATFORM_IOS && CC_TARGET_PLATFORM != CC_PLATFORM_MAC && CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID)
// root name of xml
#define USERDEFAULT_ROOT_NAME "userDefaultRoot"
#define XML_FILE_NAME "UserDefault.xml"
using namespace std;
NS_CC_BEGIN
/**
* define the functions here because we don't want to
* export xmlNodePtr and other types in "CCUserDefault.h"
*/
2013-01-26 14:27:49 +08:00
static tinyxml2::XMLElement* getXMLNodeForKey(const char* pKey, tinyxml2::XMLElement** rootNode, tinyxml2::XMLDocument **doc)
{
tinyxml2::XMLElement* curNode = nullptr;
// check the key value
if (! pKey)
{
return nullptr;
}
do
{
tinyxml2::XMLDocument* xmlDoc = new (std::nothrow) tinyxml2::XMLDocument();
2015-07-06 17:01:17 +08:00
*doc = xmlDoc;
std::string xmlBuffer = FileUtils::getInstance()->getStringFromFile(UserDefault::getInstance()->getXMLFilePath());
2015-07-06 17:01:17 +08:00
if (xmlBuffer.empty())
{
CCLOG("can not read xml file");
break;
}
xmlDoc->Parse(xmlBuffer.c_str(), xmlBuffer.size());
// get root node
*rootNode = xmlDoc->RootElement();
if (nullptr == *rootNode)
{
CCLOG("read root node error");
break;
}
// find the node
curNode = (*rootNode)->FirstChildElement();
while (nullptr != curNode)
{
const char* nodeName = curNode->Value();
if (!strcmp(nodeName, pKey))
{
break;
}
curNode = curNode->NextSiblingElement();
}
} while (0);
return curNode;
}
static void setValueForKey(const char* pKey, const char* pValue)
{
2015-07-06 17:01:17 +08:00
tinyxml2::XMLElement* rootNode;
tinyxml2::XMLDocument* doc;
tinyxml2::XMLElement* node;
// check the params
if (! pKey || ! pValue)
{
return;
}
// find the node
node = getXMLNodeForKey(pKey, &rootNode, &doc);
// if node exist, change the content
if (node)
{
2013-02-26 12:44:20 +08:00
if (node->FirstChild())
{
node->FirstChild()->SetValue(pValue);
}
else
{
tinyxml2::XMLText* content = doc->NewText(pValue);
node->LinkEndChild(content);
}
2015-07-06 17:01:17 +08:00
}
else
{
if (rootNode)
{
tinyxml2::XMLElement* tmpNode = doc->NewElement(pKey);//new tinyxml2::XMLElement(pKey);
rootNode->LinkEndChild(tmpNode);
tinyxml2::XMLText* content = doc->NewText(pValue);//new tinyxml2::XMLText(pValue);
tmpNode->LinkEndChild(content);
}
}
2013-01-26 10:41:27 +08:00
2013-02-26 12:44:20 +08:00
// save file and free doc
2015-07-06 17:01:17 +08:00
if (doc)
{
doc->SaveFile(FileUtils::getInstance()->getSuitableFOpen(UserDefault::getInstance()->getXMLFilePath()).c_str());
2015-07-06 17:01:17 +08:00
delete doc;
}
}
/**
* implements of UserDefault
*/
UserDefault* UserDefault::_userDefault = nullptr;
string UserDefault::_filePath = string("");
bool UserDefault::_isFilePathInitialized = false;
UserDefault::~UserDefault()
{
}
UserDefault::UserDefault()
{
}
bool UserDefault::getBoolForKey(const char* pKey)
{
return getBoolForKey(pKey, false);
}
bool UserDefault::getBoolForKey(const char* pKey, bool defaultValue)
{
const char* value = nullptr;
2015-07-06 17:01:17 +08:00
tinyxml2::XMLElement* rootNode;
tinyxml2::XMLDocument* doc;
tinyxml2::XMLElement* node;
node = getXMLNodeForKey(pKey, &rootNode, &doc);
// find the node
if (node && node->FirstChild())
{
2013-01-26 10:41:27 +08:00
value = (const char*)(node->FirstChild()->Value());
2015-07-06 17:01:17 +08:00
}
2013-01-26 10:41:27 +08:00
2015-07-06 17:01:17 +08:00
bool ret = defaultValue;
2013-01-26 10:41:27 +08:00
2015-07-06 17:01:17 +08:00
if (value)
{
ret = (! strcmp(value, "true"));
}
2013-01-26 10:41:27 +08:00
if (doc) delete doc;
2015-07-06 17:01:17 +08:00
return ret;
}
int UserDefault::getIntegerForKey(const char* pKey)
{
return getIntegerForKey(pKey, 0);
}
int UserDefault::getIntegerForKey(const char* pKey, int defaultValue)
{
2015-07-06 17:01:17 +08:00
const char* value = nullptr;
tinyxml2::XMLElement* rootNode;
tinyxml2::XMLDocument* doc;
tinyxml2::XMLElement* node;
node = getXMLNodeForKey(pKey, &rootNode, &doc);
// find the node
if (node && node->FirstChild())
{
2013-01-26 10:41:27 +08:00
value = (const char*)(node->FirstChild()->Value());
2015-07-06 17:01:17 +08:00
}
2013-01-26 10:41:27 +08:00
2015-07-06 17:01:17 +08:00
int ret = defaultValue;
2013-01-26 10:41:27 +08:00
2015-07-06 17:01:17 +08:00
if (value)
{
ret = atoi(value);
}
2013-01-26 10:41:27 +08:00
2015-07-06 17:01:17 +08:00
if(doc)
{
delete doc;
}
2013-01-26 10:41:27 +08:00
2015-07-06 17:01:17 +08:00
return ret;
}
float UserDefault::getFloatForKey(const char* pKey)
{
return getFloatForKey(pKey, 0.0f);
}
float UserDefault::getFloatForKey(const char* pKey, float defaultValue)
{
float ret = (float)getDoubleForKey(pKey, (double)defaultValue);
return ret;
}
double UserDefault::getDoubleForKey(const char* pKey)
{
return getDoubleForKey(pKey, 0.0);
}
double UserDefault::getDoubleForKey(const char* pKey, double defaultValue)
{
2015-07-06 17:01:17 +08:00
const char* value = nullptr;
tinyxml2::XMLElement* rootNode;
tinyxml2::XMLDocument* doc;
tinyxml2::XMLElement* node;
node = getXMLNodeForKey(pKey, &rootNode, &doc);
// find the node
if (node && node->FirstChild())
{
2013-01-26 10:41:27 +08:00
value = (const char*)(node->FirstChild()->Value());
2015-07-06 17:01:17 +08:00
}
2013-01-26 10:41:27 +08:00
2015-07-06 17:01:17 +08:00
double ret = defaultValue;
2013-01-26 10:41:27 +08:00
2015-07-06 17:01:17 +08:00
if (value)
{
ret = utils::atof(value);
}
2013-01-26 10:41:27 +08:00
if (doc) delete doc;
2015-07-06 17:01:17 +08:00
return ret;
}
std::string UserDefault::getStringForKey(const char* pKey)
{
return getStringForKey(pKey, "");
}
string UserDefault::getStringForKey(const char* pKey, const std::string & defaultValue)
{
const char* value = nullptr;
2015-07-06 17:01:17 +08:00
tinyxml2::XMLElement* rootNode;
tinyxml2::XMLDocument* doc;
tinyxml2::XMLElement* node;
node = getXMLNodeForKey(pKey, &rootNode, &doc);
// find the node
if (node && node->FirstChild())
{
2013-01-26 10:41:27 +08:00
value = (const char*)(node->FirstChild()->Value());
2015-07-06 17:01:17 +08:00
}
2013-01-26 10:41:27 +08:00
2015-07-06 17:01:17 +08:00
string ret = defaultValue;
2013-01-26 10:41:27 +08:00
2015-07-06 17:01:17 +08:00
if (value)
{
ret = string(value);
}
2013-01-26 10:41:27 +08:00
if (doc) delete doc;
2015-07-06 17:01:17 +08:00
return ret;
}
Data UserDefault::getDataForKey(const char* pKey)
{
2013-12-20 21:16:46 +08:00
return getDataForKey(pKey, Data::Null);
}
Data UserDefault::getDataForKey(const char* pKey, const Data& defaultValue)
{
const char* encodedData = nullptr;
2015-07-06 17:01:17 +08:00
tinyxml2::XMLElement* rootNode;
tinyxml2::XMLDocument* doc;
tinyxml2::XMLElement* node;
node = getXMLNodeForKey(pKey, &rootNode, &doc);
// find the node
if (node && node->FirstChild())
{
encodedData = (const char*)(node->FirstChild()->Value());
2015-07-06 17:01:17 +08:00
}
2015-07-06 17:01:17 +08:00
Data ret = defaultValue;
2015-07-06 17:01:17 +08:00
if (encodedData)
{
unsigned char * decodedData = nullptr;
int decodedDataLen = base64Decode((unsigned char*)encodedData, (unsigned int)strlen(encodedData), &decodedData);
if (decodedData) {
ret.fastSet(decodedData, decodedDataLen);
}
2015-07-06 17:01:17 +08:00
}
if (doc) delete doc;
2015-07-06 17:01:17 +08:00
return ret;
}
void UserDefault::setBoolForKey(const char* pKey, bool value)
{
// save bool value as string
if (true == value)
{
setStringForKey(pKey, "true");
}
else
{
setStringForKey(pKey, "false");
}
}
void UserDefault::setIntegerForKey(const char* pKey, int value)
{
// check key
if (! pKey)
{
return;
}
// format the value
char tmp[50];
memset(tmp, 0, 50);
sprintf(tmp, "%d", value);
setValueForKey(pKey, tmp);
}
void UserDefault::setFloatForKey(const char* pKey, float value)
{
setDoubleForKey(pKey, value);
}
void UserDefault::setDoubleForKey(const char* pKey, double value)
{
// check key
if (! pKey)
{
return;
}
// format the value
char tmp[50];
memset(tmp, 0, 50);
sprintf(tmp, "%f", value);
setValueForKey(pKey, tmp);
}
void UserDefault::setStringForKey(const char* pKey, const std::string & value)
{
// check key
if (! pKey)
{
return;
}
setValueForKey(pKey, value.c_str());
}
void UserDefault::setDataForKey(const char* pKey, const Data& value) {
// check key
if (! pKey)
{
return;
}
2016-05-25 02:09:11 +08:00
char *encodedData = nullptr;
2013-12-06 16:32:06 +08:00
base64Encode(value.getBytes(), static_cast<unsigned int>(value.getSize()), &encodedData);
setValueForKey(pKey, encodedData);
if (encodedData)
free(encodedData);
}
UserDefault* UserDefault::getInstance()
{
if (!_userDefault)
{
initXMLFilePath();
// only create xml file one time
// the file exists after the program exit
if ((!isXMLFileExist()) && (!createXMLFile()))
{
return nullptr;
}
_userDefault = new (std::nothrow) UserDefault();
}
return _userDefault;
}
void UserDefault::destroyInstance()
{
CC_SAFE_DELETE(_userDefault);
}
void UserDefault::setDelegate(UserDefault *delegate)
{
if (_userDefault)
delete _userDefault;
_userDefault = delegate;
}
// FIXME:: deprecated
UserDefault* UserDefault::sharedUserDefault()
{
return UserDefault::getInstance();
}
// FIXME:: deprecated
void UserDefault::purgeSharedUserDefault()
{
return UserDefault::destroyInstance();
}
bool UserDefault::isXMLFileExist()
{
return FileUtils::getInstance()->isFileExist(_filePath);
}
void UserDefault::initXMLFilePath()
{
if (! _isFilePathInitialized)
{
_filePath += FileUtils::getInstance()->getWritablePath() + XML_FILE_NAME;
_isFilePathInitialized = true;
}
}
// create new xml file
bool UserDefault::createXMLFile()
{
2015-07-06 17:01:17 +08:00
bool bRet = false;
tinyxml2::XMLDocument *pDoc = new (std::nothrow) tinyxml2::XMLDocument();
if (nullptr==pDoc)
2013-01-26 10:41:27 +08:00
{
return false;
}
2015-07-06 17:01:17 +08:00
tinyxml2::XMLDeclaration *pDeclaration = pDoc->NewDeclaration(nullptr);
if (nullptr==pDeclaration)
{
return false;
}
pDoc->LinkEndChild(pDeclaration);
tinyxml2::XMLElement *pRootEle = pDoc->NewElement(USERDEFAULT_ROOT_NAME);
if (nullptr==pRootEle)
{
return false;
}
pDoc->LinkEndChild(pRootEle);
bRet = tinyxml2::XML_SUCCESS == pDoc->SaveFile(FileUtils::getInstance()->getSuitableFOpen(_filePath).c_str());
2013-01-26 10:41:27 +08:00
2015-07-06 17:01:17 +08:00
if(pDoc)
{
delete pDoc;
}
2013-01-26 10:41:27 +08:00
2015-07-06 17:01:17 +08:00
return bRet;
}
const string& UserDefault::getXMLFilePath()
{
return _filePath;
}
void UserDefault::flush()
{
}
2015-07-06 17:01:17 +08:00
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
#endif // (CC_TARGET_PLATFORM != CC_PLATFORM_IOS && CC_PLATFORM != CC_PLATFORM_ANDROID)