|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using UnityEditor; |
| 5 | +using UnityEngine; |
| 6 | + |
| 7 | +public class MLAPIEditor : EditorWindow |
| 8 | +{ |
| 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() |
| 16 | + { |
| 17 | + GetWindow<MLAPIEditor>(); |
| 18 | + } |
| 19 | + |
| 20 | + private void Init() |
| 21 | + { |
| 22 | + lastUpdated = 0; |
| 23 | + |
| 24 | + if (EditorPrefs.HasKey("MLAPI_version")) |
| 25 | + currentVersion = EditorPrefs.GetString("MLAPI_version"); |
| 26 | + else |
| 27 | + currentVersion = "None"; |
| 28 | + } |
| 29 | + |
| 30 | + private void Awake() |
| 31 | + { |
| 32 | + Init(); |
| 33 | + } |
| 34 | + |
| 35 | + private void OnFocus() |
| 36 | + { |
| 37 | + Init(); |
| 38 | + } |
| 39 | + |
| 40 | + private void OnGUI() |
| 41 | + { |
| 42 | + if(foldoutStatus != null) |
| 43 | + { |
| 44 | + for (int i = 0; i < foldoutStatus.Length; i++) |
| 45 | + { |
| 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]) |
| 50 | + { |
| 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) |
| 57 | + { |
| 58 | + GUIStyle style = new GUIStyle(EditorStyles.boldLabel); |
| 59 | + style.normal.textColor = new Color(1f, 0.5f, 0f); |
| 60 | + EditorGUILayout.LabelField("Pre-release", style); |
| 61 | + } |
| 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 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + GUILayout.BeginArea(new Rect(5, position.height - 20, position.width, 20)); |
| 85 | + |
| 86 | + if (GUILayout.Button("Check for updates")) |
| 87 | + GetReleases(); |
| 88 | + |
| 89 | + GUILayout.EndArea(); |
| 90 | + |
| 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); |
| 93 | + |
| 94 | + if ((DateTime.Now - new DateTime(lastUpdated)).Seconds > 3600) |
| 95 | + GetReleases(); |
| 96 | + |
| 97 | + Repaint(); |
| 98 | + } |
| 99 | + |
| 100 | + private void InstallRelease(int index) |
| 101 | + { |
| 102 | + for (int i = 0; i < releases[index].assets.Length; i++) |
| 103 | + { |
| 104 | + WWW www = new WWW(releases[index].assets[i].browser_download_url); |
| 105 | + while (!www.isDone && string.IsNullOrEmpty(www.error)) |
| 106 | + { |
| 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); |
| 114 | + |
| 115 | + if (releases[index].assets[i].name.EndsWith(".unitypackage")) |
| 116 | + AssetDatabase.ImportPackage(Application.dataPath + "/MLAPI/Lib/" + releases[index].assets[i].name, false); |
| 117 | + } |
| 118 | + |
| 119 | + EditorPrefs.SetString("MLAPI_version", releases[index].tag_name); |
| 120 | + currentVersion = releases[index].tag_name; |
| 121 | + AssetDatabase.Refresh(); |
| 122 | + } |
| 123 | + |
| 124 | + private void GetReleases() |
| 125 | + { |
| 126 | + lastUpdated = DateTime.Now.Ticks; |
| 127 | + |
| 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..."); |
| 132 | + } |
| 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++) |
| 141 | + { |
| 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] == ',') |
| 155 | + { |
| 156 | + releasesJson.Add(currentObject); |
| 157 | + currentObject = ""; |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + releases = new GithubRelease[releasesJson.Count]; |
| 162 | + foldoutStatus = new bool[releasesJson.Count]; |
| 163 | + |
| 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; |
| 171 | + } |
| 172 | + } |
| 173 | +} |
0 commit comments