Improve Imc Handling.

This commit is contained in:
Ottermandias 2024-06-18 17:52:34 +02:00
parent d7a8c9415b
commit 03d3c38ad5
16 changed files with 197 additions and 233 deletions

View file

@ -1,20 +1,22 @@
using Penumbra.GameData.Structs;
using Penumbra.Interop.PathResolving;
using Penumbra.Meta;
using Penumbra.Meta.Files;
using Penumbra.Meta.Manipulations;
using Penumbra.String.Classes;
using Penumbra.String;
namespace Penumbra.Collections.Cache;
public sealed class ImcCache(MetaFileManager manager, ModCollection collection) : MetaCacheBase<ImcIdentifier, ImcEntry>(manager, collection)
{
private readonly Dictionary<Utf8GamePath, (ImcFile, HashSet<ImcIdentifier>)> _imcFiles = [];
private readonly Dictionary<ByteString, (ImcFile, HashSet<ImcIdentifier>)> _imcFiles = [];
public override void SetFiles()
=> SetFiles(false);
{ }
public bool GetFile(Utf8GamePath path, [NotNullWhen(true)] out ImcFile? file)
public bool HasFile(ByteString path)
=> _imcFiles.ContainsKey(path);
public bool GetFile(ByteString path, [NotNullWhen(true)] out ImcFile? file)
{
if (!_imcFiles.TryGetValue(path, out var p))
{
@ -26,56 +28,31 @@ public sealed class ImcCache(MetaFileManager manager, ModCollection collection)
return true;
}
public void SetFiles(bool fromFullCompute)
{
if (fromFullCompute)
foreach (var (path, _) in _imcFiles)
Collection._cache!.ForceFileSync(path, PathDataHandler.CreateImc(path.Path, Collection));
else
foreach (var (path, _) in _imcFiles)
Collection._cache!.ForceFile(path, PathDataHandler.CreateImc(path.Path, Collection));
}
public void ResetFiles()
{
foreach (var (path, _) in _imcFiles)
Collection._cache!.ForceFile(path, FullPath.Empty);
}
protected override void IncorporateChangesInternal()
{
if (!Manager.CharacterUtility.Ready)
return;
foreach (var (identifier, (_, entry)) in this)
ApplyFile(identifier, entry);
Penumbra.Log.Verbose($"{Collection.AnonymizedName}: Loaded {Count} delayed IMC manipulations.");
}
{ }
public void Reset()
{
foreach (var (path, (file, set)) in _imcFiles)
foreach (var (_, (file, set)) in _imcFiles)
{
Collection._cache!.RemovePath(path);
file.Reset();
set.Clear();
}
_imcFiles.Clear();
Clear();
}
protected override void ApplyModInternal(ImcIdentifier identifier, ImcEntry entry)
{
++Collection.ImcChangeCounter;
if (Manager.CharacterUtility.Ready)
ApplyFile(identifier, entry);
ApplyFile(identifier, entry);
}
private void ApplyFile(ImcIdentifier identifier, ImcEntry entry)
{
var path = identifier.GamePath();
var path = identifier.GamePath().Path;
try
{
if (!_imcFiles.TryGetValue(path, out var pair))
@ -87,8 +64,6 @@ public sealed class ImcCache(MetaFileManager manager, ModCollection collection)
pair.Item2.Add(identifier);
_imcFiles[path] = pair;
var fullPath = PathDataHandler.CreateImc(pair.Item1.Path.Path, Collection);
Collection._cache!.ForceFile(path, fullPath);
}
catch (ImcException e)
{
@ -104,7 +79,7 @@ public sealed class ImcCache(MetaFileManager manager, ModCollection collection)
protected override void RevertModInternal(ImcIdentifier identifier)
{
++Collection.ImcChangeCounter;
var path = identifier.GamePath();
var path = identifier.GamePath().Path;
if (!_imcFiles.TryGetValue(path, out var pair))
return;
@ -114,17 +89,12 @@ public sealed class ImcCache(MetaFileManager manager, ModCollection collection)
if (pair.Item2.Count == 0)
{
_imcFiles.Remove(path);
Collection._cache!.ForceFile(pair.Item1.Path, FullPath.Empty);
pair.Item1.Dispose();
return;
}
var def = ImcFile.GetDefault(Manager, pair.Item1.Path, identifier.EquipSlot, identifier.Variant, out _);
if (!Apply(pair.Item1, identifier, def))
return;
var fullPath = PathDataHandler.CreateImc(pair.Item1.Path.Path, Collection);
Collection._cache!.ForceFile(pair.Item1.Path, fullPath);
Apply(pair.Item1, identifier, def);
}
public static bool Apply(ImcFile file, ImcIdentifier identifier, ImcEntry entry)