Improve handling of mod selection.

This commit is contained in:
Ottermandias 2024-08-28 18:28:49 +02:00
parent a3c22f2826
commit d713d5a112
12 changed files with 227 additions and 148 deletions

View file

@ -10,22 +10,21 @@ using Penumbra.Mods.Manager.OptionEditor;
using Penumbra.Mods.SubMods;
using Penumbra.Services;
using Penumbra.String.Classes;
using Penumbra.UI.ModsTab;
namespace Penumbra.Mods.Editor;
public class ModMerger : IDisposable, IService
{
private readonly Configuration _config;
private readonly CommunicatorService _communicator;
private readonly ModGroupEditor _editor;
private readonly ModFileSystemSelector _selector;
private readonly DuplicateManager _duplicates;
private readonly ModManager _mods;
private readonly ModCreator _creator;
private readonly Configuration _config;
private readonly CommunicatorService _communicator;
private readonly ModGroupEditor _editor;
private readonly ModSelection _selection;
private readonly DuplicateManager _duplicates;
private readonly ModManager _mods;
private readonly ModCreator _creator;
public Mod? MergeFromMod
=> _selector.Selected;
=> _selection.Mod;
public Mod? MergeToMod;
public string OptionGroupName = "Merges";
@ -41,23 +40,23 @@ public class ModMerger : IDisposable, IService
public readonly IReadOnlyList<string> Warnings = new List<string>();
public Exception? Error { get; private set; }
public ModMerger(ModManager mods, ModGroupEditor editor, ModFileSystemSelector selector, DuplicateManager duplicates,
public ModMerger(ModManager mods, ModGroupEditor editor, ModSelection selection, DuplicateManager duplicates,
CommunicatorService communicator, ModCreator creator, Configuration config)
{
_editor = editor;
_selector = selector;
_duplicates = duplicates;
_communicator = communicator;
_creator = creator;
_config = config;
_mods = mods;
_selector.SelectionChanged += OnSelectionChange;
_editor = editor;
_selection = selection;
_duplicates = duplicates;
_communicator = communicator;
_creator = creator;
_config = config;
_mods = mods;
_selection.Subscribe(OnSelectionChange, ModSelection.Priority.ModMerger);
_communicator.ModPathChanged.Subscribe(OnModPathChange, ModPathChanged.Priority.ModMerger);
}
public void Dispose()
{
_selector.SelectionChanged -= OnSelectionChange;
_selection.Unsubscribe(OnSelectionChange);
_communicator.ModPathChanged.Unsubscribe(OnModPathChange);
}
@ -390,7 +389,7 @@ public class ModMerger : IDisposable, IService
}
}
private void OnSelectionChange(Mod? oldSelection, Mod? newSelection, in ModFileSystemSelector.ModState state)
private void OnSelectionChange(Mod? oldSelection, Mod? newSelection)
{
if (OptionGroupName == "Merges" && OptionName.Length == 0 || OptionName == oldSelection?.Name.Text)
OptionName = newSelection?.Name.Text ?? string.Empty;

View file

@ -0,0 +1,104 @@
using OtterGui.Classes;
using Penumbra.Api.Enums;
using Penumbra.Collections;
using Penumbra.Collections.Manager;
using Penumbra.Communication;
using Penumbra.Mods.Manager;
using Penumbra.Mods.Settings;
using Penumbra.Services;
namespace Penumbra.Mods;
/// <summary>
/// Triggered whenever the selected mod changes
/// <list type="number">
/// <item>Parameter is the old selected mod. </item>
/// <item>Parameter is the new selected mod </item>
/// </list>
/// </summary>
public class ModSelection : EventWrapper<Mod?, Mod?, ModSelection.Priority>
{
private readonly ActiveCollections _collections;
private readonly EphemeralConfig _config;
private readonly CommunicatorService _communicator;
public ModSelection(CommunicatorService communicator, ModManager mods, ActiveCollections collections, EphemeralConfig config)
: base(nameof(ModSelection))
{
_communicator = communicator;
_collections = collections;
_config = config;
if (_config.LastModPath.Length > 0)
SelectMod(mods.FirstOrDefault(m => string.Equals(m.Identifier, config.LastModPath, StringComparison.OrdinalIgnoreCase)));
_communicator.CollectionChange.Subscribe(OnCollectionChange, CollectionChange.Priority.ModSelection);
_communicator.CollectionInheritanceChanged.Subscribe(OnInheritanceChange, CollectionInheritanceChanged.Priority.ModSelection);
_communicator.ModSettingChanged.Subscribe(OnSettingChange, ModSettingChanged.Priority.ModSelection);
}
public ModSettings Settings { get; private set; } = ModSettings.Empty;
public ModCollection Collection { get; private set; } = ModCollection.Empty;
public Mod? Mod { get; private set; }
public void SelectMod(Mod? mod)
{
if (mod == Mod)
return;
var oldMod = Mod;
Mod = mod;
OnCollectionChange(CollectionType.Current, null, _collections.Current, string.Empty);
Invoke(oldMod, Mod);
_config.LastModPath = mod?.ModPath.Name ?? string.Empty;
_config.Save();
}
protected override void Dispose(bool _)
{
_communicator.CollectionChange.Unsubscribe(OnCollectionChange);
_communicator.CollectionInheritanceChanged.Unsubscribe(OnInheritanceChange);
_communicator.ModSettingChanged.Unsubscribe(OnSettingChange);
}
private void OnCollectionChange(CollectionType type, ModCollection? oldCollection, ModCollection? newCollection, string _2)
{
if (type is CollectionType.Current && oldCollection != newCollection)
UpdateSettings();
}
private void OnSettingChange(ModCollection collection, ModSettingChange _1, Mod? mod, Setting _2, int _3, bool _4)
{
if (collection == _collections.Current && mod == Mod)
UpdateSettings();
}
private void OnInheritanceChange(ModCollection collection, bool arg2)
{
if (collection == _collections.Current)
UpdateSettings();
}
private void UpdateSettings()
{
if (Mod == null)
{
Settings = ModSettings.Empty;
Collection = ModCollection.Empty;
}
else
{
(var settings, Collection) = _collections.Current[Mod.Index];
Settings = settings ?? ModSettings.Empty;
}
}
public enum Priority
{
/// <seealso cref="UI.ModsTab.ModPanel.OnSelectionChange"/>
ModPanel = 0,
/// <seealso cref="Editor.ModMerger.OnSelectionChange"/>
ModMerger = 0,
}
}