foxygit / fake-08-xbox360 Log in
commit a07d4230e6e78a64b64bb9b80998c94651795908
Author:     Jon Bell <jondbell@protonmail.com>
AuthorDate: Tue Apr 14 18:02:38 2020 -0600
Commit:     Jon Bell <jondbell@protonmail.com>
CommitDate: Tue Apr 14 18:02:38 2020 -0600

    Separate lua and spritesheet parts of p8 file
---
 source/cart.cpp            |  51 ++++++++++++++++----
 source/tests/cart_test.cpp | 114 +++++++++++++++++++++++++++++++++++++++++++++
 source/tests/cart_test.h   |   2 +
 source/utils.h             |  51 ++++++++++++++++++++
 4 files changed, 208 insertions(+), 10 deletions(-)

diff --git a/source/cart.cpp b/source/cart.cpp
index 335b4e5..568407e 100644
--- a/source/cart.cpp
+++ b/source/cart.cpp
@@ -1,8 +1,11 @@
 #include <string>
+#include <sstream>

 #include "cart.h"
 #include "filehelpers.h"

+#include "utils.h"
+
 #include "tests/test_switch.h"
 #if _TEST
 #include "tests/cart_test.h"
@@ -11,21 +14,49 @@
 void LoadCart(std::string filename){
     auto cartStr = get_file_contents(filename.c_str());

-    /*
-    PicoCart cart = {
-        filename,
-        cartStr,
-        nullptr,
-        nullptr,
-        nullptr,
-        nullptr
-    };
-    */
+    std::istringstream s(cartStr);
+    std::string line;
+    std::string lua = "";
+    std::string spritesheet = "";
+    std::string currSec = "";
+    while (std::getline(s, line)) {
+		line = utils::trimright(line, " \n\r");
+
+        if (line.length() > 2 && line[0] == '_' && line[1] == '_') {
+            currSec = line;
+        }
+        else if (currSec == "__lua__"){
+            lua += line + "\n";
+        }
+        else if (currSec == "__gfx__"){
+            spritesheet += line + "\n";
+        }
+
+        /*
+		if (!check_include_file(line, cart, filenum)) {
+			if (valid_sections.find(line) != valid_sections.end()) {
+				cart.sections["cur_sect"] = line;
+				logr << "section " << line;
+			} else {
+				if (cart.sections["cur_sect"] == "__lua__") {
+					cart.source.push_back(Line{filenum, line});
+				} else {
+					cart.sections[cart.sections["cur_sect"]] += line + "\n";
+				}
+			}
+		}
+        */
+	}


     #if _TEST
+    //run tests to make sure cart is parsed correctly
     verifyFullCartText(cartStr);

+    verifyLuaText(lua);
+
+    verifySpriteSheetText(spritesheet);
+
     #endif

     //return cart;
diff --git a/source/tests/cart_test.cpp b/source/tests/cart_test.cpp
index 9bd9bbd..e414e1f 100644
--- a/source/tests/cart_test.cpp
+++ b/source/tests/cart_test.cpp
@@ -107,3 +107,117 @@ bool verifyFullCartText(std::string cartText){

     return equal;
 }
+
+const char* expected_lua_text = R"(local r1x0 = 10
+local r1y0 = 10
+local r1x1 = 50
+local r1y1 = 50
+local r1c = 2
+
+local r2x0 = 60
+local r2y0 = 60
+local r2x1 = 100
+local r2y1 = 100
+local r2c = 3
+
+local r1dx = .5
+local r1dy = 2
+
+local r2dx = 2
+local r2dy = 2
+
+function _draw()
+	cls()
+
+    rectfill(0, 0, 127, 127, 0)
+
+    print("abcdefghijklmnopqrstuvwxyz", 0, 0, 7)
+
+    rectfill(50, -1, 60, 10, 11)
+
+	rectfill(r1x0, r1y0, r1x1, r1y1, r1c)
+
+	rect(r2x0, r2y0, r2x1, r2y1, r2c)
+
+    pset(40, 10, 13)
+
+    line(120, 10, 120, 120, 14)
+
+    line(0, 0, 20, 20, 15)
+
+    line(0, 20, 20, 0, 15)
+
+    line(20, 30, 0, 20, 12)
+
+    line(0, 30, 20, 20, 12)
+
+    circ(90, 90, 30, 8)
+
+    circfill(100, 100, 15, 9)
+
+    print("test str", 64, 64, 7)
+
+    spr(7, 40, 40, 1, 1, false, false)
+end
+
+function _update()
+    if (r1x0 < 0 or r1x1 > 127) then
+        r1dx = -1 * r1dx;
+    end
+    if (r1y0 < 0 or r1y1 > 127) then
+        r1dy = -1 * r1dy;
+    end
+    if (r2x0 < 0 or r2x1 > 127) then
+        r2dx = -1 * r2dx;
+    end
+    if (r2y0 < 0 or r2y1 > 127) then
+        r2dy = -1 * r2dy;
+    end
+    r1x0 = r1dx + r1x0
+    r1x1 = r1dx + r1x1
+    r1y0 = r1dy + r1y0
+    r1y1 = r1dy + r1y1
+end
+)";
+
+bool verifyLuaText(std::string luaText){
+    bool equal = luaText == expected_lua_text;
+    if (equal) {
+        printf("Lua Text: PASS;\n");
+    }
+    else {
+        printf("Lua Text: FAIL;\n");
+    }
+
+    return equal;
+}
+
+const char* expected_spritesheet_text = R"(e0000000000000000000000007700770000770000000000000000000000660005000000500000000000000000000000000000000000000000000000000000000
+00000000000000067000000078877887077777700500000000000050800660085005500500000000000000000000000000000000000000000000000000000000
+0070070000000006600000007888888707bbbb700500000000000050600550065055550500000000000000000000000000000000000000000000000000000000
+000770000000000660000000078888707bbbbbb70500000000000050600550065550055500000000000000000000000000000000000000000000000000000000
+000770000000000660000000078888707bbbbbb70500000550000050600660065555555500000000000000000000000000000000000000000000000000000000
+0070070000000066670000000078870007bbbb700500005555000050666cc66650b55b0500000000000000000000000000000000000000000000000000000000
+00000000008000666600080000077000077777700550055555500050666cc6665006600500000000000000000000000000000000000000000000000000000000
+0000000080d00065d6000d0800000000000770000555555555555550000660005000000500000000000000000000000000000000000000000000000000000000
+0b00080060d0006556000d0600000000000000000550055665500550000000000000000000000000000000000000000000000000000000000000000000000000
+0b00080060ddd065560ddd0600000000000000000500005555000050000000000000000000000000000000000000000000000000000000000000000000000000
+0b000800600ddd6556ddd00600000000000000000500008558000050000000000000000000000000000000000000000000000000000000000000000000000000
+0b000800666666666666666600000000000000000500000000000050000000000000000000000000000000000000000000000000000000000000000000000000
+00000000666866666666666600000000000000000500000000000050000000000000000000000000000000000000000000000000000000000000000000000000
+000000000666666cc666666000000000000000000500000000000050000000000000000000000000000000000000000000000000000000000000000000000000
+000000000006666cc666600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00000000000000666600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+)";
+
+bool verifySpriteSheetText(std::string spritesheet) {
+    bool equal = spritesheet == expected_spritesheet_text;
+    if (equal) {
+        printf("Spritesheet Text: PASS;\n");
+    }
+    else {
+        printf("Spritesheet Text: FAIL;\n");
+    }
+
+    return equal;
+}
diff --git a/source/tests/cart_test.h b/source/tests/cart_test.h
index 82fbc8a..5885460 100644
--- a/source/tests/cart_test.h
+++ b/source/tests/cart_test.h
@@ -3,3 +3,5 @@
 #include <string>

 bool verifyFullCartText(std::string cartText);
