Created
September 3, 2014 10:08
-
-
Save rubentorresbonet/808e61d7fc0d4147221b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* DDVersioning.cpp | |
* | |
* Created on: 22.08.2014 | |
* Author: ruben.torres | |
*/ | |
#include <boost/algorithm/string/predicate.hpp> | |
#include <cocos2d.h> | |
#include "DDVersionManager.h" | |
// -------------------------------------------------------------------------------------------- | |
// -------------------------------------------------------------------------------------------- | |
// -------------------------------------------------------------------------------------------- | |
DDVersion::VersionName | |
DDVersion::getVersionNameByCode(std::string versionCode) | |
{ | |
DDVersion::VersionName versionName = DDVersion::VersionName::UNKNOWN; | |
if (boost::starts_with(versionCode, "30")) { | |
versionName = DDVersion::VersionName::GLOBAL_TOURNAMENT_3_0; | |
} | |
else if (boost::starts_with(versionCode, "31")) { | |
versionName = DDVersion::VersionName::FIFTH_BOOST_3_1; | |
} | |
// ... | |
return versionName; | |
} | |
// -------------------------------------------------------------------------------------------- | |
void | |
DDVersionManager::init() | |
{ | |
CCPAssert(currentVersion == nullptr, "DDVersionManager::hasUpdated(): already initialized"); | |
// 1. Current version. | |
std::string currentVersionCode = JNICallStringVoid(JAVA_PACKAGE_NAME JAVA_CLASS_DD, "getVersionCode"); // Use your custom JNI method | |
DDVersion::VersionName currentVersionName = DDVersion::getVersionNameByCode(currentVersionCode); | |
this->currentVersion.reset(new DDVersion()); | |
currentVersion->code = currentVersionCode; | |
currentVersion->name = currentVersionName; | |
// 2. Older version. | |
cocos2d::CCUserDefault *sharedUserDefault = cocos2d::CCUserDefault::sharedUserDefault(); // Use your custom data storage. | |
std::string oldVersionCode = sharedUserDefault->getStringForKey( LAST_LAUNCHED_VERSION_CODE, ""); | |
if (oldVersionCode == "" || (oldVersionCode.length() > 2 && oldVersionCode != currentVersionCode)) { // If it's empty it means either: A) the user deleted the data or B) the version he had didn't have this feature so it's still an update. | |
this->oldVersion.reset(new DDVersion()); | |
oldVersion->code = oldVersionCode; | |
oldVersion->name = DDVersion::getVersionNameByCode(oldVersionCode); | |
} | |
// 3. Now that we saved both the current and the old version in memory, we update the permanent storage to set the current version as the older one. | |
// Hopefully we will do our stuff with the version transitions before the application crashes. | |
sharedUserDefault->setStringForKey(LAST_LAUNCHED_VERSION_CODE, currentVersionCode); | |
sharedUserDefault->flush(); | |
} | |
// -------------------------------------------------------------------------------------------- | |
bool | |
DDVersionManager::hasUpdated(UpdateType updateType) | |
{ | |
CCPAssert(currentVersion, "DDVersionManager::hasUpdated(): currentVersion null"); | |
if (currentVersion == nullptr || oldVersion == nullptr) { | |
return false; | |
} | |
if (oldVersion->name == DDVersion::VersionName::UNKNOWN) { // It is an old version which didn't have this version system. Therefore we updated. | |
return true; | |
} | |
if (updateType == UpdateType::MAJOR) { | |
return (currentVersion->code[0] > oldVersion->code[0]); | |
} else if (updateType == UpdateType::MINOR) { | |
return (currentVersion->code[1] > oldVersion->code[1]); | |
} else if (updateType == UpdateType::PATCH) { | |
return (currentVersion->code[2] > oldVersion->code[2]); | |
} else if (updateType == UpdateType::ANY) { | |
return *oldVersion.get() < *currentVersion.get(); | |
} | |
CCPAssert(false, "DDVersionManager::hasUpdated(): should not happen"); | |
return false; | |
} | |
// -------------------------------------------------------------------------------------------- | |
DDVersion::VersionName | |
DDVersionManager::getOldVersionName() { | |
if (oldVersion) { | |
return oldVersion->name; | |
} | |
return DDVersion::VersionName::UNKNOWN; | |
} | |
// -------------------------------------------------------------------------------------------- | |
DDVersion::VersionName | |
DDVersionManager::getCurrentVersionName() { | |
if (currentVersion) { | |
return currentVersion->name; | |
} | |
return DDVersion::VersionName::UNKNOWN; | |
} | |
// -------------------------------------------------------------------------------------------- | |
bool operator< (DDVersion &v1, DDVersion &v2) { | |
int n1 = boost::lexical_cast<int>(v1.code); | |
int n2 = boost::lexical_cast<int>(v2.code); | |
return n1 < n2; | |
} | |
// -------------------------------------------------------------------------------------------- | |
// -------------------------------------------------------------------------------------------- | |
// -------------------------------------------------------------------------------------------- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* DDVersionManager.h | |
* | |
* Created on: 22.08.2014 | |
* Author: ruben.torres | |
*/ | |
#pragma once | |
#include <boost/lexical_cast.hpp> | |
#include <memory> | |
class DDVersion { | |
public: | |
DDVersion() = default; | |
~DDVersion() = default; | |
enum class VersionName { | |
UNKNOWN = 0, | |
GLOBAL_TOURNAMENT_3_0 = 10, | |
FIFTH_BOOST_3_1, // ... | |
}; | |
/** | |
* Example: 320030 | |
*/ | |
std::string code; | |
/** | |
* Our friendly way of naming versions. | |
*/ | |
VersionName name; | |
static VersionName getVersionNameByCode(std::string versionCode); | |
friend bool operator< (DDVersion &v1, DDVersion &v2); | |
}; | |
/** | |
* This class keeps the track of the user's DD updates. | |
* | |
* The current version will always be the version that is presently running. | |
* The old version is optional and will be set only during the first app launch after it's been updated. After next launches it will be empty again. | |
*/ | |
class DDVersionManager { | |
public: | |
/** | |
* For 341000 | |
*/ | |
enum class UpdateType { | |
MAJOR, // 3 | |
MINOR, // 2 | |
PATCH, // 1 | |
ANY // 3, 2 or 1 | |
}; | |
DDVersionManager() = default; | |
~DDVersionManager() = default; | |
/** | |
* To be called only once. It initializes the versions. | |
*/ | |
void init(); | |
/** | |
* It returns whether the application just updated. Will be true as long as we don't start it again. | |
* @return | |
*/ | |
bool hasUpdated(UpdateType updateType = UpdateType::ANY); | |
/** | |
* Returns the old version name. | |
* @return | |
*/ | |
DDVersion::VersionName getOldVersionName(); | |
/** | |
* Returns the old version name. | |
* @return | |
*/ | |
DDVersion::VersionName getCurrentVersionName(); | |
private: | |
/** | |
* Where do we update from, if any. | |
*/ | |
std::unique_ptr<DDVersion> oldVersion; | |
/** | |
* Which version we're running at the moment. | |
*/ | |
std::unique_ptr<DDVersion> currentVersion; | |
}; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Gets the version code. Example: 320030 | |
* @return | |
*/ | |
public static String getVersionCode() | |
{ | |
try | |
{ | |
PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0); | |
return String.valueOf(pInfo.versionCode); | |
} | |
catch( NameNotFoundException e ) | |
{ | |
Log.d("DiamondDash", "Could not retrieve DiamondDash version."); | |
} | |
return ""; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment