diff --git a/cocos/2d/libcocos2d_8_1/libcocos2d_8_1/libcocos2d_8_1.Shared/libcocos2d_8_1.Shared.vcxitems b/cocos/2d/libcocos2d_8_1/libcocos2d_8_1/libcocos2d_8_1.Shared/libcocos2d_8_1.Shared.vcxitems index 939a17cb9c..cc2f9152a9 100644 --- a/cocos/2d/libcocos2d_8_1/libcocos2d_8_1/libcocos2d_8_1.Shared/libcocos2d_8_1.Shared.vcxitems +++ b/cocos/2d/libcocos2d_8_1/libcocos2d_8_1/libcocos2d_8_1.Shared/libcocos2d_8_1.Shared.vcxitems @@ -907,7 +907,7 @@ - + diff --git a/cocos/2d/libcocos2d_8_1/libcocos2d_8_1/libcocos2d_8_1.Shared/libcocos2d_8_1.Shared.vcxitems.filters b/cocos/2d/libcocos2d_8_1/libcocos2d_8_1/libcocos2d_8_1.Shared/libcocos2d_8_1.Shared.vcxitems.filters index 6bdd59555d..bf8468a436 100644 --- a/cocos/2d/libcocos2d_8_1/libcocos2d_8_1/libcocos2d_8_1.Shared/libcocos2d_8_1.Shared.vcxitems.filters +++ b/cocos/2d/libcocos2d_8_1/libcocos2d_8_1/libcocos2d_8_1.Shared/libcocos2d_8_1.Shared.vcxitems.filters @@ -2812,9 +2812,6 @@ base - - base - base @@ -3477,6 +3474,9 @@ physics3d + + base + diff --git a/cocos/audio/winrt/SimpleAudioEngine.cpp b/cocos/audio/winrt/SimpleAudioEngine.cpp index cc5dd6c9d7..df69cf263c 100644 --- a/cocos/audio/winrt/SimpleAudioEngine.cpp +++ b/cocos/audio/winrt/SimpleAudioEngine.cpp @@ -83,7 +83,7 @@ void SimpleAudioEngine::playBackgroundMusic(const char* pszFilePath, bool bLoop) return; } - string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(pszFilePath); + string fullPath = CCFileUtils::getInstance()->fullPathForFilename(pszFilePath); sharedAudioController()->PlayBackgroundMusic(fullPath.c_str(), bLoop); } @@ -124,7 +124,7 @@ bool SimpleAudioEngine::isBackgroundMusicPlaying() unsigned int SimpleAudioEngine::playEffect(const char* pszFilePath, bool bLoop,float pitch, float pan, float gain) { unsigned int sound; - string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(pszFilePath); + string fullPath = CCFileUtils::getInstance()->fullPathForFilename(pszFilePath); sharedAudioController()->PlaySoundEffect(fullPath.c_str(), bLoop, sound); // TODO: need to support playEffect parameters return sound; } @@ -136,7 +136,7 @@ void SimpleAudioEngine::stopEffect(unsigned int nSoundId) void SimpleAudioEngine::preloadEffect(const char* pszFilePath) { - string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(pszFilePath); + string fullPath = CCFileUtils::getInstance()->fullPathForFilename(pszFilePath); sharedAudioController()->PreloadSoundEffect(fullPath.c_str()); } @@ -172,7 +172,7 @@ void SimpleAudioEngine::preloadBackgroundMusic(const char* pszFilePath) void SimpleAudioEngine::unloadEffect(const char* pszFilePath) { - string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(pszFilePath); + string fullPath = CCFileUtils::getInstance()->fullPathForFilename(pszFilePath); sharedAudioController()->UnloadSoundEffect(fullPath.c_str()); } diff --git a/cocos/base/CCUserDefault-winrt.cpp b/cocos/base/CCUserDefault-winrt.cpp new file mode 100644 index 0000000000..4279c67ecb --- /dev/null +++ b/cocos/base/CCUserDefault-winrt.cpp @@ -0,0 +1,334 @@ +/**************************************************************************** +Copyright (c) 2010-2012 cocos2d-x.org +Copyright (c) 2013-2014 Chukong Technologies Inc. + +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. +****************************************************************************/ +#include "base/CCUserDefault.h" +#include "platform/CCCommon.h" +#include "base/base64.h" +#include "base/ccUtils.h" + +#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) +#include "CCWinRTUtils.h" + +using namespace Windows::Storage; +using namespace Windows::Foundation; +using namespace std; + +#define XML_FILE_NAME "UserDefault.xml" + +NS_CC_BEGIN + + +/** + * WinRT implementation of UserDefault + */ + +UserDefault* UserDefault::_userDefault = nullptr; +string UserDefault::_filePath = string(""); +bool UserDefault::_isFilePathInitialized = false; + +UserDefault::~UserDefault() +{ +} + +UserDefault::UserDefault() +{ +} + +Platform::Object^ getPlatformKeyValue(const char* pKey) +{ + // check key + if (!pKey) + { + return nullptr; + } + + ApplicationDataContainer^ localSettings = ApplicationData::Current->LocalSettings; + auto key = PlatformStringFromString(pKey); + auto values = localSettings->Values; + + if (values->HasKey(key)) + { + return values->Lookup(key); + } + + return nullptr; +} + +void setPlatformKeyValue(const char* pKey, PropertyValue^ value) +{ + // check key + if (!pKey) + { + return; + } + + ApplicationDataContainer^ localSettings = ApplicationData::Current->LocalSettings; + auto values = localSettings->Values; + values->Insert(PlatformStringFromString(pKey), value); +} + +bool UserDefault::getBoolForKey(const char* pKey) +{ + return getBoolForKey(pKey, false); +} + +bool UserDefault::getBoolForKey(const char* pKey, bool defaultValue) +{ + bool ret = defaultValue; + auto value = getPlatformKeyValue(pKey); + if (value) + { + ret = safe_cast(value); + } + return ret; +} + +int UserDefault::getIntegerForKey(const char* pKey) +{ + return getIntegerForKey(pKey, 0); +} + +int UserDefault::getIntegerForKey(const char* pKey, int defaultValue) +{ + int ret = defaultValue; + auto value = getPlatformKeyValue(pKey); + if (value) + { + ret = safe_cast(value); + } + return ret; +} + +float UserDefault::getFloatForKey(const char* pKey) +{ + return getFloatForKey(pKey, 0.0f); +} + +float UserDefault::getFloatForKey(const char* pKey, float defaultValue) +{ + float ret = (float)getDoubleForKey(pKey, (double)defaultValue); + + return ret; +} + +double UserDefault::getDoubleForKey(const char* pKey) +{ + return getDoubleForKey(pKey, 0.0); +} + +double UserDefault::getDoubleForKey(const char* pKey, double defaultValue) +{ + double ret = defaultValue; + auto value = getPlatformKeyValue(pKey); + if (value) + { + ret = safe_cast(value); + } + return ret; +} + +std::string UserDefault::getStringForKey(const char* pKey) +{ + return getStringForKey(pKey, ""); +} + +string UserDefault::getStringForKey(const char* pKey, const std::string & defaultValue) +{ + string ret = defaultValue; + auto value = getPlatformKeyValue(pKey); + if (value) + { + auto result = safe_cast(value); + ret = PlatformStringToString(result); + } + + return ret; +} + +Data UserDefault::getDataForKey(const char* pKey) +{ + return getDataForKey(pKey, Data::Null); +} + +Data UserDefault::getDataForKey(const char* pKey, const Data& defaultValue) +{ + Data ret = defaultValue; + std::string encodedData = getStringForKey(pKey,""); + + if (!encodedData.empty()) + { + unsigned char* decodedData = nullptr; + int decodedDataLen = base64Decode((unsigned char*) encodedData.c_str(), (unsigned int) encodedData.length(), &decodedData); + if (decodedData && decodedDataLen > 0) + { + ret.fastSet(decodedData, decodedDataLen); + } + } + + return ret; +} + +void UserDefault::setBoolForKey(const char* pKey, bool value) +{ + // check key + if (!pKey) + { + return; + } + + setPlatformKeyValue(pKey, dynamic_cast(PropertyValue::CreateBoolean(value))); +} + +void UserDefault::setIntegerForKey(const char* pKey, int value) +{ + // check key + if (! pKey) + { + return; + } + + setPlatformKeyValue(pKey, dynamic_cast(PropertyValue::CreateInt32(value))); +} + +void UserDefault::setFloatForKey(const char* pKey, float value) +{ + setDoubleForKey(pKey, value); +} + +void UserDefault::setDoubleForKey(const char* pKey, double value) +{ + // check key + if (! pKey) + { + return; + } + + setPlatformKeyValue(pKey, dynamic_cast(PropertyValue::CreateDouble(value))); +} + +void UserDefault::setStringForKey(const char* pKey, const std::string & value) +{ + // check key + if (! pKey) + { + return; + } + + setPlatformKeyValue(pKey, dynamic_cast(PropertyValue::CreateString(PlatformStringFromString(value)))); +} + +void UserDefault::setDataForKey(const char* pKey, const Data& value) { + // check key + if (! pKey) + { + return; + } + + char *encodedData = 0; + base64Encode(value.getBytes(), static_cast(value.getSize()), &encodedData); + + setPlatformKeyValue(pKey, dynamic_cast(PropertyValue::CreateString(PlatformStringFromString(encodedData)))); + + if (encodedData) + free(encodedData); +} + +UserDefault* UserDefault::getInstance() +{ + if (!_userDefault) + { + initXMLFilePath(); + + // only create xml file one time + // the file exists after the program exit + if ((!isXMLFileExist()) && (!createXMLFile())) + { + return nullptr; + } + + _userDefault = new (std::nothrow) UserDefault(); + } + + return _userDefault; +} + +void UserDefault::destroyInstance() +{ + CC_SAFE_DELETE(_userDefault); +} + +void UserDefault::setDelegate(UserDefault *delegate) +{ + if (_userDefault) + delete _userDefault; + + _userDefault = delegate; +} + + +// FIXME:: deprecated +UserDefault* UserDefault::sharedUserDefault() +{ + return UserDefault::getInstance(); +} + +// FIXME:: deprecated +void UserDefault::purgeSharedUserDefault() +{ + return UserDefault::destroyInstance(); +} + +bool UserDefault::isXMLFileExist() +{ + //return FileUtils::getInstance()->isFileExist(_filePath); + return true; +} + +void UserDefault::initXMLFilePath() +{ + if (! _isFilePathInitialized) + { + _filePath += FileUtils::getInstance()->getWritablePath() + XML_FILE_NAME; + _isFilePathInitialized = true; + } +} + +// create new xml file +bool UserDefault::createXMLFile() +{ + return false; +} + +const string& UserDefault::getXMLFilePath() +{ + return _filePath; +} + +void UserDefault::flush() +{ +} + +NS_CC_END + +#endif // (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) diff --git a/cocos/base/CCUserDefault.h b/cocos/base/CCUserDefault.h index 740fbb127f..5d080cbdb9 100644 --- a/cocos/base/CCUserDefault.h +++ b/cocos/base/CCUserDefault.h @@ -237,7 +237,7 @@ public: * @js NA */ static const std::string& getXMLFilePath(); - /** All supported platforms other iOS & Android use xml file to save values. This function checks whether the xml file exists or not. + /** All supported platforms other iOS & Android and CC_PLATFORM_WINRT use xml file to save values. This function checks whether the xml file exists or not. * @return True if the xml file exists, flase if not. * @js NA */ diff --git a/tests/cpp-tests/Classes/UserDefaultTest/UserDefaultTest.cpp b/tests/cpp-tests/Classes/UserDefaultTest/UserDefaultTest.cpp index 12f28a324b..1f7aee2cb4 100644 --- a/tests/cpp-tests/Classes/UserDefaultTest/UserDefaultTest.cpp +++ b/tests/cpp-tests/Classes/UserDefaultTest/UserDefaultTest.cpp @@ -1,12 +1,18 @@ #include "UserDefaultTest.h" #include "stdio.h" #include "stdlib.h" +#include +#include +#include + +using namespace std; USING_NS_CC; // enable log #define COCOS2D_DEBUG 1 + UserDefaultTests::UserDefaultTests() { ADD_TEST_CASE(UserDefaultTest); @@ -22,6 +28,51 @@ UserDefaultTest::UserDefaultTest() doTest(); } +template +void logData(const char* key) +{ + Data data = UserDefault::getInstance()->getDataForKey(key); + T* buffer = (T*) data.getBytes(); + int length = data.getSize() / sizeof(T); + + std::ostringstream ss; + ss << setprecision(2) << std::fixed; + for (int i = 0; i < length; i++) + { + ss << buffer[i] << " "; + } + + CCLOG("%s is %s", key, ss.str().c_str()); +} + +template +void setData(const char* key) +{ + Data data; + vector v; + + for (int i = 0; i <= 5; i++) + { + v.push_back(static_cast(i)); + } + data.copy((unsigned char*) v.data(), v.size() * sizeof(T)); + UserDefault::getInstance()->setDataForKey(key, data); +} + +template +void setData2(const char* key) +{ + Data data; + vector v; + + for (int i = 5; i >= 0; i--) + { + v.push_back(static_cast(i)); + } + data.copy((unsigned char*) v.data(), v.size() * sizeof(T)); + UserDefault::getInstance()->setDataForKey(key, data); +} + void UserDefaultTest::doTest() { CCLOG("********************** init value ***********************"); @@ -34,6 +85,11 @@ void UserDefaultTest::doTest() UserDefault::getInstance()->setDoubleForKey("double", 2.4); UserDefault::getInstance()->setBoolForKey("bool", true); + // test saving of Data buffers + setData("int_data"); + setData("float_data"); + setData("double_data"); + // print value std::string ret = UserDefault::getInstance()->getStringForKey("string"); @@ -57,7 +113,11 @@ void UserDefaultTest::doTest() { CCLOG("bool is false"); } - + + logData("int_data"); + logData("float_data"); + logData("double_data"); + //CCUserDefault::getInstance()->flush(); CCLOG("********************** after change value ***********************"); @@ -70,6 +130,10 @@ void UserDefaultTest::doTest() UserDefault::getInstance()->setDoubleForKey("double", 2.6); UserDefault::getInstance()->setBoolForKey("bool", false); + setData2("int_data"); + setData2("float_data"); + setData2("double_data"); + UserDefault::getInstance()->flush(); // print value @@ -95,6 +159,10 @@ void UserDefaultTest::doTest() { CCLOG("bool is false"); } + + logData("int_data"); + logData("float_data"); + logData("double_data"); }