Advertisement
Guest User

HelpLastRelease

a guest
Nov 2nd, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.57 KB | None | 0 0
  1. /*  v13 (02.11.2016)
  2.     Save into Editor folder. Launch from menu:
  3.         Help/Last Release...
  4.         Help/Statistics...
  5.         Help/Roadmap...
  6.         Help/Archive...
  7.         Help/Report...
  8. */
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Diagnostics;
  12. using System.IO;
  13. using System.Text;
  14. using UnityEngine;
  15. using UnityEditor;
  16. using Debug = UnityEngine.Debug;
  17.  
  18. public class HelpLastRelease : EditorWindow {
  19.     const string statsUrl = @"http://hwstats.unity3d.com/index.html";
  20.     const string roadmapUrl = @"http://unity3d.com/unity/roadmap";
  21.     const string archiveUrl = @"http://unity3d.com/get-unity/download/archive";
  22.     const string reportUrl = @"http://files.unity3d.com/build-report/";
  23.     const string releaseUrl = @"http://beta.unity3d.com/download/{0}/download.html";
  24.     const string serverUrl = @"http://symbolserver.unity3d.com/";
  25.     const string historyUrl = serverUrl + @"000Admin/history.txt";
  26.     const string finalRN = @"http://unity3d.com/unity/whats-new/unity-";
  27.     const string betaRN = @"http://unity3d.com/unity/beta/unity";
  28.     const string patchRN = @"http://unity3d.com/unity/qa/patch-releases/";
  29.  
  30.     const string baseName = "UnityYAMLMerge.ex";
  31.     const string compressedName = baseName + "_";
  32.     const string extractedName = baseName + "e";
  33.     static string tempDir;
  34.     static WWW wwwHistory, wwwList, wwwMerger;
  35.     static readonly string zipName = Application.platform == RuntimePlatform.WindowsEditor ? "7z" : "7za";
  36.  
  37.     static SortedList<string, string> fullList;
  38.     static SortedList<string, string> sortedList;
  39.     static SortedList<string, string> currentList;
  40.     static int selected;
  41.     static HelpLastRelease window;
  42.     const string wndTitle = "Unity Builds";
  43.     static string search = "";
  44.     static GUIStyle style;
  45.     private static Dictionary<string, Color> colors = new Dictionary<string, Color>() {
  46.         { "5.4.", Color.green },
  47.         { "5.5.", Color.yellow },
  48.         { "5.6.", Color.red }
  49.     };
  50.  
  51.  
  52.     const string prefsCount = "HelpLastRelease.count";
  53.  
  54.     [MenuItem("Help/Statistics...")]
  55.     static void OpenStatistics() {
  56.         Application.OpenURL(statsUrl);
  57.     }
  58.  
  59.     [MenuItem("Help/Roadmap...")]
  60.     static void OpenRoadmap() {
  61.         Application.OpenURL(roadmapUrl);
  62.     }
  63.  
  64.     [MenuItem("Help/Archive...")]
  65.     static void OpenArchive() {
  66.         Application.OpenURL(archiveUrl);
  67.     }
  68.  
  69.     [MenuItem("Help/Report...")]
  70.     static void OpenReport() {
  71.         Application.OpenURL(reportUrl);
  72.     }
  73.  
  74.     [MenuItem("Help/Last Release...")]
  75.     static void Init() {
  76.         window = GetWindow<HelpLastRelease>(wndTitle);
  77.     }
  78.  
  79.     void OnGUI() {
  80.         if (fullList != null) {
  81.             ListGUI();
  82.         } else WaitGUI();
  83.     }
  84.  
  85.     public static void ListGUI() {
  86.         style = new GUIStyle(GUI.skin.button);
  87.         style.alignment = TextAnchor.MiddleLeft;
  88.         GUILayout.BeginVertical();
  89.         SearchGUI();
  90.         if (currentList == null) currentList = fullList;
  91.         for (int i = currentList.Count - 1; i >= 0; i--) {
  92.             GUILayout.BeginHorizontal();
  93.             ColorGUI(i);
  94.             if (GUILayout.Button("RN", style)) {
  95.                 OpenReleaseNotes(i);
  96.             }
  97.             if (GUILayout.Button(currentList.Values[i], style, GUILayout.MinWidth(142f))) {
  98.                 DownloadList(i);
  99.             }
  100.             GUILayout.FlexibleSpace();
  101.             GUILayout.EndHorizontal();
  102.         }
  103.         GUILayout.EndVertical();
  104.         GUILayout.FlexibleSpace();
  105.     }
  106.  
  107.     static void ColorGUI(int i) {
  108.         GUI.contentColor = Color.white;
  109.         foreach (var k in colors.Keys) {
  110.             if (currentList.Values[i].Contains(k)) {
  111.                 GUI.contentColor = colors[k];
  112.                 break;
  113.             }
  114.         }
  115.     }
  116.  
  117.     void OnEnable() {
  118.         tempDir = Application.dataPath + "/../Temp/LastRelease";
  119.         DownloadHistory();
  120.     }
  121.  
  122.     static void CheckNewVersion() {
  123.         int count = EditorPrefs.GetInt(prefsCount, 0);
  124.         if (count > 0 && fullList.Count > count) {
  125.             EditorApplication.Beep();
  126.             Debug.LogFormat("New version: <color=yellow>{0}</color>", fullList.Values[fullList.Count - 1]);
  127.         }
  128.         EditorPrefs.SetInt(prefsCount, fullList.Count);
  129.     }
  130.  
  131.     static void OpenReleaseNotes(int num) {
  132.         string url = "", version = "";
  133.         if (fullList.Values[num].Contains("a")) return;
  134.         if (fullList.Values[num].Contains("p")) {
  135.             version = fullList.Values[num].Split(' ')[0];
  136.             url = patchRN + version;
  137.         }
  138.         if (fullList.Values[num].Contains("f")) {
  139.             version = fullList.Values[num].Split('f')[0];
  140.             url = finalRN + version;
  141.         }
  142.         if (fullList.Values[num].Contains("b")) {
  143.             version = fullList.Values[num].Split(' ')[0];
  144.             url = betaRN + version;
  145.         }
  146.         if (!string.IsNullOrEmpty(url)) Application.OpenURL(url);
  147.     }
  148.  
  149.     static void FillMenu(WWW history) {
  150.         fullList = new SortedList<string, string>();
  151.         string build;
  152.         string[] parts, releases = history.text.Split('\n');
  153.         for (int i = 0; i < releases.Length; i++) {
  154.             parts = releases[i].Split(',');
  155.             DateTime dt;
  156.             if (DateTime.TryParse(string.Format("{0} {1}", parts[3], parts[4]), out dt)) {
  157.                 build = string.Format("{0} ({1})", parts[6].Trim('\"'), dt.ToString("dd-MM-yyyy"));
  158.                 fullList.Add(parts[0], build);
  159.             }
  160.             //Debug.LogWarningFormat("releases[{0}]={1}\nparts={2}", i, releases[i], parts.ToStringRecursive());
  161.         }
  162.         if (window == null) {
  163.             HelpLastRelease[] w = Resources.FindObjectsOfTypeAll<HelpLastRelease>();
  164.             if (w != null && w.Length > 0) window = w[0];
  165.         }
  166.         if (window != null) window.Repaint();
  167.  
  168.     }
  169.  
  170.     static void SearchVersion() {
  171.         string path = Path.Combine(tempDir, extractedName);
  172.         if (File.Exists(path)) {
  173.             string[] lines;
  174.             lines = File.ReadAllLines(path, Encoding.Unicode);
  175.             FileUtil.DeleteFileOrDirectory(Path.GetDirectoryName(path));
  176.             string version = fullList.Values[selected].Split(' ')[0] + "_";
  177.             for (int i = 0; i < lines.Length; i++) {
  178.                 if (lines[i].Contains(version)) {
  179.                     int pos = lines[i].IndexOf(version);
  180.                     string revision = lines[i].Substring(pos + version.Length, 12);
  181.                     Application.OpenURL(string.Format(releaseUrl, revision));
  182.                     break;
  183.                 }
  184.             }
  185.         }
  186.     }
  187.  
  188.     static void DownloadHistory() {
  189.         wwwHistory = new WWW(historyUrl);
  190.         EditorApplication.update += WaitHistory;
  191.     }
  192.  
  193.     static void DownloadList(int historyNum) {
  194.         selected = historyNum;
  195.         string listUrl = string.Format("{0}000Admin/{1}", serverUrl, fullList.Keys[historyNum]);
  196.         wwwList = new WWW(listUrl);
  197.         EditorApplication.update += WaitList;
  198.     }
  199.  
  200.     static void WaitList() {
  201.         Wait(wwwList, WaitList, ParseList);
  202.     }
  203.  
  204.     static void WaitHistory() {
  205.         Wait(wwwHistory, WaitHistory, FillMenu, CheckNewVersion);
  206.     }
  207.  
  208.     static void Wait(WWW www, EditorApplication.CallbackFunction caller, Action<WWW> action, Action done = null) {
  209.         if (www != null && www.isDone) {
  210.             EditorApplication.update -= caller;
  211.             if (string.IsNullOrEmpty(www.error) && www.size > 0) {
  212.                 //Debug.LogFormat("{0} kB: {1}", www.size/1024, www.url);
  213.                 if (action != null) action(www);
  214.                 if (done != null) done();
  215.             } else Debug.LogWarningFormat("{0}[{1}]: {1}", www.url, www.size, www.error);
  216.             www = null;
  217.         } else {
  218.             if (www == null) EditorApplication.update -= caller;
  219.         }
  220.     }
  221.  
  222.     static void ParseList(WWW list) {
  223.         string[] files = list.text.Split('\n');
  224.         string[] parts;
  225.         for (int i = 0; i < files.Length; i++) {
  226.             parts = files[i].Split(',');
  227.             if (parts[0].Contains(extractedName)) {
  228.                 string mergerUrl = string.Format("{0}{1}/{2}", serverUrl, parts[0].Trim('\"').Replace('\\', '/'), compressedName);
  229.                 DownloadMerger(mergerUrl);
  230.                 break;
  231.             }
  232.         }
  233.     }
  234.  
  235.     static void DownloadMerger(string mergerUrl) {
  236.         wwwMerger = new WWW(mergerUrl);
  237.         EditorApplication.update += WaitMerger;
  238.     }
  239.  
  240.     static void WaitMerger() {
  241.         Wait(wwwMerger, WaitMerger, SaveMerger);
  242.     }
  243.  
  244.     static void SaveMerger(WWW merger) {
  245.         if (!Directory.Exists(tempDir)) {
  246.             Directory.CreateDirectory(tempDir);
  247.         }
  248.         string path = Path.Combine(tempDir, compressedName);
  249.         Debug.LogFormat("path: {0}", path);
  250.         File.WriteAllBytes(path, merger.bytes);
  251.         ExtractMerger(path);
  252.     }
  253.  
  254.     static void ExtractMerger(string path) {
  255.         string zipPath = string.Format("{0}/Tools/{1}", EditorApplication.applicationContentsPath, zipName);
  256.         string arg = string.Format("e -y \"{0}\"", path);
  257.         try {
  258.             using (Process unzip = new Process()) {
  259.                 unzip.StartInfo.FileName = zipPath;
  260.                 unzip.StartInfo.Arguments = arg;
  261.                 unzip.StartInfo.WorkingDirectory = Path.GetDirectoryName(path);
  262.                 unzip.StartInfo.CreateNoWindow = true;
  263.                 unzip.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
  264.                 unzip.Start();
  265.                 unzip.WaitForExit();
  266.                 SearchVersion();
  267.             }
  268.         } catch (Exception e) {
  269.             Debug.LogErrorFormat("{0} {1}\n{2}", zipPath, arg, e.Message);
  270.         }
  271.     }
  272.  
  273.     bool focus;
  274.  
  275.     static void SearchGUI() {
  276.         string s = EditorGUILayout.TextField(search, GUILayout.MaxWidth(174f));
  277.         if (s != search) {
  278.             search = s;
  279.             if (!string.IsNullOrEmpty(search)) {
  280.                 sortedList = new SortedList<string, string>();
  281.                 for (int i = fullList.Count - 1; i >= 0; i--) {
  282.                     if (fullList.Values[i].Contains(search)) {
  283.                         sortedList.Add(fullList.Keys[i], fullList.Values[i]);
  284.                     }
  285.                 }
  286.                 currentList = sortedList;
  287.             } else currentList = fullList;
  288.         }
  289.     }
  290.  
  291.     void WaitGUI() {
  292.         GUILayout.BeginHorizontal();
  293.         GUILayout.FlexibleSpace();
  294.         GUILayout.Label("Wait...");
  295.         GUILayout.FlexibleSpace();
  296.         GUILayout.EndHorizontal();
  297.     }
  298. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement