EncoderArguments.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. #include "Base.h"
  2. #include "EncoderArguments.h"
  3. #include "StringUtil.h"
  4. #ifdef WIN32
  5. #define PATH_MAX _MAX_PATH
  6. #define realpath(A,B) _fullpath(B,A,PATH_MAX)
  7. #endif
  8. namespace gameplay
  9. {
  10. static EncoderArguments* __instance;
  11. extern int __logVerbosity = 1;
  12. EncoderArguments::EncoderArguments(size_t argc, const char** argv) :
  13. _fontSize(0),
  14. _parseError(false),
  15. _fontPreview(false),
  16. _textOutput(false),
  17. _daeOutput(false),
  18. _optimizeAnimations(false)
  19. {
  20. __instance = this;
  21. if (argc > 1)
  22. {
  23. // read the options
  24. std::vector<std::string> arguments;
  25. for (size_t i = 1; i < argc; ++i)
  26. {
  27. arguments.push_back(argv[i]);
  28. }
  29. size_t index = 0;
  30. for (size_t i = 0; i < arguments.size(); ++i)
  31. {
  32. if (arguments[i][0] == '-')
  33. {
  34. readOption(arguments, &i);
  35. index = i + 1;
  36. }
  37. }
  38. if (arguments.size() - index == 2)
  39. {
  40. setInputfilePath(arguments[index]);
  41. setOutputfilePath(arguments[index + 1]);
  42. }
  43. else if (arguments.size() - index == 1)
  44. {
  45. setInputfilePath(arguments[index]);
  46. }
  47. }
  48. else
  49. {
  50. _parseError = true;
  51. }
  52. }
  53. EncoderArguments::~EncoderArguments(void)
  54. {
  55. }
  56. EncoderArguments* EncoderArguments::getInstance()
  57. {
  58. return __instance;
  59. }
  60. const std::string& EncoderArguments::getFilePath() const
  61. {
  62. return _filePath;
  63. }
  64. const char* EncoderArguments::getFilePathPointer() const
  65. {
  66. return _filePath.c_str();
  67. }
  68. std::string EncoderArguments::getOutputDirPath() const
  69. {
  70. if (_fileOutputPath.size() > 0)
  71. {
  72. int pos = _fileOutputPath.find_last_of('/');
  73. return (pos == -1 ? _fileOutputPath : _fileOutputPath.substr(0, pos));
  74. }
  75. else
  76. {
  77. int pos = _filePath.find_last_of('/');
  78. return (pos == -1 ? _filePath : _filePath.substr(0, pos));
  79. }
  80. }
  81. std::string EncoderArguments::getOutputFilePath() const
  82. {
  83. if (_fileOutputPath.size() > 0)
  84. {
  85. return _fileOutputPath;
  86. }
  87. else
  88. {
  89. int pos = _filePath.find_last_of('.');
  90. if (pos > 0)
  91. {
  92. std::string outputFilePath(_filePath.substr(0, pos));
  93. outputFilePath.append(".gpb");
  94. return outputFilePath;
  95. }
  96. else
  97. {
  98. std::string outputFilePath(_filePath);
  99. outputFilePath.append(".gpb");
  100. return outputFilePath;
  101. }
  102. }
  103. }
  104. const std::string& EncoderArguments::getDAEOutputPath() const
  105. {
  106. return _daeOutputPath;
  107. }
  108. const std::vector<std::string>& EncoderArguments::getGroupAnimationNodeId() const
  109. {
  110. return _groupAnimationNodeId;
  111. }
  112. const std::vector<std::string>& EncoderArguments::getGroupAnimationAnimationId() const
  113. {
  114. return _groupAnimationAnimationId;
  115. }
  116. bool EncoderArguments::containsGroupNodeId(const std::string& nodeId) const
  117. {
  118. return find(_groupAnimationNodeId.begin(), _groupAnimationNodeId.end(), nodeId) != _groupAnimationNodeId.end();
  119. }
  120. const std::string EncoderArguments::getAnimationId(const std::string& nodeId) const
  121. {
  122. for (size_t i = 0, size = _groupAnimationNodeId.size(); i < size; ++i)
  123. {
  124. if (_groupAnimationNodeId[i].compare(nodeId) == 0)
  125. {
  126. return _groupAnimationAnimationId[i];
  127. }
  128. }
  129. return "";
  130. }
  131. const std::vector<EncoderArguments::HeightmapOption>& EncoderArguments::getHeightmapOptions() const
  132. {
  133. return _heightmaps;
  134. }
  135. bool EncoderArguments::parseErrorOccured() const
  136. {
  137. return _parseError;
  138. }
  139. bool EncoderArguments::fileExists() const
  140. {
  141. if (_filePath.length() > 0)
  142. {
  143. struct stat buf;
  144. if (stat(_filePath.c_str(), &buf) != -1)
  145. {
  146. return true;
  147. }
  148. }
  149. return false;
  150. }
  151. void EncoderArguments::printUsage() const
  152. {
  153. LOG(1, "Usage: gameplay-encoder [options] <input filepath> <output filepath>\n\n");
  154. LOG(1, "Supported file extensions:\n");
  155. LOG(1, " .dae\t(COLLADA)\n");
  156. LOG(1, " .fbx\t(FBX)\n");
  157. LOG(1, " .ttf\t(TrueType Font)\n");
  158. LOG(1, "\n");
  159. LOG(1, "General Options:\n");
  160. LOG(1, " -v <verbosity>\tVerbosity level (0-4).\n");
  161. LOG(1, "\n");
  162. LOG(1, "COLLADA and FBX file options:\n");
  163. LOG(1, " -i <id>\tFilter by node ID.\n");
  164. LOG(1, " -t\t\tWrite text/xml.\n");
  165. LOG(1, " -g <node id> <animation id>\n" \
  166. "\t\tGroup all animation channels targeting the nodes into a new animation.\n");
  167. LOG(1, " -oa\n" \
  168. "\t\tOptimizes animations by analyzing animation channel data and\n" \
  169. "\t\tremoving any channels that contain default/identity values\n" \
  170. "\t\tand removing any duplicate contiguous keyframes, which are common\n" \
  171. "\t\twhen exporting baked animation data.\n");
  172. LOG(1, " -h \"<node ids>\" <filename>\n" \
  173. "\t\tGenerates a single heightmap image using meshes from the specified\n" \
  174. "\t\tnodes. Node id list should be in quotes with a space between each id.\n" \
  175. "\t\tFilename is the name of the image (PNG) to be saved.\n" \
  176. "\t\tMultiple -h arguments can be supplied to generate more than one heightmap.\n" \
  177. "\t\tFor 24-bit packed height data use -hp instead of -h.\n");
  178. LOG(1, "\n");
  179. LOG(1, "TTF file options:\n");
  180. LOG(1, " -s <size>\tSize of the font.\n");
  181. LOG(1, " -p\t\tOutput font preview.\n");
  182. LOG(1, "\n");
  183. exit(8);
  184. }
  185. bool EncoderArguments::fontPreviewEnabled() const
  186. {
  187. return _fontPreview;
  188. }
  189. bool EncoderArguments::textOutputEnabled() const
  190. {
  191. return _textOutput;
  192. }
  193. bool EncoderArguments::DAEOutputEnabled() const
  194. {
  195. return _daeOutput;
  196. }
  197. bool EncoderArguments::optimizeAnimationsEnabled() const
  198. {
  199. return _optimizeAnimations;
  200. }
  201. const char* EncoderArguments::getNodeId() const
  202. {
  203. if (_nodeId.length() == 0)
  204. {
  205. return NULL;
  206. }
  207. return _nodeId.c_str();
  208. }
  209. unsigned int EncoderArguments::getFontSize() const
  210. {
  211. return _fontSize;
  212. }
  213. EncoderArguments::FileFormat EncoderArguments::getFileFormat() const
  214. {
  215. if (_filePath.length() < 5)
  216. {
  217. return FILEFORMAT_UNKNOWN;
  218. }
  219. // Extract the extension
  220. std::string ext = "";
  221. size_t pos = _filePath.find_last_of(".");
  222. if (pos != std::string::npos)
  223. {
  224. ext = _filePath.substr(pos + 1);
  225. }
  226. // Match every supported extension with its format constant
  227. if (ext.compare("dae") == 0 || ext.compare("DAE") == 0)
  228. {
  229. return FILEFORMAT_DAE;
  230. }
  231. if (ext.compare("fbx") == 0 || ext.compare("FBX") == 0)
  232. {
  233. return FILEFORMAT_FBX;
  234. }
  235. if (ext.compare("ttf") == 0 || ext.compare("TTF") == 0)
  236. {
  237. return FILEFORMAT_TTF;
  238. }
  239. if (ext.compare("gpb") == 0 || ext.compare("GPB") == 0)
  240. {
  241. return FILEFORMAT_GPB;
  242. }
  243. return FILEFORMAT_UNKNOWN;
  244. }
  245. void EncoderArguments::readOption(const std::vector<std::string>& options, size_t* index)
  246. {
  247. const std::string& str = options[*index];
  248. if (str.length() == 0 && str[0] != '-')
  249. {
  250. return;
  251. }
  252. switch (str[1])
  253. {
  254. case 'd':
  255. if (str.compare("-dae") == 0)
  256. {
  257. // read one string, make sure not to go out of bounds
  258. if ((*index + 1) >= options.size())
  259. {
  260. LOG(1, "Error: -dae requires 1 argument.\n");
  261. _parseError = true;
  262. return;
  263. }
  264. (*index)++;
  265. _daeOutputPath = options[*index];
  266. _daeOutput = true;
  267. }
  268. break;
  269. case 'g':
  270. if (str.compare("-groupAnimations") == 0 || str.compare("-g") == 0)
  271. {
  272. // read two strings, make sure not to go out of bounds
  273. if ((*index + 2) >= options.size())
  274. {
  275. LOG(1, "Error: -g requires 2 arguments.\n");
  276. _parseError = true;
  277. return;
  278. }
  279. (*index)++;
  280. _groupAnimationNodeId.push_back(options[*index]);
  281. (*index)++;
  282. _groupAnimationAnimationId.push_back(options[*index]);
  283. }
  284. break;
  285. case 'i':
  286. // Node ID
  287. (*index)++;
  288. if (*index < options.size())
  289. {
  290. _nodeId.assign(options[*index]);
  291. }
  292. else
  293. {
  294. LOG(1, "Error: missing arguemnt for -%c.\n", str[1]);
  295. _parseError = true;
  296. return;
  297. }
  298. break;
  299. case 'o':
  300. // Optimization flag
  301. if (str == "-oa")
  302. {
  303. // Optimize animations
  304. _optimizeAnimations = true;
  305. }
  306. break;
  307. case 'h':
  308. {
  309. bool isHighPrecision = str.compare("-hp") == 0;
  310. if (str.compare("-heightmap") == 0 || str.compare("-h") == 0 || isHighPrecision)
  311. {
  312. (*index)++;
  313. if (*index < (options.size() + 1))
  314. {
  315. _heightmaps.resize(_heightmaps.size() + 1);
  316. HeightmapOption& heightmap = _heightmaps.back();
  317. heightmap.isHighPrecision = isHighPrecision;
  318. // Split node id list into tokens
  319. unsigned int length = options[*index].size() + 1;
  320. char* nodeIds = new char[length];
  321. strcpy(nodeIds, options[*index].c_str());
  322. nodeIds[length-1] = 0;
  323. char* id = strtok(nodeIds, " ");
  324. while (id)
  325. {
  326. heightmap.nodeIds.push_back(id);
  327. id = strtok(NULL, " ");
  328. }
  329. delete[] nodeIds;
  330. // Store output filename
  331. (*index)++;
  332. heightmap.filename = options[*index];
  333. if (heightmap.filename.empty())
  334. {
  335. LOG(1, "Error: missing filename argument for -h|-heightmap.\n");
  336. _parseError = true;
  337. return;
  338. }
  339. // Ensure the output filename has a .png extention
  340. if (heightmap.filename.length() > 5)
  341. {
  342. const char* ext = heightmap.filename.c_str() + (heightmap.filename.length() - 4);
  343. if (ext[0] != '.' || tolower(ext[1]) != 'p' || tolower(ext[2]) != 'n' || tolower(ext[3]) != 'g')
  344. heightmap.filename += ".png";
  345. }
  346. else
  347. heightmap.filename += ".png";
  348. }
  349. else
  350. {
  351. LOG(1, "Error: missing argument for -h|-heightmap.\n");
  352. _parseError = true;
  353. return;
  354. }
  355. }
  356. }
  357. break;
  358. case 'p':
  359. _fontPreview = true;
  360. break;
  361. case 's':
  362. // Font Size
  363. // old format was -s##
  364. if (str.length() > 2)
  365. {
  366. char n = str[2];
  367. if (n > '0' && n <= '9')
  368. {
  369. const char* number = str.c_str() + 2;
  370. _fontSize = atoi(number);
  371. break;
  372. }
  373. }
  374. (*index)++;
  375. if (*index < options.size())
  376. {
  377. _fontSize = atoi(options[*index].c_str());
  378. }
  379. else
  380. {
  381. LOG(1, "Error: missing arguemnt for -%c.\n", str[1]);
  382. _parseError = true;
  383. return;
  384. }
  385. break;
  386. case 't':
  387. _textOutput = true;
  388. break;
  389. case 'v':
  390. (*index)++;
  391. if (*index < options.size())
  392. {
  393. __logVerbosity = atoi(options[*index].c_str());
  394. if (__logVerbosity < 0)
  395. __logVerbosity = 0;
  396. else if (__logVerbosity > 4)
  397. __logVerbosity = 4;
  398. }
  399. default:
  400. break;
  401. }
  402. }
  403. void EncoderArguments::setInputfilePath(const std::string& inputPath)
  404. {
  405. _filePath.assign(getRealPath(inputPath));
  406. }
  407. void EncoderArguments::setOutputfilePath(const std::string& outputPath)
  408. {
  409. if (outputPath.size() > 0 && outputPath[0] != '\0')
  410. {
  411. std::string realPath = getRealPath(outputPath);
  412. if (endsWith(realPath.c_str(), ".gpb"))
  413. {
  414. _fileOutputPath.assign(realPath);
  415. }
  416. else if (endsWith(outputPath.c_str(), "/"))
  417. {
  418. std::string filenameNoExt = getFilenameNoExt(getFilenameFromFilePath(_filePath));
  419. _fileOutputPath.assign(outputPath);
  420. _fileOutputPath.append(filenameNoExt);
  421. _fileOutputPath.append(".gpb");
  422. }
  423. else
  424. {
  425. std::string filenameNoExt = getFilenameNoExt(getFilenameFromFilePath(realPath));
  426. int pos = realPath.find_last_of("/");
  427. if (pos)
  428. {
  429. _fileOutputPath = realPath.substr(0, pos);
  430. _fileOutputPath.append("/");
  431. _fileOutputPath.append(filenameNoExt);
  432. _fileOutputPath.append(".gpb");
  433. }
  434. }
  435. }
  436. }
  437. std::string EncoderArguments::getRealPath(const std::string& filepath)
  438. {
  439. char path[PATH_MAX + 1]; /* not sure about the "+ 1" */
  440. realpath(filepath.c_str(), path);
  441. replace_char(path, '\\', '/');
  442. return std::string(path);
  443. }
  444. void EncoderArguments::replace_char(char* str, char oldChar, char newChar)
  445. {
  446. for (; *str != '\0'; ++str)
  447. {
  448. if (*str == oldChar)
  449. {
  450. *str = newChar;
  451. }
  452. }
  453. }
  454. std::string concat(const std::string& a, const char* b)
  455. {
  456. std::string str(a);
  457. str.append(b);
  458. return str;
  459. }
  460. void unittestsEncoderArguments()
  461. {
  462. std::string dir = EncoderArguments::getRealPath(".");
  463. std::string exePath = EncoderArguments::getRealPath(".");
  464. exePath.append("/gameplay-encoder.exe");
  465. const char* exe = exePath.c_str();
  466. {
  467. const char* argv[] = {exe, "-g", "root", "movements", "C:\\Git\\gaming\\GamePlay\\gameplay-encoder\\res\\seymour.dae"};
  468. EncoderArguments args(sizeof(argv) / sizeof(char*), (const char**)argv);
  469. assert(equals(args.getAnimationId("root"), ("movements")));
  470. assert(equals(args.getGroupAnimationNodeId()[0], ("root")));
  471. assert(equals(args.getOutputFilePath(), "C:/Git/gaming/GamePlay/gameplay-encoder/res/seymour.gpb"));
  472. }
  473. {
  474. // Test with only input file name (relative)
  475. const char* argv[] = {exe, "input.dae"};
  476. EncoderArguments args(sizeof(argv) / sizeof(char*), (const char**)argv);
  477. assert(equals(args.getFilePath(), concat(dir, "/input.dae")));
  478. assert(equals(args.getOutputFilePath(), concat(dir, "/input.gpb")));
  479. equals(args.getOutputDirPath(), dir);
  480. }
  481. {
  482. // Test specifying a relative output path
  483. const char* argv[] = {exe, "input.dae", "output.gpb"};
  484. EncoderArguments args(sizeof(argv) / sizeof(char*), (const char**)argv);
  485. assert(equals(args.getFilePath(), concat(dir, "/input.dae")));
  486. assert(equals(args.getOutputFilePath(), concat(dir, "/output.gpb")));
  487. }
  488. {
  489. // Test specifying a relative output path
  490. const char* argv[] = {exe, "input.fbx", "output.gpb"};
  491. EncoderArguments args(sizeof(argv) / sizeof(char*), (const char**)argv);
  492. assert(equals(args.getFilePath(), concat(dir, "/input.fbx")));
  493. assert(equals(args.getOutputFilePath(), concat(dir, "/output.gpb")));
  494. }
  495. {
  496. // Test specifying a relative output path to a directory
  497. const char* argv[] = {exe, "input.dae", "stuff/output.gpb"};
  498. EncoderArguments args(sizeof(argv) / sizeof(char*), (const char**)argv);
  499. assert(equals(args.getFilePath(), concat(dir, "/input.dae")));
  500. assert(equals(args.getOutputFilePath(), concat(dir, "/stuff/output.gpb")));
  501. }
  502. {
  503. // Test parsing some arguments
  504. const char* argv[] = {exe, "-dae", "collada.dae", "-t", "input.dae", "output.gpb"};
  505. EncoderArguments args(sizeof(argv) / sizeof(char*), (const char**)argv);
  506. assert(equals(args.getFilePath(), concat(dir, "/input.dae")));
  507. assert(equals(args.getOutputFilePath(), concat(dir, "/output.gpb")));
  508. assert(args.textOutputEnabled());
  509. //assert(equals(args.getDAEOutputPath(), concat(dir, "/collada.dae")));
  510. }
  511. {
  512. // Test output file with no file extension
  513. const char* argv[] = {exe, "input.dae", "output"};
  514. EncoderArguments args(sizeof(argv) / sizeof(char*), (const char**)argv);
  515. assert(equals(args.getFilePath(), concat(dir, "/input.dae")));
  516. assert(equals(args.getOutputFilePath(), concat(dir, "/output.gpb")));
  517. }
  518. {
  519. // Test output file with wrong file extension
  520. const char* argv[] = {exe, "input.dae", "output.dae"};
  521. EncoderArguments args(sizeof(argv) / sizeof(char*), (const char**)argv);
  522. assert(equals(args.getFilePath(), concat(dir, "/input.dae")));
  523. assert(equals(args.getOutputFilePath(), concat(dir, "/output.gpb")));
  524. }
  525. }
  526. }