trade_patch_list.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include "../pokemon_app.h"
  2. #include "trade_patch_list.h"
  3. struct patch_list* plist_alloc(void) {
  4. struct patch_list* plist = NULL;
  5. plist = malloc(sizeof(struct patch_list));
  6. plist->index = 0;
  7. plist->next = NULL;
  8. return plist;
  9. }
  10. void plist_append(struct patch_list* plist, uint8_t index) {
  11. furi_assert(plist);
  12. for(;;) {
  13. if(plist->next == NULL) break;
  14. plist = plist->next;
  15. }
  16. plist->index = index;
  17. plist->next = plist_alloc();
  18. }
  19. void plist_free(struct patch_list* plist) {
  20. struct patch_list* plist_next = NULL;
  21. while(plist != NULL) {
  22. plist_next = plist->next;
  23. free(plist);
  24. plist = plist_next;
  25. }
  26. }
  27. /* Returns the index value at offset member of the list. If offset is beyond
  28. * the length of the allocated list, it will just return 0.
  29. */
  30. uint8_t plist_index_get(struct patch_list* plist, int offset) {
  31. furi_assert(plist);
  32. int i;
  33. for(i = 0; i < offset; i++) {
  34. if(plist->next == NULL) break;
  35. plist = plist->next;
  36. }
  37. return plist->index;
  38. }
  39. void plist_create(struct patch_list** pplist, TradeBlock* trade_block) {
  40. furi_assert(trade_block);
  41. uint8_t* trade_party_flat = (uint8_t*)trade_block->party;
  42. int i;
  43. /* If plist is non-NULL that means its already been created. Tear it down
  44. * first.
  45. */
  46. if(*pplist != NULL) {
  47. plist_free(*pplist);
  48. *pplist = NULL;
  49. }
  50. *pplist = plist_alloc();
  51. /* NOTE: 264 magic number is the length of the party block, 44 * 6 */
  52. /* The first half of the patch list covers offsets 0x00 - 0xfb, which
  53. * is expressed as 0x01 - 0xfc. An 0xFF byte is added to signify the
  54. * end of the first part. The second half of the patch list covers
  55. * offsets 0xfc - 0x107. Which is expressed as 0x01 - 0xc. A 0xFF byte
  56. * is added to signify the end of the second part/
  57. */
  58. for(i = 0; i < 264; i++) {
  59. FURI_LOG_D(TAG, "%02X", trade_party_flat[i]);
  60. if(i == 0xFC) {
  61. FURI_LOG_D(TAG, "[plist] part 1 end");
  62. plist_append(*pplist, 0xFF);
  63. }
  64. if(trade_party_flat[i] == 0xFE) {
  65. FURI_LOG_D(
  66. TAG, "[plist] patching byte 0x%02X, adding 0x%02X to plist", i, (i % 0xfc) + 1);
  67. plist_append(*pplist, (i % 0xfc) + 1);
  68. trade_party_flat[i] = 0xFF;
  69. }
  70. }
  71. FURI_LOG_D(TAG, "[plist] part 2 end");
  72. plist_append(*pplist, 0xFF);
  73. }