main.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include <stdio.h>
  23. #include "platform/platform.h"
  24. #include "platform/platformVolume.h"
  25. #include "app/mainLoop.h"
  26. #include "T3D/gameFunctions.h"
  27. #include "core/stream/fileStream.h"
  28. #include "core/resourceManager.h"
  29. #include "ts/tsShape.h"
  30. #include "ts/tsShapeConstruct.h"
  31. #ifdef TORQUE_OS_WIN
  32. #include "platformWin32/platformWin32.h"
  33. #include "platformWin32/winConsole.h"
  34. #endif
  35. extern TSShape* loadColladaShape( const Torque::Path &path );
  36. /** Print the usage string */
  37. void printUsage()
  38. {
  39. Con::printf(
  40. "DAE-2-DTS Converter v%s (c) GarageGames, LLC.\n\n"
  41. "dae2dts [options] daeFilename\n\n"
  42. "--config cfgFilename Set the conversion configuration filename.\n"
  43. "--output dtsFilename Set the output DTS filename.\n"
  44. "--dsq If set, all sequences in the shape will be saved\n"
  45. " to DSQ files instead of being embedded in the DTS\n"
  46. " file.\n"
  47. "--dsq-only Same as --dsq, but no DTS file will be saved (handy for\n"
  48. " animation only input files).\n"
  49. "--compat If set, write to DTS v24 for compatibility with\n"
  50. " ShowToolPro (no support for vertex colors, 2nd UV\n"
  51. " set, autobillboards etc)\n"
  52. "--diffuse If set, the diffuse texture will be used as the\n"
  53. " material name (instead of the COLLADA <material> name)\n"
  54. "--materials If set, generate a materials.cs script in the output \n"
  55. " folder to define Materials used in the shape.\n"
  56. "--verbose If set, output progress information\n\n"
  57. "Exits with zero on success, non-zero on failure\n\n",
  58. TORQUE_APP_VERSION_STRING );
  59. }
  60. Torque::Path makeFullPath( const char* path )
  61. {
  62. char tempBuf[1024];
  63. Platform::makeFullPathName( path, tempBuf, sizeof(tempBuf), Platform::getCurrentDirectory() );
  64. return Torque::Path( String( tempBuf ) );
  65. }
  66. S32 TorqueMain( S32 argc, const char **argv )
  67. {
  68. S32 failed = 0;
  69. // Initialize the subsystems.
  70. StandardMainLoop::init();
  71. Con::setVariable( "Con::Prompt", "" );
  72. WindowsConsole->enable( true );
  73. // install all drives for now until we have everything using the volume stuff
  74. Platform::FS::InstallFileSystems();
  75. Platform::FS::MountDefaults();
  76. bool compatMode = false;
  77. bool diffuseNames = false;
  78. bool verbose = false;
  79. bool saveDTS = true;
  80. bool saveDSQ = false;
  81. bool genMaterials = false;
  82. Torque::Path cfgPath, srcPath, destPath;
  83. // Parse arguments
  84. S32 i;
  85. for ( i = 1; i < argc-1; i++ )
  86. {
  87. if ( dStrEqual( argv[i], "--config" ) )
  88. cfgPath = makeFullPath( argv[++i] );
  89. else if ( dStrEqual( argv[i], "--output" ) )
  90. destPath = makeFullPath( argv[++i] );
  91. else if ( dStrEqual( argv[i], "--dsq" ) )
  92. saveDSQ = true;
  93. else if ( dStrEqual( argv[i], "--dsq-only" ) )
  94. {
  95. saveDTS = false;
  96. saveDSQ = true;
  97. }
  98. else if ( dStrEqual( argv[i], "--compat" ) )
  99. compatMode = true;
  100. else if ( dStrEqual( argv[i], "--diffuse" ) )
  101. diffuseNames = true;
  102. else if ( dStrEqual( argv[i], "--materials" ) )
  103. genMaterials = true;
  104. else if ( dStrEqual( argv[i], "--verbose" ) )
  105. verbose = true;
  106. }
  107. if ( ( i >= argc ) || ( !dStrEndsWith(argv[i], ".dae") && !dStrEndsWith(argv[i], ".kmz" ) ) )
  108. {
  109. Con::errorf( "Error: no DAE file specified.\n" );
  110. printUsage();
  111. return -1;
  112. }
  113. srcPath = makeFullPath( argv[i] );
  114. if ( destPath.isEmpty() )
  115. {
  116. destPath = srcPath;
  117. destPath.setExtension( "dts" );
  118. }
  119. if ( !cfgPath.isEmpty() )
  120. Con::printf( "Configuration files not yet supported.\n" );
  121. // Define script callbacks
  122. if ( verbose )
  123. Con::evaluate( "function UpdateTSShapeLoadProgress(%progress, %msg) { echo(%msg); }" );
  124. else
  125. Con::evaluate( "function UpdateTSShapeLoadProgress(%progress, %msg) { }" );
  126. if ( verbose )
  127. Con::printf( "Parsing configuration file...\n" );
  128. // Set import options
  129. ColladaUtils::getOptions().reset();
  130. ColladaUtils::getOptions().forceUpdateMaterials = genMaterials;
  131. ColladaUtils::getOptions().useDiffuseNames = diffuseNames;
  132. if ( verbose )
  133. Con::printf( "Reading dae file...\n" );
  134. // Attempt to load the DAE file
  135. Resource<TSShape> shape = ResourceManager::get().load( srcPath );
  136. if ( !shape )
  137. {
  138. Con::errorf( "Failed to convert DAE file: %s\n", srcPath.getFullPath() );
  139. failed = 1;
  140. }
  141. else
  142. {
  143. if ( compatMode && !shape->canWriteOldFormat() )
  144. {
  145. Con::errorf( "Warning: Attempting to save to DTS v24 but the shape "
  146. "contains v26 features. Resulting DTS file may not be valid." );
  147. }
  148. FileStream outStream;
  149. if ( saveDSQ )
  150. {
  151. Torque::Path dsqPath( destPath );
  152. dsqPath.setExtension( "dsq" );
  153. for ( S32 i = 0; i < shape->sequences.size(); i++ )
  154. {
  155. const String& seqName = shape->getName( shape->sequences[i].nameIndex );
  156. if ( verbose )
  157. Con::printf( "Writing DSQ file for sequence '%s'...\n", seqName.c_str() );
  158. dsqPath.setFileName( destPath.getFileName() + "_" + seqName );
  159. if ( outStream.open( dsqPath, Torque::FS::File::Write ) )
  160. {
  161. shape->exportSequence( &outStream, shape->sequences[i], compatMode );
  162. outStream.close();
  163. }
  164. else
  165. {
  166. Con::errorf( "Failed to save sequence to %s\n", dsqPath.getFullPath().c_str() );
  167. failed = 1;
  168. }
  169. }
  170. }
  171. if ( saveDTS )
  172. {
  173. if ( verbose )
  174. Con::printf( "Writing DTS file...\n" );
  175. if ( outStream.open( destPath, Torque::FS::File::Write ) )
  176. {
  177. if ( saveDSQ )
  178. shape->sequences.setSize(0);
  179. shape->write( &outStream, compatMode );
  180. outStream.close();
  181. }
  182. else
  183. {
  184. Con::errorf( "Failed to save shape to %s\n", destPath.getFullPath().c_str() );
  185. failed = 1;
  186. }
  187. }
  188. }
  189. // Clean everything up.
  190. StandardMainLoop::shutdown();
  191. // Do we need to restart?
  192. if( StandardMainLoop::requiresRestart() )
  193. Platform::restartInstance();
  194. return failed;
  195. }