axmol/cocos/base/CCUserDefault-apple.mm

255 lines
6.5 KiB
Plaintext
Raw Normal View History

/****************************************************************************
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2015-07-06 17:01:17 +08:00
http://www.cocos2d-x.org
2015-07-06 17:01:17 +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
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
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.
****************************************************************************/
#include "platform/CCPlatformConfig.h"
Squashed commit of the following: commit b9181ae92541d11119bedd766773ddb01f0bb486 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Sep 9 09:34:40 2014 -0700 Compiles luasocket on Mac commit 91fc79aa2101867fedfbcd7d846e9508009894e6 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Sep 9 09:12:30 2014 -0700 disables luasockets commit 31f2ee462708ab7b604b9399eaabd275e2da9e02 Merge: 19099fd 0abe4cb Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Sep 9 08:10:48 2014 -0700 Merge remote-tracking branch 'cocos2d/v3' into cmakefile_mac_osx Conflicts: cocos/3d/cocos3d.h commit 19099fd021b4ecf1f2eebd73ea14e84572d1ee33 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Sep 8 23:21:03 2014 -0700 compiles on Linux commit eec7b92817ac9208123468bb882612aad89f94bf Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Sep 8 23:16:46 2014 -0700 all projecs compiles with CMake commit 40fcc8325b409a580fdbf26d2ee27aa2d1db0109 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Sep 8 21:47:27 2014 -0700 CCUserDefualt.mm better includes commit 30709ec8243562858498cb118240bb45bf9b33b0 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Sep 8 21:26:36 2014 -0700 cpp-tests compiles and works on Mac using CMake commit 67f0fc8df887d08d3d7a27c5fdadd335d6841676 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Sep 8 20:12:09 2014 -0700 Adds missing files to CMakeLists.txt commit 36f939ea7f157ca851aee138300c13d9888c0078 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Sep 8 19:52:34 2014 -0700 Adds more box2d missing files commit 00f81e3b5ec97150974cc6022e86c394ff488ccb Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Sep 8 19:39:54 2014 -0700 Adds missing box2d files commit 9673227b4d7d0e6d9eced49bdd69b98cb519968a Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Sep 8 18:44:15 2014 -0700 Initial CMake support for Mac
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"
Squashed commit of the following: commit b9181ae92541d11119bedd766773ddb01f0bb486 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Sep 9 09:34:40 2014 -0700 Compiles luasocket on Mac commit 91fc79aa2101867fedfbcd7d846e9508009894e6 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Sep 9 09:12:30 2014 -0700 disables luasockets commit 31f2ee462708ab7b604b9399eaabd275e2da9e02 Merge: 19099fd 0abe4cb Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Sep 9 08:10:48 2014 -0700 Merge remote-tracking branch 'cocos2d/v3' into cmakefile_mac_osx Conflicts: cocos/3d/cocos3d.h commit 19099fd021b4ecf1f2eebd73ea14e84572d1ee33 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Sep 8 23:21:03 2014 -0700 compiles on Linux commit eec7b92817ac9208123468bb882612aad89f94bf Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Sep 8 23:16:46 2014 -0700 all projecs compiles with CMake commit 40fcc8325b409a580fdbf26d2ee27aa2d1db0109 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Sep 8 21:47:27 2014 -0700 CCUserDefualt.mm better includes commit 30709ec8243562858498cb118240bb45bf9b33b0 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Sep 8 21:26:36 2014 -0700 cpp-tests compiles and works on Mac using CMake commit 67f0fc8df887d08d3d7a27c5fdadd335d6841676 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Sep 8 20:12:09 2014 -0700 Adds missing files to CMakeLists.txt commit 36f939ea7f157ca851aee138300c13d9888c0078 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Sep 8 19:52:34 2014 -0700 Adds more box2d missing files commit 00f81e3b5ec97150974cc6022e86c394ff488ccb Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Sep 8 19:39:54 2014 -0700 Adds missing box2d files commit 9673227b4d7d0e6d9eced49bdd69b98cb519968a Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Sep 8 18:44:15 2014 -0700 Initial CMake support for Mac
2014-09-10 03:18:31 +08:00
#import "platform/CCFileUtils.h"
using namespace std;
NS_CC_BEGIN
/**
* implements of UserDefault
*/
UserDefault* UserDefault::_userDefault = nullptr;
string UserDefault::_filePath = string("");
bool UserDefault::_isFilePathInitialized = false;
UserDefault::~UserDefault()
{
}
UserDefault::UserDefault()
{
}
bool UserDefault::getBoolForKey(const char* pKey)
{
return getBoolForKey(pKey, false);
}
bool UserDefault::getBoolForKey(const char* pKey, bool defaultValue)
{
bool ret = defaultValue;
2015-07-06 17:01:17 +08:00
NSNumber *value = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithUTF8String:pKey]];
if (value)
{
ret = [value boolValue];
}
2015-07-06 17:01:17 +08:00
return ret;
}
int UserDefault::getIntegerForKey(const char* pKey)
{
return getIntegerForKey(pKey, 0);
}
int UserDefault::getIntegerForKey(const char* pKey, int defaultValue)
{
int ret = defaultValue;
2015-07-06 17:01:17 +08:00
NSNumber *value = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithUTF8String:pKey]];
if (value)
{
ret = [value intValue];
}
2015-07-06 17:01:17 +08:00
return ret;
}
float UserDefault::getFloatForKey(const char* pKey)
{
return getFloatForKey(pKey, 0);
}
float UserDefault::getFloatForKey(const char* pKey, float defaultValue)
{
float ret = defaultValue;
2015-07-06 17:01:17 +08:00
NSNumber *value = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithUTF8String:pKey]];
if (value)
{
ret = [value floatValue];
}
2015-07-06 17:01:17 +08:00
return ret;
}
double UserDefault::getDoubleForKey(const char* pKey)
{
return getDoubleForKey(pKey, 0);
}
double UserDefault::getDoubleForKey(const char* pKey, double defaultValue)
{
double ret = defaultValue;
2015-07-06 17:01:17 +08:00
NSNumber *value = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithUTF8String:pKey]];
if (value)
{
ret = [value doubleValue];
}
2015-07-06 17:01:17 +08:00
return ret;
}
std::string UserDefault::getStringForKey(const char* pKey)
{
return getStringForKey(pKey, "");
}
string UserDefault::getStringForKey(const char* pKey, const std::string & defaultValue)
{
NSString *str = [[NSUserDefaults standardUserDefaults] stringForKey:[NSString stringWithUTF8String:pKey]];
if (! str)
{
return defaultValue;
}
else
{
return [str UTF8String];
}
}
Data UserDefault::getDataForKey(const char* pKey)
{
2013-12-20 21:16:46 +08:00
return getDataForKey(pKey, Data::Null);
}
Data UserDefault::getDataForKey(const char* pKey, const Data& defaultValue)
{
NSData *data = [[NSUserDefaults standardUserDefaults] dataForKey:[NSString stringWithUTF8String:pKey]];
if (! data)
{
return defaultValue;
}
else
{
Data ret;
ret.copy((unsigned char*)data.bytes, data.length);
return ret;
}
}
void UserDefault::setBoolForKey(const char* pKey, bool value)
{
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:value] forKey:[NSString stringWithUTF8String:pKey]];
}
void UserDefault::setIntegerForKey(const char* pKey, int value)
{
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:value] forKey:[NSString stringWithUTF8String:pKey]];
}
void UserDefault::setFloatForKey(const char* pKey, float value)
{
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithFloat:value] forKey:[NSString stringWithUTF8String:pKey]];
}
void UserDefault::setDoubleForKey(const char* pKey, double value)
{
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithDouble:value] forKey:[NSString stringWithUTF8String:pKey]];
}
void UserDefault::setStringForKey(const char* pKey, const std::string & value)
{
[[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithUTF8String:value.c_str()] forKey:[NSString stringWithUTF8String:pKey]];
}
void UserDefault::setDataForKey(const char* pKey, const Data& value) {
[[NSUserDefaults standardUserDefaults] setObject:[NSData dataWithBytes: value.getBytes() length: value.getSize()] forKey:[NSString stringWithUTF8String:pKey]];
}
UserDefault* UserDefault::getInstance()
2015-07-06 17:01:17 +08:00
{
if (! _userDefault)
{
_userDefault = new (std::nothrow) UserDefault();
}
2015-07-06 17:01:17 +08:00
return _userDefault;
}
void UserDefault::destroyInstance()
{
CC_SAFE_DELETE(_userDefault);
}
bool UserDefault::isXMLFileExist()
{
return FileUtils::getInstance()->isFileExist(_filePath);
}
void UserDefault::initXMLFilePath()
{
}
// create new xml file
bool UserDefault::createXMLFile()
{
return false;
}
const string& UserDefault::getXMLFilePath()
{
return _filePath;
}
void UserDefault::flush()
{
[[NSUserDefaults standardUserDefaults] synchronize];
}
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();
}
NS_CC_END
#endif // (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)