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:
+54
-14
@@ -11,10 +11,16 @@ namespace AssetStudioCLI
|
||||
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 filenameFormat = CLIOptions.o_filenameFormat.Value;
|
||||
var canOverwrite = CLIOptions.f_overwriteExisting.Value;
|
||||
switch (filenameFormat)
|
||||
@@ -26,7 +32,7 @@ namespace AssetStudioCLI
|
||||
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))
|
||||
@@ -36,7 +42,7 @@ namespace AssetStudioCLI
|
||||
}
|
||||
else if (filenameFormat == FilenameFormat.AssetName)
|
||||
{
|
||||
fullPath = Path.Combine(dir, fileName + item.UniqueID + extension);
|
||||
fullPath = SafeFilePath(dir, $"{fileName}{item.UniqueID}{extension}");
|
||||
if (CanWrite(fullPath, dir, canOverwrite))
|
||||
{
|
||||
return true;
|
||||
@@ -268,10 +274,10 @@ namespace AssetStudioCLI
|
||||
|
||||
public static bool ExportAnimator(AssetItem item, string exportPath, List<AssetItem> animationList = null)
|
||||
{
|
||||
var exportFullPath = Path.Combine(exportPath, "FBX_Animator", item.Text, item.Text + ".fbx");
|
||||
var exportFullPath = SafeFilePath(exportPath, "FBX_Animator", 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");
|
||||
}
|
||||
var m_Animator = (Animator)item.Asset;
|
||||
var convert = animationList != null
|
||||
@@ -302,10 +308,10 @@ namespace AssetStudioCLI
|
||||
{
|
||||
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);
|
||||
}
|
||||
@@ -436,20 +442,54 @@ namespace AssetStudioCLI
|
||||
var convert = animationList != null
|
||||
? new ModelConverter(gameObject, CLIOptions.o_imageFormat.Value, animationList.Select(x => (AnimationClip)x.Asset).ToList())
|
||||
: new ModelConverter(gameObject, CLIOptions.o_imageFormat.Value);
|
||||
var modelName = FixFileName(gameObject.m_Name);
|
||||
var exportFullPath = Path.Combine(exportPath, "FBX_GameObjects", modelName, modelName + ".fbx");
|
||||
var modelName = gameObject.m_Name;
|
||||
var exportFullPath = SafeFilePath(exportPath, "FBX_GameObjects", modelName, modelName + ".fbx");
|
||||
if (File.Exists(exportFullPath))
|
||||
{
|
||||
exportFullPath = Path.Combine(exportPath, $"{modelName}_{gameObject.GetHashCode():X}", modelName + ".fbx");
|
||||
exportFullPath = SafeFilePath(exportPath, $"{modelName}_{gameObject.GetHashCode():X}", modelName + ".fbx");
|
||||
}
|
||||
ExportFbx(convert, exportFullPath);
|
||||
}
|
||||
|
||||
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()
|
||||
|
||||
@@ -3,7 +3,6 @@ using AssetStudioCLI.Options;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace AssetStudioCLI
|
||||
@@ -192,7 +191,7 @@ namespace AssetStudioCLI
|
||||
|
||||
private static bool TryExportFile(string dir, AssetItem item, string extension, out string fullPath)
|
||||
{
|
||||
var fileName = FixFileName(item.Text);
|
||||
var fileName = item.Text;
|
||||
var filenameFormat = CLIOptions.o_filenameFormat.Value;
|
||||
var canOverwrite = CLIOptions.f_overwriteExisting.Value;
|
||||
switch (filenameFormat)
|
||||
@@ -208,7 +207,7 @@ namespace AssetStudioCLI
|
||||
break;
|
||||
}
|
||||
extension = extension.ToLower();
|
||||
fullPath = Path.Combine(dir, $"{fileName}{extension}");
|
||||
fullPath = Exporter.SafeFilePath(dir, $"{fileName}{extension}");
|
||||
if (ExportPathDict.TryAdd(fullPath, true))
|
||||
{
|
||||
if (CanWrite(fullPath, dir, canOverwrite))
|
||||
@@ -218,7 +217,7 @@ namespace AssetStudioCLI
|
||||
}
|
||||
else if (filenameFormat == FilenameFormat.AssetName || (filenameFormat == FilenameFormat.AssetName_Hash && item.Hash == 0))
|
||||
{
|
||||
fullPath = Path.Combine(dir, $"{fileName}{item.UniqueID}{extension}");
|
||||
fullPath = Exporter.SafeFilePath(dir, $"{fileName}{item.UniqueID}{extension}");
|
||||
if (CanWrite(fullPath, dir, canOverwrite))
|
||||
{
|
||||
return true;
|
||||
@@ -252,13 +251,6 @@ namespace AssetStudioCLI
|
||||
}
|
||||
}
|
||||
|
||||
private static string FixFileName(string str)
|
||||
{
|
||||
return str.Length >= 260
|
||||
? Path.GetRandomFileName()
|
||||
: Path.GetInvalidFileNameChars().Aggregate(str, (current, c) => current.Replace(c, '_'));
|
||||
}
|
||||
|
||||
public static void ClearHash()
|
||||
{
|
||||
ExportPathDict.Clear();
|
||||
|
||||
@@ -679,7 +679,7 @@ namespace AssetStudioCLI
|
||||
exportPath = Path.Combine(savePath, containerPath);
|
||||
if (groupOption == AssetGroupOption.ContainerPathFull)
|
||||
{
|
||||
exportPath = Path.Combine(exportPath, Path.GetFileNameWithoutExtension(asset.Container));
|
||||
exportPath = Exporter.SafeFilePath(exportPath, Path.GetFileNameWithoutExtension(asset.Container));
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -700,7 +700,7 @@ namespace AssetStudioCLI
|
||||
case AssetGroupOption.SceneHierarchy:
|
||||
if (asset.Node != null)
|
||||
{
|
||||
exportPath = Path.Combine(savePath, asset.Node.FullPath);
|
||||
exportPath = Exporter.SafePathCombine(savePath, asset.Node.FullPath);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -866,7 +866,7 @@ namespace AssetStudioCLI
|
||||
var exportedCount = 0;
|
||||
var k = 0;
|
||||
|
||||
Logger.Info($"Searching for objects to export..");
|
||||
Logger.Info("Searching for objects to export..");
|
||||
Progress.Reset();
|
||||
var count = gameObjectTree.Sum(x => x.nodes.Count);
|
||||
foreach (var node in gameObjectTree)
|
||||
@@ -913,14 +913,14 @@ namespace AssetStudioCLI
|
||||
foreach (var gameObjectNode in exportableObjects)
|
||||
{
|
||||
var gameObject = gameObjectNode.gameObject;
|
||||
var filename = Exporter.FixFileName(gameObject.m_Name);
|
||||
var targetPath = $"{savePath}{filename}{Path.DirectorySeparatorChar}";
|
||||
var filename = gameObject.m_Name;
|
||||
var targetPath = $"{Exporter.SafeFilePath(savePath, filename)}{Path.DirectorySeparatorChar}";
|
||||
//重名文件处理
|
||||
for (int i = 1; ; i++)
|
||||
{
|
||||
if (Directory.Exists(targetPath))
|
||||
{
|
||||
targetPath = $"{savePath}{filename} ({i}){Path.DirectorySeparatorChar}";
|
||||
targetPath = $"{Exporter.SafeFilePath(savePath, filename)} ({i}){Path.DirectorySeparatorChar}";
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -1229,7 +1229,7 @@ namespace AssetStudioCLI
|
||||
break;
|
||||
}
|
||||
|
||||
var destPath = Path.Combine(baseDestPath, modelPath);
|
||||
var destPath = Exporter.SafePathCombine(baseDestPath, modelPath);
|
||||
cubismExtractor.ExtractCubismModel(destPath, motionMode, forceBezier, parallelTaskCount);
|
||||
modelCounter++;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user