Rename Mod BasePath to ModPath, add simple Directory Renaming and Reloading, some fixes, Cleanup EditWindow.

This commit is contained in:
Ottermandias 2022-05-02 16:19:24 +02:00
parent c416d044a4
commit 65bbece9cf
17 changed files with 636 additions and 368 deletions

View file

@ -16,10 +16,86 @@ public partial class Mod
// Rename/Move a mod directory.
// Updates all collection settings and sort order settings.
public void MoveModDirectory( Index idx, DirectoryInfo newDirectory )
public void MoveModDirectory( int idx, string newName )
{
var mod = this[ idx ];
// TODO
var mod = this[ idx ];
var oldName = mod.Name;
var oldDirectory = mod.ModPath;
switch( NewDirectoryValid( oldDirectory.Name, newName, out var dir ) )
{
case NewDirectoryState.NonExisting:
// Nothing to do
break;
case NewDirectoryState.ExistsEmpty:
try
{
Directory.Delete( dir!.FullName );
}
catch( Exception e )
{
PluginLog.Error( $"Could not delete empty directory {dir!.FullName} to move {mod.Name} to it:\n{e}" );
return;
}
break;
// Should be caught beforehand.
case NewDirectoryState.ExistsNonEmpty:
case NewDirectoryState.ExistsAsFile:
case NewDirectoryState.ContainsInvalidSymbols:
// Nothing to do at all.
case NewDirectoryState.Identical:
default:
return;
}
try
{
Directory.Move( oldDirectory.FullName, dir!.FullName );
}
catch( Exception e )
{
PluginLog.Error( $"Could not move {mod.Name} from {oldDirectory.Name} to {dir!.Name}:\n{e}" );
return;
}
dir.Refresh();
mod.ModPath = dir;
if( !mod.Reload( out var metaChange ) )
{
PluginLog.Error( $"Error reloading moved mod {mod.Name}." );
return;
}
ModPathChanged.Invoke( ModPathChangeType.Moved, mod, oldDirectory, BasePath );
if( metaChange != MetaChangeType.None )
{
ModMetaChanged?.Invoke( metaChange, mod, oldName );
}
}
// Reload a mod without changing its base directory.
// If the base directory does not exist anymore, the mod will be deleted.
public void ReloadMod( int idx )
{
var mod = this[ idx ];
var oldName = mod.Name;
if( !mod.Reload( out var metaChange ) )
{
PluginLog.Warning( mod.Name.Length == 0
? $"Reloading mod {oldName} has failed, new name is empty. Deleting instead."
: $"Reloading mod {oldName} failed, {mod.ModPath.FullName} does not exist anymore or it ha. Deleting instead." );
DeleteMod( idx );
return;
}
ModPathChanged.Invoke( ModPathChangeType.Reloaded, mod, mod.ModPath, mod.ModPath );
if( metaChange != MetaChangeType.None )
{
ModMetaChanged?.Invoke( metaChange, mod, oldName );
}
}
// Delete a mod by its index.
@ -28,16 +104,16 @@ public partial class Mod
public void DeleteMod( int idx )
{
var mod = this[ idx ];
if( Directory.Exists( mod.BasePath.FullName ) )
if( Directory.Exists( mod.ModPath.FullName ) )
{
try
{
Directory.Delete( mod.BasePath.FullName, true );
PluginLog.Debug( "Deleted directory {Directory:l} for {Name:l}.", mod.BasePath.FullName, mod.Name );
Directory.Delete( mod.ModPath.FullName, true );
PluginLog.Debug( "Deleted directory {Directory:l} for {Name:l}.", mod.ModPath.FullName, mod.Name );
}
catch( Exception e )
{
PluginLog.Error( $"Could not delete the mod {mod.BasePath.Name}:\n{e}" );
PluginLog.Error( $"Could not delete the mod {mod.ModPath.Name}:\n{e}" );
}
}
@ -47,14 +123,14 @@ public partial class Mod
--remainingMod.Index;
}
ModPathChanged.Invoke( ModPathChangeType.Deleted, mod, mod.BasePath, null );
ModPathChanged.Invoke( ModPathChangeType.Deleted, mod, mod.ModPath, null );
PluginLog.Debug( "Deleted mod {Name:l}.", mod.Name );
}
// Load a new mod and add it to the manager if successful.
public void AddMod( DirectoryInfo modFolder )
{
if( _mods.Any( m => m.BasePath.Name == modFolder.Name ) )
if( _mods.Any( m => m.ModPath.Name == modFolder.Name ) )
{
return;
}
@ -67,10 +143,61 @@ public partial class Mod
mod.Index = _mods.Count;
_mods.Add( mod );
ModPathChanged.Invoke( ModPathChangeType.Added, mod, null, mod.BasePath );
PluginLog.Debug( "Added new mod {Name:l} from {Directory:l}.", mod.Name, modFolder.FullName );
ModPathChanged.Invoke( ModPathChangeType.Added, mod, null, mod.ModPath );
PluginLog.Debug( "Added new mod {Name:l} from {Directory:l}.", mod.Name, modFolder.FullName );
}
public enum NewDirectoryState
{
NonExisting,
ExistsEmpty,
ExistsNonEmpty,
ExistsAsFile,
ContainsInvalidSymbols,
Identical,
Empty,
}
// Return the state of the new potential name of a directory.
public static NewDirectoryState NewDirectoryValid( string oldName, string newName, out DirectoryInfo? directory )
{
directory = null;
if( newName.Length == 0 )
{
return NewDirectoryState.Empty;
}
if( oldName == newName )
{
return NewDirectoryState.Identical;
}
var fixedNewName = ReplaceBadXivSymbols( newName );
if( fixedNewName != newName )
{
return NewDirectoryState.ContainsInvalidSymbols;
}
directory = new DirectoryInfo( Path.Combine( Penumbra.ModManager.BasePath.FullName, fixedNewName ) );
if( File.Exists( directory.FullName ) )
{
return NewDirectoryState.ExistsAsFile;
}
if( !Directory.Exists( directory.FullName ) )
{
return NewDirectoryState.NonExisting;
}
if( directory.EnumerateFileSystemInfos().Any() )
{
return NewDirectoryState.ExistsNonEmpty;
}
return NewDirectoryState.ExistsEmpty;
}
// Add new mods to NewMods and remove deleted mods from NewMods.
private void OnModPathChange( ModPathChangeType type, Mod mod, DirectoryInfo? oldDirectory,
DirectoryInfo? newDirectory )

View file

@ -55,7 +55,7 @@ public sealed partial class Mod
return;
}
group.DeleteFile( mod.BasePath, groupIdx );
group.DeleteFile( mod.ModPath, groupIdx );
var _ = group switch
{
@ -86,7 +86,7 @@ public sealed partial class Mod
{
var group = mod._groups[ groupIdx ];
mod._groups.RemoveAt( groupIdx );
group.DeleteFile( mod.BasePath, groupIdx );
group.DeleteFile( mod.ModPath, groupIdx );
ModOptionChanged.Invoke( ModOptionChangeType.GroupDeleted, mod, groupIdx, -1, -1 );
}
@ -426,7 +426,7 @@ public sealed partial class Mod
}
else
{
IModGroup.SaveModGroup( mod._groups[ groupIdx ], mod.BasePath, groupIdx );
IModGroup.SaveModGroup( mod._groups[ groupIdx ], mod.ModPath, groupIdx );
}
}