From fa25bb37426c49f27ab835ead07b33ebb112d6d6 Mon Sep 17 00:00:00 2001 From: Landon & Emma <80786423+LandonAndEmma@users.noreply.github.com> Date: Sun, 7 Apr 2024 20:55:00 -0400 Subject: [PATCH] Update main.cpp --- main.cpp | 204 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 117 insertions(+), 87 deletions(-) diff --git a/main.cpp b/main.cpp index 68aa8c0..0021fb4 100644 --- a/main.cpp +++ b/main.cpp @@ -1,115 +1,145 @@ -#include +#include #include +#include #include #include +#include using namespace std; -const int TRACK_COUNT = 53; -const int OFFSET = 1388909; - -// Structure to hold track information -struct Track { - string name; - int seqValue; -}; - -// Function to load track names and default sequence values -vector loadTracks() { - vector tracks(TRACK_COUNT); - // Initialize track names and default sequence values - tracks[0] = {"Unknown", 0}; - tracks[1] = {"Course Intro 2", 0}; - tracks[2] = {"Course Intro 1", 0}; - // Add more tracks... - return tracks; -} - -// Function to read file contents into a string -string getFileContents(const string& filename) { - ifstream file(filename, ios::in | ios::binary); - if (!file.is_open()) { - cerr << "Error: Unable to open file " << filename << endl; +// Function to read file contents +string getFileContents(const string &filename) { + ifstream in(filename, ios::in | ios::binary); + if (in) { + ostringstream contents; // Correctly declared ostringstream + contents << in.rdbuf(); + in.close(); + return contents.str(); + } else { + cerr << "Failed to open file: " << filename << endl; + cin.ignore(); exit(1); } - // Read file contents into a string - return string((istreambuf_iterator(file)), istreambuf_iterator()); } -// Function to save string contents into a file -void saveFileContents(const string& filename, const string& contents) { - ofstream file(filename, ios::binary); - if (!file.is_open()) { - cerr << "Error: Unable to save file " << filename << endl; +// Function to get the path of the ARM9 binary file +string getArm9BinPath() { + OPENFILENAME ofn; + char szFile[260] = {0}; + + ZeroMemory(&ofn, sizeof(ofn)); + ofn.lStructSize = sizeof(ofn); + ofn.hwndOwner = NULL; + ofn.lpstrFile = szFile; + ofn.lpstrFile[0] = '\0'; + ofn.nMaxFile = sizeof(szFile); + ofn.lpstrFilter = "Binary Files (*.bin)\0*.bin\0All Files (*.*)\0*.*\0"; + ofn.nFilterIndex = 1; + ofn.lpstrFileTitle = NULL; + ofn.nMaxFileTitle = 0; + ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR; + + if (GetOpenFileName(&ofn) == TRUE) { + return string(szFile); + } else { + cerr << "No file selected." << endl; + cin.ignore(); exit(1); } - // Write string contents into a file - file << contents; -} - -// Function to print track names and their corresponding sequence values -void printTracks(const vector& tracks) { - for (int i = 0; i < TRACK_COUNT; ++i) { - cout << i << ") " << tracks[i].name << " [" << tracks[i].seqValue << "]" << endl; - } } int main() { - // Load track information - vector tracks = loadTracks(); - // Read arm9.bin file into a string - string arm9Data = getFileContents("arm9.bin"); - // Extract sequence values from arm9.bin data - vector arm9Values(arm9Data.begin() + OFFSET, arm9Data.begin() + OFFSET + TRACK_COUNT * 4); - - // Prompt user to start the program - cout << "Mario Kart DS ARM9 Music Table Editor" << endl; - cout << "Press ENTER to start the program." << endl; + const int offset = 1388909; + int selected; + int newValue; + char choice; + + // Track names + vector tracks = { + "Unknown", "Course Intro 2", "Course Intro 1", "Course Intro 3", "Course Intro 1", "Battle Mode Intro", + "Boss Intro", "Figure-8 Circuit", "GCN Luigi Circuit", "GCN Yoshi Circuit", "Cheep Cheep Beach", + "Yoshi Falls", "GCN Baby Park", "N64 Moo Moo Farm", "N64 Frappe Snowland", "Delfino Sqare", + "Airship Fortress", "Wario Stadium", "GCN Mushroom Bridge", "Peach Gardens", "Luigi's Mansion", + "SNES Mario Circuit 1", "SNES Koopa Beach 2", "SNES Donut Plains 1", "SNES Choco Island 2", + "GBA Peach Circuit", "GBA Luigi Circuit", "Shroom Ridge", "N64 Choco Mountain", "N64 Banshee Boardwalk", + "DK Pass", "Desert Hills", "Waluigi Pinball", "Tick-Tock Clock", "Mario Circuit", "Rainbow Road", + "GBA Bowser Castle 2", "Bowser Castle", "GBA Sky Garden", "Battle Stage Theme", "Boss Battle Theme", + "Jingle", "GP Results", "Credits", "Credits True", "Wi-Fi Menu", "Multiplayer Menu", "Records Menu", + "Options Menu", "Intro", "Singleplayer Menu", "Unknown", "Mario Circuit" + }; + + cout << "Mario Kart DS ARM9 Music Table Editor by Ermelber and Landon & Emma\n\nPress any key to start the program.\n"; cin.ignore(); - char choice; + string arm9BinPath = getArm9BinPath(); + string text = getFileContents(arm9BinPath); + vector armValues(211); + for (int i = offset; i < min(text.size(), offset + 211); i++) + armValues[i - offset] = static_cast(text[i]); + + int seqIndex; // Declare seqIndex outside the loop + do { - // Display track names and sequence values - printTracks(tracks); + for (size_t i = 0; i < tracks.size(); i++) { + cout << i << ") " << tracks[i] << " [" << armValues[i * 4] << "]" << endl; + } - int selected; - // Prompt user to select a track slot do { - cout << "\nSelect a slot to change [0.." << TRACK_COUNT - 1 << "]: "; + cout << "\n\nSelect a track to change [0.." << tracks.size() - 1 << "]: "; cin >> selected; - } while (selected < 0 || selected >= TRACK_COUNT); - - // Prompt user to confirm editing of the selected track - cout << "\nDo you want to change Slot " << selected << "'s (" << tracks[selected].name << ") SEQ value? [Y/N] "; - cin >> choice; - - // If user confirms editing, prompt for new sequence value - if (tolower(choice) == 'y') { - int newValue; - do { - cout << "\nEnter the new SEQ value (Old value was " << arm9Values[selected] << ") [-1..75]: "; - cin >> newValue; - } while (newValue < -1 || newValue > 75); - // Update the selected track's sequence value - arm9Values[selected] = newValue; + } while (selected < 0 || selected >= tracks.size()); + + while (true) { + cout << "\nDo you want to change Track " << selected << "'s (" << tracks[selected] << ") SEQ value? [Y/N] "; + cin >> choice; + if (toupper(choice) == 'Y') { + do { + // Get the index of the SEQ value for the selected track + seqIndex = selected * 4; + + cout << "\nInsert the new SEQ value (Old value was " << armValues[seqIndex] << ") [-1..75]="; + cin >> newValue; + } while (newValue < -1 || newValue > 75); + + // Update the SEQ value for the selected track + armValues[seqIndex] = newValue; + break; + } else if (toupper(choice) == 'N') { + break; + } else { + cout << "\nThe Choice isn't valid."; + } } - // Prompt user to continue editing - cout << "\nDo you want to edit it further? [Y/N] "; - cin >> choice; - } while (tolower(choice) == 'y'); + while (true) { + cout << "\nDo you want to edit it furthermore? [Y/N] "; + cin >> choice; + + if (toupper(choice) == 'Y') { + break; + } else if (toupper(choice) == 'N') { + break; + } else { + cout << "\nThe Choice isn't valid."; + } + } + } while (toupper(choice) == 'Y'); - // Update arm9 data with modified sequence values and save it - for (int i = 0; i < TRACK_COUNT; ++i) { - arm9Data[OFFSET + i * 4] = arm9Values[i]; - } - saveFileContents("arm9.bin.bak", arm9Data); - saveFileContents("arm9.bin", arm9Data); + // Save the modified ARM9 binary file + ofstream out("arm9.bin.bak", ios_base::binary | ios_base::out); + out << text; + out.close(); - // Inform user about successful save and prompt to exit - cout << "\nSuccessfully saved edited_arm9.bin in your folder!" << endl; - cout << "Press ENTER to exit the program." << endl; + for (int i = offset; i < min(text.size(), offset + 211); i++) + text[i] = armValues[i - offset]; + + out.open("arm9.bin", ios_base::binary | ios_base::out); + out << text; + out.close(); + + cout << "\nSuccessfully saved edited_arm9.bin in your folder!\nPress ENTER to exit the program :3\n"; cin.ignore(); cin.ignore(); + return 0; -} \ No newline at end of file +}