EncoderArguments.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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. fprintf(stderr,"\n");
  169. fprintf(stderr,"TTF file options:\n");
  170. fprintf(stderr," -s <size>\tSize of the font.\n");
  171. fprintf(stderr," -p\t\tOutput font preview.\n");
  172. fprintf(stderr, "\n");
  173. exit(8);
  174. }
  175. bool EncoderArguments::fontPreviewEnabled() const
  176. {
  177. return _fontPreview;
  178. }
  179. bool EncoderArguments::textOutputEnabled() const
  180. {
  181. return _textOutput;
  182. }
  183. bool EncoderArguments::DAEOutputEnabled() const
  184. {
  185. return _daeOutput;
  186. }
  187. const char* EncoderArguments::getNodeId() const
  188. {
  189. if (_nodeId.length() == 0)
  190. {
  191. return NULL;
  192. }
  193. return _nodeId.c_str();
  194. }
  195. unsigned int EncoderArguments::getFontSize() const
  196. {
  197. return _fontSize;
  198. }
  199. EncoderArguments::FileFormat EncoderArguments::getFileFormat() const
  200. {
  201. if (_filePath.length() < 5)
  202. {
  203. return FILEFORMAT_UNKNOWN;
  204. }
  205. // Extract the extension
  206. std::string ext = "";
  207. size_t pos = _filePath.find_last_of(".");
  208. if (pos != std::string::npos)
  209. {
  210. ext = _filePath.substr(pos + 1);
  211. }
  212. // Match every supported extension with its format constant
  213. if (ext.compare("dae") == 0 || ext.compare("DAE") == 0)
  214. {
  215. return FILEFORMAT_DAE;
  216. }
  217. if (ext.compare("fbx") == 0 || ext.compare("FBX") == 0)
  218. {
  219. return FILEFORMAT_FBX;
  220. }
  221. if (ext.compare("ttf") == 0 || ext.compare("TTF") == 0)
  222. {
  223. return FILEFORMAT_TTF;
  224. }
  225. if (ext.compare("gpb") == 0 || ext.compare("GPB") == 0)
  226. {
  227. return FILEFORMAT_GPB;
  228. }
  229. return FILEFORMAT_UNKNOWN;
  230. }
  231. void EncoderArguments::readOption(const std::vector<std::string>& options, size_t* index)
  232. {
  233. const std::string& str = options[*index];
  234. if (str.length() == 0 && str[0] != '-')
  235. {
  236. return;
  237. }
  238. switch (str[1])
  239. {
  240. case 'd':
  241. if (str.compare("-dae") == 0)
  242. {
  243. // read one string, make sure not to go out of bounds
  244. if ((*index + 1) >= options.size())
  245. {
  246. fprintf(stderr, "Error: -dae requires 1 argument.\n");
  247. _parseError = true;
  248. return;
  249. }
  250. (*index)++;
  251. _daeOutputPath = options[*index];
  252. _daeOutput = true;
  253. }
  254. break;
  255. case 'g':
  256. if (str.compare("-groupAnimations") == 0 || str.compare("-g") == 0)
  257. {
  258. // read two strings, make sure not to go out of bounds
  259. if ((*index + 2) >= options.size())
  260. {
  261. fprintf(stderr, "Error: -g requires 2 arguments.\n");
  262. _parseError = true;
  263. return;
  264. }
  265. (*index)++;
  266. _groupAnimationNodeId.push_back(options[*index]);
  267. (*index)++;
  268. _groupAnimationAnimationId.push_back(options[*index]);
  269. }
  270. break;
  271. case 'i':
  272. case 'o':
  273. // Node ID
  274. (*index)++;
  275. if (*index < options.size())
  276. {
  277. _nodeId.assign(options[*index]);
  278. }
  279. else
  280. {
  281. fprintf(stderr, "Error: missing arguemnt for -%c.\n", str[1]);
  282. _parseError = true;
  283. return;
  284. }
  285. break;
  286. case 'h':
  287. {
  288. if (str.compare("-heightmap") == 0 || str.compare("-h") == 0)
  289. {
  290. (*index)++;
  291. if (*index < (options.size() + 1))
  292. {
  293. _heightmaps.resize(_heightmaps.size() + 1);
  294. HeightmapOption& heightmap = _heightmaps.back();
  295. // Split node id list into tokens
  296. unsigned int length = options[*index].size() + 1;
  297. char* nodeIds = new char[length];
  298. strcpy(nodeIds, options[*index].c_str());
  299. nodeIds[length-1] = 0;
  300. char* id = strtok(nodeIds, " ");
  301. while (id)
  302. {
  303. heightmap.nodeIds.push_back(id);
  304. id = strtok(NULL, " ");
  305. }
  306. delete[] nodeIds;
  307. // Store output filename
  308. (*index)++;
  309. heightmap.filename = options[*index];
  310. if (heightmap.filename.empty())
  311. {
  312. fprintf(stderr, "Error: missing filename argument for -h|-heightmap.\n");
  313. _parseError = true;
  314. return;
  315. }
  316. // Ensure the output filename has a .png extention
  317. if (heightmap.filename.length() > 5)
  318. {
  319. const char* ext = heightmap.filename.c_str() + (heightmap.filename.length() - 4);
  320. if (ext[0] != '.' || tolower(ext[1]) != 'p' || tolower(ext[2]) != 'n' || tolower(ext[3]) != 'g')
  321. heightmap.filename += ".png";
  322. }
  323. else
  324. heightmap.filename += ".png";
  325. }
  326. else
  327. {
  328. fprintf(stderr, "Error: missing argument for -h|-heightmap.\n");
  329. _parseError = true;
  330. return;
  331. }
  332. }
  333. }
  334. break;
  335. case 'p':
  336. _fontPreview = true;
  337. break;
  338. case 's':
  339. // Font Size
  340. // old format was -s##
  341. if (str.length() > 2)
  342. {
  343. char n = str[2];
  344. if (n > '0' && n <= '9')
  345. {
  346. const char* number = str.c_str() + 2;
  347. _fontSize = atoi(number);
  348. break;
  349. }
  350. }
  351. (*index)++;
  352. if (*index < options.size())
  353. {
  354. _fontSize = atoi(options[*index].c_str());
  355. }
  356. else
  357. {
  358. fprintf(stderr, "Error: missing arguemnt for -%c.\n", str[1]);
  359. _parseError = true;
  360. return;
  361. }
  362. break;
  363. case 't':
  364. _textOutput = true;
  365. break;
  366. default:
  367. break;
  368. }
  369. }
  370. void EncoderArguments::setInputfilePath(const std::string& inputPath)
  371. {
  372. _filePath.assign(getRealPath(inputPath));
  373. }
  374. void EncoderArguments::setOutputfilePath(const std::string& outputPath)
  375. {
  376. if (outputPath.size() > 0 && outputPath[0] != '\0')
  377. {
  378. std::string realPath = getRealPath(outputPath);
  379. if (endsWith(realPath.c_str(), ".gpb"))
  380. {
  381. _fileOutputPath.assign(realPath);
  382. }
  383. else if (endsWith(outputPath.c_str(), "/"))
  384. {
  385. std::string filenameNoExt = getFilenameNoExt(getFilenameFromFilePath(_filePath));
  386. _fileOutputPath.assign(outputPath);
  387. _fileOutputPath.append(filenameNoExt);
  388. _fileOutputPath.append(".gpb");
  389. }
  390. else
  391. {
  392. std::string filenameNoExt = getFilenameNoExt(getFilenameFromFilePath(realPath));
  393. int pos = realPath.find_last_of("/");
  394. if (pos)
  395. {
  396. _fileOutputPath = realPath.substr(0, pos);
  397. _fileOutputPath.append("/");
  398. _fileOutputPath.append(filenameNoExt);
  399. _fileOutputPath.append(".gpb");
  400. }
  401. }
  402. }
  403. }
  404. std::string EncoderArguments::getRealPath(const std::string& filepath)
  405. {
  406. char path[PATH_MAX + 1]; /* not sure about the "+ 1" */
  407. realpath(filepath.c_str(), path);
  408. replace_char(path, '\\', '/');
  409. return std::string(path);
  410. }
  411. void EncoderArguments::replace_char(char* str, char oldChar, char newChar)
  412. {
  413. for (; *str != '\0'; ++str)
  414. {
  415. if (*str == oldChar)
  416. {
  417. *str = newChar;
  418. }
  419. }
  420. }
  421. std::string concat(const std::string& a, const char* b)
  422. {
  423. std::string str(a);
  424. str.append(b);
  425. return str;
  426. }
  427. void unittestsEncoderArguments()
  428. {
  429. std::string dir = EncoderArguments::getRealPath(".");
  430. std::string exePath = EncoderArguments::getRealPath(".");
  431. exePath.append("/gameplay-encoder.exe");
  432. const char* exe = exePath.c_str();
  433. {
  434. const char* argv[] = {exe, "-g", "root", "movements", "C:\\Git\\gaming\\GamePlay\\gameplay-encoder\\res\\seymour.dae"};
  435. EncoderArguments args(sizeof(argv) / sizeof(char*), (const char**)argv);
  436. assert(equals(args.getAnimationId("root"), ("movements")));
  437. assert(equals(args.getGroupAnimationNodeId()[0], ("root")));
  438. assert(equals(args.getOutputFilePath(), "C:/Git/gaming/GamePlay/gameplay-encoder/res/seymour.gpb"));
  439. }
  440. {
  441. // Test with only input file name (relative)
  442. const char* argv[] = {exe, "input.dae"};
  443. EncoderArguments args(sizeof(argv) / sizeof(char*), (const char**)argv);
  444. assert(equals(args.getFilePath(), concat(dir, "/input.dae")));
  445. assert(equals(args.getOutputFilePath(), concat(dir, "/input.gpb")));
  446. equals(args.getOutputDirPath(), dir);
  447. }
  448. {
  449. // Test specifying a relative output path
  450. const char* argv[] = {exe, "input.dae", "output.gpb"};
  451. EncoderArguments args(sizeof(argv) / sizeof(char*), (const char**)argv);
  452. assert(equals(args.getFilePath(), concat(dir, "/input.dae")));
  453. assert(equals(args.getOutputFilePath(), concat(dir, "/output.gpb")));
  454. }
  455. {
  456. // Test specifying a relative output path
  457. const char* argv[] = {exe, "input.fbx", "output.gpb"};
  458. EncoderArguments args(sizeof(argv) / sizeof(char*), (const char**)argv);
  459. assert(equals(args.getFilePath(), concat(dir, "/input.fbx")));
  460. assert(equals(args.getOutputFilePath(), concat(dir, "/output.gpb")));
  461. }
  462. {
  463. // Test specifying a relative output path to a directory
  464. const char* argv[] = {exe, "input.dae", "stuff/output.gpb"};
  465. EncoderArguments args(sizeof(argv) / sizeof(char*), (const char**)argv);
  466. assert(equals(args.getFilePath(), concat(dir, "/input.dae")));
  467. assert(equals(args.getOutputFilePath(), concat(dir, "/stuff/output.gpb")));
  468. }
  469. {
  470. // Test parsing some arguments
  471. const char* argv[] = {exe, "-dae", "collada.dae", "-t", "input.dae", "output.gpb"};
  472. EncoderArguments args(sizeof(argv) / sizeof(char*), (const char**)argv);
  473. assert(equals(args.getFilePath(), concat(dir, "/input.dae")));
  474. assert(equals(args.getOutputFilePath(), concat(dir, "/output.gpb")));
  475. assert(args.textOutputEnabled());
  476. //assert(equals(args.getDAEOutputPath(), concat(dir, "/collada.dae")));
  477. }
  478. {
  479. // Test output file with no file extension
  480. const char* argv[] = {exe, "input.dae", "output"};
  481. EncoderArguments args(sizeof(argv) / sizeof(char*), (const char**)argv);
  482. assert(equals(args.getFilePath(), concat(dir, "/input.dae")));
  483. assert(equals(args.getOutputFilePath(), concat(dir, "/output.gpb")));
  484. }
  485. {
  486. // Test output file with wrong file extension
  487. const char* argv[] = {exe, "input.dae", "output.dae"};
  488. EncoderArguments args(sizeof(argv) / sizeof(char*), (const char**)argv);
  489. assert(equals(args.getFilePath(), concat(dir, "/input.dae")));
  490. assert(equals(args.getOutputFilePath(), concat(dir, "/output.gpb")));
  491. }
  492. }
  493. }