2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2023-12-08 00:13:39 +08:00
|
|
|
Copyright (c) 2019-present Axmol Engine contributors (see AUTHORS.md).
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
https://axmolengine.github.io/
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
#ifndef __SUPPORT_CCUSERDEFAULT_H__
|
|
|
|
#define __SUPPORT_CCUSERDEFAULT_H__
|
|
|
|
|
2023-06-11 13:08:08 +08:00
|
|
|
#include "platform/PlatformMacros.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
#include <string>
|
2020-02-12 16:58:47 +08:00
|
|
|
|
|
|
|
#include <unordered_map>
|
|
|
|
#include "mio/mio.hpp"
|
2023-06-24 21:18:27 +08:00
|
|
|
#include "yasio/string_view.hpp"
|
2023-09-02 19:56:50 +08:00
|
|
|
#include "platform/FileStream.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @addtogroup base
|
|
|
|
* @{
|
|
|
|
*/
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* UserDefault 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").
|
2021-12-25 10:04:45 +08:00
|
|
|
*
|
2019-11-23 20:27:39 +08:00
|
|
|
* It supports the following base types:
|
|
|
|
* bool, int, float, double, string
|
|
|
|
*
|
|
|
|
* @warning: On windows, linux, use XML to store data, which means there are some limitations of
|
|
|
|
* the key string, for example, `/` is not valid.
|
|
|
|
*/
|
2022-07-16 10:43:05 +08:00
|
|
|
class AX_DLL UserDefault
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
// get value methods
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
bool getBoolForKey(const char* key);
|
|
|
|
|
2019-11-23 20:27:39 +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.
|
|
|
|
* @js NA
|
|
|
|
*/
|
|
|
|
virtual bool getBoolForKey(const char* key, bool defaultValue);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +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
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
int getIntegerForKey(const char* key);
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
2020-11-15 12:26:40 +08:00
|
|
|
* Get integer value by key, if the key doesn't exist, will return passed default value.
|
2019-11-23 20:27:39 +08:00
|
|
|
* @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.
|
|
|
|
* @js NA
|
|
|
|
*/
|
|
|
|
virtual int getIntegerForKey(const char* key, int defaultValue);
|
2020-10-02 19:52:32 +08:00
|
|
|
|
|
|
|
/**
|
2020-11-15 12:26:40 +08:00
|
|
|
* Get large integer value by key, if the key doesn't exist, will return 0.
|
2020-10-02 19:52:32 +08:00
|
|
|
* 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
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
int64_t getLargeIntForKey(const char* key);
|
2020-10-02 19:52:32 +08:00
|
|
|
|
|
|
|
/**
|
2020-11-15 12:26:40 +08:00
|
|
|
* Get large integer value by key, if the key doesn't exist, will return passed default value.
|
2020-10-02 19:52:32 +08:00
|
|
|
* @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.
|
|
|
|
* @js NA
|
|
|
|
*/
|
|
|
|
virtual int64_t getLargeIntForKey(const char* key, int64_t defaultValue);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +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
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
float getFloatForKey(const char* key);
|
|
|
|
|
2019-11-23 20:27:39 +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.
|
|
|
|
* @js NA
|
|
|
|
*/
|
|
|
|
virtual float getFloatForKey(const char* key, float defaultValue);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +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
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
double getDoubleForKey(const char* key);
|
|
|
|
|
2019-11-23 20:27:39 +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.
|
|
|
|
* @js NA
|
|
|
|
*/
|
|
|
|
virtual double getDoubleForKey(const char* key, double defaultValue);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +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
|
|
|
|
*/
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string_view getStringForKey(const char* key);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +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.
|
|
|
|
* @js NA
|
|
|
|
*/
|
2021-12-31 12:12:40 +08:00
|
|
|
virtual std::string_view getStringForKey(const char* key, std::string_view defaultValue);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// set value methods
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set bool value by key.
|
|
|
|
* @param key The key to set.
|
|
|
|
* @param value A bool value to set to the key.
|
|
|
|
* @js NA
|
|
|
|
*/
|
|
|
|
virtual void setBoolForKey(const char* key, bool value);
|
|
|
|
/**
|
|
|
|
* Set integer value by key.
|
|
|
|
* @param key The key to set.
|
|
|
|
* @param value A integer value to set to the key.
|
|
|
|
* @js NA
|
|
|
|
*/
|
|
|
|
virtual void setIntegerForKey(const char* key, int value);
|
2020-10-02 19:52:32 +08:00
|
|
|
/**
|
2020-11-15 12:26:40 +08:00
|
|
|
* Set large integer value by key.
|
2020-10-02 19:52:32 +08:00
|
|
|
* @param key The key to set.
|
|
|
|
* @param value A integer value to set to the key.
|
|
|
|
* @js NA
|
|
|
|
*/
|
|
|
|
virtual void setLargeIntForKey(const char* key, int64_t value);
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Set float value by key.
|
|
|
|
* @param key The key to set.
|
|
|
|
* @param value A float value to set to the key.
|
|
|
|
* @js NA
|
|
|
|
*/
|
|
|
|
virtual void setFloatForKey(const char* key, float value);
|
|
|
|
/**
|
|
|
|
* Set double value by key.
|
|
|
|
* @param key The key to set.
|
|
|
|
* @param value A double value to set to the key.
|
|
|
|
* @js NA
|
|
|
|
*/
|
|
|
|
virtual void setDoubleForKey(const char* key, double value);
|
|
|
|
/**
|
|
|
|
* Set string value by key.
|
|
|
|
* @param key The key to set.
|
|
|
|
* @param value A string value to set to the key.
|
|
|
|
* @js NA
|
|
|
|
*/
|
2021-12-31 12:12:40 +08:00
|
|
|
virtual void setStringForKey(const char* key, std::string_view value);
|
2020-02-12 16:58:47 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
2020-11-15 12:26:40 +08:00
|
|
|
* Since we reimplement UserDefault with file mapping io,
|
2020-11-15 13:13:24 +08:00
|
|
|
* you don't needs call this function manually
|
2019-11-23 20:27:39 +08:00
|
|
|
* @js NA
|
|
|
|
*/
|
|
|
|
virtual void flush();
|
|
|
|
|
|
|
|
/**
|
2021-12-25 10:04:45 +08:00
|
|
|
* delete any value by key,
|
|
|
|
* @param key The key to delete value.
|
|
|
|
* @js NA
|
|
|
|
*/
|
2019-11-23 20:27:39 +08:00
|
|
|
virtual void deleteValueForKey(const char* key);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/** Returns the singleton.
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
|
|
|
static UserDefault* getInstance();
|
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
*/
|
|
|
|
static void destroyInstance();
|
|
|
|
|
|
|
|
/**
|
2021-12-25 10:04:45 +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.
|
|
|
|
*
|
|
|
|
* @warning It will delete previous delegate
|
|
|
|
*/
|
|
|
|
static void setDelegate(UserDefault* delegate);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2020-08-27 12:37:20 +08:00
|
|
|
/*
|
2020-02-12 16:58:47 +08:00
|
|
|
** @params:
|
|
|
|
** key: 16bytes key
|
|
|
|
** iv: 16bytes iv
|
|
|
|
*/
|
2021-04-22 22:01:47 +08:00
|
|
|
virtual void setEncryptEnabled(bool enabled, cxx17::string_view key, cxx17::string_view iv);
|
2020-02-12 16:58:47 +08:00
|
|
|
|
2020-08-27 12:37:20 +08:00
|
|
|
/*
|
2021-12-25 10:04:45 +08:00
|
|
|
* Mark encrypt function as virtual, default use AES cfb128 encrypt/decrypt
|
|
|
|
* you can write your own delegate to replace encrypt/decrypt algorithm
|
|
|
|
*/
|
2020-08-27 12:37:20 +08:00
|
|
|
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);
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
protected:
|
|
|
|
UserDefault();
|
|
|
|
virtual ~UserDefault();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-11-15 11:44:06 +08:00
|
|
|
void lazyInit();
|
2020-02-12 16:58:47 +08:00
|
|
|
|
|
|
|
void closeFileMapping();
|
|
|
|
|
2020-11-15 11:44:06 +08:00
|
|
|
// The low level API of all getXXXForKey
|
2021-12-31 12:12:40 +08:00
|
|
|
const std::string* getValueForKey(std::string_view key);
|
2020-11-15 11:44:06 +08:00
|
|
|
|
|
|
|
// The low level API of all setXXXForKey
|
2021-12-31 12:12:40 +08:00
|
|
|
void setValueForKey(std::string_view key, std::string_view value);
|
2020-08-27 12:37:20 +08:00
|
|
|
|
2020-11-15 12:16:54 +08:00
|
|
|
// Update value without lazyInit
|
2021-12-31 12:12:40 +08:00
|
|
|
void updateValueForKey(std::string_view key, std::string_view value);
|
2020-11-15 11:44:06 +08:00
|
|
|
|
2020-08-27 12:37:20 +08:00
|
|
|
protected:
|
2021-12-31 12:12:40 +08:00
|
|
|
hlookup::string_map<std::string> _values;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
static UserDefault* _userDefault;
|
2020-02-12 16:58:47 +08:00
|
|
|
std::string _filePath;
|
2023-09-02 19:56:50 +08:00
|
|
|
FileStream _fileStream; // the file handle for data persistence
|
2020-02-12 16:58:47 +08:00
|
|
|
std::shared_ptr<mio::mmap_sink> _rwmmap;
|
2021-12-25 10:04:45 +08:00
|
|
|
int _curMapSize = 4096; // init mapsize is 4K
|
|
|
|
int _realSize = 0; // real data size without key/value entities count field
|
2020-02-12 16:58:47 +08:00
|
|
|
bool _initialized = false;
|
|
|
|
|
2020-08-27 12:37:20 +08:00
|
|
|
// encrpyt args
|
2020-09-22 17:34:52 +08:00
|
|
|
bool _encryptEnabled = false;
|
2020-08-27 12:37:20 +08:00
|
|
|
std::string _key;
|
|
|
|
std::string _iv;
|
2019-11-23 20:27:39 +08:00
|
|
|
};
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|
2019-11-23 20:27:39 +08:00
|
|
|
// end of base group
|
|
|
|
/** @} */
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
#endif // __SUPPORT_CCUSERDEFAULT_H__
|