+bool verifyLuaText(std::string luaText);
+bool verifySpriteSheetText(std::string spritesheet);
diff --git a/source/utils.h b/source/utils.h
new file mode 100644
index 0000000..fb35b91
--- /dev/null
+++ b/source/utils.h
@@ -0,0 +1,51 @@
+#pragma once
+//sourced from tac08
+
+#include <string>
+#include <vector>
+
+namespace path {
+	std::string removeRelative(std::string path);
+	std::string normalisePath(std::string path);
+	std::string getPath(std::string path);
+	std::string getFilename(std::string path);
+	std::pair<std::string, std::string> splitFilename(std::string filename);
+	void test();
+}  // namespace path
+
+namespace utils {
+	template <typename T>
+	T limit(T n, T min, T max) {
+		if (n < min)
+			return min;
+		if (n > max)
+			return max;
+		return n;
+	}
+
+	inline std::string trimleft(const std::string& str, const char* wschars = " \t") {
+		std::string result = str;
+
+		result.erase(0, str.find_first_not_of(wschars));
+		return result;
+	}
+
+	inline std::string trimright(const std::string& str, const char* wschars = " \t") {
+		std::string result = str;
+
+		result.erase(str.find_last_not_of(wschars) + 1);
+		return result;
+	}
+
+	inline std::string trimboth(const std::string& str, const char* wschars = " \t") {
+		return trimleft(trimright(str, wschars), wschars);
+	}
+
+	void splitString(const std::string& input,
+	                 std::vector<std::string>& output,
+	                 const char* sep_chars);
+
+}  // namespace utils
+
+#define STRINGIFY(x) #x
+#define TOSTRING(x) STRINGIFY(x)