patch_list.c 2.3 KB

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