main.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. // bean header
  2. #include "includes/BEAN.h"
  3. ////////////////////////////
  4. // ENUM WITH PROGRAM ACTIONS
  5. typedef enum
  6. {
  7. // nothing to do
  8. NOTHING_TO_DO,
  9. // checking
  10. CHECK_ANIM, // check if anim file has a correct structure
  11. CHECK_MODEL, // check if model file has a correct structure
  12. CHECK_ANIM_WITH_MODEL, // check if anim file (either CSV or BIN) and binary model file are related
  13. // reading
  14. READ_ANIM, // read anim file (either CSV or BIN)
  15. READ_MODEL, // read binary model file
  16. READ_ANIM_WITH_MODEL, // read both anim file (either CSV or BIN) and binary model file
  17. // converting
  18. CONVERT_CSV_ANIM_TO_BIN_ANIM, // convert csv anim file smax_to bin anim file
  19. CONVERT_BIN_ANIM_TO_CSV_ANIM, // convert bin anim file smax_to csv anim file
  20. // side stuff
  21. EXTRACT_MODEL_TEXTURES,
  22. } P_ACTION;
  23. //////////////////////////
  24. // PROGRAM ACTION VARIABLE
  25. P_ACTION ACTION = NOTHING_TO_DO;
  26. ////////////////////////
  27. // VALID COMMAND STRINGS
  28. char_t ANIM_INPUT_ARG[] = "-anim";
  29. char_t MODEL_INPUT_ARG[] = "-model";
  30. char_t READ_FILE_ARG[] = "-read";
  31. char_t CHECK_FILE_ARG[] = "-check";
  32. char_t CONVERT_FILE_ARG[] = "-convert";
  33. char_t CONVERT_TO_CSV[] = "csv";
  34. char_t CONVERT_TO_BIN[] = "bin";
  35. char_t EXTRACT_TEXTURES[] = "--extract-bti";
  36. // functions only used on main.c
  37. void display_help(void);
  38. void check_command(char_t, char_t * []);
  39. bool_t is_bin_btp(char_t * file_path);
  40. smax_t is_anim(char_t *);
  41. void read_anim(char_t *);
  42. ////////////////
  43. // MAIN FUNCTION
  44. int main(int argc, char * argv[])
  45. {
  46. // first, check system endianness
  47. CHECK_SYS_ENDIAN();
  48. // don't want to deal with warnnings
  49. // I only want to work with bytes
  50. // (I will handle manually the sign)
  51. smax_t arg_count = (smax_t) argc;
  52. char_t ** arg_vector = (char_t **) argv;
  53. // check command and get program action
  54. check_command(arg_count, arg_vector);
  55. // another epic switch statement
  56. switch(ACTION)
  57. {
  58. /////////////////////////////////////////
  59. // ./a.out -model mario.bdl --extract-bti
  60. case(EXTRACT_MODEL_TEXTURES):
  61. EXTRACT_MODEL_BTI(arg_vector[2]);
  62. break;
  63. ////////////////////////////////////
  64. // ./a.out -anim iceflick.btp -check
  65. case(CHECK_ANIM):
  66. PRINT_BIN_BTP_CHECK(arg_vector[2]);
  67. break;
  68. //////////////////////////////////
  69. // ./a.out -model mario.bdl -check
  70. case(CHECK_MODEL):
  71. PRINT_MODEL_CHECK(arg_vector[2]);
  72. break;
  73. ///////////////////////////////////
  74. // ./a.out -anim iceflick.btp -read
  75. case(READ_ANIM):
  76. PRINT_BIN_BTP_CHECK(arg_vector[2]);
  77. PRINT_BIN_BTP_PTR_TB(arg_vector[2]);
  78. break;
  79. /////////////////////////////////
  80. // ./a.out -model mario.bdl -read
  81. case(READ_MODEL):
  82. PRINT_MODEL_CHECK(arg_vector[2]);
  83. PRINT_MODEL_PTR_TB(arg_vector[2]);
  84. break;
  85. /////////////////////////////////////////////////////
  86. // ./a.out -anim iceflick.btp -model mario.bdl -check
  87. case(CHECK_ANIM_WITH_MODEL):
  88. // check anim file and model file
  89. PRINT_MODEL_CHECK(arg_vector[4]);
  90. PRINT_BIN_BTP_CHECK(arg_vector[2]);
  91. // decide what to do with the anim file type
  92. if (GET_BIN_BTP_TYPE(arg_vector[2]) == BIN_BTP_TYPE)
  93. if (CHECK_BIN_BTP_WITH_BMD_BDL(arg_vector[2], arg_vector[4]) == false)
  94. exit(1);
  95. //~ else if (ANIM_TYPE == CSV_BTP_TYPE) {
  96. //~ if (CHECK_CSV_BTP_WITH_BMD_BDL(arg_vector[2], arg_vector[4]) == false)
  97. //~ exit(1);
  98. //~ }
  99. break;
  100. ////////////////////////////////////////////////////
  101. // ./a.out -anim iceflick.btp -model mario.bdl -read
  102. case(READ_ANIM_WITH_MODEL):
  103. // check anim file and model file
  104. PRINT_MODEL_CHECK(arg_vector[4]);
  105. PRINT_BIN_BTP_CHECK(arg_vector[2]);
  106. // read both files
  107. PRINT_MODEL_PTR_TB(arg_vector[4]);
  108. PRINT_BIN_BTP_PTR_TB(arg_vector[2]);
  109. break;
  110. ///////////////////////////////////////////////////////////
  111. // ./a.out -anim iceflick.btp -convert csv
  112. // ./a.out -anim iceflick.btp -model mario.bdl -convert csv
  113. case(CONVERT_BIN_ANIM_TO_CSV_ANIM):
  114. // if model is provided check if both anim and model files are related
  115. PRINT_BIN_BTP_CHECK(arg_vector[2]);
  116. // check if the file is actually valid for conversion
  117. if (GET_BIN_BTP_TYPE(arg_vector[2]) != BIN_BTP_TYPE) {
  118. printf("\nCannot convert CSV file to CSV file.\nTerminating program.\n");
  119. exit(1);
  120. }
  121. // model is provided
  122. if (arg_count == 7)
  123. {
  124. // decide what to do with the anim file type
  125. if (GET_BIN_BTP_TYPE(arg_vector[2]) == BIN_BTP_TYPE)
  126. {
  127. if (CHECK_BIN_BTP_WITH_BMD_BDL(arg_vector[2], arg_vector[4]) == false)
  128. exit(1);
  129. }
  130. }
  131. WRITE_CSV_BTP(arg_vector[2]);
  132. break;
  133. //~ ///////////////////////////////////////////////////////////////
  134. //~ // ./a.out -anim iceflick.btp.csv -model mario.bdl -convert bin
  135. //~ case(CONVERT_CSV_ANIM_TO_BIN_ANIM):
  136. //~ // check anim file
  137. //~ is_anim(arg_vector[2]);
  138. //~ // check if the file is actually valid for conversion
  139. //~ if (ANIM_TYPE == BIN_BTP_TYPE)
  140. //~ {
  141. //~ printf("\nCannot convert BIN file smax_to BIN file.\nTerminating program.\n");
  142. //~ exit(1);
  143. //~ }
  144. //~ // check model file
  145. //~ is_model(arg_vector[4]);
  146. //~ // decide what to do with the anim file type
  147. //~ if (ANIM_TYPE == CSV_BTP_TYPE)
  148. //~ {
  149. //~ if (CHECK_CSV_BTP_WITH_BMD_BDL(arg_vector[2], arg_vector[4]) == false)
  150. //~ exit(1);
  151. //~ }
  152. //~ // write csv animation file
  153. //~ WRITE_BIN_BTP(arg_vector[2], arg_vector[4]);
  154. //~ break;
  155. ///////
  156. // how?
  157. default:
  158. printf("How on earth you managed to get here?\n");
  159. break;
  160. }
  161. // program end
  162. return 0;
  163. }
  164. //////////////////////////
  165. // display_help() function
  166. // function used to display program usage in case
  167. // user inputs wrong information to the program.
  168. // Also terminates the program session.
  169. void display_help(void)
  170. {
  171. // display some help
  172. printf("Mateee... I dunnae know what to do with that command <:[\n");
  173. printf("Try again after these command examples:\n");
  174. printf("\n### Extract BTIs from model file\n");
  175. printf("bean -model mario.bdl --extract-bti\n");
  176. printf("\n### Check model file\n");
  177. printf("bean -model mario.bdl -check\n");
  178. printf("\n### Check anim file\n");
  179. printf("bean -anim iceflick.btp -check\n");
  180. printf("\n### Read model file file\n");
  181. printf("bean -model mario.bdl -read\n");
  182. printf("\n### Read anim file file\n");
  183. printf("bean -anim iceflick.btp -read\n");
  184. printf("\n### Convert a BIN anim file smax_to a CSV (NOT providing a model file)\n");
  185. printf("bean -anim iceflick.btp -convert csv\n");
  186. printf("\n### Read both an anim file and a model file\n");
  187. printf("bean -anim iceflick.btp -model mario.bdl -read\n");
  188. printf("\n### Check if an anim file and a model file are related\n");
  189. printf("bean -anim iceflick.btp -model mario.bdl -check\n");
  190. printf("\n### Convert a BIN anim file smax_to a CSV (providing a model file)\n");
  191. printf("bean -anim iceflick.btp -model mario.bdl -convert csv\n");
  192. printf("\n### Convert a CSV anim file smax_to a BIN (providing a model file)\n");
  193. printf("bean -anim iceflick.btp.csv -model mario.bdl -convert bin\n");
  194. // incorrect command exit
  195. exit(1);
  196. }
  197. ///////////////////////////
  198. // check_command() function
  199. // function used to check if command
  200. // entered by the user is valid. Will
  201. // call display_help() in case it isn't.
  202. void check_command(char_t arg_count, char_t * arg_vector[])
  203. {
  204. // huge and epic switch statement >:]
  205. switch (arg_count)
  206. {
  207. // - checking a single file (anim or model)
  208. // - reading a single file (anim or model)
  209. // ./a.out -model mario.bdl -check
  210. // ./a.out -anim iceflick.btp -read
  211. case(4):
  212. // checking an anim file
  213. if (compare_strings(arg_vector[1], ANIM_INPUT_ARG, 0) && compare_strings(arg_vector[3], CHECK_FILE_ARG, 0))
  214. ACTION = CHECK_ANIM;
  215. // checking a model file
  216. else if (compare_strings(arg_vector[1], MODEL_INPUT_ARG, 0) && compare_strings(arg_vector[3], CHECK_FILE_ARG, 0))
  217. ACTION = CHECK_MODEL;
  218. // reading an anim file
  219. else if (compare_strings(arg_vector[1], ANIM_INPUT_ARG, 0) && compare_strings(arg_vector[3], READ_FILE_ARG, 0))
  220. ACTION = READ_ANIM;
  221. // reading an model file
  222. else if (compare_strings(arg_vector[1], MODEL_INPUT_ARG, 0) && compare_strings(arg_vector[3], READ_FILE_ARG, 0))
  223. ACTION = READ_MODEL;
  224. // invalid command
  225. else if (compare_strings(arg_vector[1], MODEL_INPUT_ARG, 0) && compare_strings(arg_vector[3], EXTRACT_TEXTURES, 0))
  226. ACTION = EXTRACT_MODEL_TEXTURES;
  227. // invalid command
  228. else
  229. ACTION = NOTHING_TO_DO;
  230. break;
  231. // - converting a bin anim file smax_to csv (without providing model file)
  232. // ./a.out -anim iceflick.btp -convert csv
  233. case(5):
  234. // converting a bin anim file smax_to csv
  235. if (compare_strings(arg_vector[1], ANIM_INPUT_ARG, 0) &&
  236. compare_strings(arg_vector[3], CONVERT_FILE_ARG, 0) &&
  237. compare_strings(arg_vector[4], CONVERT_TO_CSV, 0))
  238. ACTION = CONVERT_BIN_ANIM_TO_CSV_ANIM;
  239. // invalid command
  240. else
  241. ACTION = NOTHING_TO_DO;
  242. break;
  243. // - checking if an anim file and a model file relate
  244. // - reading both an anim file and a model file (don't check if they relate)
  245. // ./a.out -anim iceflick.btp -model mario.bdl -read
  246. // ./a.out -anim iceflick.btp -model mario.bdl -check
  247. case(6):
  248. if (compare_strings(arg_vector[1], ANIM_INPUT_ARG, 0) &&
  249. compare_strings(arg_vector[3], MODEL_INPUT_ARG, 0))
  250. {
  251. // read both anim and model files
  252. if (compare_strings(arg_vector[5], READ_FILE_ARG, 0))
  253. ACTION = READ_ANIM_WITH_MODEL;
  254. // check if both anim and model files are related
  255. else if (compare_strings(arg_vector[5], CHECK_FILE_ARG, 0))
  256. ACTION = CHECK_ANIM_WITH_MODEL;
  257. // invalid command
  258. else
  259. ACTION = NOTHING_TO_DO;
  260. }
  261. // invalid command
  262. else
  263. ACTION = NOTHING_TO_DO;
  264. break;
  265. // - converting a binary anim file smax_to a csv file (providing model file)
  266. // - converting a csv anim file smax_to a bin file (providing model file)
  267. // ./a.out -anim iceflick.btp -model mario.bdl -convert csv
  268. // ./a.out -anim iceflick.btp.csv -model mario.bdl -convert bin
  269. case(7):
  270. if (compare_strings(arg_vector[1], ANIM_INPUT_ARG, 0) &&
  271. compare_strings(arg_vector[3], MODEL_INPUT_ARG, 0) &&
  272. compare_strings(arg_vector[5], CONVERT_FILE_ARG, 0))
  273. {
  274. // convert to csv
  275. if (compare_strings(arg_vector[6], CONVERT_TO_CSV, 0))
  276. ACTION = CONVERT_BIN_ANIM_TO_CSV_ANIM;
  277. // convert to bin
  278. else if (compare_strings(arg_vector[6], CONVERT_TO_BIN, 0))
  279. ACTION = CONVERT_CSV_ANIM_TO_BIN_ANIM;
  280. // invalid command
  281. else
  282. ACTION = NOTHING_TO_DO;
  283. }
  284. // invalid command
  285. else
  286. ACTION = NOTHING_TO_DO;
  287. break;
  288. default:
  289. ACTION = NOTHING_TO_DO;
  290. break;
  291. }
  292. if (ACTION == NOTHING_TO_DO)
  293. display_help();
  294. }
  295. // is_bin_btp() function
  296. // function used to check if a file is a BIN BTP file
  297. bool_t is_bin_btp(char_t * file_path)
  298. {
  299. // first check if the file is valid
  300. Jbtp * btp = OPEN_BIN_BTP(file_path);
  301. CLOSE_BIN_BTP(btp);
  302. if (btp == NULL)
  303. return false;
  304. // all is good
  305. return true;
  306. }
  307. // is_anim() function
  308. // function used to check if a file is a correct anim file
  309. smax_t is_anim(char_t * anim_file_path)
  310. {
  311. // get file extension
  312. char_t * file_ext = get_file_ext(anim_file_path);
  313. //~ // CSV file check
  314. //~ if (compare_strings(file_ext, FILE_EXT[0], 0))
  315. //~ ANIM_TYPE = GET_CSV_TYPE(anim_file_path);
  316. // BIN BTP check
  317. if (compare_strings(file_ext, FILE_EXT[3], 0))
  318. return is_bin_btp(anim_file_path);
  319. // not an anim file
  320. return false;
  321. }
  322. ///////////////////////
  323. // read_anim() function
  324. // function to read an anim file based on the anim file type
  325. void read_anim(char_t * anim_file_path)
  326. {
  327. // decide what to do with the anim file type
  328. if (GET_BIN_BTP_TYPE(anim_file_path) == BIN_BTP_TYPE)
  329. PRINT_BIN_BTP_PTR_TB(anim_file_path);
  330. //~ else if (ANIM_TYPE == CSV_BTP_TYPE)
  331. //~ PRINT_CSV_BTP_STRUCT(anim_file_path);
  332. }