| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538 | 
							- /****************************************************************************
 
- Copyright (c) 2010-2012 cocos2d-x.org
 
- Copyright (c) 2013-2017 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/CCPlatformConfig.h"
 
- #include "base/ccUtils.h"
 
- #include "platform/CCCommon.h"
 
- #include "base/base64.h"
 
- #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
 
- #include "platform/android/jni/JniHelper.h"
 
- // root name of xml
 
- #define USERDEFAULT_ROOT_NAME    "userDefaultRoot"
 
- #define KEEP_COMPATABILITY
 
- #define XML_FILE_NAME "UserDefault.xml"
 
- #ifdef KEEP_COMPATABILITY
 
- #include "platform/CCFileUtils.h"
 
- #include "tinyxml2.h"
 
- #endif
 
- static const std::string helperClassName = "org/cocos2dx/lib/Cocos2dxHelper";
 
- using namespace std;
 
- NS_CC_BEGIN
 
- /**
 
-  * implements of UserDefault
 
-  */
 
- UserDefault* UserDefault::_userDefault = nullptr;
 
- string UserDefault::_filePath = string("");
 
- bool UserDefault::_isFilePathInitialized = false;
 
- #ifdef KEEP_COMPATABILITY
 
- static tinyxml2::XMLElement* getXMLNodeForKey(const char* pKey, tinyxml2::XMLDocument **doc)
 
- {
 
-     tinyxml2::XMLElement* curNode = nullptr;
 
-     tinyxml2::XMLElement* rootNode = nullptr;
 
-     if (! UserDefault::isXMLFileExist())
 
-     {
 
-         return nullptr;
 
-     }
 
-     // check the key value
 
-     if (! pKey)
 
-     {
 
-         return nullptr;
 
-     }
 
-     do
 
-     {
 
-         tinyxml2::XMLDocument* xmlDoc = new (std::nothrow) tinyxml2::XMLDocument();
 
-         *doc = xmlDoc;
 
-         ssize_t size;
 
-         std::string xmlBuffer = FileUtils::getInstance()->getStringFromFile(UserDefault::getInstance()->getXMLFilePath());
 
-         if (xmlBuffer.empty())
 
-         {
 
-             CCLOG("can not read xml file");
 
-             break;
 
-         }
 
-         xmlDoc->Parse(xmlBuffer.c_str());
 
-         // get root node
 
-         rootNode = xmlDoc->RootElement();
 
-         if (nullptr == rootNode)
 
-         {
 
-             CCLOG("read root node error");
 
-             break;
 
-         }
 
-         // find the node
 
-         curNode = rootNode->FirstChildElement();
 
-         if (!curNode)
 
-         {
 
-             // There is not xml node, delete xml file.
 
-             remove(UserDefault::getInstance()->getXMLFilePath().c_str());
 
-             return nullptr;
 
-         }
 
-         while (nullptr != curNode)
 
-         {
 
-             const char* nodeName = curNode->Value();
 
-             if (!strcmp(nodeName, pKey))
 
-             {
 
-                 // delete the node
 
-                 break;
 
-             }
 
-             curNode = curNode->NextSiblingElement();
 
-         }
 
-     } while (0);
 
-     return curNode;
 
- }
 
- static void deleteNode(tinyxml2::XMLDocument* doc, tinyxml2::XMLElement* node)
 
- {
 
-     if (node)
 
-     {
 
-         doc->DeleteNode(node);
 
-         doc->SaveFile(UserDefault::getInstance()->getXMLFilePath().c_str());
 
-         delete doc;
 
-     }
 
- }
 
- static void deleteNodeByKey(const char *pKey)
 
- {
 
-     tinyxml2::XMLDocument* doc = nullptr;
 
-     tinyxml2::XMLElement* node = getXMLNodeForKey(pKey, &doc);
 
-     deleteNode(doc, node);
 
- }
 
- #endif
 
- UserDefault::~UserDefault()
 
- {
 
- }
 
- UserDefault::UserDefault()
 
- {
 
- }
 
- // FIXME:: deprecated
 
- void UserDefault::purgeSharedUserDefault()
 
- {
 
-     UserDefault::destroyInstance();
 
- }
 
- void UserDefault::destroyInstance()
 
