Anim_Import.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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. //BC don't bother checking.
  66. return true;
  67. #ifndef _WIN32
  68. return false;
  69. #elif 0
  70. HKEY hKey;
  71. long lres, lType;
  72. lres = RegOpenKey( HKEY_LOCAL_MACHINE, "SOFTWARE\\Alias|Wavefront\\Maya\\4.5\\Setup\\InstallPath", &hKey );
  73. if ( lres != ERROR_SUCCESS ) {
  74. return false;
  75. }
  76. lres = RegQueryValueEx( hKey, "MAYA_INSTALL_LOCATION", NULL, (unsigned long*)&lType, (unsigned char*)NULL, (unsigned long*)NULL );
  77. RegCloseKey( hKey );
  78. if ( lres != ERROR_SUCCESS ) {
  79. return false;
  80. }
  81. return true;
  82. #else
  83. HKEY hKey;
  84. long lres;
  85. // only check the non-version specific key so that we only have to update the maya dll when new versions are released
  86. lres = RegOpenKey( HKEY_LOCAL_MACHINE, "SOFTWARE\\Alias|Wavefront\\Maya", &hKey );
  87. RegCloseKey( hKey );
  88. if ( lres != ERROR_SUCCESS ) {
  89. return false;
  90. }
  91. return true;
  92. #endif
  93. }
  94. /*
  95. =====================
  96. idModelExport::LoadMayaDll
  97. Checks to see if we can load the Maya export dll
  98. =====================
  99. */
  100. void idModelExport::LoadMayaDll( void ) {
  101. exporterDLLEntry_t dllEntry;
  102. char dllPath[ MAX_OSPATH ];
  103. fileSystem->FindDLL( "MayaImport", dllPath, false );
  104. if ( !dllPath[ 0 ] ) {
  105. return;
  106. }
  107. importDLL = sys->DLL_Load( dllPath );
  108. if ( !importDLL ) {
  109. return;
  110. }
  111. // look up the dll interface functions
  112. dllEntry = ( exporterDLLEntry_t )sys->DLL_GetProcAddress( importDLL, "dllEntry" );
  113. Maya_ConvertModel = ( exporterInterface_t )sys->DLL_GetProcAddress( importDLL, "Maya_ConvertModel" );
  114. Maya_Shutdown = ( exporterShutdown_t )sys->DLL_GetProcAddress( importDLL, "Maya_Shutdown" );
  115. if ( !Maya_ConvertModel || !dllEntry || !Maya_Shutdown ) {
  116. Maya_ConvertModel = NULL;
  117. Maya_Shutdown = NULL;
  118. sys->DLL_Unload( importDLL );
  119. importDLL = 0;
  120. gameLocal.Error( "Invalid interface on export DLL." );
  121. return;
  122. }
  123. // initialize the DLL
  124. if ( !dllEntry( MD5_VERSION, common, sys ) ) {
  125. // init failed
  126. Maya_ConvertModel = NULL;
  127. Maya_Shutdown = NULL;
  128. sys->DLL_Unload( importDLL );
  129. importDLL = 0;
  130. gameLocal.Error( "Export DLL init failed." );
  131. return;
  132. }
  133. }
  134. /*
  135. =====================
  136. idModelExport::ConvertMayaToMD5
  137. Checks if a Maya model should be converted to an MD5, and converts if if the time/date or
  138. version number has changed.
  139. =====================
  140. */
  141. bool idModelExport::ConvertMayaToMD5( void ) {
  142. ID_TIME_T sourceTime;
  143. ID_TIME_T destTime;
  144. int version;
  145. idToken cmdLine;
  146. idStr path;
  147. // check if our DLL got loaded
  148. if ( initialized && !Maya_ConvertModel ) {
  149. Maya_Error = "MayaImport dll not loaded. Place mayaimportx86.dll in Doom3's root folder.";
  150. return false;
  151. }
  152. // if idAnimManager::forceExport is set then we always reexport Maya models
  153. if ( idAnimManager::forceExport ) {
  154. force = true;
  155. }
  156. // get the source file's time
  157. if ( fileSystem->ReadFile( src, NULL, &sourceTime ) < 0 ) {
  158. // source file doesn't exist
  159. return true;
  160. }
  161. // get the destination file's time
  162. if ( !force && ( fileSystem->ReadFile( dest, NULL, &destTime ) >= 0 ) ) {
  163. idParser parser( LEXFL_ALLOWPATHNAMES | LEXFL_NOSTRINGESCAPECHARS );
  164. parser.LoadFile( dest );
  165. // read the file version
  166. if ( parser.CheckTokenString( MD5_VERSION_STRING ) ) {
  167. version = parser.ParseInt();
  168. // check the command line
  169. if ( parser.CheckTokenString( "commandline" ) ) {
  170. parser.ReadToken( &cmdLine );
  171. // check the file time, scale, and version
  172. if ( ( destTime >= sourceTime ) && ( version == MD5_VERSION ) && ( cmdLine == commandLine ) ) {
  173. // don't convert it
  174. gameLocal.Printf( "MD5 is up-to-date. Stopping export.\n" );
  175. return true;
  176. }
  177. }
  178. }
  179. }
  180. // if this is the first time we've been run, check if Maya is installed and load our DLL
  181. if ( !initialized ) {
  182. initialized = true;
  183. if ( !CheckMayaInstall() ) {
  184. Maya_Error = "Maya not installed in registry.";
  185. return false;
  186. }
  187. LoadMayaDll();
  188. // check if our DLL got loaded
  189. if ( !Maya_ConvertModel ) {
  190. Maya_Error = "Could not load MayaImport dll. Place mayaimportx86.dll in Doom3's root folder.";
  191. return false;
  192. }
  193. }
  194. // we need to make sure we have a full path, so convert the filename to an OS path
  195. // _D3XP :: we work out of the cdpath, at least until we get Alienbrain
  196. src = fileSystem->RelativePathToOSPath( src, "fs_cdpath" );
  197. dest = fileSystem->RelativePathToOSPath( dest, "fs_cdpath" );
  198. dest.ExtractFilePath( path );
  199. if ( path.Length() ) {
  200. fileSystem->CreateOSPath( path );
  201. }
  202. // get the os path in case it needs to create one
  203. path = fileSystem->RelativePathToOSPath( "", "fs_cdpath" /* _D3XP */ );
  204. common->SetRefreshOnPrint( true );
  205. Maya_Error = Maya_ConvertModel( path, commandLine );
  206. common->SetRefreshOnPrint( false );
  207. if ( Maya_Error != "Ok" ) {
  208. return false;
  209. }
  210. // conversion succeded
  211. return true;
  212. }
  213. /*
  214. ====================
  215. idModelExport::Reset
  216. ====================
  217. */
  218. void idModelExport::Reset( void ) {
  219. force = false;
  220. commandLine = "";
  221. src = "";
  222. dest = "";
  223. }
  224. /*
  225. ====================
  226. idModelExport::ExportModel
  227. ====================
  228. */
  229. bool idModelExport::ExportModel( const char *model ) {
  230. const char *game = cvarSystem->GetCVarString( "fs_game" );
  231. if ( strlen(game) == 0 ) {
  232. game = BASE_GAMEDIR;
  233. }
  234. Reset();
  235. src = model;
  236. dest = model;
  237. dest.SetFileExtension( MD5_MESH_EXT );
  238. sprintf( commandLine, "mesh %s -dest %s -game %s", src.c_str(), dest.c_str(), game );
  239. if ( !ConvertMayaToMD5() ) {
  240. gameLocal.Printf( "Failed to export '%s' : %s", src.c_str(), Maya_Error.c_str() );
  241. return false;
  242. }
  243. return true;
  244. }
  245. /*
  246. ====================
  247. idModelExport::ExportAnim
  248. ====================
  249. */
  250. bool idModelExport::ExportAnim( const char *anim ) {
  251. const char *game = cvarSystem->GetCVarString( "fs_game" );
  252. if ( strlen(game) == 0 ) {
  253. game = BASE_GAMEDIR;
  254. }
  255. Reset();
  256. src = anim;
  257. dest = anim;
  258. dest.SetFileExtension( MD5_ANIM_EXT );
  259. sprintf( commandLine, "anim %s -dest %s -game %s", src.c_str(), dest.c_str(), game );
  260. if ( !ConvertMayaToMD5() ) {
  261. gameLocal.Printf( "Failed to export '%s' : %s", src.c_str(), Maya_Error.c_str() );
  262. return false;
  263. }
  264. return true;
  265. }
  266. /*
  267. ====================
  268. idModelExport::ParseOptions
  269. ====================
  270. */
  271. bool idModelExport::ParseOptions( idLexer &lex ) {
  272. idToken token;
  273. idStr destdir;
  274. idStr sourcedir;
  275. if ( !lex.ReadToken( &token ) ) {
  276. lex.Error( "Expected filename" );
  277. return false;
  278. }
  279. src = token;
  280. dest = token;
  281. while( lex.ReadToken( &token ) ) {
  282. if ( token == "-" ) {
  283. if ( !lex.ReadToken( &token ) ) {
  284. lex.Error( "Expecting option" );
  285. return false;
  286. }
  287. if ( token == "sourcedir" ) {
  288. if ( !lex.ReadToken( &token ) ) {
  289. lex.Error( "Missing pathname after -sourcedir" );
  290. return false;
  291. }
  292. sourcedir = token;
  293. } else if ( token == "destdir" ) {
  294. if ( !lex.ReadToken( &token ) ) {
  295. lex.Error( "Missing pathname after -destdir" );
  296. return false;
  297. }
  298. destdir = token;
  299. } else if ( token == "dest" ) {
  300. if ( !lex.ReadToken( &token ) ) {
  301. lex.Error( "Missing filename after -dest" );
  302. return false;
  303. }
  304. dest = token;
  305. } else {
  306. commandLine += va( " -%s", token.c_str() );
  307. }
  308. } else {
  309. commandLine += va( " %s", token.c_str() );
  310. }
  311. }
  312. if ( sourcedir.Length() ) {
  313. src.StripPath();
  314. sourcedir.BackSlashesToSlashes();
  315. sprintf( src, "%s/%s", sourcedir.c_str(), src.c_str() );
  316. }
  317. if ( destdir.Length() ) {
  318. dest.StripPath();
  319. destdir.BackSlashesToSlashes();
  320. sprintf( dest, "%s/%s", destdir.c_str(), dest.c_str() );
  321. }
  322. return true;
  323. }
  324. /*
  325. ====================
  326. idModelExport::ParseExportSection
  327. ====================
  328. */
  329. int idModelExport::ParseExportSection( idParser &parser ) {
  330. idToken command;
  331. idToken token;
  332. idStr defaultCommands;
  333. idLexer lex;
  334. idStr temp;
  335. idStr parms;
  336. int count;
  337. const char *game = cvarSystem->GetCVarString( "fs_game" );
  338. if ( strlen(game) == 0 ) {
  339. game = BASE_GAMEDIR;
  340. }
  341. // only export sections that match our export mask
  342. if ( g_exportMask.GetString()[ 0 ] ) {
  343. if ( parser.CheckTokenString( "{" ) ) {
  344. parser.SkipBracedSection( false );
  345. return 0;
  346. }
  347. parser.ReadToken( &token );
  348. if ( token.Icmp( g_exportMask.GetString() ) ) {
  349. parser.SkipBracedSection();
  350. return 0;
  351. }
  352. parser.ExpectTokenString( "{" );
  353. } else if ( !parser.CheckTokenString( "{" ) ) {
  354. // skip the export mask
  355. parser.ReadToken( &token );
  356. parser.ExpectTokenString( "{" );
  357. }
  358. count = 0;
  359. lex.SetFlags( LEXFL_NOSTRINGCONCAT | LEXFL_ALLOWPATHNAMES | LEXFL_ALLOWMULTICHARLITERALS | LEXFL_ALLOWBACKSLASHSTRINGCONCAT );
  360. while( 1 ) {
  361. if ( !parser.ReadToken( &command ) ) {
  362. parser.Error( "Unexpoected end-of-file" );
  363. break;
  364. }
  365. if ( command == "}" ) {
  366. break;
  367. }
  368. if ( command == "options" ) {
  369. parser.ParseRestOfLine( defaultCommands );
  370. } else if ( command == "addoptions" ) {
  371. parser.ParseRestOfLine( temp );
  372. defaultCommands += " ";
  373. defaultCommands += temp;
  374. } else if ( ( command == "mesh" ) || ( command == "anim" ) || ( command == "camera" ) ) {
  375. if ( !parser.ReadToken( &token ) ) {
  376. parser.Error( "Expected filename" );
  377. }
  378. temp = token;
  379. parser.ParseRestOfLine( parms );
  380. if ( defaultCommands.Length() ) {
  381. sprintf( temp, "%s %s", temp.c_str(), defaultCommands.c_str() );
  382. }
  383. if ( parms.Length() ) {
  384. sprintf( temp, "%s %s", temp.c_str(), parms.c_str() );
  385. }
  386. lex.LoadMemory( temp, temp.Length(), parser.GetFileName() );
  387. Reset();
  388. if ( ParseOptions( lex ) ) {
  389. const char *game = cvarSystem->GetCVarString( "fs_game" );
  390. if ( strlen(game) == 0 ) {
  391. game = BASE_GAMEDIR;
  392. }
  393. if ( command == "mesh" ) {
  394. dest.SetFileExtension( MD5_MESH_EXT );
  395. } else if ( command == "anim" ) {
  396. dest.SetFileExtension( MD5_ANIM_EXT );
  397. } else if ( command == "camera" ) {
  398. dest.SetFileExtension( MD5_CAMERA_EXT );
  399. } else {
  400. dest.SetFileExtension( command );
  401. }
  402. idStr back = commandLine;
  403. sprintf( commandLine, "%s %s -dest %s -game %s%s", command.c_str(), src.c_str(), dest.c_str(), game, commandLine.c_str() );
  404. if ( ConvertMayaToMD5() ) {
  405. count++;
  406. } else {
  407. parser.Warning( "Failed to export '%s' : %s", src.c_str(), Maya_Error.c_str() );
  408. }
  409. }
  410. lex.FreeSource();
  411. } else {
  412. parser.Error( "Unknown token: %s", command.c_str() );
  413. parser.SkipBracedSection( false );
  414. break;
  415. }
  416. }
  417. return count;
  418. }
  419. /*
  420. ================
  421. idModelExport::ExportDefFile
  422. ================
  423. */
  424. int idModelExport::ExportDefFile( const char *filename ) {
  425. idParser parser( LEXFL_NOSTRINGCONCAT | LEXFL_ALLOWPATHNAMES | LEXFL_ALLOWMULTICHARLITERALS | LEXFL_ALLOWBACKSLASHSTRINGCONCAT );
  426. idToken token;
  427. int count;
  428. count = 0;
  429. if ( !parser.LoadFile( filename ) ) {
  430. gameLocal.Printf( "Could not load '%s'\n", filename );
  431. return 0;
  432. }
  433. while( parser.ReadToken( &token ) ) {
  434. if ( token == "export" ) {
  435. count += ParseExportSection( parser );
  436. } else {
  437. parser.ReadToken( &token );
  438. parser.SkipBracedSection();
  439. }
  440. }
  441. return count;
  442. }
  443. /*
  444. ================
  445. idModelExport::ExportModels
  446. ================
  447. */
  448. int idModelExport::ExportModels( const char *pathname, const char *extension ) {
  449. int count;
  450. count = 0;
  451. idFileList *files;
  452. int i;
  453. if ( !CheckMayaInstall() ) {
  454. // if Maya isn't installed, don't bother checking if we have anims to export
  455. return 0;
  456. }
  457. gameLocal.Printf( "--------- Exporting models --------\n" );
  458. if ( !g_exportMask.GetString()[ 0 ] ) {
  459. gameLocal.Printf( " Export mask: '%s'\n", g_exportMask.GetString() );
  460. }
  461. count = 0;
  462. files = fileSystem->ListFiles( pathname, extension );
  463. for( i = 0; i < files->GetNumFiles(); i++ ) {
  464. count += ExportDefFile( va( "%s/%s", pathname, files->GetFile( i ) ) );
  465. }
  466. fileSystem->FreeFileList( files );
  467. gameLocal.Printf( "...%d models exported.\n", count );
  468. gameLocal.Printf( "-----------------------------------\n" );
  469. return count;
  470. }