Skip to content

Commit

Permalink
Merged Shader Generation code into single file
Browse files Browse the repository at this point in the history
  • Loading branch information
Pepper-Man committed Jul 28, 2022
1 parent 6bc17aa commit c6c2dd1
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 118 deletions.
2 changes: 1 addition & 1 deletion Launcher/ToolkitInterface/H2AToolkit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public override async Task ImportModel(string path, ModelCompile importType, boo

if (importType.HasFlag(ModelCompile.render))
// Generate shaders if requested
if (genShaders) { if (!AutoShadersGen2.GenerateEmptyShaders(BaseDirectory, path, "H2")) { return; }; }
if (genShaders) { if (!AutoShaders.CreateEmptyShadersGen2(BaseDirectory, path, "H2")) { return; }; }
await RunTool(ToolType.Tool, new() { "render", path, accurateRender.ToString(), renderPRT.ToString() });
if (importType.HasFlag(ModelCompile.collision))
await RunTool(ToolType.Tool, new() { "collision", path });
Expand Down
2 changes: 1 addition & 1 deletion Launcher/ToolkitInterface/H3ODSTToolkit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public override async Task ImportModel(string path, ModelCompile importType, boo

if (importType.HasFlag(ModelCompile.render))
// Generate shaders if requested
if (genShaders) { if (!AutoShadersGen3.GenerateEmptyShaders(BaseDirectory, path, "H3ODST")) { return; }; }
if (genShaders) { if (!AutoShaders.CreateEmptyShadersGen3(BaseDirectory, path, "H3ODST")) { return; }; }
if (skyRender)
await RunTool(ToolType.Tool, new() { "render-sky", path });
else if (accurateRender)
Expand Down
2 changes: 1 addition & 1 deletion Launcher/ToolkitInterface/H3Toolkit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public override async Task ImportModel(string path, ModelCompile importType, boo

if (importType.HasFlag(ModelCompile.render))
// Generate shaders if requested
if (genShaders) { if (!AutoShadersGen3.GenerateEmptyShaders(BaseDirectory, path, "H3")) { return; }; }
if (genShaders) { if (!AutoShaders.CreateEmptyShadersGen3(BaseDirectory, path, "H3")) { return; }; }
if (skyRender)
await RunTool(ToolType.Tool, new() { "render-sky", path });
else if (accurateRender)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
using System.Collections.Generic;
using System.Text.RegularExpressions;

internal class AutoShadersGen3
internal class AutoShaders
{
public static bool GenerateEmptyShaders(string BaseDirectory, string path, string gameType)
public static bool CreateEmptyShadersGen3(string BaseDirectory, string path, string gameType)
{
// Variables
string full_jms_path = "";
Expand Down Expand Up @@ -125,6 +125,109 @@ static void shaderGen(string[] shaders, int counter, string full_jms_path, strin
// Default fall-through
return true;
}

public static bool CreateEmptyShadersGen2(string BaseDirectory, string path, string gameType)
{
// Variables
string full_jms_path = "";
int counter = 0;

//Grabbing full path from drive letter to render folder
string jmsPath = (BaseDirectory + @"\data\" + path + @"\render").Replace("\\\\", "\\");

// Get all files in render folder
string[] files = Array.Empty<string>();
try
{
files = Directory.GetFiles(jmsPath);
}
catch (DirectoryNotFoundException)
{
MessageBox.Show("Unable to find JMS filepath!\nThis usually happens if your filepath contains invalid characters.\nAborting model import and shader generation...", "Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}

string destinationShadersFolder = BaseDirectory + @"\tags\" + path + @"\shaders";

// Checking if shaders already exist, if so don't re-gen them
try
{
if (!(Directory.GetFiles(destinationShadersFolder) == Array.Empty<string>()))
{
Debug.WriteLine("Shaders already exist!");
if (MessageBox.Show("Shaders for this model already exist!\nWould you like to generate any missing shaders?", "Shader Gen. Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
{
string[] shaders = JMSMaterialReader.ReadAllMaterials(files, counter, full_jms_path, BaseDirectory, gameType);
shaderGen(shaders, counter, full_jms_path, destinationShadersFolder, BaseDirectory);
}
else
{
return true;
}
}
else
{
string[] shaders = JMSMaterialReader.ReadAllMaterials(files, counter, full_jms_path, BaseDirectory, gameType);
shaderGen(shaders, counter, full_jms_path, destinationShadersFolder, BaseDirectory);
}
}
catch (DirectoryNotFoundException)
{
Debug.WriteLine("No folders exist, proceeding with shader gen");
string[] shaders = JMSMaterialReader.ReadAllMaterials(files, counter, full_jms_path, BaseDirectory, gameType);
shaderGen(shaders, counter, full_jms_path, destinationShadersFolder, BaseDirectory);
}

static void shaderGen(string[] shaders, int counter, string full_jms_path, string destinationShadersFolder, string BaseDirectory)
{
// Create directories
Directory.CreateDirectory(destinationShadersFolder);

// Make sure default.shader exists, if not, create it
string defaultShaderLocation = BaseDirectory + @"\tags\shaders\default.shader";
if (!File.Exists(defaultShaderLocation))
{
File.WriteAllBytes(defaultShaderLocation, ToolkitLauncher.Utility.Resources.defaultH2);
}

// Write each shader
foreach (string shader in shaders)
{
string shaderName = shader + ".shader";
if (shaderName == ".shader")
{
MessageBox.Show("Detected an invalid (possibly blank) shader name!\nThis shader will not be generated.\nThis won't work well in-game.", "Shader Gen. Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
try { File.Copy(defaultShaderLocation, Path.Combine(destinationShadersFolder, "im.too.dumb.to.name.my.shader")); } catch { Debug.WriteLine("ah well"); };
continue;
}
if (!File.Exists(Path.Combine(destinationShadersFolder, shaderName)))
{
try
{
File.Copy(defaultShaderLocation, Path.Combine(destinationShadersFolder, shaderName));
}
catch (FileNotFoundException)
{
// Will probably only occur if user somehow deletes default.shader after the check for its existence occurs,
// but before shaders are generated
if (MessageBox.Show("Unable to find shader to copy from!\nThis really shouldn't have happened.\nPress OK to try again, or Cancel to skip shader generation.", "Shader Gen. Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
{
File.WriteAllBytes(defaultShaderLocation, ToolkitLauncher.Utility.Resources.defaultH2);
counter = 0;
shaderGen(shaders, counter, full_jms_path, destinationShadersFolder, BaseDirectory);
break;
}
else
{
break;
}
}
}
}
}
// Default fall-through
return true;
}
}

// JMS Material Parser
Expand Down
113 changes: 0 additions & 113 deletions Launcher/Utility/AutoShadersGen2.cs

This file was deleted.

0 comments on commit c6c2dd1

Please sign in to comment.