- {
 
-    CC_SAFE_DELETE(_userDefault);
 
- }
 
- bool UserDefault::getBoolForKey(const char* pKey)
 
- {
 
-     return getBoolForKey(pKey, false);
 
- }
 
- bool UserDefault::getBoolForKey(const char* pKey, bool defaultValue)
 
- {
 
- #ifdef KEEP_COMPATABILITY
 
-     tinyxml2::XMLDocument* doc = nullptr;
 
-     tinyxml2::XMLElement* node = getXMLNodeForKey(pKey, &doc);
 
-     if (node)
 
-     {
 
-         if (node->FirstChild())
 
-         {
 
-             const char* value = (const char*)node->FirstChild()->Value();
 
-             bool ret = (! strcmp(value, "true"));
 
-             // set value in NSUserDefaults
 
-             setBoolForKey(pKey, ret);
 
-             flush();
 
-             // delete xmle node
 
-             deleteNode(doc, node);
 
-             return ret;
 
-         }
 
-         else
 
-         {
 
-             // delete xmle node
 
-             deleteNode(doc, node);
 
-         }
 
-     }
 
- #endif
 
-     return JniHelper::callStaticBooleanMethod(helperClassName, "getBoolForKey", pKey, defaultValue);
 
- }
 
- int UserDefault::getIntegerForKey(const char* pKey)
 
- {
 
-     return getIntegerForKey(pKey, 0);
 
- }
 
- int UserDefault::getIntegerForKey(const char* pKey, int defaultValue)
 
- {
 
- #ifdef KEEP_COMPATABILITY
 
-     tinyxml2::XMLDocument* doc = nullptr;
 
-     tinyxml2::XMLElement* node = getXMLNodeForKey(pKey, &doc);
 
-     if (node)
 
-     {
 
-         if (node->FirstChild())
 
-         {
 
-             int ret = atoi((const char*)node->FirstChild()->Value());
 
-             // set value in NSUserDefaults
 
-             setIntegerForKey(pKey, ret);
 
-             flush();
 
-             // delete xmle node
 
-             deleteNode(doc, node);
 
-             return ret;
 
-         }
 
-         else
 
-         {
 
-             // delete xmle node
 
-             deleteNode(doc, node);
 
-         }
 
-     }
 
- #endif
 
- 	return JniHelper::callStaticIntMethod(helperClassName, "getIntegerForKey", pKey, defaultValue);
 
- }
 
- float UserDefault::getFloatForKey(const char* pKey)
 
- {
 
-     return getFloatForKey(pKey, 0.0f);
 
- }
 
- float UserDefault::getFloatForKey(const char* pKey, float defaultValue)
 
- {
 
- #ifdef KEEP_COMPATABILITY
 
-     tinyxml2::XMLDocument* doc = nullptr;
 
-     tinyxml2::XMLElement* node = getXMLNodeForKey(pKey, &doc);
 
-     if (node)
 
-     {
 
-         if (node->FirstChild())
 
-         {
 
-             float ret = utils::atof((const char*)node->FirstChild()->Value());
 
-             // set value in NSUserDefaults
 
-             setFloatForKey(pKey, ret);
 
-             flush();
 
-             // delete xmle node
 
-             deleteNode(doc, node);
 
-             return ret;
 
-         }
 
-         else
 
-         {
 
-             // delete xmle node
 
-             deleteNode(doc, node);
 
-         }
 
-     }
 
- #endif
 
-     return JniHelper::callStaticFloatMethod(helperClassName, "getFloatForKey", pKey, defaultValue);
 
- }
 
- double  UserDefault::getDoubleForKey(const char* pKey)
 
- {
 
-     return getDoubleForKey(pKey, 0.0);
 
- }
 
- double UserDefault::getDoubleForKey(const char* pKey, double defaultValue)
 
