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