SteamVR_Update.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. //
  3. // Purpose: Notify developers when a new version of the plugin is available.
  4. //
  5. //=============================================================================
  6. using UnityEngine;
  7. using UnityEditor;
  8. using System.IO;
  9. using System.Text.RegularExpressions;
  10. [InitializeOnLoad]
  11. public class SteamVR_Update : EditorWindow
  12. {
  13. const string currentVersion = "1.2.3";
  14. const string versionUrl = "http://media.steampowered.com/apps/steamvr/unitypluginversion.txt";
  15. const string notesUrl = "http://media.steampowered.com/apps/steamvr/unityplugin-v{0}.txt";
  16. const string pluginUrl = "http://u3d.as/content/valve-corporation/steam-vr-plugin";
  17. const string doNotShowKey = "SteamVR.DoNotShow.v{0}";
  18. static bool gotVersion = false;
  19. static WWW wwwVersion, wwwNotes;
  20. static string version, notes;
  21. static SteamVR_Update window;
  22. static SteamVR_Update()
  23. {
  24. EditorApplication.update += Update;
  25. }
  26. static void Update()
  27. {
  28. if (!gotVersion)
  29. {
  30. if (wwwVersion == null)
  31. wwwVersion = new WWW(versionUrl);
  32. if (!wwwVersion.isDone)
  33. return;
  34. if (UrlSuccess(wwwVersion))
  35. version = wwwVersion.text;
  36. wwwVersion = null;
  37. gotVersion = true;
  38. if (ShouldDisplay())
  39. {
  40. var url = string.Format(notesUrl, version);
  41. wwwNotes = new WWW(url);
  42. window = GetWindow<SteamVR_Update>(true);
  43. window.minSize = new Vector2(320, 440);
  44. //window.title = "SteamVR";
  45. }
  46. }
  47. if (wwwNotes != null)
  48. {
  49. if (!wwwNotes.isDone)
  50. return;
  51. if (UrlSuccess(wwwNotes))
  52. notes = wwwNotes.text;
  53. wwwNotes = null;
  54. if (notes != "")
  55. window.Repaint();
  56. }
  57. EditorApplication.update -= Update;
  58. }
  59. static bool UrlSuccess(WWW www)
  60. {
  61. if (!string.IsNullOrEmpty(www.error))
  62. return false;
  63. if (Regex.IsMatch(www.text, "404 not found", RegexOptions.IgnoreCase))
  64. return false;
  65. return true;
  66. }
  67. static bool ShouldDisplay()
  68. {
  69. if (string.IsNullOrEmpty(version))
  70. return false;
  71. if (version == currentVersion)
  72. return false;
  73. if (EditorPrefs.HasKey(string.Format(doNotShowKey, version)))
  74. return false;
  75. // parse to see if newer (e.g. 1.0.4 vs 1.0.3)
  76. var versionSplit = version.Split('.');
  77. var currentVersionSplit = currentVersion.Split('.');
  78. for (int i = 0; i < versionSplit.Length && i < currentVersionSplit.Length; i++)
  79. {
  80. int versionValue, currentVersionValue;
  81. if (int.TryParse(versionSplit[i], out versionValue) &&
  82. int.TryParse(currentVersionSplit[i], out currentVersionValue))
  83. {
  84. if (versionValue > currentVersionValue)
  85. return true;
  86. if (versionValue < currentVersionValue)
  87. return false;
  88. }
  89. }
  90. // same up to this point, now differentiate based on number of sub values (e.g. 1.0.4.1 vs 1.0.4)
  91. if (versionSplit.Length <= currentVersionSplit.Length)
  92. return false;
  93. return true;
  94. }
  95. Vector2 scrollPosition;
  96. bool toggleState;
  97. string GetResourcePath()
  98. {
  99. var ms = MonoScript.FromScriptableObject(this);
  100. var path = AssetDatabase.GetAssetPath(ms);
  101. path = Path.GetDirectoryName(path);
  102. return path.Substring(0, path.Length - "Editor".Length) + "Textures/";
  103. }
  104. public void OnGUI()
  105. {
  106. EditorGUILayout.HelpBox("A new version of the SteamVR plugin is available!", MessageType.Warning);
  107. var resourcePath = GetResourcePath();
  108. var logo = AssetDatabase.LoadAssetAtPath<Texture2D>(resourcePath + "logo.png");
  109. var rect = GUILayoutUtility.GetRect(position.width, 150, GUI.skin.box);
  110. if (logo)
  111. GUI.DrawTexture(rect, logo, ScaleMode.ScaleToFit);
  112. scrollPosition = GUILayout.BeginScrollView(scrollPosition);
  113. GUILayout.Label("Current version: " + currentVersion);
  114. GUILayout.Label("New version: " + version);
  115. if (notes != "")
  116. {
  117. GUILayout.Label("Release notes:");
  118. EditorGUILayout.HelpBox(notes, MessageType.Info);
  119. }
  120. GUILayout.EndScrollView();
  121. GUILayout.FlexibleSpace();
  122. if (GUILayout.Button("Get Latest Version"))
  123. {
  124. Application.OpenURL(pluginUrl);
  125. }
  126. EditorGUI.BeginChangeCheck();
  127. var doNotShow = GUILayout.Toggle(toggleState, "Do not prompt for this version again.");
  128. if (EditorGUI.EndChangeCheck())
  129. {
  130. toggleState = doNotShow;
  131. var key = string.Format(doNotShowKey, version);
  132. if (doNotShow)
  133. EditorPrefs.SetBool(key, true);
  134. else
  135. EditorPrefs.DeleteKey(key);
  136. }
  137. }
  138. }