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>
8 #include "../ToolCommand.hpp"
9 #include "../console.h"
11 #ifdef TOOL_LIB_EXPORTS
12 #define TOOL_LIB_API __declspec(dllexport)
14 #define TOOL_LIB_API __declspec(dllimport)
25 std::string placeholder_marker =
"@SURFACE_SHADER@";
27 bool combine_files(std::string template_path, std::string surface_path, std::string output_path)
29 if (!BlamStrings::Utils::IO::FileExists(template_path))
31 BlamStrings::Logger::LogEvent(
"failed to read shader template: '" + template_path +
"'", BlamLogLevel::Warning);
35 if (!BlamStrings::Utils::IO::FileExists(surface_path))
37 BlamStrings::Logger::LogEvent(
"failed to read surface shader: '" + surface_path +
"'", BlamLogLevel::Warning);
41 std::string template_text = BlamStrings::Utils::IO::GetFileContentsAsString(template_path);
42 std::string surface_text = BlamStrings::Utils::IO::GetFileContentsAsString(surface_path);
44 if (!BlamStrings::Utils::String::Contains(template_text, placeholder_marker))
46 BlamStrings::Logger::LogEvent(
"template file does not contain '" + placeholder_marker +
"' marker", BlamLogLevel::Warning);
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"
56 "// Template: " + template_path +
"\n"
57 "// Surface: " + surface_path +
"\n"
58 "// =====================================================================\n\n";
60 std::string combined_text = generation_header + BlamStrings::Utils::String::Replace(template_text, placeholder_marker, surface_text);
62 if (!BlamStrings::Utils::IO::CreateNewFile(output_path, combined_text))
64 BlamStrings::Logger::LogEvent(
"failed to write output file: '" + output_path +
"'", BlamLogLevel::Warning);
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";
80 int execute(std::vector<std::string> args)
84 BlamStrings::Logger::LogEvent(
"combine-shaders requires three arguments: template, surface, output", BlamLogLevel::Warning);
88 std::string template_path = args[0];
89 std::string surface_path = args[1];
90 std::string output_path = args[2];
92 if (!BlamStrings::Utils::IO::FileExists(template_path))
94 BlamStrings::Logger::LogEvent(
"template file '" + template_path +
"' does not exist", BlamLogLevel::Warning);
98 if (!BlamStrings::Utils::IO::FileExists(surface_path))
100 BlamStrings::Logger::LogEvent(
"surface file '" + surface_path +
"' does not exist", BlamLogLevel::Warning);
104 bool success = combine_files(template_path, surface_path, output_path);
108 BlamStrings::Logger::LogEvent(
"shader combination succeeded: '" + output_path +
"'", TerminalColor::BrightGreen);
112 BlamStrings::Logger::LogEvent(
"shader combination failed", TerminalColor::BrightRed);