1
0
mirror of synced 2024-11-12 02:00:50 +01:00

Add NUSHDB shader file.

This commit is contained in:
KillzXGaming 2019-05-31 19:08:55 -04:00
parent 3dd300e15f
commit 01ae391de6
12 changed files with 229 additions and 67 deletions

Binary file not shown.

View File

@ -55,14 +55,6 @@ namespace FirstPlugin
return null;
}
public enum ShaderType
{
Vertex,
Geometry,
Fragment,
Compute
}
public class Header
{
public List<ShaderVariation> ShaderVariations = new List<ShaderVariation>();
@ -200,7 +192,7 @@ namespace FirstPlugin
{
reader.Seek(VertexShaderOffset, SeekOrigin.Begin);
VertexShader = new ShaderSourceData();
VertexShader.shaderType = BNSH.ShaderType.Vertex;
VertexShader.shaderType = NSWShaderDecompile.NswShaderType.Vertex;
VertexShader.Format = Format;
VertexShader.Read(reader);
}
@ -208,7 +200,7 @@ namespace FirstPlugin
{
reader.Seek(FragmentShaderOffset, SeekOrigin.Begin);
FragmentShader = new ShaderSourceData();
FragmentShader.shaderType = BNSH.ShaderType.Fragment;
FragmentShader.shaderType = NSWShaderDecompile.NswShaderType.Fragment;
FragmentShader.Format = Format;
FragmentShader.Read(reader);
}
@ -219,7 +211,7 @@ namespace FirstPlugin
{
reader.Seek(VertexShaderOffset, SeekOrigin.Begin);
VertexShader = new ShaderData();
VertexShader.shaderType = BNSH.ShaderType.Vertex;
VertexShader.shaderType = NSWShaderDecompile.NswShaderType.Vertex;
VertexShader.Format = Format;
VertexShader.Read(reader);
}
@ -239,7 +231,7 @@ namespace FirstPlugin
{
reader.Seek(GeometryShaderOffset, SeekOrigin.Begin);
GeometryShader = new ShaderData();
GeometryShader.shaderType = BNSH.ShaderType.Geometry;
GeometryShader.shaderType = NSWShaderDecompile.NswShaderType.Geometry;
GeometryShader.Format = Format;
GeometryShader.Read(reader);
}
@ -247,7 +239,7 @@ namespace FirstPlugin
{
reader.Seek(FragmentShaderOffset, SeekOrigin.Begin);
FragmentShader = new ShaderData();
FragmentShader.shaderType = BNSH.ShaderType.Fragment;
FragmentShader.shaderType = NSWShaderDecompile.NswShaderType.Fragment;
FragmentShader.Format = Format;
FragmentShader.Read(reader);
}
@ -255,7 +247,7 @@ namespace FirstPlugin
{
reader.Seek(ComputeShaderOffset, SeekOrigin.Begin);
ComputeShader = new ShaderData();
ComputeShader.shaderType = BNSH.ShaderType.Compute;
ComputeShader.shaderType = NSWShaderDecompile.NswShaderType.Compute;
ComputeShader.Format = Format;
ComputeShader.Read(reader);
}
@ -323,7 +315,7 @@ namespace FirstPlugin
public class ShaderData : TreeNodeCustom
{
public ShaderType shaderType;
public NSWShaderDecompile.NswShaderType shaderType;
public List<byte[]> data = new List<byte[]>();
public int Format;
@ -392,57 +384,7 @@ namespace FirstPlugin
}
private string DecompileShader(int ShaderIndex = 1)
{
string TypeArg = "v";
switch (shaderType)
{
case ShaderType.Vertex: TypeArg = "v"; break;
case ShaderType.Fragment: TypeArg = "f"; break;
case ShaderType.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[1]);
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.CreateNoWindow = false;
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
{
return "";
}
}
}
// File.WriteAllBytes("temp/shader1.bin", Utils.CombineByteArray(data.ToArray()));
return "";
return NSWShaderDecompile.DecompileShader(shaderType, data[1]);
}
}
}

View File

@ -0,0 +1,77 @@
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)
{
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.CreateNoWindow = false;
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
{
return "";
}
}
}
// File.WriteAllBytes("temp/shader1.bin", Utils.CombineByteArray(data.ToArray()));
return "";
}
}
}

View File

