Some fixes regarding Forward-Slashes in ModNames.

This commit is contained in:
Ottermandias 2021-07-24 20:32:51 +02:00
parent 03a722b92f
commit d6bb45f33c
8 changed files with 72 additions and 21 deletions

View file

@ -50,11 +50,8 @@ namespace Penumbra.Importer
_resolvedTempFilePath = Path.Combine( _outDirectory.FullName, TempFileName );
}
private static string ReplaceBadXivSymbols( string source )
=> source.ReplaceInvalidPathSymbols().RemoveNonAsciiSymbols();
private static DirectoryInfo NewOptionDirectory( DirectoryInfo baseDir, string optionName )
=> new( Path.Combine( baseDir.FullName, ReplaceBadXivSymbols( optionName ) ) );
=> new( Path.Combine( baseDir.FullName, optionName.ReplaceBadXivSymbols() ) );
public void ImportModPack( FileInfo modPackFile )
{
@ -197,7 +194,12 @@ namespace Penumbra.Importer
public static DirectoryInfo CreateModFolder( DirectoryInfo outDirectory, string modListName )
{
var newModFolderBase = NewOptionDirectory( outDirectory, Path.GetFileName( modListName ) );
var name = Path.GetFileName( modListName );
if( !name.Any() )
{
name = "_";
}
var newModFolderBase = NewOptionDirectory( outDirectory, name );
var newModFolder = newModFolderBase;
var i = 2;
while( newModFolder.Exists && i < 12 )

View file

@ -123,7 +123,7 @@ namespace Penumbra.Meta
_resolvedFiles = resolvedFiles;
_default = Service< MetaDefaults >.Get();
_resourceManagement = Service< GameResourceManagement >.Get();
_dir = new DirectoryInfo( Path.Combine( modDir.FullName, TmpDirectory, name.ReplaceInvalidPathSymbols().RemoveNonAsciiSymbols() ) );
_dir = new DirectoryInfo( Path.Combine( modDir.FullName, TmpDirectory, name.ReplaceBadXivSymbols() ) );
ClearDirectory();
}

View file

@ -25,7 +25,7 @@ namespace Penumbra.Mod
Meta = meta;
Resources = resources;
MetaFile = MetaFileInfo( basePath );
SortOrder = meta.Name;
SortOrder = meta.Name.Replace( '/', '\\' );
ComputeChangedItems();
}

View file

@ -138,6 +138,11 @@ namespace Penumbra.Mods
return false;
}
if( Config.ModSortOrder.TryGetValue( mod.BasePath.Name, out var sortOrder ) )
{
mod.SortOrder = sortOrder;
}
if( Mods.ContainsKey( modFolder.Name ) )
{
return false;
@ -166,6 +171,14 @@ namespace Penumbra.Mods
if( metaChanges || fileChanges.HasFlag( ResourceChange.Files ) )
{
mod.ComputeChangedItems();
if( Config.ModSortOrder.TryGetValue( mod.BasePath.Name, out var sortOrder ) )
{
mod.SortOrder = sortOrder;
}
else
{
mod.SortOrder = mod.Meta.Name.Replace( '/', '\\' );
}
}
var nameChange = !string.Equals( oldName, mod.Meta.Name, StringComparison.InvariantCulture );

View file

@ -39,9 +39,10 @@ namespace Penumbra.Mods
return false;
}
if( newSortOrder == string.Empty || newSortOrder == mod.Meta.Name )
var modName = mod.Meta.Name.Replace( '/', '\\' );
if( newSortOrder == string.Empty || newSortOrder == modName )
{
mod.SortOrder = mod.Meta.Name;
mod.SortOrder = modName;
manager.Config.ModSortOrder.Remove( mod.BasePath.Name );
}
else

View file

@ -254,7 +254,8 @@ namespace Penumbra.UI
private void DrawOpenModFolderButton()
{
if( ImGui.Button( ButtonOpenModFolder ) )
Mod!.Data.BasePath.Refresh();
if( ImGui.Button( ButtonOpenModFolder ) && Mod.Data.BasePath.Exists )
{
Process.Start( Mod!.Data.BasePath.FullName );
}
@ -270,7 +271,7 @@ namespace Penumbra.UI
private void RenameModFolder( string newName )
{
_newName = newName.RemoveNonAsciiSymbols().RemoveInvalidPathSymbols();
_newName = newName.ReplaceBadXivSymbols();
if( _newName.Length == 0 )
{
PluginLog.Debug( "New Directory name {NewName} was empty after removing invalid symbols.", newName );

View file

@ -157,7 +157,7 @@ namespace Penumbra.UI
var modMeta = new ModMeta
{
Author = "Unknown",
Name = newName,
Name = newName.Replace('/', '\\'),
Description = string.Empty,
};

View file

@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.IO;
using System.Text;
@ -5,27 +6,60 @@ namespace Penumbra.Util
{
public static class StringPathExtensions
{
private static readonly char[] Invalid = Path.GetInvalidFileNameChars();
private static readonly HashSet< char > Invalid = new( Path.GetInvalidFileNameChars() );
public static string ReplaceInvalidPathSymbols( this string s, string replacement = "_" )
=> string.Join( replacement, s.Split( Invalid ) );
public static string RemoveInvalidPathSymbols( this string s )
=> string.Concat( s.Split( Invalid ) );
public static string RemoveNonAsciiSymbols( this string s, string replacement = "_" )
{
StringBuilder sb = new( s.Length );
foreach( var c in s )
{
if( c < 128 )
if( Invalid.Contains( c ) )
{
sb.Append( c );
sb.Append( replacement );
}
else
{
sb.Append( c );
}
}
return sb.ToString();
}
public static string RemoveInvalidPathSymbols( this string s )
=> string.Concat( s.Split( Path.GetInvalidFileNameChars() ) );
public static string ReplaceNonAsciiSymbols( this string s, string replacement = "_" )
{
StringBuilder sb = new( s.Length );
foreach( var c in s )
{
if( c >= 128 )
{
sb.Append( replacement );
}
else
{
sb.Append( c );
}
}
return sb.ToString();
}
public static string ReplaceBadXivSymbols( this string s, string replacement = "_" )
{
StringBuilder sb = new( s.Length );
foreach( var c in s )
{
if( c >= 128 || Invalid.Contains( c ) )
{
sb.Append( replacement );
}
else
{
sb.Append( c );
}
}
return sb.ToString();