foxygit / fake-08-xbox360 Log in
commit 6279216964e351409201e987653e3d708fd4ad78
Author:     Jon Bell <j.to.the.bell@gmail.com>
AuthorDate: Tue Feb 21 21:40:27 2023 -0700
Commit:     Jon Bell <j.to.the.bell@gmail.com>
CommitDate: Tue Feb 21 21:40:27 2023 -0700

    Allow loading p8 text carts from memory
---
 source/cart.cpp    | 171 ++++++++++++++++++++++++++++++++---------------------
 source/cart.h      |   2 +
 test/Makefile      |   2 +-
 test/carttests.cpp |  23 +++++++
 4 files changed, 129 insertions(+), 69 deletions(-)

diff --git a/source/cart.cpp b/source/cart.cpp
index 5324457..bd5e71f 100644
--- a/source/cart.cpp
+++ b/source/cart.cpp
@@ -26,7 +26,7 @@


 static char const *legacyCompressionLut = "\n 0123456789abcdefghijklmnopqrstuvwxyz!#%(){}[]<>+=/*:;.,~_";
-
+static std::regex _includeRegex = std::regex("\\s*#include\\s+([\\\\/A-Za-z0-9_\\-\\.]+)");

 //https://github.com/samhocevar/zepto8/blob/b1a13516945c49e47495c739e6a43a241ad99291/src/pico8/code.cpp
 // Move to front structure
@@ -323,18 +323,111 @@ bool Cart::loadCartFromPng(std::string filename){
     return loadCartFromPng(image);
 }

