Skip to content

Commit

Permalink
Second refactor pass
Browse files Browse the repository at this point in the history
  • Loading branch information
Lumina committed Jul 27, 2023
1 parent c61aaf2 commit 5d918a6
Show file tree
Hide file tree
Showing 36 changed files with 323 additions and 249 deletions.
2 changes: 1 addition & 1 deletion Editor/AttributesEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
}
else
{
DebugHelper.LogError
EssentialsDebugger.LogError
($"Attempting to use the <b>'[{nameof(RangedFloat)}(float min, float max)]'</b> attribute on a <color=red>{property.type}</color> type field. " +
$"Should be <color=green>'{nameof(RangedFloat)}'</color> instead.");
}
Expand Down
7 changes: 7 additions & 0 deletions Editor/Packages/Attributes.unitypackage.meta

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

7 changes: 7 additions & 0 deletions Editor/Packages/Full Package.unitypackage.meta

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

7 changes: 7 additions & 0 deletions Editor/Packages/Helpers.unitypackage.meta

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

7 changes: 7 additions & 0 deletions Editor/Packages/Misc.unitypackage.meta

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

7 changes: 7 additions & 0 deletions Editor/Packages/Sequencer.unitypackage.meta

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

7 changes: 7 additions & 0 deletions Editor/Packages/Shortcuts.unitypackage.meta

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

