From 6f3b977c1e695191dc89486018cc937b76851c5e Mon Sep 17 00:00:00 2001 From: jianglong0156 Date: Mon, 6 Jul 2015 17:01:17 +0800 Subject: [PATCH] add UserDefault api deleteValueForKey --- cocos/base/CCUserDefault-android.cpp | 88 +++-- cocos/base/CCUserDefault-apple.mm | 118 ++++--- cocos/base/CCUserDefault-winrt.cpp | 23 +- cocos/base/CCUserDefault.cpp | 331 ++++++++++-------- cocos/base/CCUserDefault.h | 9 +- .../src/org/cocos2dx/lib/Cocos2dxHelper.java | 7 + .../Java_org_cocos2dx_lib_Cocos2dxHelper.cpp | 13 + .../Java_org_cocos2dx_lib_Cocos2dxHelper.h | 2 +- .../UserDefaultTest/UserDefaultTest.cpp | 104 +++--- .../Classes/UserDefaultTest/UserDefaultTest.h | 3 + 10 files changed, 404 insertions(+), 294 deletions(-) diff --git a/cocos/base/CCUserDefault-android.cpp b/cocos/base/CCUserDefault-android.cpp index a6c7877323..162ae29729 100644 --- a/cocos/base/CCUserDefault-android.cpp +++ b/cocos/base/CCUserDefault-android.cpp @@ -60,24 +60,24 @@ static tinyxml2::XMLElement* getXMLNodeForKey(const char* pKey, tinyxml2::XMLDoc { 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 tinyxml2::XMLDocument(); *doc = xmlDoc; ssize_t size; - + std::string xmlBuffer = FileUtils::getInstance()->getStringFromFile(UserDefault::getInstance()->getXMLFilePath().c_str()); if (xmlBuffer.empty()) @@ -100,10 +100,10 @@ static tinyxml2::XMLElement* getXMLNodeForKey(const char* pKey, tinyxml2::XMLDoc { // There is not xml node, delete xml file. remove(UserDefault::getInstance()->getXMLFilePath().c_str()); - + return nullptr; } - + while (nullptr != curNode) { const char* nodeName = curNode->Value(); @@ -112,11 +112,11 @@ static tinyxml2::XMLElement* getXMLNodeForKey(const char* pKey, tinyxml2::XMLDoc // delete the node break; } - + curNode = curNode->NextSiblingElement(); } } while (0); - + return curNode; } @@ -173,14 +173,14 @@ bool UserDefault::getBoolForKey(const char* pKey, bool defaultValue) { 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 @@ -209,14 +209,14 @@ int UserDefault::getIntegerForKey(const char* pKey, int defaultValue) 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 @@ -226,7 +226,7 @@ int UserDefault::getIntegerForKey(const char* pKey, int defaultValue) } } #endif - + return getIntegerForKeyJNI(pKey, defaultValue); } @@ -245,14 +245,14 @@ float UserDefault::getFloatForKey(const char* pKey, float defaultValue) 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 @@ -281,14 +281,14 @@ double UserDefault::getDoubleForKey(const char* pKey, double defaultValue) 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 @@ -317,14 +317,14 @@ string UserDefault::getStringForKey(const char* pKey, const std::string & defaul 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 @@ -353,22 +353,22 @@ Data UserDefault::getDataForKey(const char* pKey, const Data& defaultValue) 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; } } @@ -379,22 +379,22 @@ Data UserDefault::getDataForKey(const char* pKey, const Data& defaultValue) } } #endif - + char * encodedDefaultData = NULL; unsigned int encodedDefaultDataLen = !defaultValue.isNull() ? base64Encode(defaultValue.getBytes(), defaultValue.getSize(), &encodedDefaultData) : 0; - + string encodedStr = getStringForKeyJNI(pKey, 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); @@ -455,15 +455,15 @@ 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); - + setStringForKeyJNI(pKey, encodedData); - + if (encodedData) free(encodedData); } @@ -475,7 +475,7 @@ UserDefault* UserDefault::sharedUserDefault() } UserDefault* UserDefault::getInstance() -{ +{ if (! _userDefault) { #ifdef KEEP_COMPATABILITY @@ -519,6 +519,18 @@ void UserDefault::flush() { } +void UserDefault::deleteValueForKey(const char* key) +{ + // check the params + if (!key) + { + CCLOG("the key is invalid"); + } + + deleteValueForKeyJNI(key); + + flush(); +} NS_CC_END #endif // (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) diff --git a/cocos/base/CCUserDefault-apple.mm b/cocos/base/CCUserDefault-apple.mm index 8923d9c31d..9476cbb420 100644 --- a/cocos/base/CCUserDefault-apple.mm +++ b/cocos/base/CCUserDefault-apple.mm @@ -1,19 +1,19 @@ /**************************************************************************** Copyright (c) 2010-2012 cocos2d-x.org Copyright (c) 2013-2014 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 @@ -61,18 +61,18 @@ static tinyxml2::XMLElement* getXMLNodeForKey(const char* pKey, tinyxml2::XMLDoc { 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 tinyxml2::XMLDocument(); @@ -100,10 +100,10 @@ static tinyxml2::XMLElement* getXMLNodeForKey(const char* pKey, tinyxml2::XMLDoc { // There is not xml node, delete xml file. remove(UserDefault::getInstance()->getXMLFilePath().c_str()); - + return nullptr; } - + while (nullptr != curNode) { const char* nodeName = curNode->Value(); @@ -112,11 +112,11 @@ static tinyxml2::XMLElement* getXMLNodeForKey(const char* pKey, tinyxml2::XMLDoc // delete the node break; } - + curNode = curNode->NextSiblingElement(); } } while (0); - + return curNode; } @@ -162,14 +162,14 @@ bool UserDefault::getBoolForKey(const char* pKey, bool defaultValue) { 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 @@ -179,15 +179,15 @@ bool UserDefault::getBoolForKey(const char* pKey, bool defaultValue) } } #endif - + bool ret = defaultValue; - + NSNumber *value = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithUTF8String:pKey]]; if (value) { ret = [value boolValue]; } - + return ret; } @@ -206,14 +206,14 @@ int UserDefault::getIntegerForKey(const char* pKey, int defaultValue) 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 @@ -223,15 +223,15 @@ int UserDefault::getIntegerForKey(const char* pKey, int defaultValue) } } #endif - + int ret = defaultValue; - + NSNumber *value = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithUTF8String:pKey]]; if (value) { ret = [value intValue]; } - + return ret; } @@ -250,14 +250,14 @@ float UserDefault::getFloatForKey(const char* pKey, float defaultValue) if (node->FirstChild()) { float ret = atof((const char*)node->FirstChild()->Value()); - + // set value in NSUserDefaults setFloatForKey(pKey, ret); flush(); - + // delete xmle node deleteNode(doc, node); - + return ret; } else @@ -267,15 +267,15 @@ float UserDefault::getFloatForKey(const char* pKey, float defaultValue) } } #endif - + float ret = defaultValue; - + NSNumber *value = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithUTF8String:pKey]]; if (value) { ret = [value floatValue]; } - + return ret; } @@ -294,14 +294,14 @@ double UserDefault::getDoubleForKey(const char* pKey, double defaultValue) if (node->FirstChild()) { double ret = atof((const char*)node->FirstChild()->Value()); - + // set value in NSUserDefaults setDoubleForKey(pKey, ret); flush(); - + // delete xmle node deleteNode(doc, node); - + return ret; } else @@ -311,15 +311,15 @@ double UserDefault::getDoubleForKey(const char* pKey, double defaultValue) } } #endif - + double ret = defaultValue; - + NSNumber *value = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithUTF8String:pKey]]; if (value) { ret = [value doubleValue]; } - + return ret; } @@ -338,14 +338,14 @@ string UserDefault::getStringForKey(const char* pKey, const std::string & defaul 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 @@ -355,7 +355,7 @@ string UserDefault::getStringForKey(const char* pKey, const std::string & defaul } } #endif - + NSString *str = [[NSUserDefaults standardUserDefaults] stringForKey:[NSString stringWithUTF8String:pKey]]; if (! str) { @@ -388,15 +388,15 @@ Data UserDefault::getDataForKey(const char* pKey, const Data& defaultValue) if (decodedData) { Data ret; ret.fastSet(decodedData, decodedDataLen); - + // set value in NSUserDefaults setDataForKey(pKey, ret); - + flush(); - + // delete xmle node deleteNode(doc, node); - + return ret; } } @@ -407,7 +407,7 @@ Data UserDefault::getDataForKey(const char* pKey, const Data& defaultValue) } } #endif - + NSData *data = [[NSUserDefaults standardUserDefaults] dataForKey:[NSString stringWithUTF8String:pKey]]; if (! data) { @@ -426,7 +426,7 @@ void UserDefault::setBoolForKey(const char* pKey, bool value) #ifdef KEEP_COMPATABILITY deleteNodeByKey(pKey); #endif - + [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:value] forKey:[NSString stringWithUTF8String:pKey]]; } @@ -435,7 +435,7 @@ void UserDefault::setIntegerForKey(const char* pKey, int value) #ifdef KEEP_COMPATABILITY deleteNodeByKey(pKey); #endif - + [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:value] forKey:[NSString stringWithUTF8String:pKey]]; } @@ -444,7 +444,7 @@ void UserDefault::setFloatForKey(const char* pKey, float value) #ifdef KEEP_COMPATABILITY deleteNodeByKey(pKey); #endif - + [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithFloat:value] forKey:[NSString stringWithUTF8String:pKey]]; } @@ -453,7 +453,7 @@ void UserDefault::setDoubleForKey(const char* pKey, double value) #ifdef KEEP_COMPATABILITY deleteNodeByKey(pKey); #endif - + [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithDouble:value] forKey:[NSString stringWithUTF8String:pKey]]; } @@ -462,7 +462,7 @@ void UserDefault::setStringForKey(const char* pKey, const std::string & value) #ifdef KEEP_COMPATABILITY deleteNodeByKey(pKey); #endif - + [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithUTF8String:value.c_str()] forKey:[NSString stringWithUTF8String:pKey]]; } @@ -470,12 +470,12 @@ void UserDefault::setDataForKey(const char* pKey, const Data& value) { #ifdef KEEP_COMPATABILITY deleteNodeByKey(pKey); #endif - + [[NSUserDefaults standardUserDefaults] setObject:[NSData dataWithBytes: value.getBytes() length: value.getSize()] forKey:[NSString stringWithUTF8String:pKey]]; } UserDefault* UserDefault::getInstance() -{ +{ if (! _userDefault) { #ifdef KEEP_COMPATABILITY @@ -483,7 +483,7 @@ UserDefault* UserDefault::getInstance() #endif _userDefault = new (std::nothrow) UserDefault(); } - + return _userDefault; } @@ -519,7 +519,7 @@ void UserDefault::initXMLFilePath() NSString *documentsDirectory = [paths objectAtIndex:0]; _filePath = [documentsDirectory UTF8String]; _filePath.append("/"); - + _filePath += XML_FILE_NAME; _isFilePathInitialized = true; } @@ -542,6 +542,18 @@ void UserDefault::flush() [[NSUserDefaults standardUserDefaults] synchronize]; } +void UserDefault::deleteValueForKey(const char* key) +{ + // check the params + if (!key) + { + CCLOG("the key is invalid"); + } + + [[NSUserDefaults standardUserDefaults] removeObjectForKey:[NSString stringWithUTF8String:key]]; + + flush(); +} NS_CC_END diff --git a/cocos/base/CCUserDefault-winrt.cpp b/cocos/base/CCUserDefault-winrt.cpp index 4279c67ecb..7c03a259e2 100644 --- a/cocos/base/CCUserDefault-winrt.cpp +++ b/cocos/base/CCUserDefault-winrt.cpp @@ -128,7 +128,7 @@ float UserDefault::getFloatForKey(const char* pKey) float UserDefault::getFloatForKey(const char* pKey, float defaultValue) { float ret = (float)getDoubleForKey(pKey, (double)defaultValue); - + return ret; } @@ -180,7 +180,7 @@ Data UserDefault::getDataForKey(const char* pKey, const Data& defaultValue) { unsigned char* decodedData = nullptr; int decodedDataLen = base64Decode((unsigned char*) encodedData.c_str(), (unsigned int) encodedData.length(), &decodedData); - if (decodedData && decodedDataLen > 0) + if (decodedData && decodedDataLen > 0) { ret.fastSet(decodedData, decodedDataLen); } @@ -247,7 +247,7 @@ void UserDefault::setDataForKey(const char* pKey, const Data& value) { char *encodedData = 0; base64Encode(value.getBytes(), static_cast(value.getSize()), &encodedData); - + setPlatformKeyValue(pKey, dynamic_cast(PropertyValue::CreateString(PlatformStringFromString(encodedData)))); if (encodedData) @@ -311,7 +311,7 @@ void UserDefault::initXMLFilePath() { _filePath += FileUtils::getInstance()->getWritablePath() + XML_FILE_NAME; _isFilePathInitialized = true; - } + } } // create new xml file @@ -329,6 +329,21 @@ void UserDefault::flush() { } +void UserDefault::deleteValueForKey(const char* key) +{ + // check the params + if (!key) + { + CCLOG("the key is invalid"); + } + + ApplicationDataContainer^ localSettings = ApplicationData::Current->LocalSettings; + auto values = localSettings->Values; + values->Remove(PlatformStringFromString(key)); + + flush(); +} + NS_CC_END #endif // (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) diff --git a/cocos/base/CCUserDefault.cpp b/cocos/base/CCUserDefault.cpp index fb72126cf1..880ef4d2a8 100644 --- a/cocos/base/CCUserDefault.cpp +++ b/cocos/base/CCUserDefault.cpp @@ -57,57 +57,57 @@ static tinyxml2::XMLElement* getXMLNodeForKey(const char* pKey, tinyxml2::XMLEle do { - tinyxml2::XMLDocument* xmlDoc = new tinyxml2::XMLDocument(); - *doc = xmlDoc; + tinyxml2::XMLDocument* xmlDoc = new tinyxml2::XMLDocument(); + *doc = xmlDoc; std::string xmlBuffer = FileUtils::getInstance()->getStringFromFile(UserDefault::getInstance()->getXMLFilePath()); - if (xmlBuffer.empty()) - { - CCLOG("can not read xml file"); - break; - } - xmlDoc->Parse(xmlBuffer.c_str(), xmlBuffer.size()); + if (xmlBuffer.empty()) + { + CCLOG("can not read xml file"); + break; + } + xmlDoc->Parse(xmlBuffer.c_str(), xmlBuffer.size()); - // get root node - *rootNode = xmlDoc->RootElement(); - if (nullptr == *rootNode) - { - CCLOG("read root node error"); - break; - } - // find the node - curNode = (*rootNode)->FirstChildElement(); - while (nullptr != curNode) - { - const char* nodeName = curNode->Value(); - if (!strcmp(nodeName, pKey)) - { - break; - } + // get root node + *rootNode = xmlDoc->RootElement(); + if (nullptr == *rootNode) + { + CCLOG("read root node error"); + break; + } + // find the node + curNode = (*rootNode)->FirstChildElement(); + while (nullptr != curNode) + { + const char* nodeName = curNode->Value(); + if (!strcmp(nodeName, pKey)) + { + break; + } - curNode = curNode->NextSiblingElement(); - } - } while (0); + curNode = curNode->NextSiblingElement(); + } + } while (0); - return curNode; + return curNode; } static void setValueForKey(const char* pKey, const char* pValue) { - tinyxml2::XMLElement* rootNode; - tinyxml2::XMLDocument* doc; - tinyxml2::XMLElement* node; - // check the params - if (! pKey || ! pValue) - { - return; - } - // find the node - node = getXMLNodeForKey(pKey, &rootNode, &doc); - // if node exist, change the content - if (node) - { + tinyxml2::XMLElement* rootNode; + tinyxml2::XMLDocument* doc; + tinyxml2::XMLElement* node; + // check the params + if (! pKey || ! pValue) + { + return; + } + // find the node + node = getXMLNodeForKey(pKey, &rootNode, &doc); + // if node exist, change the content + if (node) + { if (node->FirstChild()) { node->FirstChild()->SetValue(pValue); @@ -117,24 +117,24 @@ static void setValueForKey(const char* pKey, const char* pValue) tinyxml2::XMLText* content = doc->NewText(pValue); node->LinkEndChild(content); } - } - else - { - if (rootNode) - { - tinyxml2::XMLElement* tmpNode = doc->NewElement(pKey);//new tinyxml2::XMLElement(pKey); - rootNode->LinkEndChild(tmpNode); - tinyxml2::XMLText* content = doc->NewText(pValue);//new tinyxml2::XMLText(pValue); - tmpNode->LinkEndChild(content); - } - } + } + else + { + if (rootNode) + { + tinyxml2::XMLElement* tmpNode = doc->NewElement(pKey);//new tinyxml2::XMLElement(pKey); + rootNode->LinkEndChild(tmpNode); + tinyxml2::XMLText* content = doc->NewText(pValue);//new tinyxml2::XMLText(pValue); + tmpNode->LinkEndChild(content); + } + } // save file and free doc - if (doc) - { + if (doc) + { doc->SaveFile(FileUtils::getInstance()->getSuitableFOpen(UserDefault::getInstance()->getXMLFilePath()).c_str()); - delete doc; - } + delete doc; + } } /** @@ -161,26 +161,26 @@ bool UserDefault::getBoolForKey(const char* pKey) bool UserDefault::getBoolForKey(const char* pKey, bool defaultValue) { const char* value = nullptr; - tinyxml2::XMLElement* rootNode; - tinyxml2::XMLDocument* doc; - tinyxml2::XMLElement* node; - node = getXMLNodeForKey(pKey, &rootNode, &doc); - // find the node - if (node && node->FirstChild()) - { + tinyxml2::XMLElement* rootNode; + tinyxml2::XMLDocument* doc; + tinyxml2::XMLElement* node; + node = getXMLNodeForKey(pKey, &rootNode, &doc); + // find the node + if (node && node->FirstChild()) + { value = (const char*)(node->FirstChild()->Value()); - } + } - bool ret = defaultValue; + bool ret = defaultValue; - if (value) - { - ret = (! strcmp(value, "true")); - } + if (value) + { + ret = (! strcmp(value, "true")); + } if (doc) delete doc; - return ret; + return ret; } int UserDefault::getIntegerForKey(const char* pKey) @@ -190,31 +190,31 @@ int UserDefault::getIntegerForKey(const char* pKey) int UserDefault::getIntegerForKey(const char* pKey, int defaultValue) { - const char* value = nullptr; - tinyxml2::XMLElement* rootNode; - tinyxml2::XMLDocument* doc; - tinyxml2::XMLElement* node; - node = getXMLNodeForKey(pKey, &rootNode, &doc); - // find the node - if (node && node->FirstChild()) - { + const char* value = nullptr; + tinyxml2::XMLElement* rootNode; + tinyxml2::XMLDocument* doc; + tinyxml2::XMLElement* node; + node = getXMLNodeForKey(pKey, &rootNode, &doc); + // find the node + if (node && node->FirstChild()) + { value = (const char*)(node->FirstChild()->Value()); - } + } - int ret = defaultValue; + int ret = defaultValue; - if (value) - { - ret = atoi(value); - } + if (value) + { + ret = atoi(value); + } - if(doc) - { - delete doc; - } + if(doc) + { + delete doc; + } - return ret; + return ret; } float UserDefault::getFloatForKey(const char* pKey) @@ -236,27 +236,27 @@ double UserDefault::getDoubleForKey(const char* pKey) double UserDefault::getDoubleForKey(const char* pKey, double defaultValue) { - const char* value = nullptr; - tinyxml2::XMLElement* rootNode; - tinyxml2::XMLDocument* doc; - tinyxml2::XMLElement* node; - node = getXMLNodeForKey(pKey, &rootNode, &doc); - // find the node - if (node && node->FirstChild()) - { + const char* value = nullptr; + tinyxml2::XMLElement* rootNode; + tinyxml2::XMLDocument* doc; + tinyxml2::XMLElement* node; + node = getXMLNodeForKey(pKey, &rootNode, &doc); + // find the node + if (node && node->FirstChild()) + { value = (const char*)(node->FirstChild()->Value()); - } + } - double ret = defaultValue; + double ret = defaultValue; - if (value) - { - ret = utils::atof(value); - } + if (value) + { + ret = utils::atof(value); + } if (doc) delete doc; - return ret; + return ret; } std::string UserDefault::getStringForKey(const char* pKey) @@ -267,26 +267,26 @@ std::string UserDefault::getStringForKey(const char* pKey) string UserDefault::getStringForKey(const char* pKey, const std::string & defaultValue) { const char* value = nullptr; - tinyxml2::XMLElement* rootNode; - tinyxml2::XMLDocument* doc; - tinyxml2::XMLElement* node; - node = getXMLNodeForKey(pKey, &rootNode, &doc); - // find the node - if (node && node->FirstChild()) - { + tinyxml2::XMLElement* rootNode; + tinyxml2::XMLDocument* doc; + tinyxml2::XMLElement* node; + node = getXMLNodeForKey(pKey, &rootNode, &doc); + // find the node + if (node && node->FirstChild()) + { value = (const char*)(node->FirstChild()->Value()); - } + } - string ret = defaultValue; + string ret = defaultValue; - if (value) - { - ret = string(value); - } + if (value) + { + ret = string(value); + } if (doc) delete doc; - return ret; + return ret; } Data UserDefault::getDataForKey(const char* pKey) @@ -297,31 +297,31 @@ Data UserDefault::getDataForKey(const char* pKey) Data UserDefault::getDataForKey(const char* pKey, const Data& defaultValue) { const char* encodedData = nullptr; - tinyxml2::XMLElement* rootNode; - tinyxml2::XMLDocument* doc; - tinyxml2::XMLElement* node; - node = getXMLNodeForKey(pKey, &rootNode, &doc); - // find the node - if (node && node->FirstChild()) - { + tinyxml2::XMLElement* rootNode; + tinyxml2::XMLDocument* doc; + tinyxml2::XMLElement* node; + node = getXMLNodeForKey(pKey, &rootNode, &doc); + // find the node + if (node && node->FirstChild()) + { encodedData = (const char*)(node->FirstChild()->Value()); - } + } - Data ret = defaultValue; + Data ret = defaultValue; - if (encodedData) - { + if (encodedData) + { unsigned char * decodedData = nullptr; int decodedDataLen = base64Decode((unsigned char*)encodedData, (unsigned int)strlen(encodedData), &decodedData); if (decodedData) { ret.fastSet(decodedData, decodedDataLen); } - } + } if (doc) delete doc; - return ret; + return ret; } @@ -466,32 +466,32 @@ void UserDefault::initXMLFilePath() // create new xml file bool UserDefault::createXMLFile() { - bool bRet = false; + bool bRet = false; tinyxml2::XMLDocument *pDoc = new tinyxml2::XMLDocument(); if (nullptr==pDoc) { return false; } - tinyxml2::XMLDeclaration *pDeclaration = pDoc->NewDeclaration(nullptr); - if (nullptr==pDeclaration) - { - return false; - } - pDoc->LinkEndChild(pDeclaration); - tinyxml2::XMLElement *pRootEle = pDoc->NewElement(USERDEFAULT_ROOT_NAME); - if (nullptr==pRootEle) - { - return false; - } - pDoc->LinkEndChild(pRootEle); + tinyxml2::XMLDeclaration *pDeclaration = pDoc->NewDeclaration(nullptr); + if (nullptr==pDeclaration) + { + return false; + } + pDoc->LinkEndChild(pDeclaration); + tinyxml2::XMLElement *pRootEle = pDoc->NewElement(USERDEFAULT_ROOT_NAME); + if (nullptr==pRootEle) + { + return false; + } + pDoc->LinkEndChild(pRootEle); bRet = tinyxml2::XML_SUCCESS == pDoc->SaveFile(FileUtils::getInstance()->getSuitableFOpen(_filePath).c_str()); - if(pDoc) - { - delete pDoc; - } + if(pDoc) + { + delete pDoc; + } - return bRet; + return bRet; } const string& UserDefault::getXMLFilePath() @@ -503,6 +503,39 @@ void UserDefault::flush() { } +void UserDefault::deleteValueForKey(const char* key) +{ + tinyxml2::XMLElement* rootNode; + tinyxml2::XMLDocument* doc; + tinyxml2::XMLElement* node; + + // check the params + if (!key) + { + CCLOG("the key is invalid"); + return; + } + + // find the node + node = getXMLNodeForKey(key, &rootNode, &doc); + + // if node not exist, don't need to delete + if (!node) + { + return; + } + + // save file and free doc + if (doc) + { + doc->DeleteNode(node); + doc->SaveFile(FileUtils::getInstance()->getSuitableFOpen(UserDefault::getInstance()->getXMLFilePath()).c_str()); + delete doc; + } + + flush(); +} + NS_CC_END #endif // (CC_TARGET_PLATFORM != CC_PLATFORM_IOS && CC_PLATFORM != CC_PLATFORM_ANDROID) diff --git a/cocos/base/CCUserDefault.h b/cocos/base/CCUserDefault.h index 5d080cbdb9..af9825aa64 100644 --- a/cocos/base/CCUserDefault.h +++ b/cocos/base/CCUserDefault.h @@ -202,6 +202,13 @@ public: */ virtual void flush(); + /** + * delete any value by key, + * @param key The key to delete value. + * @js NA + */ + virtual void deleteValueForKey(const char* key); + /** Returns the singleton. * @js NA * @lua NA @@ -238,7 +245,7 @@ public: */ static const std::string& getXMLFilePath(); /** All supported platforms other iOS & Android and CC_PLATFORM_WINRT use xml file to save values. This function checks whether the xml file exists or not. - * @return True if the xml file exists, flase if not. + * @return True if the xml file exists, false if not. * @js NA */ static bool isXMLFileExist(); diff --git a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java index 187a7a3853..6eefcd775a 100644 --- a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java +++ b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java @@ -526,6 +526,13 @@ public class Cocos2dxHelper { editor.commit(); } + public static void deleteValueForKey(String key) { + SharedPreferences settings = sActivity.getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0); + SharedPreferences.Editor editor = settings.edit(); + editor.remove(key); + editor.commit(); + } + // =========================================================== // Inner and Anonymous Classes // =========================================================== diff --git a/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.cpp b/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.cpp index 6530c28b82..f482ae71d3 100644 --- a/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.cpp +++ b/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.cpp @@ -400,3 +400,16 @@ void setStringForKeyJNI(const char* key, const char* value) t.env->DeleteLocalRef(stringArg2); } } + +void deleteValueForKeyJNI(const char* key) +{ + JniMethodInfo t; + + if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "deleteValueForKey", "(Ljava/lang/String;)V")) { + jstring stringArg1 = t.env->NewStringUTF(key); + t.env->CallStaticVoidMethod(t.classID, t.methodID, stringArg1); + + t.env->DeleteLocalRef(t.classID); + t.env->DeleteLocalRef(stringArg1); + } +} diff --git a/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h b/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h index 042ccc5a11..5770c265ff 100644 --- a/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h +++ b/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h @@ -53,5 +53,5 @@ extern void setIntegerForKeyJNI(const char* key, int value); extern void setFloatForKeyJNI(const char* key, float value); extern void setDoubleForKeyJNI(const char* key, double value); extern void setStringForKeyJNI(const char* key, const char* value); - +extern void deleteValueForKeyJNI(const char* key); #endif /* __Java_org_cocos2dx_lib_Cocos2dxHelper_H__ */ diff --git a/tests/cpp-tests/Classes/UserDefaultTest/UserDefaultTest.cpp b/tests/cpp-tests/Classes/UserDefaultTest/UserDefaultTest.cpp index 8891f5085a..fa80e3b761 100644 --- a/tests/cpp-tests/Classes/UserDefaultTest/UserDefaultTest.cpp +++ b/tests/cpp-tests/Classes/UserDefaultTest/UserDefaultTest.cpp @@ -21,10 +21,16 @@ UserDefaultTests::UserDefaultTests() UserDefaultTest::UserDefaultTest() { auto s = Director::getInstance()->getWinSize(); - auto label = Label::createWithTTF("CCUserDefault test see log", "fonts/arial.ttf", 28); + auto label = Label::createWithTTF("CCUserDefault test Log data see console", "fonts/arial.ttf", 22); addChild(label, 0); label->setPosition( Vec2(s.width/2, s.height-50) ); + this->_label = Label::createWithTTF("result", "fonts/arial.ttf", 12); + addChild(this->_label, 0); + + label->setPosition(Vec2(s.width / 2, s.height - 50)); + this->_label->setPosition(Vec2(s.width / 2, s.height / 2)); + doTest(); } @@ -75,7 +81,7 @@ void setData2(const char* key) void UserDefaultTest::doTest() { - CCLOG("********************** init value ***********************"); + this->_label->setString(this->_label->getString() + "\n" + "********************** init value ***********************"); // set default value @@ -90,29 +96,7 @@ void UserDefaultTest::doTest() setData("float_data"); setData("double_data"); - // print value - - std::string ret = UserDefault::getInstance()->getStringForKey("string"); - CCLOG("string is %s", ret.c_str()); - - double d = UserDefault::getInstance()->getDoubleForKey("double"); - CCLOG("double is %f", d); - - int i = UserDefault::getInstance()->getIntegerForKey("integer"); - CCLOG("integer is %d", i); - - float f = UserDefault::getInstance()->getFloatForKey("float"); - CCLOG("float is %f", f); - - bool b = UserDefault::getInstance()->getBoolForKey("bool"); - if (b) - { - CCLOG("bool is true"); - } - else - { - CCLOG("bool is false"); - } + printValue(); logData("int_data"); logData("float_data"); @@ -120,7 +104,7 @@ void UserDefaultTest::doTest() //CCUserDefault::getInstance()->flush(); - CCLOG("********************** after change value ***********************"); + this->_label->setString(this->_label->getString() + "\n" + "********************** after change value ***********************"); // change the value @@ -137,35 +121,59 @@ void UserDefaultTest::doTest() UserDefault::getInstance()->flush(); // print value - - ret = UserDefault::getInstance()->getStringForKey("string"); - CCLOG("string is %s", ret.c_str()); - - d = UserDefault::getInstance()->getDoubleForKey("double"); - CCLOG("double is %f", d); - - i = UserDefault::getInstance()->getIntegerForKey("integer"); - CCLOG("integer is %d", i); - - f = UserDefault::getInstance()->getFloatForKey("float"); - CCLOG("float is %f", f); - - b = UserDefault::getInstance()->getBoolForKey("bool"); - if (b) - { - CCLOG("bool is true"); - } - else - { - CCLOG("bool is false"); - } + printValue(); logData("int_data"); logData("float_data"); logData("double_data"); + + this->_label->setString(this->_label->getString() + "\n" + "********************** after delete value ***********************"); + + UserDefault::getInstance()->deleteValueForKey("string"); + UserDefault::getInstance()->deleteValueForKey("integer"); + UserDefault::getInstance()->deleteValueForKey("float"); + UserDefault::getInstance()->deleteValueForKey("double"); + UserDefault::getInstance()->deleteValueForKey("bool"); + + // print value + printValue(); } +void UserDefaultTest::printValue() +{ + char strTemp[256] = ""; + // print value + std::string ret = UserDefault::getInstance()->getStringForKey("string"); + sprintf(strTemp, "string is %s", ret.c_str()); + this->_label->setString(this->_label->getString() + "\n" + strTemp); + + double d = UserDefault::getInstance()->getDoubleForKey("double"); + sprintf(strTemp, "double is %f", d); + this->_label->setString(this->_label->getString() + "\n" + strTemp); + + int i = UserDefault::getInstance()->getIntegerForKey("integer"); + sprintf(strTemp, "integer is %d", i); + this->_label->setString(this->_label->getString() + "\n" + strTemp); + + float f = UserDefault::getInstance()->getFloatForKey("float"); + sprintf(strTemp, "float is %f", f); + this->_label->setString(this->_label->getString() + "\n" + strTemp); + + bool b = UserDefault::getInstance()->getBoolForKey("bool"); + if (b) + { + sprintf(strTemp, "bool is true"); + this->_label->setString(this->_label->getString() + "\n" + strTemp); + } + else + { + sprintf(strTemp, "bool is false"); + this->_label->setString(this->_label->getString() + "\n" + strTemp); + } +} UserDefaultTest::~UserDefaultTest() { } + + diff --git a/tests/cpp-tests/Classes/UserDefaultTest/UserDefaultTest.h b/tests/cpp-tests/Classes/UserDefaultTest/UserDefaultTest.h index bec4560638..d3d10045b9 100644 --- a/tests/cpp-tests/Classes/UserDefaultTest/UserDefaultTest.h +++ b/tests/cpp-tests/Classes/UserDefaultTest/UserDefaultTest.h @@ -3,6 +3,7 @@ #include "cocos2d.h" #include "../BaseTest.h" +#include "2d/CCLabel.h" DEFINE_TEST_SUITE(UserDefaultTests); @@ -15,6 +16,8 @@ public: private: void doTest(); + void printValue(); + cocos2d::Label* _label; }; #endif // _USERDEFAULT_TEST_H_