mirror of https://github.com/axmolengine/axmol.git
Merge pull request #11953 from MSOpenTech/v3-winrt-settings
V3: Windows 8.1 Universal App CCUserDefault implementation
This commit is contained in:
commit
678b279754
|
@ -907,7 +907,7 @@
|
|||
<ClCompile Include="$(MSBuildThisFileDirectory)..\..\..\..\base\CCScriptSupport.cpp" />
|
||||
<ClCompile Include="$(MSBuildThisFileDirectory)..\..\..\..\base\CCTouch.cpp" />
|
||||
<ClCompile Include="$(MSBuildThisFileDirectory)..\..\..\..\base\ccTypes.cpp" />
|
||||
<ClCompile Include="$(MSBuildThisFileDirectory)..\..\..\..\base\CCUserDefault.cpp" />
|
||||
<ClCompile Include="$(MSBuildThisFileDirectory)..\..\..\..\base\CCUserDefault-winrt.cpp" />
|
||||
<ClCompile Include="$(MSBuildThisFileDirectory)..\..\..\..\base\ccUTF8.cpp" />
|
||||
<ClCompile Include="$(MSBuildThisFileDirectory)..\..\..\..\base\ccUtils.cpp" />
|
||||
<ClCompile Include="$(MSBuildThisFileDirectory)..\..\..\..\base\CCValue.cpp" />
|
||||
|
|
|
@ -2812,9 +2812,6 @@
|
|||
<ClCompile Include="$(MSBuildThisFileDirectory)..\..\..\..\base\ccTypes.cpp">
|
||||
<Filter>base</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="$(MSBuildThisFileDirectory)..\..\..\..\base\CCUserDefault.cpp">
|
||||
<Filter>base</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="$(MSBuildThisFileDirectory)..\..\..\..\base\ccUTF8.cpp">
|
||||
<Filter>base</Filter>
|
||||
</ClCompile>
|
||||
|
@ -3477,6 +3474,9 @@
|
|||
<ClCompile Include="$(MSBuildThisFileDirectory)..\..\..\..\physics3d\CCPhysicsSprite3D.cpp">
|
||||
<Filter>physics3d</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="$(MSBuildThisFileDirectory)..\..\..\..\base\CCUserDefault-winrt.cpp">
|
||||
<Filter>base</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Filter Include="2d">
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
||||
|
|
|
@ -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<bool>(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<int>(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<double>(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<Platform::String^>(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^>(PropertyValue::CreateBoolean(value)));
|
||||
}
|
||||
|
||||
void UserDefault::setIntegerForKey(const char* pKey, int value)
|
||||
{
|
||||
// check key
|
||||
if (! pKey)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
setPlatformKeyValue(pKey, dynamic_cast<PropertyValue^>(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^>(PropertyValue::CreateDouble(value)));
|
||||
}
|
||||
|
||||
void UserDefault::setStringForKey(const char* pKey, const std::string & value)
|
||||
{
|
||||
// check key
|
||||
if (! pKey)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
setPlatformKeyValue(pKey, dynamic_cast<PropertyValue^>(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<unsigned int>(value.getSize()), &encodedData);
|
||||
|
||||
setPlatformKeyValue(pKey, dynamic_cast<PropertyValue^>(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)
|
|
@ -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
|
||||
*/
|
||||
|
|
|
@ -1,12 +1,18 @@
|
|||
#include "UserDefaultTest.h"
|
||||
#include "stdio.h"
|
||||
#include "stdlib.h"
|
||||
#include <vector>
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
|
||||
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<typename T>
|
||||
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<typename T>
|
||||
void setData(const char* key)
|
||||
{
|
||||
Data data;
|
||||
vector<T> v;
|
||||
|
||||
for (int i = 0; i <= 5; i++)
|
||||
{
|
||||
v.push_back(static_cast<T>(i));
|
||||
}
|
||||
data.copy((unsigned char*) v.data(), v.size() * sizeof(T));
|
||||
UserDefault::getInstance()->setDataForKey(key, data);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void setData2(const char* key)
|
||||
{
|
||||
Data data;
|
||||
vector<T> v;
|
||||
|
||||
for (int i = 5; i >= 0; i--)
|
||||
{
|
||||
v.push_back(static_cast<T>(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>("int_data");
|
||||
setData<float>("float_data");
|
||||
setData<double>("double_data");
|
||||
|
||||
// print value
|
||||
|
||||
std::string ret = UserDefault::getInstance()->getStringForKey("string");
|
||||
|
@ -57,7 +113,11 @@ void UserDefaultTest::doTest()
|
|||
{
|
||||
CCLOG("bool is false");
|
||||
}
|
||||
|
||||
|
||||
logData<int>("int_data");
|
||||
logData<float>("float_data");
|
||||
logData<double>("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>("int_data");
|
||||
setData2<float>("float_data");
|
||||
setData2<double>("double_data");
|
||||
|
||||
UserDefault::getInstance()->flush();
|
||||
|
||||
// print value
|
||||
|
@ -95,6 +159,10 @@ void UserDefaultTest::doTest()
|
|||
{
|
||||
CCLOG("bool is false");
|
||||
}
|
||||
|
||||
logData<int>("int_data");
|
||||
logData<float>("float_data");
|
||||
logData<double>("double_data");
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue