From afeaefeeca02e832bfe38ff3c2ebf33baa6948a0 Mon Sep 17 00:00:00 2001 From: MorenoB Date: Wed, 5 Jul 2017 10:31:07 +0200 Subject: [PATCH 1/3] Added 'DeleteOldObbFiles' method in GooglePlayDownloader. - This function will delete any previous older obb version if available on the device. --- Plugins/Android/GooglePlayDownloader.cs | 159 +++++++++++++----------- 1 file changed, 87 insertions(+), 72 deletions(-) diff --git a/Plugins/Android/GooglePlayDownloader.cs b/Plugins/Android/GooglePlayDownloader.cs index 31a655e..d75095d 100644 --- a/Plugins/Android/GooglePlayDownloader.cs +++ b/Plugins/Android/GooglePlayDownloader.cs @@ -1,60 +1,60 @@ -using UnityEngine; -using System.Collections; -using System.IO; -using System; - -public static class GooglePlayDownloader -{ -#if UNITY_ANDROID && !UNITY_EDITOR - private static string PublicKey = "REPLACE THIS WITH YOUR PUBLIC KEY"; +using UnityEngine; +using System.Collections; +using System.IO; +using System; + +public static class GooglePlayDownloader +{ +#if UNITY_ANDROID && !UNITY_EDITOR + private static string PublicKey = "REPLACE THIS WITH YOUR PUBLIC KEY"; private static AndroidJavaClass AndroidOSBuildClass = new AndroidJavaClass("android.os.Build"); - private static AndroidJavaClass EnvironmentClass = new AndroidJavaClass("android.os.Environment"); - private const string Environment_MediaMounted = "mounted"; - + private static AndroidJavaClass EnvironmentClass = new AndroidJavaClass("android.os.Environment"); + private const string Environment_MediaMounted = "mounted"; + public static void SetPublicKey(string newKey) { PublicKey = newKey; - } - - public static bool RunningOnAndroid() + } + + public static bool RunningOnAndroid() + { + return AndroidOSBuildClass.GetRawClass() != IntPtr.Zero; + } + + static GooglePlayDownloader() { - return AndroidOSBuildClass.GetRawClass() != IntPtr.Zero; - } - - static GooglePlayDownloader() - { - if (!RunningOnAndroid()) - return; - - using (AndroidJavaClass downloaderServiceClass = new AndroidJavaClass("com.unity3d.plugin.downloader.UnityDownloaderService")) + if (!RunningOnAndroid()) + return; + + using (AndroidJavaClass downloaderServiceClass = new AndroidJavaClass("com.unity3d.plugin.downloader.UnityDownloaderService")) { - downloaderServiceClass.SetStatic("BASE64_PUBLIC_KEY", PublicKey); - // Used by the preference obfuscater - downloaderServiceClass.SetStatic("SALT", new byte[]{1, 43, 256-12, 256-1, 54, 98, 256-100, 256-12, 43, 2, 256-8, 256-4, 9, 5, 256-106, 256-108, 256-33, 45, 256-1, 84}); - } - } - - public static string GetExpansionFilePath() - { - if (EnvironmentClass.CallStatic("getExternalStorageState") != Environment_MediaMounted) - return null; - + downloaderServiceClass.SetStatic("BASE64_PUBLIC_KEY", PublicKey); + // Used by the preference obfuscater + downloaderServiceClass.SetStatic("SALT", new byte[]{1, 43, 256-12, 256-1, 54, 98, 256-100, 256-12, 43, 2, 256-8, 256-4, 9, 5, 256-106, 256-108, 256-33, 45, 256-1, 84}); + } + } + + public static string GetExpansionFilePath() + { + if (EnvironmentClass.CallStatic("getExternalStorageState") != Environment_MediaMounted) + return null; + PopulateOBBData(); const string obbPath = "Android/obb"; - using (AndroidJavaObject externalStorageDirectory = EnvironmentClass.CallStatic("getExternalStorageDirectory")) - { + using (AndroidJavaObject externalStorageDirectory = EnvironmentClass.CallStatic("getExternalStorageDirectory")) + { string root = externalStorageDirectory.Call("getPath"); - return String.Format("{0}/{1}/{2}", root, obbPath, ObbPackage); - } - } - - public static string GetMainOBBPath(string expansionFilePath) + return String.Format("{0}/{1}/{2}", root, obbPath, ObbPackage); + } + } + + public static string GetMainOBBPath(string expansionFilePath) { return GetOBBPackagePath(expansionFilePath, "main"); - } - - public static string GetPatchOBBPath(string expansionFilePath) + } + + public static string GetPatchOBBPath(string expansionFilePath) { return GetOBBPackagePath(expansionFilePath, "patch"); } @@ -67,19 +67,19 @@ private static string GetOBBPackagePath(string expansionFilePath, string prefix) PopulateOBBData(); string filePath = String.Format("{0}/{1}.{2}.{3}.obb", expansionFilePath, prefix, ObbVersion, ObbPackage); return File.Exists(filePath) ? filePath : null; - } - - public static void FetchOBB() - { - using (AndroidJavaClass unityPlayerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) + } + + public static void FetchOBB() + { + using (AndroidJavaClass unityPlayerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) { - AndroidJavaObject currentActivity = unityPlayerClass.GetStatic("currentActivity"); + AndroidJavaObject currentActivity = unityPlayerClass.GetStatic("currentActivity"); AndroidJavaObject intent = new AndroidJavaObject("android.content.Intent", - currentActivity, - new AndroidJavaClass("com.unity3d.plugin.downloader.UnityDownloaderActivity")); - - const int Intent_FLAG_ACTIVITY_NO_ANIMATION = 0x10000; - intent.Call("addFlags", Intent_FLAG_ACTIVITY_NO_ANIMATION); + currentActivity, + new AndroidJavaClass("com.unity3d.plugin.downloader.UnityDownloaderActivity")); + + const int Intent_FLAG_ACTIVITY_NO_ANIMATION = 0x10000; + intent.Call("addFlags", Intent_FLAG_ACTIVITY_NO_ANIMATION); intent.Call("putExtra", "unityplayer.Activity", currentActivity.Call("getClass").Call("getName")); try @@ -89,25 +89,40 @@ public static void FetchOBB() catch (Exception e) { Debug.LogError("Exception occurred while attempting to start DownloaderActivity - is the AndroidManifest.xml incorrect?\n" + e.Message); - } - } - } - - // This code will reuse the package version from the .apk when looking for the .obb + } + } + } + + public static void DeleteOldObbFiles(string expansionFilePath) + { + if (string.IsNullOrEmpty(expansionFilePath)) + return; + + for (int i = 1; i < obb_version; i++) + { + string obbFile = string.Format("{0}/main.{1}.{2}.obb", expansionFilePath, i, obb_package); + if (File.Exists(obbFile)) + { + File.Delete(obbFile); + } + } + } + + // This code will reuse the package version from the .apk when looking for the .obb // Modify as appropriate - private static string ObbPackage; + private static string ObbPackage; private static int ObbVersion = 0; - private static void PopulateOBBData() + private static void PopulateOBBData() { - if (ObbVersion != 0) - return; - using (AndroidJavaClass unityPlayerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) - { - AndroidJavaObject currentActivity = unityPlayerClass.GetStatic("currentActivity"); + if (ObbVersion != 0) + return; + using (AndroidJavaClass unityPlayerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) + { + AndroidJavaObject currentActivity = unityPlayerClass.GetStatic("currentActivity"); ObbPackage = currentActivity.Call("getPackageName"); AndroidJavaObject packageInfo = currentActivity.Call("getPackageManager").Call("getPackageInfo", ObbPackage, 0); - ObbVersion = packageInfo.Get("versionCode"); - } - } + ObbVersion = packageInfo.Get("versionCode"); + } + } #endif -} +} From fb6c03694190c4c8682167a7365f810d0d5d793b Mon Sep 17 00:00:00 2001 From: MorenoB Date: Fri, 7 Jul 2017 16:21:47 +0200 Subject: [PATCH 2/3] Revert "Added 'DeleteOldObbFiles' method in GooglePlayDownloader." This reverts commit afeaefeeca02e832bfe38ff3c2ebf33baa6948a0. --- Plugins/Android/GooglePlayDownloader.cs | 159 +++++++++++------------- 1 file changed, 72 insertions(+), 87 deletions(-) diff --git a/Plugins/Android/GooglePlayDownloader.cs b/Plugins/Android/GooglePlayDownloader.cs index d75095d..31a655e 100644 --- a/Plugins/Android/GooglePlayDownloader.cs +++ b/Plugins/Android/GooglePlayDownloader.cs @@ -1,60 +1,60 @@ -using UnityEngine; -using System.Collections; -using System.IO; -using System; - -public static class GooglePlayDownloader -{ -#if UNITY_ANDROID && !UNITY_EDITOR - private static string PublicKey = "REPLACE THIS WITH YOUR PUBLIC KEY"; +using UnityEngine; +using System.Collections; +using System.IO; +using System; + +public static class GooglePlayDownloader +{ +#if UNITY_ANDROID && !UNITY_EDITOR + private static string PublicKey = "REPLACE THIS WITH YOUR PUBLIC KEY"; private static AndroidJavaClass AndroidOSBuildClass = new AndroidJavaClass("android.os.Build"); - private static AndroidJavaClass EnvironmentClass = new AndroidJavaClass("android.os.Environment"); - private const string Environment_MediaMounted = "mounted"; - + private static AndroidJavaClass EnvironmentClass = new AndroidJavaClass("android.os.Environment"); + private const string Environment_MediaMounted = "mounted"; + public static void SetPublicKey(string newKey) { PublicKey = newKey; - } - - public static bool RunningOnAndroid() - { - return AndroidOSBuildClass.GetRawClass() != IntPtr.Zero; - } - - static GooglePlayDownloader() + } + + public static bool RunningOnAndroid() { - if (!RunningOnAndroid()) - return; - - using (AndroidJavaClass downloaderServiceClass = new AndroidJavaClass("com.unity3d.plugin.downloader.UnityDownloaderService")) + return AndroidOSBuildClass.GetRawClass() != IntPtr.Zero; + } + + static GooglePlayDownloader() + { + if (!RunningOnAndroid()) + return; + + using (AndroidJavaClass downloaderServiceClass = new AndroidJavaClass("com.unity3d.plugin.downloader.UnityDownloaderService")) { - downloaderServiceClass.SetStatic("BASE64_PUBLIC_KEY", PublicKey); - // Used by the preference obfuscater - downloaderServiceClass.SetStatic("SALT", new byte[]{1, 43, 256-12, 256-1, 54, 98, 256-100, 256-12, 43, 2, 256-8, 256-4, 9, 5, 256-106, 256-108, 256-33, 45, 256-1, 84}); - } - } - - public static string GetExpansionFilePath() - { - if (EnvironmentClass.CallStatic("getExternalStorageState") != Environment_MediaMounted) - return null; - + downloaderServiceClass.SetStatic("BASE64_PUBLIC_KEY", PublicKey); + // Used by the preference obfuscater + downloaderServiceClass.SetStatic("SALT", new byte[]{1, 43, 256-12, 256-1, 54, 98, 256-100, 256-12, 43, 2, 256-8, 256-4, 9, 5, 256-106, 256-108, 256-33, 45, 256-1, 84}); + } + } + + public static string GetExpansionFilePath() + { + if (EnvironmentClass.CallStatic("getExternalStorageState") != Environment_MediaMounted) + return null; + PopulateOBBData(); const string obbPath = "Android/obb"; - using (AndroidJavaObject externalStorageDirectory = EnvironmentClass.CallStatic("getExternalStorageDirectory")) - { + using (AndroidJavaObject externalStorageDirectory = EnvironmentClass.CallStatic("getExternalStorageDirectory")) + { string root = externalStorageDirectory.Call("getPath"); - return String.Format("{0}/{1}/{2}", root, obbPath, ObbPackage); - } - } - - public static string GetMainOBBPath(string expansionFilePath) + return String.Format("{0}/{1}/{2}", root, obbPath, ObbPackage); + } + } + + public static string GetMainOBBPath(string expansionFilePath) { return GetOBBPackagePath(expansionFilePath, "main"); - } - - public static string GetPatchOBBPath(string expansionFilePath) + } + + public static string GetPatchOBBPath(string expansionFilePath) { return GetOBBPackagePath(expansionFilePath, "patch"); } @@ -67,19 +67,19 @@ private static string GetOBBPackagePath(string expansionFilePath, string prefix) PopulateOBBData(); string filePath = String.Format("{0}/{1}.{2}.{3}.obb", expansionFilePath, prefix, ObbVersion, ObbPackage); return File.Exists(filePath) ? filePath : null; - } - - public static void FetchOBB() - { - using (AndroidJavaClass unityPlayerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) + } + + public static void FetchOBB() + { + using (AndroidJavaClass unityPlayerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) { - AndroidJavaObject currentActivity = unityPlayerClass.GetStatic("currentActivity"); + AndroidJavaObject currentActivity = unityPlayerClass.GetStatic("currentActivity"); AndroidJavaObject intent = new AndroidJavaObject("android.content.Intent", - currentActivity, - new AndroidJavaClass("com.unity3d.plugin.downloader.UnityDownloaderActivity")); - - const int Intent_FLAG_ACTIVITY_NO_ANIMATION = 0x10000; - intent.Call("addFlags", Intent_FLAG_ACTIVITY_NO_ANIMATION); + currentActivity, + new AndroidJavaClass("com.unity3d.plugin.downloader.UnityDownloaderActivity")); + + const int Intent_FLAG_ACTIVITY_NO_ANIMATION = 0x10000; + intent.Call("addFlags", Intent_FLAG_ACTIVITY_NO_ANIMATION); intent.Call("putExtra", "unityplayer.Activity", currentActivity.Call("getClass").Call("getName")); try @@ -89,40 +89,25 @@ public static void FetchOBB() catch (Exception e) { Debug.LogError("Exception occurred while attempting to start DownloaderActivity - is the AndroidManifest.xml incorrect?\n" + e.Message); - } - } - } - - public static void DeleteOldObbFiles(string expansionFilePath) - { - if (string.IsNullOrEmpty(expansionFilePath)) - return; - - for (int i = 1; i < obb_version; i++) - { - string obbFile = string.Format("{0}/main.{1}.{2}.obb", expansionFilePath, i, obb_package); - if (File.Exists(obbFile)) - { - File.Delete(obbFile); - } - } - } - - // This code will reuse the package version from the .apk when looking for the .obb + } + } + } + + // This code will reuse the package version from the .apk when looking for the .obb // Modify as appropriate - private static string ObbPackage; + private static string ObbPackage; private static int ObbVersion = 0; - private static void PopulateOBBData() + private static void PopulateOBBData() { - if (ObbVersion != 0) - return; - using (AndroidJavaClass unityPlayerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) - { - AndroidJavaObject currentActivity = unityPlayerClass.GetStatic("currentActivity"); + if (ObbVersion != 0) + return; + using (AndroidJavaClass unityPlayerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) + { + AndroidJavaObject currentActivity = unityPlayerClass.GetStatic("currentActivity"); ObbPackage = currentActivity.Call("getPackageName"); AndroidJavaObject packageInfo = currentActivity.Call("getPackageManager").Call("getPackageInfo", ObbPackage, 0); - ObbVersion = packageInfo.Get("versionCode"); - } - } + ObbVersion = packageInfo.Get("versionCode"); + } + } #endif -} +} From d5028cf3b8225049f20c698b56e96ee018ce37ec Mon Sep 17 00:00:00 2001 From: MorenoB Date: Fri, 7 Jul 2017 16:22:32 +0200 Subject: [PATCH 3/3] Re-Commited again due to line endings being changed in previous commit. --- Plugins/Android/GooglePlayDownloader.cs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Plugins/Android/GooglePlayDownloader.cs b/Plugins/Android/GooglePlayDownloader.cs index 31a655e..5dabc0c 100644 --- a/Plugins/Android/GooglePlayDownloader.cs +++ b/Plugins/Android/GooglePlayDownloader.cs @@ -92,6 +92,21 @@ public static void FetchOBB() } } } + + public static void DeleteOldObbFiles(string expansionFilePath) + { + if (string.IsNullOrEmpty(expansionFilePath)) + return; + + for (int i = 1; i < obb_version; i++) + { + string obbFile = string.Format("{0}/main.{1}.{2}.obb", expansionFilePath, i, obb_package); + if (File.Exists(obbFile)) + { + File.Delete(obbFile); + } + } + } // This code will reuse the package version from the .apk when looking for the .obb // Modify as appropriate