Elaztek Developer Hub
Blamite Game Engine - Tool (Library)
A command-line utility that aids in the creation of Blamite Cache (.map) Files.
PrepareSurfaceShader.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <Strings/components/utils/io/io.h>
4 #include <Strings/components/utils/string/string.h>
5 #include <Strings/components/logger/logger.h>
6 #include <Strings/components/utils/list/list.h>
7 
8 #include "../ToolCommand.hpp"
9 #include "../console.h"
10 
11 #ifdef TOOL_LIB_EXPORTS
12 #define TOOL_LIB_API __declspec(dllexport)
13 #else
14 #define TOOL_LIB_API __declspec(dllimport)
15 #endif
16 
23 {
24 private:
25  std::string placeholder_marker = "@SURFACE_SHADER@";
26 
27  bool combine_files(std::string template_path, std::string surface_path, std::string output_path)
28  {
29  if (!BlamStrings::Utils::IO::FileExists(template_path))
30  {
31  BlamStrings::Logger::LogEvent("failed to read shader template: '" + template_path + "'", BlamLogLevel::Warning);
32  return false;
33  }
34 
35  if (!BlamStrings::Utils::IO::FileExists(surface_path))
36  {
37  BlamStrings::Logger::LogEvent("failed to read surface shader: '" + surface_path + "'", BlamLogLevel::Warning);
38  return false;
39  }
40 
41  std::string template_text = BlamStrings::Utils::IO::GetFileContentsAsString(template_path);
42  std::string surface_text = BlamStrings::Utils::IO::GetFileContentsAsString(surface_path);
43 
44  if (!BlamStrings::Utils::String::Contains(template_text, placeholder_marker))
45  {
46  BlamStrings::Logger::LogEvent("template file does not contain '" + placeholder_marker + "' marker", BlamLogLevel::Warning);
47  return false;
48  }
49 
50  std::string generation_header =
51  "// =====================================================================\n"
52  "// GENERATED FILE - DO NOT EDIT\n"
53  "// Produced by 'tool combine-shaders' from a template and surface file.\n"
54  "// Modify the template or surface source instead.\n"
55  "//\n"
56  "// Template: " + template_path + "\n"
57  "// Surface: " + surface_path + "\n"
58  "// =====================================================================\n\n";
59 
60  std::string combined_text = generation_header + BlamStrings::Utils::String::Replace(template_text, placeholder_marker, surface_text);
61 
62  if (!BlamStrings::Utils::IO::CreateNewFile(output_path, combined_text))
63  {
64  BlamStrings::Logger::LogEvent("failed to write output file: '" + output_path + "'", BlamLogLevel::Warning);
65  return false;
66  }
67 
68  return true;
69  }
70 
71 public:
72 
74  {
75  command = "prepare-surface-shader";
76  syntax = "prepare-surface-shader <template_path> <surface_path> <output_path>";
77  description = "combines a shader template with a surface shader";
78  }
79 
80  int execute(std::vector<std::string> args)
81  {
82  if (args.size() < 3)
83  {
84  BlamStrings::Logger::LogEvent("combine-shaders requires three arguments: template, surface, output", BlamLogLevel::Warning);
85  return -1;
86  }
87 
88  std::string template_path = args[0];
89  std::string surface_path = args[1];
90  std::string output_path = args[2];
91 
92  if (!BlamStrings::Utils::IO::FileExists(template_path))
93  {
94  BlamStrings::Logger::LogEvent("template file '" + template_path + "' does not exist", BlamLogLevel::Warning);
95  return -1;
96  }
97 
98  if (!BlamStrings::Utils::IO::FileExists(surface_path))
99  {
100  BlamStrings::Logger::LogEvent("surface file '" + surface_path + "' does not exist", BlamLogLevel::Warning);
101  return -1;
102  }
103 
104  bool success = combine_files(template_path, surface_path, output_path);
105 
106  if (success)
107  {
108  BlamStrings::Logger::LogEvent("shader combination succeeded: '" + output_path + "'", TerminalColor::BrightGreen);
109  return 0;
110  }
111 
112  BlamStrings::Logger::LogEvent("shader combination failed", TerminalColor::BrightRed);
113  return -1;
114  }
115 };
PrepareSurfaceShaderCommand
Class for the Prepare Surface Shader command.
Definition: PrepareSurfaceShader.hpp:22
PrepareSurfaceShaderCommand::PrepareSurfaceShaderCommand
PrepareSurfaceShaderCommand()
Definition: PrepareSurfaceShader.hpp:73
PrepareSurfaceShaderCommand::execute
int execute(std::vector< std::string > args)
Executes the command.
Definition: PrepareSurfaceShader.hpp:80
ToolCommand::syntax
std::string syntax
The syntax of the command. This should include the command name, as well as any arguments.
Definition: ToolCommand.hpp:19
ToolCommand::command
std::string command
The name of the command.
Definition: ToolCommand.hpp:18
ToolCommand
Base class representing a Tool command.
Definition: ToolCommand.hpp:15
ToolCommand::description
std::string description
A description of the command.
Definition: ToolCommand.hpp:20