|
1 | 1 | using System;
|
2 | 2 | using System.Collections.Generic;
|
3 | 3 | using System.IO;
|
| 4 | +using UnityEditor; |
4 | 5 | using UnityEngine;
|
5 | 6 |
|
6 |
| -namespace UnityEditor |
| 7 | +[Serializable] |
| 8 | +public class GithubRelease |
7 | 9 | {
|
8 |
| - public class MLAPIEditor : EditorWindow |
9 |
| - { |
10 |
| - private GithubRelease[] releases = new GithubRelease[0]; |
11 |
| - private bool[] foldoutStatus = new bool[0]; |
12 |
| - private long lastUpdated = 0; |
13 |
| - private string currentVersion; |
| 10 | + public string html_url; |
| 11 | + public string tag_name; |
| 12 | + public string name; |
| 13 | + public string body; |
| 14 | + public string published_at; |
| 15 | + public bool prerelease; |
| 16 | + public GithubAsset[] assets; |
| 17 | +} |
14 | 18 |
|
15 |
| - [MenuItem("Window/MLAPI")] |
16 |
| - public static void ShowWindow() |
17 |
| - { |
18 |
| - GetWindow<MLAPIEditor>(); |
19 |
| - } |
| 19 | +[Serializable] |
| 20 | +public class GithubAsset |
| 21 | +{ |
| 22 | + public string browser_download_url; |
| 23 | + public string name; |
| 24 | +} |
20 | 25 |
|
21 |
| - private void Init() |
22 |
| - { |
23 |
| - lastUpdated = 0; |
| 26 | +public class MLAPIEditor : EditorWindow |
| 27 | +{ |
| 28 | + private GithubRelease[] releases = new GithubRelease[0]; |
| 29 | + private bool[] foldoutStatus = new bool[0]; |
| 30 | + private long lastUpdated = 0; |
| 31 | + private string currentVersion; |
24 | 32 |
|
25 |
| - if (EditorPrefs.HasKey("MLAPI_version")) |
26 |
| - currentVersion = EditorPrefs.GetString("MLAPI_version"); |
27 |
| - else |
28 |
| - currentVersion = "None"; |
29 |
| - } |
| 33 | + [MenuItem("Window/MLAPI")] |
| 34 | + public static void ShowWindow() |
| 35 | + { |
| 36 | + GetWindow<MLAPIEditor>(); |
| 37 | + } |
30 | 38 |
|
31 |
| - private void Awake() |
32 |
| - { |
33 |
| - Init(); |
34 |
| - } |
| 39 | + private void Init() |
| 40 | + { |
| 41 | + lastUpdated = 0; |
35 | 42 |
|
36 |
| - private void OnFocus() |
37 |
| - { |
38 |
| - Init(); |
39 |
| - } |
| 43 | + if (EditorPrefs.HasKey("MLAPI_version")) |
| 44 | + currentVersion = EditorPrefs.GetString("MLAPI_version"); |
| 45 | + else |
| 46 | + currentVersion = "None"; |
| 47 | + } |
| 48 | + |
| 49 | + private void Awake() |
| 50 | + { |
| 51 | + Init(); |
| 52 | + } |
| 53 | + |
| 54 | + private void OnFocus() |
| 55 | + { |
| 56 | + Init(); |
| 57 | + } |
40 | 58 |
|
41 |
| - private void OnGUI() |
| 59 | + private void OnGUI() |
| 60 | + { |
| 61 | + if (foldoutStatus != null) |
42 | 62 | {
|
43 |
| - if (foldoutStatus != null) |
| 63 | + for (int i = 0; i < foldoutStatus.Length; i++) |
44 | 64 | {
|
45 |
| - for (int i = 0; i < foldoutStatus.Length; i++) |
| 65 | + if (releases[i] == null) |
| 66 | + continue; |
| 67 | + foldoutStatus[i] = EditorGUILayout.Foldout(foldoutStatus[i], releases[i].tag_name + " - " + releases[i].name); |
| 68 | + if (foldoutStatus[i]) |
46 | 69 | {
|
47 |
| - if (releases[i] == null) |
48 |
| - continue; |
49 |
| - foldoutStatus[i] = EditorGUILayout.Foldout(foldoutStatus[i], releases[i].tag_name + " - " + releases[i].name); |
50 |
| - if (foldoutStatus[i]) |
| 70 | + EditorGUI.indentLevel++; |
| 71 | + EditorGUILayout.LabelField("Release notes", EditorStyles.boldLabel); |
| 72 | + EditorGUILayout.LabelField(releases[i].body, EditorStyles.wordWrappedLabel); |
| 73 | + EditorGUILayout.Space(); |
| 74 | + EditorGUILayout.Space(); |
| 75 | + if (releases[i].prerelease) |
| 76 | + { |
| 77 | + GUIStyle style = new GUIStyle(EditorStyles.boldLabel); |
| 78 | + style.normal.textColor = new Color(1f, 0.5f, 0f); |
| 79 | + EditorGUILayout.LabelField("Pre-release", style); |
| 80 | + } |
| 81 | + else |
| 82 | + { |
| 83 | + GUIStyle style = new GUIStyle(EditorStyles.boldLabel); |
| 84 | + style.normal.textColor = new Color(0f, 1f, 0f); |
| 85 | + EditorGUILayout.LabelField("Stable-release", style); |
| 86 | + } |
| 87 | + if (currentVersion == releases[i].tag_name) |
51 | 88 | {
|
52 |
| - EditorGUI.indentLevel++; |
53 |
| - EditorGUILayout.LabelField("Release notes", EditorStyles.boldLabel); |
54 |
| - EditorGUILayout.LabelField(releases[i].body, EditorStyles.wordWrappedLabel); |
55 |
| - EditorGUILayout.Space(); |
56 |
| - EditorGUILayout.Space(); |
57 |
| - if (releases[i].prerelease) |
58 |
| - { |
59 |
| - GUIStyle style = new GUIStyle(EditorStyles.boldLabel); |
60 |
| - style.normal.textColor = new Color(1f, 0.5f, 0f); |
61 |
| - EditorGUILayout.LabelField("Pre-release", style); |
62 |
| - } |
63 |
| - else |
64 |
| - { |
65 |
| - GUIStyle style = new GUIStyle(EditorStyles.boldLabel); |
66 |
| - style.normal.textColor = new Color(0f, 1f, 0f); |
67 |
| - EditorGUILayout.LabelField("Stable-release", style); |
68 |
| - } |
69 |
| - if (currentVersion == releases[i].tag_name) |
70 |
| - { |
71 |
| - GUIStyle boldStyle = new GUIStyle(EditorStyles.boldLabel); |
72 |
| - boldStyle.normal.textColor = new Color(0.3f, 1f, 0.3f); |
73 |
| - EditorGUILayout.LabelField("Installed", boldStyle); |
74 |
| - } |
75 |
| - EditorGUILayout.LabelField("Release date: " + DateTime.Parse(DateTime.Parse(releases[i].published_at).ToString()), EditorStyles.miniBoldLabel); |
76 |
| - |
77 |
| - if (currentVersion != releases[i].tag_name && GUILayout.Button("Install")) |
78 |
| - InstallRelease(i); |
79 |
| - |
80 |
| - EditorGUI.indentLevel--; |
| 89 | + GUIStyle boldStyle = new GUIStyle(EditorStyles.boldLabel); |
| 90 | + boldStyle.normal.textColor = new Color(0.3f, 1f, 0.3f); |
| 91 | + EditorGUILayout.LabelField("Installed", boldStyle); |
81 | 92 | }
|
| 93 | + EditorGUILayout.LabelField("Release date: " + DateTime.Parse(DateTime.Parse(releases[i].published_at).ToString()), EditorStyles.miniBoldLabel); |
| 94 | + |
| 95 | + if (currentVersion != releases[i].tag_name && GUILayout.Button("Install")) |
| 96 | + InstallRelease(i); |
| 97 | + |
| 98 | + EditorGUI.indentLevel--; |
82 | 99 | }
|
83 | 100 | }
|
| 101 | + } |
84 | 102 |
|
85 |
| - GUILayout.BeginArea(new Rect(5, position.height - 20, position.width, 20)); |
| 103 | + GUILayout.BeginArea(new Rect(5, position.height - 20, position.width, 20)); |
86 | 104 |
|
87 |
| - if (GUILayout.Button("Check for updates")) |
88 |
| - GetReleases(); |
| 105 | + if (GUILayout.Button("Check for updates")) |
| 106 | + GetReleases(); |
89 | 107 |
|
90 |
| - GUILayout.EndArea(); |
| 108 | + GUILayout.EndArea(); |
91 | 109 |
|
92 |
| - string lastUpdatedString = lastUpdated == 0 ? "Never" : new DateTime(lastUpdated).ToShortTimeString(); |
93 |
| - EditorGUI.LabelField(new Rect(5, position.height - 40, position.width, 20), "Last checked: " + lastUpdatedString, EditorStyles.centeredGreyMiniLabel); |
| 110 | + string lastUpdatedString = lastUpdated == 0 ? "Never" : new DateTime(lastUpdated).ToShortTimeString(); |
| 111 | + EditorGUI.LabelField(new Rect(5, position.height - 40, position.width, 20), "Last checked: " + lastUpdatedString, EditorStyles.centeredGreyMiniLabel); |
94 | 112 |
|
95 |
| - if ((DateTime.Now - new DateTime(lastUpdated)).Seconds > 3600) |
96 |
| - GetReleases(); |
| 113 | + if ((DateTime.Now - new DateTime(lastUpdated)).Seconds > 3600) |
| 114 | + GetReleases(); |
97 | 115 |
|
98 |
| - Repaint(); |
99 |
| - } |
| 116 | + Repaint(); |
| 117 | + } |
100 | 118 |
|
101 |
| - private void InstallRelease(int index) |
| 119 | + private void InstallRelease(int index) |
| 120 | + { |
| 121 | + for (int i = 0; i < releases[index].assets.Length; i++) |
102 | 122 | {
|
103 |
| - for (int i = 0; i < releases[index].assets.Length; i++) |
| 123 | + WWW www = new WWW(releases[index].assets[i].browser_download_url); |
| 124 | + while (!www.isDone && string.IsNullOrEmpty(www.error)) |
104 | 125 | {
|
105 |
| - WWW www = new WWW(releases[index].assets[i].browser_download_url); |
106 |
| - while (!www.isDone && string.IsNullOrEmpty(www.error)) |
107 |
| - { |
108 |
| - EditorGUI.ProgressBar(new Rect(5, position.height - 60, position.width, 20), www.progress, "Installing " + i + "/" + releases[index].assets.Length); |
109 |
| - } |
110 |
| - |
111 |
| - if (!Directory.Exists(Application.dataPath + "/MLAPI/Lib/")) |
112 |
| - Directory.CreateDirectory(Application.dataPath + "/MLAPI/Lib/"); |
| 126 | + EditorGUI.ProgressBar(new Rect(5, position.height - 60, position.width, 20), www.progress, "Installing " + i + "/" + releases[index].assets.Length); |
| 127 | + } |
113 | 128 |
|
114 |
| - File.WriteAllBytes(Application.dataPath + "/MLAPI/Lib/" + releases[index].assets[i].name, www.bytes); |
| 129 | + if (!Directory.Exists(Application.dataPath + "/MLAPI/Lib/")) |
| 130 | + Directory.CreateDirectory(Application.dataPath + "/MLAPI/Lib/"); |
115 | 131 |
|
116 |
| - if (releases[index].assets[i].name.EndsWith(".unitypackage")) |
117 |
| - AssetDatabase.ImportPackage(Application.dataPath + "/MLAPI/Lib/" + releases[index].assets[i].name, false); |
118 |
| - } |
| 132 | + File.WriteAllBytes(Application.dataPath + "/MLAPI/Lib/" + releases[index].assets[i].name, www.bytes); |
119 | 133 |
|
120 |
| - EditorPrefs.SetString("MLAPI_version", releases[index].tag_name); |
121 |
| - currentVersion = releases[index].tag_name; |
122 |
| - AssetDatabase.Refresh(); |
| 134 | + if (releases[index].assets[i].name.EndsWith(".unitypackage")) |
| 135 | + AssetDatabase.ImportPackage(Application.dataPath + "/MLAPI/Lib/" + releases[index].assets[i].name, false); |
123 | 136 | }
|
124 | 137 |
|
125 |
| - private void GetReleases() |
126 |
| - { |
127 |
| - lastUpdated = DateTime.Now.Ticks; |
| 138 | + EditorPrefs.SetString("MLAPI_version", releases[index].tag_name); |
| 139 | + currentVersion = releases[index].tag_name; |
| 140 | + AssetDatabase.Refresh(); |
| 141 | + } |
128 | 142 |
|
129 |
| - WWW www = new WWW("https://api.github.com/repos/TwoTenPvP/MLAPI/releases"); |
130 |
| - while (!www.isDone && string.IsNullOrEmpty(www.error)) |
131 |
| - { |
132 |
| - EditorGUI.ProgressBar(new Rect(5, position.height - 60, position.width, 20), www.progress, "Fetching..."); |
133 |
| - } |
134 |
| - string json = www.text; |
135 |
| - |
136 |
| - //This makes it from a json array to the individual objects in the array. |
137 |
| - //The JSON serializer cant take arrays. We have to split it up outselves. |
138 |
| - List<string> releasesJson = new List<string>(); |
139 |
| - int depth = 0; |
140 |
| - string currentObject = ""; |
141 |
| - for (int i = 1; i < json.Length - 1; i++) |
| 143 | + private void GetReleases() |
| 144 | + { |
| 145 | + lastUpdated = DateTime.Now.Ticks; |
| 146 | + |
| 147 | + WWW www = new WWW("https://api.github.com/repos/TwoTenPvP/MLAPI/releases"); |
| 148 | + while (!www.isDone && string.IsNullOrEmpty(www.error)) |
| 149 | + { |
| 150 | + EditorGUI.ProgressBar(new Rect(5, position.height - 60, position.width, 20), www.progress, "Fetching..."); |
| 151 | + } |
| 152 | + string json = www.text; |
| 153 | + |
| 154 | + //This makes it from a json array to the individual objects in the array. |
| 155 | + //The JSON serializer cant take arrays. We have to split it up outselves. |
| 156 | + List<string> releasesJson = new List<string>(); |
| 157 | + int depth = 0; |
| 158 | + string currentObject = ""; |
| 159 | + for (int i = 1; i < json.Length - 1; i++) |
| 160 | + { |
| 161 | + if (json[i] == '[') |
| 162 | + depth++; |
| 163 | + else if (json[i] == ']') |
| 164 | + depth--; |
| 165 | + else if (json[i] == '{') |
| 166 | + depth++; |
| 167 | + else if (json[i] == '}') |
| 168 | + depth--; |
| 169 | + |
| 170 | + if ((depth == 0 && json[i] != ',') || depth > 0) |
| 171 | + currentObject += json[i]; |
| 172 | + |
| 173 | + if (depth == 0 && json[i] == ',') |
142 | 174 | {
|
143 |
| - if (json[i] == '[') |
144 |
| - depth++; |
145 |
| - else if (json[i] == ']') |
146 |
| - depth--; |
147 |
| - else if (json[i] == '{') |
148 |
| - depth++; |
149 |
| - else if (json[i] == '}') |
150 |
| - depth--; |
151 |
| - |
152 |
| - if ((depth == 0 && json[i] != ',') || depth > 0) |
153 |
| - currentObject += json[i]; |
154 |
| - |
155 |
| - if (depth == 0 && json[i] == ',') |
156 |
| - { |
157 |
| - releasesJson.Add(currentObject); |
158 |
| - currentObject = ""; |
159 |
| - } |
| 175 | + releasesJson.Add(currentObject); |
| 176 | + currentObject = ""; |
160 | 177 | }
|
| 178 | + } |
161 | 179 |
|
162 |
| - releases = new GithubRelease[releasesJson.Count]; |
163 |
| - foldoutStatus = new bool[releasesJson.Count]; |
| 180 | + releases = new GithubRelease[releasesJson.Count]; |
| 181 | + foldoutStatus = new bool[releasesJson.Count]; |
164 | 182 |
|
165 |
| - for (int i = 0; i < releasesJson.Count; i++) |
166 |
| - { |
167 |
| - releases[i] = JsonUtility.FromJson<GithubRelease>(releasesJson[i]); |
168 |
| - if (i == 0) |
169 |
| - foldoutStatus[i] = true; |
170 |
| - else |
171 |
| - foldoutStatus[i] = false; |
172 |
| - } |
| 183 | + for (int i = 0; i < releasesJson.Count; i++) |
| 184 | + { |
| 185 | + releases[i] = JsonUtility.FromJson<GithubRelease>(releasesJson[i]); |
| 186 | + if (i == 0) |
| 187 | + foldoutStatus[i] = true; |
| 188 | + else |
| 189 | + foldoutStatus[i] = false; |
173 | 190 | }
|
174 | 191 | }
|
175 | 192 | }
|
0 commit comments