Skip to content

Commit

Permalink
feat(import): begin work
Browse files Browse the repository at this point in the history
  • Loading branch information
anna-is-cute committed Jan 20, 2024
1 parent a8676cf commit e04952b
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 21 deletions.
10 changes: 10 additions & 0 deletions PenumbraIpc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ internal class PenumbraIpc : IDisposable {
private FuncSubscriber<string, string, (PenumbraApiEc, string, bool)> GetModPathSubscriber { get; }
private FuncSubscriber<TabType, string, string, PenumbraApiEc> OpenMainWindowSubscriber { get; }
private FuncSubscriber<string, string, string, bool, (PenumbraApiEc, (bool, int, IDictionary<string, IList<string>>, bool)?)> GetCurrentModSettingsSubscriber { get; set; }
private FuncSubscriber<IList<(string, string)>> GetModsSubscriber { get; }

private EventSubscriber<string>? ModAddedEvent { get; set; }
private EventSubscriber<string>? ModDeletedEvent { get; set; }
Expand All @@ -37,6 +38,7 @@ internal PenumbraIpc(Plugin plugin) {
this.GetModPathSubscriber = Penumbra.Api.Ipc.GetModPath.Subscriber(this.Plugin.Interface);
this.OpenMainWindowSubscriber = Penumbra.Api.Ipc.OpenMainWindow.Subscriber(this.Plugin.Interface);
this.GetCurrentModSettingsSubscriber = Penumbra.Api.Ipc.GetCurrentModSettings.Subscriber(this.Plugin.Interface);
this.GetModsSubscriber = Penumbra.Api.Ipc.GetMods.Subscriber(this.Plugin.Interface);

this.RegisterEvents();
}
Expand Down Expand Up @@ -182,6 +184,14 @@ internal void OpenSettings() {
return null;
}
}

internal IList<(string Directory, string Name)>? GetMods() {
try {
return this.GetModsSubscriber.Invoke();
} catch (Exception) {
return null;
}
}
}

