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