improve export

This commit is contained in:
Perfare
2020-08-06 21:07:37 +08:00
parent 1766dcbdeb
commit 4e97b4b898
5 changed files with 77 additions and 74 deletions

View File

@ -10,7 +10,7 @@ namespace AssetStudioGUI
{
internal static class Exporter
{
public static bool ExportTexture2D(AssetItem item, string exportPathName)
public static bool ExportTexture2D(AssetItem item, string exportPath)
{
var m_Texture2D = (Texture2D)item.Asset;
if (Properties.Settings.Default.convertTexture)
@ -36,25 +36,25 @@ namespace AssetStudioGUI
tga = true;
break;
}
var exportFullName = exportPathName + item.Text + "." + ext.ToLower();
if (ExportFileExists(exportFullName))
if (!TryExportFile(exportPath, item, "." + ext.ToLower(), out var exportFullPath))
return false;
if (tga)
{
var file = new TGA(bitmap);
file.Save(exportFullName);
file.Save(exportFullPath);
}
else
bitmap.Save(exportFullName, format);
{
bitmap.Save(exportFullPath, format);
}
bitmap.Dispose();
return true;
}
else
{
var exportFullName = exportPathName + item.Text + ".tex";
if (ExportFileExists(exportFullName))
if (!TryExportFile(exportPath, item, ".tex", out var exportFullPath))
return false;
File.WriteAllBytes(exportFullName, m_Texture2D.image_data.GetData());
File.WriteAllBytes(exportFullPath, m_Texture2D.image_data.GetData());
return true;
}
}
@ -68,28 +68,25 @@ namespace AssetStudioGUI
var converter = new AudioClipConverter(m_AudioClip);
if (Properties.Settings.Default.convertAudio && converter.IsSupport)
{
var exportFullName = exportPath + item.Text + ".wav";
if (ExportFileExists(exportFullName))
if (!TryExportFile(exportPath, item, ".wav", out var exportFullPath))
return false;
var buffer = converter.ConvertToWav();
if (buffer == null)
return false;
File.WriteAllBytes(exportFullName, buffer);
File.WriteAllBytes(exportFullPath, buffer);
}
else
{
var exportFullName = exportPath + item.Text + converter.GetExtensionName();
if (ExportFileExists(exportFullName))
if (!TryExportFile(exportPath, item, converter.GetExtensionName(), out var exportFullPath))
return false;
File.WriteAllBytes(exportFullName, m_AudioData);
File.WriteAllBytes(exportFullPath, m_AudioData);
}
return true;
}
public static bool ExportShader(AssetItem item, string exportPath)
{
var exportFullName = exportPath + item.Text + ".shader";
if (ExportFileExists(exportFullName))
if (!TryExportFile(exportPath, item, ".shader", out var exportFullPath))
return false;
var m_Shader = (Shader)item.Asset;
if (m_Shader.compressedBlob != null) //5.5 and up
@ -104,7 +101,7 @@ namespace AssetStudioGUI
else
{
var str = ShaderConverter.Convert(m_Shader);
File.WriteAllText(exportFullName, str);
File.WriteAllText(exportFullPath, str);
}
return true;
}
@ -120,21 +117,19 @@ namespace AssetStudioGUI
extension = Path.GetExtension(item.Container);
}
}
var exportFullName = exportPath + item.Text + extension;
if (ExportFileExists(exportFullName))
if (!TryExportFile(exportPath, item, extension, out var exportFullPath))
return false;
File.WriteAllBytes(exportFullName, m_TextAsset.m_Script);
File.WriteAllBytes(exportFullPath, m_TextAsset.m_Script);
return true;
}
public static bool ExportMonoBehaviour(AssetItem item, string exportPath)
{
var exportFullName = exportPath + item.Text + ".txt";
if (ExportFileExists(exportFullName))
if (!TryExportFile(exportPath, item, ".txt", out var exportFullPath))
return false;
var m_MonoBehaviour = (MonoBehaviour)item.Asset;
var str = m_MonoBehaviour.Dump() ?? Studio.GetScriptString(item.Asset.reader);
File.WriteAllText(exportFullName, str);
File.WriteAllText(exportFullPath, str);
return true;
}
@ -148,10 +143,9 @@ namespace AssetStudioGUI
{
extension = ".otf";
}
var exportFullName = exportPath + item.Text + extension;
if (ExportFileExists(exportFullName))
if (!TryExportFile(exportPath, item, extension, out var exportFullPath))
return false;
File.WriteAllBytes(exportFullName, m_Font.m_FontData);
File.WriteAllBytes(exportFullPath, m_Font.m_FontData);
return true;
}
return false;
@ -162,8 +156,7 @@ namespace AssetStudioGUI
var m_Mesh = (Mesh)item.Asset;
if (m_Mesh.m_VertexCount <= 0)
return false;
var exportFullName = exportPath + item.Text + ".obj";
if (ExportFileExists(exportFullName))
if (!TryExportFile(exportPath, item, ".obj", out var exportFullPath))
return false;
var sb = new StringBuilder();
sb.AppendLine("g " + m_Mesh.m_Name);
@ -235,7 +228,7 @@ namespace AssetStudioGUI
#endregion
sb.Replace("NaN", "0");
File.WriteAllText(exportFullName, sb.ToString());
File.WriteAllText(exportFullPath, sb.ToString());
return true;
}
@ -245,10 +238,9 @@ namespace AssetStudioGUI
var m_VideoData = m_VideoClip.m_VideoData.GetData();
if (m_VideoData != null && m_VideoData.Length != 0)
{
var exportFullName = exportPath + item.Text + Path.GetExtension(m_VideoClip.m_OriginalPath);
if (ExportFileExists(exportFullName))
if (!TryExportFile(exportPath, item, Path.GetExtension(m_VideoClip.m_OriginalPath), out var exportFullPath))
return false;
File.WriteAllBytes(exportFullName, m_VideoData);
File.WriteAllBytes(exportFullPath, m_VideoData);
return true;
}
return false;
@ -257,10 +249,9 @@ namespace AssetStudioGUI
public static bool ExportMovieTexture(AssetItem item, string exportPath)
{
var m_MovieTexture = (MovieTexture)item.Asset;
var exportFullName = exportPath + item.Text + ".ogv";
if (ExportFileExists(exportFullName))
if (!TryExportFile(exportPath, item, ".ogv", out var exportFullPath))
return false;
File.WriteAllBytes(exportFullName, m_MovieTexture.m_MovieData);
File.WriteAllBytes(exportFullPath, m_MovieTexture.m_MovieData);
return true;
}
@ -284,8 +275,7 @@ namespace AssetStudioGUI
tga = true;
break;
}
var exportFullName = exportPath + item.Text + "." + type.ToLower();
if (ExportFileExists(exportFullName))
if (!TryExportFile(exportPath, item, "." + type.ToLower(), out var exportFullPath))
return false;
var bitmap = ((Sprite)item.Asset).GetImage();
if (bitmap != null)
@ -293,10 +283,12 @@ namespace AssetStudioGUI
if (tga)
{
var file = new TGA(bitmap);
file.Save(exportFullName);
file.Save(exportFullPath);
}
else
bitmap.Save(exportFullName, format);
{
bitmap.Save(exportFullPath, format);
}
bitmap.Dispose();
return true;
}
@ -305,36 +297,47 @@ namespace AssetStudioGUI
public static bool ExportRawFile(AssetItem item, string exportPath)
{
var exportFullName = exportPath + item.Text + ".dat";
if (ExportFileExists(exportFullName))
if (!TryExportFile(exportPath, item, ".dat", out var exportFullPath))
return false;
File.WriteAllBytes(exportFullName, item.Asset.GetRawData());
File.WriteAllBytes(exportFullPath, item.Asset.GetRawData());
return true;
}
private static bool ExportFileExists(string filename)
private static bool TryExportFile(string dir, AssetItem item, string extension, out string fullPath)
{
if (File.Exists(filename))
var fileName = FixFileName(item.Text);
fullPath = Path.Combine(dir, fileName + extension);
if (!File.Exists(fullPath))
{
Directory.CreateDirectory(dir);
return true;
}
fullPath = Path.Combine(dir, fileName + item.UniqueID + extension);
if (!File.Exists(fullPath))
{
Directory.CreateDirectory(dir);
return true;
}
Directory.CreateDirectory(Path.GetDirectoryName(filename));
return false;
}
public static bool ExportAnimator(AssetItem item, string exportPath, List<AssetItem> animationList = null)
{
var exportFullPath = Path.Combine(exportPath, item.Text, item.Text + ".fbx");
if (File.Exists(exportFullPath))
{
exportFullPath = Path.Combine(exportPath, item.Text + item.UniqueID, item.Text + ".fbx");
}
var m_Animator = (Animator)item.Asset;
var convert = animationList != null ? new ModelConverter(m_Animator, animationList.Select(x => (AnimationClip)x.Asset).ToArray()) : new ModelConverter(m_Animator);
exportPath = $"{exportPath}{item.Text}\\{item.Text}.fbx";
ExportFbx(convert, exportPath);
ExportFbx(convert, exportFullPath);
return true;
}
public static void ExportGameObject(GameObject gameObject, string exportPath, List<AssetItem> animationList = null)
{
var convert = animationList != null ? new ModelConverter(gameObject, animationList.Select(x => (AnimationClip)x.Asset).ToArray()) : new ModelConverter(gameObject);
exportPath = exportPath + Studio.FixFileName(gameObject.m_Name) + ".fbx";
exportPath = exportPath + FixFileName(gameObject.m_Name) + ".fbx";
ExportFbx(convert, exportPath);
}
@ -364,13 +367,12 @@ namespace AssetStudioGUI
public static bool ExportDumpFile(AssetItem item, string exportPath)
{
var exportFullName = exportPath + item.Text + ".txt";
if (ExportFileExists(exportFullName))
if (!TryExportFile(exportPath, item, ".txt", out var exportFullPath))
return false;
var str = item.Asset.Dump();
if (str != null)
{
File.WriteAllText(exportFullName, str);
File.WriteAllText(exportFullPath, str);
return true;
}
return false;
@ -408,5 +410,11 @@ namespace AssetStudioGUI
return ExportRawFile(item, exportPath);
}
}
public static string FixFileName(string str)
{
if (str.Length >= 260) return Path.GetRandomFileName();
return Path.GetInvalidFileNameChars().Aggregate(str, (current, c) => current.Replace(c, '_'));
}
}
}