commit 13bd0e79e4940cda6cd1712b24e36d5be48b9a2c
Author: Jon Bell <j.to.the.bell@gmail.com>
AuthorDate: Sun Feb 19 20:09:09 2023 -0700
Commit: Jon Bell <j.to.the.bell@gmail.com>
CommitDate: Sun Feb 19 20:09:09 2023 -0700
Allow libretro to load roms from memory
---
platform/libretro/libretro.cpp | 17 +++++++++++++++--
source/vm.cpp | 35 ++++++++++++++++++++++++++++++++++-
source/vm.h | 4 ++++
3 files changed, 53 insertions(+), 3 deletions(-)
diff --git a/platform/libretro/libretro.cpp b/platform/libretro/libretro.cpp
index 927ea8d..2b289ee 100644
--- a/platform/libretro/libretro.cpp
+++ b/platform/libretro/libretro.cpp
@@ -150,7 +150,7 @@ EXPORT void retro_get_system_info(struct retro_system_info *info)
info->library_name = "fake-08";
info->library_version = "0.0.2.19"; //todo: get from build flags
info->valid_extensions = "p8|png";
- info->need_fullpath = true; // we load our own carts for now
+ info->need_fullpath = false;
}
EXPORT void retro_get_system_av_info(struct retro_system_av_info *info)
@@ -456,13 +456,26 @@ EXPORT void retro_cheat_set(unsigned index, bool enabled, const char *code)
EXPORT bool retro_load_game(struct retro_game_info const *info)
{
+ printf("loading game\n");
auto containingDir = getDirectory(info->path);
+ printf("containing dir: %s\n", containingDir.c_str());
+
if (containingDir.length() > 0) {
setCartDirectory(containingDir);
}
- _vm->QueueCartChange(info->path);
+ if (info->size > 0) {
+ printf("using memory for rom. size: %zu\n", info->size);
+ const unsigned char* data = reinterpret_cast<const unsigned char*>(info->data);
+ _vm->QueueCartChange(data, info->size);
+
+ }
+ else {
+ printf("using file path for rom %s\n", info->path);
+ _vm->QueueCartChange(info->path);
+ }
+
return true;
}
diff --git a/source/vm.cpp b/source/vm.cpp
index 10118dc..3631629 100644
--- a/source/vm.cpp
+++ b/source/vm.cpp
@@ -434,6 +434,24 @@ void Vm::LoadCart(std::string filename, bool loadBiosOnFail){
}
}
+void Vm::LoadCart(const unsigned char* cartData, size_t size, bool loadBiosOnFail){
+ Logger_Write("Loading cart from memory\n");
+ CloseCart();
+
+ Logger_Write("Calling Cart Constructor\n");
+ Cart *cart = new Cart(cartData, size);
+
+ _cartLoadError = cart->LoadError;
+
+ bool success = loadCart(cart);
+
+ if (loadBiosOnFail && !success) {
+ CloseCart();
+ //todo: show an error message on the bios?
+ LoadBiosCart();
+ }
+}
+
void Vm::togglePauseMenu(){
_input->SetState(0, 0);
if (_memory->drawState.suppressPause) {
@@ -527,7 +545,12 @@ void Vm::UpdateAndDraw() {
if (_cartChangeQueued) {
_prevCartKey = CurrentCartFilename();
- LoadCart(_nextCartKey);
+ if (_nextCartSize > 0){
+ LoadCart(_nextCartData, _nextCartSize);
+ }
+ else {
+ LoadCart(_nextCartKey);
+ }
}
if (_pauseMenu){
@@ -622,6 +645,16 @@ void Vm::CloseCart() {
void Vm::QueueCartChange(std::string filename){
_nextCartKey = filename;
+ _nextCartData = nullptr;
+ _nextCartSize = 0;
+ _cartChangeQueued = true;
+ _pauseMenu = false;
+}
+
+void Vm::QueueCartChange(const unsigned char* cartData, size_t size){
+ _nextCartKey = "";
+ _nextCartData = cartData;
+ _nextCartSize = size;
_cartChangeQueued = true;
_pauseMenu = false;
}
diff --git a/source/vm.h b/source/vm.h
index 2107b04..e4c3031 100644
--- a/source/vm.h
+++ b/source/vm.h
@@ -42,6 +42,8 @@ class Vm {
bool _pauseMenu;
string _prevCartKey;
string _nextCartKey;
+ const unsigned char* _nextCartData;
+ size_t _nextCartSize;
string _cartLoadError;
@@ -70,6 +72,7 @@ class Vm {
void LoadSettingsCart();
void LoadCart(string filename, bool loadBiosOnFail = true);
+ void LoadCart(const unsigned char* cartData, size_t size, bool loadBiosOnFail = true);
void UpdateAndDraw();
@@ -81,6 +84,7 @@ class Vm {
void CloseCart();
void QueueCartChange(string newcart);
+ void QueueCartChange(const unsigned char* cartData, size_t size);
int GetTargetFps();