95 std::string pattern{
"\\(\\s*" };
97 for (
const auto& i : aMeshMsg.attribute() )
101 case AttributeMsg::BYTE:
102 case AttributeMsg::SHORT:
103 case AttributeMsg::INT:
104 for (
size_t j = 0; j < i.size(); ++j )
106 if ( pattern.back() ==
')' )
108 pattern += separator_pattern;
110 pattern += int_pattern;
113 case AttributeMsg::UNSIGNED_BYTE:
114 case AttributeMsg::UNSIGNED_SHORT:
115 case AttributeMsg::UNSIGNED_INT:
116 for (
size_t j = 0; j < i.size(); ++j )
118 if ( pattern.back() ==
')' )
120 pattern += separator_pattern;
122 pattern += uint_pattern;
125 case AttributeMsg::HALF_FLOAT:
126 case AttributeMsg::FLOAT:
127 case AttributeMsg::FIXED:
128 case AttributeMsg::DOUBLE:
129 for (
size_t j = 0; j < i.size(); ++j )
131 if ( pattern.back() ==
')' )
133 pattern += separator_pattern;
135 pattern += float_pattern;
142 pattern +=
"\\s*\\)";
288 template<
class T>
size_t Parse (
size_t index,
const std::smatch& match_results, std::string& vertex_buffer,
size_t count )
291 for (
size_t i = 0; i < count; ++i )
293 if constexpr ( std::is_same_v<T, int32_t> )
295 value = stoi ( match_results[index++] );
297 else if constexpr ( std::is_same_v<T, uint32_t> )
299 value =
static_cast<T
> ( stoul ( match_results[index++] ) );
301 else if constexpr ( std::is_same_v<T, float> )
303 value = stof ( match_results[index++] );
305 else if constexpr ( std::is_same_v<T, double> )
307 value = stod ( match_results[index++] );
309 vertex_buffer.append (
reinterpret_cast<char*
> ( &value ),
sizeof ( T ) );
316 std::string vertex_buffer;
317 std::smatch match_results;
318 std::string vertex_string{ aMeshMsg.vertexbuffer() };
322 while ( std::regex_search ( vertex_string, match_results, vertex_regex ) )
325 for (
const auto& i : aMeshMsg.attribute() )
329 case AttributeMsg::BYTE:
330 index =
Parse<int8_t> ( index, match_results, vertex_buffer, i.size() );
332 case AttributeMsg::SHORT:
333 index =
Parse<int16_t> ( index, match_results, vertex_buffer, i.size() );
335 case AttributeMsg::INT:
336 index =
Parse<int32_t> ( index, match_results, vertex_buffer, i.size() );
338 case AttributeMsg::UNSIGNED_BYTE:
339 index =
Parse<uint8_t> ( index, match_results, vertex_buffer, i.size() );
341 case AttributeMsg::UNSIGNED_SHORT:
342 index =
Parse<uint16_t> ( index, match_results, vertex_buffer, i.size() );
344 case AttributeMsg::UNSIGNED_INT:
345 index =
Parse<uint32_t> ( index, match_results, vertex_buffer, i.size() );
347 case AttributeMsg::HALF_FLOAT:
350 case AttributeMsg::FLOAT:
351 index =
Parse<float> ( index, match_results, vertex_buffer, i.size() );
353 case AttributeMsg::FIXED:
356 case AttributeMsg::DOUBLE:
357 index =
Parse<double> ( index, match_results, vertex_buffer, i.size() );
363 vertex_string = match_results.suffix();
365 return vertex_buffer;
367 catch (
const std::regex_error& e )
369 std::cout <<
"Error: " << e.what() <<
" at " << __func__ <<
" line " << __LINE__ << std::endl;
380 std::string index_buffer;
381 std::smatch match_results;
382 std::string index_string{ aMeshMsg.indexbuffer() };
385 std::regex index_regex ( int_pattern );
386 while ( std::regex_search ( index_string, match_results, index_regex ) )
388 uint8_t uint8_t_value;
389 uint16_t uint16_t_value;
390 uint32_t uint32_t_value;
391 switch ( aMeshMsg.indexsize() )
394 uint8_t_value =
static_cast<uint8_t
> ( std::stoi ( match_results[1] ) );
395 index_buffer.append (
reinterpret_cast<char*
> ( &uint8_t_value ),
sizeof ( uint8_t ) );
398 uint16_t_value =
static_cast<uint16_t
> ( std::stoi ( match_results[1] ) );
399 index_buffer.append (
reinterpret_cast<char*
> ( &uint16_t_value ),
sizeof ( uint16_t ) );
402 uint32_t_value =
static_cast<uint32_t
> ( std::stoi ( match_results[1] ) );
403 index_buffer.append (
reinterpret_cast<char*
> ( &uint32_t_value ),
sizeof ( uint32_t ) );
406 index_string = match_results.suffix();
410 catch (
const std::regex_error& e )
412 std::cout <<
"Error: " << e.what() <<
" at " << __func__ <<
" line " << __LINE__ << std::endl;
479 ProcessArgs ( argc, argv );
480 PipelineMsg pipeline_buffer;
481 MaterialMsg material_buffer;
483 SkeletonMsg skeleton_buffer;
484 ::google::protobuf::Message* message =
nullptr;
485 char magick_number[8] = { 0 };
486 bool binary_input =
false;
489 struct stat stat_buffer;
490 if ( stat ( mInputFile.c_str(), &stat_buffer ) != 0 )
492 std::ostringstream stream;
493 stream <<
"File " << mInputFile <<
" Not Found (error code:" << errno <<
")";
494 throw std::runtime_error ( stream.str().c_str() );
497 file.exceptions ( std::ifstream::failbit | std::ifstream::badbit );
498 file.open ( mInputFile, std::ifstream::in | std::ifstream::binary );
499 file.read ( magick_number,
sizeof ( magick_number ) );
500 file.exceptions ( std::ifstream::badbit );
503 switch ( GetFileType ( magick_number ) )
506 case FileType::AEONPLNB:
509 case FileType::AEONPLNT:
510 message = &pipeline_buffer;
513 case FileType::AEONMTLB:
516 case FileType::AEONMTLT:
517 message = &material_buffer;
520 case FileType::AEONMSHB:
523 case FileType::AEONMSHT:
524 message = &mesh_buffer;
527 case FileType::AEONSKLB:
530 case FileType::AEONSKLT:
531 message = &skeleton_buffer;
535 std::ostringstream stream;
536 stream <<
"File" << mInputFile <<
" is not in a valid AeonGames format.";
537 throw std::runtime_error ( stream.str().c_str() );
540 assert ( message &&
"Message is null." );
545 if ( !message->ParseFromIstream ( &file ) )
547 throw std::runtime_error (
"Binary file parsing failed." );
552 google::protobuf::TextFormat::Parser parser;
553 std::string text ( ( std::istreambuf_iterator<char> ( file ) ), std::istreambuf_iterator<char>() );
554 if ( !parser.ParseFromString (
558 throw std::runtime_error (
"Text file parsing failed." );
560 if ( message == &mesh_buffer )
564 if (
GetStride ( mesh_buffer ) *mesh_buffer.vertexcount() != mesh_buffer.vertexbuffer().size() )
570 if ( ( mesh_buffer.indexcount() * mesh_buffer.indexsize() ) != mesh_buffer.indexbuffer().size() )
583 google::protobuf::TextFormat::Printer printer;
586 if ( message == &pipeline_buffer )
588 for (
int i = 1; i <= pipeline_buffer.GetDescriptor()->field_count() ; ++i )
590 if ( !printer.RegisterFieldValuePrinter (
591 pipeline_buffer.GetDescriptor()->FindFieldByNumber ( i ),
594 std::cout <<
"Failed to register field value printer." << std::endl;
600 if ( ( message == &mesh_buffer ) && ( !printer.RegisterFieldValuePrinter (
601 mesh_buffer.GetDescriptor()->FindFieldByName (
"VertexBuffer" ),
604 std::cout <<
"Failed to register vertex buffer printer." << std::endl;
608 if ( ( message == &mesh_buffer ) && ( !printer.RegisterFieldValuePrinter (
609 mesh_buffer.GetDescriptor()->FindFieldByName (
"IndexBuffer" ),
612 std::cout <<
"Failed to register index buffer printer." << std::endl;
615 std::string text_string;
616 std::ofstream text_file ( mOutputFile, std::ifstream::out );
617 if ( !printer.PrintToString ( *message, &text_string ) )
619 std::cerr <<
"Failed to serialize message to text format.";
620 throw std::runtime_error (
"Failed to serialize message to text format." );
622 text_file << magick_number << std::endl;
623 text_file.write ( text_string.c_str(), text_string.length() );
628 std::ofstream binary_file ( mOutputFile, std::ifstream::out | std::ifstream::binary );
629 magick_number[7] =
'\0';
630 binary_file << magick_number <<
'\0';
631 if ( !message->SerializeToOstream ( &binary_file ) )
633 std::cerr <<
"Failed to serialize message to binary format.";
634 throw std::runtime_error (
"Failed to serialize message to binary format." );