Advertisement
Guest User

HelpLastRelease

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