Skip to content

Commit

Permalink
Add updater
Browse files Browse the repository at this point in the history
  • Loading branch information
GotoFinal committed Apr 14, 2020
1 parent fac8bce commit ae5683e
Show file tree
Hide file tree
Showing 18 changed files with 412 additions and 12 deletions.
93 changes: 85 additions & 8 deletions Assets/GotoUdon/Editor/GotoUdonEditor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using GotoUdon.Editor;
using GotoUdon.Editor.VersionChecker;
using GotoUdon.Utils;
using GotoUdon.Utils.Editor;
using GotoUdon.VRC;
using UnityEditor;
Expand All @@ -10,6 +13,8 @@
[InitializeOnLoad]
public class GotoUdonEditor : EditorWindow
{
private const string VERSION = "v1.0.1";

// register an event handler when the class is initialized
static GotoUdonEditor()
{
Expand Down Expand Up @@ -67,18 +72,54 @@ private void OnPlayEnd()

private const float OPTION_SPACING = 7;
private const float SECTION_SPACING = 15;
private DateTime _lastUpdateCheck = DateTime.UtcNow.Subtract(TimeSpan.FromDays(1));
private ReleaseResponse _updateCheckerResponse = null;
private bool _downloading = false;

private void OnGUI()
private void OnFocus()
{
// TODO: add version checker in next release?
if (GUILayout.Button("Version: 1.0.0. Click to check for new version at: https://github.com/GotoFinal/GotoUdon/releases", EditorStyles.helpBox))
if (DateTime.UtcNow.Subtract(_lastUpdateCheck).TotalHours < 1)
{
Application.OpenURL("https://github.com/GotoFinal/GotoUdon/releases");
return;
}
if (GUILayout.Button("Click to join (or just add me GotoFinal#5189) on discord for help: https://discord.gg/B8hbbax", EditorStyles.helpBox))

_lastUpdateCheck = DateTime.UtcNow;
CheckForUpdate();
}

private void CheckForUpdate()
{
GotoLog.Log("Checking for updates");
VersionChecker.GetNewestVersion("GotoFinal", "GotoUdon", response => { _updateCheckerResponse = response; });
}

private void UpdateLibrary()
{
if (_updateCheckerResponse == null || _updateCheckerResponse.IsError) return;

ReleaseAsset unityPackage = _updateCheckerResponse.ReleaseInfo.UnityPackage;
if (unityPackage == null) return;

_downloading = true;
Updater.Update("GotoUdon", _updateCheckerResponse.ReleaseInfo.UnityPackage, result =>
{
_downloading = false;
if (!result.IsError)
{
AssetDatabase.ImportPackage(result.DownloadPath, true);
}
});
}

private void OnGUI()
{
DrawVersionInformation();
string discordUrl = "https://discord.gg/B8hbbax";
if (GUILayout.Button($"Click to join (or just add me GotoFinal#5189) on discord for help: {discordUrl}", EditorStyles.helpBox))
{
Application.OpenURL("https://discord.gg/B8hbbax");
Application.OpenURL(discordUrl);
}

SimpleGUI.WarningBox(true,
"NETWORK AND VRCHAT PHYSICS ARE NOT SIMULATED, NETWORK RELATED SETTINGS ONLY AFFECT RETURNED VALUES IN SCRIPTS, DEFAULT UNITY PHYSICS APPLIES (might be improved later)");

Expand All @@ -90,6 +131,42 @@ private void OnGUI()
GUILayout.EndScrollView();
}

private void DrawVersionInformation()
{
string githubUrl = "https://github.com/GotoFinal/GotoUdon/releases";
// TODO: add version checker in next release?
if (_updateCheckerResponse != null)
{
if (SimpleGUI.WarningBox(_updateCheckerResponse.IsError, _updateCheckerResponse.Error))
{
if (GUILayout.Button($"Current version: {VERSION}. Click to check for new version at: {githubUrl}", EditorStyles.helpBox))
{
Application.OpenURL(githubUrl);
}

return;
}

ReleaseInfo releaseInfo = _updateCheckerResponse.ReleaseInfo;
if (releaseInfo.UnityPackage != null && SimpleGUI.InfoBox(releaseInfo.IsNewerThan(VERSION),
$"There is new version available: {releaseInfo.Version}! Click to update!\n{releaseInfo.Name}\n{releaseInfo.Description}")
)
{
GUILayout.BeginHorizontal();
if (!_downloading)
SimpleGUI.ActionButton($"Update to {releaseInfo.Version}!", UpdateLibrary);
SimpleGUI.ActionButton("Download manually.", () => Application.OpenURL(releaseInfo.UnityPackage.DownloadUrl));
GUILayout.EndHorizontal();
return;
}
}

if (GUILayout.Button($"Version: {VERSION}. Click to retry check for new version at: {githubUrl}", EditorStyles.helpBox))
{
CheckForUpdate();
}
}

private void DrawPlayersEditor()
{
if (SimpleGUI.InfoBox(!VRCEmulator.IsReady, "Waiting for emulation to begin...")) return;
Expand Down Expand Up @@ -168,7 +245,7 @@ private void DrawGlobalOptions(GotoUdonSettings settings)
SimpleGUI.ErrorBox(settings.spawnPoint == null,
"You need to select some spawn point to use this resource!");

GUILayout.Label("Global settings");
GUILayout.Label("Global settings");
SimpleGUI.Indent(() =>
{
settings.avatarPrefab = SimpleGUI.ObjectField("Avatar prefab", settings.avatarPrefab, false);
Expand Down
11 changes: 8 additions & 3 deletions Assets/GotoUdon/Editor/UdonDebuggerEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using UnityEditor;
using UnityEngine;
using VRC.Udon;
using VRC.Udon.Serialization.OdinSerializer.Utilities;

namespace GotoUdon.Editor
{
Expand Down Expand Up @@ -47,15 +46,21 @@ private void ActionButton(string name, Action<UdonBehaviour> action)
{
if (GUILayout.Button(name))
{
target.behaviours.ForEach(action);
foreach (UdonBehaviour behaviour in target.behaviours)
{
action(behaviour);
}
}
}

private void ActionButton<T1>(string name, T1 arg1, Action<UdonBehaviour, T1> action)
{
if (GUILayout.Button(name))
{
target.behaviours.ForEach(behaviour => action(behaviour, arg1));
foreach (UdonBehaviour behaviour in target.behaviours)
{
action(behaviour, arg1);
}
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions Assets/GotoUdon/Editor/VersionChecker.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions Assets/GotoUdon/Editor/VersionChecker/ReleaseAsset.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace GotoUdon.Editor
{
public class ReleaseAsset
{
public string Name { get; set; }
public string DownloadUrl { get; set; }
public bool IsUnityPackage => Name.EndsWith(".unitypackage");

public string AsFileName(string path)
{
return path.EndsWith("/") ? path + Name : path + "/" + Name;
}
}
}
3 changes: 3 additions & 0 deletions Assets/GotoUdon/Editor/VersionChecker/ReleaseAsset.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 62 additions & 0 deletions Assets/GotoUdon/Editor/VersionChecker/ReleaseInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;

namespace GotoUdon.Editor
{
public class ReleaseInfo
{
public string Name { get; set; }
public string Version { get; set; }
public string Description { get; set; }
public List<ReleaseAsset> Assets { get; set; }

public ReleaseAsset UnityPackage
{
get
{
foreach (ReleaseAsset asset in Assets)
{
if (asset.IsUnityPackage)
{
return asset;
}
}

return null;
}
}

// supports only simple versions like "v1.0.0"
public bool IsNewerThan(string version)
{
if (version.Equals(Version))
{
return false;
}

string[] versionNumbers = ExtractVersionComponents(version);
string[] thisVersionNumbers = ExtractVersionComponents(Version);

for (int i = 0; i < Math.Min(versionNumbers.Length, thisVersionNumbers.Length); i++)
{
if (int.Parse(thisVersionNumbers[i]) > int.Parse(versionNumbers[i]))
{
return true;
}
}

// eg: 1.0.1 vs 1.0
return thisVersionNumbers.Length > versionNumbers.Length;
}

private string[] ExtractVersionComponents(string version)
{
if (version.StartsWith("v"))
{
version = version.Substring(1);
}

return version.Split('.');
}
}
}
3 changes: 3 additions & 0 deletions Assets/GotoUdon/Editor/VersionChecker/ReleaseInfo.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Assets/GotoUdon/Editor/VersionChecker/ReleaseResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace GotoUdon.Editor
{
public class ReleaseResponse
{
public string Error { get; set; }
public ReleaseInfo ReleaseInfo { get; set; }
public bool IsError => Error != null;
}
}
3 changes: 3 additions & 0 deletions Assets/GotoUdon/Editor/VersionChecker/ReleaseResponse.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 49 additions & 0 deletions Assets/GotoUdon/Editor/VersionChecker/Updater.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.IO;
using GotoUdon.Utils;
using UnityEngine;
using UnityEngine.Networking;

namespace GotoUdon.Editor.VersionChecker
{
public class Updater
{
public static void Update(string path, ReleaseAsset asset, Action<UpdateResult> callback)
{
GotoLog.Log($"Downloading update ({asset.Name}) from {asset.DownloadUrl}");
UnityWebRequest www = UnityWebRequest.Get(asset.DownloadUrl);
try
{
www.downloadHandler = new DownloadHandlerFile(Path.Combine(Application.dataPath, asset.AsFileName(path)));
UnityWebRequestAsyncOperation action = www.SendWebRequest();
action.completed += operation =>
{
UnityWebRequest request = action.webRequest;
var responseCode = request.responseCode;
if (responseCode != 200)
{
GotoLog.Error("Failed to download update: " + request.error);
callback(new UpdateResult {Error = "Failed to download" + request.error});
www.Dispose();
return;
}

callback(new UpdateResult {DownloadPath = "Assets/" + asset.AsFileName(path)});
www.Dispose();
};
}
catch (Exception exception)
{
GotoLog.Exception("Failed to request for update: " + exception.Message, exception);
www.Dispose();
}
}

public class UpdateResult
{
public string Error { get; set; }
public string DownloadPath { get; set; }
public bool IsError => Error != null;
}
}
}
3 changes: 3 additions & 0 deletions Assets/GotoUdon/Editor/VersionChecker/Updater.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ae5683e

Please sign in to comment.