VersionChecker.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. /// <summary>
  5. /// This component will ask the server what is the latest version. The client will send the app version, platform and language,
  6. /// and the server will reply accordingly.
  7. /// If there is an update available, it will fire a ClientUpdateAvailable notification, with VersionInfo
  8. /// The VersionInfo will contain the version number, what's new and if it is a forced update.
  9. ///
  10. /// This component should be placed on an active gameObject in the hierarchy
  11. /// </summary>
  12. public class VersionChecker : MonoBehaviour
  13. {
  14. //DO NOT FILL THIS MANUALLY. THIS IS AUTOMATICALLY READ FROM PlayerSettings.BundleVersion
  15. //THROUGH AV/Build
  16. public const string BUNDLE_VERSION_NUMBER_FROM_PLAYER_SETTINGS = "1.0";
  17. private const string CACHED_VERSION_KEY = "cachedVersion";
  18. static float _localVersion = -1f;
  19. public static float LocalVersion
  20. {
  21. get
  22. {
  23. if (_localVersion == -1f)
  24. {
  25. if (!float.TryParse(BUNDLE_VERSION_NUMBER_FROM_PLAYER_SETTINGS, out _localVersion))
  26. {
  27. _localVersion = 1;
  28. AVDebug.LogError("Error parsing BUNDLE_VERSION_NUMBER_FROM_PLAYER_SETTINGS, must be a float: "+BUNDLE_VERSION_NUMBER_FROM_PLAYER_SETTINGS);
  29. }
  30. }
  31. return _localVersion;
  32. }
  33. }
  34. public void Check(string language)
  35. {
  36. StartCoroutine(CheckCoroutine(language));
  37. }
  38. IEnumerator CheckCoroutine(string language)
  39. {
  40. string platform;
  41. #if UNITY_EDITOR || DEBUG
  42. {
  43. platform = "Testing";
  44. }
  45. #elif UNITY_WEBPLAYER
  46. {
  47. platform = "Web";
  48. }
  49. #elif UNITY_IPHONE
  50. {
  51. platform = "iOS";
  52. }
  53. #elif UNITY_ANDROID
  54. {
  55. platform = "Android";
  56. }
  57. #else
  58. {
  59. AVDebug.LogError("Unsupported platform");
  60. yield break;
  61. }
  62. #endif
  63. WWW www = new WWW(string.Format("{0}version.php?platform={1}&client={2}&language={3}", GameConstants.SERVER_BASE_URL, platform, LocalVersion, language));
  64. yield return www;
  65. if (www.error == null)
  66. {
  67. PlayerPrefs.SetString(CACHED_VERSION_KEY, www.text);
  68. PlayerPrefs.Save();
  69. ParseVersionUpdate(www.text);
  70. } else
  71. {
  72. AVDebug.LogWarning("Error while retrieving game version from server (using cached version) : " + www.error);
  73. if (PlayerPrefs.HasKey(CACHED_VERSION_KEY))
  74. {
  75. ParseVersionUpdate(PlayerPrefs.GetString(CACHED_VERSION_KEY));
  76. }
  77. }
  78. }
  79. void ParseVersionUpdate(string s)
  80. {
  81. string[] tokens = s.Split('\n');
  82. VersionInfo versionInfo;
  83. if (!float.TryParse(tokens[0], out versionInfo.version))
  84. {
  85. AVDebug.LogError("Error parsing remote version, must be a float : "+tokens[0]);
  86. }
  87. if (LocalVersion >= versionInfo.version)
  88. {
  89. //client is up to date
  90. return;
  91. }
  92. versionInfo.forceUpdate = tokens[1] == "ForceUpdate";
  93. versionInfo.whatsNew = tokens[2];
  94. CoreNotificationCenter.Post(CoreNotificationType.ClientUpdateAvailable, versionInfo);
  95. }
  96. }