mirror of
https://github.com/xivdev/Penumbra.git
synced 2025-12-23 17:09:22 +01:00
Make import of files with erroneous extension a bit more robust.
This commit is contained in:
parent
5a8051e0da
commit
2f9402ae5f
2 changed files with 65 additions and 38 deletions
|
|
@ -53,11 +53,8 @@ namespace Penumbra.Importer
|
||||||
switch( modPackFile.Extension )
|
switch( modPackFile.Extension )
|
||||||
{
|
{
|
||||||
case ".ttmp":
|
case ".ttmp":
|
||||||
ImportV1ModPack( modPackFile );
|
|
||||||
break;
|
|
||||||
|
|
||||||
case ".ttmp2":
|
case ".ttmp2":
|
||||||
ImportV2ModPack( modPackFile );
|
VerifyVersionAndImport(modPackFile);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|
@ -88,15 +85,33 @@ namespace Penumbra.Importer
|
||||||
return new MagicTempFileStreamManagerAndDeleterFuckery( fs );
|
return new MagicTempFileStreamManagerAndDeleterFuckery( fs );
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ImportV1ModPack( FileInfo modPackFile )
|
private void VerifyVersionAndImport(FileInfo modPackFile)
|
||||||
|
{
|
||||||
|
using var zfs = modPackFile.OpenRead();
|
||||||
|
using var extractedModPack = new ZipFile( zfs );
|
||||||
|
var mpl = extractedModPack.GetEntry( "TTMPL.mpl" );
|
||||||
|
var modRaw = GetStringFromZipEntry( extractedModPack, mpl, Encoding.UTF8 );
|
||||||
|
|
||||||
|
// At least a better validation than going by the extension.
|
||||||
|
if (modRaw.Contains("\"TTMPVersion\":"))
|
||||||
|
{
|
||||||
|
if (modPackFile.Extension != ".ttmp2")
|
||||||
|
PluginLog.Warning($"File {modPackFile.FullName} seems to be a V2 TTMP, but has the wrong extension.");
|
||||||
|
ImportV2ModPack(modPackFile, extractedModPack, modRaw);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (modPackFile.Extension != ".ttmp")
|
||||||
|
PluginLog.Warning($"File {modPackFile.FullName} seems to be a V1 TTMP, but has the wrong extension.");
|
||||||
|
ImportV1ModPack(modPackFile, extractedModPack, modRaw);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ImportV1ModPack( FileInfo modPackFile, ZipFile extractedModPack, string modRaw )
|
||||||
{
|
{
|
||||||
PluginLog.Log( " -> Importing V1 ModPack" );
|
PluginLog.Log( " -> Importing V1 ModPack" );
|
||||||
|
|
||||||
using var zfs = modPackFile.OpenRead();
|
var modListRaw = modRaw.Split(
|
||||||
using var extractedModPack = new ZipFile( zfs );
|
|
||||||
|
|
||||||
var mpl = extractedModPack.GetEntry( "TTMPL.mpl" );
|
|
||||||
var modListRaw = GetStringFromZipEntry( extractedModPack, mpl, Encoding.UTF8 ).Split(
|
|
||||||
new[] { "\r\n", "\r", "\n" },
|
new[] { "\r\n", "\r", "\n" },
|
||||||
StringSplitOptions.None
|
StringSplitOptions.None
|
||||||
);
|
);
|
||||||
|
|
@ -129,33 +144,26 @@ namespace Penumbra.Importer
|
||||||
ExtractSimpleModList( newModFolder, modList, modData );
|
ExtractSimpleModList( newModFolder, modList, modData );
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ImportV2ModPack( FileInfo modPackFile )
|
private void ImportV2ModPack( FileInfo modPackFile, ZipFile extractedModPack, string modRaw )
|
||||||
{
|
{
|
||||||
using var zfs = modPackFile.OpenRead();
|
var modList = JsonConvert.DeserializeObject< SimpleModPack >( modRaw );
|
||||||
using var extractedModPack = new ZipFile( zfs );
|
|
||||||
|
|
||||||
var mpl = extractedModPack.GetEntry( "TTMPL.mpl" );
|
|
||||||
var modList = JsonConvert.DeserializeObject< SimpleModPack >( GetStringFromZipEntry( extractedModPack, mpl, Encoding.UTF8 ) );
|
|
||||||
|
|
||||||
if( modList.TTMPVersion.EndsWith( "s" ) )
|
if( modList.TTMPVersion.EndsWith( "s" ) )
|
||||||
{
|
{
|
||||||
ImportSimpleV2ModPack( extractedModPack );
|
ImportSimpleV2ModPack( extractedModPack, modList );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if( modList.TTMPVersion.EndsWith( "w" ) )
|
if( modList.TTMPVersion.EndsWith( "w" ) )
|
||||||
{
|
{
|
||||||
ImportExtendedV2ModPack( extractedModPack );
|
ImportExtendedV2ModPack( extractedModPack, modRaw );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ImportSimpleV2ModPack( ZipFile extractedModPack )
|
private void ImportSimpleV2ModPack( ZipFile extractedModPack, SimpleModPack modList )
|
||||||
{
|
{
|
||||||
PluginLog.Log( " -> Importing Simple V2 ModPack" );
|
PluginLog.Log( " -> Importing Simple V2 ModPack" );
|
||||||
|
|
||||||
var mpl = extractedModPack.GetEntry( "TTMPL.mpl" );
|
|
||||||
var modList = JsonConvert.DeserializeObject< SimpleModPack >( GetStringFromZipEntry( extractedModPack, mpl, Encoding.UTF8 ) );
|
|
||||||
|
|
||||||
// Create a new ModMeta from the TTMP modlist info
|
// Create a new ModMeta from the TTMP modlist info
|
||||||
var modMeta = new ModMeta
|
var modMeta = new ModMeta
|
||||||
{
|
{
|
||||||
|
|
@ -179,12 +187,11 @@ namespace Penumbra.Importer
|
||||||
ExtractSimpleModList( newModFolder, modList.SimpleModsList, modData );
|
ExtractSimpleModList( newModFolder, modList.SimpleModsList, modData );
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ImportExtendedV2ModPack( ZipFile extractedModPack )
|
private void ImportExtendedV2ModPack( ZipFile extractedModPack, string modRaw )
|
||||||
{
|
{
|
||||||
PluginLog.Log( " -> Importing Extended V2 ModPack" );
|
PluginLog.Log( " -> Importing Extended V2 ModPack" );
|
||||||
|
|
||||||
var mpl = extractedModPack.GetEntry( "TTMPL.mpl" );
|
var modList = JsonConvert.DeserializeObject< ExtendedModPack >( modRaw );
|
||||||
var modList = JsonConvert.DeserializeObject< ExtendedModPack >( GetStringFromZipEntry( extractedModPack, mpl, Encoding.UTF8 ) );
|
|
||||||
|
|
||||||
// Create a new ModMeta from the TTMP modlist info
|
// Create a new ModMeta from the TTMP modlist info
|
||||||
var modMeta = new ModMeta
|
var modMeta = new ModMeta
|
||||||
|
|
@ -202,7 +209,7 @@ namespace Penumbra.Importer
|
||||||
|
|
||||||
var newModFolder = new DirectoryInfo(
|
var newModFolder = new DirectoryInfo(
|
||||||
Path.Combine( _outDirectory.FullName,
|
Path.Combine( _outDirectory.FullName,
|
||||||
Path.GetFileNameWithoutExtension( modList.Name )
|
Path.GetFileNameWithoutExtension( modList.Name ).ReplaceInvalidPathSymbols()
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
newModFolder.Create();
|
newModFolder.Create();
|
||||||
|
|
@ -216,10 +223,12 @@ namespace Penumbra.Importer
|
||||||
// Iterate through all pages
|
// Iterate through all pages
|
||||||
foreach( var page in modList.ModPackPages)
|
foreach( var page in modList.ModPackPages)
|
||||||
{
|
{
|
||||||
foreach(var group in page.ModGroups) {
|
foreach(var group in page.ModGroups)
|
||||||
var groupFolder = new DirectoryInfo(Path.Combine(newModFolder.FullName, group.GroupName));
|
{
|
||||||
foreach(var option in group.OptionList) {
|
var groupFolder = new DirectoryInfo(Path.Combine(newModFolder.FullName, group.GroupName.ReplaceInvalidPathSymbols()));
|
||||||
var optionFolder = new DirectoryInfo( Path.Combine( groupFolder.FullName, option.Name ) );
|
foreach(var option in group.OptionList)
|
||||||
|
{
|
||||||
|
var optionFolder = new DirectoryInfo( Path.Combine( groupFolder.FullName, option.Name.ReplaceInvalidPathSymbols()) );
|
||||||
ExtractSimpleModList( optionFolder, option.ModsJsons, modData );
|
ExtractSimpleModList( optionFolder, option.ModsJsons, modData );
|
||||||
}
|
}
|
||||||
AddMeta(newModFolder, groupFolder, group, modMeta);
|
AddMeta(newModFolder, groupFolder, group, modMeta);
|
||||||
|
|
@ -248,7 +257,7 @@ namespace Penumbra.Importer
|
||||||
OptionDesc = String.IsNullOrEmpty(opt.Description) ? "" : opt.Description,
|
OptionDesc = String.IsNullOrEmpty(opt.Description) ? "" : opt.Description,
|
||||||
OptionFiles = new Dictionary<string, HashSet<string>>()
|
OptionFiles = new Dictionary<string, HashSet<string>>()
|
||||||
};
|
};
|
||||||
var optDir = new DirectoryInfo(Path.Combine( groupFolder.FullName, opt.Name));
|
var optDir = new DirectoryInfo(Path.Combine( groupFolder.FullName, opt.Name.ReplaceInvalidPathSymbols()));
|
||||||
if (optDir.Exists)
|
if (optDir.Exists)
|
||||||
{
|
{
|
||||||
foreach ( var file in optDir.EnumerateFiles("*.*", SearchOption.AllDirectories) )
|
foreach ( var file in optDir.EnumerateFiles("*.*", SearchOption.AllDirectories) )
|
||||||
|
|
@ -307,7 +316,7 @@ namespace Penumbra.Importer
|
||||||
}
|
}
|
||||||
catch( Exception ex )
|
catch( Exception ex )
|
||||||
{
|
{
|
||||||
PluginLog.LogError( ex, "Could not export mod." );
|
PluginLog.LogError( ex, "Could not extract mod." );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
18
Penumbra/Util/StringPathExtensions.cs
Normal file
18
Penumbra/Util/StringPathExtensions.cs
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
namespace Penumbra
|
||||||
|
{
|
||||||
|
public static class StringPathExtensions
|
||||||
|
{
|
||||||
|
private static char[] _invalid = Path.GetInvalidFileNameChars();
|
||||||
|
public static string ReplaceInvalidPathSymbols( this string s, string replacement = "_" )
|
||||||
|
{
|
||||||
|
return string.Join( replacement, s.Split( _invalid ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string RemoveInvalidPathSymbols( this string s )
|
||||||
|
{
|
||||||
|
return string.Concat( s.Split( _invalid ) );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue