100 {
".vert", &PipelineMsg::mutable_vert },
101 {
".frag", &PipelineMsg::mutable_frag },
102 {
".comp", &PipelineMsg::mutable_comp },
103 {
".tesc", &PipelineMsg::mutable_tesc },
104 {
".tese", &PipelineMsg::mutable_tese },
105 {
".geom", &PipelineMsg::mutable_geom }
121 ProcessArgs ( argc, argv );
122 std::filesystem::path input_path{mInputFile};
123 std::filesystem::path output_path{mOutputFile};
124 char magick_number[8] =
"AEONPLN";
125 if ( !input_path.has_extension() )
127 if ( output_path.extension() !=
".pln" && output_path.extension() !=
".txt" )
129 throw std::runtime_error (
"Unsupported output file extension, must be .pln or .txt" );
131 PipelineMsg pipeline_msg;
132 bool found_shader{
false};
135 std::filesystem::path shader_path{input_path.replace_extension ( extension ) };
136 if ( std::filesystem::exists ( shader_path ) )
138 std::ifstream shader_file ( shader_path );
141 throw std::runtime_error (
"Failed to open shader file: " + shader_path.string() );
143 std::string shader_code ( ( std::istreambuf_iterator<char> ( shader_file ) ), std::istreambuf_iterator<char>() );
145 setter ( &pipeline_msg )->assign ( shader_code );
151 if ( output_path.extension() ==
".pln" )
153 std::ofstream binary_file ( mOutputFile, std::ios::out | std::ios::binary );
154 binary_file << magick_number <<
'\0';
155 if ( !pipeline_msg.SerializeToOstream ( &binary_file ) )
157 std::cerr <<
"Failed to serialize pipeline message to binary format.";
158 throw std::runtime_error (
"Failed to serialize pipeline message to binary format." );
164 google::protobuf::TextFormat::Printer printer;
165 for (
int i = 1; i <= pipeline_msg.GetDescriptor()->field_count() ; ++i )
167 if ( !printer.RegisterFieldValuePrinter (
168 pipeline_msg.GetDescriptor()->FindFieldByNumber ( i ),
171 std::cout <<
"Failed to register field value printer." << std::endl;
174 std::string text_string;
175 std::ofstream text_file ( mOutputFile, std::ios::out );
176 if ( !printer.PrintToString ( pipeline_msg, &text_string ) )
178 std::cerr <<
"Failed to serialize pipeline message to text format.";
179 throw std::runtime_error (
"Failed to serialize pipeline message to text format." );
181 text_file << magick_number << std::endl;
182 text_file.write ( text_string.c_str(), text_string.length() );
188 throw std::runtime_error (
"No suitable shader file found." );
191 else if ( input_path.extension() ==
".pln" || input_path.extension() ==
".txt" )
194 PipelineMsg pipeline_msg;
195 std::ifstream input_file ( mInputFile, input_path.extension() ==
".pln" ? ( std::ios::in | std::ios::binary ) : std::ios::in );
198 throw std::runtime_error (
"Failed to open pipeline file: " + mInputFile );
203 if ( input_path.extension() ==
".pln" )
205 input_file.read ( file_magick, 8 );
206 if ( strncmp ( file_magick, magick_number, 7 ) != 0 )
208 throw std::runtime_error (
"Invalid pipeline file format (magic number mismatch)" );
210 if ( !pipeline_msg.ParseFromIstream ( &input_file ) )
212 throw std::runtime_error (
"Failed to parse binary pipeline file" );
217 input_file.getline ( file_magick, 8 );
218 if ( strncmp ( file_magick, magick_number, 7 ) != 0 )
220 throw std::runtime_error (
"Invalid pipeline file format (magic number mismatch)" );
222 std::string text_content ( ( std::istreambuf_iterator<char> ( input_file ) ), std::istreambuf_iterator<char>() );
223 if ( !google::protobuf::TextFormat::ParseFromString ( text_content, &pipeline_msg ) )
225 throw std::runtime_error (
"Failed to parse text pipeline file" );
231 std::filesystem::path base_path = output_path.has_extension() ? output_path.replace_extension() : output_path;
232 bool extracted_any =
false;
236 const std::string& shader_code = getter ( &pipeline_msg );
237 if ( !shader_code.empty() )
239 std::filesystem::path shader_path = base_path;
240 shader_path.replace_extension ( extension );
241 std::ofstream shader_file ( shader_path, std::ios::out );
244 throw std::runtime_error (
"Failed to create shader file: " + shader_path.string() );
246 shader_file << shader_code;
248 std::cout <<
"Extracted: " << shader_path.filename().string() << std::endl;
249 extracted_any =
true;
253 if ( !extracted_any )
255 std::cout <<
"Warning: No shader code found in pipeline file" << std::endl;
260 throw std::runtime_error (
"Unsupported file extension" );