foxygit / fake-08-xbox360 Log in
commit f00a1f8537cbbefdc9e77f803af98587a27665b3
Author:     jtothebell <j.to.the.bell@gmail.com>
AuthorDate: Wed Jun 16 21:54:17 2021 -0600
Commit:     jtothebell <j.to.the.bell@gmail.com>
CommitDate: Wed Jun 16 21:54:17 2021 -0600

    Refactor __loadcart to match pico 8 load api
---
 source/FakoBios.h     |  4 ++--
 source/picoluaapi.cpp | 32 +++++++++++++++++++++++---------
 source/picoluaapi.h   |  3 ++-
 source/vm.cpp         | 18 +++++++++++++++++-
 source/vm.h           |  9 +++++++++
 5 files changed, 53 insertions(+), 13 deletions(-)

diff --git a/source/FakoBios.h b/source/FakoBios.h
index 8aaa12d..0d5dc5c 100644
--- a/source/FakoBios.h
+++ b/source/FakoBios.h
@@ -84,8 +84,8 @@ function _update60()
 		--make call to global
 		--load cart here

-		if __loadcart and carttoload then
-			__loadcart(carttoload)
+		if load and carttoload then
+			load(carttoload)
 		end
 	end

diff --git a/source/picoluaapi.cpp b/source/picoluaapi.cpp
index 929733b..9540b9d 100644
--- a/source/picoluaapi.cpp
+++ b/source/picoluaapi.cpp
@@ -785,7 +785,7 @@ int stat(lua_State *L) {
         //argument
         case 6:
             // no args or loading other cards currently supported
-            lua_pushstring(L, "");
+            lua_pushstring(L, _vmForLuaApi->getCartParam().c_str());
             return 1;
         break;
         //frame rate
@@ -853,6 +853,9 @@ int stat(lua_State *L) {
             lua_pushnumber(L, _vmForLuaApi->getSecond());
             return 1;
         break;
+        case 100:
+            lua_pushstring(L, _vmForLuaApi->getCartBreadcrumb().c_str());
+            return 1;
         //unknown? used by serial carts
         case 108:
             lua_pushnumber(L, 32);
@@ -1105,6 +1108,25 @@ int extcmd(lua_State *L){
     return 0;
 }

+int load(lua_State *L) {
+    const char* filename = "";
+    const char* breadcrumb = "";
+    const char* param = "";
+    if (lua_isstring(L, 1)){
+        filename = lua_tolstring(L, 1, nullptr);
+        if (lua_gettop(L) > 1){
+            breadcrumb = lua_tolstring(L, 2, nullptr);
+        }
+        if (lua_gettop(L) > 2){
+            param = lua_tolstring(L, 3, nullptr);
+        }
+
+        _vmForLuaApi->vm_load(filename, breadcrumb, param);
+    }
+
+    return 0;
+}
+
 int listcarts(lua_State *L) {
     //get cart list from VM (who should get it from host)
     vector<string> carts = _vmForLuaApi->GetCartList();
@@ -1123,15 +1145,7 @@ int listcarts(lua_State *L) {
     return 1;
 }

-int loadcart(lua_State *L) {
-    if (lua_isstring(L, 1)){
-        const char * str = "";
-        str = lua_tolstring(L, 1, nullptr);
-        _vmForLuaApi->QueueCartChange(str);
-    }

-    return 0;
-}

 int getbioserror(lua_State *L) {
     string error = _vmForLuaApi->GetBiosError();
diff --git a/source/picoluaapi.h b/source/picoluaapi.h
index 2f9cda9..713b97f 100644
--- a/source/picoluaapi.h
+++ b/source/picoluaapi.h
@@ -92,7 +92,7 @@ int printh(lua_State *L);

 //file system/vm functions
 int listcarts(lua_State *L);
-int loadcart(lua_State *L);
+
 int getbioserror(lua_State *L);
 int loadbioscart(lua_State *L);
 int togglepausemenu(lua_State *L);
@@ -105,6 +105,7 @@ int srand(lua_State *L);
 int _update_buttons(lua_State *L);
 int run(lua_State *L);
 int extcmd(lua_State *L);
+int load(lua_State *L);

 //api.tonum(val)

diff --git a/source/vm.cpp b/source/vm.cpp
index 5c68c36..4aedfe1 100644
--- a/source/vm.cpp
+++ b/source/vm.cpp
@@ -162,11 +162,11 @@ bool Vm::loadCart(Cart* cart) {
     //system
     //must be registered before loading globals for pause menu to work
     lua_register(_luaState, "__listcarts", listcarts);
-    lua_register(_luaState, "__loadcart", loadcart);
     lua_register(_luaState, "__getbioserror", getbioserror);
     lua_register(_luaState, "__loadbioscart", loadbioscart);
     lua_register(_luaState, "__togglepausemenu", togglepausemenu);
     lua_register(_luaState, "__resetcart", resetcart);
+    lua_register(_luaState, "load", load);

     //load in global lua fuctions for pico 8
     auto convertedGlobalLuaFunctions = convert_emojis(p8GlobalLuaFunctions);
@@ -439,6 +439,7 @@ void Vm::UpdateAndDraw() {
     }

     if (_cartChangeQueued) {
+        _prevCartKey = CurrentCartFilename();
         LoadCart(_nextCartKey);
     }

@@ -898,6 +899,13 @@ void Vm::vm_extcmd(std::string cmd){
     }
 }

+void Vm::vm_load(std::string filename, std::string breadcrumb, std::string param){
+    _cartBreadcrumb = breadcrumb;
+    _cartParam = param;
+
+    QueueCartChange(filename);
+}
+
 int Vm::getFps(){
     //TODO: return actual fps (as fix32?)
     return _targetFps;
@@ -948,3 +956,11 @@ int Vm::getSecond(){

     return now->tm_sec;
 }
+
+std::string Vm::getCartBreadcrumb() {
+    return _cartBreadcrumb;
+}
+
+std::string Vm::getCartParam() {
+    return _cartParam;
+}
diff --git a/source/vm.h b/source/vm.h
index b331c2d..2e071e7 100644
--- a/source/vm.h
+++ b/source/vm.h
@@ -40,12 +40,16 @@ class Vm {

     bool _cartChangeQueued;
     bool _pauseMenu;
+    string _prevCartKey;
     string _nextCartKey;

     string _cartLoadError;

     string _cartdataKey;

+    string _cartBreadcrumb;
+    string _cartParam;
+
     vector<string> _cartList;

     bool loadCart(Cart* cart);
@@ -127,6 +131,8 @@ class Vm {
     void vm_run();
     void vm_extcmd(string  cmd);

+    void vm_load(string filename, string breadcrumb, string param);
+
     int getFps();
     int getTargetFps();

@@ -136,5 +142,8 @@ class Vm {
     int getHour();
     int getMinute();
     int getSecond();
+
+    string getCartBreadcrumb();
+    string getCartParam();
 };