Add ExportManager.

This commit is contained in:
Ottermandias 2023-03-28 16:58:20 +02:00
parent 2b7292adb8
commit a8000fbf14
11 changed files with 141 additions and 89 deletions

View file

@ -10,17 +10,15 @@ public class ModBackup
{
public static bool CreatingBackup { get; private set; }
private readonly ModManager _modManager;
private readonly Mod _mod;
public readonly string Name;
public readonly bool Exists;
private readonly Mod _mod;
public readonly string Name;
public readonly bool Exists;
public ModBackup(ModManager modManager, Mod mod)
{
_modManager = modManager;
_mod = mod;
Name = Path.Combine(_modManager.ExportDirectory.FullName, _mod.ModPath.Name) + ".pmp";
Exists = File.Exists(Name);
public ModBackup(ExportManager exportManager, Mod mod)
{
_mod = mod;
Name = Path.Combine(exportManager.ExportDirectory.FullName, _mod.ModPath.Name) + ".pmp";
Exists = File.Exists(Name);
}
/// <summary> Migrate file extensions. </summary>
@ -118,7 +116,7 @@ public class ModBackup
/// Restore a mod from a pre-existing backup. Does not check if the mod contained in the backup is even similar.
/// Does an automatic reload after extraction.
/// </summary>
public void Restore()
public void Restore(ModManager modManager)
{
try
{
@ -130,7 +128,7 @@ public class ModBackup
ZipFile.ExtractToDirectory(Name, _mod.ModPath.FullName);
Penumbra.Log.Debug($"Extracted exported file {Name} to {_mod.ModPath.FullName}.");
_modManager.ReloadMod(_mod.Index);
modManager.ReloadMod(_mod.Index);
}
catch (Exception e)
{

View file

@ -0,0 +1,89 @@
using System;
using System.IO;
namespace Penumbra.Mods;
public class ExportManager : IDisposable
{
private readonly Configuration _config;
private readonly ModManager _modManager;
private DirectoryInfo? _exportDirectory;
public DirectoryInfo ExportDirectory
=> _exportDirectory ?? _modManager.BasePath;
public ExportManager(Configuration config, ModManager modManager)
{
_config = config;
_modManager = modManager;
UpdateExportDirectory(_config.ExportDirectory, false);
_modManager.ModPathChanged += OnModPathChange;
}
/// <inheritdoc cref="UpdateExportDirectory(string, bool)"/>
public void UpdateExportDirectory(string newDirectory)
=> UpdateExportDirectory(newDirectory, true);
/// <summary>
/// Update the export directory to a new directory. Can also reset it to null with empty input.
/// If the directory is changed, all existing backups will be moved to the new one.
/// </summary>
/// <param name="newDirectory">The new directory name.</param>
/// <param name="change">Can be used to stop saving for the initial setting</param>
private void UpdateExportDirectory(string newDirectory, bool change)
{
if (newDirectory.Length == 0)
{
if (_exportDirectory == null)
return;
_exportDirectory = null;
_config.ExportDirectory = string.Empty;
_config.Save();
return;
}
var dir = new DirectoryInfo(newDirectory);
if (dir.FullName.Equals(_exportDirectory?.FullName, StringComparison.OrdinalIgnoreCase))
return;
if (!dir.Exists)
try
{
Directory.CreateDirectory(dir.FullName);
}
catch (Exception e)
{
Penumbra.Log.Error($"Could not create Export Directory:\n{e}");
return;
}
if (change)
foreach (var mod in _modManager)
new ModBackup(this, mod).Move(dir.FullName);
_exportDirectory = dir;
if (!change)
return;
_config.ExportDirectory = dir.FullName;
_config.Save();
}
public void Dispose()
=> _modManager.ModPathChanged -= OnModPathChange;
/// <summary> Automatically migrate the backup file to the new name if any exists. </summary>
private void OnModPathChange(ModPathChangeType type, Mod mod, DirectoryInfo? oldDirectory,
DirectoryInfo? newDirectory)
{
if (type is not ModPathChangeType.Moved || oldDirectory == null || newDirectory == null)
return;
mod.ModPath = oldDirectory;
new ModBackup(this, mod).Move(null, newDirectory.Name);
mod.ModPath = newDirectory;
}
}

View file

@ -57,7 +57,6 @@ public partial class ModManager
}
DataEditor.MoveDataFile(oldDirectory, dir);
new ModBackup(this, mod).Move(null, dir.Name);
dir.Refresh();
mod.ModPath = dir;

View file

@ -9,11 +9,6 @@ namespace Penumbra.Mods;
public sealed partial class ModManager
{
public DirectoryInfo BasePath { get; private set; } = null!;
private DirectoryInfo? _exportDirectory;
public DirectoryInfo ExportDirectory
=> _exportDirectory ?? BasePath;
public bool Valid { get; private set; }
public event Action? ModDiscoveryStarted;
@ -105,45 +100,4 @@ public sealed partial class ModManager
if (MigrateModBackups)
ModBackup.MigrateZipToPmp(this);
}
public void UpdateExportDirectory(string newDirectory, bool change)
{
if (newDirectory.Length == 0)
{
if (_exportDirectory == null)
return;
_exportDirectory = null;
_config.ExportDirectory = string.Empty;
_config.Save();
return;
}
var dir = new DirectoryInfo(newDirectory);
if (dir.FullName.Equals(_exportDirectory?.FullName, StringComparison.OrdinalIgnoreCase))
return;
if (!dir.Exists)
try
{
Directory.CreateDirectory(dir.FullName);
}
catch (Exception e)
{
Penumbra.Log.Error($"Could not create Export Directory:\n{e}");
return;
}
if (change)
foreach (var mod in _mods)
new ModBackup(this, mod).Move(dir.FullName);
_exportDirectory = dir;
if (change)
{
_config.ExportDirectory = dir.FullName;
_config.Save();
}
}
}

View file

@ -114,7 +114,6 @@ public sealed partial class ModManager : IReadOnlyList<Mod>, IDisposable
OptionEditor = optionEditor;
ModDirectoryChanged += OnModDirectoryChange;
SetBaseDirectory(config.ModDirectory, true);
UpdateExportDirectory(_config.ExportDirectory, false);
_communicator.ModOptionChanged.Event += OnModOptionChange;
ModPathChanged += OnModPathChange;
DiscoverMods();

View file

@ -47,11 +47,11 @@ public readonly struct ModSaveGroup : ISavable
public ModSaveGroup(Mod mod, int groupIdx)
{
_basePath = mod.ModPath;
_groupIdx = groupIdx;
if (_groupIdx < 0)
_defaultMod = mod.Default;
else
_group = mod.Groups[groupIdx];
_groupIdx = groupIdx;
_group = mod.Groups[_groupIdx];
}
public ModSaveGroup(DirectoryInfo basePath, IModGroup group, int groupIdx)