CCUserDefault-winrt.cpp 8.0 KB

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