-static std::regex _includeRegex = std::regex("\\s*#include\\s+([\\\\/A-Za-z0-9_\\-\\.]+)");
+bool Cart::loadCartFromString(std::string cartStr) {
+    fullCartText = cartStr;
+
+    std::istringstream s(cartStr);
+    std::string line;
+    std::string currSec = "";
+    std::smatch sm;
+
+    while (std::getline(s, line)) {
+        line = utils::trimright(line, " \n\r");
+        line = charset::utf8_to_pico8(line);
+        //line = convert_emojis(line);
+
+        if (line.length() > 2 && line[0] == '_' && line[1] == '_') {
+            currSec = line;
+        }
+        else if (currSec == "__lua__"){
+            if (std::regex_match(line, sm, _includeRegex)) {
+                auto dir = getDirectory(FullCartPath);
+                auto fullPath = dir + "/" + sm[1].str();
+
+                auto includeContents = get_file_contents(fullPath);
+                if (includeContents.length() > 0){
+                    includeContents = charset::utf8_to_pico8(includeContents);
+                    LuaString += includeContents + "\n";
+                }
+                else{
+                    //todo: report error
+                    //error: can't find included file
+                    LoadError = "Can't find included file";
+                    return false;
+                }
+            }
+            else {
+                LuaString += line + "\n";
+            }
+        }
+        else if (currSec == "__gfx__"){
+            SpriteSheetString += line + "\n";
+        }
+        else if (currSec == "__gff__"){
+            SpriteFlagsString += line + "\n";
+        }
+        else if (currSec == "__map__"){
+            MapString += line + "\n";
+        }
+        else if (currSec == "__sfx__"){
+            SfxString += line + "\n";
+        }
+        else if (currSec == "__music__"){
+            MusicString += line + "\n";
+        }
+        else if (currSec == "__label__"){
+            LabelString += line + "\n";
+        }
+    }
+
+    Logger_Write("Setting cart graphics rom data from strings\n");
+    setSpriteSheet(SpriteSheetString);
+    setSpriteFlags(SpriteFlagsString);
+    setMapData(MapString);
+
+    Logger_Write("Setting cart audio rom data from strings\n");
+    setSfx(SfxString);
+    setMusic(MusicString);
+
+    return true;
+}

 Cart::Cart (const unsigned char* cartData, size_t size){
+    if (size < 5) {
+        LoadError = "Invalid cart. Less than 5 bytes";
+        return;
+    }
+
     //TODO: check if it is a text cart, decode string, then parse
-    bool success = loadCartFromPng(cartData, size);
+    if ((char)cartData[0] == '\x89' &&
+        (char)cartData[1] == 'P' &&
+        (char)cartData[2] == 'N' &&
+        (char)cartData[3] == 'G') {
+        bool success = loadCartFromPng(cartData, size);

-    if (!success){
-        return;
+        if (!success){
+            return;
+        }
+        LoadError = "";
+        Logger_Write("got valid png cart\n");
     }
+    else if((char)cartData[0] == 'p' &&
+         (char)cartData[1] == 'i' &&
+         (char)cartData[2] == 'c' &&
+         (char)cartData[3] == 'o') {
+        std::string strContents(reinterpret_cast<const char*>(cartData), size);

-    LoadError = "";
-    Logger_Write("got valid png cart\n");
+        bool success = loadCartFromString(strContents);
+
+        if (!success){
+            return;
+        }
+        LoadError = "";
+        Logger_Write("got valid p8 cart\n");
+    }
+    else {
+        LoadError = "unknown cart file format";
+    }

 }

@@ -377,69 +470,11 @@ Cart::Cart(std::string filename, std::string cartDirectory){
         }
         Logger_Write("Got file contents... parsing cart\n");

-        fullCartText = cartStr;
-
-        std::istringstream s(cartStr);
-        std::string line;
-        std::string currSec = "";
-        std::smatch sm;
-
-        while (std::getline(s, line)) {
-            line = utils::trimright(line, " \n\r");
-            line = charset::utf8_to_pico8(line);
-            //line = convert_emojis(line);
+        bool success = loadCartFromString(cartStr);

-            if (line.length() > 2 && line[0] == '_' && line[1] == '_') {
-                currSec = line;
-            }
-            else if (currSec == "__lua__"){
-                if (std::regex_match(line, sm, _includeRegex)) {
-                    auto dir = getDirectory(FullCartPath);
-                    auto fullPath = dir + "/" + sm[1].str();
-
-                    auto includeContents = get_file_contents(fullPath);
-                    if (includeContents.length() > 0){
-                        includeContents = charset::utf8_to_pico8(includeContents);
-                        LuaString += includeContents + "\n";
-                    }
-                    else{
-                        //todo: report error
-                        //error: can't find included file
-                        return;
-                    }
-                }
-                else {
-                    LuaString += line + "\n";
-                }
-            }
-            else if (currSec == "__gfx__"){
-                SpriteSheetString += line + "\n";
-            }
-            else if (currSec == "__gff__"){
-                SpriteFlagsString += line + "\n";
-            }
-            else if (currSec == "__map__"){
-                MapString += line + "\n";
-            }
-            else if (currSec == "__sfx__"){
-                SfxString += line + "\n";
-            }
-            else if (currSec == "__music__"){
-                MusicString += line + "\n";
-            }
-            else if (currSec == "__label__"){
-                LabelString += line + "\n";
-            }
+        if (!success){
+            return;
         }
-
-        Logger_Write("Setting cart graphics rom data from strings\n");
-        setSpriteSheet(SpriteSheetString);
-        setSpriteFlags(SpriteFlagsString);
-        setMapData(MapString);
-
-        Logger_Write("Setting cart audio rom data from strings\n");
-        setSfx(SfxString);
-        setMusic(MusicString);
     }
     else if (firstFourChars == "\x89PNG") {
         bool success = loadCartFromPng(FullCartPath);
diff --git a/source/cart.h b/source/cart.h
index a2208ab..dfb27de 100644
--- a/source/cart.h
+++ b/source/cart.h
@@ -37,6 +37,8 @@ class Cart {
     bool loadCartFromPng(std::string filename);
     bool loadCartFromPng(const unsigned char* cartData, size_t size);
     bool loadCartFromPng(std::vector<unsigned char> image);
+
+    bool loadCartFromString(std::string cartStr);

     public:
     Cart (std::string filename, std::string cartDirectory);
diff --git a/test/Makefile b/test/Makefile
index c2bdeea..966cd1e 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -16,7 +16,7 @@ INCLUDES  	:= ../source ../include ../libs/z8lua ../libs/utf8-util ../libs/lodep
 #---------------------------------------------------------------------------------
 CC = $(CXX)

-CFLAGS	:=	-g -O2 -Wall -Wno-deprecated -ffunction-sections -std=c++17 \
+CFLAGS	:=	-g -Wall -Wno-deprecated -ffunction-sections -std=c++17 \
 			$(DEFINES)

 CFLAGS	+=	$(INCLUDE) -DVER_STR=\"$(APP_VERSION)\"
diff --git a/test/carttests.cpp b/test/carttests.cpp
index 7386892..24e918f 100644
--- a/test/carttests.cpp
+++ b/test/carttests.cpp
@@ -246,5 +246,28 @@ TEST_CASE("Load legacy png cart from memory") {
         CHECK(cart->CartRom.SpriteSheetData[3] == 0);
     }

+    delete cart;
+}
+
+TEST_CASE("Load text cart from memory") {
+    auto buffer = get_file_as_buffer("carts/cartparsetest.p8");
+    const unsigned char* char_array = reinterpret_cast<const unsigned char*>(buffer.data());
+    Cart* cart = new Cart(char_array, buffer.size());
+
+    SUBCASE("error is empty") {
+        CHECK(cart->LoadError == "");
+    }
+    SUBCASE("Lua section is populated") {
+        CHECK(cart->LuaString == "a=1\n");
+    }
+    SUBCASE("Gfx section is populated") {
+        CHECK(cart->SpriteSheetString ==
+            "ff102030555555550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n");
+    }
+    SUBCASE("Sprite flags section is populated") {
+        CHECK(cart->SpriteFlagsString ==
+            "0001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n");
+    }
+
     delete cart;
 }
\ No newline at end of file