internal struct CurrentModSettings {
Expand Down
86 changes: 86 additions & 0 deletions Ui/Components/ModChooser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System.Numerics;
using Dalamud.Interface;
using Dalamud.Interface.Utility;
using Heliosphere.Util;
using ImGuiNET;

namespace Heliosphere.Ui.Components;

internal class ModChooser {
private Plugin Plugin { get; }
private IList<(string Directory, string Name)>? Mods { get; set; }
private IList<(string Directory, string Name)> Filtered { get; set; } = Array.Empty<(string, string)>();
private string _query = string.Empty;
private (string Directory, string Name)? _selected;

internal ModChooser(Plugin plugin) {
this.Plugin = plugin;
this.Refresh();
}

private void Refresh() {
this.Mods = this.Plugin.Penumbra.GetMods();
this.Filter();
}

private void Filter() {
if (this.Mods == null) {
this.Filtered = Array.Empty<(string, string)>();
return;
}

if (string.IsNullOrWhiteSpace(this._query)) {
this.Filtered = this.Mods;
return;
}

var query = this._query.ToLowerInvariant();
this.Filtered = this.Mods
.Where(tuple => tuple.Name.ToLowerInvariant().Contains(query))
.ToList();
}

internal (string Directory, string Name)? Draw() {
if (this.Mods == null) {
return null; // FIXME
}

var outsideAvail = ImGui.GetContentRegionAvail();

var changed = false;

ImGui.TextUnformatted("Penumbra mod");
ImGui.SetNextItemWidth(-1);
if (ImGui.BeginCombo("##combo", "Preview")) {
using var endCombo = new OnDispose(ImGui.EndCombo);

Vector2 buttonSize;
using (ImGuiHelper.WithFont(UiBuilder.IconFont)) {
buttonSize = ImGuiHelpers.GetButtonSize(FontAwesomeIcon.RedoAlt.ToIconString());
}

var textBoxWidth = outsideAvail.X - buttonSize.X - ImGui.GetStyle().ItemSpacing.X;
ImGui.SetNextItemWidth(textBoxWidth);
ImGui.SetKeyboardFocusHere(1);
if (ImGui.InputTextWithHint("##query", "Search...", ref this._query, 256, ImGuiInputTextFlags.AutoSelectAll)) {
this.Filter();
}

ImGui.SameLine();
if (ImGuiHelper.IconButton(FontAwesomeIcon.RedoAlt)) {
this.Refresh();
}

ImGui.Separator();

foreach (var (directory, name) in this.Filtered) {
if (ImGui.Selectable($"{name}##{directory}", this._selected == (directory, name))) {
this._selected = (directory, name);
changed = true;
}
}
}

return changed ? this._selected : null;
}
}
63 changes: 42 additions & 21 deletions Ui/PromptWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Dalamud.Interface.Internal.Notifications;
using Dalamud.Interface.Style;
using Heliosphere.Model.Generated;
using Heliosphere.Ui.Components;
using Heliosphere.Util;
using ImGuiNET;

Expand All @@ -13,6 +14,7 @@ internal class PromptWindow : IDrawable {
private IInstallerWindow_GetVersion Info { get; }
private Guid VersionId { get; }
private string Version { get; }
private ModChooser ModChooser { get; }

private bool _visible = true;
private bool _includeTags;
Expand All @@ -27,6 +29,7 @@ private PromptWindow(Plugin plugin, Guid packageId, IInstallerWindow_GetVersion
this.Info = info;
this.VersionId = versionId;
this.Version = version;
this.ModChooser = new ModChooser(this.Plugin);
this._coverImage = coverImage;
this._includeTags = this.Plugin.Config.IncludeTags;
this._openInPenumbra = this.Plugin.Config.OpenPenumbraAfterInstall;
Expand Down Expand Up @@ -145,20 +148,29 @@ ref this._collection
}

if (this.Info.Groups.Count > 0 && ImGui.CollapsingHeader("Advanced options")) {
using (ImGuiHelper.TextWrap()) {
var model = StyleModel.GetConfiguredStyle() ?? StyleModel.GetFromCurrent();
var orange = model.BuiltInColors?.DalamudOrange;

const string warningText = "Warning! You likely do not want to use these options. These are for advanced users who know what they're doing. You are very likely to break mods if you use these options incorrectly.";

if (orange == null) {
ImGui.TextUnformatted(warningText);
} else {
ImGuiHelper.TextUnformattedColour(warningText, orange.Value);
using var popWrap = ImGuiHelper.TextWrap();

if (ImGui.CollapsingHeader("Import from existing mod")) {
if (this.ModChooser.Draw() is var (directory, name)) {
Plugin.Log.Info(directory);
Plugin.Log.Info(name);
// TODO: - run through all the files and hash them
// - check if all the hashes are there
// - display results to user (x/y expected files found, proceed?)
// - rename files and delete any left over
// - download any missing files
// - create heliosphere meta
// - replace group files
// - remove and re-add mod in penumbra
}
}

ImGui.Spacing();
ImGuiHelper.TextUnformattedColour(
$"If you already have this mod installed, you can use this to attempt to convert it to a {Plugin.Name} mod, skipping most or all of the download.",
ImGuiCol.TextDisabled
);

// ---

var shiftHeld = ImGui.GetIO().KeyShift;
using (ImGuiHelper.WithDisabled(!shiftHeld)) {
Expand All @@ -178,20 +190,29 @@ ref this._collection
}

if (!shiftHeld) {
ImGuiHelper.Tooltip("Hold the Shift key to enable this button.", ImGuiHoveredFlags.AllowWhenDisabled);
ImGuiHelper.Tooltip("Hold the Shift key to enable this dangerous button.", ImGuiHoveredFlags.AllowWhenDisabled);
}

using (ImGuiHelper.TextWrap()) {
ImGuiHelper.TextUnformattedColour(
"Choose specific options to download and install. This may result in a partial or invalid mod install if not used correctly.",
ImGuiCol.TextDisabled
);
var model = StyleModel.GetConfiguredStyle() ?? StyleModel.GetFromCurrent();
var orange = model.BuiltInColors?.DalamudOrange;

const string warningText = "Warning! You likely do not want to use this option. This is for advanced users who know what they're doing. You are very likely to break mods if you use this option incorrectly.";

ImGuiHelper.TextUnformattedColour(
"If you install a mod using this option, please do not look for support; reinstall the mod normally first.",
ImGuiCol.TextDisabled
);
if (orange == null) {
ImGui.TextUnformatted(warningText);
} else {
ImGuiHelper.TextUnformattedColour(warningText, orange.Value);
}

ImGuiHelper.TextUnformattedColour(
"Choose specific options to download and install. This may result in a partial or invalid mod install if not used correctly.",
ImGuiCol.TextDisabled
);

ImGuiHelper.TextUnformattedColour(
"If you install a mod using this option, please do not look for support; reinstall the mod normally first.",
ImGuiCol.TextDisabled
);
}

ImGui.End();
Expand Down
5 changes: 5 additions & 0 deletions Util/ImGuiHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,11 @@ internal static void TextUnformattedDisabled(string text) {
var disabledColour = ImGui.GetStyle().Colors[(int) ImGuiCol.TextDisabled];
TextUnformattedColour(text, disabledColour);
}

internal static OnDispose WithFont(ImFontPtr font) {
ImGui.PushFont(font);
return new OnDispose(ImGui.PopFont);
}
}

internal class OnDispose : IDisposable {
Expand Down

0 comments on commit e04952b

Please sign in to comment.