Skip to content

Commit

Permalink
feat: adding change log parsing to welcome window. (#771)
Browse files Browse the repository at this point in the history
Signed-off-by: dragonslaya <steve.s@bigredplanetgames.com>

Co-authored-by: dragonslaya <steve.s@bigredplanetgames.com>
  • Loading branch information
dragonslaya84 and dragonslaya-ss committed Apr 12, 2021
1 parent e3f1d26 commit e5409ff
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 57 deletions.
19 changes: 1 addition & 18 deletions Assets/Mirage/Editor/WelcomeWindow/Resources/WelcomeWindow.uss
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,14 @@
padding-top: 0;
flex: 3;
white-space: normal;
flex-basis: auto;
flex-grow: 1;
}

#Spacer {
display: flex;
flex: 0.02;
flex-basis: auto;
}

.navButton {
Button {
padding: 2px;
height: 35px;
}
Expand All @@ -93,17 +90,13 @@
flex: 1;
flex-direction: column;
padding-top: 3px;
flex-basis: 35%;
flex-shrink: 0;
}

#RightColumnBox {
display: flex;
flex: 2;
flex-direction: column;
justify-content: space-between;
flex-basis: auto;
flex-grow: 1;
}

#Module {
Expand All @@ -129,13 +122,3 @@
border-top-color: rgb(147, 147, 147);
border-bottom-color: rgb(147, 147, 147);
}

.screen {
flex-grow: 1;
flex-basis: 100%;
}

