Skip to content

Commit

Permalink
Workshop - Replace non-workshop plugins when installing them from the…
Browse files Browse the repository at this point in the history
… workshop
  • Loading branch information
RobertBeekman committed Mar 14, 2024
1 parent fa8b031 commit 18d7531
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
16 changes: 14 additions & 2 deletions src/Artemis.Core/Services/PluginManagementService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ public void UnloadPlugins()

// It is appropriate to call this now that we have the features of this plugin
bool autoEnabled = plugin.AutoEnableIfNew();

if (autoEnabled || addedNewFeature)
_pluginRepository.SavePlugin(entity);

Expand Down Expand Up @@ -671,7 +671,19 @@ public void RemovePlugin(Plugin plugin, bool removeSettings)
UnloadPlugin(plugin);
}

directory.Delete(true);
// Delete plugin.json since that should never be in use and prevents future loads
File.Delete(Path.Combine(directory.FullName, "plugin.json"));

try
{
// Give a good effort to remove the directory, files may be in use though :\
directory.Delete(true);
}
catch (Exception e)
{
_logger.Warning(e, "Failed to fully remove plugin directory {Directory}", directory.FullName);
}

if (removeSettings)
RemovePluginSettings(plugin);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,17 @@ public async Task<EntryInstallResult> InstallAsync(IEntryDetails entry, IRelease
using ZipArchive archive = new(stream);
archive.ExtractToDirectory(releaseDirectory.FullName);

// If there is already a version of the plugin installed, disable it
if (installedEntry.TryGetMetadata("PluginId", out Guid pluginId))
PluginInfo pluginInfo = CoreJson.Deserialize<PluginInfo>(await File.ReadAllTextAsync(Path.Combine(releaseDirectory.FullName, "plugin.json"), cancellationToken))!;

// If there is already a version of the plugin installed, remove it
Plugin? currentVersion = _pluginManagementService.GetAllPlugins().FirstOrDefault(p => p.Guid == pluginInfo.Guid);
if (currentVersion != null)
{
Plugin? currentVersion = _pluginManagementService.GetAllPlugins().FirstOrDefault(p => p.Guid == pluginId);
if (currentVersion != null)
// If the current version isn't from the workshop, remove it first
if (currentVersion.Directory.FullName.StartsWith(Constants.PluginsFolder))
_pluginManagementService.RemovePlugin(currentVersion, false);
// If the current version is from the workshop only unload it, the old folder will be orphaned and cleaned up later
else
_pluginManagementService.UnloadPlugin(currentVersion);
}

Expand Down Expand Up @@ -96,6 +102,8 @@ public async Task<EntryInstallResult> InstallAsync(IEntryDetails entry, IRelease
return EntryInstallResult.FromFailure(e.Message);
}

installedEntry.ApplyRelease(release);

_workshopService.SaveInstalledEntry(installedEntry);
return EntryInstallResult.FromSuccess(installedEntry);
}
Expand Down

0 comments on commit 18d7531

Please sign in to comment.