4 changes: 2 additions & 2 deletions Editor/UI/Editor Window/Management/AutoSave.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace Lumina.Essentials.Editor.UI.Management
{
[CustomEditor(typeof(AutoSave))]
public class AutoSave : UnityEditor.Editor
internal class AutoSave : UnityEditor.Editor
{
static CancellationTokenSource tokenSource;
static Task task;
Expand Down Expand Up @@ -45,7 +45,7 @@ static async Task SaveInterval(CancellationToken token)
if (!InternalEditorUtility.isApplicationActive) continue;

EditorSceneManager.SaveOpenScenes();
if (AutoSaveConfig.Logging) DebugHelper.Log($"Auto-saved at {DateTime.Now:h:mm:ss tt}");
if (AutoSaveConfig.Logging) EssentialsDebugger.Log($"Auto-saved at {DateTime.Now:h:mm:ss tt}");
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Editor/UI/Editor Window/Management/AutoSaveConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Lumina.Essentials.Editor.UI.Management
{
public static class AutoSaveConfig
internal static class AutoSaveConfig
{
/// <summary>
/// Enable auto save functionality
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@

namespace Lumina.Essentials.Editor.UI.Management
{
public static class DebugHelper
internal static class EssentialsDebugger
{
/// <summary>
/// The log level to be used when logging messages.
/// <param name="Quiet"> Does not log any messages. </param>
/// <param name="Verbose"> Logs all messages. </param>
/// </summary>
public static LogLevel LogBehaviour { get; internal set; } = LogLevel.Verbose;
internal static LogLevel LogBehaviour { get; set; } = LogLevel.Verbose;

public enum LogLevel
internal enum LogLevel
{
[UsedImplicitly] // An option in the Settings menu of the Utility Window.
Quiet,
Expand All @@ -26,29 +26,29 @@ public enum LogLevel
const string ErrorMessagePrefix = "<color=orange>[Lumina Essentials] ►</color>";
const string DefaultErrorMessage = "An Error Has Occurred:";

public static void Log(string message)
internal static void Log(string message)
{
if (!DebugVersion && LogBehaviour == LogLevel.Verbose)
Debug.Log($"{ErrorMessagePrefix} {message ?? DefaultErrorMessage}");
}

// ReSharper disable Unity.PerformanceAnalysis
public static void LogWarning(string message)
internal static void LogWarning(string message)
{
if (!DebugVersion && LogBehaviour == LogLevel.Verbose)
Debug.LogWarning($"{ErrorMessagePrefix} {message ?? DefaultErrorMessage}");
}

// ReSharper disable Unity.PerformanceAnalysis
public static void LogAbort(bool safeMode = false) =>
internal static void LogAbort(bool safeMode = false) =>
Debug.LogWarning($"{ErrorMessagePrefix} The action was aborted. " + $"\n{(safeMode ? "Safe Mode is enabled." : "")}");

// ReSharper disable Unity.PerformanceAnalysis
/// <summary>
/// Log error message with specific format.
/// </summary>
/// <param name="message">The custom message to be logged.</param>
public static void LogError(string message)
internal static void LogError(string message)
{
if (!DebugVersion && LogBehaviour == LogLevel.Verbose)
Debug.LogError($"{ErrorMessagePrefix} {message ?? DefaultErrorMessage}");
Expand All @@ -58,7 +58,7 @@ public static void LogError(string message)
/// Log exception message with specific format.
/// </summary>
/// <param name="exception">The exception to be logged.</param>
public static void LogError(Exception exception)
internal static void LogError(Exception exception)
{
if (!DebugVersion && LogBehaviour == LogLevel.Verbose)
Debug.LogError($"{ErrorMessagePrefix} {exception.Message ?? DefaultErrorMessage}");
Expand Down
125 changes: 34 additions & 91 deletions Editor/UI/Editor Window/Management/StartupChecks.cs
Original file line number Diff line number Diff line change
@@ -1,107 +1,50 @@
using System;
#region
using UnityEditor;
#endregion

namespace Lumina.Essentials.Editor.UI.Management
{
public static class StartupChecks
/// <summary>
/// Runs checks upon startup to ensure the user is up-to-date and to display warnings if necessary.
/// </summary>
internal static class StartupChecks
{
internal static void DisplayVersionAlert()
{
internal static void DisplayVersionAlert()
{
if (!VersionManager.DebugVersion)
{
CheckForUpdatesAfterOneWeek();
}

DebugBuildWarning();
}

/// <summary>
/// Checks if a new version is available by comparing the current version with the latest version.
/// </summary>
/// <param name="currentVersion"> The current version of Lumina's Essentials. </param>
/// <param name="comparisonVersion"> The version to compare the current version with. </param>
/// <returns> True if the current version is older than the comparison version. </returns>
internal static bool IsNewVersionAvailable(string currentVersion, string comparisonVersion) =>
!VersionManager.CompareVersions(currentVersion, comparisonVersion);

static void CheckForUpdatesAfterOneWeek()
{
if (TimeSinceLastUpdateInDays() > 7)
{
EssentialsUpdater.CheckForUpdates();

// If there is an update available, display a warning to the user.
if (IsNewVersionAvailable(VersionManager.CurrentVersion, VersionManager.LatestVersion))
{
EditorUtility.DisplayDialog
("Update Available",
"A new version of Lumina's Essentials is available. \n" +
"Please check the changelog for more information.", "OK");
}
}
}

internal static void DebugBuildWarning()
{
if (!VersionManager.DontShow_DebugBuildWarning && VersionManager.DebugVersion)
{
if (EditorUtility.DisplayDialog
("Debug Version", "You are currently using a Debug Version of Lumina's Essentials. " + "\nThis means the application might not work as intended.", "OK"))
{
VersionManager.DontShow_DebugBuildWarning = true;
EditorPrefs.SetBool("DontShowAgain", VersionManager.DontShow_DebugBuildWarning);
}
}
}

public static string TimeSinceLastUpdate()
{
string lastUpdateCheckString = EditorPrefs.GetString("LastUpdateCheck");
bool isParsedSuccessfully = DateTime.TryParse(lastUpdateCheckString, out DateTime lastUpdateCheck);
if (!VersionManager.DebugVersion) CheckForUpdatesAfterOneWeek();

if (!isParsedSuccessfully) return "Failed to parse stored date time string.";

TimeSpan timeSpanSinceLastUpdate = DateTime.Now - lastUpdateCheck;

(TimeSpan timeSpan, string singularMessage, string pluralMessage)[] timeFrames ={
(TimeSpan.FromHours(1), "{0} minute ago", "{0} minutes ago"),
(TimeSpan.FromHours(2), "{0} hour ago", "{0} hour ago"),
(TimeSpan.FromDays(1), "{0} hours ago", "{0} hours ago"),
(TimeSpan.FromDays(2), "{0} day ago", "{0} day ago"),
(TimeSpan.FromDays(7), "{0} days ago", "{0} days ago"),
(TimeSpan.FromDays(30), "{0} weeks ago", "{0} weeks ago"),
(TimeSpan.MaxValue, "<color=red>More than a week ago</color>",
"<color=red>More than a week ago</color>") };

foreach ((TimeSpan timeSpan, string singularMessage, string pluralMessage) in timeFrames)
{
if (timeSpanSinceLastUpdate < timeSpan) return FormatTimeMessage(timeSpanSinceLastUpdate, singularMessage, pluralMessage);
}
DebugBuildWarning();
}

return string.Empty; // unreachable code but added to satisfy C# rules
}
/// <summary>
/// Checks if a new version is available by comparing the current version with the latest version.
/// </summary>
/// <param name="currentVersion"> The current version of Lumina's Essentials. </param>
/// <param name="comparisonVersion"> The version to compare the current version with. </param>
/// <returns> True if the current version is older than the comparison version. </returns>
internal static bool IsNewVersionAvailable(string currentVersion, string comparisonVersion) => !VersionManager.CompareVersions(currentVersion, comparisonVersion);

static string FormatTimeMessage(TimeSpan deltaTime, string singularMessage, string pluralMessage)
static void CheckForUpdatesAfterOneWeek()
{
if (TimeManager.TimeSinceLastUpdateInDays() > 7)
{
if (deltaTime.TotalMinutes < 1) { return "Less than a minute ago"; }
if (deltaTime.TotalMinutes < 2) return string.Format(singularMessage, 1);
if (deltaTime.TotalHours < 2) return string.Format(pluralMessage, (int) deltaTime.TotalMinutes);
if (deltaTime.TotalDays < 2) return string.Format(pluralMessage, (int) deltaTime.TotalHours);
if (deltaTime.TotalDays >= 2) return string.Format(pluralMessage, (int) deltaTime.TotalDays);
VersionUpdater.CheckForUpdates();

return "";
// If there is an update available, display a warning to the user.
if (IsNewVersionAvailable(VersionManager.CurrentVersion, VersionManager.LatestVersion))
EditorUtility.DisplayDialog("Update Available", "A new version of Lumina's Essentials is available. \n" + "Please check the changelog for more information.", "OK");
}
}

static int TimeSinceLastUpdateInDays()
{
if (DateTime.TryParse(EssentialsUpdater.LastUpdateCheck, out DateTime lastUpdateCheck))
internal static void DebugBuildWarning()
{
if (!VersionManager.DontShow_DebugBuildWarning && VersionManager.DebugVersion)
if (EditorUtility.DisplayDialog
("Debug Version", "You are currently using a Debug Version of Lumina's Essentials. " + "\nThis means the application might not work as intended.", "OK"))
{
TimeSpan timeSpanSinceLastUpdate = DateTime.Now - lastUpdateCheck;
return (int) timeSpanSinceLastUpdate.TotalDays;
VersionManager.DontShow_DebugBuildWarning = true;
EditorPrefs.SetBool("DontShowAgain", VersionManager.DontShow_DebugBuildWarning);
}

// Handle parse failure - depends on your requirements
return -1; // Example: return -1 if the date could not be parsed
}
}
}
}
71 changes: 71 additions & 0 deletions Editor/UI/Editor Window/Management/TimeManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#region
using System;
using Lumina.Essentials.Editor.UI.Management;
using UnityEditor;
#endregion

namespace Lumina.Essentials.Editor
{
/// <summary>
/// Handles time-related operations.
/// Formats time messages for the Utility window.
/// </summary>
internal static class TimeManager
{
internal static string TimeSinceLastUpdate()
{
string lastUpdateCheckString = EditorPrefs.GetString("LastUpdateCheck");
bool isParsedSuccessfully = DateTime.TryParse(lastUpdateCheckString, out DateTime lastUpdateCheck);

if (!isParsedSuccessfully) return "Failed to parse stored date time string.";

TimeSpan timeSpanSinceLastUpdate = DateTime.Now - lastUpdateCheck;

(TimeSpan timeSpan, string singularMessage, string pluralMessage)[] timeFrames ={
(TimeSpan.FromHours(1), "{0} minute ago", "{0} minutes ago"),
(TimeSpan.FromHours(2), "{0} hour ago", "{0} hour ago"),
(TimeSpan.FromDays(1), "{0} hours ago", "{0} hours ago"),
(TimeSpan.FromDays(2), "{0} day ago", "{0} day ago"),
(TimeSpan.FromDays(7), "{0} days ago", "{0} days ago"),
(TimeSpan.FromDays(30), "{0} weeks ago", "{0} weeks ago"),
(TimeSpan.MaxValue, "<color=red>More than a week ago</color>",
"<color=red>More than a week ago</color>") };

foreach ((TimeSpan timeSpan, string singularMessage, string pluralMessage) in timeFrames)
{
if (timeSpanSinceLastUpdate < timeSpan) return FormatTimeMessage(timeSpanSinceLastUpdate, singularMessage, pluralMessage);
}

return string.Empty; // unreachable code but added to satisfy C# rules
}

static string FormatTimeMessage(TimeSpan deltaTime, string singularMessage, string pluralMessage)
{
if (deltaTime.TotalMinutes < 1) return "Less than a minute ago";
if (deltaTime.TotalMinutes < 2) return string.Format(singularMessage, 1);
if (deltaTime.TotalMinutes < 60) return string.Format(pluralMessage, (int) deltaTime.TotalMinutes);

// Convert minutes to hours in delta time_totalMinutes is more than 60 but less than 120
if (deltaTime.TotalMinutes >= 60 && deltaTime.TotalMinutes < 120) return string.Format(singularMessage, 1);

if (deltaTime.TotalHours < 2) return string.Format(singularMessage, (int) deltaTime.TotalHours);
if (deltaTime.TotalHours < 24) return string.Format(pluralMessage, (int) deltaTime.TotalHours);

if (deltaTime.TotalDays < 2) return string.Format(singularMessage, (int) deltaTime.TotalDays);
if (deltaTime.TotalDays >= 2) return string.Format(pluralMessage, (int) deltaTime.TotalDays);
return "";
}

internal static int TimeSinceLastUpdateInDays()
{
if (DateTime.TryParse(VersionUpdater.LastUpdateCheck, out DateTime lastUpdateCheck))
{
TimeSpan timeSpanSinceLastUpdate = DateTime.Now - lastUpdateCheck;
return (int) timeSpanSinceLastUpdate.TotalDays;
}

// Handle parse failure - depends on your requirements
return -1; // Example: return -1 if the date could not be parsed
}
}
}
11 changes: 11 additions & 0 deletions Editor/UI/Editor Window/Management/TimeManager.cs.meta

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

Loading

0 comments on commit 5d918a6

Please sign in to comment.