Skip to content

Commit

Permalink
feat: add version mismatch dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
anna-is-cute committed Dec 11, 2023
1 parent 9cb7ba2 commit b26baad
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 23 deletions.
9 changes: 9 additions & 0 deletions Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Heliosphere.Exceptions;
using Heliosphere.Model.Generated;
using Heliosphere.Ui;
using Heliosphere.Ui.Dialogs;
using Heliosphere.Util;
using Microsoft.Extensions.DependencyInjection;
using Sentry;
Expand Down Expand Up @@ -186,6 +187,14 @@ public Plugin() {
this.PluginUi.OpenAntiVirusWarning();
}

// FIXME: replace constant heliosphere-plugin
if (this.Interface.InstalledPlugins.FirstOrDefault(plugin => plugin.InternalName == "heliosphere-plugin") is { } installed) {
var actual = typeof(Plugin).Assembly.GetName().Version;
if (actual != null && installed.Version != actual) {
this.PluginUi.AddIfNotPresent(new VersionMismatchDialog(installed.Version, actual));
}
}

Task.Run(async () => await this.State.UpdatePackages());
}

Expand Down
22 changes: 4 additions & 18 deletions Ui/AntiVirusWindow.cs → Ui/Dialogs/AntiVirusDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,16 @@
using Heliosphere.Util;
using ImGuiNET;

namespace Heliosphere.Ui;
namespace Heliosphere.Ui.Dialogs;

