AssetStudio/AssetStudioGUI/Exporter.cs

339 lines
14 KiB
C#
Raw Normal View History

2018-11-18 22:48:06 +00:00
using System.Collections.Generic;
2018-03-01 12:01:25 +00:00
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
2018-11-18 22:48:06 +00:00
using AssetStudio;
2018-03-01 12:01:25 +00:00
2018-11-18 22:48:06 +00:00
namespace AssetStudioGUI
2018-03-01 12:01:25 +00:00
{
2018-11-18 22:48:06 +00:00
internal static class Exporter
2018-03-01 12:01:25 +00:00
{
2018-11-18 22:48:06 +00:00
public static bool ExportTexture2D(AssetItem item, string exportPathName)
2018-03-01 12:01:25 +00:00
{
2018-11-21 07:37:56 +00:00
var converter = new Texture2DConverter((Texture2D)item.Asset);
2018-09-26 17:15:37 +00:00
var convertTexture = (bool)Properties.Settings.Default["convertTexture"];
if (convertTexture)
2018-03-01 12:01:25 +00:00
{
2018-11-18 22:48:06 +00:00
var bitmap = converter.ConvertToBitmap(true);
2018-09-26 17:15:37 +00:00
if (bitmap == null)
return false;
2018-03-01 12:01:25 +00:00
ImageFormat format = null;
var ext = (string)Properties.Settings.Default["convertType"];
switch (ext)
{
case "BMP":
format = ImageFormat.Bmp;
break;
case "PNG":
format = ImageFormat.Png;
break;
case "JPEG":
format = ImageFormat.Jpeg;
break;
}
2018-11-18 22:48:06 +00:00
var exportFullName = exportPathName + item.Text + "." + ext.ToLower();
2018-03-01 12:01:25 +00:00
if (ExportFileExists(exportFullName))
return false;
bitmap.Save(exportFullName, format);
bitmap.Dispose();
return true;
}
2018-04-11 15:45:39 +00:00
else
2018-03-01 12:01:25 +00:00
{
2018-11-18 22:48:06 +00:00
var exportFullName = exportPathName + item.Text + converter.GetExtensionName();
2018-03-01 12:01:25 +00:00
if (ExportFileExists(exportFullName))
return false;
2018-09-26 17:15:37 +00:00
File.WriteAllBytes(exportFullName, converter.ConvertToContainer());
2018-03-01 12:01:25 +00:00
return true;
}
}
2018-11-18 22:48:06 +00:00
public static bool ExportAudioClip(AssetItem item, string exportPath)
2018-03-01 12:01:25 +00:00
{
2018-11-21 07:37:56 +00:00
var m_AudioClip = (AudioClip)item.Asset;
var m_AudioData = m_AudioClip.m_AudioData.Value;
if (m_AudioData == null || m_AudioData.Length == 0)
2018-03-01 12:01:25 +00:00
return false;
var convertAudio = (bool)Properties.Settings.Default["convertAudio"];
2018-09-26 17:15:37 +00:00
var converter = new AudioClipConverter(m_AudioClip);
if (convertAudio && converter.IsFMODSupport)
2018-03-01 12:01:25 +00:00
{
2018-11-18 22:48:06 +00:00
var exportFullName = exportPath + item.Text + ".wav";
2018-03-01 12:01:25 +00:00
if (ExportFileExists(exportFullName))
return false;
2018-09-26 17:15:37 +00:00
var buffer = converter.ConvertToWav();
if (buffer == null)
2018-03-01 12:01:25 +00:00
return false;
File.WriteAllBytes(exportFullName, buffer);
}
else
{
2018-11-18 22:48:06 +00:00
var exportFullName = exportPath + item.Text + converter.GetExtensionName();
2018-03-01 12:01:25 +00:00
if (ExportFileExists(exportFullName))
return false;
2018-11-21 07:37:56 +00:00
File.WriteAllBytes(exportFullName, m_AudioData);
2018-03-01 12:01:25 +00:00
}
return true;
}
2018-11-18 22:48:06 +00:00
public static bool ExportShader(AssetItem item, string exportPath)
2018-03-01 12:01:25 +00:00
{
2018-11-18 22:48:06 +00:00
var exportFullName = exportPath + item.Text + ".shader";
2018-03-01 12:01:25 +00:00
if (ExportFileExists(exportFullName))
return false;
2019-07-17 04:51:00 +00:00
var m_Shader = (Shader)item.Asset;
2018-12-11 05:01:10 +00:00
if (m_Shader.compressedBlob != null) //5.5 and up
{
var strs = ShaderConverter.ConvertMultiple(m_Shader);
for (int i = 0; i < strs.Length; i++)
{
var platformName = ShaderConverter.GetPlatformString(m_Shader.platforms[i]);
File.WriteAllText($"{exportPath}{item.Text}_{platformName}.shader", strs[i]);
}
}
else
{
var str = ShaderConverter.Convert(m_Shader);
File.WriteAllText(exportFullName, str);
}
2018-03-01 12:01:25 +00:00
return true;
}
2018-11-18 22:48:06 +00:00
public static bool ExportTextAsset(AssetItem item, string exportPath)
2018-03-01 12:01:25 +00:00
{
2018-11-21 07:37:56 +00:00
var m_TextAsset = (TextAsset)(item.Asset);
var exportFullName = exportPath + item.Text + (item.Extension ?? ".txt");
2018-03-01 12:01:25 +00:00
if (ExportFileExists(exportFullName))
return false;
File.WriteAllBytes(exportFullName, m_TextAsset.m_Script);
return true;
}
2018-11-18 22:48:06 +00:00
public static bool ExportMonoBehaviour(AssetItem item, string exportPath)
2018-03-01 12:01:25 +00:00
{
2018-11-18 22:48:06 +00:00
var exportFullName = exportPath + item.Text + ".txt";
2018-03-01 12:01:25 +00:00
if (ExportFileExists(exportFullName))
return false;
2018-11-21 07:37:56 +00:00
var m_MonoBehaviour = (MonoBehaviour)item.Asset;
var str = m_MonoBehaviour.Dump() ?? Studio.GetScriptString(item.Asset.reader);
2018-08-04 20:46:27 +00:00
File.WriteAllText(exportFullName, str);
2018-03-01 12:01:25 +00:00
return true;
}
2018-11-18 22:48:06 +00:00
public static bool ExportFont(AssetItem item, string exportPath)
2018-03-01 12:01:25 +00:00
{
2018-11-21 07:37:56 +00:00
var m_Font = (Font)item.Asset;
2018-03-01 12:01:25 +00:00
if (m_Font.m_FontData != null)
{
2018-11-07 21:00:53 +00:00
var extension = ".ttf";
2018-09-26 17:15:37 +00:00
if (m_Font.m_FontData[0] == 79 && m_Font.m_FontData[1] == 84 && m_Font.m_FontData[2] == 84 && m_Font.m_FontData[3] == 79)
{
extension = ".otf";
}
2018-11-18 22:48:06 +00:00
var exportFullName = exportPath + item.Text + extension;
2018-03-01 12:01:25 +00:00
if (ExportFileExists(exportFullName))
return false;
File.WriteAllBytes(exportFullName, m_Font.m_FontData);
return true;
}
return false;
}
2018-11-18 22:48:06 +00:00
public static bool ExportMesh(AssetItem item, string exportPath)
2018-03-01 12:01:25 +00:00
{
2018-11-21 07:37:56 +00:00
var m_Mesh = (Mesh)item.Asset;
2018-03-01 12:01:25 +00:00
if (m_Mesh.m_VertexCount <= 0)
return false;
2018-11-18 22:48:06 +00:00
var exportFullName = exportPath + item.Text + ".obj";
2018-03-01 12:01:25 +00:00
if (ExportFileExists(exportFullName))
return false;
var sb = new StringBuilder();
sb.AppendLine("g " + m_Mesh.m_Name);
#region Vertices
2018-07-14 10:22:14 +00:00
if (m_Mesh.m_Vertices == null || m_Mesh.m_Vertices.Length == 0)
{
return false;
}
2018-03-01 12:01:25 +00:00
int c = 3;
if (m_Mesh.m_Vertices.Length == m_Mesh.m_VertexCount * 4)
{
c = 4;
}
for (int v = 0; v < m_Mesh.m_VertexCount; v++)
{
sb.AppendFormat("v {0} {1} {2}\r\n", -m_Mesh.m_Vertices[v * c], m_Mesh.m_Vertices[v * c + 1], m_Mesh.m_Vertices[v * c + 2]);
}
#endregion
#region UV
2019-07-27 14:54:18 +00:00
if (m_Mesh.m_UV0?.Length > 0)
2018-03-01 12:01:25 +00:00
{
2019-07-27 14:54:18 +00:00
if (m_Mesh.m_UV0.Length == m_Mesh.m_VertexCount * 2)
2018-03-01 12:01:25 +00:00
{
2019-07-27 14:54:18 +00:00
c = 2;
}
else if (m_Mesh.m_UV0.Length == m_Mesh.m_VertexCount * 3)
{
c = 3;
2018-03-01 12:01:25 +00:00
}
for (int v = 0; v < m_Mesh.m_VertexCount; v++)
{
2019-07-27 14:54:18 +00:00
sb.AppendFormat("vt {0} {1}\r\n", m_Mesh.m_UV0[v * c], m_Mesh.m_UV0[v * c + 1]);
2018-03-01 12:01:25 +00:00
}
}
#endregion
#region Normals
2019-07-27 14:54:18 +00:00
if (m_Mesh.m_Normals?.Length > 0)
2018-03-01 12:01:25 +00:00
{
if (m_Mesh.m_Normals.Length == m_Mesh.m_VertexCount * 3)
{
c = 3;
}
else if (m_Mesh.m_Normals.Length == m_Mesh.m_VertexCount * 4)
{
c = 4;
}
for (int v = 0; v < m_Mesh.m_VertexCount; v++)
{
sb.AppendFormat("vn {0} {1} {2}\r\n", -m_Mesh.m_Normals[v * c], m_Mesh.m_Normals[v * c + 1], m_Mesh.m_Normals[v * c + 2]);
}
}
#endregion
#region Face
int sum = 0;
for (var i = 0; i < m_Mesh.m_SubMeshes.Length; i++)
2018-03-01 12:01:25 +00:00
{
sb.AppendLine($"g {m_Mesh.m_Name}_{i}");
int indexCount = (int)m_Mesh.m_SubMeshes[i].indexCount;
var end = sum + indexCount / 3;
for (int f = sum; f < end; f++)
{
sb.AppendFormat("f {0}/{0}/{0} {1}/{1}/{1} {2}/{2}/{2}\r\n", m_Mesh.m_Indices[f * 3 + 2] + 1, m_Mesh.m_Indices[f * 3 + 1] + 1, m_Mesh.m_Indices[f * 3] + 1);
}
sum = end;
}
#endregion
sb.Replace("NaN", "0");
File.WriteAllText(exportFullName, sb.ToString());
return true;
}
2018-11-18 22:48:06 +00:00
public static bool ExportVideoClip(AssetItem item, string exportPath)
2018-03-01 12:01:25 +00:00
{
2018-11-21 07:37:56 +00:00
var m_VideoClip = (VideoClip)item.Asset;
var m_VideoData = m_VideoClip.m_VideoData.Value;
if (m_VideoData != null && m_VideoData.Length != 0)
2018-03-01 12:01:25 +00:00
{
2018-11-18 22:48:06 +00:00
var exportFullName = exportPath + item.Text + Path.GetExtension(m_VideoClip.m_OriginalPath);
2018-03-01 12:01:25 +00:00
if (ExportFileExists(exportFullName))
return false;
2018-11-21 07:37:56 +00:00
File.WriteAllBytes(exportFullName, m_VideoData);
2018-03-01 12:01:25 +00:00
return true;
}
return false;
}
2018-11-18 22:48:06 +00:00
public static bool ExportMovieTexture(AssetItem item, string exportPath)
2018-03-01 12:01:25 +00:00
{
2018-11-21 07:37:56 +00:00
var m_MovieTexture = (MovieTexture)item.Asset;
2018-11-18 22:48:06 +00:00
var exportFullName = exportPath + item.Text + ".ogv";
2018-03-01 12:01:25 +00:00
if (ExportFileExists(exportFullName))
return false;
File.WriteAllBytes(exportFullName, m_MovieTexture.m_MovieData);
return true;
}
2018-11-18 22:48:06 +00:00
public static bool ExportSprite(AssetItem item, string exportPath)
2018-03-01 12:01:25 +00:00
{
ImageFormat format = null;
var type = (string)Properties.Settings.Default["convertType"];
switch (type)
{
case "BMP":
format = ImageFormat.Bmp;
break;
case "PNG":
format = ImageFormat.Png;
break;
case "JPEG":
format = ImageFormat.Jpeg;
break;
}
2018-11-18 22:48:06 +00:00
var exportFullName = exportPath + item.Text + "." + type.ToLower();
2018-03-01 12:01:25 +00:00
if (ExportFileExists(exportFullName))
return false;
2018-11-21 07:37:56 +00:00
var bitmap = SpriteHelper.GetImageFromSprite((Sprite)item.Asset);
2018-03-01 12:01:25 +00:00
if (bitmap != null)
{
bitmap.Save(exportFullName, format);
2018-11-08 01:09:55 +00:00
bitmap.Dispose();
2018-03-01 12:01:25 +00:00
return true;
}
return false;
}
2018-11-18 22:48:06 +00:00
public static bool ExportRawFile(AssetItem item, string exportPath)
2018-03-01 12:01:25 +00:00
{
2018-11-18 22:48:06 +00:00
var exportFullName = exportPath + item.Text + ".dat";
2018-03-01 12:01:25 +00:00
if (ExportFileExists(exportFullName))
return false;
2018-11-21 07:37:56 +00:00
File.WriteAllBytes(exportFullName, item.Asset.GetRawData());
2018-03-01 12:01:25 +00:00
return true;
}
private static bool ExportFileExists(string filename)
{
if (File.Exists(filename))
{
return true;
}
Directory.CreateDirectory(Path.GetDirectoryName(filename));
return false;
}
2018-04-06 23:51:33 +00:00
2018-11-18 22:48:06 +00:00
public static bool ExportAnimator(AssetItem item, string exportPath, List<AssetItem> animationList = null)
2018-04-11 07:52:37 +00:00
{
2018-11-21 07:37:56 +00:00
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";
2019-07-17 04:51:00 +00:00
ExportFbx(convert, exportPath);
return true;
2018-04-11 07:52:37 +00:00
}
2019-07-17 04:51:00 +00:00
public static void ExportGameObject(GameObject gameObject, string exportPath, List<AssetItem> animationList = null)
2018-04-11 07:52:37 +00:00
{
2018-11-21 07:37:56 +00:00
var convert = animationList != null ? new ModelConverter(gameObject, animationList.Select(x => (AnimationClip)x.Asset).ToArray()) : new ModelConverter(gameObject);
2018-09-26 17:15:37 +00:00
exportPath = exportPath + Studio.FixFileName(gameObject.m_Name) + ".fbx";
2019-07-17 04:51:00 +00:00
ExportFbx(convert, exportPath);
2018-04-11 07:52:37 +00:00
}
2019-07-17 04:51:00 +00:00
public static void ExportGameObjectMerge(List<GameObject> gameObject, string exportPath, List<AssetItem> animationList = null)
{
var rootName = Path.GetFileNameWithoutExtension(exportPath);
var convert = animationList != null ? new ModelConverter(rootName, gameObject, animationList.Select(x => (AnimationClip)x.Asset).ToArray()) : new ModelConverter(rootName, gameObject);
ExportFbx(convert, exportPath);
}
private static void ExportFbx(IImported convert, string exportPath)
2018-04-06 23:51:33 +00:00
{
2018-11-12 17:07:24 +00:00
var eulerFilter = (bool)Properties.Settings.Default["eulerFilter"];
2018-04-06 23:51:33 +00:00
var filterPrecision = (float)(decimal)Properties.Settings.Default["filterPrecision"];
2019-07-28 10:55:08 +00:00
var exportAllNodes = (bool)Properties.Settings.Default["exportAllNodes"];
var exportSkins = (bool)Properties.Settings.Default["exportSkins"];
var exportAnimations = (bool)Properties.Settings.Default["exportAnimations"];
var exportBlendShape = (bool)Properties.Settings.Default["exportBlendShape"];
var castToBone = (bool)Properties.Settings.Default["castToBone"];
2018-04-06 23:51:33 +00:00
var boneSize = (int)(decimal)Properties.Settings.Default["boneSize"];
2018-11-12 17:07:24 +00:00
var scaleFactor = (float)(decimal)Properties.Settings.Default["scaleFactor"];
2018-09-13 12:52:17 +00:00
var fbxVersion = (int)Properties.Settings.Default["fbxVersion"];
2018-10-27 01:39:45 +00:00
var fbxFormat = (int)Properties.Settings.Default["fbxFormat"];
2019-07-28 10:55:08 +00:00
ModelExporter.ExportFbx(exportPath, convert, eulerFilter, filterPrecision,
exportAllNodes, exportSkins, exportAnimations, exportBlendShape, castToBone, boneSize, scaleFactor, fbxVersion, fbxFormat == 1);
2018-04-06 23:51:33 +00:00
}
2018-03-01 12:01:25 +00:00
}
}