foxygit / fake-08-xbox360 Log in
commit 02ba1cf03133b726fb8b48c2f586efa5fe5d676b
Author:     jtothebell <j.to.the.bell@gmail.com>
AuthorDate: Mon May 3 19:29:12 2021 -0600
Commit:     jtothebell <j.to.the.bell@gmail.com>
CommitDate: Mon May 3 19:29:12 2021 -0600

    WIP refactor to store cart dir
---
 platform/SDL2Common/source/sdl2basehost.cpp |  8 +++++++-
 platform/SDL2Desktop/source/SDL2Host.cpp    | 18 +++++++++++-------
 source/FakoBios.h                           |  2 +-
 source/cart.cpp                             |  8 --------
 source/filehelpers.cpp                      |  8 ++++++++
 source/filehelpers.h                        |  2 ++
 source/host.h                               |  6 +++++-
 source/vm.cpp                               |  7 ++++++-
 8 files changed, 40 insertions(+), 19 deletions(-)

diff --git a/platform/SDL2Common/source/sdl2basehost.cpp b/platform/SDL2Common/source/sdl2basehost.cpp
index 417805d..e663f5d 100644
--- a/platform/SDL2Common/source/sdl2basehost.cpp
+++ b/platform/SDL2Common/source/sdl2basehost.cpp
@@ -152,7 +152,8 @@ void Host::setPlatformParams(
     uint32_t sdlRendererFlags,
     uint32_t sdlPixelFormat,
     std::string logFilePrefix,
-    std::string customBiosLua)
+    std::string customBiosLua,
+    std::string cartDirectory)
 {
     _windowWidth = windowWidth;
     _windowHeight = windowHeight;
@@ -172,6 +173,7 @@ void Host::setPlatformParams(

     _logFilePrefix = logFilePrefix;
     _customBiosLua = customBiosLua;
+    _cartDirectory = cartDirectory;

 }

@@ -420,3 +422,7 @@ const char* Host::logFilePrefix() {
 std::string Host::customBiosLua() {
     return _customBiosLua;
 }
+
+std::string Host::getCartDirectory() {
+    return _cartDirectory;
+}
diff --git a/platform/SDL2Desktop/source/SDL2Host.cpp b/platform/SDL2Desktop/source/SDL2Host.cpp
index 9458d03..879d1a1 100644
--- a/platform/SDL2Desktop/source/SDL2Host.cpp
+++ b/platform/SDL2Desktop/source/SDL2Host.cpp
@@ -14,6 +14,7 @@ using namespace std;
 #include "../../SDL2Common/source/sdl2basehost.h"
 #include "../../../source/hostVmShared.h"
 #include "../../../source/nibblehelpers.h"
+#include "../../../source/filehelpers.h"
 #include "../../../source/logger.h"

 // sdl
@@ -52,6 +53,10 @@ Host::Host()
         res = mkdir(cartdatadir.c_str(), 0777);
     }

+    std::string home = getenv("HOME");
+
+    std::string fullCartDir = home + "/p8carts";
+
     setPlatformParams(
         WINDOW_SIZE_X,
         WINDOW_SIZE_Y,
@@ -59,7 +64,8 @@ Host::Host()
         RENDERER_FLAGS,
         PIXEL_FORMAT,
         _desktopSdl2SettingsPrefix,
-        _desktopSdl2customBiosLua
+        _desktopSdl2customBiosLua,
+        fullCartDir
     );
 }

@@ -145,19 +151,17 @@ InputState_t Host::scanInput(){
     };
 }

-
 vector<string> Host::listcarts(){
     vector<string> carts;

     DIR *dir;
     struct dirent *ent;
-    std::string home = getenv("HOME");
-    std::string cartDir = "/p8carts";
-    std::string fullCartDir = home + cartDir;
-    if ((dir = opendir (fullCartDir.c_str())) != NULL) {
+    if ((dir = opendir (_cartDirectory.c_str())) != NULL) {
         /* print all the files and directories within directory */
         while ((ent = readdir (dir)) != NULL) {
-            carts.push_back(fullCartDir + "/" + ent->d_name);
+            if (hasEnding(ent->d_name, ".p8") || hasEnding(ent->d_name, ".png")){
+                carts.push_back(ent->d_name);
+            }
         }
         closedir (dir);
     } else {
diff --git a/source/FakoBios.h b/source/FakoBios.h
index 3fad961..ec0588b 100644
--- a/source/FakoBios.h
+++ b/source/FakoBios.h
@@ -69,7 +69,7 @@ function _update60()
 		carttoload = carts[cidx]
 		local lastslashidx = string.find(carttoload, "/[^/]*$")
 		local dispstr = carttoload
-		if lastslashidx > 0 then
+		if lastslashidx ~= nil and lastslashidx > 0 then
 			dispstr = sub(dispstr, lastslashidx + 1)
 		end
 		linebuffer = "load " .. dispstr
diff --git a/source/cart.cpp b/source/cart.cpp
index 5c4727a..3a8c66f 100644
--- a/source/cart.cpp
+++ b/source/cart.cpp
@@ -146,14 +146,6 @@ static std::string pxa_decompress(uint8_t const *input)
     return ret;
 }

-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));
-    } else {
-        return false;
-    }
-}
-
 #define HEADERLEN 8


diff --git a/source/filehelpers.cpp b/source/filehelpers.cpp
index cb0fd61..43bcbea 100644
--- a/source/filehelpers.cpp
+++ b/source/filehelpers.cpp
@@ -36,3 +36,11 @@ std::string getDirectory(const std::string& fname){
          : fname.substr(0, 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));
+    } else {
+        return false;
+    }
+}
+
diff --git a/source/filehelpers.h b/source/filehelpers.h
index 50c67d4..dd3cd40 100644
--- a/source/filehelpers.h
+++ b/source/filehelpers.h
@@ -3,3 +3,5 @@
 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);
diff --git a/source/host.h b/source/host.h
index f22b806..e77e306 100644
--- a/source/host.h
+++ b/source/host.h
@@ -32,6 +32,7 @@ class Host {

     std::string _logFilePrefix;
     std::string _customBiosLua;
+    std::string _cartDirectory;

     void loadSettingsIni();
     void saveSettingsIni();
@@ -75,6 +76,8 @@ class Host {

     void saveCartData(std::string cartDataKey, std::string contents);

+    std::string getCartDirectory();
+
     void setPlatformParams(
         int windowWidth,
         int windowHeight,
@@ -82,5 +85,6 @@ class Host {
         uint32_t sdlRendererFlags,
         uint32_t sdlPixelFormat,
         std::string logFilePrefix,
-        std::string customBiosLua);
+        std::string customBiosLua,
+        std::string cartDirectory);
 };
diff --git a/source/vm.cpp b/source/vm.cpp
index b8a4b87..77de2f9 100644
--- a/source/vm.cpp
+++ b/source/vm.cpp
@@ -342,11 +342,16 @@ void Vm::LoadBiosCart(){
 }

 void Vm::LoadCart(std::string filename){
+    if (filename == "__FAKE08-BIOS.p8") {
+        LoadBiosCart();
+        return;
+    }
     Logger_Write("Loading cart %s\n", filename.c_str());
     CloseCart();

     Logger_Write("Calling Cart Constructor\n");
-    Cart *cart = new Cart(filename);
+    auto cartDir = _host->getCartDirectory();
+    Cart *cart = new Cart(cartDir + "/" + filename);

     _cartLoadError = cart->LoadError;