mirror of
https://github.com/Ottermandias/Glamourer.git
synced 2026-02-21 06:57:44 +01:00
Make colors favoritable.
This commit is contained in:
parent
6ad9b56239
commit
4328f5d680
5 changed files with 161 additions and 23 deletions
|
|
@ -5,7 +5,6 @@ using System.Linq;
|
|||
using Dalamud.Interface.Internal.Notifications;
|
||||
using Glamourer.Services;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using OtterGui.Classes;
|
||||
using Penumbra.GameData.Structs;
|
||||
|
||||
|
|
@ -13,8 +12,10 @@ namespace Glamourer.Unlocks;
|
|||
|
||||
public class FavoriteManager : ISavable
|
||||
{
|
||||
private readonly SaveService _saveService;
|
||||
private readonly HashSet<ItemId> _favorites = new();
|
||||
private const int CurrentVersion = 1;
|
||||
private readonly SaveService _saveService;
|
||||
private readonly HashSet<ItemId> _favorites = new();
|
||||
private readonly HashSet<StainId> _favoriteColors = new();
|
||||
|
||||
public FavoriteManager(SaveService saveService)
|
||||
{
|
||||
|
|
@ -30,9 +31,23 @@ public class FavoriteManager : ISavable
|
|||
|
||||
try
|
||||
{
|
||||
var text = File.ReadAllText(file);
|
||||
var array = JsonConvert.DeserializeObject<uint[]>(text) ?? Array.Empty<uint>();
|
||||
_favorites.UnionWith(array.Select(i => (ItemId)i));
|
||||
var text = File.ReadAllText(file);
|
||||
if (text.StartsWith('['))
|
||||
{
|
||||
LoadV0(text);
|
||||
}
|
||||
else
|
||||
{
|
||||
var load = JsonConvert.DeserializeObject<LoadStruct>(text);
|
||||
switch (load.Version)
|
||||
{
|
||||
case 1:
|
||||
_favorites.UnionWith(load.FavoriteItems.Select(i => (ItemId)i));
|
||||
_favoriteColors.UnionWith(load.FavoriteColors.Select(i => (StainId)i));
|
||||
break;
|
||||
default: throw new Exception($"Unknown Version {load.Version}");
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
|
@ -40,6 +55,13 @@ public class FavoriteManager : ISavable
|
|||
}
|
||||
}
|
||||
|
||||
private void LoadV0(string text)
|
||||
{
|
||||
var array = JsonConvert.DeserializeObject<uint[]>(text) ?? Array.Empty<uint>();
|
||||
_favorites.UnionWith(array.Select(i => (ItemId)i));
|
||||
Save();
|
||||
}
|
||||
|
||||
public string ToFilename(FilenameService fileNames)
|
||||
=> fileNames.FavoriteFile;
|
||||
|
||||
|
|
@ -52,10 +74,20 @@ public class FavoriteManager : ISavable
|
|||
{
|
||||
Formatting = Formatting.Indented,
|
||||
};
|
||||
j.WriteStartObject();
|
||||
j.WritePropertyName(nameof(LoadStruct.Version));
|
||||
j.WriteValue(CurrentVersion);
|
||||
j.WritePropertyName(nameof(LoadStruct.FavoriteItems));
|
||||
j.WriteStartArray();
|
||||
foreach (var item in _favorites)
|
||||
j.WriteValue(item.Id);
|
||||
j.WriteEndArray();
|
||||
j.WritePropertyName(nameof(LoadStruct.FavoriteColors));
|
||||
j.WriteStartArray();
|
||||
foreach (var stain in _favoriteColors)
|
||||
j.WriteValue(stain.Id);
|
||||
j.WriteEndArray();
|
||||
j.WriteEndObject();
|
||||
}
|
||||
|
||||
public bool TryAdd(EquipItem item)
|
||||
|
|
@ -70,6 +102,18 @@ public class FavoriteManager : ISavable
|
|||
return true;
|
||||
}
|
||||
|
||||
public bool TryAdd(Stain stain)
|
||||
=> TryAdd(stain.RowIndex);
|
||||
|
||||
public bool TryAdd(StainId stain)
|
||||
{
|
||||
if (stain.Id == 0 || !_favoriteColors.Add(stain))
|
||||
return false;
|
||||
|
||||
Save();
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Remove(EquipItem item)
|
||||
=> Remove(item.ItemId);
|
||||
|
||||
|
|
@ -82,15 +126,37 @@ public class FavoriteManager : ISavable
|
|||
return true;
|
||||
}
|
||||
|
||||
public IEnumerator<ItemId> GetEnumerator()
|
||||
=> _favorites.GetEnumerator();
|
||||
public bool Remove(Stain stain)
|
||||
=> Remove(stain.RowIndex);
|
||||
|
||||
public int Count
|
||||
=> _favorites.Count;
|
||||
public bool Remove(StainId stain)
|
||||
{
|
||||
if (!_favoriteColors.Remove(stain))
|
||||
return false;
|
||||
|
||||
Save();
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Contains(EquipItem item)
|
||||
=> _favorites.Contains(item.ItemId);
|
||||
|
||||
public bool Contains(Stain stain)
|
||||
=> _favoriteColors.Contains(stain.RowIndex);
|
||||
|
||||
public bool Contains(ItemId item)
|
||||
=> _favorites.Contains(item);
|
||||
|
||||
public bool Contains(StainId stain)
|
||||
=> _favoriteColors.Contains(stain);
|
||||
|
||||
private struct LoadStruct
|
||||
{
|
||||
public int Version = CurrentVersion;
|
||||
public uint[] FavoriteItems = Array.Empty<uint>();
|
||||
public byte[] FavoriteColors = Array.Empty<byte>();
|
||||
|
||||
public LoadStruct()
|
||||
{ }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue