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