foxygit / fake-08-xbox360 Log in
commit 435fe5888e2def5a862bd5da843ce174c774101b
Author:     Jon Bell <j.to.the.bell@gmail.com>
AuthorDate: Wed Dec 31 12:39:17 2025 -0700
Commit:     Jon Bell <j.to.the.bell@gmail.com>
CommitDate: Wed Dec 31 12:39:17 2025 -0700

    handle cart loading errors better
---
 source/cart.cpp | 18 ++++++++++++++++++
 source/vm.cpp   | 32 +++++++++++++++++++++++++++-----
 2 files changed, 45 insertions(+), 5 deletions(-)

diff --git a/source/cart.cpp b/source/cart.cpp
index 6bf2e99..a8b5aaa 100644
--- a/source/cart.cpp
+++ b/source/cart.cpp
@@ -463,6 +463,15 @@ Cart::Cart(std::string filename, std::string cartDirectory){
     Logger_Write("getting file contents for %s\n", FullCartPath.c_str());

     std::string firstFourChars = get_first_four_chars(FullCartPath);
+
+    // For non-built-in carts, check if file exists first
+    if (!isDefaultCart && !isSettingsCart) {
+        if (firstFourChars.length() == 0) {
+            LoadError = "cart file not found: " + FullCartPath;
+            Logger_Write("%s\n", LoadError.c_str());
+            return;
+        }
+    }

     if (isDefaultCart || isSettingsCart || firstFourChars == "pico"){
         std::string cartStr;
@@ -477,6 +486,12 @@ Cart::Cart(std::string filename, std::string cartDirectory){
         }
         else {
             cartStr = get_file_contents(FullCartPath.c_str());
+            // File existence already checked above, but double-check just in case
+            if (cartStr.length() == 0) {
+                LoadError = "cart file not found: " + FullCartPath;
+                Logger_Write("%s\n", LoadError.c_str());
+                return;
+            }
         }
         Logger_Write("Got file contents... parsing cart\n");

@@ -497,6 +512,9 @@ Cart::Cart(std::string filename, std::string cartDirectory){
         Logger_Write("got valid png cart\n");
     }
     else {
+        // File exists but format is unknown
+        LoadError = "unknown cart file format: " + FullCartPath;
+        Logger_Write("%s\n", LoadError.c_str());
         return;
     }
 }
diff --git a/source/vm.cpp b/source/vm.cpp
index b1b416f..d7ba4b8 100644
--- a/source/vm.cpp
+++ b/source/vm.cpp
@@ -362,6 +362,9 @@ bool Vm::loadCart(Cart* cart) {
             _cartLoadError = "No Lua to load. Aborting cart load";
         }
         Logger_Write("%s\n", _cartLoadError.c_str());
+
+        // Clear cart change queue to prevent infinite loop
+        _cartChangeQueued = false;

         return false;
     }
@@ -397,7 +400,11 @@ bool Vm::loadCart(Cart* cart) {
         ExecuteLua("__addbreadcrumb(\"" + _cartBreadcrumb +"\", \"" + _prevCartKey +"\")", "");
     }

-    _cartLoadError = "";
+    // Only clear error if we successfully loaded a non-default cart
+    // (preserve error when loading default cart to display it)
+    if (cart->FullCartPath != DefaultCartName) {
+        _cartLoadError = "";
+    }

     return true;
 }
@@ -444,13 +451,26 @@ void Vm::LoadCart(std::string filename, bool loadBiosOnFail){

     _cartLoadError = cart->LoadError;

+    // If cart has a load error (file not found, etc.), load default cart with error
+    if (cart->LoadError.length() > 0) {
+        Logger_Write("Cart load error: %s\n", cart->LoadError.c_str());
+        // Save the error message so default cart can display it
+        _cartLoadError = cart->LoadError;
+        delete cart;
+        // Load default cart to display error
+        LoadBiosCart();
+        return;
+    }
+
     bool success = loadCart(cart);
     Logger_Write("Loaded cart into vm\n");

-    if (loadBiosOnFail && !success) {
-        CloseCart();
-        //todo: show an error message on the bios?
-        //LoadBiosCart();
+    if (!success) {
+        delete cart;
+        if (_cartLoadError.length() == 0) {
+            _cartLoadError = "Cart file has no Lua code";
+        }
+        LoadBiosCart();
     }
 }

@@ -606,6 +626,8 @@ bool Vm::Step(){
         }
         else {
             Logger_Write("Cart load failed, not running vm_run()\n");
+            // Clear cart change queue to prevent infinite retry loop
+            _cartChangeQueued = false;
         }
     }