Merge remote-tracking branch 'origin/master' into net8-rollup

This commit is contained in:
github-actions[bot] 2024-02-11 18:20:47 +00:00
commit 5fbba87e59
73 changed files with 9530 additions and 1736 deletions

View file

@ -666,6 +666,15 @@ internal partial class PluginManager : IDisposable, IServiceType
this.PluginsReady = true;
this.NotifyinstalledPluginsListChanged();
sigScanner.Save();
try
{
this.ParanoiaValidatePluginsAndProfiles();
}
catch (Exception ex)
{
Log.Error(ex, "Plugin and profile validation failed!");
}
},
tokenSource.Token);
}
@ -1088,7 +1097,7 @@ internal partial class PluginManager : IDisposable, IServiceType
{
try
{
this.PluginConfigs.Delete(plugin.Name);
this.PluginConfigs.Delete(plugin.Manifest.InternalName);
break;
}
catch (IOException)
@ -1259,6 +1268,30 @@ internal partial class PluginManager : IDisposable, IServiceType
}
}
/// <summary>
/// Check if there are any inconsistencies with our plugins, their IDs, and our profiles.
/// </summary>
private void ParanoiaValidatePluginsAndProfiles()
{
var seenIds = new List<Guid>();
foreach (var installedPlugin in this.InstalledPlugins)
{
if (installedPlugin.Manifest.WorkingPluginId == Guid.Empty)
throw new Exception($"{(installedPlugin is LocalDevPlugin ? "DevPlugin" : "Plugin")} '{installedPlugin.Manifest.InternalName}' has an empty WorkingPluginId.");
if (seenIds.Contains(installedPlugin.Manifest.WorkingPluginId))
{
throw new Exception(
$"{(installedPlugin is LocalDevPlugin ? "DevPlugin" : "Plugin")} '{installedPlugin.Manifest.InternalName}' has a duplicate WorkingPluginId '{installedPlugin.Manifest.WorkingPluginId}'");
}
seenIds.Add(installedPlugin.Manifest.WorkingPluginId);
}
this.profileManager.ParanoiaValidateProfiles();
}
private async Task<Stream> DownloadPluginAsync(RemotePluginManifest repoManifest, bool useTesting)
{
var downloadUrl = useTesting ? repoManifest.DownloadLinkTesting : repoManifest.DownloadLinkInstall;
@ -1300,7 +1333,7 @@ internal partial class PluginManager : IDisposable, IServiceType
try
{
// We don't need to apply, it doesn't matter
await this.profileManager.DefaultProfile.RemoveAsync(repoManifest.InternalName, false);
await this.profileManager.DefaultProfile.RemoveByInternalNameAsync(repoManifest.InternalName, false);
}
catch (ProfileOperationException)
{
@ -1448,73 +1481,98 @@ internal partial class PluginManager : IDisposable, IServiceType
if (isDev)
{
Log.Information($"Loading dev plugin {name}");
var devPlugin = new LocalDevPlugin(dllFile, manifest);
loadPlugin &= !isBoot;
var probablyInternalNameForThisPurpose = manifest?.InternalName ?? dllFile.Name;
var wantsInDefaultProfile =
this.profileManager.DefaultProfile.WantsPlugin(probablyInternalNameForThisPurpose);
if (wantsInDefaultProfile == null)
{
// We don't know about this plugin, so we don't want to do anything here.
// The code below will take care of it and add it with the default value.
}
else if (wantsInDefaultProfile == false && devPlugin.StartOnBoot)
{
// We didn't want this plugin, and StartOnBoot is on. That means we don't want it and it should stay off until manually enabled.
Log.Verbose("DevPlugin {Name} disabled and StartOnBoot => disable", probablyInternalNameForThisPurpose);
await this.profileManager.DefaultProfile.AddOrUpdateAsync(probablyInternalNameForThisPurpose, false, false);
loadPlugin = false;
}
else if (wantsInDefaultProfile == true && devPlugin.StartOnBoot)
{
// We wanted this plugin, and StartOnBoot is on. That means we actually do want it.
Log.Verbose("DevPlugin {Name} enabled and StartOnBoot => enable", probablyInternalNameForThisPurpose);
await this.profileManager.DefaultProfile.AddOrUpdateAsync(probablyInternalNameForThisPurpose, true, false);
loadPlugin = !doNotLoad;
}
else if (wantsInDefaultProfile == true && !devPlugin.StartOnBoot)
{
// We wanted this plugin, but StartOnBoot is off. This means we don't want it anymore.
Log.Verbose("DevPlugin {Name} enabled and !StartOnBoot => disable", probablyInternalNameForThisPurpose);
await this.profileManager.DefaultProfile.AddOrUpdateAsync(probablyInternalNameForThisPurpose, false, false);
loadPlugin = false;
}
else if (wantsInDefaultProfile == false && !devPlugin.StartOnBoot)
{
// We didn't want this plugin, and StartOnBoot is off. We don't want it.
Log.Verbose("DevPlugin {Name} disabled and !StartOnBoot => disable", probablyInternalNameForThisPurpose);
await this.profileManager.DefaultProfile.AddOrUpdateAsync(probablyInternalNameForThisPurpose, false, false);
loadPlugin = false;
}
plugin = devPlugin;
plugin = new LocalDevPlugin(dllFile, manifest);
}
else
{
Log.Information($"Loading plugin {name}");
plugin = new LocalPlugin(dllFile, manifest);
}
// Perform a migration from InternalName to GUIDs. The plugin should definitely have a GUID here.
// This will also happen if you are installing a plugin with the installer, and that's intended!
// It means that, if you have a profile which has unsatisfied plugins, installing a matching plugin will
// enter it into the profiles it can match.
if (plugin.Manifest.WorkingPluginId == Guid.Empty)
throw new Exception("Plugin should have a WorkingPluginId at this point");
this.profileManager.MigrateProfilesToGuidsForPlugin(plugin.Manifest.InternalName, plugin.Manifest.WorkingPluginId);
var wantedByAnyProfile = false;
// Now, if this is a devPlugin, figure out if we want to load it
if (isDev)
{
var devPlugin = (LocalDevPlugin)plugin;
loadPlugin &= !isBoot;
var wantsInDefaultProfile =
this.profileManager.DefaultProfile.WantsPlugin(plugin.Manifest.WorkingPluginId);
if (wantsInDefaultProfile == null)
{
// We don't know about this plugin, so we don't want to do anything here.
// The code below will take care of it and add it with the default value.
Log.Verbose("DevPlugin {Name} not wanted in default plugin", plugin.Manifest.InternalName);
// Check if any profile wants this plugin. We need to do this here, since we want to allow loading a dev plugin if a non-default profile wants it active.
// Note that this will not add the plugin to the default profile. That's done below in any other case.
wantedByAnyProfile = await this.profileManager.GetWantStateAsync(plugin.Manifest.WorkingPluginId, plugin.Manifest.InternalName, false, false);
// If it is wanted by any other profile, we do want to load it.
if (wantedByAnyProfile)
loadPlugin = true;
}
else if (wantsInDefaultProfile == false && devPlugin.StartOnBoot)
{
// We didn't want this plugin, and StartOnBoot is on. That means we don't want it and it should stay off until manually enabled.
Log.Verbose("DevPlugin {Name} disabled and StartOnBoot => disable", plugin.Manifest.InternalName);
await this.profileManager.DefaultProfile.AddOrUpdateAsync(plugin.Manifest.WorkingPluginId, plugin.Manifest.InternalName, false, false);
loadPlugin = false;
}
else if (wantsInDefaultProfile == true && devPlugin.StartOnBoot)
{
// We wanted this plugin, and StartOnBoot is on. That means we actually do want it.
Log.Verbose("DevPlugin {Name} enabled and StartOnBoot => enable", plugin.Manifest.InternalName);
await this.profileManager.DefaultProfile.AddOrUpdateAsync(plugin.Manifest.WorkingPluginId, plugin.Manifest.InternalName, true, false);
loadPlugin = !doNotLoad;
}
else if (wantsInDefaultProfile == true && !devPlugin.StartOnBoot)
{
// We wanted this plugin, but StartOnBoot is off. This means we don't want it anymore.
Log.Verbose("DevPlugin {Name} enabled and !StartOnBoot => disable", plugin.Manifest.InternalName);
await this.profileManager.DefaultProfile.AddOrUpdateAsync(plugin.Manifest.WorkingPluginId, plugin.Manifest.InternalName, false, false);
loadPlugin = false;
}
else if (wantsInDefaultProfile == false && !devPlugin.StartOnBoot)
{
// We didn't want this plugin, and StartOnBoot is off. We don't want it.
Log.Verbose("DevPlugin {Name} disabled and !StartOnBoot => disable", plugin.Manifest.InternalName);
await this.profileManager.DefaultProfile.AddOrUpdateAsync(plugin.Manifest.WorkingPluginId, plugin.Manifest.InternalName, false, false);
loadPlugin = false;
}
plugin = devPlugin;
}
#pragma warning disable CS0618
var defaultState = manifest?.Disabled != true && loadPlugin;
#pragma warning restore CS0618
// Need to do this here, so plugins that don't load are still added to the default profile
var wantToLoad = await this.profileManager.GetWantStateAsync(plugin.Manifest.InternalName, defaultState);
// Plugins that aren't in any profile will be added to the default profile with this call.
// We are skipping a double-lookup for dev plugins that are wanted by non-default profiles, as noted above.
wantedByAnyProfile = wantedByAnyProfile || await this.profileManager.GetWantStateAsync(plugin.Manifest.WorkingPluginId, plugin.Manifest.InternalName, defaultState);
Log.Information("{Name} defaultState: {State} wantedByAnyProfile: {WantedByAny} loadPlugin: {LoadPlugin}", plugin.Manifest.InternalName, defaultState, wantedByAnyProfile, loadPlugin);
if (loadPlugin)
{
try
{
if (wantToLoad && !plugin.IsOrphaned)
if (wantedByAnyProfile && !plugin.IsOrphaned)
{
await plugin.LoadAsync(reason);
}
else
{
Log.Verbose($"{name} not loaded, wantToLoad:{wantToLoad} orphaned:{plugin.IsOrphaned}");
Log.Verbose($"{name} not loaded, wantToLoad:{wantedByAnyProfile} orphaned:{plugin.IsOrphaned}");
}
}
catch (InvalidPluginException)

View file

@ -102,7 +102,7 @@ internal class Profile
/// Gets all plugins declared in this profile.
/// </summary>
public IEnumerable<ProfilePluginEntry> Plugins =>
this.modelV1.Plugins.Select(x => new ProfilePluginEntry(x.InternalName, x.IsEnabled));
this.modelV1.Plugins.Select(x => new ProfilePluginEntry(x.InternalName, x.WorkingPluginId, x.IsEnabled));
/// <summary>
/// Gets this profile's underlying model.
@ -142,13 +142,13 @@ internal class Profile
/// <summary>
/// Check if this profile contains a specific plugin, and if it is enabled.
/// </summary>
/// <param name="internalName">The internal name of the plugin.</param>
/// <param name="workingPluginId">The ID of the plugin.</param>
/// <returns>Null if this profile does not declare the plugin, true if the profile declares the plugin and wants it enabled, false if the profile declares the plugin and does not want it enabled.</returns>
public bool? WantsPlugin(string internalName)
public bool? WantsPlugin(Guid workingPluginId)
{
lock (this)
{
var entry = this.modelV1.Plugins.FirstOrDefault(x => x.InternalName == internalName);
var entry = this.modelV1.Plugins.FirstOrDefault(x => x.WorkingPluginId == workingPluginId);
return entry?.IsEnabled;
}
}
@ -157,17 +157,18 @@ internal class Profile
/// Add a plugin to this profile with the desired state, or change the state of a plugin in this profile.
/// This will block until all states have been applied.
/// </summary>
/// <param name="internalName">The internal name of the plugin.</param>
/// <param name="workingPluginId">The ID of the plugin.</param>
/// <param name="internalName">The internal name of the plugin, if available.</param>
/// <param name="state">Whether or not the plugin should be enabled.</param>
/// <param name="apply">Whether or not the current state should immediately be applied.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public async Task AddOrUpdateAsync(string internalName, bool state, bool apply = true)
public async Task AddOrUpdateAsync(Guid workingPluginId, string? internalName, bool state, bool apply = true)
{
Debug.Assert(!internalName.IsNullOrEmpty(), "!internalName.IsNullOrEmpty()");
Debug.Assert(workingPluginId != Guid.Empty, "Trying to add plugin with empty guid");
lock (this)
{
var existing = this.modelV1.Plugins.FirstOrDefault(x => x.InternalName == internalName);
var existing = this.modelV1.Plugins.FirstOrDefault(x => x.WorkingPluginId == workingPluginId);
if (existing != null)
{
existing.IsEnabled = state;
@ -177,15 +178,55 @@ internal class Profile
this.modelV1.Plugins.Add(new ProfileModelV1.ProfileModelV1Plugin
{
InternalName = internalName,
WorkingPluginId = workingPluginId,
IsEnabled = state,
});
}
}
// We need to remove this plugin from the default profile, if it declares it.
if (!this.IsDefaultProfile && this.manager.DefaultProfile.WantsPlugin(internalName) != null)
if (!this.IsDefaultProfile && this.manager.DefaultProfile.WantsPlugin(workingPluginId) != null)
{
await this.manager.DefaultProfile.RemoveAsync(internalName, false);
await this.manager.DefaultProfile.RemoveAsync(workingPluginId, false);
}
Service<DalamudConfiguration>.Get().QueueSave();
if (apply)
await this.manager.ApplyAllWantStatesAsync();
}
/// <summary>
/// Remove a plugin from this profile.
/// This will block until all states have been applied.
/// </summary>
/// <param name="workingPluginId">The ID of the plugin.</param>
/// <param name="apply">Whether or not the current state should immediately be applied.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public async Task RemoveAsync(Guid workingPluginId, bool apply = true)
{
ProfileModelV1.ProfileModelV1Plugin entry;
lock (this)
{
entry = this.modelV1.Plugins.FirstOrDefault(x => x.WorkingPluginId == workingPluginId);
if (entry == null)
throw new PluginNotFoundException(workingPluginId);
if (!this.modelV1.Plugins.Remove(entry))
throw new Exception("Couldn't remove plugin from model collection");
}
// We need to add this plugin back to the default profile, if we were the last profile to have it.
if (!this.manager.IsInAnyProfile(workingPluginId))
{
if (!this.IsDefaultProfile)
{
await this.manager.DefaultProfile.AddOrUpdateAsync(workingPluginId, entry.InternalName, this.IsEnabled && entry.IsEnabled, false);
}
else
{
throw new PluginNotInDefaultProfileException(workingPluginId.ToString());
}
}
Service<DalamudConfiguration>.Get().QueueSave();
@ -201,36 +242,50 @@ internal class Profile
/// <param name="internalName">The internal name of the plugin.</param>
/// <param name="apply">Whether or not the current state should immediately be applied.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public async Task RemoveAsync(string internalName, bool apply = true)
public async Task RemoveByInternalNameAsync(string internalName, bool apply = true)
{
ProfileModelV1.ProfileModelV1Plugin entry;
Guid? pluginToRemove = null;
lock (this)
{
entry = this.modelV1.Plugins.FirstOrDefault(x => x.InternalName == internalName);
if (entry == null)
throw new PluginNotFoundException(internalName);
if (!this.modelV1.Plugins.Remove(entry))
throw new Exception("Couldn't remove plugin from model collection");
foreach (var plugin in this.Plugins)
{
if (plugin.InternalName.Equals(internalName, StringComparison.Ordinal))
{
pluginToRemove = plugin.WorkingPluginId;
break;
}
}
}
// We need to add this plugin back to the default profile, if we were the last profile to have it.
if (!this.manager.IsInAnyProfile(internalName))
await this.RemoveAsync(pluginToRemove ?? throw new PluginNotFoundException(internalName), apply);
}
/// <summary>
/// This function tries to migrate all plugins with this internalName which do not have
/// a GUID to the specified GUID.
/// This is best-effort and will probably work well for anyone that only uses regular plugins.
/// </summary>
/// <param name="internalName">InternalName of the plugin to migrate.</param>
/// <param name="newGuid">Guid to use.</param>
public void MigrateProfilesToGuidsForPlugin(string internalName, Guid newGuid)
{
lock (this)
{
if (!this.IsDefaultProfile)
foreach (var plugin in this.modelV1.Plugins)
{
await this.manager.DefaultProfile.AddOrUpdateAsync(internalName, this.IsEnabled && entry.IsEnabled, false);
}
else
{
throw new PluginNotInDefaultProfileException(internalName);
// TODO: What should happen if a profile has a GUID locked in, but the plugin
// is not installed anymore? That probably means that the user uninstalled the plugin
// and is now reinstalling it. We should still satisfy that and update the ID.
if (plugin.InternalName == internalName && plugin.WorkingPluginId == Guid.Empty)
{
plugin.WorkingPluginId = newGuid;
Log.Information("Migrated profile {Profile} plugin {Name} to guid {Guid}", this, internalName, newGuid);
}
}
}
Service<DalamudConfiguration>.Get().QueueSave();
if (apply)
await this.manager.ApplyAllWantStatesAsync();
}
/// <inheritdoc/>
@ -280,4 +335,13 @@ internal sealed class PluginNotFoundException : ProfileOperationException
: base($"The plugin '{internalName}' was not found in the profile")
{
}
/// <summary>
/// Initializes a new instance of the <see cref="PluginNotFoundException"/> class.
/// </summary>
/// <param name="workingPluginId">The ID of the plugin causing the error.</param>
public PluginNotFoundException(Guid workingPluginId)
: base($"The plugin '{workingPluginId}' was not found in the profile")
{
}
}

View file

@ -69,11 +69,12 @@ internal class ProfileManager : IServiceType
/// <summary>
/// Check if any enabled profile wants a specific plugin enabled.
/// </summary>
/// <param name="internalName">The internal name of the plugin.</param>
/// <param name="workingPluginId">The ID of the plugin.</param>
/// <param name="internalName">The internal name of the plugin, if available.</param>
/// <param name="defaultState">The state the plugin shall be in, if it needs to be added.</param>
/// <param name="addIfNotDeclared">Whether or not the plugin should be added to the default preset, if it's not present in any preset.</param>
/// <returns>Whether or not the plugin shall be enabled.</returns>
public async Task<bool> GetWantStateAsync(string internalName, bool defaultState, bool addIfNotDeclared = true)
public async Task<bool> GetWantStateAsync(Guid workingPluginId, string? internalName, bool defaultState, bool addIfNotDeclared = true)
{
var want = false;
var wasInAnyProfile = false;
@ -82,7 +83,7 @@ internal class ProfileManager : IServiceType
{
foreach (var profile in this.profiles)
{
var state = profile.WantsPlugin(internalName);
var state = profile.WantsPlugin(workingPluginId);
if (state.HasValue)
{
want = want || (profile.IsEnabled && state.Value);
@ -93,8 +94,8 @@ internal class ProfileManager : IServiceType
if (!wasInAnyProfile && addIfNotDeclared)
{
Log.Warning("{Name} was not in any profile, adding to default with {Default}", internalName, defaultState);
await this.DefaultProfile.AddOrUpdateAsync(internalName, defaultState, false);
Log.Warning("'{Guid}'('{InternalName}') was not in any profile, adding to default with {Default}", workingPluginId, internalName, defaultState);
await this.DefaultProfile.AddOrUpdateAsync(workingPluginId, internalName, defaultState, false);
return defaultState;
}
@ -105,22 +106,22 @@ internal class ProfileManager : IServiceType
/// <summary>
/// Check whether a plugin is declared in any profile.
/// </summary>
/// <param name="internalName">The internal name of the plugin.</param>
/// <param name="workingPluginId">The ID of the plugin.</param>
/// <returns>Whether or not the plugin is in any profile.</returns>
public bool IsInAnyProfile(string internalName)
public bool IsInAnyProfile(Guid workingPluginId)
{
lock (this.profiles)
return this.profiles.Any(x => x.WantsPlugin(internalName) != null);
return this.profiles.Any(x => x.WantsPlugin(workingPluginId) != null);
}
/// <summary>
/// Check whether a plugin is only in the default profile.
/// A plugin can never be in the default profile if it is in any other profile.
/// </summary>
/// <param name="internalName">The internal name of the plugin.</param>
/// <param name="workingPluginId">The ID of the plugin.</param>
/// <returns>Whether or not the plugin is in the default profile.</returns>
public bool IsInDefaultProfile(string internalName)
=> this.DefaultProfile.WantsPlugin(internalName) != null;
public bool IsInDefaultProfile(Guid workingPluginId)
=> this.DefaultProfile.WantsPlugin(workingPluginId) != null;
/// <summary>
/// Add a new profile.
@ -151,7 +152,7 @@ internal class ProfileManager : IServiceType
/// <returns>The newly cloned profile.</returns>
public Profile CloneProfile(Profile toClone)
{
var newProfile = this.ImportProfile(toClone.Model.Serialize());
var newProfile = this.ImportProfile(toClone.Model.SerializeForShare());
if (newProfile == null)
throw new Exception("New profile was null while cloning");
@ -172,7 +173,27 @@ internal class ProfileManager : IServiceType
newModel.Guid = Guid.NewGuid();
newModel.Name = this.GenerateUniqueProfileName(newModel.Name.IsNullOrEmpty() ? "Unknown Collection" : newModel.Name);
if (newModel is ProfileModelV1 modelV1)
{
// Disable it
modelV1.IsEnabled = false;
// Try to find matching plugins for all plugins in the profile
var pm = Service<PluginManager>.Get();
foreach (var plugin in modelV1.Plugins)
{
var installedPlugin = pm.InstalledPlugins.FirstOrDefault(x => x.Manifest.InternalName == plugin.InternalName);
if (installedPlugin != null)
{
Log.Information("Satisfying plugin {InternalName} for profile {Name} with {Guid}", plugin.InternalName, newModel.Name, installedPlugin.Manifest.WorkingPluginId);
plugin.WorkingPluginId = installedPlugin.Manifest.WorkingPluginId;
}
else
{
Log.Warning("Couldn't find plugin {InternalName} for profile {Name}", plugin.InternalName, newModel.Name);
plugin.WorkingPluginId = Guid.Empty;
}
}
}
this.config.SavedProfiles!.Add(newModel);
this.config.QueueSave();
@ -196,19 +217,18 @@ internal class ProfileManager : IServiceType
this.isBusy = true;
Log.Information("Getting want states...");
List<string> wantActive;
List<ProfilePluginEntry> wantActive;
lock (this.profiles)
{
wantActive = this.profiles
.Where(x => x.IsEnabled)
.SelectMany(profile => profile.Plugins.Where(plugin => plugin.IsEnabled)
.Select(plugin => plugin.InternalName))
.SelectMany(profile => profile.Plugins.Where(plugin => plugin.IsEnabled))
.Distinct().ToList();
}
foreach (var internalName in wantActive)
foreach (var profilePluginEntry in wantActive)
{
Log.Information("\t=> Want {Name}", internalName);
Log.Information("\t=> Want {Name}({WorkingPluginId})", profilePluginEntry.InternalName, profilePluginEntry.WorkingPluginId);
}
Log.Information("Applying want states...");
@ -218,7 +238,7 @@ internal class ProfileManager : IServiceType
var pm = Service<PluginManager>.Get();
foreach (var installedPlugin in pm.InstalledPlugins)
{
var wantThis = wantActive.Contains(installedPlugin.Manifest.InternalName);
var wantThis = wantActive.Any(x => x.WorkingPluginId == installedPlugin.Manifest.WorkingPluginId);
switch (wantThis)
{
case true when !installedPlugin.IsLoaded:
@ -267,7 +287,7 @@ internal class ProfileManager : IServiceType
// We need to remove all plugins from the profile first, so that they are re-added to the default profile if needed
foreach (var plugin in profile.Plugins.ToArray())
{
await profile.RemoveAsync(plugin.InternalName, false);
await profile.RemoveAsync(plugin.WorkingPluginId, false);
}
if (!this.config.SavedProfiles!.Remove(profile.Model))
@ -279,6 +299,42 @@ internal class ProfileManager : IServiceType
this.config.QueueSave();
}
/// <summary>
/// This function tries to migrate all plugins with this internalName which do not have
/// a GUID to the specified GUID.
/// This is best-effort and will probably work well for anyone that only uses regular plugins.
/// </summary>
/// <param name="internalName">InternalName of the plugin to migrate.</param>
/// <param name="newGuid">Guid to use.</param>
public void MigrateProfilesToGuidsForPlugin(string internalName, Guid newGuid)
{
lock (this.profiles)
{
foreach (var profile in this.profiles)
profile.MigrateProfilesToGuidsForPlugin(internalName, newGuid);
}
}
/// <summary>
/// Validate profiles for errors.
/// </summary>
/// <exception cref="Exception">Thrown when a profile is not sane.</exception>
public void ParanoiaValidateProfiles()
{
foreach (var profile in this.profiles)
{
var seenIds = new List<Guid>();
foreach (var pluginEntry in profile.Plugins)
{
if (seenIds.Contains(pluginEntry.WorkingPluginId))
throw new Exception($"Plugin '{pluginEntry.WorkingPluginId}'('{pluginEntry.InternalName}') is twice in profile '{profile.Guid}'('{profile.Name}')");
seenIds.Add(pluginEntry.WorkingPluginId);
}
}
}
private string GenerateUniqueProfileName(string startingWith)
{
if (this.profiles.All(x => x.Name != startingWith))

View file

@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using Dalamud.Utility;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace Dalamud.Plugin.Internal.Profiles;
@ -39,11 +41,11 @@ public abstract class ProfileModel
}
/// <summary>
/// Serialize this model into a string usable for sharing.
/// Serialize this model into a string usable for sharing, without including GUIDs.
/// </summary>
/// <returns>The serialized representation of the model.</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown when an unsupported model is serialized.</exception>
public string Serialize()
public string SerializeForShare()
{
string prefix;
switch (this)
@ -55,6 +57,32 @@ public abstract class ProfileModel
throw new ArgumentOutOfRangeException();
}
return prefix + Convert.ToBase64String(Util.CompressString(JsonConvert.SerializeObject(this)));
// HACK: Just filter the ID for now, we should split the sharing + saving model
var serialized = JsonConvert.SerializeObject(this, new JsonSerializerSettings()
{ ContractResolver = new IgnorePropertiesResolver(new[] { "WorkingPluginId" }) });
return prefix + Convert.ToBase64String(Util.CompressString(serialized));
}
// Short helper class to ignore some properties from serialization
private class IgnorePropertiesResolver : DefaultContractResolver
{
private readonly HashSet<string> ignoreProps;
public IgnorePropertiesResolver(IEnumerable<string> propNamesToIgnore)
{
this.ignoreProps = new HashSet<string>(propNamesToIgnore);
}
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
var property = base.CreateProperty(member, memberSerialization);
if (this.ignoreProps.Contains(property.PropertyName))
{
property.ShouldSerialize = _ => false;
}
return property;
}
}
}

View file

@ -46,6 +46,11 @@ public class ProfileModelV1 : ProfileModel
/// Gets or sets the internal name of the plugin.
/// </summary>
public string? InternalName { get; set; }
/// <summary>
/// Gets or sets an ID uniquely identifying this specific instance of a plugin.
/// </summary>
public Guid WorkingPluginId { get; set; }
/// <summary>
/// Gets or sets a value indicating whether or not this entry is enabled.

View file

@ -9,10 +9,12 @@ internal class ProfilePluginEntry
/// Initializes a new instance of the <see cref="ProfilePluginEntry"/> class.
/// </summary>
/// <param name="internalName">The internal name of the plugin.</param>
/// <param name="workingPluginId">The ID of the plugin.</param>
/// <param name="state">A value indicating whether or not this entry is enabled.</param>
public ProfilePluginEntry(string internalName, bool state)
public ProfilePluginEntry(string internalName, Guid workingPluginId, bool state)
{
this.InternalName = internalName;
this.WorkingPluginId = workingPluginId;
this.IsEnabled = state;
}
@ -20,6 +22,11 @@ internal class ProfilePluginEntry
/// Gets the internal name of the plugin.
/// </summary>
public string InternalName { get; }
/// <summary>
/// Gets or sets an ID uniquely identifying this specific instance of a plugin.
/// </summary>
public Guid WorkingPluginId { get; set; }
/// <summary>
/// Gets a value indicating whether or not this entry is enabled.

View file

@ -164,7 +164,7 @@ internal class LocalPlugin : IDisposable
/// INCLUDES the default profile.
/// </summary>
public bool IsWantedByAnyProfile =>
Service<ProfileManager>.Get().GetWantStateAsync(this.manifest.InternalName, false, false).GetAwaiter().GetResult();
Service<ProfileManager>.Get().GetWantStateAsync(this.manifest.WorkingPluginId, this.Manifest.InternalName, false, false).GetAwaiter().GetResult();
/// <summary>
/// Gets a value indicating whether this plugin's API level is out of date.