SteamVR_Update.cs 5.7 KB

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