#ifndef __SUPPORT_CCUSERDEFAULT_H__
#define __SUPPORT_CCUSERDEFAULT_H__
#include "platform/CCPlatformMacros.h"
#include <string>
NS_CC_BEGIN
/**
* @addtogroup data_storage
* @{
*/
/**
* CCUserDefault acts as a tiny database. You can save and get base type values by it.
* For example, setBoolForKey("played", true) will add a bool value true into the database.
* Its key is "played". You can get the value of the key by getBoolForKey("played").
*
* It supports the following base types:
* bool, int, float, double, string
*/
class CC_DLL CCUserDefault
{
public:
~CCUserDefault();
// get value methods
/**
@brief Get bool value by key, if the key doesn't exist, a default value will return.
You can set the default value, or it is false.
*/
bool getBoolForKey(const char* pKey);
bool getBoolForKey(const char* pKey, bool defaultValue);
/**
@brief Get integer value by key, if the key doesn't exist, a default value will return.
You can set the default value, or it is 0.
*/
int getIntegerForKey(const char* pKey);
int getIntegerForKey(const char* pKey, int defaultValue);
/**
@brief Get float value by key, if the key doesn't exist, a default value will return.
You can set the default value, or it is 0.0f.
*/
float getFloatForKey(const char* pKey);
float getFloatForKey(const char* pKey, float defaultValue);
/**
@brief Get double value by key, if the key doesn't exist, a default value will return.
You can set the default value, or it is 0.0.
*/
double getDoubleForKey(const char* pKey);
double getDoubleForKey(const char* pKey, double defaultValue);
/**
@brief Get string value by key, if the key doesn't exist, a default value will return.
You can set the default value, or it is "".
*/
std::string getStringForKey(const char* pKey);
std::string getStringForKey(const char* pKey, const std::string & defaultValue);
// set value methods
/**
@brief Set bool value by key.
*/
void setBoolForKey(const char* pKey, bool value);
/**
@brief Set integer value by key.
*/
void setIntegerForKey(const char* pKey, int value);
/**
@brief Set float value by key.
*/
void setFloatForKey(const char* pKey, float value);
/**
@brief Set double value by key.
*/
void setDoubleForKey(const char* pKey, double value);
/**
@brief Set string value by key.
*/
void setStringForKey(const char* pKey, const std::string & value);
/**
@brief Save content to xml file
*/
void flush();
static CCUserDefault* sharedUserDefault();
static void purgeSharedUserDefault();
const static std::string& getXMLFilePath();
static bool isXMLFileExist();
private:
CCUserDefault();
static bool createXMLFile();
static void initXMLFilePath();
static CCUserDefault* m_spUserDefault;
static std::string m_sFilePath;
static bool m_sbIsFilePathInitialized;
};
// end of data_storage group
/// @}
NS_CC_END
#endif // __SUPPORT_CCUSERDEFAULT_H__
CCUserDefault是Cocos2d-x引擎提供的持久化方案,其作用是存储所有游戏通用的用户配置信息,例如音乐和音效配置等。为了方便起见,有时我们也可以用CCUserDefault来存储金币数目这种简单的数据项。
CCUserDefault可以看做一个永久存储的字典,本质是一个XML文件,将每个键及其对应的值以节点的形式存储到外存中。值只支持int和float等基本类型.
- CCUserDefault::sharedUserDefault()->setBoolForKey("exit", true);
- pan style="color:#ff0000;"> CCUserDefault::sharedUserDefault()->flush();</span>//这行一定要加上,不然下次启动游戏的时候,该数据就没有保存。
- bool bexit = CCUserDefault::sharedUserDefault()->getBoolForKey("exit");
这里要注意, CCUserDefault中有个 flush()的函数,这个用来将数据写入xml文件中,也就是说当你使用setXX的一些函数后记得提交(调用一下flush函数)
XML的一个很严重的问题是明文存储,存储在外部的数据一旦被截获,就将直接暴露在攻击者面前,小则篡改用户数据,大则泄露用户隐私信息。因此,对存储在文件中的信息加密不可忽视。
幸运的是,前面我们已经设计好了序列化和反序列化过程,只要在其中加入合适的加密和解密算法,即可保证我们的数据不会被轻易窃取。这里我们只使用一个简单的编码轮换来加密,相关代码如下:
- void encode(string &str)
- {
- for(int i = 0; i < str.length(); i++) {
- int ch = str[i];
- ch = 0xff & (((ch & (1 << 7)) >> 7) & (ch << 1));
- str[i] = ch;
- }
- }
- void decode(string &str)
- {
- for(int i = 0; i < str.length(); i++) {
- int ch = str[i];
- ch = 0xff & (((ch & (1)) << 7) & (ch >> 1));
- str[i] = ch;
- }
- }