trade_patch_list.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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, PokemonData* pdata) {
  40. furi_assert(pdata);
  41. uint8_t* trade_party_flat = pdata->party;
  42. size_t 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. /* The first half of the patch list covers offsets 0x00 - 0xfb, which
  52. * is expressed as 0x01 - 0xfc. An 0xFF byte is added to signify the
  53. * end of the first part. The second half of the patch list covers
  54. * offsets 0xfc - 0x107 (more in gen ii). Which is expressed as
  55. * 0x01 - 0xc. A 0xFF byte is added to signify the end of the second part.
  56. */
  57. for(i = 0; i < pdata->party_sz; i++) {
  58. if(i == 0xFC) {
  59. FURI_LOG_D(TAG, "[plist] part 1 end");
  60. plist_append(*pplist, 0xFF);
  61. }
  62. if(trade_party_flat[i] == 0xFE) {
  63. FURI_LOG_D(
  64. TAG, "[plist] patching byte 0x%02X, adding 0x%02X to plist", i, (i % 0xfc) + 1);
  65. plist_append(*pplist, (i % 0xfc) + 1);
  66. trade_party_flat[i] = 0xFF;
  67. }
  68. }
  69. FURI_LOG_D(TAG, "[plist] part 2 end");
  70. plist_append(*pplist, 0xFF);
  71. }