Added profile priority system, fixed a few issues

This commit is contained in:
RisaDev
2024-10-19 04:51:51 +03:00
parent 7085cf616c
commit 93802e6115
11 changed files with 119 additions and 60 deletions

View File

@@ -55,6 +55,11 @@ public sealed class Profile : ISavable
public ProfileType ProfileType { get; set; }
/// <summary>
/// Profile priority when there are several profiles affecting same character
/// </summary>
public int Priority { get; set; }
/// <summary>
/// Tells us if this profile is not persistent (ex. was made via IPC calls) and should have specific treatement like not being shown in UI, etc.
/// WARNING, TEMPLATES FOR TEMPORARY PROFILES *ARE NOT* STORED IN TemplateManager
@@ -107,6 +112,7 @@ public sealed class Profile : ISavable
["Name"] = Name.Text,
["Enabled"] = Enabled,
["IsWriteProtected"] = IsWriteProtected,
["Priority"] = Priority,
["Templates"] = SerializeTemplates()
};

View File

@@ -15,6 +15,7 @@ public sealed class ProfileChanged() : EventWrapper<ProfileChanged.Type, Profile
Deleted,
Renamed,
Toggled,
PriorityChanged,
AddedCharacter,
RemovedCharacter,
//ChangedCharacter,

View File

@@ -13,6 +13,7 @@ using Penumbra.String;
using Penumbra.GameData.Structs;
using Dalamud.Game.ClientState.Objects.Enums;
using Penumbra.GameData.Gui;
using System.Xml;
namespace CustomizePlus.Profiles;
@@ -53,20 +54,16 @@ public partial class ProfileManager : IDisposable
foreach (var profile in Profiles)
{
//This will solve any issues if file on disk was manually edited and we have more than a single active profile
if (profile.Enabled)
SetEnabled(profile, true, true);
if (_configuration.DefaultProfile == profile.UniqueId)
DefaultProfile = profile;
if (_configuration.DefaultLocalPlayerProfile == profile.UniqueId)
DefaultLocalPlayerProfile = profile;
}
//insert temp profiles back into profile list
if (temporaryProfiles.Count > 0)
{
Profiles.AddRange(temporaryProfiles);
Profiles.Sort((x, y) => y.IsTemporary.CompareTo(x.IsTemporary));
}
var failed = MoveInvalidNames(invalidNames);
if (invalidNames.Count > 0)
@@ -135,6 +132,8 @@ public partial class ProfileManager : IDisposable
{
var profile = LoadProfileV4V5(obj);
profile.Priority = obj["Priority"]?.ToObject<int>() ?? throw new ArgumentNullException("Priority");
if (obj["Characters"] is not JArray characterArray)
return profile;

View File

@@ -248,51 +248,11 @@ public partial class ProfileManager : IDisposable
if (profile.Enabled == value && !force)
return;
var oldValue = profile.Enabled;
profile.Enabled = value;
if (value)
{
_logger.Debug($"Setting {profile} as enabled...");
SaveProfile(profile);
foreach (var otherProfile in Profiles)
{
if (otherProfile == profile || !otherProfile.Enabled || otherProfile.IsTemporary)
continue;
bool shouldDisable = false;
//my god this is ugly
foreach(var otherCharacter in otherProfile.Characters)
{
foreach(var currentCharacter in profile.Characters)
{
if(otherCharacter.MatchesIgnoringOwnership(currentCharacter))
{
shouldDisable = true;
break;
}
}
if (shouldDisable)
break;
}
if(shouldDisable)
{
_logger.Debug($"\t-> {otherProfile} disabled");
SetEnabled(otherProfile, false);
}
}
}
if (oldValue != value)
{
profile.Enabled = value;
SaveProfile(profile);
_event.Invoke(ProfileChanged.Type.Toggled, profile, value);
}
_event.Invoke(ProfileChanged.Type.Toggled, profile, value);
}
public void SetEnabled(Guid guid, bool value)
@@ -306,6 +266,21 @@ public partial class ProfileManager : IDisposable
throw new ProfileNotFoundException();
}
public void SetPriority(Profile profile, int value)
{
if (profile.Priority == value)
return;
if (value > int.MaxValue || value < int.MinValue)
return;
profile.Priority = value;
SaveProfile(profile);
_event.Invoke(ProfileChanged.Type.PriorityChanged, profile, value);
}
public void DeleteTemplate(Profile profile, int templateIndex)
{
_logger.Debug($"Deleting template #{templateIndex} from {profile}...");
@@ -408,6 +383,7 @@ public partial class ProfileManager : IDisposable
profile.Enabled = true;
profile.ProfileType = ProfileType.Temporary;
profile.Priority = int.MaxValue; //Make sure temporary profile is always at max priority
var permanentIdentifier = identifier.CreatePermanent();
profile.Characters.Clear();
@@ -423,9 +399,6 @@ public partial class ProfileManager : IDisposable
Profiles.Add(profile);
//Make sure temporary profiles come first, so they are returned by all other methods first
Profiles.Sort((x, y) => y.IsTemporary.CompareTo(x.IsTemporary));
_logger.Debug($"Added temporary profile for {permanentIdentifier}");
_event.Invoke(ProfileChanged.Type.TemporaryProfileAdded, profile, null);
}
@@ -457,7 +430,7 @@ public partial class ProfileManager : IDisposable
if (!actor.Identifier(_actorManager, out var identifier))
throw new ActorNotFoundException();
var profile = Profiles.FirstOrDefault(x => x.Characters[0] == identifier && x.IsTemporary);
var profile = Profiles.FirstOrDefault(x => x.Characters.Count == 1 && x.Characters[0] == identifier && x.IsTemporary);
if (profile == null)
throw new ProfileNotFoundException();
@@ -478,7 +451,7 @@ public partial class ProfileManager : IDisposable
if (enabledOnly)
query = query.Where(x => x.Enabled);
var profile = query.FirstOrDefault();
var profile = query.OrderByDescending(x => x.Priority).FirstOrDefault();
if (profile == null)
return null;
@@ -529,7 +502,7 @@ public partial class ProfileManager : IDisposable
if (_templateEditorManager.IsEditorActive && _templateEditorManager.EditorProfile.Enabled && IsProfileAppliesToCurrentActor(_templateEditorManager.EditorProfile))
yield return _templateEditorManager.EditorProfile;
foreach (var profile in Profiles)
foreach (var profile in Profiles.OrderByDescending(x => x.Priority))
{
if(profile.Enabled && IsProfileAppliesToCurrentActor(profile))
yield return profile;
@@ -553,7 +526,7 @@ public partial class ProfileManager : IDisposable
if (template == null)
yield break;
foreach (var profile in Profiles)
foreach (var profile in Profiles.OrderByDescending(x => x.Priority))
if (profile.Templates.Contains(template))
yield return profile;