2011-05-06 15:31:51 +08:00
|
|
|
/****************************************************************************
|
2012-09-24 21:22:20 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2020-02-04 22:28:54 +08:00
|
|
|
Copyright (c) 2017-2020 c4games.com.
|
2011-05-06 15:31:51 +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.
|
2012-04-19 14:35:52 +08:00
|
|
|
****************************************************************************/
|
|
|
|
#ifndef __SUPPORT_CCUSERDEFAULT_H__
|
|
|
|
#define __SUPPORT_CCUSERDEFAULT_H__
|
|
|
|
|
2014-09-10 08:17:07 +08:00
|
|
|
#include "platform/CCPlatformMacros.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
#include <string>
|
2020-02-04 22:28:54 +08:00
|
|
|
|
|
|
|
#include <unordered_map>
|
|
|
|
#include "mio/mio.hpp"
|
2020-02-06 23:30:33 +08:00
|
|
|
#include "yasio/cxx17/string_view.hpp"
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-06-20 18:09:11 +08:00
|
|
|
/**
|
2015-03-24 11:15:40 +08:00
|
|
|
* @addtogroup base
|
2012-06-20 18:09:11 +08:00
|
|
|
* @{
|
|
|
|
*/
|
2015-03-27 12:01:20 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2012-06-20 18:09:11 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/**
|
2013-06-20 14:13:12 +08:00
|
|
|
* UserDefault acts as a tiny database. You can save and get base type values by it.
|
2012-04-19 14:35:52 +08:00
|
|
|
* 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
|
2017-01-09 09:27:38 +08:00
|
|
|
*
|
|
|
|
* @warning: On windows, linux, use XML to store data, which means there are some limitations of
|
|
|
|
* the key string, for example, `/` is not valid.
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
class CC_DLL UserDefault
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
// get value methods
|
|
|
|
|
|
|
|
/**
|
2015-03-18 21:24:31 +08:00
|
|
|
* Get bool value by key, if the key doesn't exist, will return false.
|
|
|
|
* You can set the default value, or it is false.
|
|
|
|
* @param key The key to get value.
|
|
|
|
* @return Bool value by `key`.
|
|
|
|
* @js NA
|
|
|
|
*/
|
|
|
|
bool getBoolForKey(const char* key);
|
|
|
|
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
2015-03-18 21:24:31 +08:00
|
|
|
* Get bool value by key, if the key doesn't exist, will return passed default value.
|
|
|
|
* @param key The key to get value.
|
|
|
|
* @param defaultValue The default value to return if the key doesn't exist.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
*/
|
2015-04-20 03:07:48 +08:00
|
|
|
virtual bool getBoolForKey(const char* key, bool defaultValue);
|
2015-03-18 21:24:31 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/**
|
2015-03-18 21:24:31 +08:00
|
|
|
* Get integer value by key, if the key doesn't exist, will return 0.
|
|
|
|
* You can set the default value, or it is 0.
|
|
|
|
* @param key The key to get value.
|
|
|
|
* @return Integer value of the key.
|
|
|
|
* @js NA
|
|
|
|
*/
|
|
|
|
int getIntegerForKey(const char* key);
|
|
|
|
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
2015-03-18 21:24:31 +08:00
|
|
|
* Get bool value by key, if the key doesn't exist, will return passed default value.
|
|
|
|
* @param key The key to get value.
|
|
|
|
* @param defaultValue The default value to return if the key doesn't exist.
|
|
|
|
* @return Integer value of the key.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
*/
|
2015-04-20 03:07:48 +08:00
|
|
|
virtual int getIntegerForKey(const char* key, int defaultValue);
|
2015-03-18 21:24:31 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/**
|
2015-03-18 21:24:31 +08:00
|
|
|
* Get float value by key, if the key doesn't exist, will return 0.0.
|
|
|
|
* @param key The key to get value.
|
|
|
|
* @return Float value of the key.
|
|
|
|
* @js NA
|
|
|
|
*/
|
|
|
|
float getFloatForKey(const char* key);
|
|
|
|
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
2015-03-18 21:24:31 +08:00
|
|
|
* Get float value by key, if the key doesn't exist, will return passed default value.
|
|
|
|
* @param key The key to get value.
|
|
|
|
* @param defaultValue The default value to return if the key doesn't exist.
|
|
|
|
* @return Float value of the key.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
*/
|
2015-04-20 03:07:48 +08:00
|
|
|
virtual float getFloatForKey(const char* key, float defaultValue);
|
2015-03-18 21:24:31 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/**
|
2015-03-18 21:24:31 +08:00
|
|
|
* Get double value by key, if the key doesn't exist, will return 0.0.
|
|
|
|
* @param key The key to get value.
|
|
|
|
* @return Double value of the key.
|
|
|
|
* @js NA
|
|
|
|
*/
|
|
|
|
double getDoubleForKey(const char* key);
|
|
|
|
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
2015-03-18 21:24:31 +08:00
|
|
|
* Get double value by key, if the key doesn't exist, will return passed default value.
|
|
|
|
* @param key The key to get value.
|
|
|
|
* @param defaultValue The default value to return if the key doesn't exist.
|
|
|
|
* @return Double value of the key.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
*/
|
2015-04-20 03:07:48 +08:00
|
|
|
virtual double getDoubleForKey(const char* key, double defaultValue);
|
2015-03-18 21:24:31 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/**
|
2015-03-18 21:24:31 +08:00
|
|
|
* Get string value by key, if the key doesn't exist, will return an empty string.
|
|
|
|
* @param key The key to get value.
|
|
|
|
* @return String value of the key.
|
|
|
|
* @js NA
|
|
|
|
*/
|
|
|
|
std::string getStringForKey(const char* key);
|
|
|
|
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
2015-03-18 21:24:31 +08:00
|
|
|
* Get string value by key, if the key doesn't exist, will return passed default value.
|
|
|
|
* @param key The key to get value.
|
|
|
|
* @param defaultValue The default value to return if the key doesn't exist.
|
|
|
|
* @return String value of the key.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
*/
|
2015-04-20 03:07:48 +08:00
|
|
|
virtual std::string getStringForKey(const char* key, const std::string & defaultValue);
|
2015-03-18 21:24:31 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// set value methods
|
|
|
|
|
|
|
|
/**
|
2015-03-18 21:24:31 +08:00
|
|
|
* Set bool value by key.
|
|
|
|
* @param key The key to set.
|
|
|
|
* @param value A bool value to set to the key.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
*/
|
2015-04-20 03:07:48 +08:00
|
|
|
virtual void setBoolForKey(const char* key, bool value);
|
2012-04-19 14:35:52 +08:00
|
|
|
/**
|
2015-03-18 21:24:31 +08:00
|
|
|
* Set integer value by key.
|
|
|
|
* @param key The key to set.
|
|
|
|
* @param value A integer value to set to the key.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
*/
|
2015-04-20 03:07:48 +08:00
|
|
|
virtual void setIntegerForKey(const char* key, int value);
|
2012-04-19 14:35:52 +08:00
|
|
|
/**
|
2015-03-18 21:24:31 +08:00
|
|
|
* Set float value by key.
|
|
|
|
* @param key The key to set.
|
|
|
|
* @param value A float value to set to the key.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
*/
|
2015-04-20 03:07:48 +08:00
|
|
|
virtual void setFloatForKey(const char* key, float value);
|
2012-04-19 14:35:52 +08:00
|
|
|
/**
|
2015-03-18 21:24:31 +08:00
|
|
|
* Set double value by key.
|
|
|
|
* @param key The key to set.
|
|
|
|
* @param value A double value to set to the key.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
*/
|
2015-04-20 03:07:48 +08:00
|
|
|
virtual void setDoubleForKey(const char* key, double value);
|
2012-04-19 14:35:52 +08:00
|
|
|
/**
|
2015-03-18 21:24:31 +08:00
|
|
|
* Set string value by key.
|
|
|
|
* @param key The key to set.
|
|
|
|
* @param value A string value to set to the key.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
*/
|
2015-04-20 03:07:48 +08:00
|
|
|
virtual void setStringForKey(const char* key, const std::string & value);
|
2020-02-04 22:28:54 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/**
|
2015-03-18 21:24:31 +08:00
|
|
|
* You should invoke this function to save values set by setXXXForKey().
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
2015-04-20 03:07:48 +08:00
|
|
|
virtual void flush();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2015-07-06 17:01:17 +08:00
|
|
|
/**
|
|
|
|
* delete any value by key,
|
|
|
|
* @param key The key to delete value.
|
|
|
|
* @js NA
|
|
|
|
*/
|
|
|
|
virtual void deleteValueForKey(const char* key);
|
|
|
|
|
2015-03-18 21:24:31 +08:00
|
|
|
/** Returns the singleton.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-07-12 06:24:23 +08:00
|
|
|
static UserDefault* getInstance();
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
*/
|
2013-07-12 06:24:23 +08:00
|
|
|
static void destroyInstance();
|
|
|
|
|
2015-04-20 03:07:48 +08:00
|
|
|
/**
|
|
|
|
* You can inherit from platform dependent implementation of UserDefault, such as UserDefaultAndroid,
|
|
|
|
* and use this function to set delegate, then UserDefault will invoke delegate's implementation.
|
|
|
|
* For example, your store native data base or other format store.
|
|
|
|
*
|
|
|
|
* If you don't want to system default implementation after setting delegate, you can just pass nullptr
|
|
|
|
* to this function.
|
|
|
|
*
|
2016-07-03 23:42:10 +08:00
|
|
|
* @warning It will delete previous delegate
|
2015-04-20 03:07:48 +08:00
|
|
|
*/
|
|
|
|
static void setDelegate(UserDefault *delegate);
|
|
|
|
|
2020-08-27 12:37:20 +08:00
|
|
|
/*
|
2020-02-06 23:30:33 +08:00
|
|
|
** @params:
|
|
|
|
** key: 16bytes key
|
|
|
|
** iv: 16bytes iv
|
|
|
|
*/
|
2020-08-27 12:37:20 +08:00
|
|
|
virtual void setEncryptEnabled(bool enabled, const cxx17::string_view& key, const cxx17::string_view& iv);
|
2020-02-06 23:30:33 +08:00
|
|
|
|
2020-08-27 12:37:20 +08:00
|
|
|
/*
|
|
|
|
* Mark encrypt function as virtual, default use AES cfb128 encrypt/decrypt
|
|
|
|
* you can write your own delegate to replace encrypt/decrypt algorithm
|
|
|
|
*/
|
|
|
|
virtual void encrypt(char* inout, size_t size, int enc);
|
2020-02-06 23:30:33 +08:00
|
|
|
|
2020-08-27 12:44:30 +08:00
|
|
|
void encrypt(std::string& inout, int enc);
|
|
|
|
|
2015-04-20 03:07:48 +08:00
|
|
|
protected:
|
2013-06-20 14:13:12 +08:00
|
|
|
UserDefault();
|
2015-05-01 16:45:47 +08:00
|
|
|
virtual ~UserDefault();
|
2013-12-20 14:06:24 +08:00
|
|
|
|
2020-02-04 22:28:54 +08:00
|
|
|
void init();
|
2020-02-06 01:08:51 +08:00
|
|
|
|
|
|
|
void closeFileMapping();
|
2020-02-06 23:30:33 +08:00
|
|
|
|
|
|
|
void setValueForKey(const std::string& key, const std::string& value);
|
2020-08-27 12:37:20 +08:00
|
|
|
|
|
|
|
protected:
|
2020-02-04 22:28:54 +08:00
|
|
|
|
|
|
|
std::unordered_map<std::string, std::string> _values;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
static UserDefault* _userDefault;
|
2020-02-04 22:28:54 +08:00
|
|
|
std::string _filePath;
|
|
|
|
int _fd = -1; // the file handle for data persistence
|
|
|
|
std::shared_ptr<mio::mmap_sink> _rwmmap;
|
|
|
|
int _curMapSize = 4096; // init mapsize is 4K
|
2020-02-06 11:55:28 +08:00
|
|
|
int _realSize = 0; // real data size without key/value entities count field
|
2020-02-04 22:28:54 +08:00
|
|
|
bool _initialized = false;
|
2020-02-06 23:30:33 +08:00
|
|
|
|
2020-08-27 12:37:20 +08:00
|
|
|
// encrpyt args
|
|
|
|
bool _encryptEnabled;
|
|
|
|
std::string _key;
|
|
|
|
std::string _iv;
|
2012-04-19 14:35:52 +08:00
|
|
|
};
|
|
|
|
|
2012-06-20 18:09:11 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
NS_CC_END
|
2015-03-27 12:01:20 +08:00
|
|
|
// end of base group
|
|
|
|
/** @} */
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
#endif // __SUPPORT_CCUSERDEFAULT_H__
|