Skip to content

Commit

Permalink
Removed duplicate code based on engine version, streamlined into a si…
Browse files Browse the repository at this point in the history
…ngle function
  • Loading branch information
Pepper-Man committed Jul 28, 2022
1 parent c6c2dd1 commit f723d05
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 110 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 (!AutoShaders.CreateEmptyShadersGen2(BaseDirectory, path, "H2")) { return; }; }
if (genShaders) { if (!AutoShaders.CreateEmptyShaders(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 (!AutoShaders.CreateEmptyShadersGen3(BaseDirectory, path, "H3ODST")) { return; }; }
if (genShaders) { if (!AutoShaders.CreateEmptyShaders(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 (!AutoShaders.CreateEmptyShadersGen3(BaseDirectory, path, "H3")) { return; }; }
if (genShaders) { if (!AutoShaders.CreateEmptyShaders(BaseDirectory, path, "H3")) { return; }; }
if (skyRender)
await RunTool(ToolType.Tool, new() { "render-sky", path });
else if (accurateRender)
Expand Down
124 changes: 17 additions & 107 deletions Launcher/Utility/AutoShaders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

internal class AutoShaders
{
public static bool CreateEmptyShadersGen3(string BaseDirectory, string path, string gameType)
public static bool CreateEmptyShaders(string BaseDirectory, string path, string gameType)
{
// Variables
string full_jms_path = "";
Expand Down Expand Up @@ -62,22 +62,30 @@ public static bool CreateEmptyShadersGen3(string BaseDirectory, string path, str

static void shaderGen(string[] shaders, int counter, string full_jms_path, string destinationShadersFolder, string BaseDirectory, string gameType)
{
string defaultShaderLocation = "";

// Create directories
Directory.CreateDirectory(destinationShadersFolder);

// Make sure default.shader exists, if not, create it
string defaultShaderLocation = BaseDirectory + @"\tags\levels\shared\shaders\simple\default.shader";
defaultShaderLocation = gameType == "H2"
? BaseDirectory + @"\tags\shaders\default.shader"
: BaseDirectory + @"\tags\levels\shared\shaders\simple\default.shader";

if (!File.Exists(defaultShaderLocation))
{
if (gameType == "H3")
{
File.WriteAllBytes(defaultShaderLocation, ToolkitLauncher.Utility.Resources.defaultH3);
}
else
else if (gameType == "H3ODST")
{
File.WriteAllBytes(defaultShaderLocation, ToolkitLauncher.Utility.Resources.defaultODST);
}

else
{
File.WriteAllBytes(defaultShaderLocation, ToolkitLauncher.Utility.Resources.defaultH2);
}
}

// Write each shader
Expand Down Expand Up @@ -106,10 +114,14 @@ static void shaderGen(string[] shaders, int counter, string full_jms_path, strin
{
File.WriteAllBytes(defaultShaderLocation, ToolkitLauncher.Utility.Resources.defaultH3);
}
else
else if (gameType == "H3ODST")
{
File.WriteAllBytes(defaultShaderLocation, ToolkitLauncher.Utility.Resources.defaultODST);
}
else
{
File.WriteAllBytes(defaultShaderLocation, ToolkitLauncher.Utility.Resources.defaultH2);
}
counter = 0;
shaderGen(shaders, counter, full_jms_path, destinationShadersFolder, BaseDirectory, gameType);
break;
Expand All @@ -122,109 +134,7 @@ 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;
}
Expand Down

0 comments on commit f723d05

Please sign in to comment.