user_default/CCUserDefault(本地缓存 --通过读写xml文件)

本文介绍了Cocos2d-x引擎中CCUserDefault类的功能与用法,用于存储游戏通用的用户配置信息,并讨论了如何在使用此类时进行数据加密以保护用户隐私。通过示例展示了如何设置和获取各种类型的配置值,并提供了一个简单的加密方法来确保数据安全性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#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等基本类型.

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. CCUserDefault::sharedUserDefault()->setBoolForKey("exit"true);  
  2. pan style="color:#ff0000;">  CCUserDefault::sharedUserDefault()->flush();</span>//这行一定要加上,不然下次启动游戏的时候,该数据就没有保存。  
  3. bool bexit = CCUserDefault::sharedUserDefault()->getBoolForKey("exit");  

这里要注意,    CCUserDefault中有个  flush()的函数,这个用来将数据写入xml文件中,也就是说当你使用setXX的一些函数后记得提交(调用一下flush函数)

XML的一个很严重的问题是明文存储,存储在外部的数据一旦被截获,就将直接暴露在攻击者面前,小则篡改用户数据,大则泄露用户隐私信息。因此,对存储在文件中的信息加密不可忽视。
 幸运的是,前面我们已经设计好了序列化和反序列化过程,只要在其中加入合适的加密和解密算法,即可保证我们的数据不会被轻易窃取。这里我们只使用一个简单的编码轮换来加密,相关代码如下:


[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. void encode(string &str)  
  2. {  
  3.     for(int i = 0; i < str.length(); i++) {  
  4.         int ch = str[i];  
  5.         ch = 0xff & (((ch & (1 << 7)) >> 7) & (ch << 1));  
  6.         str[i] = ch;  
  7.     }  
  8. }  
  9. void decode(string &str)  
  10. {  
  11.   
  12.     for(int i = 0; i < str.length(); i++) {  
  13.         int ch = str[i];  
  14.         ch = 0xff & (((ch & (1)) << 7) & (ch >> 1));  
  15.         str[i] = ch;  
  16.     }  
  17.   
  18. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值