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.
|
2022-04-25 19:15:46 +08:00
|
|
|
Copyright (c) 2017-2020 C4games Ltd.
|
2022-04-27 18:48:57 +08:00
|
|
|
Copyright (c) 2021-2022 Bytedance Inc.
|
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.
|
|
|
|
****************************************************************************/
|
2020-02-12 16:58:47 +08:00
|
|
|
#if defined(_WIN32)
|
2021-12-25 10:04:45 +08:00
|
|
|
# include <io.h>
|
|
|
|
# include <direct.h>
|
2020-02-12 16:58:47 +08:00
|
|
|
#else
|
2021-12-25 10:04:45 +08:00
|
|
|
# include <unistd.h>
|
|
|
|
# include <errno.h>
|
2020-02-12 16:58:47 +08:00
|
|
|
#endif
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2020-10-02 19:52:32 +08:00
|
|
|
#include <inttypes.h>
|
2021-04-22 22:01:32 +08:00
|
|
|
#include <sstream>
|
2020-10-02 19:52:32 +08:00
|
|
|
|
2020-02-12 16:58:47 +08:00
|
|
|
#include "openssl/aes.h"
|
|
|
|
#include "openssl/modes.h"
|
2020-08-27 12:37:20 +08:00
|
|
|
#include "openssl/rc4.h"
|
2020-02-12 16:58:47 +08:00
|
|
|
|
2023-06-24 09:17:14 +08:00
|
|
|
#include "yasio/ibstream.hpp"
|
|
|
|
#include "yasio/obstream.hpp"
|
|
|
|
#include "yasio/sz.hpp"
|
2022-04-27 18:48:57 +08:00
|
|
|
|
2023-06-11 13:08:08 +08:00
|
|
|
#include "base/UserDefault.h"
|
|
|
|
#include "platform/Common.h"
|
|
|
|
#include "platform/FileUtils.h"
|
2020-10-21 12:28:34 +08:00
|
|
|
#include "pugixml/pugixml.hpp"
|
2023-06-11 13:08:08 +08:00
|
|
|
#include "base/Utils.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-04-22 23:00:00 +08:00
|
|
|
#define USER_DEFAULT_PLAIN_MODE 0
|
2020-02-12 16:58:47 +08:00
|
|
|
|
|
|
|
#if !USER_DEFAULT_PLAIN_MODE
|
2021-12-25 10:04:45 +08:00
|
|
|
# define USER_DEFAULT_FILENAME "UserDefault.bin"
|
2020-02-12 16:58:47 +08:00
|
|
|
#else
|
2021-12-25 10:04:45 +08:00
|
|
|
# define USER_DEFAULT_FILENAME "UserDefault.xml"
|
2020-02-12 16:58:47 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef int32_t udflen_t;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2020-02-04 22:56:49 +08:00
|
|
|
/**
|
2020-02-12 16:58:47 +08:00
|
|
|
* implements of UserDefault
|
2020-02-04 22:56:49 +08:00
|
|
|
*/
|
|
|
|
|
2020-02-12 16:58:47 +08:00
|
|
|
UserDefault* UserDefault::_userDefault = nullptr;
|
2020-02-11 23:54:17 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
static void ud_setkey(std::string& lhs, const cxx17::string_view& rhs)
|
|
|
|
{
|
2020-02-12 16:58:47 +08:00
|
|
|
static const size_t keyLen = 16;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!rhs.empty())
|
|
|
|
{
|
2020-02-12 16:58:47 +08:00
|
|
|
lhs.assign(rhs.data(), std::min(rhs.length(), keyLen));
|
|
|
|
if (lhs.size() < keyLen)
|
2021-12-25 10:04:45 +08:00
|
|
|
lhs.insert(lhs.end(), keyLen - lhs.size(), '\0'); // fill 0, if key insufficient
|
2020-02-12 16:58:47 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
lhs.assign(keyLen, '\0');
|
|
|
|
}
|
2020-02-11 23:54:17 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
static void ud_write_v_s(UserDefault* ud, yasio::obstream& obs, const cxx17::string_view value)
|
2020-02-12 16:58:47 +08:00
|
|
|
{
|
2020-11-15 11:44:06 +08:00
|
|
|
size_t value_offset = obs.length();
|
|
|
|
obs.write_v(value);
|
|
|
|
value_offset += (obs.length() - value_offset - value.length());
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!value.empty())
|
2021-04-22 19:48:49 +08:00
|
|
|
ud->encrypt(obs.data() + value_offset, value.length(), AES_ENCRYPT);
|
2020-02-06 23:30:33 +08:00
|
|
|
}
|
|
|
|
|
2021-04-22 19:48:49 +08:00
|
|
|
void UserDefault::setEncryptEnabled(bool enabled, cxx17::string_view key, cxx17::string_view iv)
|
2020-02-06 23:30:33 +08:00
|
|
|
{
|
2020-02-12 16:58:47 +08:00
|
|
|
_encryptEnabled = enabled;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_encryptEnabled)
|
|
|
|
{
|
2020-02-12 16:58:47 +08:00
|
|
|
ud_setkey(_key, key);
|
|
|
|
ud_setkey(_iv, iv);
|
2020-02-11 23:54:17 +08:00
|
|
|
}
|
2020-02-12 16:58:47 +08:00
|
|
|
}
|
2020-11-15 13:11:53 +08:00
|
|
|
|
2020-02-12 16:58:47 +08:00
|
|
|
void UserDefault::encrypt(std::string& inout, int enc)
|
|
|
|
{
|
|
|
|
if (!inout.empty())
|
|
|
|
encrypt(&inout.front(), inout.size(), enc);
|
2020-02-04 22:56:49 +08:00
|
|
|
}
|
|
|
|
|
2020-02-12 16:58:47 +08:00
|
|
|
void UserDefault::encrypt(char* inout, size_t size, int enc)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (size > 0)
|
|
|
|
{
|
2020-02-12 16:58:47 +08:00
|
|
|
AES_KEY aeskey;
|
|
|
|
AES_set_encrypt_key((const unsigned char*)_key.c_str(), 128, &aeskey);
|
2020-02-11 23:54:17 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
unsigned char iv[16] = {0};
|
2020-02-12 16:58:47 +08:00
|
|
|
memcpy(iv, _iv.c_str(), std::min(sizeof(iv), _iv.size()));
|
|
|
|
|
|
|
|
int ignored_num = 0;
|
|
|
|
AES_cfb128_encrypt((unsigned char*)inout, (unsigned char*)inout, size, &aeskey, iv, &ignored_num, enc);
|
|
|
|
}
|
|
|
|
}
|
2020-02-11 23:54:17 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
UserDefault::~UserDefault()
|
|
|
|
{
|
2020-02-12 16:58:47 +08:00
|
|
|
closeFileMapping();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
UserDefault::UserDefault() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2020-02-12 16:58:47 +08:00
|
|
|
void UserDefault::closeFileMapping()
|
|
|
|
{
|
|
|
|
_rwmmap.reset();
|
2021-04-22 22:01:32 +08:00
|
|
|
#if !USER_DEFAULT_PLAIN_MODE
|
2023-08-31 17:56:09 +08:00
|
|
|
if (_fileStream.isOpen())
|
|
|
|
_fileStream.close();
|
2021-04-22 22:01:32 +08:00
|
|
|
#endif
|
2020-02-12 16:58:47 +08:00
|
|
|
}
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
bool UserDefault::getBoolForKey(const char* pKey)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
return getBoolForKey(pKey, false);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool UserDefault::getBoolForKey(const char* pKey, bool defaultValue)
|
|
|
|
{
|
2020-11-15 11:44:06 +08:00
|
|
|
auto pValue = getValueForKey(pKey);
|
|
|
|
if (pValue)
|
|
|
|
return *pValue == "true";
|
2020-02-04 22:56:49 +08:00
|
|
|
|
2020-02-12 16:58:47 +08:00
|
|
|
return defaultValue;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int UserDefault::getIntegerForKey(const char* pKey)
|
|
|
|
{
|
|
|
|
return getIntegerForKey(pKey, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int UserDefault::getIntegerForKey(const char* pKey, int defaultValue)
|
|
|
|
{
|
2020-11-15 11:44:06 +08:00
|
|
|
auto pValue = getValueForKey(pKey);
|
|
|
|
if (pValue)
|
|
|
|
return atoi(pValue->c_str());
|
2020-02-04 22:56:49 +08:00
|
|
|
|
2020-02-12 16:58:47 +08:00
|
|
|
return defaultValue;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2020-10-02 19:52:32 +08:00
|
|
|
int64_t UserDefault::getLargeIntForKey(const char* key)
|
|
|
|
{
|
|
|
|
return getLargeIntForKey(key, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t UserDefault::getLargeIntForKey(const char* key, int64_t defaultValue)
|
|
|
|
{
|
2020-11-15 11:44:06 +08:00
|
|
|
auto pValue = getValueForKey(key);
|
|
|
|
if (pValue)
|
|
|
|
return std::stoll(pValue->c_str());
|
2020-10-02 19:52:32 +08:00
|
|
|
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
float UserDefault::getFloatForKey(const char* pKey)
|
|
|
|
{
|
|
|
|
return getFloatForKey(pKey, 0.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
float UserDefault::getFloatForKey(const char* pKey, float defaultValue)
|
|
|
|
{
|
2020-11-15 11:44:06 +08:00
|
|
|
return static_cast<float>(getDoubleForKey(pKey, defaultValue));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
double UserDefault::getDoubleForKey(const char* pKey)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return getDoubleForKey(pKey, 0.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
double UserDefault::getDoubleForKey(const char* pKey, double defaultValue)
|
|
|
|
{
|
2020-11-15 11:44:06 +08:00
|
|
|
auto pValue = getValueForKey(pKey);
|
|
|
|
if (pValue)
|
|
|
|
return utils::atof(pValue->c_str());
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2020-02-12 16:58:47 +08:00
|
|
|
return defaultValue;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string_view UserDefault::getStringForKey(const char* pKey)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return getStringForKey(pKey, "");
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string_view UserDefault::getStringForKey(const char* pKey, std::string_view defaultValue)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2020-11-15 11:44:06 +08:00
|
|
|
auto pValue = getValueForKey(pKey);
|
|
|
|
if (pValue)
|
|
|
|
return *pValue;
|
2020-02-04 22:56:49 +08:00
|
|
|
|
2020-02-12 16:58:47 +08:00
|
|
|
return defaultValue;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
const std::string* UserDefault::getValueForKey(std::string_view key)
|
2020-11-15 11:44:06 +08:00
|
|
|
{
|
|
|
|
// do lazyInit at here to make sure _encryptEnabled works well,
|
|
|
|
lazyInit();
|
|
|
|
|
|
|
|
auto it = this->_values.find(key);
|
|
|
|
if (it != this->_values.end())
|
|
|
|
return &it->second;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
void UserDefault::setBoolForKey(const char* pKey, bool value)
|
|
|
|
{
|
|
|
|
// save bool value as string
|
2020-02-12 16:58:47 +08:00
|
|
|
setStringForKey(pKey, value ? "true" : "false");
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UserDefault::setIntegerForKey(const char* pKey, int value)
|
|
|
|
{
|
|
|
|
// check key
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!pKey)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// format the value
|
|
|
|
char tmp[50];
|
2020-10-02 19:52:32 +08:00
|
|
|
memset(tmp, 0, sizeof(tmp));
|
2022-11-14 15:02:16 +08:00
|
|
|
snprintf(tmp, sizeof(tmp), "%d", value);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2020-02-12 16:58:47 +08:00
|
|
|
setStringForKey(pKey, tmp);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2020-10-02 19:52:32 +08:00
|
|
|
void UserDefault::setLargeIntForKey(const char* pKey, int64_t value)
|
2021-12-25 10:04:45 +08:00
|
|
|
{ // check key
|
2020-10-02 19:52:32 +08:00
|
|
|
if (!pKey)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// format the value
|
|
|
|
char tmp[96];
|
|
|
|
memset(tmp, 0, sizeof(tmp));
|
2022-11-14 15:02:16 +08:00
|
|
|
snprintf(tmp, sizeof(tmp), "%" PRId64, value);
|
2020-10-02 19:52:32 +08:00
|
|
|
|
|
|
|
setStringForKey(pKey, tmp);
|
|
|
|
}
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
void UserDefault::setFloatForKey(const char* pKey, float value)
|
|
|
|
{
|
|
|
|
setDoubleForKey(pKey, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UserDefault::setDoubleForKey(const char* pKey, double value)
|
|
|
|
{
|
|
|
|
// check key
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!pKey)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// format the value
|
|
|
|
char tmp[50];
|
|
|
|
memset(tmp, 0, 50);
|
2022-11-14 15:02:16 +08:00
|
|
|
snprintf(tmp, sizeof(tmp), "%f", value);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2020-02-12 16:58:47 +08:00
|
|
|
setStringForKey(pKey, tmp);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void UserDefault::setStringForKey(const char* pKey, std::string_view value)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2020-02-12 16:58:47 +08:00
|
|
|
// ignore empty key
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!pKey || !*pKey)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-12 16:58:47 +08:00
|
|
|
setValueForKey(pKey, value);
|
2020-02-06 21:54:45 +08:00
|
|
|
|
2020-02-12 16:58:47 +08:00
|
|
|
#if !USER_DEFAULT_PLAIN_MODE
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_rwmmap)
|
|
|
|
{
|
2020-02-12 16:58:47 +08:00
|
|
|
yasio::obstream obs;
|
|
|
|
if (_encryptEnabled)
|
|
|
|
{
|
2020-11-15 13:08:21 +08:00
|
|
|
ud_write_v_s(this, obs, pKey);
|
|
|
|
ud_write_v_s(this, obs, value);
|
2020-02-12 16:58:47 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else
|
|
|
|
{
|
2020-02-12 16:58:47 +08:00
|
|
|
obs.write_v(pKey);
|
|
|
|
obs.write_v(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((_realSize + obs.length() + sizeof(udflen_t)) < _curMapSize)
|
2021-12-25 10:04:45 +08:00
|
|
|
{ // fast append entity without flush
|
2020-02-12 16:58:47 +08:00
|
|
|
// increase entities count
|
2020-11-25 19:05:31 +08:00
|
|
|
yasio::obstream::swrite(_rwmmap->data(), 1 + yasio::ibstream::sread<udflen_t>(_rwmmap->data()));
|
2020-02-12 16:58:47 +08:00
|
|
|
|
|
|
|
// append entity
|
|
|
|
::memcpy(_rwmmap->data() + sizeof(udflen_t) + _realSize, obs.data(), obs.length());
|
2021-10-09 13:48:56 +08:00
|
|
|
_realSize += static_cast<int>(obs.length());
|
2020-02-12 16:58:47 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else
|
|
|
|
{
|
2020-02-12 16:58:47 +08:00
|
|
|
flush();
|
|
|
|
}
|
2020-02-06 23:28:33 +08:00
|
|
|
}
|
2020-02-12 16:58:47 +08:00
|
|
|
#else
|
|
|
|
flush();
|
|
|
|
#endif
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void UserDefault::setValueForKey(std::string_view key, std::string_view value)
|
2020-11-15 11:44:06 +08:00
|
|
|
{
|
|
|
|
// do lazyInit at here to make sure _encryptEnabled works well
|
|
|
|
lazyInit();
|
|
|
|
|
|
|
|
updateValueForKey(key, value);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void UserDefault::updateValueForKey(std::string_view key, std::string_view value)
|
2020-02-12 16:58:47 +08:00
|
|
|
{
|
|
|
|
auto it = _values.find(key);
|
|
|
|
if (it != _values.end())
|
|
|
|
it->second = value;
|
|
|
|
else
|
|
|
|
_values.emplace(key, value);
|
2020-02-06 23:30:33 +08:00
|
|
|
}
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
UserDefault* UserDefault::getInstance()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_userDefault)
|
|
|
|
return _userDefault;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
return (_userDefault = new UserDefault());
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UserDefault::destroyInstance()
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_DELETE(_userDefault);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void UserDefault::setDelegate(UserDefault* delegate)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (_userDefault)
|
|
|
|
delete _userDefault;
|
|
|
|
|
|
|
|
_userDefault = delegate;
|
|
|
|
}
|
|
|
|
|
2020-11-15 11:44:06 +08:00
|
|
|
void UserDefault::lazyInit()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_initialized)
|
|
|
|
return;
|
2020-11-15 11:44:06 +08:00
|
|
|
|
2020-02-12 16:58:47 +08:00
|
|
|
#if !USER_DEFAULT_PLAIN_MODE
|
2021-04-22 23:00:00 +08:00
|
|
|
_filePath = FileUtils::getInstance()->getNativeWritableAbsolutePath() + USER_DEFAULT_FILENAME;
|
|
|
|
|
2020-11-15 11:44:06 +08:00
|
|
|
// construct file mapping
|
2023-08-31 17:56:09 +08:00
|
|
|
if (!_fileStream.open(_filePath, IFileStream::Mode::OVERLAPPED))
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2023-07-01 08:51:29 +08:00
|
|
|
log("[Warning] UserDefault::init open storage file '%s' failed!", _filePath.c_str());
|
2020-11-15 11:44:06 +08:00
|
|
|
return;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2023-08-31 17:56:09 +08:00
|
|
|
int filesize = static_cast<int>(_fileStream.size());
|
2020-02-11 23:54:17 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (filesize < _curMapSize)
|
|
|
|
{ // construct a empty file mapping
|
2023-08-31 17:56:09 +08:00
|
|
|
if (!_fileStream.resize(_curMapSize))
|
2023-07-01 08:51:29 +08:00
|
|
|
{
|
|
|
|
log("[Warning] UserDefault::init failed to truncate '%s'.", _filePath.c_str());
|
|
|
|
return;
|
|
|
|
}
|
2023-08-31 17:56:09 +08:00
|
|
|
_rwmmap = std::make_shared<mio::mmap_sink>(_fileStream.nativeHandle(), 0, _curMapSize);
|
2020-11-15 11:44:06 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else
|
|
|
|
{ /// load to memory _values
|
2023-08-31 17:56:09 +08:00
|
|
|
_rwmmap = std::make_shared<mio::mmap_sink>(_fileStream.nativeHandle(), 0,
|
|
|
|
mio::map_entire_file);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_rwmmap->is_mapped())
|
|
|
|
{ // no error
|
2020-11-15 11:44:06 +08:00
|
|
|
yasio::ibstream_view ibs(_rwmmap->data(), _rwmmap->length());
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (ibs.length() > 0)
|
|
|
|
{
|
2020-11-15 11:44:06 +08:00
|
|
|
// read count of keyvals.
|
2020-11-25 19:05:31 +08:00
|
|
|
int count = ibs.read<int>();
|
2021-12-25 10:04:45 +08:00
|
|
|
for (auto i = 0; i < count; ++i)
|
|
|
|
{
|
2020-11-15 11:44:06 +08:00
|
|
|
if (_encryptEnabled)
|
|
|
|
{
|
2023-02-23 21:59:27 +08:00
|
|
|
std::string key(ibs.read_v());
|
|
|
|
std::string value(ibs.read_v());
|
2020-11-15 11:44:06 +08:00
|
|
|
this->encrypt(key, AES_DECRYPT);
|
|
|
|
this->encrypt(value, AES_DECRYPT);
|
2023-02-23 21:59:27 +08:00
|
|
|
updateValueForKey(key, value);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::string_view key(ibs.read_v());
|
|
|
|
std::string_view value(ibs.read_v());
|
|
|
|
updateValueForKey(key, value);
|
2020-02-12 16:58:47 +08:00
|
|
|
}
|
|
|
|
}
|
2020-11-16 13:34:53 +08:00
|
|
|
_realSize = static_cast<int>(ibs.seek(0, SEEK_CUR) - sizeof(udflen_t));
|
2020-02-12 16:58:47 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else
|
|
|
|
{
|
2020-11-15 11:44:06 +08:00
|
|
|
closeFileMapping();
|
|
|
|
::remove(_filePath.c_str());
|
2023-07-01 08:51:29 +08:00
|
|
|
log("[Warning] UserDefault::init map file '%s' failed, we can't save data persisit this time, next time "
|
2021-12-25 10:04:45 +08:00
|
|
|
"we will retry!",
|
|
|
|
_filePath.c_str());
|
2020-02-12 16:58:47 +08:00
|
|
|
}
|
2020-11-15 11:44:06 +08:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
pugi::xml_document doc;
|
2021-04-22 22:01:32 +08:00
|
|
|
|
2021-04-22 23:00:00 +08:00
|
|
|
_filePath = FileUtils::getInstance()->getWritablePath() + USER_DEFAULT_FILENAME;
|
|
|
|
|
2021-04-22 22:01:32 +08:00
|
|
|
if (FileUtils::getInstance()->isFileExist(_filePath))
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto data = FileUtils::getInstance()->getDataFromFile(_filePath);
|
2021-04-22 22:01:32 +08:00
|
|
|
pugi::xml_parse_result ret = doc.load_buffer_inplace(data.getBytes(), data.getSize());
|
2021-12-25 10:04:45 +08:00
|
|
|
if (ret)
|
|
|
|
{
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& elem : doc.document_element())
|
2021-04-22 22:01:32 +08:00
|
|
|
updateValueForKey(elem.name(), elem.text().as_string());
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else
|
|
|
|
{
|
2021-04-22 22:01:32 +08:00
|
|
|
log("UserDefault::init load xml file: %s failed, %s", _filePath.c_str(), ret.description());
|
|
|
|
}
|
2020-11-15 11:44:06 +08:00
|
|
|
}
|
2021-04-22 22:01:32 +08:00
|
|
|
|
2020-02-12 16:58:47 +08:00
|
|
|
#endif
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2020-11-15 11:44:06 +08:00
|
|
|
_initialized = true;
|
2020-02-11 23:54:17 +08:00
|
|
|
}
|
2020-02-06 21:54:45 +08:00
|
|
|
|
2020-02-11 23:54:17 +08:00
|
|
|
void UserDefault::flush()
|
|
|
|
{
|
2020-02-12 16:58:47 +08:00
|
|
|
#if !USER_DEFAULT_PLAIN_MODE
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_rwmmap)
|
|
|
|
{
|
2020-02-12 16:58:47 +08:00
|
|
|
yasio::obstream obs;
|
2020-11-25 19:05:31 +08:00
|
|
|
obs.write<int>(static_cast<int>(this->_values.size()));
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& item : this->_values)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2020-02-12 16:58:47 +08:00
|
|
|
if (_encryptEnabled)
|
|
|
|
{
|
2020-11-15 13:08:21 +08:00
|
|
|
ud_write_v_s(this, obs, item.first);
|
|
|
|
ud_write_v_s(this, obs, item.second);
|
2020-02-12 16:58:47 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else
|
|
|
|
{
|
2020-02-12 16:58:47 +08:00
|
|
|
obs.write_v(item.first);
|
|
|
|
obs.write_v(item.second);
|
|
|
|
}
|
|
|
|
}
|
2020-02-11 23:54:17 +08:00
|
|
|
|
2020-02-12 16:58:47 +08:00
|
|
|
std::error_code error;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (obs.length() > _curMapSize)
|
|
|
|
{
|
2020-02-12 16:58:47 +08:00
|
|
|
_rwmmap->unmap();
|
2022-08-02 21:11:33 +08:00
|
|
|
while (obs.length() > _curMapSize)
|
|
|
|
_curMapSize <<= 1; // X2
|
2023-07-01 08:51:29 +08:00
|
|
|
|
2023-08-31 17:56:09 +08:00
|
|
|
if (!_fileStream.resize(_curMapSize))
|
|
|
|
ax::log("[Warning] UserDefault::flush failed to truncate '%s'.", _filePath.c_str());
|
2023-07-01 08:51:29 +08:00
|
|
|
|
2023-08-31 17:56:09 +08:00
|
|
|
_rwmmap->map(_fileStream.nativeHandle(), 0, _curMapSize, error);
|
2020-02-12 16:58:47 +08:00
|
|
|
}
|
2020-02-11 23:54:17 +08:00
|
|
|
|
2020-02-12 16:58:47 +08:00
|
|
|
if (!error && _rwmmap->is_mapped())
|
2021-12-25 10:04:45 +08:00
|
|
|
{ // mapping status is good
|
2020-02-12 16:58:47 +08:00
|
|
|
::memcpy(_rwmmap->data(), obs.data(), obs.length());
|
2020-11-16 13:34:53 +08:00
|
|
|
_realSize = static_cast<int>(obs.length() - sizeof(udflen_t));
|
2020-02-12 16:58:47 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else
|
|
|
|
{
|
2020-02-12 16:58:47 +08:00
|
|
|
// close file mapping and do a simple workaround fix to don't do persist later at this time
|
|
|
|
closeFileMapping();
|
|
|
|
::remove(_filePath.c_str());
|
|
|
|
}
|
2020-02-11 23:54:17 +08:00
|
|
|
}
|
2020-02-12 16:58:47 +08:00
|
|
|
#else
|
|
|
|
pugi::xml_document doc;
|
|
|
|
doc.load_string(R"(<?xml version="1.0" ?>
|
|
|
|
<r />)");
|
|
|
|
auto r = doc.document_element();
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& kv : _values)
|
2021-12-25 10:04:45 +08:00
|
|
|
r.append_child(kv.first.c_str()).append_child(pugi::xml_node_type::node_pcdata).set_value(kv.second.c_str());
|
2021-04-22 22:01:32 +08:00
|
|
|
|
|
|
|
std::stringstream ss;
|
|
|
|
doc.save(ss, " ");
|
|
|
|
FileUtils::getInstance()->writeStringToFile(ss.str(), _filePath);
|
2020-02-12 16:58:47 +08:00
|
|
|
#endif
|
|
|
|
}
|
2020-02-11 23:54:17 +08:00
|
|
|
|
2020-02-12 16:58:47 +08:00
|
|
|
void UserDefault::deleteValueForKey(const char* key)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (this->_values.erase(key) > 0)
|
2020-02-12 16:58:47 +08:00
|
|
|
flush();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|