actually load multiple separate instances of the core (OS will deduplicate by path)

This commit is contained in:
lifning 2021-11-08 22:34:53 -08:00
parent 53642557a0
commit aa33f331f0
3 changed files with 12 additions and 4 deletions

View File

@ -11,4 +11,4 @@ cc = "^1"
libretro-sys = "0.1"
libloading = "0.5"
num_enum = "0.4"
once_cell = "1.8"
once_cell = "1"

View File

@ -15,6 +15,7 @@ ffmpeg-next = { version = "4.3.8", optional = true }
sdl2 = { version = "0.32", optional = true, features = ["gfx"] }
gl = { version = "0.14", optional = true }
crossbeam-channel = { version = "0.4", optional = true }
tempfile = "3"
[dev-dependencies]
crossbeam-channel = "0.4"

View File

@ -4,6 +4,7 @@
use crate::prelude::*;
use ferretro_base::retro::ffi::*;
use ferretro_base::prelude::{LibretroWrapperAccess, RetroHandlerId};
use core::any::{Any, TypeId};
use core::pin::Pin;
@ -13,7 +14,6 @@ use std::path::{PathBuf, Path};
use std::io::Read;
use std::collections::HashMap;
use std::ffi::c_void;
use ferretro_base::prelude::{LibretroWrapperAccess, RetroHandlerId};
pub struct RetroComponentBase {
retro: LibretroWrapper,
@ -37,6 +37,7 @@ pub struct RetroComponentBase {
cached_geometry: Option<GameGeometry>,
handler_id: Option<RetroHandlerId>,
_temp_dir: tempfile::TempDir,
}
// TODO: replace with std::ops::ControlFlow when it becomes stable
@ -61,13 +62,18 @@ pub trait RetroComponent: RetroCallbacks + Any {
impl RetroComponentBase {
// TODO: constructor & wrapper that uses a statically linked libretro?
pub fn new(core_path: impl AsRef<Path>) -> Pin<Box<Self>> {
let lib = libloading::Library::new(core_path.as_ref()).unwrap();
// allow multiple copies of same lib, possibly?
let temp_dir = tempfile::tempdir().unwrap();
let libretro_path = temp_dir.path().join(core_path.as_ref().file_name().unwrap());
std::fs::copy(core_path.as_ref(), &libretro_path).unwrap();
let lib = libloading::Library::new(&libretro_path).unwrap();
let raw_retro = ferretro_base::retro::loading::LibretroApi::from_library(lib).unwrap();
let retro = LibretroWrapper::from(raw_retro);
let emu = RetroComponentBase {
retro,
libretro_path: core_path.as_ref().to_path_buf(),
libretro_path,
components: Default::default(),
component_ptrs: Default::default(),
cached_rom_path: None,
@ -82,6 +88,7 @@ impl RetroComponentBase {
cached_memory_map: None,
cached_geometry: None,
handler_id: None,
_temp_dir: temp_dir,
};
let mut pin_emu = Box::pin(emu);