2013-03-05 14:53:37 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:25:07 +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.
|
2015-07-06 17:01:17 +08:00
|
|
|
|
2013-03-05 14:53:37 +08:00
|
|
|
http://www.cocos2d-x.org
|
2015-07-06 17:01:17 +08:00
|
|
|
|
2013-03-05 14:53:37 +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:
|
2015-07-06 17:01:17 +08:00
|
|
|
|
2013-03-05 14:53:37 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2015-07-06 17:01:17 +08:00
|
|
|
|
2013-03-05 14:53:37 +08:00
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-09-10 08:17:07 +08:00
|
|
|
#include "platform/CCPlatformConfig.h"
|
2014-09-10 03:18:31 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
|
|
|
|
|
|
|
|
#import <Foundation/Foundation.h>
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
2016-03-21 20:12:58 +08:00
|
|
|
#import "base/CCUserDefault.h"
|
2014-09-12 08:36:49 +08:00
|
|
|
#import "platform/CCPlatformConfig.h"
|
|
|
|
#import "platform/CCPlatformMacros.h"
|
2016-03-21 20:12:58 +08:00
|
|
|
#import "base/base64.h"
|
2014-09-10 03:18:31 +08:00
|
|
|
#import "platform/CCFileUtils.h"
|
2013-04-08 21:35:43 +08:00
|
|
|
|
2013-03-05 14:53:37 +08:00
|
|
|
using namespace std;
|
|
|
|
|
2013-04-08 21:35:43 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-03-05 14:53:37 +08:00
|
|
|
/**
|
2013-06-20 14:13:12 +08:00
|
|
|
* implements of UserDefault
|
2013-03-05 14:53:37 +08:00
|
|
|
*/
|
|
|
|
|
2013-12-20 14:06:24 +08:00
|
|
|
UserDefault* UserDefault::_userDefault = nullptr;
|
2013-06-20 14:13:12 +08:00
|
|
|
string UserDefault::_filePath = string("");
|
2013-07-12 06:24:23 +08:00
|
|
|
bool UserDefault::_isFilePathInitialized = false;
|
2013-03-05 14:53:37 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
UserDefault::~UserDefault()
|
2013-03-05 14:53:37 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
UserDefault::UserDefault()
|
2013-03-05 14:53:37 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool UserDefault::getBoolForKey(const char* pKey)
|
2013-03-05 14:53:37 +08:00
|
|
|
{
|
|
|
|
return getBoolForKey(pKey, false);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool UserDefault::getBoolForKey(const char* pKey, bool defaultValue)
|
2013-03-05 14:53:37 +08:00
|
|
|
{
|
2013-03-11 14:45:07 +08:00
|
|
|
bool ret = defaultValue;
|
2015-07-06 17:01:17 +08:00
|
|
|
|
2013-03-11 14:45:07 +08:00
|
|
|
NSNumber *value = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithUTF8String:pKey]];
|
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
ret = [value boolValue];
|
|
|
|
}
|
2015-07-06 17:01:17 +08:00
|
|
|
|
2013-03-11 14:45:07 +08:00
|
|
|
return ret;
|
2013-03-05 14:53:37 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
int UserDefault::getIntegerForKey(const char* pKey)
|
2013-03-05 14:53:37 +08:00
|
|
|
{
|
|
|
|
return getIntegerForKey(pKey, 0);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
int UserDefault::getIntegerForKey(const char* pKey, int defaultValue)
|
2013-03-05 14:53:37 +08:00
|
|
|
{
|
2013-03-11 14:45:07 +08:00
|
|
|
int ret = defaultValue;
|
2015-07-06 17:01:17 +08:00
|
|
|
|
2013-03-11 14:45:07 +08:00
|
|
|
NSNumber *value = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithUTF8String:pKey]];
|
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
ret = [value intValue];
|
|
|
|
}
|
2015-07-06 17:01:17 +08:00
|
|
|
|
2013-03-11 14:45:07 +08:00
|
|
|
return ret;
|
2013-03-05 14:53:37 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
float UserDefault::getFloatForKey(const char* pKey)
|
2013-03-05 14:53:37 +08:00
|
|
|
{
|
|
|
|
return getFloatForKey(pKey, 0);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
float UserDefault::getFloatForKey(const char* pKey, float defaultValue)
|
2013-03-05 14:53:37 +08:00
|
|
|
{
|
2013-03-11 14:45:07 +08:00
|
|
|
float ret = defaultValue;
|
2015-07-06 17:01:17 +08:00
|
|
|
|
2013-03-11 14:45:07 +08:00
|
|
|
NSNumber *value = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithUTF8String:pKey]];
|
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
ret = [value floatValue];
|
|
|
|
}
|
2015-07-06 17:01:17 +08:00
|
|
|
|
2013-03-11 14:45:07 +08:00
|
|
|
return ret;
|
2013-03-05 14:53:37 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
double UserDefault::getDoubleForKey(const char* pKey)
|
2013-03-05 14:53:37 +08:00
|
|
|
{
|
2013-03-11 14:45:07 +08:00
|
|
|
return getDoubleForKey(pKey, 0);
|
2013-03-05 14:53:37 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
double UserDefault::getDoubleForKey(const char* pKey, double defaultValue)
|
2013-03-05 14:53:37 +08:00
|
|
|
{
|
2013-03-11 14:45:07 +08:00
|
|
|
double ret = defaultValue;
|
2015-07-06 17:01:17 +08:00
|
|
|
|
2013-03-11 14:45:07 +08:00
|
|
|
NSNumber *value = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithUTF8String:pKey]];
|
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
ret = [value doubleValue];
|
|
|
|
}
|
2015-07-06 17:01:17 +08:00
|
|
|
|
2013-03-11 14:45:07 +08:00
|
|
|
return ret;
|
2013-03-05 14:53:37 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
std::string UserDefault::getStringForKey(const char* pKey)
|
2013-03-05 14:53:37 +08:00
|
|
|
{
|
|
|
|
return getStringForKey(pKey, "");
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
string UserDefault::getStringForKey(const char* pKey, const std::string & defaultValue)
|
2013-03-05 14:53:37 +08:00
|
|
|
{
|
|
|
|
NSString *str = [[NSUserDefaults standardUserDefaults] stringForKey:[NSString stringWithUTF8String:pKey]];
|
|
|
|
if (! str)
|
|
|
|
{
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return [str UTF8String];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-18 14:58:17 +08:00
|
|
|
Data UserDefault::getDataForKey(const char* pKey)
|
2013-05-29 08:06:41 +08:00
|
|
|
{
|
2013-12-20 21:16:46 +08:00
|
|
|
return getDataForKey(pKey, Data::Null);
|
2013-05-29 08:06:41 +08:00
|
|
|
}
|
|
|
|
|
2013-12-18 14:58:17 +08:00
|
|
|
Data UserDefault::getDataForKey(const char* pKey, const Data& defaultValue)
|
2013-05-29 08:06:41 +08:00
|
|
|
{
|
|
|
|
NSData *data = [[NSUserDefaults standardUserDefaults] dataForKey:[NSString stringWithUTF8String:pKey]];
|
|
|
|
if (! data)
|
|
|
|
{
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-18 14:58:17 +08:00
|
|
|
Data ret;
|
|
|
|
ret.copy((unsigned char*)data.bytes, data.length);
|
2013-05-29 08:06:41 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void UserDefault::setBoolForKey(const char* pKey, bool value)
|
2013-03-05 14:53:37 +08:00
|
|
|
{
|
2013-03-11 14:45:07 +08:00
|
|
|
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:value] forKey:[NSString stringWithUTF8String:pKey]];
|
2013-03-05 14:53:37 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void UserDefault::setIntegerForKey(const char* pKey, int value)
|
2013-03-05 14:53:37 +08:00
|
|
|
{
|
2013-03-11 14:45:07 +08:00
|
|
|
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:value] forKey:[NSString stringWithUTF8String:pKey]];
|
2013-03-05 14:53:37 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void UserDefault::setFloatForKey(const char* pKey, float value)
|
2013-03-05 14:53:37 +08:00
|
|
|
{
|
2013-03-11 14:45:07 +08:00
|
|
|
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithFloat:value] forKey:[NSString stringWithUTF8String:pKey]];
|
2013-03-05 14:53:37 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void UserDefault::setDoubleForKey(const char* pKey, double value)
|
2013-03-05 14:53:37 +08:00
|
|
|
{
|
2013-03-11 14:45:07 +08:00
|
|
|
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithDouble:value] forKey:[NSString stringWithUTF8String:pKey]];
|
2013-03-05 14:53:37 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void UserDefault::setStringForKey(const char* pKey, const std::string & value)
|
2013-03-05 14:53:37 +08:00
|
|
|
{
|
|
|
|
[[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithUTF8String:value.c_str()] forKey:[NSString stringWithUTF8String:pKey]];
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void UserDefault::setDataForKey(const char* pKey, const Data& value) {
|
2013-05-29 08:06:41 +08:00
|
|
|
[[NSUserDefaults standardUserDefaults] setObject:[NSData dataWithBytes: value.getBytes() length: value.getSize()] forKey:[NSString stringWithUTF8String:pKey]];
|
|
|
|
}
|
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
UserDefault* UserDefault::getInstance()
|
2015-07-06 17:01:17 +08:00
|
|
|
{
|
2013-07-12 06:24:23 +08:00
|
|
|
if (! _userDefault)
|
2013-03-05 14:53:37 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
_userDefault = new (std::nothrow) UserDefault();
|
2013-03-05 14:53:37 +08:00
|
|
|
}
|
2015-07-06 17:01:17 +08:00
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
return _userDefault;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UserDefault::destroyInstance()
|
|
|
|
{
|
2013-12-20 14:06:24 +08:00
|
|
|
CC_SAFE_DELETE(_userDefault);
|
2013-07-12 06:24:23 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool UserDefault::isXMLFileExist()
|
2013-03-05 14:53:37 +08:00
|
|
|
{
|
2015-04-20 03:07:48 +08:00
|
|
|
return FileUtils::getInstance()->isFileExist(_filePath);
|
2013-03-05 14:53:37 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void UserDefault::initXMLFilePath()
|
2013-03-05 14:53:37 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// create new xml file
|
2013-06-20 14:13:12 +08:00
|
|
|
bool UserDefault::createXMLFile()
|
2013-03-05 14:53:37 +08:00
|
|
|
{
|
2013-04-08 21:35:43 +08:00
|
|
|
return false;
|
2013-03-05 14:53:37 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
const string& UserDefault::getXMLFilePath()
|
2013-03-05 14:53:37 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _filePath;
|
2013-03-05 14:53:37 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void UserDefault::flush()
|
2013-03-05 14:53:37 +08:00
|
|
|
{
|
2013-04-08 21:35:43 +08:00
|
|
|
[[NSUserDefaults standardUserDefaults] synchronize];
|
2013-03-05 14:53:37 +08:00
|
|
|
}
|
|
|
|
|
2015-07-06 17:01:17 +08:00
|
|
|
void UserDefault::deleteValueForKey(const char* key)
|
|
|
|
{
|
|
|
|
// check the params
|
|
|
|
if (!key)
|
|
|
|
{
|
|
|
|
CCLOG("the key is invalid");
|
|
|
|
}
|
|
|
|
|
|
|
|
[[NSUserDefaults standardUserDefaults] removeObjectForKey:[NSString stringWithUTF8String:key]];
|
|
|
|
|
|
|
|
flush();
|
|
|
|
}
|
2013-03-05 14:53:37 +08:00
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|
|
|
|
#endif // (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
|