sitch to bevy_persistent to allow two clients

bevy_pkv was holding a file lock to prevent that
This commit is contained in:
2025-12-20 13:21:30 -05:00
parent 3901ee1174
commit f35275ab9f
4 changed files with 71 additions and 73 deletions

View File

@@ -1,19 +1,30 @@
use bevy::prelude::*;
use bevy_pkv::prelude::*;
use crate::{client::audio::SoundSettings, utils::Debounce};
use bevy::prelude::*;
use bevy_persistent::{Persistent, StorageFormat};
pub fn plugin(app: &mut App) {
#[cfg(not(feature = "dbg"))]
app.insert_resource(PkvStore::new("Rustunit", "HEDZ"));
app.insert_resource(
Persistent::<SoundSettings>::builder()
.name("audio")
.format(StorageFormat::Ron)
.path(
dirs::config_dir()
.unwrap()
.join("com.rustunit.hedzreloaded")
.join("audio.ron"),
)
.default(SoundSettings::default())
.build()
.unwrap(),
);
app.add_systems(Update, persist_settings.run_if(resource_exists::<PkvStore>));
app.add_systems(Startup, load_settings.run_if(resource_exists::<PkvStore>));
app.add_systems(Update, persist_settings);
app.add_systems(Startup, load_settings);
}
fn persist_settings(
settings: Res<SoundSettings>,
mut pkv: ResMut<PkvStore>,
mut persistent: ResMut<Persistent<SoundSettings>>,
mut debounce: Debounce<1000>,
) -> Result {
if settings.is_changed() {
@@ -21,16 +32,12 @@ fn persist_settings(
}
if debounce.finished() {
pkv.set("audio", &*settings)?;
persistent.set(*settings)?;
}
Ok(())
}
fn load_settings(mut settings: ResMut<SoundSettings>, pkv: Res<PkvStore>) -> Result {
if let Ok(loaded) = pkv.get::<SoundSettings>("audio") {
*settings = loaded;
}
Ok(())
fn load_settings(persistent: Res<Persistent<SoundSettings>>, mut settings: ResMut<SoundSettings>) {
*settings = *persistent.get();
}