Skip to content

Commit

Permalink
Remove deleted designs from automated design sets.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ottermandias committed Sep 29, 2023
1 parent db6687d commit 869618e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
34 changes: 32 additions & 2 deletions Glamourer/Automation/AutoDesignManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
using Glamourer.Structs;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using OtterGui;
using OtterGui.Filesystem;
using Penumbra.GameData.Actors;

namespace Glamourer.Automation;

public class AutoDesignManager : ISavable, IReadOnlyList<AutoDesignSet>
public class AutoDesignManager : ISavable, IReadOnlyList<AutoDesignSet>, IDisposable
{
public const int CurrentVersion = 1;

Expand All @@ -28,6 +29,7 @@ public class AutoDesignManager : ISavable, IReadOnlyList<AutoDesignSet>
private readonly DesignManager _designs;
private readonly ActorService _actors;
private readonly AutomationChanged _event;
private readonly DesignChanged _designEvent;

private readonly List<AutoDesignSet> _data = new();
private readonly Dictionary<ActorIdentifier, AutoDesignSet> _enabled = new();
Expand All @@ -36,17 +38,22 @@ public IReadOnlyDictionary<ActorIdentifier, AutoDesignSet> EnabledSets
=> _enabled;

public AutoDesignManager(JobService jobs, ActorService actors, SaveService saveService, DesignManager designs, AutomationChanged @event,
FixedDesignMigrator migrator, DesignFileSystem fileSystem)
FixedDesignMigrator migrator, DesignFileSystem fileSystem, DesignChanged designEvent)
{
_jobs = jobs;
_actors = actors;
_saveService = saveService;
_designs = designs;
_event = @event;
_designEvent = designEvent;
_designEvent.Subscribe(OnDesignChange, DesignChanged.Priority.AutoDesignManager);
Load();
migrator.ConsumeMigratedData(_actors, fileSystem, this);
}

public void Dispose()
=> _designEvent.Unsubscribe(OnDesignChange);

public IEnumerator<AutoDesignSet> GetEnumerator()
=> _data.GetEnumerator();

Expand Down Expand Up @@ -538,4 +545,27 @@ static ActorIdentifier[] CreateNpcs(ActorManager manager, ActorIdentifier identi
_ => Array.Empty<ActorIdentifier>(),
};
}

private void OnDesignChange(DesignChanged.Type type, Design design, object? data)
{
if (type is not DesignChanged.Type.Deleted)
return;

foreach (var (set, idx) in this.WithIndex())
{
var deleted = 0;
for (var i = 0; i < set.Designs.Count; ++i)
{
if (set.Designs[i].Design != design)
continue;

DeleteDesign(set, i--);
++deleted;
}

if (deleted > 0)
Glamourer.Log.Information(
$"Removed {deleted} automated designs from automated design set {idx} due to deletion of {design.Incognito}.");
}
}
}
3 changes: 3 additions & 0 deletions Glamourer/Events/DesignChanged.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ public enum Priority

/// <seealso cref="Gui.Tabs.DesignTab.DesignFileSystemSelector.OnDesignChange"/>
DesignFileSystemSelector = -1,

/// <seealso cref="Automation.AutoDesignManager.OnDesignChange"/>
AutoDesignManager = 1,
}

public DesignChanged()
Expand Down

0 comments on commit 869618e

Please sign in to comment.