Implement need_fullpath support

This makes it much less painful to specify disk images (as well as reduces memory consumption when loading!)
This commit is contained in:
Lily Tsuru 2024-08-06 21:57:47 -04:00
parent 16e6875228
commit 3be054c390

View file

@ -68,6 +68,7 @@ pub struct Frontend {
pub(crate) game_loaded: bool, pub(crate) game_loaded: bool,
pub(crate) av_info: Option<SystemAvInfo>, pub(crate) av_info: Option<SystemAvInfo>,
pub(crate) sys_info: Option<SystemInfo>,
/// The core's requested pixel format. /// The core's requested pixel format.
/// TODO: HW accel. (or just not care) /// TODO: HW accel. (or just not care)
@ -114,6 +115,7 @@ impl Frontend {
game_loaded: false, game_loaded: false,
av_info: None, av_info: None,
sys_info: None,
pixel_format: PixelFormat::RGB565, pixel_format: PixelFormat::RGB565,
converted_pixel_buffer: Vec::new(), converted_pixel_buffer: Vec::new(),
@ -374,32 +376,46 @@ impl Frontend {
return Err(Error::CoreNotLoaded); return Err(Error::CoreNotLoaded);
} }
// For now I'm only implementing the gameinfo garbage that
// makes you read the whole file in. Later on I'll look into VFS
// support; but for now, it seems more cores will probably
// play ball with this.. which sucks :(
// I'm aware this is nasty but bleh // I'm aware this is nasty but bleh
let slice = path.as_ref().as_os_str().as_bytes(); let slice = path.as_ref().as_os_str().as_bytes();
let path_string = CString::new(slice).expect("shouldn't fail"); let path_string = CString::new(slice).expect("shouldn't fail");
let contents = fs::read(path)?;
let gameinfo = GameInfo { let system_info = self.get_system_info()?;
path: path_string.as_ptr(),
data: contents.as_ptr() as *const ffi::c_void,
size: contents.len(),
meta: std::ptr::null(),
};
let core_api = self.core_api.as_ref().unwrap(); let core_api = self.core_api.as_ref().unwrap();
unsafe { let mut gameinfo = GameInfo {
if !(core_api.retro_load_game)(&gameinfo) { path: path_string.as_ptr(),
return Err(Error::RomLoadFailed); data: std::ptr::null(),
} size: 0,
meta: std::ptr::null(),
};
self.game_loaded = true; // If the core does not need fullpath, then
Ok(()) // read the file data into a buffer we give to the core.
// This is pretty wasteful but works.
if !system_info.need_fullpath {
let contents = fs::read(path)?;
gameinfo.data = contents.as_ptr() as *const ffi::c_void;
gameinfo.size = contents.len();
unsafe {
if !(core_api.retro_load_game)(&gameinfo) {
return Err(Error::RomLoadFailed);
}
self.game_loaded = true;
Ok(())
}
} else {
unsafe {
if !(core_api.retro_load_game)(&gameinfo) {
return Err(Error::RomLoadFailed);
}
self.game_loaded = true;
Ok(())
}
} }
} }
@ -443,6 +459,28 @@ impl Frontend {
} }
} }
pub fn get_system_info(&mut self) -> Result<SystemInfo> {
if !self.core_loaded() {
return Err(Error::CoreNotLoaded);
}
if let Some(sys) = self.sys_info.as_ref() {
Ok(sys.clone())
} else {
let mut sys_info: MaybeUninit<SystemInfo> = MaybeUninit::uninit();
// Actually get the system info
unsafe {
let core_api = self.core_api.as_ref().unwrap();
(core_api.retro_get_system_info)(sys_info.as_mut_ptr());
self.sys_info = Some(sys_info.assume_init());
}
Ok(self.sys_info.as_ref().unwrap().clone())
}
}
pub fn get_size(&mut self) -> (u32, u32) { pub fn get_size(&mut self) -> (u32, u32) {
(self.fb_width, self.fb_height) (self.fb_width, self.fb_height)
} }