Choose save data location
This commit is contained in:
+1
-1
@@ -26,10 +26,10 @@ use crate::{
|
|||||||
config::{AppConfig, CliArgs},
|
config::{AppConfig, CliArgs},
|
||||||
controller::ControllerManager,
|
controller::ControllerManager,
|
||||||
emulator::{EmulatorClient, EmulatorCommand, SimId},
|
emulator::{EmulatorClient, EmulatorCommand, SimId},
|
||||||
|
filesystem::Persistence,
|
||||||
images::ImageTextureLoader,
|
images::ImageTextureLoader,
|
||||||
input::{MappingProvider, ShortcutProvider},
|
input::{MappingProvider, ShortcutProvider},
|
||||||
memory::MemoryClient,
|
memory::MemoryClient,
|
||||||
persistence::Persistence,
|
|
||||||
window::{AppWindow, ChildWindow, GameScreen, GameWindow, InitArgs},
|
window::{AppWindow, ChildWindow, GameScreen, GameWindow, InitArgs},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
+25
-1
@@ -1,9 +1,10 @@
|
|||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
use bytemuck::NoUninit;
|
||||||
use clap::{Parser, ValueEnum};
|
use clap::{Parser, ValueEnum};
|
||||||
use egui::{Color32, Pos2, Vec2};
|
use egui::{Color32, Pos2, Vec2};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::{emulator::SimId, persistence::Persistence, window::DisplayMode};
|
use crate::{emulator::SimId, filesystem::Persistence, window::DisplayMode};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
@@ -53,6 +54,9 @@ pub struct CliArgs {
|
|||||||
/// Set a preference for whether to use a low-power or high-performance GPU adapter
|
/// Set a preference for whether to use a low-power or high-performance GPU adapter
|
||||||
#[arg(long)]
|
#[arg(long)]
|
||||||
pub wgpu_power_preference: Option<PowerPreferenceWrapper>,
|
pub wgpu_power_preference: Option<PowerPreferenceWrapper>,
|
||||||
|
/// Choose whether save files are kept in the rom or the data directory
|
||||||
|
#[arg(long)]
|
||||||
|
pub save_data_location: Option<SaveDataLocation>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(ValueEnum, Clone, Copy)]
|
#[derive(ValueEnum, Clone, Copy)]
|
||||||
@@ -90,12 +94,27 @@ const fn default_power_preference() -> wgpu::PowerPreference {
|
|||||||
wgpu::PowerPreference::LowPower
|
wgpu::PowerPreference::LowPower
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Clone, Copy, PartialEq, Eq, ValueEnum, Debug, NoUninit)]
|
||||||
|
#[repr(usize)]
|
||||||
|
pub enum SaveDataLocation {
|
||||||
|
/// The same directory as the ROM
|
||||||
|
RomDirectory,
|
||||||
|
/// This application's data directory
|
||||||
|
DataDirectory,
|
||||||
|
}
|
||||||
|
|
||||||
|
const fn default_save_data_location() -> SaveDataLocation {
|
||||||
|
SaveDataLocation::RomDirectory
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Clone, PartialEq, Eq)]
|
#[derive(Serialize, Deserialize, Clone, PartialEq, Eq)]
|
||||||
pub struct AppConfig {
|
pub struct AppConfig {
|
||||||
#[serde(default = "default_power_preference")]
|
#[serde(default = "default_power_preference")]
|
||||||
pub power_preference: wgpu::PowerPreference,
|
pub power_preference: wgpu::PowerPreference,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub force_fallback: bool,
|
pub force_fallback: bool,
|
||||||
|
#[serde(default = "default_save_data_location")]
|
||||||
|
pub save_data_location: SaveDataLocation,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AppConfig {
|
impl AppConfig {
|
||||||
@@ -106,6 +125,7 @@ impl AppConfig {
|
|||||||
Self {
|
Self {
|
||||||
power_preference: default_power_preference(),
|
power_preference: default_power_preference(),
|
||||||
force_fallback: false,
|
force_fallback: false,
|
||||||
|
save_data_location: default_save_data_location(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -123,6 +143,10 @@ impl AppConfig {
|
|||||||
self.force_fallback = force_fallback;
|
self.force_fallback = force_fallback;
|
||||||
updated = true;
|
updated = true;
|
||||||
}
|
}
|
||||||
|
if let Some(save_data_location) = args.save_data_location {
|
||||||
|
self.save_data_location = save_data_location;
|
||||||
|
updated = true;
|
||||||
|
}
|
||||||
updated
|
updated
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+72
-44
@@ -18,6 +18,7 @@ use tracing::{error, warn};
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
audio::Audio,
|
audio::Audio,
|
||||||
|
config::SaveDataLocation,
|
||||||
graphics::TextureSink,
|
graphics::TextureSink,
|
||||||
memory::{MemoryRange, MemoryRegion},
|
memory::{MemoryRange, MemoryRegion},
|
||||||
};
|
};
|
||||||
@@ -74,12 +75,13 @@ pub struct EmulatorBuilder {
|
|||||||
state: Arc<Atomic<EmulatorState>>,
|
state: Arc<Atomic<EmulatorState>>,
|
||||||
audio_on: Arc<[AtomicBool; 2]>,
|
audio_on: Arc<[AtomicBool; 2]>,
|
||||||
linked: Arc<AtomicBool>,
|
linked: Arc<AtomicBool>,
|
||||||
|
save_data_location: Arc<Atomic<SaveDataLocation>>,
|
||||||
start_paused: bool,
|
start_paused: bool,
|
||||||
watch_rom: Arc<[AtomicBool; 2]>,
|
watch_rom: Arc<[AtomicBool; 2]>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl EmulatorBuilder {
|
impl EmulatorBuilder {
|
||||||
pub fn new() -> (Self, EmulatorClient) {
|
pub fn new(save_data_location: SaveDataLocation) -> (Self, EmulatorClient) {
|
||||||
let (queue, commands) = mpsc::channel();
|
let (queue, commands) = mpsc::channel();
|
||||||
let builder = Self {
|
let builder = Self {
|
||||||
rom: None,
|
rom: None,
|
||||||
@@ -91,6 +93,7 @@ impl EmulatorBuilder {
|
|||||||
state: Arc::new(Atomic::new(EmulatorState::Paused)),
|
state: Arc::new(Atomic::new(EmulatorState::Paused)),
|
||||||
audio_on: Arc::new([AtomicBool::new(true), AtomicBool::new(true)]),
|
audio_on: Arc::new([AtomicBool::new(true), AtomicBool::new(true)]),
|
||||||
linked: Arc::new(AtomicBool::new(false)),
|
linked: Arc::new(AtomicBool::new(false)),
|
||||||
|
save_data_location: Arc::new(Atomic::new(save_data_location)),
|
||||||
start_paused: false,
|
start_paused: false,
|
||||||
watch_rom: Arc::new([AtomicBool::new(false), AtomicBool::new(false)]),
|
watch_rom: Arc::new([AtomicBool::new(false), AtomicBool::new(false)]),
|
||||||
};
|
};
|
||||||
@@ -98,8 +101,9 @@ impl EmulatorBuilder {
|
|||||||
queue,
|
queue,
|
||||||
sim_state: builder.sim_state.clone(),
|
sim_state: builder.sim_state.clone(),
|
||||||
state: builder.state.clone(),
|
state: builder.state.clone(),
|
||||||
watch_rom: builder.watch_rom.clone(),
|
|
||||||
linked: builder.linked.clone(),
|
linked: builder.linked.clone(),
|
||||||
|
save_data_location: builder.save_data_location.clone(),
|
||||||
|
watch_rom: builder.watch_rom.clone(),
|
||||||
};
|
};
|
||||||
(builder, client)
|
(builder, client)
|
||||||
}
|
}
|
||||||
@@ -131,18 +135,42 @@ impl EmulatorBuilder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn build(self) -> Result<Emulator> {
|
pub fn build(self) -> Result<Emulator> {
|
||||||
let mut emulator = Emulator::new(
|
let Self {
|
||||||
self.commands,
|
rom,
|
||||||
self.sim_state,
|
commands,
|
||||||
self.state,
|
sim_state,
|
||||||
self.audio_on,
|
state,
|
||||||
self.watch_rom,
|
audio_on,
|
||||||
self.linked,
|
watch_rom,
|
||||||
);
|
linked,
|
||||||
if let Some(path) = self.rom {
|
save_data_location,
|
||||||
|
start_paused,
|
||||||
|
} = self;
|
||||||
|
let mut emulator = Emulator {
|
||||||
|
sims: vec![],
|
||||||
|
carts: [None, None],
|
||||||
|
audio: Audio::init(),
|
||||||
|
commands,
|
||||||
|
sim_state,
|
||||||
|
state,
|
||||||
|
audio_on,
|
||||||
|
watch_rom,
|
||||||
|
linked,
|
||||||
|
save_data_location,
|
||||||
|
profilers: [None, None],
|
||||||
|
renderers: HashMap::new(),
|
||||||
|
messages: HashMap::new(),
|
||||||
|
debuggers: HashMap::new(),
|
||||||
|
stdouts: HashMap::new(),
|
||||||
|
watched_regions: HashMap::new(),
|
||||||
|
eye_contents: [vec![0u8; 384 * 224 * 2], vec![0u8; 384 * 224 * 2]],
|
||||||
|
audio_samples: Vec::with_capacity(EXPECTED_FRAME_SIZE),
|
||||||
|
buffer: vec![],
|
||||||
|
};
|
||||||
|
if let Some(path) = rom {
|
||||||
emulator.load_cart(SimId::Player1, &path)?;
|
emulator.load_cart(SimId::Player1, &path)?;
|
||||||
}
|
}
|
||||||
if self.start_paused {
|
if start_paused {
|
||||||
emulator.pause_sims()?;
|
emulator.pause_sims()?;
|
||||||
}
|
}
|
||||||
Ok(emulator)
|
Ok(emulator)
|
||||||
@@ -159,6 +187,7 @@ pub struct Emulator {
|
|||||||
audio_on: Arc<[AtomicBool; 2]>,
|
audio_on: Arc<[AtomicBool; 2]>,
|
||||||
watch_rom: Arc<[AtomicBool; 2]>,
|
watch_rom: Arc<[AtomicBool; 2]>,
|
||||||
linked: Arc<AtomicBool>,
|
linked: Arc<AtomicBool>,
|
||||||
|
save_data_location: Arc<Atomic<SaveDataLocation>>,
|
||||||
profilers: [Option<ProfileSender>; 2],
|
profilers: [Option<ProfileSender>; 2],
|
||||||
renderers: HashMap<SimId, TextureSink>,
|
renderers: HashMap<SimId, TextureSink>,
|
||||||
messages: HashMap<SimId, mpsc::Sender<Toast>>,
|
messages: HashMap<SimId, mpsc::Sender<Toast>>,
|
||||||
@@ -171,36 +200,6 @@ pub struct Emulator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Emulator {
|
impl Emulator {
|
||||||
fn new(
|
|
||||||
commands: mpsc::Receiver<EmulatorCommand>,
|
|
||||||
sim_state: Arc<[Atomic<SimState>; 2]>,
|
|
||||||
state: Arc<Atomic<EmulatorState>>,
|
|
||||||
audio_on: Arc<[AtomicBool; 2]>,
|
|
||||||
watch_rom: Arc<[AtomicBool; 2]>,
|
|
||||||
linked: Arc<AtomicBool>,
|
|
||||||
) -> Self {
|
|
||||||
Self {
|
|
||||||
sims: vec![],
|
|
||||||
carts: [None, None],
|
|
||||||
audio: Audio::init(),
|
|
||||||
commands,
|
|
||||||
sim_state,
|
|
||||||
state,
|
|
||||||
audio_on,
|
|
||||||
watch_rom,
|
|
||||||
linked,
|
|
||||||
profilers: [None, None],
|
|
||||||
renderers: HashMap::new(),
|
|
||||||
messages: HashMap::new(),
|
|
||||||
debuggers: HashMap::new(),
|
|
||||||
stdouts: HashMap::new(),
|
|
||||||
watched_regions: HashMap::new(),
|
|
||||||
eye_contents: [vec![0u8; 384 * 224 * 2], vec![0u8; 384 * 224 * 2]],
|
|
||||||
audio_samples: Vec::with_capacity(EXPECTED_FRAME_SIZE),
|
|
||||||
buffer: vec![],
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn reload_cart(&mut self, sim_id: SimId) -> Result<()> {
|
pub fn reload_cart(&mut self, sim_id: SimId) -> Result<()> {
|
||||||
let Some(cart) = &self.carts[sim_id.to_index()] else {
|
let Some(cart) = &self.carts[sim_id.to_index()] else {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
@@ -211,7 +210,12 @@ impl Emulator {
|
|||||||
|
|
||||||
pub fn load_cart(&mut self, sim_id: SimId, path: &Path) -> Result<()> {
|
pub fn load_cart(&mut self, sim_id: SimId, path: &Path) -> Result<()> {
|
||||||
let watch = self.watch_rom[sim_id.to_index()].load(Ordering::Acquire);
|
let watch = self.watch_rom[sim_id.to_index()].load(Ordering::Acquire);
|
||||||
let cart = Cart::load(path, sim_id, watch)?;
|
let cart = Cart::load(
|
||||||
|
path,
|
||||||
|
sim_id,
|
||||||
|
self.save_data_location.load(Ordering::Acquire),
|
||||||
|
watch,
|
||||||
|
)?;
|
||||||
self.try_reset_sim(sim_id, Some(cart))?;
|
self.try_reset_sim(sim_id, Some(cart))?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -224,7 +228,12 @@ impl Emulator {
|
|||||||
};
|
};
|
||||||
let watch = self.watch_rom[SimId::Player2.to_index()].load(Ordering::Acquire);
|
let watch = self.watch_rom[SimId::Player2.to_index()].load(Ordering::Acquire);
|
||||||
let cart = match file_path {
|
let cart = match file_path {
|
||||||
Some(rom_path) => Some(Cart::load(&rom_path, SimId::Player2, watch)?),
|
Some(rom_path) => Some(Cart::load(
|
||||||
|
&rom_path,
|
||||||
|
SimId::Player2,
|
||||||
|
self.save_data_location.load(Ordering::Acquire),
|
||||||
|
watch,
|
||||||
|
)?),
|
||||||
None => None,
|
None => None,
|
||||||
};
|
};
|
||||||
self.try_reset_sim(SimId::Player2, cart)?;
|
self.try_reset_sim(SimId::Player2, cart)?;
|
||||||
@@ -687,6 +696,20 @@ impl Emulator {
|
|||||||
cart.stop_watching();
|
cart.stop_watching();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
EmulatorCommand::SetSaveDataLocation(save_data_location) => {
|
||||||
|
self.save_data_location
|
||||||
|
.store(save_data_location, Ordering::Release);
|
||||||
|
for sim_id in SimId::values() {
|
||||||
|
if let Some(cart) = self.carts[sim_id.to_index()].as_mut()
|
||||||
|
&& let Err(error) = cart.change_save_data_location(save_data_location)
|
||||||
|
{
|
||||||
|
self.report_error(
|
||||||
|
sim_id,
|
||||||
|
format!("Error changing save data location: {error}"),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
EmulatorCommand::StartSecondSim(path) => {
|
EmulatorCommand::StartSecondSim(path) => {
|
||||||
if let Err(error) = self.start_second_sim(path) {
|
if let Err(error) = self.start_second_sim(path) {
|
||||||
self.report_error(
|
self.report_error(
|
||||||
@@ -854,6 +877,7 @@ pub enum EmulatorCommand {
|
|||||||
LoadGame(SimId, PathBuf),
|
LoadGame(SimId, PathBuf),
|
||||||
ReloadRom(SimId),
|
ReloadRom(SimId),
|
||||||
WatchRom(SimId, bool),
|
WatchRom(SimId, bool),
|
||||||
|
SetSaveDataLocation(SaveDataLocation),
|
||||||
StartSecondSim(Option<PathBuf>),
|
StartSecondSim(Option<PathBuf>),
|
||||||
StopSecondSim,
|
StopSecondSim,
|
||||||
Pause,
|
Pause,
|
||||||
@@ -943,6 +967,7 @@ pub struct EmulatorClient {
|
|||||||
sim_state: Arc<[Atomic<SimState>; 2]>,
|
sim_state: Arc<[Atomic<SimState>; 2]>,
|
||||||
state: Arc<Atomic<EmulatorState>>,
|
state: Arc<Atomic<EmulatorState>>,
|
||||||
linked: Arc<AtomicBool>,
|
linked: Arc<AtomicBool>,
|
||||||
|
save_data_location: Arc<Atomic<SaveDataLocation>>,
|
||||||
watch_rom: Arc<[AtomicBool; 2]>,
|
watch_rom: Arc<[AtomicBool; 2]>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -959,6 +984,9 @@ impl EmulatorClient {
|
|||||||
pub fn are_sims_linked(&self) -> bool {
|
pub fn are_sims_linked(&self) -> bool {
|
||||||
self.linked.load(Ordering::Acquire)
|
self.linked.load(Ordering::Acquire)
|
||||||
}
|
}
|
||||||
|
pub fn save_data_location(&self) -> SaveDataLocation {
|
||||||
|
self.save_data_location.load(Ordering::Acquire)
|
||||||
|
}
|
||||||
pub fn is_rom_watched(&self, sim_id: SimId) -> bool {
|
pub fn is_rom_watched(&self, sim_id: SimId) -> bool {
|
||||||
self.watch_rom[sim_id.to_index()].load(Ordering::Acquire)
|
self.watch_rom[sim_id.to_index()].load(Ordering::Acquire)
|
||||||
}
|
}
|
||||||
|
|||||||
+39
-7
@@ -8,10 +8,15 @@ use std::{
|
|||||||
sync::{Arc, atomic::AtomicBool},
|
sync::{Arc, atomic::AtomicBool},
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::emulator::{SimId, game_info::GameInfo, shrooms_vb_util::rom_from_isx};
|
use crate::{
|
||||||
|
config::SaveDataLocation,
|
||||||
|
emulator::{SimId, game_info::GameInfo, shrooms_vb_util::rom_from_isx},
|
||||||
|
filesystem,
|
||||||
|
};
|
||||||
|
|
||||||
pub struct Cart {
|
pub struct Cart {
|
||||||
pub file_path: PathBuf,
|
pub file_path: PathBuf,
|
||||||
|
sim_id: SimId,
|
||||||
pub rom: Vec<u8>,
|
pub rom: Vec<u8>,
|
||||||
sram_file: File,
|
sram_file: File,
|
||||||
pub sram: Vec<u8>,
|
pub sram: Vec<u8>,
|
||||||
@@ -21,7 +26,12 @@ pub struct Cart {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Cart {
|
impl Cart {
|
||||||
pub fn load(file_path: &Path, sim_id: SimId, watch: bool) -> Result<Self> {
|
pub fn load(
|
||||||
|
file_path: &Path,
|
||||||
|
sim_id: SimId,
|
||||||
|
save_data_location: SaveDataLocation,
|
||||||
|
watch: bool,
|
||||||
|
) -> Result<Self> {
|
||||||
let (rom, info) = Self::read_rom(file_path)?;
|
let (rom, info) = Self::read_rom(file_path)?;
|
||||||
|
|
||||||
let mut sram_file = File::options()
|
let mut sram_file = File::options()
|
||||||
@@ -29,7 +39,7 @@ impl Cart {
|
|||||||
.write(true)
|
.write(true)
|
||||||
.create(true)
|
.create(true)
|
||||||
.truncate(false)
|
.truncate(false)
|
||||||
.open(sram_path(file_path, sim_id))?;
|
.open(sram_path(file_path, sim_id, save_data_location))?;
|
||||||
|
|
||||||
let sram = if sram_file.metadata()?.len() == 0 {
|
let sram = if sram_file.metadata()?.len() == 0 {
|
||||||
// new SRAM file, randomize the contents
|
// new SRAM file, randomize the contents
|
||||||
@@ -54,6 +64,7 @@ impl Cart {
|
|||||||
|
|
||||||
Ok(Cart {
|
Ok(Cart {
|
||||||
file_path: file_path.to_path_buf(),
|
file_path: file_path.to_path_buf(),
|
||||||
|
sim_id,
|
||||||
rom,
|
rom,
|
||||||
sram_file,
|
sram_file,
|
||||||
sram,
|
sram,
|
||||||
@@ -73,6 +84,19 @@ impl Cart {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn change_save_data_location(
|
||||||
|
&mut self,
|
||||||
|
save_data_location: SaveDataLocation,
|
||||||
|
) -> Result<()> {
|
||||||
|
self.sram_file = File::options()
|
||||||
|
.read(true)
|
||||||
|
.write(true)
|
||||||
|
.create(true)
|
||||||
|
.truncate(false)
|
||||||
|
.open(sram_path(&self.file_path, self.sim_id, save_data_location))?;
|
||||||
|
self.save_sram()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn save_sram(&mut self) -> Result<()> {
|
pub fn save_sram(&mut self) -> Result<()> {
|
||||||
self.sram_file.seek(SeekFrom::Start(0))?;
|
self.sram_file.seek(SeekFrom::Start(0))?;
|
||||||
self.sram_file.write_all(&self.sram)?;
|
self.sram_file.write_all(&self.sram)?;
|
||||||
@@ -177,9 +201,17 @@ fn parse_elf_program<Elf: object::read::elf::FileHeader<Endian = object::Endiann
|
|||||||
Some(bytes)
|
Some(bytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn sram_path(file_path: &Path, sim_id: SimId) -> PathBuf {
|
fn sram_path(file_path: &Path, sim_id: SimId, save_data_location: SaveDataLocation) -> PathBuf {
|
||||||
match sim_id {
|
let extension = match sim_id {
|
||||||
SimId::Player1 => file_path.with_extension("p1.sram"),
|
SimId::Player1 => "p1.sram",
|
||||||
SimId::Player2 => file_path.with_extension("p2.sram"),
|
SimId::Player2 => "p2.sram",
|
||||||
|
};
|
||||||
|
if let SaveDataLocation::DataDirectory = save_data_location
|
||||||
|
&& let Some(data_dir) = filesystem::init_data_dir()
|
||||||
|
&& let Some(rom_name) = file_path.file_name()
|
||||||
|
{
|
||||||
|
data_dir.join(rom_name).with_extension(extension)
|
||||||
|
} else {
|
||||||
|
file_path.with_extension(extension)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,52 @@
|
|||||||
|
use std::{fs, path::PathBuf};
|
||||||
|
|
||||||
|
use anyhow::{Result, bail};
|
||||||
|
use directories::ProjectDirs;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct Persistence {
|
||||||
|
config_dir: Option<PathBuf>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Persistence {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
config_dir: init_config_dir(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn save_config<T: Serialize>(&self, file: &str, data: &T) -> Result<()> {
|
||||||
|
if let Some(config_dir) = self.config_dir.as_ref() {
|
||||||
|
let bytes = serde_json::to_vec_pretty(data)?;
|
||||||
|
let filename = config_dir.join(file).with_extension("json");
|
||||||
|
fs::write(&filename, bytes)?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn load_config<T: for<'a> Deserialize<'a>>(&self, file: &str) -> Result<T> {
|
||||||
|
let Some(config_dir) = self.config_dir.as_ref() else {
|
||||||
|
bail!("config directory not found");
|
||||||
|
};
|
||||||
|
let filename = config_dir.join(file).with_extension("json");
|
||||||
|
let bytes = fs::read(filename)?;
|
||||||
|
Ok(serde_json::from_slice(&bytes)?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn init_config_dir() -> Option<PathBuf> {
|
||||||
|
let config_dir = project_dirs()?.config_dir().to_path_buf();
|
||||||
|
fs::create_dir_all(&config_dir).ok()?;
|
||||||
|
Some(config_dir)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn init_data_dir() -> Option<PathBuf> {
|
||||||
|
let data_dir = project_dirs()?.data_dir().to_path_buf();
|
||||||
|
fs::create_dir_all(&data_dir).ok()?;
|
||||||
|
Some(data_dir)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn project_dirs() -> Option<ProjectDirs> {
|
||||||
|
ProjectDirs::from("com", "virtual-boy", "Lemur")
|
||||||
|
}
|
||||||
+1
-1
@@ -14,7 +14,7 @@ use winit::keyboard::{KeyCode, PhysicalKey};
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
emulator::{SimId, VBKey},
|
emulator::{SimId, VBKey},
|
||||||
persistence::Persistence,
|
filesystem::Persistence,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Hash)]
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||||
|
|||||||
+11
-15
@@ -15,7 +15,7 @@ use winit::event_loop::{ControlFlow, EventLoop};
|
|||||||
use crate::{
|
use crate::{
|
||||||
config::{AppConfig, CliArgs, SimConfig},
|
config::{AppConfig, CliArgs, SimConfig},
|
||||||
emulator::SimId,
|
emulator::SimId,
|
||||||
persistence::Persistence,
|
filesystem::Persistence,
|
||||||
};
|
};
|
||||||
|
|
||||||
mod app;
|
mod app;
|
||||||
@@ -24,12 +24,12 @@ mod config;
|
|||||||
mod controller;
|
mod controller;
|
||||||
mod emulator;
|
mod emulator;
|
||||||
mod filepicker;
|
mod filepicker;
|
||||||
|
mod filesystem;
|
||||||
mod gdbserver;
|
mod gdbserver;
|
||||||
mod graphics;
|
mod graphics;
|
||||||
mod images;
|
mod images;
|
||||||
mod input;
|
mod input;
|
||||||
mod memory;
|
mod memory;
|
||||||
mod persistence;
|
|
||||||
mod profiler;
|
mod profiler;
|
||||||
mod window;
|
mod window;
|
||||||
|
|
||||||
@@ -60,14 +60,9 @@ fn set_panic_handler() {
|
|||||||
|
|
||||||
eprint!("{message}");
|
eprint!("{message}");
|
||||||
|
|
||||||
let Some(project_dirs) = directories::ProjectDirs::from("com", "virtual-boy", "Lemur")
|
let Some(data_dir) = filesystem::init_data_dir() else {
|
||||||
else {
|
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
let data_dir = project_dirs.data_dir();
|
|
||||||
if std::fs::create_dir_all(data_dir).is_err() {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
let timestamp = SystemTime::now()
|
let timestamp = SystemTime::now()
|
||||||
.duration_since(SystemTime::UNIX_EPOCH)
|
.duration_since(SystemTime::UNIX_EPOCH)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
@@ -98,7 +93,14 @@ fn main() -> Result<()> {
|
|||||||
|
|
||||||
let persistence = Persistence::new();
|
let persistence = Persistence::new();
|
||||||
|
|
||||||
let (mut builder, client) = EmulatorBuilder::new();
|
let mut config = AppConfig::load(&persistence);
|
||||||
|
if config.update(&args) {
|
||||||
|
let _ = config.save(&persistence);
|
||||||
|
}
|
||||||
|
let p1 = SimConfig::load(&persistence, SimId::Player1);
|
||||||
|
let p2 = SimConfig::load(&persistence, SimId::Player2);
|
||||||
|
|
||||||
|
let (mut builder, client) = EmulatorBuilder::new(config.save_data_location);
|
||||||
if let Some(path) = &args.rom {
|
if let Some(path) = &args.rom {
|
||||||
builder = builder.with_rom(path);
|
builder = builder.with_rom(path);
|
||||||
}
|
}
|
||||||
@@ -111,12 +113,6 @@ fn main() -> Result<()> {
|
|||||||
if args.profile {
|
if args.profile {
|
||||||
builder = builder.start_paused(true)
|
builder = builder.start_paused(true)
|
||||||
}
|
}
|
||||||
let mut config = AppConfig::load(&persistence);
|
|
||||||
if config.update(&args) {
|
|
||||||
let _ = config.save(&persistence);
|
|
||||||
}
|
|
||||||
let p1 = SimConfig::load(&persistence, SimId::Player1);
|
|
||||||
let p2 = SimConfig::load(&persistence, SimId::Player2);
|
|
||||||
|
|
||||||
let watch = args.watch;
|
let watch = args.watch;
|
||||||
|
|
||||||
|
|||||||
@@ -1,46 +0,0 @@
|
|||||||
use std::{fs, path::PathBuf};
|
|
||||||
|
|
||||||
use anyhow::{Result, bail};
|
|
||||||
use directories::ProjectDirs;
|
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct Persistence {
|
|
||||||
dirs: Option<Dirs>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Persistence {
|
|
||||||
pub fn new() -> Self {
|
|
||||||
Self { dirs: init_dirs() }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn save_config<T: Serialize>(&self, file: &str, data: &T) -> Result<()> {
|
|
||||||
if let Some(dirs) = self.dirs.as_ref() {
|
|
||||||
let bytes = serde_json::to_vec_pretty(data)?;
|
|
||||||
let filename = dirs.config_dir.join(file).with_extension("json");
|
|
||||||
fs::write(&filename, bytes)?;
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn load_config<T: for<'a> Deserialize<'a>>(&self, file: &str) -> Result<T> {
|
|
||||||
let Some(dirs) = self.dirs.as_ref() else {
|
|
||||||
bail!("config directory not found");
|
|
||||||
};
|
|
||||||
let filename = dirs.config_dir.join(file).with_extension("json");
|
|
||||||
let bytes = fs::read(filename)?;
|
|
||||||
Ok(serde_json::from_slice(&bytes)?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
struct Dirs {
|
|
||||||
config_dir: PathBuf,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn init_dirs() -> Option<Dirs> {
|
|
||||||
let dirs = ProjectDirs::from("com", "virtual-boy", "Lemur")?;
|
|
||||||
let config_dir = dirs.config_dir().to_path_buf();
|
|
||||||
fs::create_dir_all(&config_dir).ok()?;
|
|
||||||
Some(Dirs { config_dir })
|
|
||||||
}
|
|
||||||
+33
-2
@@ -7,13 +7,13 @@ use std::{
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
app::UserEvent,
|
app::UserEvent,
|
||||||
config::{COLOR_PRESETS, SimConfig},
|
config::{AppConfig, COLOR_PRESETS, SaveDataLocation, SimConfig},
|
||||||
emulator::{EmulatorClient, EmulatorCommand, EmulatorState, SimId, SimState},
|
emulator::{EmulatorClient, EmulatorCommand, EmulatorState, SimId, SimState},
|
||||||
filepicker::FilePicker,
|
filepicker::FilePicker,
|
||||||
|
filesystem::Persistence,
|
||||||
images::ImageTextureLoader,
|
images::ImageTextureLoader,
|
||||||
input::{Command, MappingProvider, ShortcutProvider},
|
input::{Command, MappingProvider, ShortcutProvider},
|
||||||
memory::MemoryClient,
|
memory::MemoryClient,
|
||||||
persistence::Persistence,
|
|
||||||
window::{
|
window::{
|
||||||
AboutWindow, BgMapWindow, CharacterDataWindow, FrameBufferWindow, GdbServerWindow,
|
AboutWindow, BgMapWindow, CharacterDataWindow, FrameBufferWindow, GdbServerWindow,
|
||||||
HotkeysWindow, InitArgs, InputWindow, ObjectWindow, ProfileWindow, RegisterWindow,
|
HotkeysWindow, InitArgs, InputWindow, ObjectWindow, ProfileWindow, RegisterWindow,
|
||||||
@@ -604,6 +604,37 @@ impl GameWindow {
|
|||||||
if ui.button("Hotkeys").clicked() {
|
if ui.button("Hotkeys").clicked() {
|
||||||
self.open(ChildWindow::Hotkeys);
|
self.open(ChildWindow::Hotkeys);
|
||||||
}
|
}
|
||||||
|
ui.menu_button("Save Data Location", |ui| {
|
||||||
|
let save_data_location = self.client.save_data_location();
|
||||||
|
let update_save_data_location = |new_location: SaveDataLocation| {
|
||||||
|
if new_location == save_data_location {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
let mut app_config = AppConfig::load(&self.persistence);
|
||||||
|
app_config.save_data_location = new_location;
|
||||||
|
let _ = app_config.save(&self.persistence);
|
||||||
|
self.client
|
||||||
|
.send_command(EmulatorCommand::SetSaveDataLocation(new_location));
|
||||||
|
};
|
||||||
|
if ui
|
||||||
|
.selectable_button(
|
||||||
|
save_data_location == SaveDataLocation::RomDirectory,
|
||||||
|
"Same Directory as ROM",
|
||||||
|
)
|
||||||
|
.clicked()
|
||||||
|
{
|
||||||
|
update_save_data_location(SaveDataLocation::RomDirectory);
|
||||||
|
}
|
||||||
|
if ui
|
||||||
|
.selectable_button(
|
||||||
|
save_data_location == SaveDataLocation::DataDirectory,
|
||||||
|
"App Data Directory",
|
||||||
|
)
|
||||||
|
.clicked()
|
||||||
|
{
|
||||||
|
update_save_data_location(SaveDataLocation::DataDirectory);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
fn show_color_picker(&mut self, ui: &mut Ui) {
|
fn show_color_picker(&mut self, ui: &mut Ui) {
|
||||||
|
|||||||
Reference in New Issue
Block a user