Add Filesystem Compression as a toggle and button. Also some auto-formatting.

This commit is contained in:
Ottermandias 2023-09-14 17:23:54 +02:00
parent e26873934b
commit 4e704770cb
21 changed files with 385 additions and 344 deletions

View file

@ -9,6 +9,7 @@ using Dalamud.Plugin.Services;
using ImGuiNET;
using OtterGui;
using OtterGui.Classes;
using OtterGui.Compression;
using OtterGui.Raii;
using OtterGui.Widgets;
using Penumbra.GameData.Files;
@ -18,15 +19,16 @@ using Penumbra.UI.Classes;
namespace Penumbra.UI.AdvancedWindow;
public class FileEditor<T> : IDisposable where T : class, IWritable
public class FileEditor<T> : IDisposable where T : class, IWritable
{
private readonly FileDialogService _fileDialog;
private readonly IDataManager _gameData;
private readonly IDataManager _gameData;
private readonly ModEditWindow _owner;
private readonly FileCompactor _compactor;
public FileEditor(ModEditWindow owner, IDataManager gameData, Configuration config, FileDialogService fileDialog, string tabName,
string fileType, Func<IReadOnlyList<FileRegistry>> getFiles, Func<T, bool, bool> drawEdit, Func<string> getInitialPath,
Func<byte[], string, bool, T?> parseFile)
public FileEditor(ModEditWindow owner, IDataManager gameData, Configuration config, FileCompactor compactor, FileDialogService fileDialog,
string tabName, string fileType, Func<IReadOnlyList<FileRegistry>> getFiles, Func<T, bool, bool> drawEdit, Func<string> getInitialPath,
Func<byte[], string, bool, T?> parseFile)
{
_owner = owner;
_gameData = gameData;
@ -36,6 +38,7 @@ public class FileEditor<T> : IDisposable where T : class, IWritable
_drawEdit = drawEdit;
_getInitialPath = getInitialPath;
_parseFile = parseFile;
_compactor = compactor;
_combo = new Combo(config, getFiles);
}
@ -60,19 +63,19 @@ public class FileEditor<T> : IDisposable where T : class, IWritable
DrawFilePanel();
}
public void Dispose()
{
(_currentFile as IDisposable)?.Dispose();
public void Dispose()
{
(_currentFile as IDisposable)?.Dispose();
_currentFile = null;
(_defaultFile as IDisposable)?.Dispose();
_defaultFile = null;
}
private readonly string _tabName;
private readonly string _fileType;
private readonly Func<T, bool, bool> _drawEdit;
private readonly Func<string> _getInitialPath;
private readonly Func<byte[], string, bool, T?> _parseFile;
}
private readonly string _tabName;
private readonly string _fileType;
private readonly Func<T, bool, bool> _drawEdit;
private readonly Func<string> _getInitialPath;
private readonly Func<byte[], string, bool, T?> _parseFile;
private FileRegistry? _currentPath;
private T? _currentFile;
@ -107,9 +110,9 @@ public class FileEditor<T> : IDisposable where T : class, IWritable
if (file != null)
{
_defaultException = null;
(_defaultFile as IDisposable)?.Dispose();
_defaultFile = null; // Avoid double disposal if an exception occurs during the parsing of the new file.
_defaultFile = _parseFile(file.Data, _defaultPath, false);
(_defaultFile as IDisposable)?.Dispose();
_defaultFile = null; // Avoid double disposal if an exception occurs during the parsing of the new file.
_defaultFile = _parseFile(file.Data, _defaultPath, false);
}
else
{
@ -135,7 +138,7 @@ public class FileEditor<T> : IDisposable where T : class, IWritable
try
{
File.WriteAllBytes(name, _defaultFile?.Write() ?? throw new Exception("File invalid."));
_compactor.WriteAllBytes(name, _defaultFile?.Write() ?? throw new Exception("File invalid."));
}
catch (Exception e)
{
@ -168,9 +171,9 @@ public class FileEditor<T> : IDisposable where T : class, IWritable
{
_currentException = null;
_currentPath = null;
(_currentFile as IDisposable)?.Dispose();
_currentFile = null;
_changed = false;
(_currentFile as IDisposable)?.Dispose();
_currentFile = null;
_changed = false;
}
private void DrawFileSelectCombo()
@ -192,13 +195,13 @@ public class FileEditor<T> : IDisposable where T : class, IWritable
try
{
var bytes = File.ReadAllBytes(_currentPath.File.FullName);
(_currentFile as IDisposable)?.Dispose();
_currentFile = null; // Avoid double disposal if an exception occurs during the parsing of the new file.
_currentFile = _parseFile(bytes, _currentPath.File.FullName, true);
(_currentFile as IDisposable)?.Dispose();
_currentFile = null; // Avoid double disposal if an exception occurs during the parsing of the new file.
_currentFile = _parseFile(bytes, _currentPath.File.FullName, true);
}
catch (Exception e)
{
(_currentFile as IDisposable)?.Dispose();
(_currentFile as IDisposable)?.Dispose();
_currentFile = null;
_currentException = e;
}
@ -209,7 +212,7 @@ public class FileEditor<T> : IDisposable where T : class, IWritable
if (ImGuiUtil.DrawDisabledButton("Save to File", Vector2.Zero,
$"Save the selected {_fileType} file with all changes applied. This is not revertible.", !_changed))
{
File.WriteAllBytes(_currentPath!.File.FullName, _currentFile!.Write());
_compactor.WriteAllBytes(_currentPath!.File.FullName, _currentFile!.Write());
_changed = false;
}
}