Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "oot-music.h"
- #include <math.h>
- #include <stdbool.h>
- #include <stdint.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <emscripten/emscripten.h>
- #define true 1
- #define false 0
- #define max(a, b) (((a) > (b)) ? (a) : (b))
- #define min(a, b) (((a) < (b)) ? (a) : (b))
- #define abs(a) (((a) > 0) ? (a) : (-(a)))
- /********
- Sequence IDs
- ********/
- typedef struct Sequence_id {
- char* title;
- int id;
- const char* type;
- int size;
- } Sequence_id;
- const Sequence_id bgm_sequence_ids[] = {
- {"Hyrule Field", 0x02, "bgm"},
- {"Dodongos Cavern", 0x18, "bgm"},
- {"Kakariko Adult", 0x19, "bgm"},
- {"Battle", 0x1A, "bgm"},
- {"Boss Battle", 0x1B, "bgm"},
- {"Inside Deku Tree", 0x1C, "bgm"},
- {"Market", 0x1D, "bgm"},
- {"Title Theme", 0x1E, "bgm"},
- {"House", 0x1F, "bgm"},
- {"Jabu Jabu", 0x26, "bgm"},
- {"Kakariko Child", 0x27, "bgm"},
- {"Fairy Fountain", 0x28, "bgm"},
- {"Zelda Theme", 0x29, "bgm"},
- {"Fire Temple", 0x2A, "bgm"},
- {"Forest Temple", 0x2C, "bgm"},
- {"Castle Courtyard", 0x2D, "bgm"},
- {"Ganondorf Theme", 0x2E, "bgm"},
- {"Lon Lon Ranch", 0x2F, "bgm"},
- {"Goron City", 0x30, "bgm"},
- {"Miniboss Battle", 0x38, "bgm"},
- {"Temple of Time", 0x3A, "bgm"},
- {"Kokiri Forest", 0x3C, "bgm"},
- {"Lost Woods", 0x3E, "bgm"},
- {"Spirit Temple", 0x3F, "bgm"},
- {"Horse Race", 0x40, "bgm"},
- {"Ingo Theme", 0x42, "bgm"},
- {"Fairy Flying", 0x4A, "bgm"},
- {"Deku Tree", 0x4B, "bgm"},
- {"Windmill Hut", 0x4C, "bgm"},
- {"Shooting Gallery", 0x4E, "bgm"},
- {"Sheik Theme", 0x4F, "bgm"},
- {"Zoras Domain", 0x50, "bgm"},
- {"Shop", 0x55, "bgm"},
- {"Chamber of the Sages", 0x56, "bgm"},
- {"Ice Cavern", 0x58, "bgm"},
- {"Kaepora Gaebora", 0x5A, "bgm"},
- {"Shadow Temple", 0x5B, "bgm"},
- {"Water Temple", 0x5C, "bgm"},
- {"Gerudo Valley", 0x5F, "bgm"},
- {"Potion Shop", 0x60, "bgm"},
- {"Kotake and Koume", 0x61, "bgm"},
- {"Castle Escape", 0x62, "bgm"},
- {"Castle Underground", 0x63, "bgm"},
- {"Ganondorf Battle", 0x64, "bgm"},
- {"Ganon Battle", 0x65, "bgm"},
- {"Fire Boss", 0x6B, "bgm"},
- {"Mini-game", 0x6C, "bgm"},
- };
- const Sequence_id fanfare_sequence_ids[] = {
- {"Game Over", 0x20, "fanfare"},
- {"Boss Defeated", 0x21, "fanfare"},
- {"Item Get", 0x22, "fanfare"},
- {"Ganondorf Appears", 0x23, "fanfare"},
- {"Heart Container Get", 0x24, "fanfare"},
- {"Treasure Chest", 0x2B, "fanfare"},
- {"Spirit Stone Get", 0x32, "fanfare"},
- {"Heart Piece Get", 0x39, "fanfare"},
- {"Escape from Ranch", 0x3B, "fanfare"},
- {"Learn Song", 0x3D, "fanfare"},
- {"Epona Race Goal", 0x41, "fanfare"},
- {"Medallion Get", 0x43, "fanfare"},
- {"Zelda Turns Around", 0x51, "fanfare"},
- {"Master Sword", 0x53, "fanfare"},
- {"Door of Time", 0x59, "fanfare"},
- {"Ganons Rainbow Bridge", 0x5D, "fanfare"},
- };
- const Sequence_id ocarina_sequence_ids[] = {
- {"Prelude of Light", 0x25, "ocarina"},
- {"Bolero of Fire", 0x33, "ocarina"},
- {"Minuet of Forest", 0x34, "ocarina"},
- {"Serenade of Water", 0x35, "ocarina"},
- {"Requiem of Spirit", 0x36, "ocarina"},
- {"Nocturne of Shadow", 0x37, "ocarina"},
- {"Saria's Song", 0x44, "ocarina"},
- {"Epona's Song", 0x45, "ocarina"},
- {"Zelda's Lullaby", 0x46, "ocarina"},
- {"Sun's Song", 0x47, "ocarina"},
- {"Song of Time", 0x48, "ocarina"},
- {"Song of Storms", 0x49, "ocarina"},
- };
- const Sequence_id credit_sequence_ids[] = {
- {"Zeldas Theme Orchestra", 0x52, "credit"},
- {"Zeldas Ocarina Son", 0x66, "credit"},
- {"Ending Credits Part 4", 0x67, "credit"},
- {"Ending Credits Part 4", 0x68, "credit"},
- {"Ending Credits Part 4", 0x69, "credit"},
- {"Ending Credits Part 4", 0x6A, "credit"},
- };
- const Sequence_id fileselect_sequence_ids[] = {
- {"File Select", 0x57, "fileselect"},
- };
- Sequence_id* get_sequence_ids(bool bgm, bool fanfare, bool ocarina, bool credit, bool fileselect) {
- int size = 0;
- size += bgm ? sizeof(bgm_sequence_ids) : 0;
- size += fanfare ? sizeof(fanfare_sequence_ids) : 0;
- size += ocarina ? sizeof(ocarina_sequence_ids) : 0;
- size += credit ? sizeof(credit_sequence_ids) : 0;
- size += fileselect ? sizeof(fileselect_sequence_ids) : 0;
- Sequence_id* ids = (Sequence_id*)malloc(size);
- int ids_index = 0;
- if (bgm) {
- for (int i = 0; i < sizeof(bgm_sequence_ids) / sizeof(Sequence_id); i++) {
- ids[ids_index] = bgm_sequence_ids[i];
- ids_index++;
- }
- }
- if (fanfare) {
- for (int i = 0; i < sizeof(fanfare_sequence_ids) / sizeof(Sequence_id); i++) {
- ids[ids_index] = fanfare_sequence_ids[i];
- ids_index++;
- }
- }
- if (ocarina) {
- for (int i = 0; i < sizeof(ocarina_sequence_ids) / sizeof(Sequence_id); i++) {
- ids[ids_index] = ocarina_sequence_ids[i];
- ids_index++;
- }
- }
- if (credit) {
- for (int i = 0; i < sizeof(credit_sequence_ids) / sizeof(Sequence_id); i++) {
- ids[ids_index] = credit_sequence_ids[i];
- ids_index++;
- }
- }
- if (fileselect) {
- for (int i = 0; i < sizeof(fileselect_sequence_ids) / sizeof(Sequence_id); i++) {
- ids[ids_index] = fileselect_sequence_ids[i];
- ids_index++;
- }
- }
- ids->size = size;
- return ids;
- }
- /********
- Sound vector
- ********/
- void sound_Vector_Init(sound_Vector* vector) {
- vector->len = 0;
- vector->size = 64;
- vector->data = (sound*)malloc(vector->size); //ToDo: Does this make sense?
- if (vector->data == NULL) {
- vector->size = 0;
- }
- }
- bool sound_Vector_grow(sound_Vector* vector, int newSize) {
- int size_new = vector->size + newSize;
- if (vector->size == 0) {
- size_new = 64;
- }
- sound* data_new = (sound*)malloc(size_new);
- if (data_new == NULL) {
- return false;
- }
- if (vector->size > 0) {
- memcpy(data_new, vector->data, vector->size);
- }
- if (vector->data != NULL)
- free(vector->data);
- vector->data = data_new;
- vector->size = size_new;
- return true;
- }
- void sound_Vector_push(sound_Vector* vector, sound newSound) {
- if (!sound_Vector_grow(vector, sizeof(sound))) {
- return;
- }
- printf("[C] Adding sound to vector, not yet supported!\n"); //ToDo:
- vector->data[vector->len] = newSound;
- vector->len++;
- }
- sound sound_Vector_pop(sound_Vector* vector) {
- if (vector->len == 0) {
- sound empty;
- return empty;
- }
- sound val = vector->data[vector->len - 1];
- vector->len--;
- return val;
- }
- void sound_Vector_extend(sound_Vector* vector, const sound* new_data, int length) {
- int i = 0;
- while ((vector->len + length) >= vector->size) {
- int newSize = sizeof(sound);
- if (!sound_Vector_grow(vector, newSize)) {
- return;
- }
- i++;
- }
- memcpy(vector->data + vector->len, new_data, length);
- vector->len += length;
- }
- void sound_Vector_free(sound_Vector* vector) {
- if (vector->data != NULL) {
- for (int i = 0; i < vector->len; i++) {
- //free(vector->data[i].file); //ToDo: Not the responsibility of this code to clean external passed in data
- //free(vector->data[i].data);
- }
- free(vector->data);
- vector->data = NULL;
- }
- vector->size = 0;
- vector->len = 0;
- }
- /********
- Bank
- ********/
- typedef struct Bank {
- uint16_t index;
- char* meta;
- uint8_t* data;
- int data_length;
- sound_Vector zsounds;
- int offset;
- } Bank;
- void Bank_Init(Bank* bank, uint16_t index, char* meta, uint8_t* data) {
- bank->index = index;
- bank->meta = meta;
- bank->data = data;
- sound_Vector sounds;
- sound_Vector_Init(&sounds);
- bank->zsounds = sounds;
- bank->offset = 0;
- }
- void Bank_Free(Bank* bank) {
- sound_Vector_free(&bank->zsounds);
- }
- void Bank_add_zsound(Bank* bank, int tempaddr, sound zsound) {
- sound_Vector_push(&bank->zsounds, zsound);
- printf("[C] Adding zsound, not yet supported!\n"); //ToDo:
- }
- uint8_t* Bank_get_entry(Bank* bank, int offset) {
- //Size: 4 (offset) + 4 (datalen) + size of meta
- int entry_size = 8 + strlen(bank->meta);
- uint8_t* entry = (uint8_t*)malloc(entry_size);
- int datalen = bank->data_length;
- for (int i = 0; i < entry_size; i++) {
- //First 4 bytes
- if (i < 4) {
- entry[i] = (offset >> ((3 - i) * 8)) & 0xFF;
- }
- //Second 4 bytes
- else if (i < 8) {
- entry[i] = (datalen >> ((3 - (i - 4)) * 8)) & 0xFF;
- }
- //Rest of the bytes
- else {
- entry[i] = bank->meta[i - 8];
- }
- }
- return entry;
- }
- int Bank_get_entry_size(Bank* bank, int offset) {
- //Size: 4 (offset) + 4 (datalen) + size of meta
- int entry_size = 8 + strlen(bank->meta);
- return entry_size;
- }
- void Bank_update_zsound_pointers(Bank* bank) {
- for (int vectorIndex = 0; vectorIndex < bank->zsounds.len; vectorIndex++) {
- int datalen = bank->data_length;
- char addressBytes[4];
- addressBytes[0] = 0;
- addressBytes[1] = 0;
- for (int i = 0; i < 2; i++) {
- addressBytes[2 + i] = (bank->zsounds.data[vectorIndex].temp_addr >> ((1 - i) * 8)) & 0xFF;
- }
- //Loop through data and see if we find any matches for the address
- for (int dataIndex = 0; dataIndex < (datalen - 3); dataIndex++) {
- bool match = true;
- for (int i = 0; i < 4; i++) {
- if (!(bank->data[dataIndex + i] == addressBytes[i])) {
- match = false;
- break;
- }
- }
- //If we find a match, replace the bytes
- if (match) {
- for (int i = 0; i < 4; i++) {
- bank->data[dataIndex + i] = (bank->zsounds.data[vectorIndex].offset >> ((3 - i) * 8)) & 0xFF;
- }
- }
- }
- }
- }
- /********
- Bank vector
- ********/
- typedef struct Bank_Vector {
- Bank* data;
- int len;
- int size;
- } Bank_Vector;
- void Bank_Vector_Init(Bank_Vector* vector) {
- vector->len = 0;
- vector->size = 64;
- vector->data = (Bank*)malloc(vector->size); //ToDo: Unclear, should this allocate sizeof Bank structs, why just 64 bytes at the start?
- if (vector->data == NULL) {
- vector->size = 0;
- }
- }
- bool Bank_Vector_grow(Bank_Vector* vector, int newSize) {
- int size_new = vector->size + newSize;
- if (vector->size == 0) {
- size_new = 64;
- }
- Bank* data_new = (Bank*)malloc(size_new);
- if (data_new == NULL) {
- return false;
- }
- if (vector->size > 0) {
- memcpy(data_new, vector->data, vector->size);
- }
- if (vector->data != NULL)
- free(vector->data);
- vector->data = data_new;
- vector->size = size_new;
- return true;
- }
- void Bank_Vector_push(Bank_Vector* vector, Bank newBank) {
- if (!Bank_Vector_grow(vector, sizeof(Bank))) {
- return;
- }
- vector->data[vector->len] = newBank;
- vector->len++;
- }
- Bank Bank_Vector_pop(Bank_Vector* vector) {
- if (vector->len == 0) {
- Bank empty;
- return empty;
- }
- Bank val = vector->data[vector->len - 1];
- vector->len--;
- return val;
- }
- void Bank_Vector_extend(Bank_Vector* vector, const Bank* new_data, int length) {
- int i = 0;
- while ((vector->len + length) >= vector->size) {
- int newSize = sizeof(Bank);
- if (!Bank_Vector_grow(vector, newSize)) {
- return;
- }
- i++;
- }
- memcpy(vector->data + vector->len, new_data, length);
- vector->len += length;
- }
- void Bank_Vector_free(Bank_Vector* vector) {
- if (vector->data != NULL) {
- for (int i = 0; i < vector->len; i++) {
- //free(vector->data[i].data); //ToDo: Not the responsibility of this code to clean the passed in external data
- //free(vector->data[i].meta);
- Bank_Free(&vector->data[i]);
- }
- free(vector->data);
- vector->data = NULL;
- }
- vector->size = 0;
- vector->len = 0;
- }
- /********
- Sequence
- ********/
- void Sequence_Init(Sequence* sequence, char* name, char* cosmetic_name, int type, int instrument_set, char* instrument_set_str,
- int replaces, int vanilla_id, uint8_t* seq_file, int seq_file_length, bool new_instrument_set /*, Vector zsounds*/) {
- //Required params
- sequence->name = name;
- sequence->seq_file = seq_file;
- sequence->seq_file_length = seq_file_length;
- sequence->cosmetic_name = cosmetic_name;
- //Optional params
- sequence->type = type;
- if (sequence->type == 0) {
- sequence->type = 0x0202;
- }
- sequence->instrument_set = instrument_set;
- if (sequence->instrument_set == 0) {
- sequence->instrument_set = 0x03;
- }
- sequence->replaces = replaces;
- if (sequence->replaces == 0) {
- sequence->replaces = -1;
- }
- sequence->vanilla_id = vanilla_id;
- if (sequence->vanilla_id == 0) {
- sequence->vanilla_id = -1;
- }
- sequence->new_instrument_set = new_instrument_set;
- // sequence->zsounds = zsounds;
- // if (sequence->zsounds == NULL) {
- // Vector sounds;
- // bank->zsounds = Vector_Init(sounds);
- // }
- //Init as null
- sequence->zbank_file = NULL;
- sequence->bankmeta = NULL;
- //If we were passed a string instrument set, then we need to parse the instrument set from the string version
- if (instrument_set_str != NULL) {
- if (instrument_set_str[0] == '-') {
- sequence->new_instrument_set = true;
- } else {
- sequence->instrument_set = atoi(instrument_set_str);
- }
- }
- }
- void Sequence_copy(Sequence* copy, Sequence* original) {
- Sequence_Init(copy, original->name, original->cosmetic_name, original->type, original->instrument_set, NULL,
- original->replaces, original->vanilla_id, original->seq_file, original->seq_file_length, original->new_instrument_set);
- copy->zbank_file = original->zbank_file;
- copy->bankmeta = original->bankmeta;
- }
- Sequence* get_replacement_sequence(Sequence* sequences, int sequences_length, int id) {
- for (int i = 0; i < sequences_length; i++) {
- if (sequences[i].replaces == id) {
- return &sequences[i];
- }
- }
- return NULL;
- }
- /********
- SequenceData
- ********/
- typedef struct SequenceData {
- int address;
- int size;
- uint8_t* data;
- } SequenceData;
- void SequenceData_Init(SequenceData* sequence_data) {
- sequence_data->address = -1;
- sequence_data->size = -1;
- }
- /********
- Rom
- ********/
- #define RANDO_CONTEXT 0x03480000
- #define COSMETICS_CONTEXT_ADDR (RANDO_CONTEXT + 0x4)
- #define CFG_AUDIOBANK_TABLE_EXTENDED_ADDR_VERSION 0x1F073FDD
- #define CFG_AUDIOBANK_TABLE_EXTENDED_ADDR_OFFSET 0x64
- #define DMA_START 0x00007430
- #define DMA_SIZE 0x10
- #define AUDIOBANK_DMADATA_INDEX 3
- #define AUDIOSEQ_DMADATA_INDEX 4
- #define AUDIOTABLE_DMADATA_INDEX 5
- #define file_start AUDIOBANK_DMADATA_INDEX
- #define file_end AUDIOTABLE_DMADATA_INDEX
- typedef struct DMAEntry {
- int index;
- int start;
- int end;
- int size;
- } DMAEntry;
- typedef struct Rom {
- uint8_t* original;
- DMAEntry* dma_original;
- uint8_t* modified;
- DMAEntry* dma;
- //int dma_start;
- } Rom;
- const int rom_size = 0x4000000;
- //The python function has size as optional, but for our purposes size will always be provided
- int Rom_dma_freespace(Rom* rom, int size) {
- //Get the free space and sizes of all files
- int free_space_sizes[file_end - file_start + 1];
- int free_space_starts[file_end - file_start + 1];
- for (int i = file_start; i <= file_end; i++) {
- int end_current = ((rom->dma[i].end + 0x0F) >> 4) << 4;
- //If it's not the last entry, do bit stuff, othersize just get the length of the rom
- int start_next = i < AUDIOTABLE_DMADATA_INDEX ? ((rom->dma[i + 1].start + 0x0F) >> 4) << 4 : rom_size;
- if (end_current < start_next) {
- free_space_sizes[i - file_start] = start_next - end_current;
- free_space_starts[i - file_start] = end_current;
- }
- }
- //Find the smallest free space that fits the size requirement
- int chosen_index = -1;
- int smallest_size = 2147483647;
- int largest_size = 0; //For error printing
- for (int i = 0; i < (file_end - file_start + 1); i++) {
- int free_size = free_space_sizes[i];
- if (free_size >= size && free_size < smallest_size) {
- chosen_index = i;
- smallest_size = free_size;
- }
- if (free_size > largest_size) {
- largest_size = free_size;
- }
- }
- //No appropriate size found, throw error
- if (chosen_index == -1) {
- printf("[C]Error: Not enough free space in ROM to file a file of size %i. Largest region of free space available: %i.\n", size, largest_size);
- return -1;
- } else {
- return free_space_starts[chosen_index];
- }
- }
- bool Rom_dma_changed(Rom* rom, int entry) {
- DMAEntry changed = rom->dma[entry];
- DMAEntry original = rom->dma_original[entry];
- return changed.index != original.index || changed.start != original.start || changed.end != original.end || changed.size != original.size;
- }
- void Rom_dma_update(Rom* rom, int entry, int start, int end, int from_file) {
- //Set from_file if not provided
- if (from_file == 0) {
- //Check if this dma entry has been changed
- if (Rom_dma_changed(rom, entry)) {
- from_file = rom->dma[rom->dma[entry].index].start;
- } else {
- from_file = rom->dma[entry].start;
- }
- }
- int dma_location = DMA_START + rom->dma[entry].index * 0x10;
- //int dma_location = rom->dma_start + rom->dma[entry].index * 0x10;
- for (int i = 0; i < 16; i++) {
- //First 4 bytes: start
- if (i < 4) {
- rom->modified[dma_location + i] = (start >> (24 - ((3 - i) * 8))) & 0xFF;
- }
- //Second 4 bytes: end
- else if (i < 8) {
- rom->modified[dma_location + i] = (end >> (24 - ((3 - (i - 4)) * 8))) & 0xFF;
- }
- //Third 4 bytes: start again
- else if (i < 12) {
- rom->modified[dma_location + i] = (start >> (24 - ((3 - (i - 8)) * 8))) & 0xFF;
- }
- //Last 4 bytes: 0
- else {
- rom->modified[dma_location + i] = 0;
- }
- }
- rom->dma[entry].start = from_file;
- rom->dma[entry].end = start;
- rom->dma[entry].size = end - start;
- }
- uint16_t Rom_readint16(Rom* rom, int address, bool original) {
- uint16_t result = 0;
- for (int i = 0; i < 2; i++) {
- if (original) {
- result |= (rom->original[address + i] << ((1 - i) * 8));
- } else {
- result |= (rom->modified[address + i] << ((1 - i) * 8));
- }
- }
- return result;
- }
- void Rom_writeint16(Rom* rom, int address, uint16_t value) {
- for (int i = 0; i < 2; i++) {
- rom->modified[address + i] = (value >> (1 - i) * 8) & 0xFF;
- }
- }
- int Rom_readint32(Rom* rom, int address, bool original) {
- int result = 0;
- for (int i = 0; i < 4; i++) {
- if (original) {
- result |= (rom->original[address + i] << ((3 - i) * 8));
- } else {
- result |= (rom->modified[address + i] << ((3 - i) * 8));
- }
- }
- return result;
- }
- void Rom_writeint32(Rom* rom, int address, int value) {
- for (int i = 0; i < 4; i++) {
- rom->modified[address + i] = (value >> (3 - i) * 8) & 0xFF;
- }
- }
- /********
- Functions
- ********/
- // typedef struct Group {
- // char* name;
- // } Group;
- // typedef struct Groups {
- // char* name;
- // Group* groups;
- // } Groups;
- //Returns true if the specified sequence name is in the list of disabled sequences
- bool disabled_sequence(char* name, Sequence* disabled_sequences, int disabled_sequences_length) {
- for (int disabledIndex = 0; disabledIndex < disabled_sequences_length; disabledIndex++) {
- char* disabled_name = disabled_sequences[disabledIndex].name;
- if (strlen(name) == strlen(disabled_name)) {
- for (int i = 0; i < strlen(name); i++) {
- if (name[i] != disabled_name[i]) {
- break;
- }
- return true;
- }
- }
- }
- return false;
- }
- //This function is mainly file handling, not sure what this'll look like with how data will be passed
- //For now, I will write the part of this function that deals with the ROM and leave out the parts that deal with files
- //ToDo: Currently unused, will need malloc free fixes if it gets re-enabled
- /*
- void process_sequences(Rom rom, Sequence_id* ids, int ids_length, Sequence* disabled_source_sequences, int disabled_source_sequences_length,
- Sequence* disabled_target_sequences, int disabled_target_sequences_length, bool include_custom,
- Sequence* sequences, Sequence* target_sequences, Sequence_id* vanilla_ids) {
- //Initialize sizes for sequences
- sequences = (Sequence*)malloc(ids_length * sizeof(Sequence));
- target_sequences = (Sequence*)malloc(ids_length * sizeof(Sequence));
- for (int idIndex = 0; idIndex < ids_length; idIndex++) {
- //Initialize sequence data
- Sequence_id bgm = ids[idIndex];
- char* name = bgm.title;
- char* cosmetic_name = bgm.title;
- int id = bgm.id;
- //Get the 2-byte type from rom
- uint16_t type = Rom_readint16(&rom, 0xB89AE8 + (id * 0x10), false);
- uint8_t instrument_set = rom.modified[0xB89911 + 0xDD + (id * 2)];
- Sequence seq;
- Sequence_Init(&seq, name, cosmetic_name, type, instrument_set, NULL, 0, id, NULL, 0, NULL);
- Sequence target;
- Sequence_Init(&target, name, cosmetic_name, type, instrument_set, NULL, id, 0, NULL, 0, NULL);
- //Write the sequences into the array
- if (seq.vanilla_id != 0x57 && !disabled_sequence(cosmetic_name, disabled_source_sequences, disabled_source_sequences_length)) {
- sequences[idIndex] = seq;
- }
- if (!disabled_sequence(cosmetic_name, disabled_target_sequences, disabled_target_sequences_length)) {
- target_sequences[idIndex] = target;
- }
- }
- }
- */
- // typedef struct Mapping {
- // char* key;
- // char* value;
- // } Mapping;
- // typedef struct Symbol {
- // char* symbol;
- // int value;
- // } Symbol;
- // int find_symbol(Symbol* symbols, char* searchSymbol) {
- // int value = -1;
- // for (int i = 0; i < sizeof(symbols) / sizeof(Symbol); i++) {
- // bool match = true;
- // for (int j = 0; j < sizeof(symbols[i].symbol); j++) {
- // if (symbols[i].symbol[j] != searchSymbol[j]) {
- // match = false;
- // break;
- // }
- // }
- // if (match) {
- // value = symbols[i].value;
- // break;
- // }
- // }
- // return value;
- // }
- //First line of this one checks the rom.dma property, need to figure out how to do that
- void rebuild_sequences(Rom rom, Sequence* sequences, int sequences_length, int CFG_AUDIOBANK_TABLE_EXTENDED_ADDR) {
- bool CUSTOM_BANKS_SUPPORTED = CFG_AUDIOBANK_TABLE_EXTENDED_ADDR != -1;
- int audioseq_start = rom.dma[AUDIOSEQ_DMADATA_INDEX].start;
- int audioseq_end = rom.dma[AUDIOSEQ_DMADATA_INDEX].end;
- int audioseq_size = rom.dma[AUDIOSEQ_DMADATA_INDEX].size;
- SequenceData* old_sequences = (SequenceData*)malloc(0x6E * sizeof(SequenceData));
- for (int i = 0; i < 0x6E; i++) {
- //Set up SequenceData object
- SequenceData entry;
- SequenceData_Init(&entry);
- int entry_address = 0xB89AE0 + (i * 0x10);
- entry.address = Rom_readint32(&rom, entry_address, false);
- entry.size = Rom_readint32(&rom, entry_address + 4, false);
- //If size > 0, read the sequence data from the rom into the sequence object
- if (entry.size > 0) {
- entry.data = (uint8_t*)malloc(entry.size);
- for (int j = 0; j < entry.size; j++) {
- entry.data[j] = rom.modified[entry.address + audioseq_start + j];
- }
- } else {
- Sequence* seq = get_replacement_sequence(sequences, sequences_length, i);
- if (seq != NULL && (0 < entry.address && entry.address < 128)) {
- if (seq->replaces != 0x28) {
- seq->replaces = entry.address;
- } else {
- entry.size = old_sequences[0x57].size;
- entry.data = (uint8_t*)malloc(entry.size);
- memcpy(entry.data, old_sequences[0x57].data, entry.size);
- }
- }
- }
- //Add to old sequence list
- old_sequences[i] = entry;
- }
- SequenceData* new_sequences = (SequenceData*)malloc(0x6E * sizeof(SequenceData));
- int address = 0;
- for (int i = 0; i < 0x6E; i++) {
- //Set up SequenceData object
- SequenceData new_entry;
- SequenceData_Init(&new_entry);
- SequenceData old_sequence = old_sequences[i];
- if (old_sequence.size == 0) {
- new_entry.address = old_sequence.address;
- } else {
- new_entry.address = address;
- }
- //If size > 0, read the sequence data from the rom into the sequence object
- Sequence* seq = get_replacement_sequence(sequences, sequences_length, i);
- if (seq != NULL) {
- //Vanilla sequence: Get data from old sequences
- if (seq->vanilla_id != -1) {
- new_entry.size = old_sequences[seq->vanilla_id].size;
- new_entry.data = (uint8_t*)malloc(new_entry.size);
- memcpy(new_entry.data, old_sequences[seq->vanilla_id].data, new_entry.size);
- } else {
- //In the Python code, this is where the custom sequence data is read from the file
- //I think we are assuming here that the custom data has already been read into the sequence list, however
- //So where in the python code seq_file is the name of the file, here I'm using it as the content of the file
- new_entry.size = seq->seq_file_length;
- new_entry.data = (uint8_t*)malloc(new_entry.size);
- memcpy(new_entry.data, seq->seq_file, new_entry.size);
- }
- } else {
- new_entry.size = old_sequence.size;
- new_entry.data = (uint8_t*)malloc(new_entry.size);
- memcpy(new_entry.data, old_sequence.data, new_entry.size);
- }
- //Concatenate the full audio sequence and the new sequence data
- if (new_entry.size > 0) {
- //Align sequences to 0x10
- if (new_entry.size % 0x10 != 0) {
- //Extend size of data
- int difference = 0x10 - (new_entry.size % 0x10);
- new_entry.data = (uint8_t*)realloc(new_entry.data, new_entry.size + difference);
- //Pad out with 0s
- for (int j = new_entry.size; j < new_entry.size + difference; j++) {
- new_entry.data[j] = 0;
- }
- new_entry.size += difference;
- }
- //Increment the current address by the size of the new sequence
- address += new_entry.size;
- }
- //Add to new sequence list
- //In the original code this is before the previous stanza, but I think with Python "pointers", this is meant to store the version of the entry with the padded size?
- new_sequences[i] = new_entry;
- }
- //Combine the data into a big sequence
- //Address has been keeping track of the necessary size for us
- uint8_t* new_audio_sequence = (uint8_t*)malloc(address);
- address = 0;
- for (int i = 0; i < 0x6E; i++) {
- SequenceData entry = new_sequences[i];
- for (int j = 0; j < entry.size; j++) {
- new_audio_sequence[address] = entry.data[j];
- address++;
- }
- }
- //Find a value for the new address
- int new_address = audioseq_start;
- //Adjust the DMA table if necessary
- if (address > audioseq_size) {
- //Zero out the old audio sequence
- for (int i = audioseq_start; i < audioseq_end; i++) {
- rom.modified[i] = 0;
- }
- //Find free space and update dmatable
- new_address = Rom_dma_freespace(&rom, address);
- Rom_dma_update(&rom, AUDIOSEQ_DMADATA_INDEX, new_address, new_address + address, 0);
- }
- //Write new audio sequence file
- for (int i = 0; i < address; i++) {
- rom.modified[new_address + i] = new_audio_sequence[i];
- }
- int fanfare_bank_shift = CUSTOM_BANKS_SUPPORTED ? 0x26 : 0;
- //Update pointer table
- for (int i = 0; i < 0x6E; i++) {
- Rom_writeint32(&rom, 0xB89AE0 + (i * 0x10), new_sequences[i].address);
- Rom_writeint32(&rom, 0xB89AE0 + (i * 0x10) + 4, new_sequences[i].size);
- //Sequence* seq = get_replacement_sequence(sequences, sequences_length, i); //ToDo: No point to this?
- }
- //Update instrument sets
- Sequence_id* sequence_ids = get_sequence_ids(true, true, true, true, true);
- for (int i = 0; i < sequence_ids->size / sizeof(Sequence_id); i++) {
- Sequence_id seqId = sequence_ids[i];
- //Get the replacement sequeunce
- int id = seqId.id;
- if (new_sequences[id].size == 0) {
- id = new_sequences[id].address;
- }
- Sequence* seq = get_replacement_sequence(sequences, sequences_length, id);
- //If one was found, then write the new instrument set
- if (seq != NULL) {
- int set = seq->instrument_set;
- if (strncmp(seqId.type, "fanfare", 7) == 0 || strncmp(seqId.type, "ocarina", 7) == 0) {
- set += fanfare_bank_shift;
- }
- rom.modified[0xB89911 + 0xDD + (seqId.id * 2)] = set;
- }
- }
- //Patch new instrument sets (banks) and add new instrument sounds
- //Only if we were passed CFG_AUDIOBANK_TABLE_EXTENDED_ADDR via symbols which means we're on the right version.
- if (!CUSTOM_BANKS_SUPPORTED) {
- goto cleanup;
- }
- //Builds new audio bank entrys for fanfares to prevent fanfares killing bgm in areas like Goron City
- int bank_index_base = Rom_readint32(&rom, CFG_AUDIOBANK_TABLE_EXTENDED_ADDR, false) - 0x80400000 + 0x03480000;
- //Build new fanfare banks by copying each entry in audiobank_index
- for (int i = 0; i < 0x26; i++) {
- uint8_t bank_entry[10];
- for (int j = 0; j < 10; j++) {
- bank_entry[j] = rom.modified[bank_index_base + 0x10 + 0x10 * i + j]; //Get the vanilla entry
- }
- bank_entry[9] = 1; //Update the cache type to 1
- for (int j = 0; j < 10; j++) {
- rom.modified[bank_index_base + 0x270 + 0x10 * i + j] = bank_entry[j]; //Write the new entry at the end of the bank table.
- }
- }
- rom.modified[bank_index_base + 0x01] = 0x4C; //Updates AudioBank Index Header if no custom banks are present as this would be 0x26 which would crash the game if a fanfare was played
- Bank_Vector added_banks;
- Bank_Vector_Init(&added_banks);
- sound_Vector added_instruments;
- sound_Vector_Init(&added_instruments);
- uint8_t* instr_data;
- int instr_data_size = 0;
- uint16_t new_bank_index = 0x4C;
- int audiobank_start = rom.dma[AUDIOBANK_DMADATA_INDEX].start;
- int audiobank_size = rom.dma[AUDIOBANK_DMADATA_INDEX].size;
- int audiotable_start = rom.dma[AUDIOTABLE_DMADATA_INDEX].start;
- int audiotable_size = rom.dma[AUDIOTABLE_DMADATA_INDEX].size;
- int instr_offset_in_file = audiobank_size;
- int bank_table_base = 0;
- //Process each sequence
- for (int sequenceIndex = 0; sequenceIndex < 0x6E; sequenceIndex++) {
- int bank_table_base = Rom_readint32(&rom, CFG_AUDIOBANK_TABLE_EXTENDED_ADDR, false) - 0x80400000 + 0x3480000;
- int replacement_id = sequenceIndex;
- if (new_sequences[sequenceIndex].size <= 0) {
- replacement_id = new_sequences[sequenceIndex].address;
- }
- Sequence* seq = get_replacement_sequence(sequences, sequences_length, replacement_id);
- if (seq != NULL && seq->new_instrument_set) {
- //See if this bank has already been added
- bool alreadyAdded = false;
- uint16_t seq_bank_index = -1;
- for (int bankIndex = 0; bankIndex < added_banks.len; bankIndex++) {
- bool match = true;
- //Check if the data is found in the added banks list already
- if (added_banks.data[bankIndex].data_length == seq->zbank_file_length) {
- for (int i = 0; i < seq->zbank_file_length; i++) {
- if (added_banks.data[bankIndex].data[i] != seq->zbank_file[i]) {
- match = false;
- break;
- }
- }
- } else {
- match = false;
- }
- if (match) {
- alreadyAdded = true;
- seq_bank_index = added_banks.data[bankIndex].index;
- break;
- }
- }
- if (!alreadyAdded) {
- Bank bank;
- Bank_Init(&bank, new_bank_index, seq->bankmeta, seq->zbank_file);
- //Handle any new instruments
- for (int zsoundIndex = 0; zsoundIndex < seq->zsounds.size; zsoundIndex++) {
- sound zsound = seq->zsounds.data[zsoundIndex];
- uint16_t tempaddr = zsound.temp_addr;
- uint8_t* curr_instrument_data = zsound.data;
- int curr_instrument_data_size = zsound.size;
- //See if this instrument has already been added
- int addedInstrIndex = -1;
- for (int instrIndex = 0; instrIndex < added_instruments.len; instrIndex++) {
- bool match = true;
- //Check if the data is found in the added banks list already
- if (added_instruments.data[instrIndex].size == zsound.size) {
- for (int i = 0; i < zsound.size; i++) {
- if (added_instruments.data[instrIndex].data[i] != zsound.data[i]) {
- match = false;
- break;
- }
- }
- } else {
- match = false;
- }
- if (match) {
- addedInstrIndex = instrIndex;
- break;
- }
- }
- //Already added this instrument. Just add it to the bank
- if (addedInstrIndex != -1) {
- Bank_add_zsound(&bank, tempaddr, zsound);
- } else {
- //Add current to the total instrument data and pad out to 0x10
- int prevSize = instr_data_size;
- int nextSize = prevSize + curr_instrument_data_size;
- int padDifference = nextSize % 10 == 0 ? 0 : 0x10 - (nextSize % 0x10);
- instr_data_size = nextSize + padDifference;
- instr_data = (uint8_t*)realloc(instr_data, instr_data_size);
- for (int i = 0; i < curr_instrument_data_size + padDifference; i++) {
- if (i < curr_instrument_data_size) {
- instr_data[prevSize + i] = curr_instrument_data[i];
- } else { //Pad out the rest with 0s
- instr_data[prevSize + i] = 0;
- }
- }
- zsound.size = curr_instrument_data_size + padDifference;
- zsound.offset = instr_offset_in_file;
- instr_offset_in_file += zsound.size;
- Bank_add_zsound(&bank, tempaddr, zsound);
- sound_Vector_push(&added_instruments, zsound);
- }
- }
- seq_bank_index = bank.index;
- Bank_Vector_push(&added_banks, bank);
- new_bank_index++;
- }
- //Write a single byte for the bank index, although it is a 2 byte value elsewhere
- rom.modified[0xB89911 + 0xDD + (sequenceIndex * 2)] = seq_bank_index & 0xFF;
- }
- }
- //Patch the new instrument data into the ROM in a new file.
- //If there is any instrument data to add, move the entire audiotable file to a new location in the ROM.
- if (instr_data_size > 0) {
- //Read in original audiotable data and zero out existing file
- int audiotable_data_size = audiotable_size + instr_data_size;
- uint8_t* audiotable_data = (uint8_t*)malloc(audiotable_data_size);
- for (int i = 0; i < audiobank_size; i++) {
- audiotable_data[i] = rom.modified[audiotable_start + i];
- rom.modified[audiotable_start + i] = 0;
- }
- //Add the new data
- for (int i = 0; i < instr_data_size; i++) {
- audiotable_data[audiotable_size + i] = instr_data[i];
- }
- //Get new address for the file
- int new_audiotable_start = Rom_dma_freespace(&rom, audiotable_data_size);
- //Write the file to the new address
- for (int i = 0; i < audiotable_data_size; i++) {
- rom.modified[new_audiotable_start + i] = audiotable_data[i];
- }
- //Update DMA
- Rom_dma_update(&rom, AUDIOTABLE_DMADATA_INDEX, new_audiotable_start, new_audiotable_start + audiotable_data_size, 0);
- //Cleanup audiotable
- free(audiotable_data);
- }
- //Add new audio banks
- uint8_t* new_bank_data;
- //Read the original audiobank data
- uint8_t* audiobank_data = (uint8_t*)malloc(audiobank_size);
- for (int i = 0; i < audiobank_size; i++) {
- audiobank_data[i] = rom.modified[audiobank_start + i];
- }
- int new_bank_offset = audiobank_size;
- int new_bank_size = 0;
- for (int bankIndex = 0; bankIndex < added_banks.len; bankIndex++) {
- //Write bank data to rom
- Bank bank = added_banks.data[bankIndex];
- Bank_update_zsound_pointers(&bank);
- bank.offset = new_bank_offset;
- uint8_t* bank_entry = Bank_get_entry(&bank, new_bank_offset);
- int bank_entry_size = Bank_get_entry_size(&bank, new_bank_offset);
- for (int i = 0; i < bank_entry_size; i++) {
- rom.modified[bank_table_base + 0x10 + bank.index * 0x10 + i] = bank_entry[i];
- }
- //Add to total new bank data
- int prevSize = new_bank_size;
- int nextSize = prevSize + bank_entry_size;
- new_bank_data = (uint8_t*)realloc(new_bank_data, nextSize);
- for (int i = 0; i < bank_entry_size; i++) {
- new_bank_data[prevSize + i] = bank_entry[i];
- }
- new_bank_offset += nextSize;
- new_bank_size = nextSize;
- //Clean bank entry
- free(bank_entry);
- }
- //If there is any audiobank data to add, move the entire audiobank file to a new place in ROM. Update the existing dmadata record
- if (new_bank_size > 0) {
- //Zero out existing file
- for (int i = 0; i < audiobank_size; i++) {
- rom.modified[audiobank_start + i] = 0;
- }
- //Add the new data
- int prevSize = audiobank_size;
- int nextSize = prevSize + new_bank_size;
- audiobank_data = (uint8_t*)realloc(audiobank_data, nextSize);
- for (int i = 0; i < new_bank_size; i++) {
- audiobank_data[prevSize + i] = new_bank_data[i];
- }
- //Get new address for the file
- int new_audio_banks_addr = Rom_dma_freespace(&rom, nextSize);
- //Write the file to the new address
- for (int i = 0; i < nextSize; i++) {
- rom.modified[new_audio_banks_addr + i] = audiobank_data[i];
- }
- //Update DMA
- Rom_dma_update(&rom, AUDIOBANK_DMADATA_INDEX, new_audio_banks_addr, new_audio_banks_addr + nextSize, 0);
- //Update size of bank table in the Audiobank table header
- Rom_writeint16(&rom, bank_table_base, new_bank_index);
- }
- //Update the init heap size. This size is normally hardcoded based on the number of audio banks.
- int init_heap_size = Rom_readint32(&rom, 0xB80118, false);
- init_heap_size += (new_bank_index - 0x26) * 0x20;
- Rom_writeint32(&rom, 0xB80118, init_heap_size);
- //Cleanup buffers
- if (new_bank_data != NULL)
- free(new_bank_data);
- free(audiobank_data);
- if (instr_data != NULL)
- free(instr_data);
- //Cleanup vectors
- Bank_Vector_free(&added_banks);
- sound_Vector_free(&added_instruments);
- //Global cleanup
- cleanup:
- free(sequence_ids);
- free(new_audio_sequence);
- //ToDo: Clean old_sequences and new_sequences data first
- for (int i = 0; i < 0x6E; i++) {
- SequenceData entry = new_sequences[i];
- if (entry.data != NULL)
- free(entry.data);
- }
- free(new_sequences);
- for (int i = 0; i < 0x6E; i++) {
- SequenceData entry = old_sequences[i];
- if (entry.data != NULL)
- free(entry.data);
- }
- free(old_sequences);
- }
- void rebuild_pointers_table(Rom rom, Sequence* sequences, int sequences_length) {
- for (int sequenceIndex = 0; sequenceIndex < sequences_length; sequenceIndex++) {
- Sequence sequence = sequences[sequenceIndex];
- //Get the original values
- uint8_t bgm_sequence[0x10];
- for (int i = 0; i < 0x10; i++) {
- bgm_sequence[i] = rom.original[0xB89AE0 + (sequence.vanilla_id * 0x10) + i];
- }
- uint8_t instr_1 = rom.original[0xB89910 + 0xDD + (sequence.vanilla_id * 2)];
- uint8_t instr_2 = rom.original[0xB89910 + 0xDD + (sequence.vanilla_id * 2) + 1];
- //Replace the values at the addresses we want
- for (int i = 0; i < 0x10; i++) {
- rom.modified[0xB89AE0 + (sequence.replaces * 0x10) + i] = bgm_sequence[i];
- }
- rom.modified[0xB89910 + 0xDD + (sequence.replaces * 2)] = instr_1;
- rom.modified[0xB89910 + 0xDD + (sequence.replaces * 2) + 1] = instr_2;
- }
- //Special handling for fairy fountain file select music
- uint8_t fairy_1 = rom.modified[0xB89910 + 0xDD + (0x28 * 2)]; //Based on the python code looks like this reads from the modified rather than the original
- uint8_t fairy_2 = rom.modified[0xB89910 + 0xDD + (0x28 * 2) + 1];
- rom.modified[0xB89910 + 0xDD + (0x57 * 2)] = fairy_1;
- rom.modified[0xB89910 + 0xDD + (0x57 * 2) + 1] = fairy_2;
- }
- void disable_music(Rom rom, int* ids, int ids_length) {
- uint8_t blank_track[0x10];
- //First track is blank, extract from rom
- for (int i = 0; i < 0x10; i++) {
- blank_track[i] = rom.modified[0xB89AE0 + i];
- }
- for (int idIndex = 0; idIndex < ids_length; idIndex++) {
- for (int i = 0; i < 0x10; i++) {
- rom.modified[0xB89AE0 + (ids[idIndex] * 0x10) + i] = blank_track[i];
- }
- }
- }
- void restore_music(Rom rom) {
- //Restore all music from original
- Sequence_id* sequence_ids = get_sequence_ids(true, true, true, true, true);
- for (int i = 0; i < sequence_ids->size / sizeof(Sequence_id); i++) {
- Sequence_id bgm = sequence_ids[i];
- //Get the original values
- uint8_t bgm_sequence[0x10];
- for (int i = 0; i < 0x10; i++) {
- bgm_sequence[i] = rom.original[0xB89AE0 + (bgm.id * 0x10) + i];
- }
- uint8_t instr_1 = rom.original[0xB89910 + 0xDD + (bgm.id * 2)];
- uint8_t instr_2 = rom.original[0xB89910 + 0xDD + (bgm.id * 2) + 1];
- //Replace the values at the addresses we want
- for (int i = 0; i < 0x10; i++) {
- rom.modified[0xB89AE0 + (bgm.id * 0x10) + i] = bgm_sequence[i];
- rom.modified[0xB89910 + 0xDD + (bgm.id * 2)] = instr_1;
- rom.modified[0xB89910 + 0xDD + (bgm.id * 2) + 1] = instr_2;
- }
- //Restore file select instrument
- uint8_t file_1 = rom.original[0xB89910 + 0xDD + (0x57 * 2)]; //Based on the python code looks like this reads from the modified rather than the original
- uint8_t file_2 = rom.original[0xB89910 + 0xDD + (0x57 * 2) + 1];
- rom.modified[0xB89910 + 0xDD + (0x57 * 2)] = file_1;
- rom.modified[0xB89910 + 0xDD + (0x57 * 2) + 1] = file_2;
- //Rebuild audioseq
- int orig_start = rom.dma_original[AUDIOSEQ_DMADATA_INDEX].start;
- int orig_end = rom.dma_original[AUDIOSEQ_DMADATA_INDEX].end;
- //Check if this needs to be <= instead of <
- for (int i = orig_start; i < orig_end; i++) {
- rom.modified[i] = rom.original[i];
- }
- //If audioseq was relocated
- int start = rom.dma[AUDIOSEQ_DMADATA_INDEX].start;
- if (start != orig_start) {
- //Zero out old audioseq
- for (int i = start; i < rom.dma[AUDIOSEQ_DMADATA_INDEX].end; i++) {
- rom.modified[i] = 0;
- }
- Rom_dma_update(&rom, AUDIOSEQ_DMADATA_INDEX, orig_start, orig_end, start);
- }
- }
- //Cleanup
- free(sequence_ids);
- }
- // void randomize_music(uint8_t* rom_original, uint8_t* rom_modified, Sequence* sequences, int sequences_length, Sequence* target_sequences, int target_sequences_length,
- // Sequence* disabled_source_sequences, int disabled_source_sequences_length, Sequence* disabled_target_sequences, int disabled_target_sequences_length,
- // bool bgm_random, bool fanfare_random, bool ocarina_random, bool credit_random, bool fileselect_random, bool custom_sequences) {
- int randomize_music(uint8_t* rom_original, uint8_t* rom_modified, Sequence* sequences, int sequences_length, int* disabled_target_sequences, int disabled_target_sequences_length, char custom_sequences) {
- printf("[C] 1\n");
- //Copy raw rom data into rom struct
- Rom rom;
- rom.original = rom_original;
- rom.modified = rom_modified;
- //Get DMA for AUDIOSEQ_DMADATA
- DMAEntry seq_original;
- seq_original.index = AUDIOSEQ_DMADATA_INDEX;
- seq_original.start = Rom_readint32(&rom, DMA_START + DMA_SIZE * AUDIOSEQ_DMADATA_INDEX, true);
- seq_original.end = Rom_readint32(&rom, DMA_START + DMA_SIZE * AUDIOSEQ_DMADATA_INDEX + 4, true);
- seq_original.size = seq_original.end - seq_original.start;
- DMAEntry seq_modified;
- seq_modified.index = AUDIOSEQ_DMADATA_INDEX;
- seq_modified.start = Rom_readint32(&rom, DMA_START + DMA_SIZE * AUDIOSEQ_DMADATA_INDEX, false);
- seq_modified.end = Rom_readint32(&rom, DMA_START + DMA_SIZE * AUDIOSEQ_DMADATA_INDEX + 4, false);
- seq_modified.size = seq_modified.end - seq_modified.start;
- //Get DMA for AUDIOBANK_DMADATA
- DMAEntry bank_original;
- bank_original.index = AUDIOBANK_DMADATA_INDEX;
- bank_original.start = Rom_readint32(&rom, DMA_START + DMA_SIZE * AUDIOBANK_DMADATA_INDEX, true);
- bank_original.end = Rom_readint32(&rom, DMA_START + DMA_SIZE * AUDIOBANK_DMADATA_INDEX + 4, true);
- bank_original.size = bank_original.end - bank_original.start;
- DMAEntry bank_modified;
- bank_modified.index = AUDIOSEQ_DMADATA_INDEX;
- bank_modified.start = Rom_readint32(&rom, DMA_START + DMA_SIZE * AUDIOBANK_DMADATA_INDEX, false);
- bank_modified.end = Rom_readint32(&rom, DMA_START + DMA_SIZE * AUDIOBANK_DMADATA_INDEX + 4, false);
- bank_modified.size = bank_modified.end - bank_modified.start;
- //Get DMA for AUDIOTABLE_DMADATA
- DMAEntry table_original;
- table_original.index = AUDIOTABLE_DMADATA_INDEX;
- table_original.start = Rom_readint32(&rom, DMA_START + DMA_SIZE * AUDIOTABLE_DMADATA_INDEX, true);
- table_original.end = Rom_readint32(&rom, DMA_START + DMA_SIZE * AUDIOTABLE_DMADATA_INDEX + 4, true);
- table_original.size = table_original.end - table_original.start;
- DMAEntry table_modified;
- table_modified.index = AUDIOSEQ_DMADATA_INDEX;
- table_modified.start = Rom_readint32(&rom, DMA_START + DMA_SIZE * AUDIOTABLE_DMADATA_INDEX, false);
- table_modified.end = Rom_readint32(&rom, DMA_START + DMA_SIZE * AUDIOTABLE_DMADATA_INDEX + 4, false);
- table_modified.size = table_modified.end - table_modified.start;
- //Insert DMA entries into the rom struct
- rom.dma_original = (DMAEntry*)malloc(sizeof(DMAEntry) * (file_end + 1));
- rom.dma_original[AUDIOSEQ_DMADATA_INDEX] = seq_original;
- rom.dma_original[AUDIOBANK_DMADATA_INDEX] = bank_original;
- rom.dma_original[AUDIOTABLE_DMADATA_INDEX] = table_original;
- rom.dma = (DMAEntry*)malloc(sizeof(DMAEntry) * (file_end + 1));
- rom.dma[AUDIOSEQ_DMADATA_INDEX] = seq_modified;
- rom.dma[AUDIOBANK_DMADATA_INDEX] = bank_modified;
- rom.dma[AUDIOTABLE_DMADATA_INDEX] = table_modified;
- //Handle CFG_AUDIOBANK_TABLE_EXTENDED_ADDR
- int CFG_AUDIOBANK_TABLE_EXTENDED_ADDR = -1;
- int cosmetic_context = Rom_readint32(&rom, COSMETICS_CONTEXT_ADDR, false) - 0x80400000 + 0x03480000;
- //Check if the cosmetics version is high enough
- int cosmetics_version = Rom_readint32(&rom, cosmetic_context, false);
- if (cosmetics_version >= CFG_AUDIOBANK_TABLE_EXTENDED_ADDR_VERSION) {
- //It is, so read in the value for the address
- int CFG_AUDIOBANK_TABLE_EXTENDED_ADDR = Rom_readint32(&rom, cosmetic_context + CFG_AUDIOBANK_TABLE_EXTENDED_ADDR_OFFSET, false);
- }
- printf("[C] 2\n");
- //Business logic for calling each function
- restore_music(rom);
- printf("[C] 3\n");
- // if (bgm_random) {
- // process_sequences(rom, get_sequence_ids(true, false, false, credit_random, false), disabled_source_sequences, disabled_source_sequences_length,
- // disabled_target_sequences, disabled_target_sequences_length, custom_sequences, sequences, sequences_length, target_sequences, target_sequences_length);
- // }
- // if (fanfare_random) {
- // process_sequences(rom, get_sequence_ids(true, false, ocarina_random, false, false), disabled_source_sequences, disabled_source_sequences_length,
- // disabled_target_sequences, disabled_target_sequences_length, custom_sequences, sequences, sequences_length, target_sequences, target_sequences_length);
- // }
- if (custom_sequences) {
- rebuild_sequences(rom, sequences, sequences_length, CFG_AUDIOBANK_TABLE_EXTENDED_ADDR);
- } else {
- rebuild_pointers_table(rom, sequences, sequences_length);
- }
- printf("[C] 4\n");
- //Version where disabled_target_sequences is a list of Sequence
- // if (disabled_target_sequences_length > 0) {
- // int* disabled_ids = (int*)malloc(sizeof(int) * disabled_target_sequences_length);
- // for (int i = 0; i < disabled_target_sequences_length; i++) {
- // disabled_ids[i] = disabled_target_sequences[i].vanilla_id;
- // }
- // disable_music(rom, disabled_ids, disabled_target_sequences_length);
- // }
- //Version where disabled_target_sequences is a list of int
- if (disabled_target_sequences_length > 0) {
- disable_music(rom, disabled_target_sequences, disabled_target_sequences_length);
- }
- //Cleanup
- free(rom.dma_original);
- free(rom.dma);
- printf("[C] 5\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement