CCUserDefault-winrt.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2013-2016 Chukong Technologies Inc.
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #include "base/CCUserDefault.h"
  23. #include "platform/CCCommon.h"
  24. #include "base/base64.h"
  25. #include "base/ccUtils.h"
  26. #include "platform/CCFileUtils.h"
  27. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  28. #include "platform/winrt/CCWinRTUtils.h"
  29. using namespace Windows::Storage;
  30. using namespace Windows::Foundation;
  31. using namespace std;
  32. #define XML_FILE_NAME "UserDefault.xml"
  33. NS_CC_BEGIN
  34. /**
  35. * WinRT implementation of UserDefault
  36. */
  37. UserDefault* UserDefault::_userDefault = nullptr;
  38. string UserDefault::_filePath = string("");
  39. bool UserDefault::_isFilePathInitialized = false;
  40. UserDefault::~UserDefault()
  41. {
  42. }
  43. UserDefault::UserDefault()
  44. {
  45. }
  46. Platform::Object^ getPlatformKeyValue(const char* pKey)
  47. {
  48. // check key
  49. if (!pKey)
  50. {
  51. return nullptr;
  52. }
  53. ApplicationDataContainer^ localSettings = ApplicationData::Current->LocalSettings;
  54. auto key = PlatformStringFromString(pKey);
  55. auto values = localSettings->Values;
  56. if (values->HasKey(key))
  57. {
  58. return values->Lookup(key);
  59. }
  60. return nullptr;
  61. }
  62. void setPlatformKeyValue(const char* pKey, PropertyValue^ value)
  63. {
  64. // check key
  65. if (!pKey)
  66. {
  67. return;
  68. }
  69. ApplicationDataContainer^ localSettings = ApplicationData::Current->LocalSettings;
  70. auto values = localSettings->Values;
  71. values->Insert(PlatformStringFromString(pKey), value);
  72. }
  73. bool UserDefault::getBoolForKey(const char* pKey)
  74. {
  75. return getBoolForKey(pKey, false);
  76. }
  77. bool UserDefault::getBoolForKey(const char* pKey, bool defaultValue)
  78. {
  79. bool ret = defaultValue;
  80. auto value = getPlatformKeyValue(pKey);
  81. if (value)
  82. {
  83. ret = safe_cast<bool>(value);
  84. }
  85. return ret;
  86. }
  87. int UserDefault::getIntegerForKey(const char* pKey)
  88. {
  89. return getIntegerForKey(pKey, 0);
  90. }
  91. int UserDefault::getIntegerForKey(const char* pKey, int defaultValue)
  92. {
  93. int ret = defaultValue;
  94. auto value = getPlatformKeyValue(pKey);
  95. if (value)
  96. {
  97. ret = safe_cast<int>(value);
  98. }
  99. return ret;
  100. }
  101. float UserDefault::getFloatForKey(const char* pKey)
  102. {
  103. return getFloatForKey(pKey, 0.0f);
  104. }
  105. float UserDefault::getFloatForKey(const char* pKey, float defaultValue)
  106. {
  107. float ret = (float)getDoubleForKey(pKey, (double)defaultValue);
  108. return ret;
  109. }
  110. double UserDefault::getDoubleForKey(const char* pKey)
  111. {
  112. return getDoubleForKey(pKey, 0.0);
  113. }
  114. double UserDefault::getDoubleForKey(const char* pKey, double defaultValue)
  115. {
  116. double ret = defaultValue;
  117. auto value = getPlatformKeyValue(pKey);
  118. if (value)
  119. {
  120. ret = safe_cast<double>(value);
  121. }
  122. return ret;
  123. }
  124. std::string UserDefault::getStringForKey(const char* pKey)
  125. {
  126. return getStringForKey(pKey, "");
  127. }
  128. string UserDefault::getStringForKey(const char* pKey, const std::string & defaultValue)
  129. {
  130. string ret = defaultValue;
  131. auto value = getPlatformKeyValue(pKey);
  132. if (value)
  133. {
  134. auto result = safe_cast<Platform::String^>(value);
  135. ret = PlatformStringToString(result);
  136. }
  137. return ret;
  138. }
  139. Data UserDefault::getDataForKey(const char* pKey)
  140. {
  141. return getDataForKey(pKey, Data::Null);
  142. }
  143. Data UserDefault::getDataForKey(const char* pKey, const Data& defaultValue)
  144. {
  145. Data ret = defaultValue;
  146. std::string encodedData = getStringForKey(pKey,"");
  147. if (!encodedData.empty())
  148. {
  149. unsigned char* decodedData = nullptr;
  150. int decodedDataLen = base64Decode((unsigned char*) encodedData.c_str(), (unsigned int) encodedData.length(), &decodedData);
  151. if (decodedData && decodedDataLen > 0)
  152. {
  153. ret.fastSet(decodedData, decodedDataLen);
  154. }
  155. }
  156. return ret;
  157. }
  158. void UserDefault::setBoolForKey(const char* pKey, bool value)
  159. {
  160. // check key
  161. if (!pKey)
  162. {
  163. return;
  164. }
  165. setPlatformKeyValue(pKey, dynamic_cast<PropertyValue^>(PropertyValue::CreateBoolean(value)));
  166. }
  167. void UserDefault::setIntegerForKey(const char* pKey, int value)
  168. {
  169. // check key
  170. if (! pKey)
  171. {
  172. return;
  173. }
  174. setPlatformKeyValue(pKey, dynamic_cast<PropertyValue^>(PropertyValue::CreateInt32(value)));
  175. }
  176. void UserDefault::setFloatForKey(const char* pKey, float value)
  177. {
  178. setDoubleForKey(pKey, value);
  179. }
  180. void UserDefault::setDoubleForKey(const char* pKey, double value)
  181. {
  182. // check key
  183. if (! pKey)
  184. {
  185. return;
  186. }
  187. setPlatformKeyValue(pKey, dynamic_cast<PropertyValue^>(PropertyValue::CreateDouble(value)));
  188. }
  189. void UserDefault::setStringForKey(const char* pKey, const std::string & value)
  190. {
  191. // check key
  192. if (! pKey)
  193. {
  194. return;
  195. }
  196. setPlatformKeyValue(pKey, dynamic_cast<PropertyValue^>(PropertyValue::CreateString(PlatformStringFromString(value))));
  197. }
  198. void UserDefault::setDataForKey(const char* pKey, const Data& value) {
  199. // check key
  200. if (! pKey)
  201. {
  202. return;
  203. }
  204. char *encodedData = nullptr;
  205. base64Encode(value.getBytes(), static_cast<unsigned int>(value.getSize()), &encodedData);
  206. setPlatformKeyValue(pKey, dynamic_cast<PropertyValue^>(PropertyValue::CreateString(PlatformStringFromString(encodedData))));
  207. if (encodedData)
  208. free(encodedData);
  209. }
  210. UserDefault* UserDefault::getInstance()
  211. {
  212. if (!_userDefault)
  213. {
  214. initXMLFilePath();
  215. // only create xml file one time
  216. // the file exists after the program exit
  217. if ((!isXMLFileExist()) && (!createXMLFile()))
  218. {
  219. return nullptr;
  220. }
  221. _userDefault = new (std::nothrow) UserDefault();
  222. }
  223. return _userDefault;
  224. }
  225. void UserDefault::destroyInstance()
  226. {
  227. CC_SAFE_DELETE(_userDefault);
  228. }
  229. void UserDefault::setDelegate(UserDefault *delegate)
  230. {
  231. if (_userDefault)
  232. delete _userDefault;
  233. _userDefault = delegate;
  234. }
  235. // FIXME:: deprecated
  236. UserDefault* UserDefault::sharedUserDefault()
  237. {
  238. return UserDefault::getInstance();
  239. }
  240. // FIXME:: deprecated
  241. void UserDefault::purgeSharedUserDefault()
  242. {
  243. return UserDefault::destroyInstance();
  244. }
  245. bool UserDefault::isXMLFileExist()
  246. {
  247. //return FileUtils::getInstance()->isFileExist(_filePath);
  248. return true;
  249. }
  250. void UserDefault::initXMLFilePath()
  251. {
  252. if (! _isFilePathInitialized)
  253. {
  254. _filePath += FileUtils::getInstance()->getWritablePath() + XML_FILE_NAME;
  255. _isFilePathInitialized = true;
  256. }
  257. }
  258. // create new xml file
  259. bool UserDefault::createXMLFile()
  260. {
  261. return false;
  262. }
  263. const string& UserDefault::getXMLFilePath()
  264. {
  265. return _filePath;
  266. }
  267. void UserDefault::flush()
  268. {
  269. }
  270. void UserDefault::deleteValueForKey(const char* key)
  271. {
  272. // check the params
  273. if (!key)
  274. {
  275. CCLOG("the key is invalid");
  276. }
  277. ApplicationDataContainer^ localSettings = ApplicationData::Current->LocalSettings;
  278. auto values = localSettings->Values;
  279. values->Remove(PlatformStringFromString(key));
  280. flush();
  281. }
  282. NS_CC_END
  283. #endif // (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)