Anim_Import.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #include "../../idlib/precompiled.h"
  21. #pragma hdrstop
  22. #include "../Game_local.h"
  23. #include "../../MayaImport/maya_main.h"
  24. /***********************************************************************
  25. Maya conversion functions
  26. ***********************************************************************/
  27. static idStr Maya_Error;
  28. static exporterInterface_t Maya_ConvertModel = NULL;
  29. static exporterShutdown_t Maya_Shutdown = NULL;
  30. static int importDLL = 0;
  31. bool idModelExport::initialized = false;
  32. /*
  33. ====================
  34. idModelExport::idModelExport
  35. ====================
  36. */
  37. idModelExport::idModelExport() {
  38. Reset();
  39. }
  40. /*
  41. ====================
  42. idModelExport::Shutdown
  43. ====================
  44. */
  45. void idModelExport::Shutdown( void ) {
  46. if ( Maya_Shutdown ) {
  47. Maya_Shutdown();
  48. }
  49. if ( importDLL ) {
  50. sys->DLL_Unload( importDLL );
  51. }
  52. importDLL = 0;
  53. Maya_Shutdown = NULL;
  54. Maya_ConvertModel = NULL;
  55. Maya_Error.Clear();
  56. initialized = false;
  57. }
  58. /*
  59. =====================
  60. idModelExport::CheckMayaInstall
  61. Determines if Maya is installed on the user's machine
  62. =====================
  63. */
  64. bool idModelExport::CheckMayaInstall( void ) {
  65. #ifndef _WIN32
  66. return false;
  67. #elif 0
  68. HKEY hKey;
  69. long lres, lType;
  70. lres = RegOpenKey( HKEY_LOCAL_MACHINE, "SOFTWARE\\Alias|Wavefront\\Maya\\4.5\\Setup\\InstallPath", &hKey );
  71. if ( lres != ERROR_SUCCESS ) {
  72. return false;
  73. }
  74. lres = RegQueryValueEx( hKey, "MAYA_INSTALL_LOCATION", NULL, (unsigned long*)&lType, (unsigned char*)NULL, (unsigned long*)NULL );
  75. RegCloseKey( hKey );
  76. if ( lres != ERROR_SUCCESS ) {
  77. return false;
  78. }
  79. return true;
  80. #else
  81. HKEY hKey;
  82. long lres;
  83. // only check the non-version specific key so that we only have to update the maya dll when new versions are released
  84. lres = RegOpenKey( HKEY_LOCAL_MACHINE, "SOFTWARE\\Alias|Wavefront\\Maya", &hKey );
  85. RegCloseKey( hKey );
  86. if ( lres != ERROR_SUCCESS ) {
  87. return false;
  88. }
  89. return true;
  90. #endif
  91. }
  92. /*
  93. =====================
  94. idModelExport::LoadMayaDll
  95. Checks to see if we can load the Maya export dll
  96. =====================
  97. */
  98. void idModelExport::LoadMayaDll( void ) {
  99. exporterDLLEntry_t dllEntry;
  100. char dllPath[ MAX_OSPATH ];
  101. fileSystem->FindDLL( "MayaImport", dllPath, false );
  102. if ( !dllPath[ 0 ] ) {
  103. return;
  104. }
  105. importDLL = sys->DLL_Load( dllPath );
  106. if ( !importDLL ) {
  107. return;
  108. }
  109. // look up the dll interface functions
  110. dllEntry = ( exporterDLLEntry_t )sys->DLL_GetProcAddress( importDLL, "dllEntry" );
  111. Maya_ConvertModel = ( exporterInterface_t )sys->DLL_GetProcAddress( importDLL, "Maya_ConvertModel" );
  112. Maya_Shutdown = ( exporterShutdown_t )sys->DLL_GetProcAddress( importDLL, "Maya_Shutdown" );
  113. if ( !Maya_ConvertModel || !dllEntry || !Maya_Shutdown ) {
  114. Maya_ConvertModel = NULL;
  115. Maya_Shutdown = NULL;
  116. sys->DLL_Unload( importDLL );
  117. importDLL = 0;
  118. gameLocal.Error( "Invalid interface on export DLL." );
  119. return;
  120. }
  121. // initialize the DLL
  122. if ( !dllEntry( MD5_VERSION, common, sys ) ) {
  123. // init failed
  124. Maya_ConvertModel = NULL;
  125. Maya_Shutdown = NULL;
  126. sys->DLL_Unload( importDLL );
  127. importDLL = 0;
  128. gameLocal.Error( "Export DLL init failed." );
  129. return;
  130. }
  131. }
  132. /*
  133. =====================
  134. idModelExport::ConvertMayaToMD5
  135. Checks if a Maya model should be converted to an MD5, and converts if if the time/date or
  136. version number has changed.
  137. =====================
  138. */
  139. bool idModelExport::ConvertMayaToMD5( void ) {
  140. ID_TIME_T sourceTime;
  141. ID_TIME_T destTime;
  142. int version;
  143. idToken cmdLine;
  144. idStr path;
  145. // check if our DLL got loaded
  146. if ( initialized && !Maya_ConvertModel ) {
  147. Maya_Error = "MayaImport dll not loaded.";
  148. return false;
  149. }
  150. // if idAnimManager::forceExport is set then we always reexport Maya models
  151. if ( idAnimManager::forceExport ) {
  152. force = true;
  153. }
  154. // get the source file's time
  155. if ( fileSystem->ReadFile( src, NULL, &sourceTime ) < 0 ) {
  156. // source file doesn't exist
  157. return true;
  158. }
  159. // get the destination file's time
  160. if ( !force && ( fileSystem->ReadFile( dest, NULL, &destTime ) >= 0 ) ) {
  161. idParser parser( LEXFL_ALLOWPATHNAMES | LEXFL_NOSTRINGESCAPECHARS );
  162. parser.LoadFile( dest );
  163. // read the file version
  164. if ( parser.CheckTokenString( MD5_VERSION_STRING ) ) {
  165. version = parser.ParseInt();
  166. // check the command line
  167. if ( parser.CheckTokenString( "commandline" ) ) {
  168. parser.ReadToken( &cmdLine );
  169. // check the file time, scale, and version
  170. if ( ( destTime >= sourceTime ) && ( version == MD5_VERSION ) && ( cmdLine == commandLine ) ) {
  171. // don't convert it
  172. return true;
  173. }
  174. }
  175. }
  176. }
  177. // if this is the first time we've been run, check if Maya is installed and load our DLL
  178. if ( !initialized ) {
  179. initialized = true;
  180. if ( !CheckMayaInstall() ) {
  181. Maya_Error = "Maya not installed in registry.";
  182. return false;
  183. }
  184. LoadMayaDll();
  185. // check if our DLL got loaded
  186. if ( !Maya_ConvertModel ) {
  187. Maya_Error = "Could not load MayaImport dll.";
  188. return false;
  189. }
  190. }
  191. // we need to make sure we have a full path, so convert the filename to an OS path
  192. // _D3XP :: we work out of the cdpath, at least until we get Alienbrain
  193. src = fileSystem->RelativePathToOSPath( src, "fs_cdpath" );
  194. dest = fileSystem->RelativePathToOSPath( dest, "fs_cdpath" );
  195. dest.ExtractFilePath( path );
  196. if ( path.Length() ) {
  197. fileSystem->CreateOSPath( path );
  198. }
  199. // get the os path in case it needs to create one
  200. path = fileSystem->RelativePathToOSPath( "", "fs_cdpath" /* _D3XP */ );
  201. common->SetRefreshOnPrint( true );
  202. Maya_Error = Maya_ConvertModel( path, commandLine );
  203. common->SetRefreshOnPrint( false );
  204. if ( Maya_Error != "Ok" ) {
  205. return false;
  206. }
  207. // conversion succeded
  208. return true;
  209. }
  210. /*
  211. ====================
  212. idModelExport::Reset
  213. ====================
  214. */
  215. void idModelExport::Reset( void ) {
  216. force = false;
  217. commandLine = "";
  218. src = "";
  219. dest = "";
  220. }
  221. /*
  222. ====================
  223. idModelExport::ExportModel
  224. ====================
  225. */
  226. bool idModelExport::ExportModel( const char *model ) {
  227. const char *game = cvarSystem->GetCVarString( "fs_game" );
  228. if ( strlen(game) == 0 ) {
  229. game = BASE_GAMEDIR;
  230. }
  231. Reset();
  232. src = model;
  233. dest = model;
  234. dest.SetFileExtension( MD5_MESH_EXT );
  235. sprintf( commandLine, "mesh %s -dest %s -game %s", src.c_str(), dest.c_str(), game );
  236. if ( !ConvertMayaToMD5() ) {
  237. gameLocal.Printf( "Failed to export '%s' : %s", src.c_str(), Maya_Error.c_str() );
  238. return false;
  239. }
  240. return true;
  241. }
  242. /*
  243. ====================
  244. idModelExport::ExportAnim
  245. ====================
  246. */
  247. bool idModelExport::ExportAnim( const char *anim ) {
  248. const char *game = cvarSystem->GetCVarString( "fs_game" );
  249. if ( strlen(game) == 0 ) {
  250. game = BASE_GAMEDIR;
  251. }
  252. Reset();
  253. src = anim;
  254. dest = anim;
  255. dest.SetFileExtension( MD5_ANIM_EXT );
  256. sprintf( commandLine, "anim %s -dest %s -game %s", src.c_str(), dest.c_str(), game );
  257. if ( !ConvertMayaToMD5() ) {
  258. gameLocal.Printf( "Failed to export '%s' : %s", src.c_str(), Maya_Error.c_str() );
  259. return false;
  260. }
  261. return true;
  262. }
  263. /*
  264. ====================
  265. idModelExport::ParseOptions
  266. ====================
  267. */
  268. bool idModelExport::ParseOptions( idLexer &lex ) {
  269. idToken token;
  270. idStr destdir;
  271. idStr sourcedir;
  272. if ( !lex.ReadToken( &token ) ) {
  273. lex.Error( "Expected filename" );
  274. return false;
  275. }
  276. src = token;
  277. dest = token;
  278. while( lex.ReadToken( &token ) ) {
  279. if ( token == "-" ) {
  280. if ( !lex.ReadToken( &token ) ) {
  281. lex.Error( "Expecting option" );
  282. return false;
  283. }
  284. if ( token == "sourcedir" ) {
  285. if ( !lex.ReadToken( &token ) ) {
  286. lex.Error( "Missing pathname after -sourcedir" );
  287. return false;
  288. }
  289. sourcedir = token;
  290. } else if ( token == "destdir" ) {
  291. if ( !lex.ReadToken( &token ) ) {
  292. lex.Error( "Missing pathname after -destdir" );
  293. return false;
  294. }
  295. destdir = token;
  296. } else if ( token == "dest" ) {
  297. if ( !lex.ReadToken( &token ) ) {
  298. lex.Error( "Missing filename after -dest" );
  299. return false;
  300. }
  301. dest = token;
  302. } else {
  303. commandLine += va( " -%s", token.c_str() );
  304. }
  305. } else {
  306. commandLine += va( " %s", token.c_str() );
  307. }
  308. }
  309. if ( sourcedir.Length() ) {
  310. src.StripPath();
  311. sourcedir.BackSlashesToSlashes();
  312. sprintf( src, "%s/%s", sourcedir.c_str(), src.c_str() );
  313. }
  314. if ( destdir.Length() ) {
  315. dest.StripPath();
  316. destdir.BackSlashesToSlashes();
  317. sprintf( dest, "%s/%s", destdir.c_str(), dest.c_str() );
  318. }
  319. return true;
  320. }
  321. /*
  322. ====================
  323. idModelExport::ParseExportSection
  324. ====================
  325. */
  326. int idModelExport::ParseExportSection( idParser &parser ) {
  327. idToken command;
  328. idToken token;
  329. idStr defaultCommands;
  330. idLexer lex;
  331. idStr temp;
  332. idStr parms;
  333. int count;
  334. const char *game = cvarSystem->GetCVarString( "fs_game" );
  335. if ( strlen(game) == 0 ) {
  336. game = BASE_GAMEDIR;
  337. }
  338. // only export sections that match our export mask
  339. if ( g_exportMask.GetString()[ 0 ] ) {
  340. if ( parser.CheckTokenString( "{" ) ) {
  341. parser.SkipBracedSection( false );
  342. return 0;
  343. }
  344. parser.ReadToken( &token );
  345. if ( token.Icmp( g_exportMask.GetString() ) ) {
  346. parser.SkipBracedSection();
  347. return 0;
  348. }
  349. parser.ExpectTokenString( "{" );
  350. } else if ( !parser.CheckTokenString( "{" ) ) {
  351. // skip the export mask
  352. parser.ReadToken( &token );
  353. parser.ExpectTokenString( "{" );
  354. }
  355. count = 0;
  356. lex.SetFlags( LEXFL_NOSTRINGCONCAT | LEXFL_ALLOWPATHNAMES | LEXFL_ALLOWMULTICHARLITERALS | LEXFL_ALLOWBACKSLASHSTRINGCONCAT );
  357. while( 1 ) {
  358. if ( !parser.ReadToken( &command ) ) {
  359. parser.Error( "Unexpoected end-of-file" );
  360. break;
  361. }
  362. if ( command == "}" ) {
  363. break;
  364. }
  365. if ( command == "options" ) {
  366. parser.ParseRestOfLine( defaultCommands );
  367. } else if ( command == "addoptions" ) {
  368. parser.ParseRestOfLine( temp );
  369. defaultCommands += " ";
  370. defaultCommands += temp;
  371. } else if ( ( command == "mesh" ) || ( command == "anim" ) || ( command == "camera" ) ) {
  372. if ( !parser.ReadToken( &token ) ) {
  373. parser.Error( "Expected filename" );
  374. }
  375. temp = token;
  376. parser.ParseRestOfLine( parms );
  377. if ( defaultCommands.Length() ) {
  378. sprintf( temp, "%s %s", temp.c_str(), defaultCommands.c_str() );
  379. }
  380. if ( parms.Length() ) {
  381. sprintf( temp, "%s %s", temp.c_str(), parms.c_str() );
  382. }
  383. lex.LoadMemory( temp, temp.Length(), parser.GetFileName() );
  384. Reset();
  385. if ( ParseOptions( lex ) ) {
  386. const char *game = cvarSystem->GetCVarString( "fs_game" );
  387. if ( strlen(game) == 0 ) {
  388. game = BASE_GAMEDIR;
  389. }
  390. if ( command == "mesh" ) {
  391. dest.SetFileExtension( MD5_MESH_EXT );
  392. } else if ( command == "anim" ) {
  393. dest.SetFileExtension( MD5_ANIM_EXT );
  394. } else if ( command == "camera" ) {
  395. dest.SetFileExtension( MD5_CAMERA_EXT );
  396. } else {
  397. dest.SetFileExtension( command );
  398. }
  399. idStr back = commandLine;
  400. sprintf( commandLine, "%s %s -dest %s -game %s%s", command.c_str(), src.c_str(), dest.c_str(), game, commandLine.c_str() );
  401. if ( ConvertMayaToMD5() ) {
  402. count++;
  403. } else {
  404. parser.Warning( "Failed to export '%s' : %s", src.c_str(), Maya_Error.c_str() );
  405. }
  406. }
  407. lex.FreeSource();
  408. } else {
  409. parser.Error( "Unknown token: %s", command.c_str() );
  410. parser.SkipBracedSection( false );
  411. break;
  412. }
  413. }
  414. return count;
  415. }
  416. /*
  417. ================
  418. idModelExport::ExportDefFile
  419. ================
  420. */
  421. int idModelExport::ExportDefFile( const char *filename ) {
  422. idParser parser( LEXFL_NOSTRINGCONCAT | LEXFL_ALLOWPATHNAMES | LEXFL_ALLOWMULTICHARLITERALS | LEXFL_ALLOWBACKSLASHSTRINGCONCAT );
  423. idToken token;
  424. int count;
  425. count = 0;
  426. if ( !parser.LoadFile( filename ) ) {
  427. gameLocal.Printf( "Could not load '%s'\n", filename );
  428. return 0;
  429. }
  430. while( parser.ReadToken( &token ) ) {
  431. if ( token == "export" ) {
  432. count += ParseExportSection( parser );
  433. } else {
  434. parser.ReadToken( &token );
  435. parser.SkipBracedSection();
  436. }
  437. }
  438. return count;
  439. }
  440. /*
  441. ================
  442. idModelExport::ExportModels
  443. ================
  444. */
  445. int idModelExport::ExportModels( const char *pathname, const char *extension ) {
  446. int count;
  447. count = 0;
  448. idFileList *files;
  449. int i;
  450. if ( !CheckMayaInstall() ) {
  451. // if Maya isn't installed, don't bother checking if we have anims to export
  452. return 0;
  453. }
  454. gameLocal.Printf( "--------- Exporting models --------\n" );
  455. if ( !g_exportMask.GetString()[ 0 ] ) {
  456. gameLocal.Printf( " Export mask: '%s'\n", g_exportMask.GetString() );
  457. }
  458. count = 0;
  459. files = fileSystem->ListFiles( pathname, extension );
  460. for( i = 0; i < files->GetNumFiles(); i++ ) {
  461. count += ExportDefFile( va( "%s/%s", pathname, files->GetFile( i ) ) );
  462. }
  463. fileSystem->FreeFileList( files );
  464. gameLocal.Printf( "...%d models exported.\n", count );
  465. gameLocal.Printf( "-----------------------------------\n" );
  466. return count;
  467. }