This commit is contained in:
Ottermandias 2022-03-26 23:28:52 +01:00
parent 9a0b0bfa0f
commit ac70f8db89
41 changed files with 1546 additions and 1520 deletions

View file

@ -37,7 +37,7 @@ public static partial class ModFileSystem
// Rename the SortOrderName of a single mod. Slashes are replaced by Backslashes.
// Saves and returns true if anything changed.
public static bool Rename( this ModData mod, string newName )
public static bool Rename( this Mod.Mod mod, string newName )
{
if( RenameNoSave( mod, newName ) )
{
@ -63,7 +63,7 @@ public static partial class ModFileSystem
// Move a single mod to the target folder.
// Returns true and saves if anything changed.
public static bool Move( this ModData mod, ModFolder target )
public static bool Move( this Mod.Mod mod, ModFolder target )
{
if( MoveNoSave( mod, target ) )
{
@ -76,7 +76,7 @@ public static partial class ModFileSystem
// Move a mod to the filesystem location specified by sortOrder and rename its SortOrderName.
// Creates all necessary Subfolders.
public static void Move( this ModData mod, string sortOrder )
public static void Move( this Mod.Mod mod, string sortOrder )
{
var split = sortOrder.Split( new[] { '/' }, StringSplitOptions.RemoveEmptyEntries );
var folder = Root;
@ -129,7 +129,7 @@ public static partial class ModFileSystem
{
foreach( var mod in target.AllMods( true ) )
{
Penumbra.Config.ModSortOrder[ mod.BasePath.Name ] = mod.SortOrder.FullName;
Penumbra.Config.ModSortOrder[ mod.BasePath.Name ] = mod.Order.FullName;
}
Penumbra.Config.Save();
@ -137,16 +137,16 @@ public static partial class ModFileSystem
}
// Sets and saves the sort order of a single mod, removing the entry if it is unnecessary.
private static void SaveMod( ModData mod )
private static void SaveMod( Mod.Mod mod )
{
if( ReferenceEquals( mod.SortOrder.ParentFolder, Root )
&& string.Equals( mod.SortOrder.SortOrderName, mod.Meta.Name.Replace( '/', '\\' ), StringComparison.InvariantCultureIgnoreCase ) )
if( ReferenceEquals( mod.Order.ParentFolder, Root )
&& string.Equals( mod.Order.SortOrderName, mod.Meta.Name.Replace( '/', '\\' ), StringComparison.InvariantCultureIgnoreCase ) )
{
Penumbra.Config.ModSortOrder.Remove( mod.BasePath.Name );
}
else
{
Penumbra.Config.ModSortOrder[ mod.BasePath.Name ] = mod.SortOrder.FullName;
Penumbra.Config.ModSortOrder[ mod.BasePath.Name ] = mod.Order.FullName;
}
Penumbra.Config.Save();
@ -184,30 +184,30 @@ public static partial class ModFileSystem
return true;
}
private static bool RenameNoSave( ModData mod, string newName )
private static bool RenameNoSave( Mod.Mod mod, string newName )
{
newName = newName.Replace( '/', '\\' );
if( mod.SortOrder.SortOrderName == newName )
if( mod.Order.SortOrderName == newName )
{
return false;
}
mod.SortOrder.ParentFolder.RemoveModIgnoreEmpty( mod );
mod.SortOrder = new SortOrder( mod.SortOrder.ParentFolder, newName );
mod.SortOrder.ParentFolder.AddMod( mod );
mod.Order.ParentFolder.RemoveModIgnoreEmpty( mod );
mod.Order = new Mod.Mod.SortOrder( mod.Order.ParentFolder, newName );
mod.Order.ParentFolder.AddMod( mod );
return true;
}
private static bool MoveNoSave( ModData mod, ModFolder target )
private static bool MoveNoSave( Mod.Mod mod, ModFolder target )
{
var oldParent = mod.SortOrder.ParentFolder;
var oldParent = mod.Order.ParentFolder;
if( ReferenceEquals( target, oldParent ) )
{
return false;
}
oldParent.RemoveMod( mod );
mod.SortOrder = new SortOrder( target, mod.SortOrder.SortOrderName );
mod.Order = new Mod.Mod.SortOrder( target, mod.Order.SortOrderName );
target.AddMod( mod );
return true;
}

View file

@ -27,7 +27,7 @@ namespace Penumbra.Mods
}
public List< ModFolder > SubFolders { get; } = new();
public List< ModData > Mods { get; } = new();
public List< Mod.Mod > Mods { get; } = new();
public ModFolder( ModFolder parent, string name )
{
@ -45,7 +45,7 @@ namespace Penumbra.Mods
=> SubFolders.Sum( f => f.TotalDescendantFolders() );
// Return all descendant mods in the specified order.
public IEnumerable< ModData > AllMods( bool foldersFirst )
public IEnumerable< Mod.Mod > AllMods( bool foldersFirst )
{
if( foldersFirst )
{
@ -59,7 +59,7 @@ namespace Penumbra.Mods
return folder.AllMods( false );
}
return new[] { ( ModData )f };
return new[] { ( Mod.Mod )f };
} );
}
@ -116,7 +116,7 @@ namespace Penumbra.Mods
// Add the given mod as a child, if it is not already a child.
// Returns the index of the found or inserted mod.
public int AddMod( ModData mod )
public int AddMod( Mod.Mod mod )
{
var idx = Mods.BinarySearch( mod, ModComparer );
if( idx >= 0 )
@ -132,7 +132,7 @@ namespace Penumbra.Mods
// Remove mod as a child if it exists.
// If this folder is empty afterwards, remove it from its parent.
public void RemoveMod( ModData mod )
public void RemoveMod( Mod.Mod mod )
{
RemoveModIgnoreEmpty( mod );
CheckEmpty();
@ -157,20 +157,20 @@ namespace Penumbra.Mods
: string.Compare( x?.Name ?? string.Empty, y?.Name ?? string.Empty, CompareType );
}
internal class ModDataComparer : IComparer< ModData >
internal class ModDataComparer : IComparer< Mod.Mod >
{
public StringComparison CompareType = StringComparison.InvariantCultureIgnoreCase;
// Compare only the direct SortOrderNames since this is only used inside an enumeration of direct mod children of one folder.
// Since mod SortOrderNames do not have to be unique inside a folder, also compare their BasePaths (and thus their identity) if necessary.
public int Compare( ModData? x, ModData? y )
public int Compare( Mod.Mod? x, Mod.Mod? y )
{
if( ReferenceEquals( x, y ) )
{
return 0;
}
var cmp = string.Compare( x?.SortOrder.SortOrderName, y?.SortOrder.SortOrderName, CompareType );
var cmp = string.Compare( x?.Order.SortOrderName, y?.Order.SortOrderName, CompareType );
if( cmp != 0 )
{
return cmp;
@ -193,7 +193,7 @@ namespace Penumbra.Mods
for( ; modIdx < Mods.Count; ++modIdx )
{
var mod = Mods[ modIdx ];
var modString = mod.SortOrder.SortOrderName;
var modString = mod.Order.SortOrderName;
if( string.Compare( folderString, modString, StringComparison.InvariantCultureIgnoreCase ) > 0 )
{
yield return mod;
@ -235,7 +235,7 @@ namespace Penumbra.Mods
}
// Remove a mod, but do not remove this folder from its parent if it is empty afterwards.
internal void RemoveModIgnoreEmpty( ModData mod )
internal void RemoveModIgnoreEmpty( Mod.Mod mod )
{
var idx = Mods.BinarySearch( mod, ModComparer );
if( idx >= 0 )

View file

@ -9,9 +9,9 @@ namespace Penumbra.Mods;
public partial class ModManagerNew
{
private readonly List< ModData > _mods = new();
private readonly List< Mod.Mod > _mods = new();
public IReadOnlyList< ModData > Mods
public IReadOnlyList< Mod.Mod > Mods
=> _mods;
public void DiscoverMods()

View file

@ -1,276 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Dalamud.Logging;
using Penumbra.GameData.ByteString;
using Penumbra.Meta;
using Penumbra.Mod;
using Penumbra.Util;
namespace Penumbra.Mods;
public enum ModChangeType
{
Added,
Removed,
Changed,
}
public delegate void ModChangeDelegate( ModChangeType type, int modIndex, ModData modData );
// The ModManager handles the basic mods installed to the mod directory.
// It also contains the CollectionManager that handles all collections.
public class ModManager : IEnumerable< ModData >
{
public DirectoryInfo BasePath { get; private set; } = null!;
private readonly List< ModData > _mods = new();
public ModData this[ int idx ]
=> _mods[ idx ];
public IReadOnlyList< ModData > Mods
=> _mods;
public IEnumerator< ModData > GetEnumerator()
=> _mods.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator()
=> GetEnumerator();
public ModFolder StructuredMods { get; } = ModFileSystem.Root;
public event ModChangeDelegate? ModChange;
public event Action? ModsRediscovered;
public bool Valid { get; private set; }
public int Count
=> _mods.Count;
public Configuration Config
=> Penumbra.Config;
public void DiscoverMods( string newDir )
{
SetBaseDirectory( newDir, false );
DiscoverMods();
}
private void SetBaseDirectory( string newPath, bool firstTime )
{
if( !firstTime && string.Equals( newPath, Config.ModDirectory, StringComparison.InvariantCultureIgnoreCase ) )
{
return;
}
if( newPath.Length == 0 )
{
Valid = false;
BasePath = new DirectoryInfo( "." );
}
else
{
var newDir = new DirectoryInfo( newPath );
if( !newDir.Exists )
{
try
{
Directory.CreateDirectory( newDir.FullName );
newDir.Refresh();
}
catch( Exception e )
{
PluginLog.Error( $"Could not create specified mod directory {newDir.FullName}:\n{e}" );
}
}
BasePath = newDir;
Valid = true;
if( Config.ModDirectory != BasePath.FullName )
{
Config.ModDirectory = BasePath.FullName;
Config.Save();
}
}
ModsRediscovered?.Invoke();
}
public ModManager()
{
SetBaseDirectory( Config.ModDirectory, true );
}
private bool SetSortOrderPath( ModData mod, string path )
{
mod.Move( path );
var fixedPath = mod.SortOrder.FullPath;
if( fixedPath.Length == 0 || string.Equals( fixedPath, mod.Meta.Name, StringComparison.InvariantCultureIgnoreCase ) )
{
Config.ModSortOrder.Remove( mod.BasePath.Name );
return true;
}
if( path != fixedPath )
{
Config.ModSortOrder[ mod.BasePath.Name ] = fixedPath;
return true;
}
return false;
}
private void SetModStructure( bool removeOldPaths = false )
{
var changes = false;
foreach( var (folder, path) in Config.ModSortOrder.ToArray() )
{
if( path.Length > 0 && _mods.FindFirst( m => m.BasePath.Name == folder, out var mod ) )
{
changes |= SetSortOrderPath( mod, path );
}
else if( removeOldPaths )
{
changes = true;
Config.ModSortOrder.Remove( folder );
}
}
if( changes )
{
Config.Save();
}
}
public void DiscoverMods()
{
_mods.Clear();
BasePath.Refresh();
StructuredMods.SubFolders.Clear();
StructuredMods.Mods.Clear();
if( Valid && BasePath.Exists )
{
foreach( var modFolder in BasePath.EnumerateDirectories() )
{
var mod = ModData.LoadMod( StructuredMods, modFolder );
if( mod == null )
{
continue;
}
_mods.Add( mod );
}
SetModStructure();
}
ModsRediscovered?.Invoke();
}
public void DeleteMod( DirectoryInfo modFolder )
{
if( Directory.Exists( modFolder.FullName ) )
{
try
{
Directory.Delete( modFolder.FullName, true );
}
catch( Exception e )
{
PluginLog.Error( $"Could not delete the mod {modFolder.Name}:\n{e}" );
}
}
var idx = _mods.FindIndex( m => m.BasePath.Name == modFolder.Name );
if( idx >= 0 )
{
var mod = _mods[ idx ];
mod.SortOrder.ParentFolder.RemoveMod( mod );
_mods.RemoveAt( idx );
ModChange?.Invoke( ModChangeType.Removed, idx, mod );
}
}
public int AddMod( DirectoryInfo modFolder )
{
var mod = ModData.LoadMod( StructuredMods, modFolder );
if( mod == null )
{
return -1;
}
if( Config.ModSortOrder.TryGetValue( mod.BasePath.Name, out var sortOrder ) )
{
if( SetSortOrderPath( mod, sortOrder ) )
{
Config.Save();
}
}
if( _mods.Any( m => m.BasePath.Name == modFolder.Name ) )
{
return -1;
}
_mods.Add( mod );
ModChange?.Invoke( ModChangeType.Added, _mods.Count - 1, mod );
return _mods.Count - 1;
}
public bool UpdateMod( int idx, bool reloadMeta = false, bool recomputeMeta = false, bool force = false )
{
var mod = Mods[ idx ];
var oldName = mod.Meta.Name;
var metaChanges = mod.Meta.RefreshFromFile( mod.MetaFile ) || force;
var fileChanges = mod.Resources.RefreshModFiles( mod.BasePath );
if( !recomputeMeta && !reloadMeta && !metaChanges && fileChanges == 0 )
{
return false;
}
if( metaChanges || fileChanges.HasFlag( ResourceChange.Files ) )
{
mod.ComputeChangedItems();
if( Config.ModSortOrder.TryGetValue( mod.BasePath.Name, out var sortOrder ) )
{
mod.Move( sortOrder );
var path = mod.SortOrder.FullPath;
if( path != sortOrder )
{
Config.ModSortOrder[ mod.BasePath.Name ] = path;
Config.Save();
}
}
else
{
mod.SortOrder = new SortOrder( StructuredMods, mod.Meta.Name );
}
}
var nameChange = !string.Equals( oldName, mod.Meta.Name, StringComparison.InvariantCulture );
recomputeMeta |= fileChanges.HasFlag( ResourceChange.Meta );
if( recomputeMeta )
{
mod.Resources.MetaManipulations.Update( mod.Resources.MetaFiles, mod.BasePath, mod.Meta );
mod.Resources.MetaManipulations.SaveToFile( MetaCollection.FileName( mod.BasePath ) );
}
// TODO: more specific mod changes?
ModChange?.Invoke( ModChangeType.Changed, idx, mod );
return true;
}
public bool UpdateMod( ModData mod, bool reloadMeta = false, bool recomputeMeta = false, bool force = false )
=> UpdateMod( Mods.IndexOf( mod ), reloadMeta, recomputeMeta, force );
public static FullPath? ResolvePath( Utf8GamePath gameResourcePath )
=> Penumbra.CollectionManager.Default.ResolvePath( gameResourcePath );
}

View file

@ -12,7 +12,7 @@ namespace Penumbra.Mods;
// Contains all change functions on a specific mod that also require corresponding changes to collections.
public static class ModManagerEditExtensions
{
public static bool RenameMod( this ModManager manager, string newName, ModData mod )
public static bool RenameMod( this Mod.Mod.Manager manager, string newName, Mod.Mod mod )
{
if( newName.Length == 0 || string.Equals( newName, mod.Meta.Name, StringComparison.InvariantCulture ) )
{
@ -25,23 +25,23 @@ public static class ModManagerEditExtensions
return true;
}
public static bool ChangeSortOrder( this ModManager manager, ModData mod, string newSortOrder )
public static bool ChangeSortOrder( this Mod.Mod.Manager manager, Mod.Mod mod, string newSortOrder )
{
if( string.Equals( mod.SortOrder.FullPath, newSortOrder, StringComparison.InvariantCultureIgnoreCase ) )
if( string.Equals( mod.Order.FullPath, newSortOrder, StringComparison.InvariantCultureIgnoreCase ) )
{
return false;
}
var inRoot = new SortOrder( manager.StructuredMods, mod.Meta.Name );
var inRoot = new Mod.Mod.SortOrder( manager.StructuredMods, mod.Meta.Name );
if( newSortOrder == string.Empty || newSortOrder == inRoot.SortOrderName )
{
mod.SortOrder = inRoot;
mod.Order = inRoot;
manager.Config.ModSortOrder.Remove( mod.BasePath.Name );
}
else
{
mod.Move( newSortOrder );
manager.Config.ModSortOrder[ mod.BasePath.Name ] = mod.SortOrder.FullPath;
manager.Config.ModSortOrder[ mod.BasePath.Name ] = mod.Order.FullPath;
}
manager.Config.Save();
@ -49,7 +49,7 @@ public static class ModManagerEditExtensions
return true;
}
public static bool RenameModFolder( this ModManager manager, ModData mod, DirectoryInfo newDir, bool move = true )
public static bool RenameModFolder( this Mod.Mod.Manager manager, Mod.Mod mod, DirectoryInfo newDir, bool move = true )
{
if( move )
{
@ -73,7 +73,7 @@ public static class ModManagerEditExtensions
var oldBasePath = mod.BasePath;
mod.BasePath = newDir;
mod.MetaFile = ModData.MetaFileInfo( newDir );
mod.MetaFile = Mod.Mod.MetaFileInfo( newDir );
manager.UpdateMod( mod );
if( manager.Config.ModSortOrder.ContainsKey( oldBasePath.Name ) )
@ -95,7 +95,7 @@ public static class ModManagerEditExtensions
return true;
}
public static bool ChangeModGroup( this ModManager manager, string oldGroupName, string newGroupName, ModData mod,
public static bool ChangeModGroup( this Mod.Mod.Manager manager, string oldGroupName, string newGroupName, Mod.Mod mod,
SelectType type = SelectType.Single )
{
if( newGroupName == oldGroupName || mod.Meta.Groups.ContainsKey( newGroupName ) )
@ -157,7 +157,7 @@ public static class ModManagerEditExtensions
return true;
}
public static bool RemoveModOption( this ModManager manager, int optionIdx, OptionGroup group, ModData mod )
public static bool RemoveModOption( this Mod.Mod.Manager manager, int optionIdx, OptionGroup group, Mod.Mod mod )
{
if( optionIdx < 0 || optionIdx >= group.Options.Count )
{
@ -202,7 +202,7 @@ public static class ModManagerEditExtensions
if( collection.HasCache && settings.Enabled )
{
collection.CalculateEffectiveFileList( mod.Resources.MetaManipulations.Count > 0,
Penumbra.CollectionManager.Default == collection );
Penumbra.CollectionManager.Default == collection );
}
}
}