.installButton {
height: initial;
flex-shrink: 1;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@
<ui:VisualElement name="Spacer" />
<ui:Button name="Redirect" text="Open Documentation" class="redirect" />
</ui:VisualElement>
<ui:VisualElement name="ChangeLog" class="screen" style="display: none;">
<ui:ScrollView name="ChangeLog" class="screen" style="display: none;">
<ui:Label name="Header" text="Change Log" />
<ui:Label name="Description" text="The Change Log is a list of changes made to Mirage. Sometimes these changes can cause your game to break." style="-unity-text-align: upper-left;" />
<ui:VisualElement name="Spacer" />
<ui:Button name="Redirect" text="Open Change Log" class="redirect" />
</ui:VisualElement>
<ui:Label text="Version 00.0.0" name="ChangeLogVersion" style="padding-left: 10px; padding-right: 10px; padding-bottom: 5px; font-size: 15px; -unity-text-align: upper-left; -unity-font-style: bold;" />
<ui:Label name="ChangeLogText" style="padding-left: 10px; padding-right: 10px; padding-bottom: 15px;" />
</ui:ScrollView>
<ui:VisualElement name="QuickStart" class="screen" style="display: none;">
<ui:Label name="Header" text="Quick Start Guide" />
<ui:Label name="Description" text="The Quick Start Guide is meant for people who just started using Mirage. The Quick Start Guide will help new users learn how to accomplish important tasks. It is highly recommended that you complete the guide." style="-unity-text-align: upper-left;" />
Expand Down
160 changes: 126 additions & 34 deletions Assets/Mirage/Editor/WelcomeWindow/WelcomeWindow.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System.Collections.Generic;
using Mirage.Logging;
using UnityEditor;
using UnityEditor.PackageManager;
using UnityEditor.PackageManager.Requests;
using UnityEngine;
using UnityEngine.UIElements;
using UnityEditor.PackageManager;
using UnityEditor.PackageManager.Requests;
using System.Collections.Generic;
using System.IO;
using System;
using PackageInfo = UnityEditor.PackageManager.PackageInfo;

/**
* Docs used:
Expand All @@ -20,8 +22,6 @@ namespace Mirage
[InitializeOnLoad]
public class WelcomeWindow : EditorWindow
{
static readonly ILogger logger = LogFactory.GetLogger<WelcomeWindow>();

private Button lastClickedTab;
private readonly List<Button> installButtons = new List<Button>();

Expand Down Expand Up @@ -60,6 +60,17 @@ public class WelcomeWindow : EditorWindow
private static string firstStartUpKey = string.Empty;
private const string firstTimeMirageKey = "MirageWelcome";

/// <summary>
/// Hard coded for source code version. If package version is found, this will
/// be set later on to package version. through checking for packages anyways.
/// </summary>
private static string changeLogPath = "Assets/Mirage/CHANGELOG.md";

private static string GetVersion()
{
return typeof(NetworkIdentity).Assembly.GetName().Version.ToString();
}

#region Handle visibility

private static bool ShowChangeLog
Expand Down Expand Up @@ -88,17 +99,13 @@ static WelcomeWindow()
private static void ShowWindowOnFirstStart()
{
EditorApplication.update -= ShowWindowOnFirstStart;
firstStartUpKey = Version.Current;
firstStartUpKey = GetVersion();

if ((!EditorPrefs.GetBool(firstTimeMirageKey, false) || !EditorPrefs.GetBool(firstStartUpKey, false)) && firstStartUpKey != "MirageUnknown")
{
EditorPrefs.SetString(screenToOpenKey, ShowChangeLog ? "ChangeLog" : "Welcome");

#if UNITY_2020_1_OR_NEWER
OpenWindow();
#else
if (logger.LogEnabled()) logger.Log($"WelcomeWindow not supported in {Application.unityVersion}, it is only supported in Unity 2020.1 or newer");
#endif
}
}

Expand Down Expand Up @@ -135,7 +142,9 @@ private void OnEnable()

//set the version text
Label versionText = root.Q<Label>("VersionText");
versionText.text = "v" + Version.Current;
versionText.text = "v" + GetVersion();

DrawChangeLog(ParseChangeLog());

#region Page buttons

Expand Down Expand Up @@ -173,7 +182,7 @@ private void ConfigureTab(string tabButtonName, string tab, string url)
tabButton.EnableInClassList("dark-selected-tab", false);
tabButton.EnableInClassList("light-selected-tab", false);

tabButton.clicked += () =>
tabButton.clicked += () =>
{
ToggleMenuButtonColor(tabButton, true);
ToggleMenuButtonColor(lastClickedTab, false);
Expand All @@ -184,7 +193,10 @@ private void ConfigureTab(string tabButtonName, string tab, string url)
};

Button redirectButton = rootVisualElement.Q<VisualElement>(tab).Q<Button>("Redirect");
redirectButton.clicked += () => Application.OpenURL(url);
if(redirectButton != null)
{
redirectButton.clicked += () => Application.OpenURL(url);
}
}

//switch between content
Expand Down Expand Up @@ -229,6 +241,72 @@ private void ToggleMenuButtonColor(Button button, bool toggle)
}
}

//parse the change log file
private List<string> ParseChangeLog()
{
List<string> content = new List<string>();

using(StreamReader reader = new StreamReader(changeLogPath))
{
string line;

while((line = reader.ReadLine()) != null)
{
//we dont need to parse an empty string
if(line == string.Empty)
continue;

//always add the first line
if(content.Count == 0)
{
content.Add(line);
continue;
}

//if we havent reached the next version yet
if(line.Contains("https://github.com/MirageNet/Mirage/compare/"))
break;

content.Add(line);
}
}

return content;
}

//draw the parsed information
private void DrawChangeLog(List<string> content)
{
Label changeLogText = rootVisualElement.Q<Label>("ChangeLogText");

for (int i = 0; i < content.Count; i++)
{
string item = content[i];

//if the item is a version
if (item.Contains("# [") || item.Contains("## ["))
{
string version = GetVersion();
rootVisualElement.Q<Label>("ChangeLogVersion").text = "Version " + version.Substring(0, version.Length - 2);
}
//if the item is a change title
else if (item.Contains("###"))
{
//only add a space above the title if it isn't the first title
if (i > 2) { changeLogText.text += "\n"; }

changeLogText.text += item.Substring(4) + "\n";
}
//if the item is a change
else
{
string change = item.Split(new string[] { "([" }, StringSplitOptions.None)[0];
change = change.Replace("*", "-");
changeLogText.text += change + "\n";
}
}
}

#region Packages

//configure the package tab when the tab button is pressed
Expand Down Expand Up @@ -281,11 +359,11 @@ private void InstallPackageProgress()
//log results
if (installRequest.Status == StatusCode.Success)
{
if (logger.LogEnabled()) logger.Log("Package install successful.");
Debug.Log("Package install successful.");
}
else if (installRequest.Status == StatusCode.Failure)
{
if (logger.ErrorEnabled()) logger.LogError($"Package install was unsuccessful. \n Error Code: {installRequest.Error.errorCode}\n Error Message: {installRequest.Error.message}");
Debug.LogError("Package install was unsuccessful. \n Error Code: " + installRequest.Error.errorCode + "\n Error Message: " + installRequest.Error.message);
}

EditorApplication.update -= InstallPackageProgress;
Expand All @@ -305,11 +383,11 @@ private void UninstallPackageProgress()

if (uninstallRequest.Status == StatusCode.Success)
{
if (logger.LogEnabled()) logger.Log("Package uninstall successful.");
Debug.Log("Package uninstall successful.");
}
else if (uninstallRequest.Status == StatusCode.Failure)
{
if (logger.ErrorEnabled()) logger.LogError($"Package uninstall was unsuccessful. \n Error Code: {uninstallRequest.Error.errorCode}\n Error Message: {uninstallRequest.Error.message}");
Debug.LogError("Package uninstall was unsuccessful. \n Error Code: " + uninstallRequest.Error.errorCode + "\n Error Message: " + uninstallRequest.Error.message);
}

//refresh the package tab
Expand All @@ -320,31 +398,45 @@ private void UninstallPackageProgress()

private void ListPackageProgress()
{
if (listRequest.IsCompleted)
if (!listRequest.IsCompleted)
{
EditorApplication.update -= ListPackageProgress;
return;
}

EditorApplication.update -= ListPackageProgress;

if (listRequest.Status == StatusCode.Success)
switch (listRequest.Status)
{
//log error
case StatusCode.Success:
{
var installedPackages = new List<string>();

//populate installedPackages
foreach (UnityEditor.PackageManager.PackageInfo package in listRequest.Result)
foreach (PackageInfo package in listRequest.Result)
{
Package? miragePackage = Packages.Find((x) => x.packageName == package.name);
if (miragePackage != null)

if (miragePackage?.packageName == null)
{
continue;
}

if (miragePackage.Value.packageName != null && miragePackage.Value.packageName.Equals("Mirage"))
{
installedPackages.Add(miragePackage.Value.displayName);
// Found mirage package let's set up our change log path.
changeLogPath = "Packages/com.miragenet.mirage/CHANGELOG.md";
}

installedPackages.Add(miragePackage.Value.displayName);
}

ConfigureInstallButtons(installedPackages);
break;
}
//log error
else if (listRequest.Status == StatusCode.Failure)
{
if (logger.ErrorEnabled()) logger.LogError($"There was an issue finding packages. \n Error Code: {listRequest.Error.errorCode}\n Error Message: {listRequest.Error.message}");
}
case StatusCode.Failure:
Debug.LogError("There was an issue finding packages. \n Error Code: " + listRequest.Error.errorCode + "\n Error Message: " + listRequest.Error.message);
break;
}
}

Expand All @@ -362,21 +454,21 @@ private void ConfigureInstallButtons(List<string> installedPackages)

//set text
installButton.text = !foundInInstalledPackages ? "Install" : "Uninstall";

//set functionality
if (!foundInInstalledPackages)
{
installButton.clicked += () =>
{
installButton.clicked += () =>
{
InstallPackage(packageName);
installButton.text = "Installing";
DisableInstallButtons();
};
}
else
{
installButton.clicked += () =>
{
installButton.clicked += () =>
{
UninstallPackage(packageName);
installButton.text = "Uninstalling";
DisableInstallButtons();
Expand Down

0 comments on commit e5409ff

Please sign in to comment.