internal class AntiVirusWindow : IDrawable {
internal class AntiVirusDialog : Dialog {
private Plugin Plugin { get; }

private bool _visible = true;

internal AntiVirusWindow(Plugin plugin) {
internal AntiVirusDialog(Plugin plugin) : base($"{Plugin.Name}##av-warning", ImGuiWindowFlags.AlwaysAutoResize) {
this.Plugin = plugin;
}

public void Dispose() {
}

public DrawStatus Draw() {
if (!this._visible) {
return DrawStatus.Finished;
}

using var end = new OnDispose(ImGui.End);
if (!ImGui.Begin($"{Plugin.Name}##av-warning", ref this._visible, ImGuiWindowFlags.AlwaysAutoResize)) {
return DrawStatus.Continue;
}

protected override DrawStatus InnerDraw() {
ImGuiHelper.TextUnformattedCentred("Warning", PluginUi.TitleSize);

ImGui.Separator();
Expand Down
33 changes: 33 additions & 0 deletions Ui/Dialogs/Dialog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Heliosphere.Util;
using ImGuiNET;

namespace Heliosphere.Ui.Dialogs;

internal abstract class Dialog : IDrawable {
internal string Title { get; }
private ImGuiWindowFlags WindowFlags { get; }
private bool _visible = true;

protected Dialog(string title, ImGuiWindowFlags windowFlags = ImGuiWindowFlags.None) {
this.Title = title;
this.WindowFlags = windowFlags;
}

public virtual void Dispose() {
}

public DrawStatus Draw() {
if (!this._visible) {
return DrawStatus.Finished;
}

using var end = new OnDispose(ImGui.End);
if (!ImGui.Begin(this.Title, ref this._visible, this.WindowFlags)) {
return DrawStatus.Continue;
}

return this.InnerDraw();
}

protected abstract DrawStatus InnerDraw();
}
20 changes: 20 additions & 0 deletions Ui/Dialogs/GenericDialog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using ImGuiNET;

namespace Heliosphere.Ui.Dialogs;

internal class GenericDialog : Dialog {
private Func<DrawStatus> Drawer { get; }

internal GenericDialog(string title, Func<DrawStatus> draw, ImGuiWindowFlags flags = ImGuiWindowFlags.None) : base(title, flags) {
this.Drawer = draw;
}

protected override DrawStatus InnerDraw() {
try {
return this.Drawer();
} catch (Exception ex) {
Plugin.Log.Error(ex, $"Failed to draw generic dialog \"{this.Title}\"");
return DrawStatus.Finished;
}
}
}
30 changes: 30 additions & 0 deletions Ui/Dialogs/VersionMismatchDialog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Numerics;
using Heliosphere.Util;
using ImGuiNET;

namespace Heliosphere.Ui.Dialogs;

internal class VersionMismatchDialog : Dialog {
private Version Dalamud { get; }
private Version Actual { get; }

public VersionMismatchDialog(Version dalamud, Version actual) : base("Heliosphere##version-mismatch") {
this.Dalamud = dalamud;
this.Actual = actual;
}

protected override DrawStatus InnerDraw() {
ImGuiHelper.TextUnformattedCentred("Version mismatch", PluginUi.TitleSize);

ImGui.Spacing();

ImGui.TextUnformatted($"Dalamud thinks you have version {this.Dalamud.ToString(3)} installed, but this is actually version {this.Actual.ToString(3)}.");
ImGui.TextUnformatted($"To fix this, open the plugin installer and disable, delete, and reinstall {Plugin.Name}.");

ImGui.Spacing();

return ImGui.Button("Close", new Vector2(ImGui.GetContentRegionAvail().X, 0))
? DrawStatus.Finished
: DrawStatus.Continue;
}
}
19 changes: 15 additions & 4 deletions Ui/PluginUi.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Numerics;
using Heliosphere.Ui.Dialogs;
using Heliosphere.Ui.Tabs;
using Heliosphere.Util;
using ImGuiNET;
Expand Down Expand Up @@ -69,7 +70,7 @@ private void OpenConfig() {
}

internal void OpenAntiVirusWarning() {
Task.Run(async () => await this.AddIfNotPresentAsync(new AntiVirusWindow(this.Plugin)));
Task.Run(async () => await this.AddIfNotPresentAsync(new AntiVirusDialog(this.Plugin)));
}

internal bool ShouldForceOpen(Tab tab) {
Expand All @@ -96,10 +97,20 @@ internal async Task AddToDrawAsync(IDrawable drawable, CancellationToken token =
}

internal void AddIfNotPresent<T>(T drawable) where T : IDrawable {
using var guard = this.ToDraw.Wait();
if (guard.Data.All(x => x is not T)) {
guard.Data.Add(drawable);
Task.Run(async () => await this.AddIfNotPresentAsync(drawable));
}

internal void AddUniqueDialog(string title, Func<DrawStatus> draw, ImGuiWindowFlags flags = ImGuiWindowFlags.None, CancellationToken token = default) {
Task.Run(async () => await this.AddUniqueDialogAsync(title, draw, flags, token), token);
}

internal async Task AddUniqueDialogAsync(string title, Func<DrawStatus> draw, ImGuiWindowFlags flags = ImGuiWindowFlags.None, CancellationToken token = default) {
using var guard = await this.ToDraw.WaitAsync(token);
if (guard.Data.Any(x => x is Dialog { Title: var other } && other == title)) {
return;
}

guard.Data.Add(new GenericDialog(title, draw, flags));
}

internal async Task AddIfNotPresentAsync<T>(T drawable, CancellationToken token = default) where T : IDrawable {
Expand Down
5 changes: 4 additions & 1 deletion Ui/Tabs/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ internal void Draw() {
return;
}

using var endTabItem = new OnDispose(ImGui.EndTabItem);

ImGui.Checkbox("Preview download status window", ref this.Ui.StatusWindow.Preview);
ImGui.SameLine();
ImGuiHelper.Help("Shows fake mod downloads so you can position the status window where you like.");
Expand Down Expand Up @@ -244,7 +246,8 @@ ref this.Plugin.Config.OneClickCollection
}
}

ImGui.EndTabItem();
var version = typeof(Plugin).Assembly.GetName().Version?.ToString(3) ?? "???";
ImGui.TextUnformatted(version);

if (anyChanged) {
this.Plugin.SaveConfig();
Expand Down

0 comments on commit b26baad

Please sign in to comment.