Switch-Toolbox/Switch_FileFormatsMain/FileFormats/Shader/NSWShaderDecompile.cs

74 lines
2.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Windows.Forms;
using Switch_Toolbox.Library;
namespace FirstPlugin
{
public class NSWShaderDecompile
{
public enum NswShaderType
{
Vertex,
Geometry,
Fragment,
Compute
}
public static string DecompileShader(NswShaderType shaderType, byte[] Data, ulong Address = 0)
{
string TypeArg = "v";
switch (shaderType)
{
case NswShaderType.Vertex: TypeArg = "v"; break;
case NswShaderType.Fragment: TypeArg = "f"; break;
case NswShaderType.Geometry: TypeArg = "g"; break;
}
if (!Directory.Exists("temp"))
Directory.CreateDirectory("temp");
if (!Directory.Exists("ShaderTools"))
Directory.CreateDirectory("ShaderTools");
// File.WriteAllBytes("temp/shader1.bin", Utils.CombineByteArray(data.ToArray()));
File.WriteAllBytes("temp/shader1.bin", Data);
if (!File.Exists($"{Runtime.ExecutableDir}/ShaderTools/Ryujinx.ShaderTools.exe"))
{
MessageBox.Show("No shader decompiler found in ShaderTools. If you want to decompile a shader, you can use Ryujinx's ShaderTools.exe and put in the ShaderTools folder of the toolbox.");
return "";
}
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = "ShaderTools/Ryujinx.ShaderTools.exe";
start.WorkingDirectory = Runtime.ExecutableDir;
start.Arguments = $"{TypeArg} {Utils.AddQuotesIfRequired("temp/shader1.bin")}";
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
start.CreateNoWindow = true;
start.WindowStyle = ProcessWindowStyle.Hidden;
using (Process process = Process.Start(start))
{
using (StreamReader reader = process.StandardOutput)
{
try
{
return reader.ReadToEnd();
}
catch (Exception ex)
{
Switch_Toolbox.Library.Forms.STErrorDialog.Show("Failed to decompile shader!", "Shader Tools", ex.ToString());
return "";
}
}
}
}
}
}