1
0

CCUserDefault-apple.mm 13 KB

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