commit b8c3c58bf47922eb4e872056906df173882eff3f
Author: jtothebell <j.to.the.bell@gmail.com>
AuthorDate: Thu Jun 17 16:59:49 2021 -0600
Commit: jtothebell <j.to.the.bell@gmail.com>
CommitDate: Thu Jun 17 16:59:49 2021 -0600
Implement relative vs absolute cart loading
---
source/cart.cpp | 31 ++++++++++++++++------
source/cart.h | 4 +--
source/filehelpers.cpp | 26 +++++++++++++++++++
source/filehelpers.h | 4 +++
source/vm.cpp | 10 ++++----
test/Makefile | 7 ++++-
test/carttests.cpp | 34 ++++++++++++++++---------
test/filehelperstests.cpp | 65 +++++++++++++++++++++++++++++++++++++++++++++++
8 files changed, 153 insertions(+), 28 deletions(-)
diff --git a/source/cart.cpp b/source/cart.cpp
index 3a8c66f..73dca47 100644
--- a/source/cart.cpp
+++ b/source/cart.cpp
@@ -298,21 +298,36 @@ 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;
+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,
+ //but for now expect the user to supply the carts
+ if (filename.length() > 0 && filename[0] == '#') {
+ filename = filename.substr(1);
+ }
+
+ if (getFileExtension(filename) == "") {
+ filename = filename + ".p8";
+ }
+
+ if (cartDirectory.length() > 0 && ! isAbsolutePath(filename)) {
+ FullCartPath = cartDirectory + "/" + filename;
+ }
+ else {
+ FullCartPath = filename;
+ }
//zero out cart rom so no garbage is left over
initCartRom();
Logger_Write("getting file contents\n");
- if (hasEnding(filename, ".p8")){
+ if (hasEnding(FullCartPath, ".p8")){
std::string cartStr;
- if (filename == "__FAKE08-BIOS.p8") {
+ if (FullCartPath == "__FAKE08-BIOS.p8") {
cartStr = fake08BiosP8;
}
else {
- cartStr = get_file_contents(filename.c_str());
+ cartStr = get_file_contents(FullCartPath.c_str());
}
Logger_Write("Got file contents... parsing cart\n");
@@ -332,7 +347,7 @@ Cart::Cart(std::string filename){
}
else if (currSec == "__lua__"){
if (std::regex_match(line, sm, _includeRegex)) {
- auto dir = getDirectory(filename);
+ auto dir = getDirectory(FullCartPath);
auto fullPath = dir + "/" + sm[1].str();
auto includeContents = get_file_contents(fullPath);
@@ -379,8 +394,8 @@ Cart::Cart(std::string filename){
setSfx(SfxString);
setMusic(MusicString);
}
- else if (hasEnding(filename, ".png")) {
- bool success = loadCartFromPng(filename);
+ else if (hasEnding(FullCartPath, ".png")) {
+ bool success = loadCartFromPng(FullCartPath);
if (!success){
return;
diff --git a/source/cart.h b/source/cart.h
index 0c61e4e..564916e 100644
--- a/source/cart.h
+++ b/source/cart.h
@@ -37,10 +37,10 @@ class Cart {
bool loadCartFromPng(std::string filename);
public:
- Cart (std::string filename);
+ Cart (std::string filename, std::string cartDirectory);
~Cart();
- std::string Filename;
+ std::string FullCartPath;
std::string LuaString;
diff --git a/source/filehelpers.cpp b/source/filehelpers.cpp
index 43bcbea..a54f55c 100644
--- a/source/filehelpers.cpp
+++ b/source/filehelpers.cpp
@@ -36,6 +36,32 @@ std::string getDirectory(const std::string& fname){
: fname.substr(0, pos);
}
+bool isAbsolutePath (std::string const &path) {
+ if (path.length() == 0) {
+ return false;
+ }
+
+ if (path[0] == '/') {
+ return true;
+ }
+
+ size_t colonPos = path.find_first_of(":");
+ size_t slashPos = path.find_first_of("/");
+
+ if (colonPos != std::string::npos && slashPos == (colonPos + 1)){
+ return true;
+ }
+
+ return false;
+}
+
+std::string getFileExtension(std::string const &path) {
+ size_t pos = path.find_last_of(".");
+ return (std::string::npos == pos)
+ ? ""
+ : path.substr(pos);
+}
+
bool hasEnding (std::string const &fullString, std::string const &ending) {
if (fullString.length() >= ending.length()) {
return (0 == fullString.compare (fullString.length() - ending.length(), ending.length(), ending));
diff --git a/source/filehelpers.h b/source/filehelpers.h
index dd3cd40..94e155e 100644
--- a/source/filehelpers.h
+++ b/source/filehelpers.h
@@ -5,3 +5,7 @@ std::string get_file_contents(std::string filename);
std::string getDirectory(const std::string& fname);
bool hasEnding (std::string const &fullString, std::string const &ending);
+
+bool isAbsolutePath (std::string const &path);
+
+std::string getFileExtension(std::string const &path);
diff --git a/source/vm.cpp b/source/vm.cpp
index 4aedfe1..ecd80ca 100644
--- a/source/vm.cpp
+++ b/source/vm.cpp
@@ -310,7 +310,7 @@ bool Vm::loadCart(Cart* cart) {
//customize bios per host's requirements
- if (cart->Filename == BiosCartName) {
+ if (cart->FullCartPath == BiosCartName) {
std::string customBiosLua = _host->customBiosLua();
if (customBiosLua.length() > 0) {
@@ -332,7 +332,7 @@ bool Vm::loadCart(Cart* cart) {
void Vm::LoadBiosCart(){
CloseCart();
- Cart *cart = new Cart(BiosCartName);
+ Cart *cart = new Cart(BiosCartName, "");
bool success = loadCart(cart);
@@ -351,7 +351,7 @@ void Vm::LoadCart(std::string filename){
Logger_Write("Calling Cart Constructor\n");
auto cartDir = _host->getCartDirectory();
- Cart *cart = new Cart(cartDir + "/" + filename);
+ Cart *cart = new Cart(filename, cartDir);
_cartLoadError = cart->LoadError;
@@ -535,7 +535,7 @@ int Vm::GetTargetFps() {
std::string Vm::CurrentCartFilename(){
if (_loadedCart){
- return _loadedCart->Filename;
+ return _loadedCart->FullCartPath;
}
return "";
@@ -757,7 +757,7 @@ void Vm::vm_reload(int destaddr, int sourceaddr, int len, string filename){
bool multicart = false;
if (filename.length() > 0) {
- cart = new Cart(filename);
+ cart = new Cart(filename, _host->getCartDirectory());
if (cart->LoadError.length() > 0) {
//error, can't load cart
//todo: see what kind of error pico 8 throws, emulate
diff --git a/test/Makefile b/test/Makefile
index 9896c5d..3b3a5ee 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -9,7 +9,7 @@ all: runtests
testrunner.a: testroot.o \
fontdatatests.o nibblehelperstests.o graphicstests.o audiotests.o \
- carttests.o endtoendtests.o vmtests.o
+ carttests.o endtoendtests.o vmtests.o filehelperstests.o
g++ -I$(CURDIR)/include -L$(CURDIR)/lib \
testroot.o \
fontdata.o fontdatatests.o \
@@ -19,6 +19,7 @@ testrunner.a: testroot.o \
vm.o endtoendtests.o picoluaapi.o Input.o \
vmtests.o stubhost.o \
nibblehelpers.o nibblehelperstests.o \
+ filehelperstests.o \
-llua -ldl \
-o testrunner.a
@@ -85,6 +86,10 @@ vmtests.o: endtoendtests.o \
vmtests.cpp stubhost.cpp
g++ $(CFLAGS) -I$(CURDIR)/include -L$(CURDIR)/lib \
vmtests.cpp stubhost.cpp ../source/vm.cpp ../source/picoluaapi.cpp ../source/Input.cpp
+
+filehelperstests.o: filehelperstests.cpp ../source/filehelpers.cpp ../source/filehelpers.h
+ g++ $(CFLAGS) filehelperstests.cpp ../source/filehelpers.cpp
+
diff --git a/test/carttests.cpp b/test/carttests.cpp
index 96b1ca7..b3fa6f5 100644
--- a/test/carttests.cpp
+++ b/test/carttests.cpp
@@ -2,10 +2,10 @@
#include "../source/cart.h"
TEST_CASE("Loads bios cart") {
- Cart* cart = new Cart("__FAKE08-BIOS.p8");
+ Cart* cart = new Cart("__FAKE08-BIOS.p8", "");
- SUBCASE("filename is correct") {
- CHECK(cart->Filename == "__FAKE08-BIOS.p8");
+ SUBCASE("FullCartPath is correct") {
+ CHECK(cart->FullCartPath == "__FAKE08-BIOS.p8");
}
SUBCASE("error is empty") {
CHECK(cart->LoadError == "");
@@ -33,10 +33,10 @@ TEST_CASE("Loads bios cart") {
}
TEST_CASE("Load simple p8 cart") {
- Cart* cart = new Cart("carts/cartparsetest.p8");
+ Cart* cart = new Cart("carts/cartparsetest.p8", "");
- SUBCASE("filename is correct") {
- CHECK(cart->Filename == "carts/cartparsetest.p8");
+ SUBCASE("FullCartPath is correct") {
+ CHECK(cart->FullCartPath == "carts/cartparsetest.p8");
}
SUBCASE("error is empty") {
CHECK(cart->LoadError == "");
@@ -137,10 +137,10 @@ TEST_CASE("Load simple p8 cart") {
}
TEST_CASE("Load simple png cart") {
- Cart* cart = new Cart("carts/cartparsetest.p8.png");
+ Cart* cart = new Cart("cartparsetest.p8.png", "carts");
- SUBCASE("filename is correct") {
- CHECK(cart->Filename == "carts/cartparsetest.p8.png");
+ SUBCASE("FullCartPath is correct") {
+ CHECK(cart->FullCartPath == "carts/cartparsetest.p8.png");
}
SUBCASE("error is empty") {
CHECK(cart->LoadError == "");
@@ -196,10 +196,10 @@ TEST_CASE("Load legacy png cart") {
//MIT license
//Might be good to have a more complex test cart for this scenario, but for now
//just making sure its decompressed is better than nothing
- Cart* cart = new Cart("carts/test_legacypng_cart.p8.png");
+ Cart* cart = new Cart("test_legacypng_cart.p8.png", "carts");
- SUBCASE("filename is correct") {
- CHECK(cart->Filename == "carts/test_legacypng_cart.p8.png");
+ SUBCASE("FullCartPath is correct") {
+ CHECK(cart->FullCartPath == "carts/test_legacypng_cart.p8.png");
}
SUBCASE("error is empty") {
CHECK(cart->LoadError == "");
@@ -216,3 +216,13 @@ TEST_CASE("Load legacy png cart") {
delete cart;
}
+
+TEST_CASE("cart loading options") {
+ Cart* cart = new Cart("#cartparsetest", "carts");
+
+ SUBCASE("FullCartPath is correct") {
+ CHECK(cart->FullCartPath == "carts/cartparsetest.p8");
+ }
+
+ delete cart;
+}
\ No newline at end of file
diff --git a/test/filehelperstests.cpp b/test/filehelperstests.cpp
new file mode 100644
index 0000000..c4d885d
--- /dev/null
+++ b/test/filehelperstests.cpp
@@ -0,0 +1,65 @@
+#include "doctest.h"
+#include "../source/filehelpers.h"
+
+TEST_CASE("isAbsolutePath") {
+ SUBCASE("empty string") {
+ CHECK_EQ(isAbsolutePath(""), false);
+ }
+ SUBCASE("filename no etension") {
+ CHECK_EQ(isAbsolutePath("file"), false);
+ }
+ SUBCASE("filename with etension") {
+ CHECK_EQ(isAbsolutePath("cart.p8"), false);
+ }
+ SUBCASE("filename with extension in folder") {
+ CHECK_EQ(isAbsolutePath("dir/cart.p8"), false);
+ }
+ SUBCASE("path starting with slash") {
+ CHECK_EQ(isAbsolutePath("/dir1/dir2/cart.p8"), true);
+ }
+ SUBCASE("vita path") {
+ CHECK_EQ(isAbsolutePath("ux0:/data/dir2/cart.p8"), true);
+ }
+}
+
+TEST_CASE("getDirectory") {
+ SUBCASE("empty string") {
+ CHECK_EQ(getDirectory(""), "");
+ }
+ SUBCASE("filename no etension") {
+ CHECK_EQ(getDirectory("file"), "");
+ }
+ SUBCASE("filename with etension") {
+ CHECK_EQ(getDirectory("cart.p8"), "");
+ }
+ SUBCASE("filename with extension in folder") {
+ CHECK_EQ(getDirectory("dir/cart.p8"), "dir");
+ }
+ SUBCASE("path starting with slash") {
+ CHECK_EQ(getDirectory("/dir1/dir2/cart.p8"), "/dir1/dir2");
+ }
+ SUBCASE("vita path") {
+ CHECK_EQ(getDirectory("ux0:/data/dir2/cart.p8"), "ux0:/data/dir2");
+ }
+}
+
+TEST_CASE("getFileExtension") {
+ SUBCASE("empty string") {
+ CHECK_EQ(getFileExtension(""), "");
+ }
+ SUBCASE("filename no etension") {
+ CHECK_EQ(getFileExtension("file"), "");
+ }
+ SUBCASE("filename with etension") {
+ CHECK_EQ(getFileExtension("cart.p8"), ".p8");
+ }
+ SUBCASE("filename with extension in folder") {
+ CHECK_EQ(getFileExtension("dir/cart.p8"), ".p8");
+ }
+ SUBCASE("path starting with slash") {
+ CHECK_EQ(getFileExtension("/dir1/dir2/cart.p8.png"), ".png");
+ }
+ SUBCASE("vita path") {
+ CHECK_EQ(getFileExtension("ux0:/data/di.r2/ca.rt.p8.longer"), ".longer");
+ }
+}