foxygit / fake-08-xbox360 Log in
commit 765bcc0ded9ace83d00c860eab30167d7a7e3a2f
Author:     Jon Bell <j.to.the.bell@gmail.com>
AuthorDate: Sat Jul 30 19:28:43 2022 -0600
Commit:     Jon Bell <j.to.the.bell@gmail.com>
CommitDate: Sat Jul 30 19:28:43 2022 -0600

    Cart data working with libretro
    There is probably a better way, but this is working for now
---
 platform/libretro/libretro.cpp     | 11 +++++++++++
 platform/libretro/libretrohost.cpp | 18 +++++++++++++-----
 source/host.h                      |  1 +
 source/vm.cpp                      |  9 +++++----
 4 files changed, 30 insertions(+), 9 deletions(-)

diff --git a/platform/libretro/libretro.cpp b/platform/libretro/libretro.cpp
index f5ad8b6..9dbcdb7 100644
--- a/platform/libretro/libretro.cpp
+++ b/platform/libretro/libretro.cpp
@@ -10,6 +10,7 @@
 #include "../../source/host.h"
 #include "../../source/hostVmShared.h"
 #include "../../source/nibblehelpers.h"
+#include "../../source/filehelpers.h"
 #include "setInput.h"


@@ -85,6 +86,16 @@ EXPORT void retro_init()
 {
     //called once. do setup (create host and vm?)
     _host = new Host();
+
+    char const *save_dir;
+    if (enviro_cb(RETRO_ENVIRONMENT_GET_SAVE_DIRECTORY, &save_dir) && save_dir) {
+        string saveDirStr = save_dir;
+        if (! hasEnding(saveDirStr, "/")) {
+            saveDirStr = saveDirStr + "/";
+        }
+        _host->overrideLogFilePrefix(saveDirStr.c_str());
+    }
+
 	_memory = new PicoRam();
 	_audio = new Audio(_memory);

diff --git a/platform/libretro/libretrohost.cpp b/platform/libretro/libretrohost.cpp
index 29e4657..7cf4652 100644
--- a/platform/libretro/libretrohost.cpp
+++ b/platform/libretro/libretrohost.cpp
@@ -2,6 +2,8 @@
 #include <stdio.h>
 #include <string.h>
 #include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>

 #include <fstream>
 #include <iostream>
@@ -102,11 +104,17 @@ std::string Host::customBiosLua() {
     return "";
 }

-
-
-
-
-
 std::string Host::getCartDirectory() {
     return "carts";
 }
+
+void Host::overrideLogFilePrefix(const char* newPrefix) {
+    _logFilePrefix = newPrefix;
+
+    struct stat st = {0};
+
+    string cartdatadir = _logFilePrefix + "cdata";
+    if (stat(cartdatadir.c_str(), &st) == -1) {
+        mkdir(cartdatadir.c_str(), 0777);
+    }
+}
diff --git a/source/host.h b/source/host.h
index bc93723..906bf7b 100644
--- a/source/host.h
+++ b/source/host.h
@@ -124,6 +124,7 @@ class Host {

     std::vector<std::string> listcarts();

+    void overrideLogFilePrefix(const char* newPrefix);
     const char* logFilePrefix();

     std::string customBiosLua();
diff --git a/source/vm.cpp b/source/vm.cpp
index e6484ca..8b1d6ea 100644
--- a/source/vm.cpp
+++ b/source/vm.cpp
@@ -118,10 +118,6 @@ bool abortLua;
 bool Vm::loadCart(Cart* cart) {
     _picoFrameCount = 0;

-    if (_cartdataKey.length() > 0) {
-        _host->saveCartData(_cartdataKey, getSerializedCartData());
-    }
-
     _cartdataKey = "";

     //reset memory (may have to be more selective about zeroing out to be accurate?)
@@ -599,6 +595,11 @@ void Vm::CloseCart() {
         _luaState = nullptr;
     }

+    Logger_Write("writing cart data\n");
+    if (_cartdataKey.length() > 0) {
+        _host->saveCartData(_cartdataKey, getSerializedCartData());
+    }
+
     Logger_Write("resetting state\n");
     _targetFps = 30;
     _picoFrameCount = 0;