mirror of
https://github.com/xivdev/Penumbra.git
synced 2025-12-26 02:19:20 +01:00
meta tmp
This commit is contained in:
parent
9037166d92
commit
0186f176d0
11 changed files with 356 additions and 461 deletions
114
Penumbra/Collections/Cache/ImcCache.cs
Normal file
114
Penumbra/Collections/Cache/ImcCache.cs
Normal file
|
|
@ -0,0 +1,114 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using OtterGui.Filesystem;
|
||||
using Penumbra.Meta.Files;
|
||||
using Penumbra.Meta.Manipulations;
|
||||
using Penumbra.String.Classes;
|
||||
|
||||
namespace Penumbra.Collections.Cache;
|
||||
|
||||
public readonly struct ImcCache : IDisposable
|
||||
{
|
||||
private readonly Dictionary< Utf8GamePath, ImcFile > _imcFiles = new();
|
||||
private readonly List< ImcManipulation > _imcManipulations = new();
|
||||
|
||||
public ImcCache()
|
||||
{ }
|
||||
|
||||
public void SetFiles(CollectionCacheManager manager, ModCollection collection)
|
||||
{
|
||||
foreach( var path in _imcFiles.Keys )
|
||||
collection._cache!.ForceFile( path, CreateImcPath( collection, path ) );
|
||||
}
|
||||
|
||||
public void Reset(CollectionCacheManager manager, ModCollection collection)
|
||||
{
|
||||
foreach( var (path, file) in _imcFiles )
|
||||
{
|
||||
collection._cache!.RemoveFile( path );
|
||||
file.Reset();
|
||||
}
|
||||
|
||||
_imcManipulations.Clear();
|
||||
}
|
||||
|
||||
public bool ApplyMod( CollectionCacheManager manager, ModCollection collection, ImcManipulation manip )
|
||||
{
|
||||
if( !manip.Valid )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
_imcManipulations.AddOrReplace( manip );
|
||||
var path = manip.GamePath();
|
||||
try
|
||||
{
|
||||
if( !_imcFiles.TryGetValue( path, out var file ) )
|
||||
{
|
||||
file = new ImcFile( manip );
|
||||
}
|
||||
|
||||
if( !manip.Apply( file ) )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
_imcFiles[ path ] = file;
|
||||
var fullPath = CreateImcPath( collection, path );
|
||||
collection._cache!.ForceFile( path, fullPath );
|
||||
|
||||
return true;
|
||||
}
|
||||
catch( ImcException e )
|
||||
{
|
||||
manager.ValidityChecker.ImcExceptions.Add( e );
|
||||
Penumbra.Log.Error( e.ToString() );
|
||||
}
|
||||
catch( Exception e )
|
||||
{
|
||||
Penumbra.Log.Error( $"Could not apply IMC Manipulation {manip}:\n{e}" );
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool RevertMod( CollectionCacheManager manager, ModCollection collection, ImcManipulation m )
|
||||
{
|
||||
if( !m.Valid || !_imcManipulations.Remove( m ) )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var path = m.GamePath();
|
||||
if( !_imcFiles.TryGetValue( path, out var file ) )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var def = ImcFile.GetDefault( path, m.EquipSlot, m.Variant, out _ );
|
||||
var manip = m.Copy( def );
|
||||
if( !manip.Apply( file ) )
|
||||
return false;
|
||||
|
||||
var fullPath = CreateImcPath( collection, path );
|
||||
collection._cache!.ForceFile( path, fullPath );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
foreach( var file in _imcFiles.Values )
|
||||
file.Dispose();
|
||||
|
||||
_imcFiles.Clear();
|
||||
_imcManipulations.Clear();
|
||||
}
|
||||
|
||||
private static FullPath CreateImcPath( ModCollection collection, Utf8GamePath path )
|
||||
=> new($"|{collection.Name}_{collection.ChangeCounter}|{path}");
|
||||
|
||||
public bool GetImcFile(Utf8GamePath path, [NotNullWhen(true)] out ImcFile? file)
|
||||
=> _imcFiles.TryGetValue(path, out file);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue