Skip to content

Commit

Permalink
chore: handle profile errors properly and stop being lazy
Browse files Browse the repository at this point in the history
  • Loading branch information
goaaats committed Oct 10, 2023
1 parent a096bd5 commit 6a01c8c
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 13 deletions.
11 changes: 9 additions & 2 deletions Dalamud/Plugin/Internal/PluginManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -790,8 +790,15 @@ public async Task<LocalPlugin> InstallPluginAsync(RemotePluginManifest repoManif
// or the user removed the plugin manually in which case we don't care
if (reason == PluginLoadReason.Installer)
{
// We don't need to apply, it doesn't matter
await this.profileManager.DefaultProfile.RemoveAsync(repoManifest.InternalName, false, false);
try
{
// We don't need to apply, it doesn't matter
await this.profileManager.DefaultProfile.RemoveAsync(repoManifest.InternalName, false);
}
catch (ProfileOperationException)
{
// ignored
}
}
else
{
Expand Down
62 changes: 51 additions & 11 deletions Dalamud/Plugin/Internal/Profiles/Profile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,22 +200,17 @@ public async Task AddOrUpdateAsync(string internalName, bool state, bool apply =
/// </summary>
/// <param name="internalName">The internal name of the plugin.</param>
/// <param name="apply">Whether or not the current state should immediately be applied.</param>
/// <param name="guardrails">
/// Throw if certain operations are invalid.
/// * Throw if the plugin is not in this profile.
/// * If this is the default profile, check if we are in any other, then throw if we aren't.
/// </param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public async Task RemoveAsync(string internalName, bool apply = true, bool guardrails = true)
public async Task RemoveAsync(string internalName, bool apply = true)
{
ProfileModelV1.ProfileModelV1Plugin entry;
lock (this)
{
entry = this.modelV1.Plugins.FirstOrDefault(x => x.InternalName == internalName);
if (entry == null && guardrails)
throw new ArgumentException($"No plugin \"{internalName}\" in profile \"{this.Guid}\"");
if (entry == null)
throw new PluginNotFoundException(internalName);

if (!this.modelV1.Plugins.Remove(entry) && guardrails)
if (!this.modelV1.Plugins.Remove(entry))
throw new Exception("Couldn't remove plugin from model collection");
}

Expand All @@ -226,9 +221,9 @@ public async Task RemoveAsync(string internalName, bool apply = true, bool guard
{
await this.manager.DefaultProfile.AddOrUpdateAsync(internalName, this.IsEnabled && entry.IsEnabled, false);
}
else if (guardrails)
else
{
throw new Exception("Removed plugin from default profile, but wasn't in any other profile");
throw new PluginNotInDefaultProfileException(internalName);
}
}

Expand All @@ -241,3 +236,48 @@ public async Task RemoveAsync(string internalName, bool apply = true, bool guard
/// <inheritdoc/>
public override string ToString() => $"{this.Guid} ({this.Name})";
}

/// <summary>
/// Exception indicating an issue during a profile operation.
/// </summary>
internal abstract class ProfileOperationException : Exception
{
/// <summary>
/// Initializes a new instance of the <see cref="ProfileOperationException"/> class.
/// </summary>
/// <param name="message">Message to pass on.</param>
protected ProfileOperationException(string message)
: base(message)
{
}
}

/// <summary>
/// Exception indicating that a plugin was not found in the default profile.
/// </summary>
internal sealed class PluginNotInDefaultProfileException : ProfileOperationException
{
/// <summary>
/// Initializes a new instance of the <see cref="PluginNotInDefaultProfileException"/> class.
/// </summary>
/// <param name="internalName">The internal name of the plugin causing the error.</param>
public PluginNotInDefaultProfileException(string internalName)
: base($"The plugin '{internalName}' is not in the default profile, and cannot be removed")
{
}
}

/// <summary>
/// Exception indicating that the plugin was not found.
/// </summary>
internal sealed class PluginNotFoundException : ProfileOperationException
{
/// <summary>
/// Initializes a new instance of the <see cref="PluginNotFoundException"/> class.
/// </summary>
/// <param name="internalName">The internal name of the plugin causing the error.</param>
public PluginNotFoundException(string internalName)
: base($"The plugin '{internalName}' was not found in the profile")
{
}
}

0 comments on commit 6a01c8c

Please sign in to comment.