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

75 lines
2.5 KiB
C#
Raw Normal View History

2019-05-31 23:08:55 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Windows.Forms;
using Toolbox.Library;
2019-05-31 23:08:55 +00:00
namespace FirstPlugin
{
public class NSWShaderDecompile
{
public enum NswShaderType
{
Vertex,
Geometry,
Fragment,
Compute
}
public static string DecompileShader(NswShaderType shaderType, byte[] Data, ulong Address = 0)
2019-05-31 23:08:55 +00:00
{
string TypeArg = "v";
switch (shaderType)
{
case NswShaderType.Vertex: TypeArg = "v"; break;
case NswShaderType.Fragment: TypeArg = "f"; break;
case NswShaderType.Geometry: TypeArg = "g"; break;
}
2019-06-01 00:20:07 +00:00
2019-05-31 23:08:55 +00:00
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;
2019-06-01 00:29:45 +00:00
start.Arguments = $"{TypeArg} {Utils.AddQuotesIfRequired("temp/shader1.bin")}";
2019-05-31 23:08:55 +00:00
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();
}
2019-06-01 00:20:07 +00:00
catch (Exception ex)
2019-05-31 23:08:55 +00:00
{
Toolbox.Library.Forms.STErrorDialog.Show("Failed to decompile shader!", "Shader Tools", ex.ToString());
2019-05-31 23:08:55 +00:00
return "";
}
}
}
}
}
}