- {
 
- #ifdef KEEP_COMPATABILITY
 
-     tinyxml2::XMLDocument* doc = nullptr;
 
-     tinyxml2::XMLElement* node = getXMLNodeForKey(pKey, &doc);
 
-     if (node)
 
-     {
 
-         if (node->FirstChild())
 
-         {
 
-             double ret = utils::atof((const char*)node->FirstChild()->Value());
 
-             // set value in NSUserDefaults
 
-             setDoubleForKey(pKey, ret);
 
-             flush();
 
-             // delete xmle node
 
-             deleteNode(doc, node);
 
-             return ret;
 
-         }
 
-         else
 
-         {
 
-             // delete xmle node
 
-             deleteNode(doc, node);
 
-         }
 
-     }
 
- #endif
 
- 	return JniHelper::callStaticDoubleMethod(helperClassName, "getDoubleForKey", pKey, defaultValue);
 
- }
 
- std::string UserDefault::getStringForKey(const char* pKey)
 
- {
 
-     return getStringForKey(pKey, "");
 
- }
 
- string UserDefault::getStringForKey(const char* pKey, const std::string & defaultValue)
 
- {
 
- #ifdef KEEP_COMPATABILITY
 
-     tinyxml2::XMLDocument* doc = nullptr;
 
-     tinyxml2::XMLElement* node = getXMLNodeForKey(pKey, &doc);
 
-     if (node)
 
-     {
 
-         if (node->FirstChild())
 
-         {
 
-             string ret = (const char*)node->FirstChild()->Value();
 
-             // set value in NSUserDefaults
 
-             setStringForKey(pKey, ret);
 
-             flush();
 
-             // delete xmle node
 
-             deleteNode(doc, node);
 
-             return ret;
 
-         }
 
-         else
 
-         {
 
-             // delete xmle node
 
-             deleteNode(doc, node);
 
-         }
 
-     }
 
- #endif
 
-     return JniHelper::callStaticStringMethod(helperClassName, "getStringForKey", pKey, defaultValue);
 
- }
 
- Data UserDefault::getDataForKey(const char* pKey)
 
- {
 
-     return getDataForKey(pKey, Data::Null);
 
- }
 
- Data UserDefault::getDataForKey(const char* pKey, const Data& defaultValue)
 
- {
 
- #ifdef KEEP_COMPATABILITY
 
-     tinyxml2::XMLDocument* doc = nullptr;
 
-     tinyxml2::XMLElement* node = getXMLNodeForKey(pKey, &doc);
 
-     if (node)
 
-     {
 
-         if (node->FirstChild())
 
-         {
 
-             const char * encodedData = node->FirstChild()->Value();
 
-             unsigned char * decodedData;
 
-             int decodedDataLen = base64Decode((unsigned char*)encodedData, (unsigned int)strlen(encodedData), &decodedData);
 
-             if (decodedData) {
 
-                 Data ret;
 
-                 ret.fastSet(decodedData, decodedDataLen);
 
-                 // set value in NSUserDefaults
 
-                 setDataForKey(pKey, ret);
 
-                 flush();
 
-                 // delete xmle node
 
-                 deleteNode(doc, node);
 
-                 return ret;
 
-             }
 
-         }
 
-         else
 
-         {
 
-             // delete xmle node
 
-             deleteNode(doc, node);
 
-         }
 
-     }
 
- #endif
 
-     char * encodedDefaultData = NULL;
 
-     unsigned int encodedDefaultDataLen = !defaultValue.isNull() ? base64Encode(defaultValue.getBytes(), defaultValue.getSize(), &encodedDefaultData) : 0;
 
-     string encodedStr = JniHelper::callStaticStringMethod(helperClassName, "getStringForKey", pKey, (const char*)encodedDefaultData);
 
-     if (encodedDefaultData)
 
-         free(encodedDefaultData);
 
-     CCLOG("ENCODED STRING: --%s--%d", encodedStr.c_str(), encodedStr.length());
 
-     unsigned char * decodedData = NULL;
 
-     int decodedDataLen = base64Decode((unsigned char*)encodedStr.c_str(), (unsigned int)encodedStr.length(), &decodedData);
 
-     CCLOG("DECODED DATA: %s %d", decodedData, decodedDataLen);
 
-     if (decodedData && decodedDataLen) {
 
-         Data ret;
 
-         ret.fastSet(decodedData, decodedDataLen);
 
-         return ret;
 
-     }
 
-     return defaultValue;
 
- }
 
- void UserDefault::setBoolForKey(const char* pKey, bool value)
 