@ -0,0 +1,120 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Switch_Toolbox.Library;
using Switch_Toolbox.Library.IO;
using System.Windows.Forms;
using Switch_Toolbox.Library.Forms;
namespace FirstPlugin
{
public class NUSHDB : TreeNodeFile, IFileFormat
{
public FileType FileType { get; set; } = FileType.Layout;
public bool CanSave { get; set; }
public string[] Description { get; set; } = new string[] { "Namco Shader Data Binary" };
public string[] Extension { get; set; } = new string[] { "*.nushdb" };
public string FileName { get; set; }
public string FilePath { get; set; }
public IFileInfo IFileInfo { get; set; }
public bool Identify(System.IO.Stream stream)
{
using (var reader = new Switch_Toolbox.Library.IO.FileReader(stream, true))
{
return reader.CheckSignature(4, "HBSS") && reader.CheckSignature(4, "RDHS", 16) ;
}
}
public Type[] Types
{
get
{
List<Type> types = new List<Type>();
return types.ToArray();
}
}
public void Load(System.IO.Stream stream)
{
Text = FileName;
using (var reader = new FileReader(stream))
{
reader.ByteOrder = Syroot.BinaryData.ByteOrder.LittleEndian;
//Skip the HBSS header
reader.Seek(0x10, System.IO.SeekOrigin.Begin);
reader.ReadSignature(4, "RDHS");
ushort VersionMajor = reader.ReadUInt16();
ushort VersionMinor = reader.ReadUInt16();
ulong Unknown = reader.ReadUInt64(); //Usually 16
ulong ProgramCount = reader.ReadUInt64();
for (int i = 0; i < (int)ProgramCount; i++)
{
ShaderProgram program = new ShaderProgram();
program.Text = reader.LoadString(true, typeof(ulong));
uint unk = reader.ReadUInt32();
uint unk2 = reader.ReadUInt32(); //Usually 2
long BinaryOffset = reader.ReadOffset(true, typeof(ulong));
ulong BinarySize = reader.ReadUInt64();
ulong BinarySize2 = reader.ReadUInt64();
ulong padding = reader.ReadUInt64();
ulong padding2 = reader.ReadUInt64();
using (reader.TemporarySeek(BinaryOffset, System.IO.SeekOrigin.Begin))
{
program.Data = reader.ReadBytes((int)BinarySize);
}
Nodes.Add(program);
}
}
}
public void Unload()
{
}
public byte[] Save()
{
return null;
}
public class ShaderProgram : TreeNodeCustom
{
NSWShaderDecompile.NswShaderType ShaderType { get; set; }
public byte[] Data { get; set; }
public string Code { get; set; }
public bool TryDecompileBinary()
{
Code = NSWShaderDecompile.DecompileShader(ShaderType, Data);
if (Code.Length > 0) return true;
else return false;
}
public override void OnClick(TreeView treeview)
{
bool IsSuccess = TryDecompileBinary();
TextEditor editor = (TextEditor)LibraryGUI.Instance.GetActiveContent(typeof(TextEditor));
if (editor == null)
{
editor = new TextEditor();
editor.Dock = DockStyle.Fill;
LibraryGUI.Instance.LoadEditor(editor);
}
editor.Text = Text;
editor.FillEditor(string.Join("", Code));
}
}
}
}

View File

@ -295,6 +295,7 @@ namespace FirstPlugin
Formats.Add(typeof(TEX3DS));
Formats.Add(typeof(NXARC));
Formats.Add(typeof(SP2));
Formats.Add(typeof(NUSHDB));
Formats.Add(typeof(Turbo.Course_MapCamera_bin));
Formats.Add(typeof(Turbo.PartsBIN));

View File

@ -232,6 +232,8 @@
<Compile Include="FileFormats\Font\BFFNT.cs" />
<Compile Include="FileFormats\Audio\Archives\BFGRP.cs" />
<Compile Include="FileFormats\Hashes\SAHT.cs" />
<Compile Include="FileFormats\Shader\NSWShaderDecompile.cs" />
<Compile Include="FileFormats\Shader\NUSHDB.cs" />
<Compile Include="FileFormats\Shader\NVNSH.cs" />
<Compile Include="FileFormats\Shader\SHARCFBNX.cs" />
<Compile Include="FileFormats\SizeTables\TPFileSizeTable.cs" />

View File

@ -1 +1 @@
1619e231c2d04d36ec585863c8bb883a2a31d0e7
0f3130142d6535028095bd6218f41b0b18c3297d

View File

@ -71,6 +71,26 @@ namespace Switch_Toolbox.Library.IO
return RealSignature;
}
public long ReadOffset(bool IsRelative, Type OffsetType)
{
long pos = Position;
long offset = 0;
if (OffsetType == typeof(long))
offset = ReadInt64();
if (OffsetType == typeof(ulong))
offset = (long)ReadUInt64();
if (OffsetType == typeof(uint))
offset = ReadUInt32();
if (OffsetType == typeof(int))
offset = ReadInt32();
if (IsRelative && offset != 0)
return pos + offset;
else
return offset;
}
public string LoadString(bool IsRelative, Type OffsetType, Encoding encoding = null, uint ReadStringLength = 0)
{
long pos = Position;