CCUserDefault-apple.mm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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 "platform/CCPlatformConfig.h"
  23. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
  24. #import <Foundation/Foundation.h>
  25. #include <string>
  26. #import "base/CCUserDefault.h"
  27. #import "tinyxml2.h"
  28. #import "platform/CCPlatformConfig.h"
  29. #import "platform/CCPlatformMacros.h"
  30. #import "base/base64.h"
  31. #import "platform/CCFileUtils.h"
  32. #define XML_FILE_NAME "UserDefault.xml"
  33. // root name of xml
  34. #define USERDEFAULT_ROOT_NAME "userDefaultRoot"
  35. #define KEEP_COMPATABILITY
  36. using namespace std;
  37. NS_CC_BEGIN
  38. /**
  39. * implements of UserDefault
  40. */
  41. UserDefault* UserDefault::_userDefault = nullptr;
  42. string UserDefault::_filePath = string("");
  43. bool UserDefault::_isFilePathInitialized = false;
  44. #ifdef KEEP_COMPATABILITY
  45. static tinyxml2::XMLElement* getXMLNodeForKey(const char* pKey, tinyxml2::XMLDocument **doc)
  46. {
  47. tinyxml2::XMLElement* curNode = nullptr;
  48. tinyxml2::XMLElement* rootNode = nullptr;
  49. if (! UserDefault::isXMLFileExist())
  50. {
  51. return nullptr;
  52. }
  53. // check the key value
  54. if (! pKey)
  55. {
  56. return nullptr;
  57. }
  58. do
  59. {
  60. tinyxml2::XMLDocument* xmlDoc = new (std::nothrow) tinyxml2::XMLDocument();
  61. *doc = xmlDoc;
  62. std::string xmlBuffer = FileUtils::getInstance()->getStringFromFile(UserDefault::getInstance()->getXMLFilePath());
  63. if (xmlBuffer.empty())
  64. {
  65. NSLog(@"can not read xml file");
  66. break;
  67. }
  68. xmlDoc->Parse(xmlBuffer.c_str(), xmlBuffer.size());
  69. // get root node
  70. rootNode = xmlDoc->RootElement();
  71. if (nullptr == rootNode)
  72. {
  73. NSLog(@"read root node error");
  74. break;
  75. }
  76. // find the node
  77. curNode = rootNode->FirstChildElement();
  78. if (!curNode)
  79. {
  80. // There is not xml node, delete xml file.
  81. remove(UserDefault::getInstance()->getXMLFilePath().c_str());
  82. return nullptr;
  83. }
  84. while (nullptr != curNode)
  85. {
  86. const char* nodeName = curNode->Value();
  87. if (!strcmp(nodeName, pKey))
  88. {
  89. // delete the node
  90. break;
  91. }
  92. curNode = curNode->NextSiblingElement();
  93. }
  94. } while (0);
  95. return curNode;
  96. }
  97. static void deleteNode(tinyxml2::XMLDocument* doc, tinyxml2::XMLElement* node)
  98. {
  99. if (node)
  100. {
  101. doc->DeleteNode(node);
  102. doc->SaveFile(UserDefault::getInstance()->getXMLFilePath().c_str());
  103. delete doc;
  104. }
  105. }
  106. static void deleteNodeByKey(const char *pKey)
  107. {
  108. tinyxml2::XMLDocument* doc = nullptr;
  109. tinyxml2::XMLElement* node = getXMLNodeForKey(pKey, &doc);
  110. deleteNode(doc, node);
  111. }
  112. #endif
  113. UserDefault::~UserDefault()
  114. {
  115. }
  116. UserDefault::UserDefault()
  117. {
  118. }
  119. bool UserDefault::getBoolForKey(const char* pKey)
  120. {
  121. return getBoolForKey(pKey, false);
  122. }
  123. bool UserDefault::getBoolForKey(const char* pKey, bool defaultValue)
  124. {
  125. #ifdef KEEP_COMPATABILITY
  126. tinyxml2::XMLDocument* doc = nullptr;
  127. tinyxml2::XMLElement* node = getXMLNodeForKey(pKey, &doc);
  128. if (node)
  129. {
  130. if (node->FirstChild())
  131. {
  132. const char* value = (const char*)node->FirstChild()->Value();
  133. bool ret = (! strcmp(value, "true"));
  134. // set value in NSUserDefaults
  135. setBoolForKey(pKey, ret);
  136. flush();
  137. // delete xmle node
  138. deleteNode(doc, node);
  139. return ret;
  140. }
  141. else
  142. {
  143. // delete xmle node
  144. deleteNode(doc, node);
  145. }
  146. }
  147. #endif
  148. bool ret = defaultValue;
  149. NSNumber *value = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithUTF8String:pKey]];
  150. if (value)
  151. {
  152. ret = [value boolValue];
  153. }
  154. return ret;
  155. }
  156. int UserDefault::getIntegerForKey(const char* pKey)
  157. {
  158. return getIntegerForKey(pKey, 0);
  159. }
  160. int UserDefault::getIntegerForKey(const char* pKey, int defaultValue)
  161. {
  162. #ifdef KEEP_COMPATABILITY
  163. tinyxml2::XMLDocument* doc = nullptr;
  164. tinyxml2::XMLElement* node = getXMLNodeForKey(pKey, &doc);
  165. if (node)
  166. {
  167. if (node->FirstChild())
  168. {
  169. int ret = atoi((const char*)node->FirstChild()->Value());
  170. // set value in NSUserDefaults
  171. setIntegerForKey(pKey, ret);
  172. flush();
  173. // delete xmle node
  174. deleteNode(doc, node);
  175. return ret;
  176. }
  177. else
  178. {
  179. // delete xmle node
  180. deleteNode(doc, node);
  181. }
  182. }
  183. #endif
  184. int ret = defaultValue;
  185. NSNumber *value = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithUTF8String:pKey]];
  186. if (value)
  187. {
  188. ret = [value intValue];
  189. }
  190. return ret;
  191. }
  192. float UserDefault::getFloatForKey(const char* pKey)
  193. {
  194. return getFloatForKey(pKey, 0);
  195. }
  196. float UserDefault::getFloatForKey(const char* pKey, float defaultValue)
  197. {
  198. #ifdef KEEP_COMPATABILITY
  199. tinyxml2::XMLDocument* doc = nullptr;
  200. tinyxml2::XMLElement* node = getXMLNodeForKey(pKey, &doc);
  201. if (node)
  202. {
  203. if (node->FirstChild())
  204. {
  205. float ret = atof((const char*)node->FirstChild()->Value());
  206. // set value in NSUserDefaults
  207. setFloatForKey(pKey, ret);
  208. flush();
  209. // delete xmle node
  210. deleteNode(doc, node);
  211. return ret;
  212. }
  213. else
  214. {
  215. // delete xmle node
  216. deleteNode(doc, node);
  217. }
  218. }
  219. #endif
  220. float ret = defaultValue;
  221. NSNumber *value = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithUTF8String:pKey]];
  222. if (value)
  223. {
  224. ret = [value floatValue];
  225. }
  226. return ret;
  227. }
  228. double UserDefault::getDoubleForKey(const char* pKey)
  229. {
  230. return getDoubleForKey(pKey, 0);
  231. }
  232. double UserDefault::getDoubleForKey(const char* pKey, double defaultValue)
  233. {
  234. #ifdef KEEP_COMPATABILITY
  235. tinyxml2::XMLDocument* doc = nullptr;
  236. tinyxml2::XMLElement* node = getXMLNodeForKey(pKey, &doc);
  237. if (node)
  238. {
  239. if (node->FirstChild())
  240. {
  241. double ret = atof((const char*)node->FirstChild()->Value());
  242. // set value in NSUserDefaults
  243. setDoubleForKey(pKey, ret);
  244. flush();
  245. // delete xmle node
  246. deleteNode(doc, node);
  247. return ret;
  248. }
  249. else
  250. {
  251. // delete xmle node
  252. deleteNode(doc, node);
  253. }
  254. }
  255. #endif
  256. double ret = defaultValue;
  257. NSNumber *value = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithUTF8String:pKey]];
  258. if (value)
  259. {
  260. ret = [value doubleValue];
  261. }
  262. return ret;
  263. }
  264. std::string UserDefault::getStringForKey(const char* pKey)
  265. {
  266. return getStringForKey(pKey, "");
  267. }
  268. string UserDefault::getStringForKey(const char* pKey, const std::string & defaultValue)
  269. {
  270. #ifdef KEEP_COMPATABILITY
  271. tinyxml2::XMLDocument* doc = nullptr;
  272. tinyxml2::XMLElement* node = getXMLNodeForKey(pKey, &doc);
  273. if (node)
  274. {
  275. if (node->FirstChild())
  276. {
  277. string ret = (const char*)node->FirstChild()->Value();
  278. // set value in NSUserDefaults
  279. setStringForKey(pKey, ret);
  280. flush();
  281. // delete xmle node
  282. deleteNode(doc, node);
  283. return ret;
  284. }
  285. else
  286. {
  287. // delete xmle node
  288. deleteNode(doc, node);
  289. }
  290. }
  291. #endif
  292. NSString *str = [[NSUserDefaults standardUserDefaults] stringForKey:[NSString stringWithUTF8String:pKey]];
  293. if (! str)
  294. {
  295. return defaultValue;
  296. }
  297. else
  298. {
  299. return [str UTF8String];
  300. }
  301. }
  302. Data UserDefault::getDataForKey(const char* pKey)
  303. {
  304. return getDataForKey(pKey, Data::Null);
  305. }
  306. Data UserDefault::getDataForKey(const char* pKey, const Data& defaultValue)
  307. {
  308. #ifdef KEEP_COMPATABILITY
  309. tinyxml2::XMLDocument* doc = nullptr;
  310. tinyxml2::XMLElement* node = getXMLNodeForKey(pKey, &doc);
  311. if (node)
  312. {
  313. if (node->FirstChild())
  314. {
  315. const char * encodedData = node->FirstChild()->Value();
  316. unsigned char * decodedData;
  317. int decodedDataLen = base64Decode((unsigned char*)encodedData, (unsigned int)strlen(encodedData), &decodedData);
  318. if (decodedData) {
  319. Data ret;
  320. ret.fastSet(decodedData, decodedDataLen);
  321. // set value in NSUserDefaults
  322. setDataForKey(pKey, ret);
  323. flush();
  324. // delete xmle node
  325. deleteNode(doc, node);
  326. return ret;
  327. }
  328. }
  329. else
  330. {
  331. // delete xmle node
  332. deleteNode(doc, node);
  333. }
  334. }
  335. #endif
  336. NSData *data = [[NSUserDefaults standardUserDefaults] dataForKey:[NSString stringWithUTF8String:pKey]];
  337. if (! data)
  338. {
  339. return defaultValue;
  340. }
  341. else
  342. {
  343. Data ret;
  344. ret.copy((unsigned char*)data.bytes, data.length);
  345. return ret;
  346. }
  347. }
  348. void UserDefault::setBoolForKey(const char* pKey, bool value)
  349. {
  350. #ifdef KEEP_COMPATABILITY
  351. deleteNodeByKey(pKey);
  352. #endif
  353. [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:value] forKey:[NSString stringWithUTF8String:pKey]];
  354. }
  355. void UserDefault::setIntegerForKey(const char* pKey, int value)
  356. {
  357. #ifdef KEEP_COMPATABILITY
  358. deleteNodeByKey(pKey);
  359. #endif
  360. [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:value] forKey:[NSString stringWithUTF8String:pKey]];
  361. }
  362. void UserDefault::setFloatForKey(const char* pKey, float value)
  363. {
  364. #ifdef KEEP_COMPATABILITY
  365. deleteNodeByKey(pKey);
  366. #endif
  367. [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithFloat:value] forKey:[NSString stringWithUTF8String:pKey]];
  368. }
  369. void UserDefault::setDoubleForKey(const char* pKey, double value)
  370. {
  371. #ifdef KEEP_COMPATABILITY
  372. deleteNodeByKey(pKey);
  373. #endif
  374. [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithDouble:value] forKey:[NSString stringWithUTF8String:pKey]];
  375. }
  376. void UserDefault::setStringForKey(const char* pKey, const std::string & value)
  377. {
  378. #ifdef KEEP_COMPATABILITY
  379. deleteNodeByKey(pKey);
  380. #endif
  381. [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithUTF8String:value.c_str()] forKey:[NSString stringWithUTF8String:pKey]];
  382. }
  383. void UserDefault::setDataForKey(const char* pKey, const Data& value) {
  384. #ifdef KEEP_COMPATABILITY
  385. deleteNodeByKey(pKey);
  386. #endif
  387. [[NSUserDefaults standardUserDefaults] setObject:[NSData dataWithBytes: value.getBytes() length: value.getSize()] forKey:[NSString stringWithUTF8String:pKey]];
  388. }
  389. UserDefault* UserDefault::getInstance()
  390. {
  391. if (! _userDefault)
  392. {
  393. #ifdef KEEP_COMPATABILITY
  394. initXMLFilePath();
  395. #endif
  396. _userDefault = new (std::nothrow) UserDefault();
  397. }
  398. return _userDefault;
  399. }
  400. void UserDefault::destroyInstance()
  401. {
  402. CC_SAFE_DELETE(_userDefault);
  403. }
  404. // FIXME:: deprecated
  405. UserDefault* UserDefault::sharedUserDefault()
  406. {
  407. return UserDefault::getInstance();
  408. }
  409. // FIXME:: deprecated
  410. void UserDefault::purgeSharedUserDefault()
  411. {
  412. UserDefault::destroyInstance();
  413. }
  414. bool UserDefault::isXMLFileExist()
  415. {
  416. return FileUtils::getInstance()->isFileExist(_filePath);
  417. }
  418. void UserDefault::initXMLFilePath()
  419. {
  420. #ifdef KEEP_COMPATABILITY
  421. if (! _isFilePathInitialized)
  422. {
  423. // xml file is stored in cache directory before 2.1.2
  424. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  425. NSString *documentsDirectory = [paths objectAtIndex:0];
  426. _filePath = [documentsDirectory UTF8String];
  427. _filePath.append("/");
  428. _filePath += XML_FILE_NAME;
  429. _isFilePathInitialized = true;
  430. }
  431. #endif
  432. }
  433. // create new xml file
  434. bool UserDefault::createXMLFile()
  435. {
  436. return false;
  437. }
  438. const string& UserDefault::getXMLFilePath()
  439. {
  440. return _filePath;
  441. }
  442. void UserDefault::flush()
  443. {
  444. [[NSUserDefaults standardUserDefaults] synchronize];
  445. }
  446. void UserDefault::deleteValueForKey(const char* key)
  447. {
  448. // check the params
  449. if (!key)
  450. {
  451. CCLOG("the key is invalid");
  452. }
  453. [[NSUserDefaults standardUserDefaults] removeObjectForKey:[NSString stringWithUTF8String:key]];
  454. flush();
  455. }
  456. NS_CC_END
  457. #endif // (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)