mirror of
https://github.com/xivdev/Penumbra.git
synced 2025-12-14 20:54:16 +01:00
Complete refactoring of most code, indiscriminate application of .editorconfig and general cleanup.
This commit is contained in:
parent
5332119a63
commit
a19ec226c5
84 changed files with 3168 additions and 1709 deletions
|
|
@ -1,156 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Dalamud.Plugin;
|
||||
using Lumina.Data.Files;
|
||||
using Penumbra.Hooks;
|
||||
using Penumbra.Util;
|
||||
using Penumbra.MetaData;
|
||||
|
||||
namespace Penumbra.Mods
|
||||
{
|
||||
public class MetaManager : IDisposable
|
||||
{
|
||||
private class FileInformation
|
||||
{
|
||||
public readonly object Data;
|
||||
public bool Changed;
|
||||
public FileInfo? CurrentFile;
|
||||
|
||||
public FileInformation( object data )
|
||||
=> Data = data;
|
||||
|
||||
public void Write( DirectoryInfo dir )
|
||||
{
|
||||
byte[] data = Data switch
|
||||
{
|
||||
EqdpFile eqdp => eqdp.WriteBytes(),
|
||||
EqpFile eqp => eqp.WriteBytes(),
|
||||
GmpFile gmp => gmp.WriteBytes(),
|
||||
EstFile est => est.WriteBytes(),
|
||||
ImcFile imc => imc.WriteBytes(),
|
||||
_ => throw new NotImplementedException(),
|
||||
};
|
||||
DisposeFile( CurrentFile );
|
||||
CurrentFile = TempFile.WriteNew( dir, data );
|
||||
Changed = false;
|
||||
}
|
||||
}
|
||||
|
||||
public const string TmpDirectory = "penumbrametatmp";
|
||||
|
||||
private readonly MetaDefaults _default;
|
||||
private readonly DirectoryInfo _dir;
|
||||
private readonly GameResourceManagement _resourceManagement;
|
||||
private readonly Dictionary< GamePath, FileInfo > _resolvedFiles;
|
||||
|
||||
private readonly HashSet< MetaManipulation > _currentManipulations = new();
|
||||
private readonly Dictionary< GamePath, FileInformation > _currentFiles = new();
|
||||
|
||||
private static void DisposeFile( FileInfo? file )
|
||||
{
|
||||
if( !( file?.Exists ?? false ) )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
file.Delete();
|
||||
}
|
||||
catch( Exception e )
|
||||
{
|
||||
PluginLog.Error( $"Could not delete temporary file \"{file.FullName}\":\n{e}" );
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
foreach( var file in _currentFiles )
|
||||
{
|
||||
_resolvedFiles.Remove( file.Key );
|
||||
DisposeFile( file.Value.CurrentFile );
|
||||
}
|
||||
|
||||
_currentManipulations.Clear();
|
||||
_currentFiles.Clear();
|
||||
ClearDirectory();
|
||||
_resourceManagement.ReloadPlayerResources();
|
||||
}
|
||||
|
||||
private void ClearDirectory()
|
||||
{
|
||||
if( _dir.Exists )
|
||||
{
|
||||
try
|
||||
{
|
||||
Directory.Delete( _dir.FullName, true );
|
||||
}
|
||||
catch( Exception e )
|
||||
{
|
||||
PluginLog.Error( $"Could not clear temporary metafile directory \"{_dir.FullName}\":\n{e}" );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public MetaManager( Dictionary< GamePath, FileInfo > resolvedFiles, DirectoryInfo modDir )
|
||||
{
|
||||
_resolvedFiles = resolvedFiles;
|
||||
_default = Service< MetaDefaults >.Get();
|
||||
_resourceManagement = Service< GameResourceManagement >.Get();
|
||||
_dir = new DirectoryInfo( Path.Combine( modDir.FullName, TmpDirectory ) );
|
||||
ClearDirectory();
|
||||
Directory.CreateDirectory( _dir.FullName );
|
||||
}
|
||||
|
||||
public void WriteNewFiles()
|
||||
{
|
||||
foreach( var kvp in _currentFiles.Where( kvp => kvp.Value.Changed ) )
|
||||
{
|
||||
kvp.Value.Write( _dir );
|
||||
_resolvedFiles[ kvp.Key ] = kvp.Value.CurrentFile!;
|
||||
}
|
||||
|
||||
_resourceManagement.ReloadPlayerResources();
|
||||
}
|
||||
|
||||
public bool ApplyMod( MetaManipulation m )
|
||||
{
|
||||
if( !_currentManipulations.Add( m ) )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var gamePath = m.CorrespondingFilename();
|
||||
try
|
||||
{
|
||||
if( !_currentFiles.TryGetValue( gamePath, out var file ) )
|
||||
{
|
||||
file = new FileInformation( _default.CreateNewFile( m ) ?? throw new IOException() )
|
||||
{
|
||||
Changed = true,
|
||||
CurrentFile = null,
|
||||
};
|
||||
_currentFiles[ gamePath ] = file;
|
||||
}
|
||||
|
||||
file.Changed |= m.Type switch
|
||||
{
|
||||
MetaType.Eqp => m.Apply( ( EqpFile )file.Data ),
|
||||
MetaType.Eqdp => m.Apply( ( EqdpFile )file.Data ),
|
||||
MetaType.Gmp => m.Apply( ( GmpFile )file.Data ),
|
||||
MetaType.Est => m.Apply( ( EstFile )file.Data ),
|
||||
MetaType.Imc => m.Apply( ( ImcFile )file.Data ),
|
||||
_ => throw new NotImplementedException(),
|
||||
};
|
||||
return true;
|
||||
}
|
||||
catch( Exception e )
|
||||
{
|
||||
PluginLog.Error( $"Could not obtain default file for manipulation {m.CorrespondingFilename()}:\n{e}" );
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue