foxygit / fake-08-xbox360 Log in
commit aa6d18e1afdcbfa4b1c9dfe3eca1457875d6b0e8
Author:     jtothebell <j.to.the.bell@gmail.com>
AuthorDate: Wed Apr 14 18:28:15 2021 -0600
Commit:     jtothebell <j.to.the.bell@gmail.com>
CommitDate: Wed Apr 14 18:28:15 2021 -0600

    Fix parsing 4th bit of music from p8 files
---
 source/cart.cpp             | 17 +++++++++++++----
 source/cart.h               |  1 +
 test/carts/cartparsetest.p8 |  1 +
 test/carttests.cpp          | 11 ++++++++---
 4 files changed, 23 insertions(+), 7 deletions(-)

diff --git a/source/cart.cpp b/source/cart.cpp
index 21187bc..3161726 100644
--- a/source/cart.cpp
+++ b/source/cart.cpp
@@ -6,8 +6,6 @@
 #include <array>
 #include <algorithm>

-//make sure 3ds and switch libpng are installed from devkitpro pacman
-//#include <png.h>
 #include "lodepng.h"

 #include "cart.h"
@@ -354,6 +352,9 @@ Cart::Cart(std::string filename){
             else if (currSec == "__music__"){
                 MusicString += line + "\n";
             }
+            else if (currSec == "__label__"){
+                LabelString += line + "\n";
+            }
         }

         Logger_Write("Setting cart graphics rom data from strings\n");
@@ -365,7 +366,7 @@ Cart::Cart(std::string filename){
         setSfx(SfxString);
         setMusic(MusicString);
     }
-    else if (hasEnding(filename, ".p8.png")) {
+    else if (hasEnding(filename, ".png")) {
         bool success = loadCartFromPng(filename);

         if (!success){
@@ -425,10 +426,15 @@ void Cart::setMusic(std::string musicString){
     int musicIdx = 0;

     while (std::getline(s, line)) {
+        if (line.length() < 11){
+            continue;
+        }
+
         buf[0] = line[0];
         buf[1] = line[1];
         uint8_t flagByte = (uint8_t)strtol(buf, NULL, 16);

+        uint8_t mode = (flagByte & 8) >> 3;
         uint8_t fstop = (flagByte & 4) >> 2;
         uint8_t frepeat = (flagByte & 2) >> 1;
         uint8_t fnext = flagByte & 1;
@@ -452,9 +458,12 @@ void Cart::setMusic(std::string musicString){
         CartRom.SongData[musicIdx].data[0] = channel1byte | fnext << 7;
         CartRom.SongData[musicIdx].data[1] = channel2byte | frepeat << 7;
         CartRom.SongData[musicIdx].data[2] = channel3byte | fstop << 7;
-        CartRom.SongData[musicIdx].data[3] = channel4byte;
+        CartRom.SongData[musicIdx].data[3] = channel4byte | mode << 7;

         musicIdx++;
+        if (musicIdx >= 64) {
+            break;
+        }
     }

 }
diff --git a/source/cart.h b/source/cart.h
index f18f2f0..0c61e4e 100644
--- a/source/cart.h
+++ b/source/cart.h
@@ -51,6 +51,7 @@ class Cart {
     std::string MapString;
     std::string SfxString;
     std::string MusicString;
+    std::string LabelString;

     CartRomData CartRom;

diff --git a/test/carts/cartparsetest.p8 b/test/carts/cartparsetest.p8
index 0fdebea..a02514c 100644
--- a/test/carts/cartparsetest.p8
+++ b/test/carts/cartparsetest.p8
@@ -14,4 +14,5 @@ __sfx__
 0010000036050310502a05019050192501925019250192501925019250192501925019250192501b2501b250192501b2501b2501b2501b2501b2501b2501b2501b2501925019250192501b250000000000000000
 __music__
 00 01424344
+0d 1b7d6f45

diff --git a/test/carttests.cpp b/test/carttests.cpp
index 6f82893..238f608 100644
--- a/test/carttests.cpp
+++ b/test/carttests.cpp
@@ -61,7 +61,7 @@ TEST_CASE("Load simple p8 cart") {
             "001d00000d0300e0500e0500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n0010000036050310502a05019050192501925019250192501925019250192501925019250192501b2501b250192501b2501b2501b2501b2501b2501b2501b2501b2501925019250192501b250000000000000000\n");
     }
     SUBCASE("Music section is populated") {
-        CHECK(cart->MusicString == "00 01424344\n\n");
+        CHECK(cart->MusicString == "00 01424344\n0d 1b7d6f45\n\n");
     }
     SUBCASE("Gfx data is populated") {
         CHECK(cart->CartRom.SpriteSheetData[0] == 255);
@@ -91,7 +91,7 @@ TEST_CASE("Load simple p8 cart") {
         CHECK(cart->CartRom.SfxData[0].notes[0].data[0] == 13);
         CHECK(cart->CartRom.SfxData[0].notes[0].data[1] == 6);
     }
-    SUBCASE("Music data is populated") {
+    SUBCASE("Music song data is populated") {
         CHECK(cart->CartRom.SongData[0].sfx0 == 1);
         CHECK(cart->CartRom.SongData[0].start == 0);
         CHECK(cart->CartRom.SongData[0].sfx1 == 66);
@@ -100,7 +100,12 @@ TEST_CASE("Load simple p8 cart") {
         CHECK(cart->CartRom.SongData[0].stop == 0);
         CHECK(cart->CartRom.SongData[0].sfx3 == 68);
         CHECK(cart->CartRom.SongData[0].mode == 0);
-
+    }
+    SUBCASE("Music data union is populated correctly") {
+        CHECK(cart->CartRom.SongData[1].data[0] == 155);
+        CHECK(cart->CartRom.SongData[1].data[1] == 125);
+        CHECK(cart->CartRom.SongData[1].data[2] == 239);
+        CHECK(cart->CartRom.SongData[1].data[3] == 197);
     }

     delete cart;