- {
 
- #ifdef KEEP_COMPATABILITY
 
-     deleteNodeByKey(pKey);
 
- #endif
 
-     JniHelper::callStaticVoidMethod(helperClassName, "setBoolForKey", pKey, value);
 
- }
 
- void UserDefault::setIntegerForKey(const char* pKey, int value)
 
- {
 
- #ifdef KEEP_COMPATABILITY
 
-     deleteNodeByKey(pKey);
 
- #endif
 
-     JniHelper::callStaticVoidMethod(helperClassName, "setIntegerForKey", pKey, value);
 
- }
 
- void UserDefault::setFloatForKey(const char* pKey, float value)
 
- {
 
- #ifdef KEEP_COMPATABILITY
 
-     deleteNodeByKey(pKey);
 
- #endif
 
-     JniHelper::callStaticVoidMethod(helperClassName, "setFloatForKey", pKey, value);
 
- }
 
- void UserDefault::setDoubleForKey(const char* pKey, double value)
 
- {
 
- #ifdef KEEP_COMPATABILITY
 
-     deleteNodeByKey(pKey);
 
- #endif
 
-     JniHelper::callStaticVoidMethod(helperClassName, "setDoubleForKey", pKey, value);
 
- }
 
- void UserDefault::setStringForKey(const char* pKey, const std::string& value)
 
- {
 
- #ifdef KEEP_COMPATABILITY
 
-     deleteNodeByKey(pKey);
 
- #endif
 
-     JniHelper::callStaticVoidMethod(helperClassName, "setStringForKey", pKey, value);
 
- }
 
- void UserDefault::setDataForKey(const char* pKey, const Data& value)
 
- {
 
- #ifdef KEEP_COMPATABILITY
 
-     deleteNodeByKey(pKey);
 
- #endif
 
-     CCLOG("SET DATA FOR KEY: --%s--%d", value.getBytes(), (int)(value.getSize()));
 
-     char * encodedData = nullptr;
 
-     unsigned int encodedDataLen = base64Encode(value.getBytes(), value.getSize(), &encodedData);
 
-     CCLOG("SET DATA ENCODED: --%s", encodedData);
 
-     JniHelper::callStaticVoidMethod(helperClassName, "setStringForKey", pKey, (const char*)encodedData);
 
-     if (encodedData)
 
-         free(encodedData);
 
- }
 
- // FIXME:: deprecated
 
- UserDefault* UserDefault::sharedUserDefault()
 
- {
 
-     return UserDefault::getInstance();
 
- }
 
- UserDefault* UserDefault::getInstance()
 
- {
 
-     if (! _userDefault)
 
-     {
 
- #ifdef KEEP_COMPATABILITY
 
-         initXMLFilePath();
 
- #endif
 
-         _userDefault = new (std::nothrow) UserDefault();
 
-     }
 
-     return _userDefault;
 
- }
 
- bool UserDefault::isXMLFileExist()
 
- {
 
-     return FileUtils::getInstance()->isFileExist(_filePath);
 
- }
 
- void UserDefault::initXMLFilePath()
 
- {
 
- #ifdef KEEP_COMPATABILITY
 
-     if (! _isFilePathInitialized)
 
-     {
 
-         // UserDefault.xml is stored in /data/data/<package-path>/ before v2.1.2
 
-         std::string packageName = JniHelper::callStaticStringMethod(helperClassName, "getCocos2dxPackageName");
 
-         _filePath += "/data/data/" + packageName + "/" + XML_FILE_NAME;
 
-         _isFilePathInitialized = true;
 
-     }
 
- #endif
 
- }
 
- // create new xml file
 
- bool UserDefault::createXMLFile()
 
- {
 
-     return false;
 
- }
 
- const string& UserDefault::getXMLFilePath()
 
- {
 
-     return _filePath;
 
- }
 
- void UserDefault::flush()
 
- {
 
- }
 
- void UserDefault::deleteValueForKey(const char* key)
 
- {
 
-     // check the params
 
-     if (!key)
 
-     {
 
-         CCLOG("the key is invalid");
 
-     }
 
-     JniHelper::callStaticVoidMethod(helperClassName, "deleteValueForKey", key);
 
-     flush();
 
- }
 
- NS_CC_END
 
- #endif // (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
 
 
  |