EncoderArguments.cpp 17 KB

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