commit 767dcfe12a0a70fdb024f2463cbd941c69a92e7a
Author: jtothebell <j.to.the.bell@gmail.com>
AuthorDate: Fri Apr 23 21:11:35 2021 -0600
Commit: jtothebell <j.to.the.bell@gmail.com>
CommitDate: Sun May 2 13:32:58 2021 -0600
Super basic/naive #include implementation
---
source/cart.cpp | 23 ++++++++++++++++++++++-
source/filehelpers.cpp | 8 ++++++++
source/filehelpers.h | 2 ++
test/endtoendtests.cpp | 2 ++
4 files changed, 34 insertions(+), 1 deletion(-)
diff --git a/source/cart.cpp b/source/cart.cpp
index d2ee0b3..5c4727a 100644
--- a/source/cart.cpp
+++ b/source/cart.cpp
@@ -5,6 +5,7 @@
#include <stack>
#include <array>
#include <algorithm>
+#include <regex>
#include "lodepng.h"
@@ -302,6 +303,8 @@ bool Cart::loadCartFromPng(std::string filename){
}
+static std::regex _includeRegex = std::regex("\\s*#include\\s+([\\\\/\\w-\\.]+)");
+
//tac08 based cart parsing and stripping of emoji
Cart::Cart(std::string filename){
Filename = filename;
@@ -326,6 +329,7 @@ Cart::Cart(std::string filename){
std::istringstream s(cartStr);
std::string line;
std::string currSec = "";
+ std::smatch sm;
while (std::getline(s, line)) {
line = utils::trimright(line, " \n\r");
@@ -335,7 +339,24 @@ Cart::Cart(std::string filename){
currSec = line;
}
else if (currSec == "__lua__"){
- LuaString += line + "\n";
+ if (std::regex_match(line, sm, _includeRegex)) {
+ auto dir = getDirectory(filename);
+ auto fullPath = dir + "/" + sm[1].str();
+
+ auto includeContents = get_file_contents(fullPath);
+ if (includeContents.length() > 0){
+ //todo: handle emojis
+ 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";
diff --git a/source/filehelpers.cpp b/source/filehelpers.cpp
index 9e91b86..cb0fd61 100644
--- a/source/filehelpers.cpp
+++ b/source/filehelpers.cpp
@@ -28,3 +28,11 @@ std::vector<unsigned char> get_file_buffer(std::string filename){
return buffer;
}
+//https://stackoverflow.com/a/8518855
+std::string getDirectory(const std::string& fname){
+ size_t pos = fname.find_last_of("\\/");
+ return (std::string::npos == pos)
+ ? ""
+ : fname.substr(0, pos);
+}
+
diff --git a/source/filehelpers.h b/source/filehelpers.h
index 1b29d71..50c67d4 100644
--- a/source/filehelpers.h
+++ b/source/filehelpers.h
@@ -1,3 +1,5 @@
#include <string>
std::string get_file_contents(std::string filename);
+
+std::string getDirectory(const std::string& fname);
diff --git a/test/endtoendtests.cpp b/test/endtoendtests.cpp
index 816daed..d9a13bd 100644
--- a/test/endtoendtests.cpp
+++ b/test/endtoendtests.cpp
@@ -55,11 +55,13 @@ bool verifyScreenshot(Vm* vm, std::string screenshotFilename) {
(pixelIsBlack && c == 0) ||
(r == col.Red && g == col.Green && b == col.Blue);
+ /*
if (!pixelMatches) {
printf("Non-matching pixel at idx %d (%d,%d): rgb: %d,%d,%d\n", pixIdx, x, y, r, g, b);
printf("fake 08 color is %d rgb: %d,%d,%d\n", c, col.Red, col.Green, col.Blue);
}
+ */
pixelsMatch &= pixelMatches;
}