Example #1
0
S32 TorqueMain( S32 argc, const char **argv )
{
   S32 failed = 0;

   // Initialize the subsystems.
   StandardMainLoop::init();
   Con::setVariable( "Con::Prompt", "" );
   WindowsConsole->enable( true );

   // install all drives for now until we have everything using the volume stuff
   Platform::FS::InstallFileSystems();
   Platform::FS::MountDefaults();

   bool compatMode = false;
   bool diffuseNames = false;
   bool verbose = false;
   bool saveDTS = true;
   bool saveDSQ = false;
   bool genMaterials = false;
   Torque::Path cfgPath, srcPath, destPath;

   // Parse arguments
   S32 i;
   for ( i = 1; i < argc-1; i++ )
   {
      if ( dStrEqual( argv[i], "--config" ) )
         cfgPath = makeFullPath( argv[++i] );
      else if ( dStrEqual( argv[i], "--output" ) )
         destPath = makeFullPath( argv[++i] );
      else if ( dStrEqual( argv[i], "--dsq" ) )
         saveDSQ = true;
      else if ( dStrEqual( argv[i], "--dsq-only" ) )
      {
         saveDTS = false;
         saveDSQ = true;
      }
      else if ( dStrEqual( argv[i], "--compat" ) )
         compatMode = true;
      else if ( dStrEqual( argv[i], "--diffuse" ) )
         diffuseNames = true;
      else if ( dStrEqual( argv[i], "--materials" ) )
         genMaterials = true;
      else if ( dStrEqual( argv[i], "--verbose" ) )
         verbose = true;
   }

   if ( ( i >= argc ) || ( !dStrEndsWith(argv[i], ".dae") && !dStrEndsWith(argv[i], ".kmz" ) ) )
   {
      Con::errorf( "Error: no DAE file specified.\n" );
      printUsage();
      return -1;
   }

   srcPath = makeFullPath( argv[i] );
   if ( destPath.isEmpty() )
   {
      destPath = srcPath;
      destPath.setExtension( "dts" );
   }

   if ( !cfgPath.isEmpty() )
      Con::printf( "Configuration files not yet supported.\n" );

   // Define script callbacks
   if ( verbose )
      Con::evaluate( "function UpdateTSShapeLoadProgress(%progress, %msg) { echo(%msg); }" );
   else
      Con::evaluate( "function UpdateTSShapeLoadProgress(%progress, %msg) { }" );

   if ( verbose )
      Con::printf( "Parsing configuration file...\n" );

   // Set import options
   ColladaUtils::getOptions().reset();
   ColladaUtils::getOptions().forceUpdateMaterials = genMaterials;
   ColladaUtils::getOptions().useDiffuseNames = diffuseNames;

   if ( verbose )
      Con::printf( "Reading dae file...\n" );

   // Attempt to load the DAE file
   Resource<TSShape> shape = ResourceManager::get().load( srcPath );
   if ( !shape )
   {
      Con::errorf( "Failed to convert DAE file: %s\n", srcPath.getFullPath() );
      failed = 1;
   }
   else
   {
      if ( compatMode && !shape->canWriteOldFormat() )
      {
         Con::errorf( "Warning: Attempting to save to DTS v24 but the shape "
                      "contains v26 features. Resulting DTS file may not be valid." );
      }

      FileStream outStream;

      if ( saveDSQ )
      {
         Torque::Path dsqPath( destPath );
         dsqPath.setExtension( "dsq" );

         for ( S32 i = 0; i < shape->sequences.size(); i++ )
         {
            const String& seqName = shape->getName( shape->sequences[i].nameIndex );
            if ( verbose )
               Con::printf( "Writing DSQ file for sequence '%s'...\n", seqName.c_str() );

            dsqPath.setFileName( destPath.getFileName() + "_" + seqName );

            if ( outStream.open( dsqPath, Torque::FS::File::Write ) )
            {
               shape->exportSequence( &outStream, shape->sequences[i], compatMode );
               outStream.close();
            }
            else
            {
               Con::errorf( "Failed to save sequence to %s\n", dsqPath.getFullPath().c_str() );
               failed = 1;
            }
         }
      }
      if ( saveDTS )
      {
         if ( verbose )
            Con::printf( "Writing DTS file...\n" );

         if ( outStream.open( destPath, Torque::FS::File::Write ) )
         {
            if ( saveDSQ )
               shape->sequences.setSize(0);

            shape->write( &outStream, compatMode );
            outStream.close();
         }
         else
         {
            Con::errorf( "Failed to save shape to %s\n", destPath.getFullPath().c_str() );
            failed = 1;
         }
      }
   }

   // Clean everything up.
   StandardMainLoop::shutdown();

   // Do we need to restart?
   if( StandardMainLoop::requiresRestart() )
      Platform::restartInstance();

   return failed;
}