diff --git a/cocos/platform/CCApplicationProtocol.h b/cocos/platform/CCApplicationProtocol.h index 2b78c0b92f..9ac60baa2b 100644 --- a/cocos/platform/CCApplicationProtocol.h +++ b/cocos/platform/CCApplicationProtocol.h @@ -136,6 +136,13 @@ public: */ virtual Platform getTargetPlatform() = 0; + /** + @brief Get application version. + * @js NA + * @lua NA + */ + virtual std::string getVersion() = 0; + /** @brief Open url in default browser. @param String with url to open. diff --git a/cocos/platform/android/CCApplication-android.cpp b/cocos/platform/android/CCApplication-android.cpp index 8d802a1814..b3f0d18bef 100644 --- a/cocos/platform/android/CCApplication-android.cpp +++ b/cocos/platform/android/CCApplication-android.cpp @@ -200,6 +200,11 @@ Application::Platform Application::getTargetPlatform() return Platform::OS_ANDROID; } +std::string Application::getVersion() +{ + return getVersionJNI(); +} + bool Application::openURL(const std::string &url) { return openURLJNI(url.c_str()); diff --git a/cocos/platform/android/CCApplication-android.h b/cocos/platform/android/CCApplication-android.h index d1f8c47c48..3555255d0e 100644 --- a/cocos/platform/android/CCApplication-android.h +++ b/cocos/platform/android/CCApplication-android.h @@ -84,6 +84,11 @@ public: */ virtual Platform getTargetPlatform(); + /** + @brief Get application version. + */ + virtual std::string getVersion() override; + /** @brief Open url in default browser @param String with url to open. 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 8bd35f4980..282382da6f 100644 --- a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java +++ b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java @@ -212,6 +212,15 @@ public class Cocos2dxHelper { sVibrateService.vibrate((long)(duration * 1000)); } + public static String getVersion() { + try { + String version = Cocos2dxActivity.getContext().getPackageManager().getPackageInfo(Cocos2dxActivity.getContext().getPackageName(), 0).versionName; + return version; + } catch(Exception e) { + return ""; + } + } + public static boolean openURL(String url) { boolean ret = false; try { 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 cbd1285e05..4a6b0a39e4 100644 --- a/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.cpp +++ b/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.cpp @@ -205,6 +205,19 @@ void vibrateJni(float duration) { } } +std::string getVersionJNI() { + JniMethodInfo t; + std::string ret(""); + + if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "getVersion", "()Ljava/lang/String;")) { + jstring str = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID); + t.env->DeleteLocalRef(t.classID); + ret = JniHelper::jstring2string(str); + t.env->DeleteLocalRef(str); + } + return ret; +} + extern bool openURLJNI(const char* url) { JniMethodInfo t; 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 534a35669c..c9f50994cc 100644 --- a/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h +++ b/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h @@ -40,6 +40,7 @@ extern void disableAccelerometerJni(); extern void setAccelerometerIntervalJni(float interval); extern void setKeepScreenOnJni(bool value); extern void vibrateJni(float duration); +extern std::string getVersionJNI(); extern bool openURLJNI(const char* url); // functions for UserDefault extern bool getBoolForKeyJNI(const char* key, bool defaultValue); diff --git a/cocos/platform/ios/CCApplication-ios.h b/cocos/platform/ios/CCApplication-ios.h index ae93c83d4d..ea9f95d59a 100644 --- a/cocos/platform/ios/CCApplication-ios.h +++ b/cocos/platform/ios/CCApplication-ios.h @@ -67,31 +67,36 @@ public: @brief Callback by Director for limit FPS. @param interval The time, expressed in seconds, between current frame and next. */ - virtual void setAnimationInterval(float interval); + virtual void setAnimationInterval(float interval) override; /** @brief Get current language config @return Current language config */ - virtual LanguageType getCurrentLanguage(); + virtual LanguageType getCurrentLanguage() override; /** @brief Get current language iso 639-1 code @return Current language iso 639-1 code */ - virtual const char * getCurrentLanguageCode(); + virtual const char * getCurrentLanguageCode() override; /** @brief Get target platform */ - virtual Platform getTargetPlatform(); + virtual Platform getTargetPlatform() override; + + /** + @brief Get application version. + */ + virtual std::string getVersion() override; /** @brief Open url in default browser @param String with url to open. @return true if the resource located by the URL was successfully opened; otherwise false. */ - virtual bool openURL(const std::string &url); + virtual bool openURL(const std::string &url) override; /** @brief This function will be called when the application screen size is changed. diff --git a/cocos/platform/ios/CCApplication-ios.mm b/cocos/platform/ios/CCApplication-ios.mm index 7411478060..69bc93c895 100644 --- a/cocos/platform/ios/CCApplication-ios.mm +++ b/cocos/platform/ios/CCApplication-ios.mm @@ -139,6 +139,14 @@ Application::Platform Application::getTargetPlatform() } } +std::string Application::getVersion() { + NSString* version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]; + if (version) { + return [version UTF8String]; + } + return ""; +} + bool Application::openURL(const std::string &url) { NSString* msg = [NSString stringWithCString:url.c_str() encoding:NSUTF8StringEncoding]; diff --git a/cocos/platform/linux/CCApplication-linux.cpp b/cocos/platform/linux/CCApplication-linux.cpp index 12f1e89228..8f19a553fb 100644 --- a/cocos/platform/linux/CCApplication-linux.cpp +++ b/cocos/platform/linux/CCApplication-linux.cpp @@ -136,6 +136,11 @@ Application::Platform Application::getTargetPlatform() return Platform::OS_LINUX; } +std::string Application::getVersion() +{ + return ""; +} + bool Application::openURL(const std::string &url) { std::string op = std::string("open ").append(url); diff --git a/cocos/platform/linux/CCApplication-linux.h b/cocos/platform/linux/CCApplication-linux.h index c827023af1..d96cb61115 100644 --- a/cocos/platform/linux/CCApplication-linux.h +++ b/cocos/platform/linux/CCApplication-linux.h @@ -78,6 +78,11 @@ public: */ virtual const char * getCurrentLanguageCode(); + /** + @brief Get application version + */ + virtual std::string getVersion() override; + /** @brief Open url in default browser @param String with url to open. diff --git a/cocos/platform/mac/CCApplication-mac.h b/cocos/platform/mac/CCApplication-mac.h index 2a243a7624..c056d73fe3 100644 --- a/cocos/platform/mac/CCApplication-mac.h +++ b/cocos/platform/mac/CCApplication-mac.h @@ -52,7 +52,7 @@ public: @brief Callback by Director for limit FPS. @param interval The time, which expressed in second in second, between current frame and next. */ - virtual void setAnimationInterval(float interval); + virtual void setAnimationInterval(float interval) override; /** @brief Get status bar rectangle in GLView window. @@ -78,25 +78,30 @@ public: @brief Get current language config @return Current language config */ - virtual LanguageType getCurrentLanguage(); + virtual LanguageType getCurrentLanguage() override; /** @brief Get current language iso 639-1 code @return Current language iso 639-1 code */ - virtual const char * getCurrentLanguageCode(); + virtual const char * getCurrentLanguageCode() override; /** @brief Get target platform */ - virtual Platform getTargetPlatform(); + virtual Platform getTargetPlatform() override; + /** + @brief Get application version. + */ + virtual std::string getVersion() override; + /** @brief Open url in default browser @param String with url to open. @return true if the resource located by the URL was successfully opened; otherwise false. */ - virtual bool openURL(const std::string &url); + virtual bool openURL(const std::string &url) override; /** * Sets the Resource root path. diff --git a/cocos/platform/mac/CCApplication-mac.mm b/cocos/platform/mac/CCApplication-mac.mm index 1666776a5c..47c55a8b07 100644 --- a/cocos/platform/mac/CCApplication-mac.mm +++ b/cocos/platform/mac/CCApplication-mac.mm @@ -118,6 +118,14 @@ Application::Platform Application::getTargetPlatform() return Platform::OS_MAC; } +std::string Application::getVersion() { + NSString* version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]; + if (version) { + return [version UTF8String]; + } + return ""; +} + ///////////////////////////////////////////////////////////////////////////////////////////////// // static member function ////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/cocos/platform/win32/CCApplication-win32.cpp b/cocos/platform/win32/CCApplication-win32.cpp index 9ee4b56f3f..1c752094ef 100644 --- a/cocos/platform/win32/CCApplication-win32.cpp +++ b/cocos/platform/win32/CCApplication-win32.cpp @@ -217,6 +217,11 @@ Application::Platform Application::getTargetPlatform() return Platform::OS_WINDOWS; } +std::string Application::getVersion() +{ + return ""; +} + bool Application::openURL(const std::string &url) { WCHAR *temp = new WCHAR[url.size() + 1]; diff --git a/cocos/platform/win32/CCApplication-win32.h b/cocos/platform/win32/CCApplication-win32.h index 71013eca9e..84b858c706 100644 --- a/cocos/platform/win32/CCApplication-win32.h +++ b/cocos/platform/win32/CCApplication-win32.h @@ -75,6 +75,11 @@ public: */ virtual Platform getTargetPlatform(); + /** + @brief Get application version + */ + virtual std::string getVersion() override; + /** @brief Open url in default browser @param String with url to open. diff --git a/cocos/platform/winrt/CCApplication.cpp b/cocos/platform/winrt/CCApplication.cpp index 374445a554..65a583994a 100644 --- a/cocos/platform/winrt/CCApplication.cpp +++ b/cocos/platform/winrt/CCApplication.cpp @@ -36,6 +36,7 @@ using namespace Windows::Foundation; #include "platform/CCFileUtils.h" #include "CCWinRTUtils.h" #include "platform/CCApplication.h" +#include "tinyxml2/tinyxml2.h" /** @brief This function change the PVRFrame show/hide setting in register. @@ -225,6 +226,25 @@ Application::Platform Application::getTargetPlatform() } } +std::string Application::getVersion() +{ + std::string r(""); + std::string s = FileUtils::getInstance()->getStringFromFile("WMAppManifest.xml"); + if (!s.empty()) { + tinyxml2::XMLDocument doc; + if (!doc.Parse(s.c_str())) { + tinyxml2::XMLElement *app = doc.RootElement()->FirstChildElement("App"); + if (app) { + const char* version = app->Attribute("Version"); + if (version) { + r = version; + } + } + } + } + return r; +} + bool Application::openURL(const std::string &url) { #if CC_TARGET_PLATFORM == CC_PLATFORM_WINRT diff --git a/cocos/platform/winrt/CCApplication.h b/cocos/platform/winrt/CCApplication.h index 453aa196fa..f2e9856ecb 100644 --- a/cocos/platform/winrt/CCApplication.h +++ b/cocos/platform/winrt/CCApplication.h @@ -63,6 +63,11 @@ public: @brief Get target platform */ virtual Platform getTargetPlatform() override; + + /** + @brief Get application version + */ + virtual std::string getVersion() override; /** @brief Open url in default browser