mirror of
https://github.com/aelurum/AssetStudio.git
synced 2026-11-15 19:28:48 -05:00
Fix some bugs with invalid names during export
This commit is contained in:
+52
-12
@@ -10,10 +10,16 @@ namespace AssetStudioGUI
|
||||
internal static class Exporter
|
||||
{
|
||||
private static readonly HashSet<string> ExportPathHashSet = new HashSet<string>(System.StringComparer.OrdinalIgnoreCase);
|
||||
private static readonly HashSet<char> InvalidChars = new HashSet<char>(Path.GetInvalidFileNameChars());
|
||||
|
||||
static Exporter()
|
||||
{
|
||||
InvalidChars.UnionWith(new[] { '"', '<', '>', '|', ':', '*', '?', '\\', '/' });
|
||||
}
|
||||
|
||||
private static bool TryExportFile(string dir, AssetItem item, string extension, out string fullPath, string mode = "Export")
|
||||
{
|
||||
var fileName = FixFileName(item.Text);
|
||||
var fileName = item.Text;
|
||||
var filenameFormatIndex = Properties.Settings.Default.filenameFormat;
|
||||
var canOverwrite = Properties.Settings.Default.overwriteExistingFiles;
|
||||
switch (filenameFormatIndex)
|
||||
@@ -25,7 +31,7 @@ namespace AssetStudioGUI
|
||||
fileName = item.m_PathID.ToString();
|
||||
break;
|
||||
}
|
||||
fullPath = Path.Combine(dir, fileName + extension);
|
||||
fullPath = SafeFilePath(dir, $"{fileName}{extension}");
|
||||
if (ExportPathHashSet.Add(fullPath))
|
||||
{
|
||||
if (CanWrite(fullPath, dir, canOverwrite))
|
||||
@@ -35,7 +41,7 @@ namespace AssetStudioGUI
|
||||
}
|
||||
else if (filenameFormatIndex == 0) //assetName
|
||||
{
|
||||
fullPath = Path.Combine(dir, fileName + item.UniqueID + extension);
|
||||
fullPath = SafeFilePath(dir, $"{fileName}{item.UniqueID}{extension}");
|
||||
if (CanWrite(fullPath, dir, canOverwrite))
|
||||
{
|
||||
return true;
|
||||
@@ -234,10 +240,10 @@ namespace AssetStudioGUI
|
||||
|
||||
public static bool ExportAnimator(AssetItem item, string exportPath, List<AssetItem> animationList = null)
|
||||
{
|
||||
var exportFullPath = Path.Combine(exportPath, item.Text, item.Text + ".fbx");
|
||||
var exportFullPath = SafeFilePath(exportPath, item.Text, item.Text + ".fbx");
|
||||
if (File.Exists(exportFullPath))
|
||||
{
|
||||
exportFullPath = Path.Combine(exportPath, item.Text + item.UniqueID, item.Text + ".fbx");
|
||||
exportFullPath = SafeFilePath(exportPath, item.Text + item.UniqueID, item.Text + ".fbx");
|
||||
}
|
||||
if (!Studio.FbxSettings.ExportAnimations)
|
||||
animationList = new List<AssetItem>();
|
||||
@@ -261,10 +267,10 @@ namespace AssetStudioGUI
|
||||
{
|
||||
var jsonDoc = mat.ToJsonDoc();
|
||||
var str = System.Text.Json.JsonSerializer.Serialize(jsonDoc, Object.JsonOptions);
|
||||
var exportFullPath = Path.Combine(exportDir, $"{mat.m_Name}.json");
|
||||
var exportFullPath = SafeFilePath(exportDir, $"{mat.m_Name}.json");
|
||||
if (File.Exists(exportFullPath))
|
||||
{
|
||||
exportFullPath = Path.Combine(exportDir, $"{mat.m_Name} #{mat.GetHashCode():x}.json");
|
||||
exportFullPath = SafeFilePath(exportDir, $"{mat.m_Name} #{mat.GetHashCode():x}.json");
|
||||
}
|
||||
File.WriteAllText(exportFullPath, str);
|
||||
}
|
||||
@@ -398,7 +404,7 @@ namespace AssetStudioGUI
|
||||
var convert = animationList != null
|
||||
? new ModelConverter(gameObject, Properties.Settings.Default.convertType, animationList.Select(x => (AnimationClip)x.Asset).ToList())
|
||||
: new ModelConverter(gameObject, Properties.Settings.Default.convertType);
|
||||
exportPath = exportPath + FixFileName(gameObject.m_Name) + ".fbx";
|
||||
exportPath = SafeFilePath(exportPath, $"{gameObject.m_Name}.fbx");
|
||||
ExportFbx(convert, exportPath);
|
||||
}
|
||||
|
||||
@@ -413,11 +419,45 @@ namespace AssetStudioGUI
|
||||
ExportFbx(convert, exportPath);
|
||||
}
|
||||
|
||||
public static string FixFileName(string str)
|
||||
public static string SafeFileName(string str)
|
||||
{
|
||||
return str.Length >= 260
|
||||
? Path.GetRandomFileName()
|
||||
: Path.GetInvalidFileNameChars().Aggregate(str, (current, c) => current.Replace(c, '_'));
|
||||
if (str.Length >= 255)
|
||||
{
|
||||
var newName = Path.GetRandomFileName();
|
||||
Logger.Warning("The asset file name is too long.\n" +
|
||||
$"\"{str}\"\n" +
|
||||
"Was replaced with:\n" +
|
||||
$"\"{newName}\"");
|
||||
return newName;
|
||||
}
|
||||
|
||||
return InvalidChars.Aggregate(str, (current, c) => current.Replace(c, '_')).Trim();
|
||||
}
|
||||
|
||||
public static string SafeFilePath(string path, params string[] names)
|
||||
{
|
||||
var result = path;
|
||||
foreach (var name in names)
|
||||
{
|
||||
result = Path.Combine(result, SafeFileName(name));
|
||||
}
|
||||
|
||||
if (result.Length >= 4096)
|
||||
{
|
||||
var newPath = Path.Combine(Directory.GetCurrentDirectory(), SafeFileName(names.Last()));
|
||||
Logger.Warning("The export path is too long.\n" +
|
||||
$"\"{result}\"\n" +
|
||||
"Was reduced to:\n" +
|
||||
$"\"{newPath}\"");
|
||||
return newPath;
|
||||
}
|
||||
|
||||
return Path.GetInvalidPathChars().Aggregate(result, (current, c) => current.Replace(c, '_'));
|
||||
}
|
||||
|
||||
public static string SafePathCombine(string pathA, string pathB)
|
||||
{
|
||||
return SafeFilePath(pathA, pathB.Split('\\', '/'));
|
||||
}
|
||||
|
||||
public static void ClearHash()
|
||||
|
||||
Reference in New Issue
Block a user