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