EncoderArguments.cpp 15 KB

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