foxygit / fake-08-xbox360 Log in
commit 94c56842a93b35d456cfba8b63e69867650d81ce
Author:     Jon Bell <j.to.the.bell@gmail.com>
AuthorDate: Sun Feb 19 19:21:53 2023 -0700
Commit:     Jon Bell <j.to.the.bell@gmail.com>
CommitDate: Sun Feb 19 19:21:53 2023 -0700

    Add new cart ctor to load png cart from memory
---
 source/cart.cpp        | 85 ++++++++++++++++++++++++++++++++++++--------------
 source/cart.h          |  3 ++
 source/filehelpers.cpp | 18 +++++++++++
 source/filehelpers.h   |  3 ++
 test/carttests.cpp     | 22 +++++++++++++
 5 files changed, 108 insertions(+), 23 deletions(-)

diff --git a/source/cart.cpp b/source/cart.cpp
index 0383df3..5324457 100644
--- a/source/cart.cpp
+++ b/source/cart.cpp
@@ -150,29 +150,8 @@ static std::string pxa_decompress(uint8_t const *input)

 #define HEADERLEN 8

-
-bool Cart::loadCartFromPng(std::string filename){
-    std::vector<unsigned char> image; //the raw pixels
-    unsigned width, height;
-
-    //decode
-    unsigned error = lodepng::decode(image, width, height, filename);
-    //the pixels are now in the vector "image", 4 bytes per pixel, ordered RGBARGBA..., use it as texture, draw it,
-
-    //if there's an error, display it
-    if(error) {
-        LoadError = "png decoder error " + std::string(lodepng_error_text(error));
-        Logger_Write("%s%s", LoadError.c_str(), "\n");
-        return false;
-    }
-
-    if (width != 160 || height != 205) {
-        LoadError = "Invalid png dimensions";
-        Logger_Write("invalid dimensions\n");
-        return false;
-    }
-
-    //160x205 == 32800 == 0x8020
+bool Cart::loadCartFromPng(std::vector<unsigned char> image) {
+        //160x205 == 32800 == 0x8020
     //0x8000 is actual used data size
     size_t imageBytes = image.size();

@@ -294,11 +273,71 @@ bool Cart::loadCartFromPng(std::string filename){
     }

     return true;
+}
+
+bool Cart::loadCartFromPng(const unsigned char* cartData, size_t size){
+    std::vector<unsigned char> image; //the raw pixels
+    unsigned width, height;
+
+    //decode
+    unsigned error = lodepng::decode(image, width, height, cartData, size);
+
+    //if there's an error, display it
+    if(error) {
+        LoadError = "png decoder error " + std::string(lodepng_error_text(error));
+        Logger_Write("%s%s", LoadError.c_str(), "\n");
+        return false;
+    }
+
+    if (width != 160 || height != 205) {
+        LoadError = "Invalid png dimensions";
+        Logger_Write("invalid dimensions\n");
+        return false;
+    }
+
+    return loadCartFromPng(image);
+}
+
+
+bool Cart::loadCartFromPng(std::string filename){
+    std::vector<unsigned char> image; //the raw pixels
+    unsigned width, height;
+
+    //decode
+    unsigned error = lodepng::decode(image, width, height, filename);
+    //the pixels are now in the vector "image", 4 bytes per pixel, ordered RGBARGBA..., use it as texture, draw it,
+
+    //if there's an error, display it
+    if(error) {
+        LoadError = "png decoder error " + std::string(lodepng_error_text(error));
+        Logger_Write("%s%s", LoadError.c_str(), "\n");
+        return false;
+    }
+
+    if (width != 160 || height != 205) {
+        LoadError = "Invalid png dimensions";
+        Logger_Write("invalid dimensions\n");
+        return false;
+    }

+    return loadCartFromPng(image);
 }

 static std::regex _includeRegex = std::regex("\\s*#include\\s+([\\\\/A-Za-z0-9_\\-\\.]+)");

+Cart::Cart (const unsigned char* cartData, size_t size){
+    //TODO: check if it is a text cart, decode string, then parse
+    bool success = loadCartFromPng(cartData, size);
+
+    if (!success){
+        return;
+    }
+
+    LoadError = "";
+    Logger_Write("got valid png cart\n");
+
+}
+
 //tac08 based cart parsing and stripping of emoji
 Cart::Cart(std::string filename, std::string cartDirectory){
     //the leading # indicates it is the BBS key. In the future, it would be nice to fetch them,
diff --git a/source/cart.h b/source/cart.h
index b5f9551..a2208ab 100644
--- a/source/cart.h
+++ b/source/cart.h
@@ -35,9 +35,12 @@ class Cart {
     void setMusic(std::string musicString);

     bool loadCartFromPng(std::string filename);
+    bool loadCartFromPng(const unsigned char* cartData, size_t size);
+    bool loadCartFromPng(std::vector<unsigned char> image);

     public:
     Cart (std::string filename, std::string cartDirectory);
+    Cart (const unsigned char* cartData, size_t size);
     ~Cart();

     std::string FullCartPath;
diff --git a/source/filehelpers.cpp b/source/filehelpers.cpp
index 50099d1..fc9a6fc 100644
--- a/source/filehelpers.cpp
+++ b/source/filehelpers.cpp
@@ -19,6 +19,24 @@ std::string get_file_contents(std::string filename){
   return "";
 }

+std::vector<char> get_file_as_buffer(std::string filename){
+    std::ifstream in(filename, std::ios::in | std::ios::binary);
+    if (in)
+    {
+        in.seekg(0, std::ios::end);
+        std::streamsize size = in.tellg();
+        in.seekg(0, std::ios::beg);
+
+        std::vector<char> buffer(size);
+        if (in.read(buffer.data(), size)) {
+            return buffer;
+        }
+    }
+
+    std::vector<char> buffer(0);
+    return buffer;
+}
+
 std::string get_first_four_chars(std::string filename){
     std::ifstream in(filename, std::ios::in | std::ios::binary);
     if (in)
diff --git a/source/filehelpers.h b/source/filehelpers.h
index a4c9b02..1482ca4 100644
--- a/source/filehelpers.h
+++ b/source/filehelpers.h
@@ -1,7 +1,10 @@
 #include <string>
+#include <vector>

 std::string get_file_contents(std::string filename);

+std::vector<char> get_file_as_buffer(std::string filename);
+
 std::string get_first_four_chars(std::string filename);

 std::string getDirectory(const std::string& fname);
diff --git a/test/carttests.cpp b/test/carttests.cpp
index b3fa6f5..7386892 100644
--- a/test/carttests.cpp
+++ b/test/carttests.cpp
@@ -1,5 +1,6 @@
 #include "doctest.h"
 #include "../source/cart.h"
+#include "../source/filehelpers.h"

 TEST_CASE("Loads bios cart") {
     Cart* cart = new Cart("__FAKE08-BIOS.p8", "");
@@ -224,5 +225,26 @@ TEST_CASE("cart loading options") {
         CHECK(cart->FullCartPath == "carts/cartparsetest.p8");
     }

+    delete cart;
+}
+
+TEST_CASE("Load legacy png cart from memory") {
+    auto buffer = get_file_as_buffer("carts/test_legacypng_cart.p8.png");
+    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 == "print(\"0.1.10c\")\n");
+    }
+    SUBCASE("Gfx data is populated") {
+        CHECK(cart->CartRom.SpriteSheetData[0] == 0);
+        CHECK(cart->CartRom.SpriteSheetData[1] == 0);
+        CHECK(cart->CartRom.SpriteSheetData[2] == 0);
+        CHECK(cart->CartRom.SpriteSheetData[3] == 0);
+    }
+
     delete cart;
 }
\ No newline at end of file