EncoderArguments.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. size_t filePathIndex = argc - 1;
  22. if (argv[filePathIndex])
  23. {
  24. _filePath.assign(getRealPath(argv[filePathIndex]));
  25. }
  26. // read the options
  27. std::vector<std::string> options;
  28. for (size_t i = 1; i < filePathIndex; ++i)
  29. {
  30. options.push_back(argv[i]);
  31. }
  32. for (size_t i = 0; i < options.size(); ++i)
  33. {
  34. if (options[i][0] == '-')
  35. {
  36. readOption(options, &i);
  37. }
  38. }
  39. }
  40. else
  41. {
  42. _parseError = true;
  43. }
  44. }
  45. EncoderArguments::~EncoderArguments(void)
  46. {
  47. }
  48. EncoderArguments* EncoderArguments::getInstance()
  49. {
  50. return __instance;
  51. }
  52. const std::string& EncoderArguments::getFilePath() const
  53. {
  54. return _filePath;
  55. }
  56. const char* EncoderArguments::getFilePathPointer() const
  57. {
  58. return _filePath.c_str();
  59. }
  60. std::string EncoderArguments::getOutputPath() const
  61. {
  62. int pos = _filePath.find_last_of('/');
  63. return (pos == -1 ? _filePath : _filePath.substr(0, pos));
  64. }
  65. const std::string& EncoderArguments::getDAEOutputPath() const
  66. {
  67. return _daeOutputPath;
  68. }
  69. const std::vector<std::string>& EncoderArguments::getGroupAnimationNodeId() const
  70. {
  71. return _groupAnimationNodeId;
  72. }
  73. const std::vector<std::string>& EncoderArguments::getGroupAnimationAnimationId() const
  74. {
  75. return _groupAnimationAnimationId;
  76. }
  77. bool EncoderArguments::containsGroupNodeId(const std::string& nodeId) const
  78. {
  79. return find(_groupAnimationNodeId.begin(), _groupAnimationNodeId.end(), nodeId) != _groupAnimationNodeId.end();
  80. }
  81. const std::string EncoderArguments::getAnimationId(const std::string& nodeId) const
  82. {
  83. std::vector<std::string>::const_iterator it = find(_groupAnimationNodeId.begin(), _groupAnimationNodeId.end(), nodeId);
  84. for (size_t i = 0, size = _groupAnimationNodeId.size(); i < size; ++i)
  85. {
  86. if (_groupAnimationNodeId[i].compare(nodeId) == 0)
  87. {
  88. return _groupAnimationAnimationId[i];
  89. }
  90. }
  91. return "";
  92. }
  93. const std::vector<std::string>& EncoderArguments::getHeightmapNodeIds() const
  94. {
  95. return _heightmapNodeIds;
  96. }
  97. bool EncoderArguments::parseErrorOccured() const
  98. {
  99. return _parseError;
  100. }
  101. bool EncoderArguments::fileExists() const
  102. {
  103. if (_filePath.length() > 0)
  104. {
  105. struct stat buf;
  106. if (stat(_filePath.c_str(), &buf) != -1)
  107. {
  108. return true;
  109. }
  110. }
  111. return false;
  112. }
  113. void EncoderArguments::printUsage() const
  114. {
  115. fprintf(stderr,"Usage: gameplay-encoder [options] <filepath>\n\n");
  116. fprintf(stderr,"Supported file extensions:\n");
  117. fprintf(stderr," .dae\t(COLLADA)\n");
  118. fprintf(stderr," .fbx\t(FBX)\n");
  119. fprintf(stderr," .ttf\t(TrueType Font)\n");
  120. fprintf(stderr,"\n");
  121. fprintf(stderr,"COLLADA and FBX file options:\n");
  122. fprintf(stderr," -i <id>\t\tFilter by node ID.\n");
  123. fprintf(stderr," -t\t\t\tWrite text/xml.\n");
  124. fprintf(stderr," -groupAnimations <node id> <animation id>\n" \
  125. "\t\t\tGroup all animation channels targetting the nodes into a new animation.\n");
  126. fprintf(stderr," -heightmaps \"<node ids>\"\n" \
  127. "\t\t\tList of nodes to generate heightmaps for.\n" \
  128. "\t\t\tNode id list should be in quotes with a space between each id.\n" \
  129. "\t\t\tHeightmaps will be saved in files named <nodeid>.png.\n");
  130. fprintf(stderr,"\n");
  131. fprintf(stderr,"COLLADA file options:\n");
  132. fprintf(stderr," -dae <filepath>\tOutput optimized DAE.\n");
  133. fprintf(stderr,"\n");
  134. fprintf(stderr,"TTF file options:\n");
  135. fprintf(stderr," -s <size of font>\tSize of the font.\n");
  136. fprintf(stderr," -p\t\t\tOutput font preview.\n");
  137. exit(8);
  138. }
  139. bool EncoderArguments::fontPreviewEnabled() const
  140. {
  141. return _fontPreview;
  142. }
  143. bool EncoderArguments::textOutputEnabled() const
  144. {
  145. return _textOutput;
  146. }
  147. bool EncoderArguments::DAEOutputEnabled() const
  148. {
  149. return _daeOutput;
  150. }
  151. const char* EncoderArguments::getNodeId() const
  152. {
  153. if (_nodeId.length() == 0)
  154. {
  155. return NULL;
  156. }
  157. return _nodeId.c_str();
  158. }
  159. unsigned int EncoderArguments::getFontSize() const
  160. {
  161. return _fontSize;
  162. }
  163. EncoderArguments::FileFormat EncoderArguments::getFileFormat() const
  164. {
  165. if (_filePath.length() < 5)
  166. {
  167. return FILEFORMAT_UNKNOWN;
  168. }
  169. // Extract the extension
  170. std::string ext = "";
  171. size_t pos = _filePath.find_last_of(".");
  172. if (pos != std::string::npos)
  173. {
  174. ext = _filePath.substr(pos + 1);
  175. }
  176. // Match every supported extension with its format constant
  177. if (ext.compare("dae") == 0 || ext.compare("DAE") == 0)
  178. {
  179. return FILEFORMAT_DAE;
  180. }
  181. if (ext.compare("fbx") == 0 || ext.compare("FBX") == 0)
  182. {
  183. return FILEFORMAT_FBX;
  184. }
  185. if (ext.compare("ttf") == 0 || ext.compare("TTF") == 0)
  186. {
  187. return FILEFORMAT_TTF;
  188. }
  189. if (ext.compare("gpb") == 0 || ext.compare("GPB") == 0)
  190. {
  191. return FILEFORMAT_GPB;
  192. }
  193. return FILEFORMAT_UNKNOWN;
  194. }
  195. void EncoderArguments::readOption(const std::vector<std::string>& options, size_t* index)
  196. {
  197. const std::string& str = options[*index];
  198. if (str.length() == 0 && str[0] != '-')
  199. {
  200. return;
  201. }
  202. switch (str[1])
  203. {
  204. case 'd':
  205. if (str.compare("-dae") == 0)
  206. {
  207. // read one string, make sure not to go out of bounds
  208. if ((*index + 1) >= options.size())
  209. {
  210. fprintf(stderr, "Error: -dae requires 1 argument.\n");
  211. _parseError = true;
  212. return;
  213. }
  214. (*index)++;
  215. _daeOutputPath = options[*index];
  216. _daeOutput = true;
  217. }
  218. break;
  219. case 'g':
  220. if (str.compare("-groupAnimations") == 0)
  221. {
  222. // read two strings, make sure not to go out of bounds
  223. if ((*index + 2) >= options.size())
  224. {
  225. fprintf(stderr, "Error: -groupAnimations requires 2 arguments.\n");
  226. _parseError = true;
  227. return;
  228. }
  229. (*index)++;
  230. _groupAnimationNodeId.push_back(options[*index]);
  231. (*index)++;
  232. _groupAnimationAnimationId.push_back(options[*index]);
  233. }
  234. break;
  235. case 'i':
  236. case 'o':
  237. // Node ID
  238. (*index)++;
  239. if (*index < options.size())
  240. {
  241. _nodeId.assign(options[*index]);
  242. }
  243. else
  244. {
  245. fprintf(stderr, "Error: missing arguemnt for -%c.\n", str[1]);
  246. _parseError = true;
  247. return;
  248. }
  249. break;
  250. case 'h':
  251. {
  252. if (str.compare("-heightmaps") == 0)
  253. {
  254. (*index)++;
  255. if (*index < options.size())
  256. {
  257. // Split node id list into tokens
  258. unsigned int length = options[*index].size() + 1;
  259. char* nodeIds = new char[length];
  260. strcpy(nodeIds, options[*index].c_str());
  261. nodeIds[length-1] = 0;
  262. char* id = strtok(nodeIds, " ");
  263. while (id)
  264. {
  265. _heightmapNodeIds.push_back(id);
  266. id = strtok(NULL, " ");
  267. }
  268. delete[] nodeIds;
  269. }
  270. else
  271. {
  272. fprintf(stderr, "Error: missing argument for -heightmaps.\n");
  273. }
  274. }
  275. }
  276. break;
  277. case 'p':
  278. _fontPreview = true;
  279. break;
  280. case 's':
  281. // Font Size
  282. // old format was -s##
  283. if (str.length() > 2)
  284. {
  285. char n = str[2];
  286. if (n > '0' && n <= '9')
  287. {
  288. const char* number = str.c_str() + 2;
  289. _fontSize = atoi(number);
  290. break;
  291. }
  292. }
  293. (*index)++;
  294. if (*index < options.size())
  295. {
  296. _fontSize = atoi(options[*index].c_str());
  297. }
  298. else
  299. {
  300. fprintf(stderr, "Error: missing arguemnt for -%c.\n", str[1]);
  301. _parseError = true;
  302. return;
  303. }
  304. break;
  305. case 't':
  306. _textOutput = true;
  307. break;
  308. default:
  309. break;
  310. }
  311. }
  312. std::string EncoderArguments::getRealPath(const std::string& filepath)
  313. {
  314. char path[PATH_MAX + 1]; /* not sure about the "+ 1" */
  315. realpath(filepath.c_str(), path);
  316. replace_char(path, '\\', '/');
  317. return std::string(path);
  318. }
  319. void EncoderArguments::replace_char(char* str, char oldChar, char newChar)
  320. {
  321. for (; *str != '\0'; ++str)
  322. {
  323. if (*str == oldChar)
  324. {
  325. *str = newChar;
  326. }
  327. }
  328. }
  329. }